Pepguy commited on
Commit
3937110
Β·
verified Β·
1 Parent(s): 6edd06d

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +1381 -397
app.js CHANGED
@@ -1,464 +1,1448 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import express from 'express';
2
  import cors from 'cors';
3
  import dotenv from 'dotenv';
4
- import OpenAI from "openai";
5
- import { BedrockRuntimeClient, ConverseCommand, ConverseStreamCommand } from "@aws-sdk/client-bedrock-runtime";
 
 
6
  import { NodeHttpHandler } from "@smithy/node-http-handler";
7
 
8
  dotenv.config();
9
- const app = express();
10
- const PORT = process.env.PORT || 7860;
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  app.use(cors());
13
- app.use(express.json({ limit: '50mb' }));
14
 
15
- // --- SYSTEM PROMPT DEFINITIONS ---
16
- const CLAUDE_SYSTEM_PROMPT = "You are a pro. Provide elite, high-level technical responses.";
17
- const GPT_SYSTEM_PROMPT = "You are a worker. Be concise, efficient, and get the job done.";
 
 
 
 
 
 
18
 
 
19
  const bedrockClient = new BedrockRuntimeClient({
20
- region: "us-east-1",
21
- requestHandler: new NodeHttpHandler({
22
- http2Handler: undefined,
23
- })
24
  });
25
 
26
- const azureOpenAI = new OpenAI({
27
- apiKey: "",
28
- baseURL: ``,
29
- defaultQuery: { "api-version": "2024-05-01-preview" },
30
- defaultHeaders: { "api-key": "" }
31
- });
32
-
33
- // --- DYNAMIC MODEL ROUTER ---
34
- function getBedrockModelId(modelName) {
35
- switch(modelName) {
36
- case "haiku":
37
- return "arn:aws:bedrock:us-east-1:106774395747:inference-profile/global.anthropic.claude-haiku-4-5-20251001-v1:0";
38
- case "maverick":
39
- return "arn:aws:bedrock:us-east-1:106774395747:inference-profile/us.meta.llama4-maverick-17b-instruct-v1:0";
40
- case "claude":
41
- default:
42
- return "arn:aws:bedrock:us-east-1:106774395747:inference-profile/global.anthropic.claude-sonnet-4-6";
43
- }
44
  }
45
 
46
- // --- NON-STREAMING ENDPOINT (UPDATED FOR VISION) ---
47
- app.post('/api/generate', async (req, res) => {
48
- // EXTRACT IMAGES HERE
49
- const { model, prompt, system_prompt, images } = req.body;
50
- console.log(`[TRAFFIC] Request for ${model} ${images?.length ? 'with images' : ''}`);
51
 
52
- try {
53
- if (model === "gpt" || model === "gpt-5-mini") {
54
- let messagesPayload =[
55
- { role: "system", content: system_prompt || GPT_SYSTEM_PROMPT }
56
- ];
57
-
58
- // VISION SUPPORT FOR AZURE OPENAI
59
- if (images && images.length > 0) {
60
- let userContent = [{ type: "text", text: prompt }];
61
- images.forEach(imgStr => {
62
- userContent.push({ type: "image_url", image_url: { url: imgStr } });
63
- });
64
- messagesPayload.push({ role: "user", content: userContent });
65
- } else {
66
- messagesPayload.push({ role: "user", content: prompt });
67
- }
68
 
69
- const response = await azureOpenAI.chat.completions.create({
70
- model: "gpt-5-mini",
71
- messages: messagesPayload,
72
- reasoning_effort: "high"
73
- });
74
-
75
- const totalTokens = response.usage ? response.usage.total_tokens : 0;
76
- res.json({ success: true, data: response.choices[0].message.content, usage: { totalTokenCount: totalTokens } });
77
 
78
- } else {
79
- // Handles Claude Sonnet, Claude Haiku, and Llama Maverick
80
- const bedrockModelId = getBedrockModelId(model);
81
-
82
- // VISION SUPPORT FOR AWS BEDROCK
83
- let contentBlock = [{ text: prompt }];
84
- if (images && images.length > 0) {
85
- const imageBlocks = images.map(imgStr => {
86
- const base64Data = imgStr.replace(/^data:image\/\w+;base64,/, "");
87
- return {
88
- image: {
89
- format: 'png', // Assuming normalized to PNG by frontend
90
- source: { bytes: Buffer.from(base64Data, 'base64') }
91
- }
92
- };
93
- });
94
- contentBlock = [...imageBlocks, ...contentBlock];
95
- }
96
 
97
- const command = new ConverseCommand({
98
- modelId: bedrockModelId,
99
- system:[{ text: system_prompt || CLAUDE_SYSTEM_PROMPT }],
100
- messages: [{ role: "user", content: contentBlock }],
101
-
102
- // Ensure maxTokens is large enough for reasoning + response
103
- inferenceConfig: {
104
- maxTokens: model.includes("haiku") ? 32000 : 4000,
105
- temperature: 1
106
- },
107
-
108
- performanceConfig: model.includes("maverick") ? { latency: "standard" } : undefined,
109
-
110
- additionalModelRequestFields: (function() {
111
- if (model.includes("haiku")) {
112
- return {
113
- reasoning_config: {
114
- type: "enabled",
115
- budget_tokens: 2048
116
- }
117
- };
118
- } else if (model.includes("claude")) {
119
- return {
120
- // thinking: { type: "adaptive" },
121
- output_config: { effort: "high" }
122
- };
123
- }
124
- return undefined;
125
- })()
126
- });
127
-
128
- const response = await bedrockClient.send(command);
129
-
130
- const text = response.output.message.content.find(b => b.text)?.text;
131
- const tokenUsage = response.usage ? (response.usage.inputTokens + response.usage.outputTokens) : 0;
132
 
133
- res.json({ success: true, data: text, usage: { totalTokenCount: tokenUsage } });
134
- }
135
- } catch (err) {
136
- console.error(`❌[${model?.toUpperCase() || 'UNKNOWN'} ERROR]:`, err.name, err.message);
137
- res.status(500).json({ success: false, error: `${err.name}: ${err.message}` });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  }
139
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
- // --- STREAMING ENDPOINT ---
142
- app.post('/api/stream', async (req, res) => {
143
- const { model, prompt, system_prompt, images } = req.body;
144
- console.log(`[STREAM] Request for ${model} ${images?.length ? 'with images' : ''}`);
145
 
146
- res.setHeader('Content-Type', 'text/plain; charset=utf-8');
147
- res.setHeader('Transfer-Encoding', 'chunked');
148
- res.setHeader('X-Accel-Buffering', 'no');
149
- res.flushHeaders();
 
150
 
151
- let totalTokenCount = 0;
 
 
 
 
 
152
 
153
- try {
154
- if (model === "gpt" || model === "gpt-5-mini") {
155
- let messagesPayload =[
156
- { role: "system", content: system_prompt || GPT_SYSTEM_PROMPT }
157
- ];
158
-
159
- let userContent =[];
160
- if (images && images.length > 0) {
161
- userContent.push({ type: "text", text: prompt });
162
- images.forEach(imgStr => {
163
- userContent.push({ type: "image_url", image_url: { url: imgStr } });
164
- });
165
- messagesPayload.push({ role: "user", content: userContent });
166
- } else {
167
- messagesPayload.push({ role: "user", content: prompt });
168
- }
169
 
170
- const stream = await azureOpenAI.chat.completions.create({
171
- model: "gpt-5-mini",
172
- messages: messagesPayload,
173
- reasoning_effort: "high",
174
- stream: true,
175
- stream_options: { include_usage: true }
176
- });
177
 
178
- for await (const chunk of stream) {
179
- const delta = chunk.choices[0]?.delta;
180
- if (delta?.reasoning_content) res.write(`__THINK__${delta.reasoning_content}`);
181
- else if (delta?.content) res.write(delta.content);
182
- if (chunk.usage) totalTokenCount = chunk.usage.total_tokens;
183
- }
184
-
185
- res.write(`__USAGE__${JSON.stringify({ totalTokenCount })}`);
186
- res.end();
187
 
188
- } else {
189
- const bedrockModelId = getBedrockModelId(model);
190
- let contentBlock = [{ text: prompt }];
191
-
192
- if (images && images.length > 0) {
193
- const imageBlocks = images.map(imgStr => {
194
- const base64Data = imgStr.replace(/^data:image\/\w+;base64,/, "");
195
- return {
196
- image: {
197
- format: 'png', // Assuming normalized to PNG by frontend
198
- source: { bytes: Buffer.from(base64Data, 'base64') }
199
- }
200
- };
201
- });
202
- contentBlock = [...imageBlocks, ...contentBlock];
203
- }
204
 
205
- const command = new ConverseStreamCommand({
206
- modelId: bedrockModelId,
207
- system:[{ text: system_prompt || CLAUDE_SYSTEM_PROMPT }],
208
- messages:[{ role: "user", content: contentBlock }],
209
- inferenceConfig: { maxTokens: 48000, temperature: 1 },
210
- additionalModelRequestFields: model.includes("claude") ? {
211
- thinking: { type: "adaptive" },
212
- output_config: { effort: "high" }
213
- } : undefined
214
- });
215
 
216
- const response = await bedrockClient.send(command);
217
-
218
- for await (const chunk of response.stream) {
219
- if (chunk.contentBlockDelta) {
220
- const delta = chunk.contentBlockDelta.delta;
221
- if (delta.reasoningContent && delta.reasoningContent.text) {
222
- res.write(`__THINK__${delta.reasoningContent.text}`);
223
- } else if (delta.text) {
224
- res.write(delta.text);
225
- }
226
- }
227
- if (chunk.metadata && chunk.metadata.usage) {
228
- totalTokenCount = (chunk.metadata.usage.inputTokens || 0) + (chunk.metadata.usage.outputTokens || 0);
229
- }
230
- }
231
-
232
- res.write(`__USAGE__${JSON.stringify({ totalTokenCount })}`);
233
- res.end();
234
- }
235
- } catch (err) {
236
- console.error(`❌ [STREAM ERROR]:`, err.message);
237
- res.write(`ERROR: ${err.message}`);
238
- res.end();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
  }
240
- });
 
 
 
241
 
242
- app.get('/', async (req, res) => { res.json({ success: true }); });
243
- app.listen(PORT, '0.0.0.0', () => console.log(`Main AI Agent live on port ${PORT}`));
 
 
 
 
 
 
244
 
 
245
 
246
- /* import express from 'express';
247
- import cors from 'cors';
248
- import dotenv from 'dotenv';
249
- import OpenAI from "openai";
250
- import { BedrockRuntimeClient, ConverseCommand, ConverseStreamCommand } from "@aws-sdk/client-bedrock-runtime";
251
- import { NodeHttpHandler } from "@smithy/node-http-handler";
252
 
253
- dotenv.config();
254
- const app = express();
255
- const PORT = process.env.PORT || 7860;
256
 
257
- app.use(cors());
258
- app.use(express.json({ limit: '50mb' }));
259
 
260
- // --- SYSTEM PROMPT DEFINITIONS ---
261
- const CLAUDE_SYSTEM_PROMPT = "You are a pro. Provide elite, high-level technical responses.";
262
- const GPT_SYSTEM_PROMPT = "You are a worker. Be concise, efficient, and get the job done.";
263
 
264
- const bedrockClient = new BedrockRuntimeClient({
265
- region: "us-east-1",
266
- requestHandler: new NodeHttpHandler({
267
- http2Handler: undefined,
268
- })
269
- });
 
 
 
 
 
 
 
 
270
 
271
- const azureOpenAI = new OpenAI({
272
- apiKey: "7U3m9NRkE38ThSWTr92hMgQ4hDCUFI9MAnFNrCgRL7MhdvckfTXwJQQJ99CBACHYHv6XJ3w3AAAAACOGV22P",
273
- baseURL: `https://hollowpad-resource.cognitiveservices.azure.com/openai/deployments/gpt-5-mini`,
274
- defaultQuery: { "api-version": "2024-05-01-preview" },
275
- defaultHeaders: { "api-key": "7U3m9NRkE38ThSWTr92hMgQ4hDCUFI9MAnFNrCgRL7MhdvckfTXwJQQJ99CBACHYHv6XJ3w3AAAAACOGV22P" }
276
- });
 
 
 
 
 
 
 
 
 
 
 
277
 
278
- // --- DYNAMIC MODEL ROUTER ---
279
- function getBedrockModelId(modelName) {
280
- switch(modelName) {
281
- case "haiku":
282
- return "arn:aws:bedrock:us-east-1:106774395747:inference-profile/global.anthropic.claude-haiku-4-5-20251001-v1:0"
283
- // return "arn:aws:bedrock:us-east-1:106774395747:inference-profile/global.anthropic.claude-haiku-4-5";
284
- case "maverick":
285
- // Standard Bedrock cross-region inference mapping for Llama
286
- // return "arn:aws:bedrock:us-east-1::foundation-model/meta.llama4-maverick-17b-instruct-v1:0";
287
- return "arn:aws:bedrock:us-east-1:106774395747:inference-profile/us.meta.llama4-maverick-17b-instruct-v1:0";
288
- case "claude":
289
- default:
290
- return "arn:aws:bedrock:us-east-1:106774395747:inference-profile/global.anthropic.claude-sonnet-4-6";
 
 
 
291
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  }
293
 
294
- // --- NON-STREAMING ENDPOINT ---
295
- app.post('/api/generate', async (req, res) => {
296
- const { model, prompt, system_prompt } = req.body;
297
- console.log(`[TRAFFIC] Request for ${model}`);
298
 
299
- try {
300
- if (model === "gpt" || model === "gpt-5-mini") {
301
- const response = await azureOpenAI.chat.completions.create({
302
- model: "gpt-5-mini",
303
- messages:[
304
- { role: "system", content: system_prompt || GPT_SYSTEM_PROMPT },
305
- { role: "user", content: prompt }
306
- ],
307
- reasoning_effort: "high"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
  });
309
-
310
- const totalTokens = response.usage ? response.usage.total_tokens : 0;
311
- res.json({ success: true, data: response.choices[0].message.content, usage: { totalTokenCount: totalTokens } });
312
-
313
- } else {
314
- // Handles Claude Sonnet, Claude Haiku, and Llama Maverick
315
- const bedrockModelId = getBedrockModelId(model);
316
-
317
-
318
- const command = new ConverseCommand({
319
- modelId: bedrockModelId,
320
- system: [{ text: system_prompt || CLAUDE_SYSTEM_PROMPT }],
321
- messages: [{ role: "user", content: [{ text: prompt }] }],
322
 
323
- // Ensure maxTokens is large enough for reasoning + response
324
- inferenceConfig: {
325
- maxTokens: model.includes("haiku") ? 32000 : 4000,
326
- temperature: 1
327
- },
328
-
329
- performanceConfig: model.includes("maverick") ? { latency: "standard" } : undefined,
330
-
331
- additionalModelRequestFields: (function() {
332
- if (model.includes("haiku")) {
333
- return {
334
- reasoning_config: {
335
- type: "enabled",
336
- budget_tokens: 2048 // As seen in your screenshot
337
- }
338
- };
339
- } else if (model.includes("claude")) {
340
- return {
341
- thinking: { type: "adaptive" },
342
- output_config: { effort: "high" }
343
- };
344
- }
345
- return undefined;
346
- })()
347
- });
348
-
349
- const response = await bedrockClient.send(command);
350
- const text = response.output.message.content.find(b => b.text)?.text;
351
- const tokenUsage = response.usage ? (response.usage.inputTokens + response.usage.outputTokens) : 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352
 
353
- res.json({ success: true, data: text, usage: { totalTokenCount: tokenUsage } });
 
 
 
 
 
 
 
354
  }
355
- } catch (err) {
356
- console.error(`❌[${model?.toUpperCase() || 'UNKNOWN'} ERROR]:`, err.name, err.message);
357
- res.status(500).json({ success: false, error: `${err.name}: ${err.message}` });
358
  }
359
- });
360
 
361
- // --- STREAMING ENDPOINT ---
362
- app.post('/api/stream', async (req, res) => {
363
- const { model, prompt, system_prompt, images } = req.body;
364
- console.log(`[STREAM] Request for ${model} ${images?.length ? 'with images' : ''}`);
365
 
366
- res.setHeader('Content-Type', 'text/plain; charset=utf-8');
367
- res.setHeader('Transfer-Encoding', 'chunked');
368
- res.setHeader('X-Accel-Buffering', 'no');
369
- res.flushHeaders();
 
 
 
 
 
370
 
371
- let totalTokenCount = 0;
 
 
372
 
373
  try {
374
- if (model === "gpt" || model === "gpt-5-mini") {
375
- let messagesPayload =[
376
- { role: "system", content: system_prompt || GPT_SYSTEM_PROMPT }
377
- ];
378
-
379
- let userContent =[];
380
- if (images && images.length > 0) {
381
- userContent.push({ type: "text", text: prompt });
382
- images.forEach(imgStr => {
383
- userContent.push({ type: "image_url", image_url: { url: imgStr } });
384
- });
385
- messagesPayload.push({ role: "user", content: userContent });
386
- } else {
387
- messagesPayload.push({ role: "user", content: prompt });
 
 
 
388
  }
 
 
 
 
389
 
390
- const stream = await azureOpenAI.chat.completions.create({
391
- model: "gpt-5-mini",
392
- messages: messagesPayload,
393
- reasoning_effort: "high",
394
- stream: true,
395
- stream_options: { include_usage: true }
396
- });
 
 
397
 
398
- for await (const chunk of stream) {
399
- const delta = chunk.choices[0]?.delta;
400
- if (delta?.reasoning_content) res.write(`__THINK__${delta.reasoning_content}`);
401
- else if (delta?.content) res.write(delta.content);
402
- if (chunk.usage) totalTokenCount = chunk.usage.total_tokens;
403
- }
404
-
405
- res.write(`__USAGE__${JSON.stringify({ totalTokenCount })}`);
406
- res.end();
407
 
408
- } else {
409
- const bedrockModelId = getBedrockModelId(model);
410
- let contentBlock = [{ text: prompt }];
411
-
412
- if (images && images.length > 0) {
413
- const imageBlocks = images.map(imgStr => {
414
- const base64Data = imgStr.replace(/^data:image\/\w+;base64,/, "");
415
- return {
416
- image: {
417
- format: 'png', // Assuming normalized to PNG by frontend
418
- source: { bytes: Buffer.from(base64Data, 'base64') }
419
- }
420
- };
421
- });
422
- contentBlock = [...imageBlocks, ...contentBlock];
423
- }
424
 
425
- const command = new ConverseStreamCommand({
426
- modelId: bedrockModelId,
427
- system:[{ text: system_prompt || CLAUDE_SYSTEM_PROMPT }],
428
- messages: [{ role: "user", content: contentBlock }],
429
- inferenceConfig: { maxTokens: 48000, temperature: 1 },
430
- additionalModelRequestFields: model.includes("claude") ? {
431
- thinking: { type: "adaptive" },
432
- output_config: { effort: "high" }
433
- } : undefined
434
- });
435
 
436
- const response = await bedrockClient.send(command);
437
-
438
- for await (const chunk of response.stream) {
439
- if (chunk.contentBlockDelta) {
440
- const delta = chunk.contentBlockDelta.delta;
441
- if (delta.reasoningContent && delta.reasoningContent.text) {
442
- res.write(`__THINK__${delta.reasoningContent.text}`);
443
- } else if (delta.text) {
444
- res.write(delta.text);
445
- }
446
- }
447
- if (chunk.metadata && chunk.metadata.usage) {
448
- totalTokenCount = (chunk.metadata.usage.inputTokens || 0) + (chunk.metadata.usage.outputTokens || 0);
 
 
 
 
 
 
 
 
 
 
 
 
449
  }
450
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451
 
452
- res.write(`__USAGE__${JSON.stringify({ totalTokenCount })}`);
453
- res.end();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454
  }
455
- } catch (err) {
456
- console.error(`❌ [STREAM ERROR]:`, err.message);
457
- res.write(`ERROR: ${err.message}`);
458
- res.end();
459
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460
  });
461
 
462
- app.get('/', async (req, res) => { res.json({ success: true }); });
463
- app.listen(PORT, '0.0.0.0', () => console.log(`Main AI Agent live on port ${PORT}`));
464
- */
 
1
+ /**
2
+ * axl_dataset_server.js
3
+ * ══════════════════════════════════════════════════════════════════════════════
4
+ * AXL Dataset Generation Server (Node.js/Express version)
5
+ * Generates UCE training samples via AWS Bedrock + synthetic math injection.
6
+ * Returns JSON for download. Tracks domain diversity across requests.
7
+ *
8
+ * Endpoints:
9
+ * GET /status β€” generation stats
10
+ * POST /generate?n=50&rps=3 β€” generate N samples, returns JSON array
11
+ * GET /dataset/download β€” download full accumulated dataset as .json
12
+ * POST /dataset/clear β€” wipe accumulated dataset
13
+ * GET /domains β€” list focus tracker state
14
+ * GET /dataset/sample?n=5 β€” preview samples
15
+ *
16
+ * Start: node axl_dataset_server.js
17
+ * ══════════════════════════════════════════════════════════════════════════════
18
+ */
19
+
20
  import express from 'express';
21
  import cors from 'cors';
22
  import dotenv from 'dotenv';
23
+ import fs from 'fs';
24
+ import path from 'path';
25
+ import crypto from 'crypto';
26
+ import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
27
  import { NodeHttpHandler } from "@smithy/node-http-handler";
28
 
29
  dotenv.config();
 
 
30
 
31
+ // ── LOGGING ──────────────────────────────────────────────────────────────────
32
+ function ts() {
33
+ const now = new Date();
34
+ return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}.${now.getMilliseconds().toString().padStart(3, '0').slice(0,3)}`;
35
+ }
36
+
37
+ const _LOG_LINES =[];
38
+
39
+ function log(msg, level = "INFO") {
40
+ const prefix = { "INFO": " ", "WARN": "⚠ ", "ERROR": "βœ– ", "OK": "βœ” ", "HEAD": "━━" }[level] || " ";
41
+ const line = `[${ts()}] ${prefix} ${msg}`;
42
+ console.log(line);
43
+ _LOG_LINES.push(line);
44
+ if (_LOG_LINES.length > 500) _LOG_LINES.shift();
45
+ }
46
+
47
+ log("Starting AXL Dataset Server in Node.js…", "HEAD");
48
+
49
+ // ── CONFIG ───────────────────────────────────────────────────────────────────
50
+ const app = express();
51
  app.use(cors());
52
+ app.use(express.json({ limit: '50mb' }));
53
 
54
+ const PORT = process.env.PORT || 8000;
55
+ const BEDROCK_REGION = process.env.AWS_REGION || "us-east-1";
56
+ const BEDROCK_MODEL_ID = process.env.BEDROCK_MODEL || "anthropic.claude-3-5-sonnet-20241022-v2:0";
57
+ const DATASET_PATH = path.resolve(process.env.DATASET_PATH || "axl_dataset.json");
58
+ const DEFAULT_RPS = parseFloat(process.env.DEFAULT_RPS || "3");
59
+ const MAX_RPS = parseFloat(process.env.MAX_RPS || "10");
60
+ const SAMPLES_PER_CALL = parseInt(process.env.SAMPLES_PER_CALL || "8");
61
+ const FOCUS_WINDOW = parseInt(process.env.FOCUS_WINDOW || "50");
62
+ const SCALE_K = 30.0;
63
 
64
+ // ── AWS BEDROCK CLIENT ───────────────────────────────────────────────────────
65
  const bedrockClient = new BedrockRuntimeClient({
66
+ region: BEDROCK_REGION,
67
+ requestHandler: new NodeHttpHandler({ http2Handler: undefined })
 
 
68
  });
69
 
70
+ async function callBedrock(prompt, maxTokens = 8000) {
71
+ const command = new ConverseCommand({
72
+ modelId: BEDROCK_MODEL_ID,
73
+ messages: [{ role: "user", content: [{ text: prompt }] }],
74
+ inferenceConfig: {
75
+ maxTokens: maxTokens,
76
+ temperature: 0.9,
77
+ topP: 0.95
78
+ }
79
+ });
80
+
81
+ const t0 = Date.now();
82
+ const response = await bedrockClient.send(command);
83
+ const text = response.output.message.content.find(b => b.text)?.text || "";
84
+ log(`Bedrock call completed in ${((Date.now() - t0) / 1000).toFixed(1)}s β€” ${text.length} chars`);
85
+ return text;
 
 
86
  }
87
 
88
+ // ── GUIDING PROMPT ───────────────────────────────────────────────────────────
89
+ const AXL_SPEC_PLACEHOLDER = `
90
+ # AXL β€” Abstraction Exchange Language
91
+ ### A Constraint-First, Bidirectionally Navigable Abstraction Language
92
+ ---
93
 
94
+ ## PHILOSOPHY
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ AXL treats existence as a consequence of constraint satisfaction.
97
+ Things exist not because they are declared, but because they are
98
+ the only things consistent with the active assumptions and bounds.
99
+ The language navigates upward (increasing abstraction, emergence)
100
+ and downward (reducing abstraction, collapse to concrete).
 
 
 
101
 
102
+ The system does not know biology, physics, or cryptography.
103
+ It knows only: what is bounded, what is tended toward, what is
104
+ observed, and what must follow. Domain knowledge lives in the
105
+ encoder. AXL is what survives after domain is stripped away.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
+ ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
+ ## PART I β€” FULL VOCABULARY
110
+
111
+ ### 1. SCOPE DECLARATION
112
+ Declares an abstraction level. Everything inside a scope inherits
113
+ its level tag. Scopes are numbered β€” lower is more fundamental.
114
+
115
+
116
+ SCOPE <name> [level: <int>]
117
+ ...contents...
118
+ END SCOPE
119
+
120
+
121
+ Example levels (not hardcoded, user defined):
122
+ - level 0 = most fundamental (quantum, bits)
123
+ - level 1 = composed structures (atoms, bytes)
124
+ - level 2 = functional systems (molecules, data blocks)
125
+ - level 3 = emergent behavior (cells, algorithms)
126
+ - level N = arbitrary abstraction ceiling
127
+
128
+ ---
129
+
130
+ ### 2. ENTITY
131
+ A thing that exists within a scope. Has fields, states, and
132
+ existence conditions. An entity does not exist unless its
133
+ REQUIRES clause is satisfied.
134
+
135
+
136
+ ENTITY <name>
137
+ FIELD <name>: <type> [BOUND <range>] [CONSERVES] [DISCRETE]
138
+ STATE <name>: REQUIRES <condition>
139
+ TENDS <field> <direction>
140
+ DERIVES FROM <entity_list>
141
+ END ENTITY
142
+
143
+
144
+ **FIELD** β€” a measurable or logical property of the entity
145
+ **CONSERVES** β€” this field cannot change across any TRANSFORM
146
+ **DISCRETE** β€” field only takes integer or enumerated values
147
+ **TENDS** β€” soft directional pull on a field (not a hard target)
148
+
149
+ ---
150
+
151
+ ### 3. ASSUME
152
+ Axiom declaration. Taken as true without derivation.
153
+ The foundation everything else builds on.
154
+ Multiple ASSUMEs compound β€” all must hold simultaneously.
155
+
156
+
157
+ ASSUME <statement>
158
+ ASSUME <entity> EXISTS
159
+ ASSUME <field> EQUALS <value>
160
+ ASSUME <relationship> HOLDS
161
+
162
+
163
+ ---
164
+
165
+ ### 4. BOUND
166
+ Hard constraint on a field or relationship.
167
+ Nothing outside a BOUND can exist in this scope.
168
+
169
+
170
+ BOUND <field> WITHIN [<min>, <max>]
171
+ BOUND <field> ABOVE <value>
172
+ BOUND <field> BELOW <value>
173
+ BOUND <field> EQUALS <value> # exact, no freedom
174
+ BOUND <entity> COUNT WITHIN [<min>, <max>]
175
+ BOUND <a> FORBIDS <b> # mutual exclusion
176
+
177
+
178
+ ---
179
+
180
+ ### 5. TEND
181
+ Soft objective. The system pulls toward this but does not
182
+ require it. Multiple TENDs create competing pressures that
183
+ produce equilibrium β€” emergence happens here.
184
+
185
+
186
+ TEND <field> MIN # pull toward minimum
187
+ TEND <field> MAX # pull toward maximum
188
+ TEND <field> TOWARD <value> # pull toward specific value
189
+ TEND <system> STABLE # resist perturbation
190
+ TEND <field> FOLLOW <other_field> # track another field
191
+
192
+
193
+ ---
194
+
195
+ ### 6. OBJECTIVE
196
+ Hard optimization target. Unlike TEND, this must be satisfied
197
+ for the scope to be considered resolved.
198
+
199
+
200
+ OBJECTIVE <name>
201
+ OPTIMIZE <field> [MIN | MAX | EQUAL <value>]
202
+ SUBJECT TO <constraint_list>
203
+ END OBJECTIVE
204
+
205
+
206
+ ---
207
+
208
+ ### 7. DERIVE
209
+ Declares that something follows necessarily from other things.
210
+ If the sources hold, the derived thing must hold.
211
+ This is the upward propagation engine.
212
+
213
+
214
+ DERIVE <entity | field | state>
215
+ FROM <source_list>
216
+ WHEN <condition>
217
+ BY <transform_name>
218
+ END DERIVE
219
+
220
+
221
+ ---
222
+
223
+ ### 8. TRANSFORM
224
+ A process that maps one configuration to another.
225
+ Transforms are the edges between scope levels.
226
+ They must declare what they CONSERVE and what they CHANGE.
227
+
228
+
229
+ TRANSFORM <name>
230
+ INPUT <entity_or_field_list>
231
+ OUTPUT <entity_or_field_list>
232
+ CONSERVES <field_list>
233
+ CHANGES <field_list>
234
+ REQUIRES <precondition>
235
+ PRODUCES <postcondition>
236
+ END TRANSFORM
237
+
238
+
239
+ ---
240
+
241
+ ### 9. OBSERVE
242
+ What has been measured. This is the B value in the UCE.
243
+ The system will work backwards from this to find what
244
+ configuration of unknowns is consistent with it.
245
+
246
+
247
+ OBSERVE <field | entity | state> EQUALS <value>
248
+ OBSERVE <field> WITHIN [<min>, <max>]
249
+ OBSERVE <pattern> IN <output>
250
+
251
+
252
+ ---
253
+
254
+ ### 10. UNKNOWN
255
+ What we are solving for. The C vector in the UCE.
256
+ The system navigates the abstraction domain to find
257
+ values of UNKNOWNs that satisfy all constraints
258
+ given the OBSERVATIONs.
259
+
260
+
261
+ UNKNOWN <name>: <type> BOUND [<min>, <max>]
262
+ UNKNOWN <name>: DISCRETE WITHIN <set>
263
+
264
+
265
+ ---
266
+
267
+ ### 11. COLLAPSE
268
+ Instruction to reduce abstraction β€” find the most concrete
269
+ configuration satisfying all active constraints.
270
+ This is reverse abstraction in one word.
271
+
272
+
273
+ COLLAPSE <scope | entity | system>
274
+ GIVEN <observation_list>
275
+ FIND <unknown_list>
276
+ CONFIDENCE <threshold>
277
+ END COLLAPSE
278
+
279
+
280
+ ---
281
+
282
+ ### 12. EXPAND
283
+ Opposite of COLLAPSE. Given a concrete configuration,
284
+ derive upward β€” what higher-level behaviors emerge?
285
+
286
+
287
+ EXPAND <entity | configuration>
288
+ THROUGH <scope_list>
289
+ OBSERVE_EMERGENT <property_list>
290
+ END EXPAND
291
+
292
+
293
+ ---
294
+
295
+ ### 13. CONTRADICT
296
+ Flags that two things cannot both be true simultaneously.
297
+ Not an error β€” a scope boundary marker.
298
+ Where contradictions appear is where the interesting
299
+ physics, biology, and logic lives.
300
+
301
+
302
+ CONTRADICT <statement_a> WITH <statement_b>
303
+ AT SCOPE <level>
304
+ BOUNDARY_TYPE [SCALE | SPEED | INFORMATION | ENERGY]
305
+
306
+
307
+ ---
308
+
309
+ ### 14. EMERGES
310
+ Declares a property that does not exist at lower scopes
311
+ but appears when lower entities compose.
312
+ Emergence is not reducible to its parts in AXL β€”
313
+ it must be explicitly declared.
314
+
315
+
316
+ EMERGES <property>
317
+ FROM COMPOSITION OF <entity_list>
318
+ AT SCOPE <level>
319
+ NOT PRESENT AT SCOPE <lower_level>
320
+
321
+
322
+ ---
323
+
324
+ ### 15. COMPOSES
325
+ Declares that an entity is built from other entities.
326
+ Composition respects all lower-scope constraints.
327
+
328
+
329
+ COMPOSES <higher_entity>
330
+ FROM <lower_entity_list>
331
+ BOUND count: <range>
332
+ CONSERVING <field_list>
333
+ END COMPOSES
334
+
335
+
336
+ ---
337
+
338
+ ### 16. STEP
339
+ Sequence counter for iterative or feedback processes.
340
+ Enables the system to learn groups across attempts.
341
+ Critical for the mining feedback loop.
342
+
343
+
344
+ STEP <n>
345
+ STATE <snapshot>
346
+ CANDIDATE <configuration>
347
+ DELTA <change_from_previous>
348
+ END STEP
349
+
350
+
351
+ ---
352
+
353
+ ### 17. CANDIDATE
354
+ A proposed solution configuration.
355
+ Multiple candidates can coexist until COLLAPSE selects.
356
+
357
+
358
+ CANDIDATE <n>
359
+ <field>: <value>
360
+ SCORE: <objective_value>
361
+ CONFIDENCE: <float 0-1>
362
+ END CANDIDATE
363
+
364
+
365
+ ---
366
+
367
+ ### 18. UNLESS
368
+ Conditional override. Higher scope can override lower scope
369
+ constraints under specific conditions.
370
+
371
+
372
+ UNLESS <condition>
373
+ OVERRIDE <constraint>
374
+ WITH <replacement>
375
+ END UNLESS
376
+
377
+
378
+ ---
379
+
380
+ ### 19. CONSERVES
381
+ Declares an invariant across the entire system regardless
382
+ of what transforms occur. Conservation laws in physics,
383
+ invariants in computation, conservation of mass β€” all CONSERVES.
384
+
385
+
386
+ CONSERVES <quantity> ACROSS <transform_list>
387
+ CONSERVES <quantity> WITHIN SCOPE <level>
388
+
389
+
390
+ ---
391
+
392
+ ### 20. SAMPLE
393
+ A specific observed instance used as training signal
394
+ for the diffusion engine. The raw material for learning.
395
+
396
+
397
+ SAMPLE <id>
398
+ INPUT <configuration>
399
+ OUTPUT <observed_value>
400
+ STEP <n>
401
+ END SAMPLE
402
+
403
+
404
+ ---
405
+
406
+ ## PART II β€” GRAMMAR RULES
407
+
408
+
409
+ program := scope_block+
410
+ scope_block := SCOPE name [level] body END SCOPE
411
+ body := statement*
412
+ statement := assume | entity | bound | tend | objective
413
+ | derive | transform | observe | unknown
414
+ | collapse | expand | contradict | emerges
415
+ | composes | step | candidate | conserves
416
+ | sample | unless
417
+
418
+ condition := field op value | entity EXISTS | state ACTIVE
419
+ | condition AND condition | condition OR condition
420
+ | NOT condition
421
+
422
+ range := [min, max] | {discrete_set}
423
+ direction := MIN | MAX | STABLE | TOWARD value | FOLLOW field
424
+ type := REAL | INTEGER | BOOLEAN | SYMBOLIC | VECTOR
425
+ op := EQUALS | ABOVE | BELOW | WITHIN | FORBIDS
426
+
427
+
428
+ ---
429
+
430
+ ## PART III β€” DEMONSTRATIONS
431
+
432
+ ---
433
+
434
+ ### DEMONSTRATION 1: THE ATOM
435
+ #### Encoding atomic structure from quantum scope upward
436
+
437
+ axl:
438
+ # ════════════════════════════════════════════════════
439
+ # ATOM SYSTEM
440
+ # Starting assumption: energy exists and is quantized
441
+ # ════════════════════════════════════════════════════
442
+
443
+ SCOPE quantum [level: 0]
444
+
445
+ ASSUME energy EXISTS
446
+ ASSUME energy DISCRETE # quantization axiom
447
+ ASSUME charge EXISTS
448
+ CONSERVES charge ACROSS ALL_TRANSFORMS
449
+ CONSERVES energy ACROSS ALL_TRANSFORMS UNLESS transform EQUALS radiation
450
+
451
+ ENTITY quark
452
+ FIELD charge: REAL BOUND [-1, 1] DISCRETE VALUES {-2/3, -1/3, 1/3, 2/3}
453
+ FIELD color: SYMBOLIC DISCRETE VALUES {red, green, blue, antired, antigreen, antiblue}
454
+ FIELD mass: REAL BOUND [0.002, 4.5] # GeV
455
+ STATE free: REQUIRES energy ABOVE confinement_threshold
456
+ STATE confined: REQUIRES energy BELOW confinement_threshold
457
+ TENDS state TOWARD confined # quarks want to be bound
458
+ END ENTITY
459
+
460
+ ENTITY gluon
461
+ FIELD charge: BOUND [0, 0] CONSERVES
462
+ FIELD color: SYMBOLIC # carries color charge
463
+ TENDS quark_binding MAX # gluons maximize binding
464
+ END ENTITY
465
+
466
+ BOUND quark COLOR_NEUTRAL IN any_free_composite # color confinement
467
+
468
+ END SCOPE
469
+
470
+
471
+ SCOPE hadron [level: 1]
472
+
473
+ ASSUME quantum SCOPE HOLDS # inherit all quantum rules
474
+
475
+ COMPOSES proton
476
+ FROM quark COUNT EQUALS 3
477
+ CONFIGURATION {up, up, down} # charge = 2/3+2/3-1/3 = +1
478
+ CONSERVING charge
479
+ BOUND total_charge EQUALS 1
480
+ BOUND color EQUALS neutral # must be colorless
481
+ END COMPOSES
482
+
483
+ COMPOSES neutron
484
+ FROM quark COUNT EQUALS 3
485
+ CONFIGURATION {up, down, down} # charge = 2/3-1/3-1/3 = 0
486
+ CONSERVING charge
487
+ BOUND total_charge EQUALS 0
488
+ BOUND color EQUALS neutral
489
+ END COMPOSES
490
+
491
+ EMERGES stability
492
+ FROM COMPOSITION OF {proton, neutron}
493
+ AT SCOPE hadron
494
+ NOT PRESENT AT SCOPE quantum
495
+ # individual quarks don't have nuclear stability β€” it emerges here
496
+
497
+ END SCOPE
498
+
499
+
500
+ SCOPE nucleus [level: 2]
501
+
502
+ ASSUME hadron SCOPE HOLDS
503
+
504
+ COMPOSES nucleus
505
+ FROM {proton, neutron}
506
+ BOUND proton_count WITHIN [1, 118] # known elements
507
+ BOUND neutron_count WITHIN [0, 177]
508
+ TEND binding_energy MAX # nucleus maximizes binding
509
+ TEND neutron_to_proton_ratio TOWARD 1.0 # stability tendency
510
+ END COMPOSES
511
+
512
+ ENTITY strong_force
513
+ FIELD range: BOUND [0, 3e-15] # femtometers β€” very short range
514
+ FIELD strength: REAL
515
+ TENDS nucleon_separation MIN # pulls nucleons together
516
+ END ENTITY
517
+
518
+ ENTITY weak_force
519
+ TRANSFORMS neutron INTO proton
520
+ WHEN neutron_to_proton_ratio ABOVE stable_ratio
521
+ PRODUCES {proton, electron, antineutrino} # beta decay
522
+ CONSERVING charge
523
+ CONSERVING lepton_number
524
+ END ENTITY
525
+
526
+ EMERGES atomic_number
527
+ FROM proton COUNT
528
+ AT SCOPE nucleus
529
+ # proton count alone determines element identity β€” pure emergence
530
+
531
+ END SCOPE
532
+
533
+
534
+ SCOPE atom [level: 3]
535
+
536
+ ASSUME nucleus SCOPE HOLDS
537
+
538
+ ENTITY electron
539
+ FIELD charge: BOUND [-1, -1] CONSERVES
540
+ FIELD mass: BOUND [9.109e-31, 9.109e-31] CONSERVES
541
+ FIELD spin: DISCRETE VALUES {-0.5, 0.5}
542
+ FIELD orbital: DISCRETE VALUES {s, p, d, f}
543
+ FIELD energy_level: INTEGER BOUND [1, 7]
544
+ TENDS energy MIN # electrons seek lowest energy
545
+ BOUND same_orbital_same_spin FORBIDS # Pauli exclusion principle
546
+ END ENTITY
547
+
548
+ COMPOSES atom
549
+ FROM {nucleus, electron}
550
+ BOUND electron_count EQUALS proton_count # neutral atom
551
+ TEND electron ORBITAL_FILL lowest_energy_first # Aufbau principle
552
+ END COMPOSES
553
+
554
+ EMERGES chemical_behavior
555
+ FROM valence_electron_configuration
556
+ AT SCOPE atom
557
+ NOT PRESENT AT SCOPE nucleus
558
+ # chemistry is purely a scope 3 emergence β€” nucleus knows nothing of it
559
+
560
+ EMERGES periodic_properties
561
+ FROM atomic_number MODULO orbital_filling_pattern
562
+ AT SCOPE atom
563
+
564
+ END SCOPE
565
+
566
+
567
+ # ══ COLLAPSE EXAMPLE: Given observed spectral lines, find element ══
568
+
569
+ OBSERVE spectral_emission EQUALS {656nm, 486nm, 434nm, 410nm}
570
+
571
+ UNKNOWN element: SYMBOLIC WITHIN periodic_table
572
+ UNKNOWN atomic_number: INTEGER BOUND [1, 118]
573
+ UNKNOWN electron_configuration: VECTOR
574
+
575
+ COLLAPSE atom
576
+ GIVEN {spectral_emission}
577
+ FIND {element, atomic_number, electron_configuration}
578
+ CONFIDENCE 0.95
579
+ END COLLAPSE
580
+
581
+ # Answer the system should derive: Hydrogen (Z=1)
582
+
583
+
584
+ ---
585
+
586
+ ### DEMONSTRATION 2: SHA-256 MINING
587
+ #### Encoding the constraint and the intelligent nonce search
588
+
589
+ axl:
590
+ # ════════════════════════════════════════════════════
591
+ # SHA-256 BITCOIN MINING SYSTEM
592
+ # Objective: find nonce such that hash < target
593
+ # The system does not know what SHA-256 is.
594
+ # It knows: there is a transform, it has an output,
595
+ # the output must satisfy a bound.
596
+ # ════════════════════════════════════════════════════
597
+
598
+ SCOPE bitfield [level: 0]
599
+
600
+ ASSUME bit EXISTS
601
+ ASSUME bit DISCRETE VALUES {0, 1}
602
+ ENTITY bit_array
603
+ FIELD length: INTEGER
604
+ FIELD value: VECTOR of bit
605
+ BOUND value COUNT EQUALS length
606
+ END ENTITY
607
+
608
+ END SCOPE
609
+
610
+
611
+ SCOPE sha256_internals [level: 1]
612
+
613
+ ASSUME bitfield SCOPE HOLDS
614
+
615
+ ENTITY message_schedule
616
+ FIELD W: VECTOR COUNT EQUALS 64 # 64 32-bit words
617
+ DERIVES FROM input_block
618
+ BY expansion_transform
619
+ END ENTITY
620
+
621
+ TRANSFORM round_function
622
+ INPUT {working_vars: VECTOR COUNT 8, W_i: bit_array}
623
+ OUTPUT {working_vars_next: VECTOR COUNT 8}
624
+ CONSERVES bit_count
625
+ CHANGES working_vars
626
+ # 64 applications of this = full SHA-256
627
+ # Each application: sigma, choice, majority, mod add
628
+ # AXL does not encode the arithmetic β€” that's the domain encoder's job
629
+ # AXL encodes only: this is a deterministic transform with known structure
630
+ END TRANSFORM
631
+
632
+ CONTRADICT input_a WITH input_b
633
+ AT SCOPE sha256_internals
634
+ BOUNDARY_TYPE INFORMATION
635
+ # Two different inputs CAN produce same output (collision)
636
+ # But finding one is computationally infeasible
637
+ # This contradiction is the security boundary
638
+
639
+ TRANSFORM sha256
640
+ INPUT {message: bit_array BOUND length WITHIN [0, 2^64]}
641
+ OUTPUT {digest: bit_array BOUND length EQUALS 256}
642
+ CONSERVES nothing # one-way: no conservation
643
+ CHANGES everything # avalanche: all output bits affected
644
+ REQUIRES input EXISTS
645
+ PRODUCES digest PSEUDO_RANDOM_FROM input
646
+ # PSEUDO_RANDOM_FROM is the key declaration:
647
+ # output is deterministic but statistically independent of input structure
648
+ END TRANSFORM
649
+
650
+ END SCOPE
651
+
652
+
653
+ SCOPE block_header [level: 2]
654
+
655
+ ASSUME sha256_internals SCOPE HOLDS
656
+
657
+ ENTITY block_header
658
+ FIELD version: INTEGER BOUND [1, 4]
659
+ FIELD prev_hash: bit_array BOUND length EQUALS 256 # GIVEN, fixed
660
+ FIELD merkle_root: bit_array BOUND length EQUALS 256 # GIVEN, fixed
661
+ FIELD timestamp: INTEGER # GIVEN, approximately fixed
662
+ FIELD bits: INTEGER # GIVEN, encodes target
663
+ FIELD nonce: INTEGER BOUND [0, 4294967295] # UNKNOWN β€” 32 bits of freedom
664
+ END ENTITY
665
+
666
+ DERIVE target
667
+ FROM bits
668
+ BY difficulty_decode_transform
669
+ # target = a 256-bit number. Valid hash must be below it.
670
+ # More leading zeros in target = harder problem
671
+
672
+ END SCOPE
673
+
674
+
675
+ SCOPE mining [level: 3]
676
+
677
+ ASSUME block_header SCOPE HOLDS
678
+
679
+ TRANSFORM double_sha256
680
+ INPUT {block_header}
681
+ OUTPUT {hash: bit_array BOUND length EQUALS 256}
682
+ BY {sha256 APPLIED_TWICE}
683
+ END TRANSFORM
684
+
685
+ OBJECTIVE valid_block
686
+ OPTIMIZE hash MIN # minimize hash value numerically
687
+ SUBJECT TO hash BELOW target # hard bound β€” must be satisfied
688
+ SUBJECT TO nonce WITHIN [0, 4294967295]
689
+ END OBJECTIVE
690
+
691
+ # ══ HERE IS THE INTELLIGENCE LAYER ══
692
+ # The nonce space has NO smooth gradient (SHA-256 destroys it)
693
+ # But the SEARCH SPACE has learnable structure:
694
+ # - which regions have been explored (coverage)
695
+ # - step count gives sequence context
696
+ # - candidate scoring gives relative ranking
697
+ # This is what the UCE learns β€” not the hash, but the search
698
+
699
+ UNKNOWN nonce: INTEGER BOUND [0, 4294967295]
700
+
701
+ STEP 0
702
+ STATE {nonce_space: unexplored}
703
+ CANDIDATE 0
704
+ nonce: SAMPLE uniform
705
+ SCORE: UNKNOWN
706
+ CONFIDENCE: 0.0
707
+ END CANDIDATE
708
+ END STEP
709
+
710
+ STEP N
711
+ STATE {
712
+ explored_regions: VECTOR, # coverage map
713
+ best_hash_so_far: bit_array,
714
+ distance_to_target: INTEGER,
715
+ step_count: N
716
  }
717
+ CANDIDATE 0..50 # 50 parallel candidates
718
+ nonce: DERIVES FROM {
719
+ explored_regions,
720
+ step_count,
721
+ best_hash_so_far
722
+ }
723
+ # The system learns: given search history,
724
+ # what nonce regions have NOT been tried
725
+ # and which historically neighboring regions
726
+ # produced closer hashes (within this block's structure)
727
+ SCORE: distance_to_target MIN
728
+ CONFIDENCE: LEARNS
729
+ END CANDIDATE
730
+ END STEP
731
 
732
+ OBSERVE hash BELOW target # this is the termination condition
 
 
 
733
 
734
+ COLLAPSE mining
735
+ GIVEN {block_header_fixed_fields, target, search_history}
736
+ FIND {nonce}
737
+ CONFIDENCE 0.99
738
+ END COLLAPSE
739
 
740
+ # ══ WHAT THE SYSTEM LEARNS ══
741
+ # Not: how to reverse SHA-256
742
+ # But: how to cover the nonce space intelligently
743
+ # avoiding redundant regions, clustering search
744
+ # around previously productive neighborhoods
745
+ # The intelligence is in the search, not the hash
746
 
747
+ END SCOPE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
748
 
 
 
 
 
 
 
 
749
 
750
+ ---
 
 
 
 
 
 
 
 
751
 
752
+ ### DEMONSTRATION 3: CELL DIVISION (MITOSIS)
753
+ #### Encoding life as constraint satisfaction
 
 
 
 
 
 
 
 
 
 
 
 
 
 
754
 
755
+ axl:
756
+ # ════════════════════════════════════════════════════
757
+ # CELL DIVISION SYSTEM
758
+ # Life as constraints on information replication
759
+ # with error correction and state machines
760
+ # ════════════════════════════════════════════════════
 
 
 
 
761
 
762
+ SCOPE molecular [level: 0]
763
+
764
+ ASSUME molecule EXISTS
765
+ ASSUME information CAN BE ENCODED IN molecule
766
+ ASSUME chemical_bond EXISTS
767
+ CONSERVES atom_count ACROSS chemical_reactions
768
+ CONSERVES charge ACROSS chemical_reactions
769
+
770
+ ENTITY nucleotide
771
+ FIELD base: SYMBOLIC DISCRETE VALUES {adenine, thymine, guanine, cytosine}
772
+ FIELD sugar: SYMBOLIC EQUALS deoxyribose
773
+ FIELD phosphate: BOOLEAN
774
+ BOUND adenine PAIRS_WITH thymine ONLY # base pairing rule
775
+ BOUND guanine PAIRS_WITH cytosine ONLY
776
+ END ENTITY
777
+
778
+ ENTITY dna_strand
779
+ FIELD sequence: VECTOR of nucleotide
780
+ FIELD length: INTEGER BOUND [1, 3e9] # up to 3 billion base pairs
781
+ FIELD direction: SYMBOLIC DISCRETE VALUES {5_to_3, 3_to_5}
782
+ CONSERVES sequence UNLESS transform EQUALS mutation
783
+ TENDS base_pair_bonds STABLE # double helix stability
784
+ END ENTITY
785
+
786
+ TRANSFORM base_pairing
787
+ INPUT {strand_a: dna_strand, strand_b: dna_strand}
788
+ OUTPUT {double_helix}
789
+ REQUIRES strand_a.direction FORBIDS strand_b.direction # antiparallel
790
+ REQUIRES each_base PAIRS_WITH complement
791
+ CONSERVES sequence_information
792
+ END TRANSFORM
793
+
794
+ END SCOPE
795
+
796
+
797
+ SCOPE genome [level: 1]
798
+
799
+ ASSUME molecular SCOPE HOLDS
800
+
801
+ ENTITY chromosome
802
+ FIELD dna: dna_strand
803
+ FIELD histone_proteins: VECTOR
804
+ FIELD centromere_position: INTEGER
805
+ FIELD telomere_length: INTEGER BOUND [0, 15000] # erosion limit
806
+ TENDS compaction MAX WHEN cell_dividing EQUALS true
807
+ TENDS compaction MIN WHEN cell_active EQUALS true
808
+ END ENTITY
809
+
810
+ ENTITY genome
811
+ FIELD chromosomes: VECTOR of chromosome
812
+ BOUND chromosome_count EQUALS 46 # human diploid β€” pairs of 23
813
+ CONSERVES chromosome_count UNLESS transform EQUALS meiosis
814
+ TENDS integrity MAX # genome resists damage
815
+ END ENTITY
816
+
817
+ ENTITY dna_polymerase
818
+ FIELD error_rate: REAL BOUND [1e-9, 1e-9] # one error per billion bases
819
+ TENDS accuracy MAX
820
+ TRANSFORMS dna_strand INTO dna_strand_copy
821
+ CONSERVES sequence UNLESS error_rate TRIGGERS
822
+ END ENTITY
823
+
824
+ EMERGES genetic_code
825
+ FROM nucleotide_triplet_sequence
826
+ AT SCOPE genome
827
+ NOT PRESENT AT SCOPE molecular
828
+ # individual nucleotides have no meaning β€” codons emerge at this level
829
+
830
+ END SCOPE
831
+
832
+
833
+ SCOPE cell [level: 2]
834
+
835
+ ASSUME genome SCOPE HOLDS
836
+
837
+ ENTITY cell
838
+ FIELD genome: genome
839
+ FIELD membrane: BOOLEAN EQUALS true # boundary condition
840
+ FIELD atp_level: REAL BOUND [0, 1] # energy state
841
+ FIELD size: REAL BOUND [minimum_viable, maximum_before_division]
842
+ FIELD age: INTEGER # division counter
843
+ FIELD state: SYMBOLIC DISCRETE VALUES {
844
+ G1, # growth phase 1
845
+ S, # synthesis (DNA replication)
846
+ G2, # growth phase 2
847
+ M, # mitosis (division)
848
+ G0, # quiescent (resting)
849
+ apoptosis # programmed death
850
  }
851
+ TENDS atp_level MAX # cells maximize energy
852
+ TENDS genome_integrity MAX # cells protect DNA
853
+ BOUND age BELOW hayflick_limit # telomere erosion limit
854
+ END ENTITY
855
 
856
+ ENTITY checkpoint_protein
857
+ FIELD name: SYMBOLIC DISCRETE VALUES {p53, rb, cyclin_B, cdk1}
858
+ FIELD active: BOOLEAN
859
+ TENDS genome_damage OBSERVE # monitors DNA integrity
860
+ TRANSFORMS cell.state INTO apoptosis
861
+ WHEN genome_damage ABOVE repair_threshold
862
+ # p53 is the key constraint enforcer β€” if damage is too great, die
863
+ END ENTITY
864
 
865
+ END SCOPE
866
 
 
 
 
 
 
 
867
 
868
+ SCOPE mitosis [level: 3]
 
 
869
 
870
+ ASSUME cell SCOPE HOLDS
 
871
 
872
+ # ══ TRIGGER CONDITIONS ══
873
+ # Cell division is itself a constraint satisfaction problem:
874
+ # divide WHEN AND ONLY WHEN all conditions hold
875
 
876
+ OBJECTIVE division_trigger
877
+ OPTIMIZE cell.state EQUAL M
878
+ SUBJECT TO {
879
+ cell.size ABOVE division_threshold,
880
+ cell.atp_level ABOVE 0.7,
881
+ dna_replication EQUALS complete,
882
+ checkpoint_all EQUALS passed,
883
+ external_growth_signal EQUALS present,
884
+ cell.age BELOW hayflick_limit
885
+ }
886
+ # ALL conditions must hold β€” AND logic, not OR
887
+ # This is why cancer is a constraint violation:
888
+ # it occurs when checkpoint constraints are BROKEN
889
+ END OBJECTIVE
890
 
891
+ TRANSFORM prophase
892
+ INPUT {cell: STATE G2}
893
+ OUTPUT {cell: chromosomes_condensed, spindle_forming}
894
+ REQUIRES division_trigger EQUALS satisfied
895
+ CONSERVES chromosome_count
896
+ CONSERVES genome_content
897
+ END TRANSFORM
898
+
899
+ TRANSFORM metaphase
900
+ INPUT {cell: chromosomes_condensed}
901
+ OUTPUT {cell: chromosomes_aligned_at_plate}
902
+ REQUIRES spindle_attached_to EQUALS all_chromosomes
903
+ # Checkpoint: spindle assembly checkpoint
904
+ # If any chromosome unattached β€” PAUSE here
905
+ BOUND unattached_chromosomes EQUALS 0
906
+ UNLESS OVERRIDE apoptosis
907
+ END TRANSFORM
908
 
909
+ TRANSFORM anaphase
910
+ INPUT {cell: chromosomes_aligned}
911
+ OUTPUT {sister_chromatids: separating}
912
+ CONSERVES chromosome_count_total
913
+ CHANGES chromosome_location
914
+ PRODUCES two_sets OF 46_chromosomes
915
+ END TRANSFORM
916
+
917
+ TRANSFORM telophase_and_cytokinesis
918
+ INPUT {cell: chromatids_separated}
919
+ OUTPUT {daughter_cell_1, daughter_cell_2}
920
+ CONSERVES genome_content IN each_daughter
921
+ CONSERVES organelle_distribution APPROXIMATELY
922
+ PRODUCES {
923
+ daughter_cell_1: IDENTICAL_GENOME_TO parent,
924
+ daughter_cell_2: IDENTICAL_GENOME_TO parent
925
  }
926
+ END TRANSFORM
927
+
928
+ EMERGES tissue
929
+ FROM COMPOSITION OF {cell COUNT ABOVE 1}
930
+ AT SCOPE mitosis
931
+ NOT PRESENT AT SCOPE cell
932
+ # individual cells have no tissue identity β€” it emerges from composition
933
+
934
+ EMERGES cancer
935
+ FROM checkpoint_protein.active EQUALS false
936
+ AND division_trigger SATISFIED_WITHOUT all_conditions
937
+ AT SCOPE mitosis
938
+ # Cancer is not a new thing β€” it is constraint violation
939
+ # The cell divides when it should not because the objective
940
+ # function has been corrupted
941
+ CONTRADICT cancer WITH normal_division
942
+ AT SCOPE mitosis
943
+ BOUNDARY_TYPE INFORMATION # corrupted information in genome
944
+
945
+ # ══ COLLAPSE EXAMPLE: Cancer diagnosis ══
946
+
947
+ OBSERVE {
948
+ cell.division_rate ABOVE normal_range,
949
+ checkpoint_protein.p53 EQUALS inactive,
950
+ cell.age ABOVE hayflick_limit
951
+ }
952
+
953
+ UNKNOWN {
954
+ mutation_site: dna_strand LOCATION,
955
+ checkpoint_broken: SYMBOLIC,
956
+ division_trigger_violated: OBJECTIVE_CLAUSE
957
+ }
958
+
959
+ COLLAPSE mitosis
960
+ GIVEN {abnormal_observations}
961
+ FIND {mutation_site, checkpoint_broken, division_trigger_violated}
962
+ CONFIDENCE 0.90
963
+ END COLLAPSE
964
+
965
+ END SCOPE
966
+
967
+
968
+ # ════════════════════════════════════════════════════
969
+ # META: THE LANGUAGE DESCRIBING ITSELF
970
+ # AXL encoding its own structure
971
+ # ════════════════════════════════════════════════════
972
+
973
+ SCOPE axl_meta [level: 0]
974
+
975
+ ASSUME symbol EXISTS
976
+ ASSUME symbol CAN CARRY meaning
977
+ ASSUME meaning IS RELATIVE TO scope
978
+
979
+ ENTITY keyword
980
+ FIELD token: SYMBOLIC
981
+ FIELD semantic: SYMBOLIC
982
+ FIELD abstraction_direction: DISCRETE VALUES {up, down, both, none}
983
+ FIELD arity: INTEGER # how many arguments it takes
984
+ END ENTITY
985
+
986
+ ENTITY statement
987
+ FIELD keywords: VECTOR of keyword
988
+ FIELD scope_level: INTEGER
989
+ DERIVES meaning FROM {keywords, scope_level, context}
990
+ # Same token, different scope = different meaning
991
+ # BOUND in scope 0 (physics) β‰  BOUND in scope 3 (biology)
992
+ END ENTITY
993
+
994
+ EMERGES program_meaning
995
+ FROM COMPOSITION OF {statement COUNT ABOVE 1}
996
+ AT SCOPE axl_meta
997
+ NOT PRESENT AT SCOPE symbol
998
+ # Individual symbols have no program meaning
999
+ # Meaning emerges from composition and scope
1000
+
1001
+ TEND expressiveness MAX
1002
+ TEND ambiguity MIN
1003
+ CONTRADICT expressiveness WITH ambiguity
1004
+ AT SCOPE axl_meta
1005
+ BOUNDARY_TYPE INFORMATION
1006
+ # All languages face this β€” AXL resolves it via scope tagging
1007
+
1008
+ END SCOPE
1009
+
1010
+ ---
1011
+
1012
+ ## PART IV β€” DESIGN NOTES
1013
+ **On contradictions:**
1014
+ Every CONTRADICT in these examples marks a real scientific boundary:
1015
+ - quantum/classical = scale boundary
1016
+ - cancer/normal = information boundary
1017
+ - expressiveness/ambiguity = the language's own limit
1018
+
1019
+ These are features, not bugs.
1020
+
1021
+ `; // USER: replace in prod
1022
+
1023
+ const SYSTEM_PROMPT_HEADER = `You are a dataset generator for the Universal Constraint Engine (UCE),
1024
+ a domain-agnostic constraint satisfaction and optimization system.
1025
+ The UCE is trained on AXL (Abstraction eXchange Language) programs.
1026
+ It learns to invert constraints β€” given an observed output B, find the input configuration C.
1027
+
1028
+ Below is the complete AXL language specification:
1029
+ `;
1030
+
1031
+ const SYSTEM_PROMPT_FOOTER = `
1032
+ ════════════════════════════════════════════════════════════════════════
1033
+ GENERATION TASK
1034
+ ════════════════════════════════════════════════════════════════════════
1035
+
1036
+ Generate exactly {n} training samples. Each sample encodes a real-world
1037
+ constraint problem in AXL and provides the ground-truth solution.
1038
+
1039
+ STRICT REQUIREMENTS:
1040
+ 1. Output ONLY a valid JSON array. No markdown, no backticks, no preamble.
1041
+ 2. Each sample must have ALL fields shown in the schema below.
1042
+ 3. b_raw and c_values must be COMPUTED EXACTLY β€” no placeholders.
1043
+ 4. Every sample must be in a DIFFERENT domain or structural pattern.
1044
+ 5. Vary: sense (EQUAL/MINIMIZE/MAXIMIZE/SELECT/TEND), n_vars (1-{max_vars}),
1045
+ abstraction depth, domain (physics/finance/biology/crypto/chemistry/
1046
+ engineering/logistics/game_theory/information_theory/ecology/etc.)
1047
+ 6. For SELECT samples: candidates is a non-empty list of alternative C configs.
1048
+ 7. For MINIMIZE/MAXIMIZE: c_values must actually be near the optimum.
1049
+ 8. Include at least one multi-scope (hierarchical) AXL program per batch.
1050
+ 9. Include at least one search/iterative problem using STEP and CANDIDATE.
1051
+ 10. Do NOT repeat domains recently used: {recent_domains}
1052
+
1053
+ JSON SCHEMA (output an array of these):
1054
+ {
1055
+ "id": "<uuid string>",
1056
+ "domain": "<domain name>",
1057
+ "subdomain": "<specific topic>",
1058
+ "sense": "<EQUAL|MINIMIZE|MAXIMIZE|SELECT|TEND>",
1059
+ "n_vars": <integer 1 to {max_vars}>,
1060
+ "vars_text": "VARS c0 IN[lo hi] c1 IN [lo hi] ...",
1061
+ "expr_text": "EXPR <AXL expression or multi-line AXL program>",
1062
+ "sense_text": "SENSE <EQUAL|MINIMIZE|MAXIMIZE|SELECT|TEND>",
1063
+ "b_raw": <float β€” the computed observed output>,
1064
+ "b_norm": <float β€” log-normalized b_raw>,
1065
+ "c_values": [<float>, ...],
1066
+ "mask":[1, 1, ..., 0, 0],
1067
+ "candidates":[],
1068
+ "step_count": 0,
1069
+ "axl_source": "<full AXL program as single string with \\n for newlines>",
1070
+ "metadata": {
1071
+ "description": "<one sentence what this problem represents>",
1072
+ "units": "<units of b_raw if applicable>",
1073
+ "difficulty": "<easy|medium|hard|expert>",
1074
+ "scope_depth": <integer β€” how many nested scopes>,
1075
+ "has_emergence": <boolean>,
1076
+ "has_contradiction": <boolean>
1077
+ }
1078
  }
1079
 
1080
+ IMPORTANT ON b_norm: compute as Math.sign(b_raw) * Math.log1p(Math.abs(b_raw)) / 30.0
 
 
 
1081
 
1082
+ OUTPUT THE JSON ARRAY NOW:`;
1083
+
1084
+ function buildPrompt(n, recentDomains, maxVars = 20) {
1085
+ const spec = AXL_SPEC_PLACEHOLDER;
1086
+ const recentStr = recentDomains.length > 0 ? recentDomains.slice(-10).join(", ") : "none yet";
1087
+ let footer = SYSTEM_PROMPT_FOOTER.replace("{n}", n).replace("{max_vars}", maxVars).replace("{max_vars}", maxVars).replace("{recent_domains}", recentStr);
1088
+ return SYSTEM_PROMPT_HEADER + spec + footer;
1089
+ }
1090
+
1091
+ // ── MATH HELPERS ────────────────────────────────────────────────────────────
1092
+ const rnd = (min, max) => Math.random() * (max - min) + min;
1093
+ const randint = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
1094
+ const randchoice = (arr) => arr[Math.floor(Math.random() * arr.length)];
1095
+ const sign = (x) => x < 0 ? -1.0 : 1.0;
1096
+ function _safeNorm(bRaw) {
1097
+ if (!isFinite(bRaw) || isNaN(bRaw)) return 0.0;
1098
+ return sign(bRaw) * Math.log1p(Math.abs(bRaw)) / SCALE_K;
1099
+ }
1100
+
1101
+ // ── SYNTHETIC INJECTORS ─────────────────────────────────────────────────────
1102
+ function synthPhysicsSamples(n) {
1103
+ let samples = [];
1104
+ const templates = [["Kinetic Energy", c => 0.5 * (c[0]*100) * Math.pow(c[1]*50, 2), [[0.01,1], [0.01,1]], "Joules"],["Ohm's Law Power", c => Math.pow(c[0]*240, 2) / (c[1]*1000 + 0.001), [[0.01,1],[0.01,1]], "Watts"],
1105
+ ["Gravitational PE", c => (c[0]*100) * 9.81 * (c[1]*100), [[0.01,1],[0.01,1]], "Joules"],
1106
+ ["Ideal Gas PV=nRT", c => ((c[0]*10)*(c[1]*100))/((c[2]*5+0.1)*8.314), [[0.01,1],[0.01,1],[0.01,1]], "Kelvin"],["Wave Frequency", c => (c[0]*1e8) / (c[1]*10+0.001), [[0.01,1],[0.01,1]], "Hz"],["Snell Refraction", c => ((c[0]*2+1)*Math.sin(c[1]*1.5)) / ((c[2]*2+1)+0.001), [[0.01,1],[0.01,1],[0.01,1]], "dim-less"],["Centripetal Accel", c => Math.pow(c[0]*50, 2)/(c[1]*10+0.001), [[0.01,1],[0.01,1]], "m/sΒ²"],
1107
+ ["Doppler Shift", c => (c[0]*1000)*(1+(c[1]*50)/(340+0.001)), [[0.01,1],[0.01,1]], "Hz"]
1108
+ ];
1109
+
1110
+ for (let i = 0; i < n; i++) {
1111
+ let tmpl = randchoice(templates);
1112
+ let cVals = tmpl[2].map(r => rnd(r[0], r[1]));
1113
+ try {
1114
+ let bRaw = tmpl[1](cVals);
1115
+ if (!isFinite(bRaw) || isNaN(bRaw)) continue;
1116
+ let nVars = cVals.length;
1117
+ samples.push({
1118
+ id: crypto.randomUUID(), domain: "physics", subdomain: tmpl[0], sense: "EQUAL", n_vars: nVars,
1119
+ vars_text: "VARS " + cVals.map((_, i) => `c${i} IN [0 1]`).join(" "),
1120
+ expr_text: `EXPR ${tmpl[0].toUpperCase().replace(/ /g, "_")} n_vars=${nVars}`,
1121
+ sense_text: "SENSE EQUAL", b_raw: Number(bRaw.toFixed(6)), b_norm: Number(_safeNorm(bRaw).toFixed(8)),
1122
+ c_values: cVals.map(v => Number(v.toFixed(6))),
1123
+ mask:[...Array(nVars).fill(1), ...Array(Math.max(0, 4 - nVars)).fill(0)],
1124
+ candidates:[], step_count: 0,
1125
+ axl_source: `SCOPE physics [level: 2]\n OBSERVE ${tmpl[0]} EQUALS ${bRaw.toFixed(4)}\n UNKNOWN c: VECTOR BOUND[0,1]\nEND SCOPE`,
1126
+ metadata: { description: `${tmpl[0]} constraint inversion`, units: tmpl[3], difficulty: "medium", scope_depth: 1, has_emergence: false, has_contradiction: false }
1127
  });
1128
+ } catch (e) {}
1129
+ }
1130
+ return samples;
1131
+ }
1132
+
1133
+ function synthFinanceSamples(n) {
1134
+ let samples = [];
1135
+ const kinds =["compound_interest", "loan_payment", "portfolio_return", "options_delta"];
 
 
 
 
 
1136
 
1137
+ for (let i = 0; i < n; i++) {
1138
+ let kind = randchoice(kinds);
1139
+ try {
1140
+ let bRaw, cVals, desc;
1141
+ if (kind === "compound_interest") {
1142
+ let P = rnd(1000, 100000), r = rnd(0.01, 0.25), t = rnd(1, 30);
1143
+ bRaw = P * Math.exp(r * t); cVals =[P/100000, r/0.25, t/30]; desc = `Compound int: P=${P|0} r=${r.toFixed(3)} t=${t.toFixed(1)}y β†’ ${bRaw.toFixed(2)}`;
1144
+ } else if (kind === "loan_payment") {
1145
+ let P = rnd(50000, 500000), r = rnd(0.002, 0.02), np = randint(60, 360);
1146
+ bRaw = (P*r*Math.pow(1+r, np))/(Math.pow(1+r, np) - 1 + 1e-9); cVals =[P/500000, r/0.02, np/360]; desc = `Monthly loan payment: ${bRaw.toFixed(2)}`;
1147
+ } else if (kind === "portfolio_return") {
1148
+ let w = [rnd(0,1), rnd(0,1), rnd(0,1)]; let sum = w.reduce((a,b)=>a+b); w = w.map(x=>x/sum);
1149
+ let ret =[rnd(0.02, 0.25), rnd(0.02, 0.25), rnd(0.02, 0.25)];
1150
+ bRaw = w[0]*ret[0] + w[1]*ret[1] + w[2]*ret[2]; cVals = w; desc = `Portfolio weighted return: ${bRaw.toFixed(4)}`;
1151
+ } else {
1152
+ let S = rnd(50, 500), K = rnd(50, 500); bRaw = Math.max(0, S - K); cVals = [S/500, K/500]; desc = `Option intrinsic value: ${bRaw.toFixed(2)}`;
1153
+ }
1154
+ if (!isFinite(bRaw) || isNaN(bRaw)) continue;
1155
+ let nVars = cVals.length;
1156
+ samples.push({
1157
+ id: crypto.randomUUID(), domain: "finance", subdomain: kind, sense: "EQUAL", n_vars: nVars,
1158
+ vars_text: "VARS " + cVals.map((_, j) => `c${j} IN [0 1]`).join(" "), expr_text: `EXPR FINANCE_${kind.toUpperCase()}`,
1159
+ sense_text: "SENSE EQUAL", b_raw: Number(bRaw.toFixed(6)), b_norm: Number(_safeNorm(bRaw).toFixed(8)), c_values: cVals.map(v => Number(v.toFixed(6))),
1160
+ mask:[...Array(nVars).fill(1), ...Array(Math.max(0, 4 - nVars)).fill(0)], candidates:[], step_count: 0,
1161
+ axl_source: `SCOPE finance [level: 2]\n OBSERVE ${kind} EQUALS ${bRaw.toFixed(6)}\n UNKNOWN c: VECTOR BOUND[0,1]\nEND SCOPE`,
1162
+ metadata: { description: desc, units: "currency", difficulty: "medium", scope_depth: 1, has_emergence: false, has_contradiction: false }
1163
+ });
1164
+ } catch (e) {}
1165
+ }
1166
+ return samples;
1167
+ }
1168
+
1169
+ function synthCipherSamples(n) {
1170
+ let samples = [];
1171
+ const kinds =["caesar","vigenere_2","vigenere_4","affine","xor_byte"];
1172
+
1173
+ for (let i=0; i<n; i++) {
1174
+ let kind = randchoice(kinds);
1175
+ try {
1176
+ let bRaw, cVals, desc;
1177
+ if (kind === "caesar") {
1178
+ let shift = randint(0,25), p = randint(0,25); bRaw = (p+shift)%26; cVals = [shift/25.0]; desc = `Caesar shift=${shift} plain=${p} β†’ ${bRaw|0}`;
1179
+ } else if (kind === "vigenere_2") {
1180
+ let k =[randint(0,25), randint(0,25)], p = [randint(0,25), randint(0,25)];
1181
+ bRaw = ((p[0]+k[0])%26) + ((p[1]+k[1])%26); cVals =[k[0]/25.0, k[1]/25.0]; desc = `Vig2 key=${k}`;
1182
+ } else if (kind === "vigenere_4") {
1183
+ let k =[randint(0,25),randint(0,25),randint(0,25),randint(0,25)], p =[randint(0,25),randint(0,25),randint(0,25),randint(0,25)];
1184
+ bRaw = k.reduce((acc, _, idx) => acc + ((p[idx]+k[idx])%26), 0); cVals = k.map(v=>v/25.0); desc = `Vig4 key=${k}`;
1185
+ } else if (kind === "affine") {
1186
+ let valid_a =[1,3,5,7,9,11,15,17,19,21,23,25], a_idx = randint(0,11); let a = valid_a[a_idx], b = randint(0,25), p = randint(0,25);
1187
+ bRaw = (a*p+b)%26; cVals =[a_idx/11.0, b/25.0]; desc = `Affine a=${a} b=${b} plain=${p}`;
1188
+ } else {
1189
+ let key = randint(0,255), plain = randint(0,255); bRaw = plain ^ key; cVals =[key/255.0]; desc = `XOR key=${key} p=${plain} β†’ ${bRaw|0}`;
1190
+ }
1191
+ let nVars = cVals.length;
1192
+ samples.push({
1193
+ id: crypto.randomUUID(), domain: "cryptography", subdomain: kind, sense: "EQUAL", n_vars: nVars,
1194
+ vars_text: "VARS " + cVals.map((_, j) => `c${j} IN [0 1]`).join(" "), expr_text: `EXPR CIPHER_${kind.toUpperCase()}`,
1195
+ sense_text: "SENSE EQUAL", b_raw: bRaw, b_norm: Number(_safeNorm(bRaw).toFixed(8)), c_values: cVals.map(v => Number(v.toFixed(6))),
1196
+ mask:[...Array(nVars).fill(1), ...Array(Math.max(0, 4 - nVars)).fill(0)], candidates:[], step_count: 0,
1197
+ axl_source: `SCOPE cryptography [level: 1]\n OBSERVE ${kind} EQUALS ${bRaw}\n UNKNOWN key: VECTOR BOUND [0,1]\nEND SCOPE`,
1198
+ metadata: { description: desc, units: "character_code", difficulty: nVars<=2?"medium":"hard", scope_depth: 1, has_emergence: false, has_contradiction: true }
1199
+ });
1200
+ } catch (e) {}
1201
+ }
1202
+ return samples;
1203
+ }
1204
+
1205
+ function synthOptimizationSamples(n) {
1206
+ let samples =[];
1207
+ for(let i=0; i<n; i++) {
1208
+ let sense = randchoice(["MINIMIZE", "MAXIMIZE"]);
1209
+ let nVars = randint(2, 5);
1210
+ try {
1211
+ let coeffs = Array.from({length: nVars}, () => rnd(0.1, 10.0));
1212
+ let cOpt = sense === "MINIMIZE" ? Array(nVars).fill(0.0) : Array(nVars).fill(1.0);
1213
+ let cVals = cOpt.map(v => Math.min(1.0, Math.max(0.0, v + rnd(-0.05, 0.05))));
1214
+ let bRaw = cVals.reduce((acc, val, j) => acc + val*coeffs[j], 0);
1215
+
1216
+ samples.push({
1217
+ id: crypto.randomUUID(), domain: "optimization", subdomain: `linear_${sense.toLowerCase()}_${nVars}var`,
1218
+ sense: sense, n_vars: nVars, vars_text: "VARS " + cVals.map((_, j) => `c${j} IN [0 1]`).join(" "),
1219
+ expr_text: "EXPR " + coeffs.map((c, j) => `${c.toFixed(3)}*c${j}`).join(" + "), sense_text: `SENSE ${sense}`,
1220
+ b_raw: Number(bRaw.toFixed(6)), b_norm: Number(_safeNorm(bRaw).toFixed(8)), c_values: cVals.map(v => Number(v.toFixed(6))),
1221
+ mask: Array(nVars).fill(1), candidates: [], step_count: 0,
1222
+ axl_source: `SCOPE optimization[level: 2]\n OBJECTIVE OPTIMIZE f ${sense}\n SUBJECT TO c WITHIN [0,1]\nEND SCOPE`,
1223
+ metadata: { description: `Linear ${sense.toLowerCase()} over ${nVars} variables`, units: "objective", difficulty: "easy", scope_depth: 1, has_emergence: false, has_contradiction: false }
1224
+ });
1225
+ } catch(e) {}
1226
+ }
1227
+ return samples;
1228
+ }
1229
+
1230
+ const SYNTHETIC_GENERATORS = [synthPhysicsSamples, synthFinanceSamples, synthCipherSamples, synthOptimizationSamples];
1231
+
1232
+ // ── FOCUS TRACKER ────────────────────────────────────────────────────────────
1233
+ class FocusTracker {
1234
+ constructor(window = 50) {
1235
+ this.window = window;
1236
+ this.recent =[];
1237
+ this.counts = {};
1238
+ this.total = 0;
1239
+ }
1240
+ record(domain) {
1241
+ this.recent.push(domain);
1242
+ if (this.recent.length > this.window) this.recent.shift();
1243
+ this.counts[domain] = (this.counts[domain] || 0) + 1;
1244
+ this.total += 1;
1245
+ }
1246
+ stats() {
1247
+ const sortedCounts = Object.entries(this.counts).sort((a,b)=>b[1]-a[1]).reduce((acc,[k,v])=>{acc[k]=v; return acc;},{});
1248
+ return { total_generated: this.total, domain_counts: sortedCounts, recent_window: this.recent.slice(-20) };
1249
+ }
1250
+ }
1251
+ const tracker = new FocusTracker(FOCUS_WINDOW);
1252
+
1253
+ // ── STATE ────────────────────────────────────────────────────────────────────
1254
+ let _dataset =[];
1255
 
1256
+ function loadExisting() {
1257
+ if (fs.existsSync(DATASET_PATH)) {
1258
+ const data = fs.readFileSync(DATASET_PATH, 'utf-8');
1259
+ try {
1260
+ _dataset = JSON.parse(data);
1261
+ log(`Loaded ${_dataset.length} existing samples from ${DATASET_PATH}`, "OK");
1262
+ } catch (e) {
1263
+ log(`JSON Load Error on existing DB, resetting: ${e.message}`, "ERROR");
1264
  }
1265
+ } else {
1266
+ log("No existing dataset found β€” starting fresh", "OK");
 
1267
  }
1268
+ }
1269
 
1270
+ function saveDataset() {
1271
+ fs.writeFileSync(DATASET_PATH, JSON.stringify(_dataset, null, 2), 'utf-8');
1272
+ log(`Saved ${_dataset.length} samples β†’ ${DATASET_PATH}`, "OK");
1273
+ }
1274
 
1275
+ // ── JSON PARSER ──────────────────────────────────────────────────────────────
1276
+ function parseAiResponse(text) {
1277
+ let raw = text.trim();
1278
+ const fences = ["```json", "```JSON", "```"];
1279
+ for (let f of fences) {
1280
+ if (raw.startsWith(f)) { raw = raw.slice(f.length); break; }
1281
+ }
1282
+ if (raw.endsWith("```")) raw = raw.slice(0, -3);
1283
+ raw = raw.trim();
1284
 
1285
+ let start = raw.indexOf('[');
1286
+ let end = raw.lastIndexOf(']');
1287
+ if (start === -1 || end === -1) return[];
1288
 
1289
  try {
1290
+ let arr = JSON.parse(raw.slice(start, end + 1));
1291
+ return arr;
1292
+ } catch (e) {
1293
+ // Fallback salvage logic
1294
+ let salvaged =[];
1295
+ let depth = 0; let inObj = false; let buf = "";
1296
+ let jsonArea = raw.slice(start+1, end);
1297
+ for(let i=0; i<jsonArea.length; i++){
1298
+ let char = jsonArea[i];
1299
+ if (char === '{') { depth++; inObj = true; }
1300
+ if (inObj) buf += char;
1301
+ if (char === '}') {
1302
+ depth--;
1303
+ if (depth === 0 && buf.trim()) {
1304
+ try { salvaged.push(JSON.parse(buf)); } catch (err) {}
1305
+ buf = ""; inObj = false;
1306
+ }
1307
  }
1308
+ }
1309
+ return salvaged;
1310
+ }
1311
+ }
1312
 
1313
+ function validateSample(s) {
1314
+ const required =["domain","sense","n_vars","vars_text","expr_text","sense_text","b_raw","b_norm","c_values","mask"];
1315
+ for (let f of required) if (!(f in s)) return false;
1316
+ if (!Array.isArray(s.c_values)) return false;
1317
+ if (isNaN(s.b_raw) || !isFinite(s.b_raw)) return false;
1318
+ if (s.c_values.length !== s.n_vars) return false;
1319
+ if (!s.id) s.id = crypto.randomUUID();
1320
+ return true;
1321
+ }
1322
 
1323
+ // ── EXPRESS APP & ENDPOINTS ──────────────────────────────────────────────────
1324
+ loadExisting();
 
 
 
 
 
 
 
1325
 
1326
+ if (SYSTEM_PROMPT_HEADER.includes("[AXL_SPEC_HERE]")) {
1327
+ log("⚠ AXL spec placeholder not replaced in production code.", "WARN");
1328
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
1329
 
1330
+ app.get('/status', (req, res) => {
1331
+ res.json({
1332
+ dataset_size: _dataset.length,
1333
+ dataset_path: DATASET_PATH,
1334
+ bedrock_model: BEDROCK_MODEL_ID,
1335
+ focus_tracker: tracker.stats(),
1336
+ server_time: new Date().toISOString()
1337
+ });
1338
+ });
 
1339
 
1340
+ app.get('/domains', (req, res) => {
1341
+ res.json(tracker.stats());
1342
+ });
1343
+
1344
+ app.post('/generate', async (req, res) => {
1345
+ const n = parseInt(req.query.n || req.body.n || "50");
1346
+ const rps = parseFloat(req.query.rps || req.body.rps || DEFAULT_RPS.toString());
1347
+ const synthFraction = parseFloat(req.query.synthetic_fraction || req.body.synthetic_fraction || "0.3");
1348
+ const maxVars = parseInt(req.query.max_vars || req.body.max_vars || "20");
1349
+
1350
+ let generated =[];
1351
+ let errors = 0;
1352
+
1353
+ log(`Generation request: n=${n} rps=${rps} synthetic=${(synthFraction*100)|0}%`);
1354
+
1355
+ let n_synth = Math.floor(n * synthFraction);
1356
+ if (n_synth > 0) {
1357
+ log(`Generating ${n_synth} synthetic samples...`);
1358
+ let perGen = Math.max(1, Math.floor(n_synth / SYNTHETIC_GENERATORS.length));
1359
+ for (let genFn of SYNTHETIC_GENERATORS) {
1360
+ let batch = genFn(perGen);
1361
+ for (let s of batch) {
1362
+ if (validateSample(s)) {
1363
+ generated.push(s);
1364
+ tracker.record(s.domain);
1365
  }
1366
  }
1367
+ }
1368
+ }
1369
+
1370
+ let n_ai = n - generated.length;
1371
+ let callsNeeded = Math.ceil(n_ai / SAMPLES_PER_CALL);
1372
+ log(`AI generation: ${n_ai} samples via ${callsNeeded} Bedrock calls`);
1373
+
1374
+ let minInterval = 1000.0 / Math.min(rps, MAX_RPS);
1375
+
1376
+ for (let callIdx = 0; callIdx < callsNeeded; callIdx++) {
1377
+ let startStamp = Date.now();
1378
+ let remaining = n_ai - generated.filter(s => s.metadata?.source === "ai").length;
1379
+ let batchN = Math.min(SAMPLES_PER_CALL, remaining + 2); // +2 pad
1380
+
1381
+ log(`Bedrock call ${callIdx+1}/${callsNeeded} β€” requesting ${batchN} samples`);
1382
+
1383
+ try {
1384
+ let prompt = buildPrompt(batchN, tracker.stats().recent_window, maxVars);
1385
+ let responseText = await callBedrock(prompt);
1386
+ let samples = parseAiResponse(responseText);
1387
+ let validCount = 0;
1388
 
1389
+ for (let s of samples) {
1390
+ if (validateSample(s)) {
1391
+ if (!s.metadata) s.metadata = {};
1392
+ s.metadata.source = "ai";
1393
+ generated.push(s);
1394
+ tracker.record(s.domain || "unknown");
1395
+ validCount++;
1396
+ } else { errors++; }
1397
+ }
1398
+ log(`Call ${callIdx+1}: ${validCount} valid / ${samples.length} parsed`, "OK");
1399
+ } catch(err) {
1400
+ log(`Bedrock call ${callIdx+1} failed: ${err.message}`, "ERROR");
1401
+ errors++;
1402
+ }
1403
+
1404
+ // Throttle RPS logic
1405
+ let duration = Date.now() - startStamp;
1406
+ if (duration < minInterval && callIdx < callsNeeded - 1) {
1407
+ await new Promise(r => setTimeout(r, minInterval - duration));
1408
  }
 
 
 
 
1409
  }
1410
+
1411
+ _dataset.push(...generated);
1412
+ saveDataset();
1413
+ log(`Generation complete: ${generated.length} new | total=${_dataset.length} | errors=${errors}`, "OK");
1414
+
1415
+ res.json({
1416
+ generated: generated.length,
1417
+ errors: errors,
1418
+ total_dataset: _dataset.length,
1419
+ domain_breakdown: generated.reduce((acc, curr) => { acc[curr.domain] = (acc[curr.domain] || 0)+1; return acc }, {}),
1420
+ samples: generated
1421
+ });
1422
+ });
1423
+
1424
+ app.get('/dataset/download', (req, res) => {
1425
+ if (!fs.existsSync(DATASET_PATH)) {
1426
+ return res.status(404).json({ error: "No dataset file found. Run /generate first." });
1427
+ }
1428
+ const timestamp = new Date().toISOString().replace(/T/, '_').replace(/:/g, '').split('.')[0];
1429
+ res.download(DATASET_PATH, `axl_dataset_${timestamp}.json`);
1430
+ });
1431
+
1432
+ app.post('/dataset/clear', (req, res) => {
1433
+ const count = _dataset.length;
1434
+ _dataset =[];
1435
+ if (fs.existsSync(DATASET_PATH)) fs.unlinkSync(DATASET_PATH);
1436
+ log(`Dataset cleared β€” removed ${count} samples`, "OK");
1437
+ res.json({ cleared: count });
1438
+ });
1439
+
1440
+ app.get('/dataset/sample', (req, res) => {
1441
+ let n = parseInt(req.query.n || "5");
1442
+ let shuffled = [..._dataset].sort(() => 0.5 - Math.random());
1443
+ res.json({ samples: shuffled.slice(0, n) });
1444
  });
1445
 
1446
+ app.listen(PORT, '0.0.0.0', () => {
1447
+ log(`Main Dataset Agent live on port ${PORT}`, "HEAD");
1448
+ });