@patchbay-nebius/agent 0.1.3 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/{scanner-HW5E6G7U.js → chunk-63LPPBLZ.js} +68 -51
- package/dist/chunk-63LPPBLZ.js.map +1 -0
- package/dist/{chunk-JVXVCXNZ.js → chunk-DZXK5OTN.js} +904 -78
- package/dist/chunk-DZXK5OTN.js.map +1 -0
- package/dist/cli.js +98 -25
- package/dist/cli.js.map +1 -1
- package/dist/fixtures-py.template.py +196 -0
- package/dist/index.d.ts +146 -38
- package/dist/index.js +35 -3
- package/dist/scanner-CEZ56J5N.js +11 -0
- package/dist/scanner-CEZ56J5N.js.map +1 -0
- package/package.json +3 -2
- package/dist/chunk-JVXVCXNZ.js.map +0 -1
- package/dist/scanner-HW5E6G7U.js.map +0 -1
|
@@ -6,6 +6,7 @@ function loadConfig(env = process.env) {
|
|
|
6
6
|
const defaultModels = provider === "openai" ? { fast: "gpt-4o-mini", mid: "gpt-4o-mini", strong: "gpt-4o" } : { fast: "nvidia/Nemotron-3_5-Lightning", mid: "nvidia/nemotron-3-super-120b-a12b", strong: "nvidia/Nemotron-3-Ultra-550b-a55b" };
|
|
7
7
|
return {
|
|
8
8
|
mock: env.PATCHBAY_MOCK !== "false",
|
|
9
|
+
lang: parseLang(env.PATCHBAY_LANG),
|
|
9
10
|
runner: env.PATCHBAY_RUNNER === "local" ? "local" : "nebius",
|
|
10
11
|
nebiusApiKey: apiKey,
|
|
11
12
|
nebiusProject: env.NEBIUS_AI_PROJECT ?? "",
|
|
@@ -19,14 +20,30 @@ function loadConfig(env = process.env) {
|
|
|
19
20
|
models: {
|
|
20
21
|
fast: { id: env.PATCHBAY_MODEL_FAST || defaultModels.fast, inputPrice: 0.06, outputPrice: 0.24 },
|
|
21
22
|
mid: { id: env.PATCHBAY_MODEL_MID || defaultModels.mid, inputPrice: 0.3, outputPrice: 0.9 },
|
|
22
|
-
strong: { id: env.PATCHBAY_MODEL_STRONG || defaultModels.strong, inputPrice: 1, outputPrice: 3 }
|
|
23
|
-
|
|
23
|
+
strong: { id: env.PATCHBAY_MODEL_STRONG || defaultModels.strong, inputPrice: 1, outputPrice: 3 },
|
|
24
|
+
repair: { id: env.PATCHBAY_MODEL_REPAIR || env.PATCHBAY_MODEL_STRONG || defaultModels.strong, inputPrice: 1, outputPrice: 3 }
|
|
25
|
+
},
|
|
26
|
+
strongOverride: env.PATCHBAY_STRONG_LLM_BASE_URL ? {
|
|
27
|
+
baseUrl: env.PATCHBAY_STRONG_LLM_BASE_URL,
|
|
28
|
+
apiKey: env.PATCHBAY_STRONG_LLM_API_KEY || apiKey,
|
|
29
|
+
provider: parseProvider(env.PATCHBAY_STRONG_LLM_PROVIDER)
|
|
30
|
+
} : null,
|
|
31
|
+
repairOverride: env.PATCHBAY_REPAIR_LLM_BASE_URL ? {
|
|
32
|
+
baseUrl: env.PATCHBAY_REPAIR_LLM_BASE_URL,
|
|
33
|
+
apiKey: env.PATCHBAY_REPAIR_LLM_API_KEY || apiKey,
|
|
34
|
+
provider: parseProvider(env.PATCHBAY_REPAIR_LLM_PROVIDER)
|
|
35
|
+
} : null
|
|
24
36
|
};
|
|
25
37
|
}
|
|
26
38
|
function parseProvider(raw) {
|
|
27
39
|
if (raw === "openai" || raw === "generic") return raw;
|
|
28
40
|
return "nebius";
|
|
29
41
|
}
|
|
42
|
+
function parseLang(raw) {
|
|
43
|
+
const normalized = (raw ?? "").toLowerCase().trim();
|
|
44
|
+
if (normalized === "py" || normalized === "python") return "py";
|
|
45
|
+
return "ts";
|
|
46
|
+
}
|
|
30
47
|
function clamp(n, min, max) {
|
|
31
48
|
return Number.isFinite(n) ? Math.min(max, Math.max(min, Math.round(n))) : min;
|
|
32
49
|
}
|
|
@@ -85,21 +102,38 @@ function createNebiusClient(opts) {
|
|
|
85
102
|
const doFetch = opts.fetch ?? fetch;
|
|
86
103
|
const retries = opts.retries ?? 2;
|
|
87
104
|
const nemotron = opts.supportsNemotronThinking ?? true;
|
|
105
|
+
const overrides = opts.modelOverrides ?? {};
|
|
88
106
|
const ledger = /* @__PURE__ */ new Map();
|
|
107
|
+
function approxTokens(messages) {
|
|
108
|
+
let chars = 0;
|
|
109
|
+
for (const m of messages) chars += m.content.length + m.role.length + 8;
|
|
110
|
+
return Math.ceil(chars / 4);
|
|
111
|
+
}
|
|
89
112
|
async function chat(model, messages, options = {}) {
|
|
113
|
+
const override = overrides[model.id];
|
|
114
|
+
const baseUrl = override?.baseUrl ?? opts.baseUrl;
|
|
115
|
+
const apiKey = override?.apiKey ?? opts.apiKey;
|
|
116
|
+
const nemotronForCall = override ? override.supportsNemotronThinking ?? false : nemotron;
|
|
117
|
+
const contextWindow = override?.contextWindow;
|
|
118
|
+
let maxTokens = options.maxTokens ?? 4096;
|
|
119
|
+
if (contextWindow) {
|
|
120
|
+
const promptTokens = Math.ceil(approxTokens(messages) * 1.2) + 2048;
|
|
121
|
+
const budget = contextWindow - promptTokens;
|
|
122
|
+
if (budget > 0 && budget < maxTokens) maxTokens = budget;
|
|
123
|
+
}
|
|
90
124
|
const body = {
|
|
91
125
|
model: model.id,
|
|
92
126
|
messages,
|
|
93
127
|
temperature: options.temperature ?? 0.2,
|
|
94
|
-
max_tokens:
|
|
95
|
-
...
|
|
128
|
+
max_tokens: maxTokens,
|
|
129
|
+
...nemotronForCall && options.thinking === false ? { chat_template_kwargs: { enable_thinking: false } } : {}
|
|
96
130
|
};
|
|
97
131
|
let lastError;
|
|
98
132
|
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
99
133
|
try {
|
|
100
|
-
const res = await doFetch(`${
|
|
134
|
+
const res = await doFetch(`${baseUrl}/chat/completions`, {
|
|
101
135
|
method: "POST",
|
|
102
|
-
headers: { Authorization: `Bearer ${
|
|
136
|
+
headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" },
|
|
103
137
|
body: JSON.stringify(body)
|
|
104
138
|
});
|
|
105
139
|
const data = await res.json().catch(() => ({}));
|
|
@@ -148,6 +182,8 @@ import { dirname, join } from "path";
|
|
|
148
182
|
// src/report.ts
|
|
149
183
|
var TSC_MARKER = "@@PATCHBAY_TSC@@";
|
|
150
184
|
var VITEST_MARKER = "@@PATCHBAY_VITEST@@";
|
|
185
|
+
var PYC_MARKER = "@@PATCHBAY_PYC@@";
|
|
186
|
+
var PYTEST_MARKER = "@@PATCHBAY_PYTEST@@";
|
|
151
187
|
var TEST_COMMAND = [
|
|
152
188
|
"cd /work",
|
|
153
189
|
"npx tsc --noEmit -p tsconfig.json > /tmp/tsc.txt 2>&1; echo $? > /tmp/tsc.code",
|
|
@@ -158,6 +194,16 @@ var TEST_COMMAND = [
|
|
|
158
194
|
`echo "${VITEST_MARKER}"`,
|
|
159
195
|
"cat /tmp/vitest.json 2>/dev/null || tail -c 6000 /tmp/vitest.log"
|
|
160
196
|
].join("; ");
|
|
197
|
+
var TEST_COMMAND_PY = [
|
|
198
|
+
"cd /work",
|
|
199
|
+
"python -m compileall -q . > /tmp/pyc.txt 2>&1; echo $? > /tmp/pyc.code",
|
|
200
|
+
"python -m pytest -q --json-report --json-report-file=/tmp/pytest.json > /tmp/pytest.log 2>&1",
|
|
201
|
+
`echo "${PYC_MARKER}"`,
|
|
202
|
+
"cat /tmp/pyc.code",
|
|
203
|
+
"head -c 6000 /tmp/pyc.txt",
|
|
204
|
+
`echo "${PYTEST_MARKER}"`,
|
|
205
|
+
"cat /tmp/pytest.json 2>/dev/null || tail -c 6000 /tmp/pytest.log"
|
|
206
|
+
].join("; ");
|
|
161
207
|
function parseTestOutput(stdout, ranOn) {
|
|
162
208
|
const tscStart = stdout.indexOf(TSC_MARKER);
|
|
163
209
|
const vitestStart = stdout.indexOf(VITEST_MARKER);
|
|
@@ -217,6 +263,68 @@ function parseTestOutput(stdout, ranOn) {
|
|
|
217
263
|
passedNames
|
|
218
264
|
};
|
|
219
265
|
}
|
|
266
|
+
function parsePytestOutput(stdout, ranOn) {
|
|
267
|
+
const pycStart = stdout.indexOf(PYC_MARKER);
|
|
268
|
+
const pytestStart = stdout.indexOf(PYTEST_MARKER);
|
|
269
|
+
const pycSection = pycStart >= 0 ? stdout.slice(pycStart + PYC_MARKER.length, pytestStart >= 0 ? pytestStart : void 0).trim() : "";
|
|
270
|
+
const pytestSection = pytestStart >= 0 ? stdout.slice(pytestStart + PYTEST_MARKER.length).trim() : stdout.trim();
|
|
271
|
+
const [codeLine = "1", ...pycLines] = pycSection.split("\n");
|
|
272
|
+
const typecheckOk = codeLine.trim() === "0";
|
|
273
|
+
const typecheckOutput = pycLines.join("\n").trim();
|
|
274
|
+
let json = null;
|
|
275
|
+
try {
|
|
276
|
+
const start = pytestSection.indexOf("{");
|
|
277
|
+
json = start >= 0 ? JSON.parse(pytestSection.slice(start)) : null;
|
|
278
|
+
} catch {
|
|
279
|
+
json = null;
|
|
280
|
+
}
|
|
281
|
+
if (!json) {
|
|
282
|
+
return {
|
|
283
|
+
typecheckOk,
|
|
284
|
+
typecheckOutput,
|
|
285
|
+
passed: 0,
|
|
286
|
+
failed: 1,
|
|
287
|
+
total: 1,
|
|
288
|
+
failures: [{ name: "pytest", message: truncate(pytestSection || "pytest produced no output", 4e3) }],
|
|
289
|
+
durationMs: 0,
|
|
290
|
+
ranOn
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
const failures = [];
|
|
294
|
+
const passedNames = [];
|
|
295
|
+
for (const collector of json.collectors ?? []) {
|
|
296
|
+
if (collector.outcome === "failed" && collector.longrepr) {
|
|
297
|
+
failures.push({ name: `collection: ${collector.nodeid ?? "unknown"}`, message: truncate(stripAnsi(collector.longrepr), 2e3) });
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
for (const test of json.tests ?? []) {
|
|
301
|
+
const name = test.nodeid ?? "unnamed test";
|
|
302
|
+
if (test.outcome === "passed" || test.outcome === "xpassed") {
|
|
303
|
+
passedNames.push(name);
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (test.outcome === "failed" || test.outcome === "error") {
|
|
307
|
+
const detail = test.call?.longrepr ?? test.setup?.longrepr ?? test.teardown?.longrepr ?? "(no detail)";
|
|
308
|
+
failures.push({ name, message: truncate(stripAnsi(detail), 2e3) });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
const summary = json.summary ?? {};
|
|
312
|
+
const passed = summary.passed ?? passedNames.length;
|
|
313
|
+
const summaryFailed = (summary.failed ?? 0) + (summary.error ?? 0);
|
|
314
|
+
const failed = Math.max(summaryFailed, failures.length);
|
|
315
|
+
const total = Math.max(summary.total ?? summary.collected ?? 0, passed + failed);
|
|
316
|
+
return {
|
|
317
|
+
typecheckOk,
|
|
318
|
+
typecheckOutput,
|
|
319
|
+
passed,
|
|
320
|
+
failed,
|
|
321
|
+
total,
|
|
322
|
+
failures,
|
|
323
|
+
durationMs: json.duration ? Math.round(json.duration * 1e3) : 0,
|
|
324
|
+
ranOn,
|
|
325
|
+
passedNames
|
|
326
|
+
};
|
|
327
|
+
}
|
|
220
328
|
function isGreen(report) {
|
|
221
329
|
const contractOk = report.contract === void 0 || report.contract === "passed";
|
|
222
330
|
return report.typecheckOk && report.failed === 0 && report.total > 0 && contractOk;
|
|
@@ -243,41 +351,95 @@ function createLocalRunner(opts = {}) {
|
|
|
243
351
|
const tscEntry = join(root, "node_modules", "typescript", "bin", "tsc");
|
|
244
352
|
const vitestEntry = join(root, "node_modules", "vitest", "vitest.mjs");
|
|
245
353
|
const timeout = opts.timeoutMs ?? 12e4;
|
|
354
|
+
const pythonBin = opts.pythonBin ?? (process.platform === "win32" ? "python" : "python3");
|
|
246
355
|
return {
|
|
247
356
|
async prepare(log) {
|
|
248
|
-
log("Local runner:
|
|
357
|
+
log("Local runner: tests run on this machine, not in an isolated sandbox");
|
|
249
358
|
return "local";
|
|
250
359
|
},
|
|
251
360
|
async runTests(_base, files, log) {
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
361
|
+
const isPy = files.some((f) => f.path.endsWith(".py"));
|
|
362
|
+
return isPy ? runPythonTests(files, log, { root, pythonBin, timeout }) : runTypeScriptTests(files, log, { root, tscEntry, vitestEntry, timeout });
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
async function runTypeScriptTests(files, log, ctx) {
|
|
367
|
+
const workRoot = join(ctx.root, ".fixture-tmp");
|
|
368
|
+
mkdirSync(workRoot, { recursive: true });
|
|
369
|
+
const work = mkdtempSync(join(workRoot, "run-"));
|
|
370
|
+
try {
|
|
371
|
+
for (const file of files.filter((f) => f.path.endsWith(".ts"))) write(work, file);
|
|
372
|
+
writeFileSync(join(work, "tsconfig.json"), JSON.stringify(TSCONFIG));
|
|
373
|
+
writeFileSync(join(work, "vitest.config.mjs"), 'export default { test: { include: ["src/**/*.test.ts"] } }\n');
|
|
374
|
+
log(`Running tsc and vitest in ${work.replace(ctx.root, ".")}`);
|
|
375
|
+
const tsc = await runNodeEntry(ctx.tscEntry, ["--noEmit", "-p", "tsconfig.json"], work, ctx.timeout);
|
|
376
|
+
await runNodeEntry(ctx.vitestEntry, ["run", "--reporter=json", "--outputFile=result.json"], work, ctx.timeout);
|
|
377
|
+
const json = existsSync(join(work, "result.json")) ? await readText(join(work, "result.json")) : "";
|
|
378
|
+
const stdout = `${TSC_MARKER}
|
|
264
379
|
${tsc.code}
|
|
265
380
|
${tsc.output}
|
|
266
381
|
${VITEST_MARKER}
|
|
267
382
|
${json}`;
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
383
|
+
return parseTestOutput(stdout, "local");
|
|
384
|
+
} finally {
|
|
385
|
+
rmSync(work, { recursive: true, force: true });
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
async function runPythonTests(files, log, ctx) {
|
|
389
|
+
await probePythonEnv(ctx.pythonBin);
|
|
390
|
+
const workRoot = join(ctx.root, ".fixture-tmp");
|
|
391
|
+
mkdirSync(workRoot, { recursive: true });
|
|
392
|
+
const work = mkdtempSync(join(workRoot, "py-"));
|
|
393
|
+
try {
|
|
394
|
+
for (const file of files.filter((f) => f.path.endsWith(".py"))) write(work, file);
|
|
395
|
+
writeFileSync(join(work, "pytest.ini"), "[pytest]\nasyncio_mode = auto\n");
|
|
396
|
+
log(`Running py_compile and pytest in ${work.replace(ctx.root, ".")}`);
|
|
397
|
+
const pyc = await runCmd(ctx.pythonBin, ["-m", "compileall", "-q", "."], work, ctx.timeout);
|
|
398
|
+
await runCmd(ctx.pythonBin, ["-m", "pytest", "-q", "--json-report", "--json-report-file=result.json"], work, ctx.timeout);
|
|
399
|
+
const json = existsSync(join(work, "result.json")) ? await readText(join(work, "result.json")) : "";
|
|
400
|
+
const stdout = `${PYC_MARKER}
|
|
401
|
+
${pyc.code}
|
|
402
|
+
${pyc.output}
|
|
403
|
+
${PYTEST_MARKER}
|
|
404
|
+
${json}`;
|
|
405
|
+
return parsePytestOutput(stdout, "local");
|
|
406
|
+
} finally {
|
|
407
|
+
rmSync(work, { recursive: true, force: true });
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
async function probePythonEnv(pythonBin) {
|
|
411
|
+
const versionOut = await runCmd(pythonBin, ["--version"], process.cwd(), 1e4).catch(() => ({ code: 1, output: "" }));
|
|
412
|
+
if (versionOut.code !== 0 || !/^Python\s+(\d+)\.(\d+)/.test(versionOut.output)) {
|
|
413
|
+
throw new Error(`Local runner (python): '${pythonBin}' not found on PATH. Install Python 3.12+ or set PATCHBAY_RUNNER=nebius.`);
|
|
414
|
+
}
|
|
415
|
+
const m = /^Python\s+(\d+)\.(\d+)/.exec(versionOut.output);
|
|
416
|
+
const major = Number(m?.[1] ?? 0);
|
|
417
|
+
const minor = Number(m?.[2] ?? 0);
|
|
418
|
+
if (major < 3 || major === 3 && minor < 12) {
|
|
419
|
+
throw new Error(`Local runner (python): found Python ${major}.${minor}, need >=3.12. Upgrade python or set PATCHBAY_RUNNER=nebius.`);
|
|
420
|
+
}
|
|
421
|
+
const missing = [];
|
|
422
|
+
const probe = async (mod, pkg) => {
|
|
423
|
+
const res = await runCmd(pythonBin, ["-c", `import ${mod}`], process.cwd(), 1e4).catch(() => ({ code: 1, output: "" }));
|
|
424
|
+
if (res.code !== 0) missing.push(pkg);
|
|
273
425
|
};
|
|
426
|
+
await probe("pytest", "pytest");
|
|
427
|
+
await probe("pytest_asyncio", "pytest-asyncio");
|
|
428
|
+
await probe("pytest_jsonreport", "pytest-json-report");
|
|
429
|
+
if (missing.length) {
|
|
430
|
+
throw new Error(
|
|
431
|
+
`Local runner (python): missing ${missing.join(", ")}. Install with:
|
|
432
|
+
${pythonBin} -m pip install ${missing.join(" ")}
|
|
433
|
+
Or set PATCHBAY_RUNNER=nebius to use the hosted sandbox.`
|
|
434
|
+
);
|
|
435
|
+
}
|
|
274
436
|
}
|
|
275
437
|
function write(dir, file) {
|
|
276
438
|
const path = join(dir, file.path);
|
|
277
439
|
mkdirSync(dirname(path), { recursive: true });
|
|
278
440
|
writeFileSync(path, file.content);
|
|
279
441
|
}
|
|
280
|
-
function
|
|
442
|
+
function runNodeEntry(entry, args, cwd, timeout) {
|
|
281
443
|
return new Promise((resolve) => {
|
|
282
444
|
execFile(process.execPath, [entry, ...args], { cwd, timeout, maxBuffer: 1e7 }, (error, stdout, stderr) => {
|
|
283
445
|
const code = error ? typeof error.code === "number" ? error.code : 1 : 0;
|
|
@@ -285,6 +447,14 @@ function run(entry, args, cwd, timeout) {
|
|
|
285
447
|
});
|
|
286
448
|
});
|
|
287
449
|
}
|
|
450
|
+
function runCmd(bin, args, cwd, timeout) {
|
|
451
|
+
return new Promise((resolve) => {
|
|
452
|
+
execFile(bin, args, { cwd, timeout, maxBuffer: 1e7 }, (error, stdout, stderr) => {
|
|
453
|
+
const code = error ? typeof error.code === "number" ? error.code : 1 : 0;
|
|
454
|
+
resolve({ code, output: `${stdout}${stderr}`.slice(0, 6e3) });
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
}
|
|
288
458
|
async function readText(path) {
|
|
289
459
|
const { readFile } = await import("fs/promises");
|
|
290
460
|
return readFile(path, "utf8");
|
|
@@ -1266,9 +1436,9 @@ function isPlaceholder(segment: string): boolean {
|
|
|
1266
1436
|
|
|
1267
1437
|
// src/replay.ts
|
|
1268
1438
|
var REPLAY_RUN = run_default;
|
|
1269
|
-
async function* replayRun(
|
|
1270
|
-
yield { type: "mode", mode: "replay" };
|
|
1271
|
-
for (const event of
|
|
1439
|
+
async function* replayRun(run = REPLAY_RUN, pace = 1) {
|
|
1440
|
+
yield { type: "mode", mode: "replay", lang: "ts" };
|
|
1441
|
+
for (const event of run.events) {
|
|
1272
1442
|
if (event.type === "mode") continue;
|
|
1273
1443
|
await sleep2(delayFor(event) * pace);
|
|
1274
1444
|
yield event;
|
|
@@ -1288,8 +1458,208 @@ function delayFor(event) {
|
|
|
1288
1458
|
}
|
|
1289
1459
|
var sleep2 = (ms) => ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve();
|
|
1290
1460
|
|
|
1461
|
+
// src/fixtures-py-template.ts
|
|
1462
|
+
var FIXTURES_PY_TEMPLATE = `# Written by patchbay from the API documentation. Regenerated on every attempt, do not edit.
|
|
1463
|
+
# {{PATCHBAY_DOCS_HEADER}}
|
|
1464
|
+
|
|
1465
|
+
"""Contract fixtures for the connector under test.
|
|
1466
|
+
|
|
1467
|
+
Exports:
|
|
1468
|
+
SOURCE_EXAMPLE dict \u2014 documented event payload
|
|
1469
|
+
TARGET_ENDPOINTS list \u2014 endpoints the connector may call, in documented order
|
|
1470
|
+
contract_fetch() \u2014 fake async transport answering documented endpoints, raising otherwise
|
|
1471
|
+
fake_fetch(handler) \u2014 fake async transport driven by a user handler
|
|
1472
|
+
json_response(body, status=200, headers=None)
|
|
1473
|
+
"""
|
|
1474
|
+
from __future__ import annotations
|
|
1475
|
+
|
|
1476
|
+
import json as _json
|
|
1477
|
+
import re as _re
|
|
1478
|
+
from typing import Any, Awaitable, Callable, TypedDict
|
|
1479
|
+
|
|
1480
|
+
#: Example payload of the source event, as documented.
|
|
1481
|
+
SOURCE_EXAMPLE: dict[str, Any] = {{PATCHBAY_SOURCE_EXAMPLE}}
|
|
1482
|
+
|
|
1483
|
+
|
|
1484
|
+
class DocumentedEndpoint(TypedDict):
|
|
1485
|
+
method: str
|
|
1486
|
+
url: str
|
|
1487
|
+
response: Any
|
|
1488
|
+
|
|
1489
|
+
|
|
1490
|
+
#: Target endpoints the connector may call, in the documented order, with documented responses.
|
|
1491
|
+
TARGET_ENDPOINTS: list[DocumentedEndpoint] = {{PATCHBAY_TARGET_ENDPOINTS}}
|
|
1492
|
+
|
|
1493
|
+
|
|
1494
|
+
class RecordedCall(TypedDict):
|
|
1495
|
+
method: str
|
|
1496
|
+
url: str
|
|
1497
|
+
body: Any
|
|
1498
|
+
json: Any
|
|
1499
|
+
headers: dict[str, str]
|
|
1500
|
+
|
|
1501
|
+
|
|
1502
|
+
class FakeResponse:
|
|
1503
|
+
"""Minimal response object: status_code, headers (dict), text, json(), raise_for_status()."""
|
|
1504
|
+
|
|
1505
|
+
def __init__(self, body: Any, status: int = 200, headers: dict[str, str] | None = None) -> None:
|
|
1506
|
+
self.status_code = status
|
|
1507
|
+
self.status = status # alias
|
|
1508
|
+
self.headers = dict(headers or {})
|
|
1509
|
+
if status == 204:
|
|
1510
|
+
self._raw = b''
|
|
1511
|
+
elif isinstance(body, (bytes, bytearray)):
|
|
1512
|
+
self._raw = bytes(body)
|
|
1513
|
+
elif isinstance(body, str):
|
|
1514
|
+
self._raw = body.encode('utf-8')
|
|
1515
|
+
else:
|
|
1516
|
+
self._raw = _json.dumps(body).encode('utf-8')
|
|
1517
|
+
self.headers.setdefault('Content-Type', 'application/json')
|
|
1518
|
+
|
|
1519
|
+
@property
|
|
1520
|
+
def content(self) -> bytes:
|
|
1521
|
+
return self._raw
|
|
1522
|
+
|
|
1523
|
+
@property
|
|
1524
|
+
def text(self) -> str:
|
|
1525
|
+
return self._raw.decode('utf-8', errors='replace')
|
|
1526
|
+
|
|
1527
|
+
def json(self) -> Any:
|
|
1528
|
+
return _json.loads(self._raw.decode('utf-8')) if self._raw else None
|
|
1529
|
+
|
|
1530
|
+
def raise_for_status(self) -> None:
|
|
1531
|
+
if self.status_code >= 400:
|
|
1532
|
+
raise Exception(f"HTTP {self.status_code}")
|
|
1533
|
+
|
|
1534
|
+
|
|
1535
|
+
FetchHandler = Callable[[RecordedCall], FakeResponse | Awaitable[FakeResponse]]
|
|
1536
|
+
|
|
1537
|
+
|
|
1538
|
+
def _parse_json(raw: Any) -> Any:
|
|
1539
|
+
if raw is None:
|
|
1540
|
+
return None
|
|
1541
|
+
# dict / list already parsed \u2014 most connectors pass body=<dict>, not body=json.dumps(dict)
|
|
1542
|
+
if isinstance(raw, (dict, list)):
|
|
1543
|
+
return raw
|
|
1544
|
+
if isinstance(raw, (bytes, bytearray)):
|
|
1545
|
+
try:
|
|
1546
|
+
return _json.loads(raw.decode('utf-8'))
|
|
1547
|
+
except Exception:
|
|
1548
|
+
return None
|
|
1549
|
+
if isinstance(raw, str):
|
|
1550
|
+
try:
|
|
1551
|
+
return _json.loads(raw)
|
|
1552
|
+
except Exception:
|
|
1553
|
+
return None
|
|
1554
|
+
return None
|
|
1555
|
+
|
|
1556
|
+
|
|
1557
|
+
_URL = _re.compile(r'^https?://([^/?#]+)([^?#]*)', _re.IGNORECASE)
|
|
1558
|
+
_PLACEHOLDER = _re.compile(r'^\\{[^}]+\\}$|^:[A-Za-z_]\\w*$|^<[^>]+>$')
|
|
1559
|
+
|
|
1560
|
+
|
|
1561
|
+
def _split_url(url: str) -> tuple[str, str] | None:
|
|
1562
|
+
m = _URL.match(url)
|
|
1563
|
+
if not m:
|
|
1564
|
+
return None
|
|
1565
|
+
host = m.group(1).lower()
|
|
1566
|
+
path = m.group(2) or '/'
|
|
1567
|
+
return host, path
|
|
1568
|
+
|
|
1569
|
+
|
|
1570
|
+
def _url_matches(template: str, actual: str) -> bool:
|
|
1571
|
+
t, a = _split_url(template), _split_url(actual)
|
|
1572
|
+
if not t or not a:
|
|
1573
|
+
return False
|
|
1574
|
+
t_host, t_path = t
|
|
1575
|
+
a_host, a_path = a
|
|
1576
|
+
if '.'.join(t_host.split('.')[-2:]) != '.'.join(a_host.split('.')[-2:]):
|
|
1577
|
+
return False
|
|
1578
|
+
t_segs = t_path.rstrip('/').split('/')
|
|
1579
|
+
a_segs = a_path.rstrip('/').split('/')
|
|
1580
|
+
if len(t_segs) != len(a_segs):
|
|
1581
|
+
return False
|
|
1582
|
+
for ts, as_ in zip(t_segs, a_segs):
|
|
1583
|
+
if _PLACEHOLDER.match(ts):
|
|
1584
|
+
continue
|
|
1585
|
+
if ts != as_:
|
|
1586
|
+
return False
|
|
1587
|
+
return True
|
|
1588
|
+
|
|
1589
|
+
|
|
1590
|
+
def find_endpoint(method: str, url: str) -> DocumentedEndpoint | None:
|
|
1591
|
+
for ep in TARGET_ENDPOINTS:
|
|
1592
|
+
if ep['method'] == method.upper() and _url_matches(ep['url'], url):
|
|
1593
|
+
return ep
|
|
1594
|
+
return None
|
|
1595
|
+
|
|
1596
|
+
|
|
1597
|
+
def fake_fetch(handler: FetchHandler) -> tuple[Callable[..., Awaitable[FakeResponse]], list[RecordedCall]]:
|
|
1598
|
+
"""A correctly-typed fake async fetch for every non-contract test.
|
|
1599
|
+
|
|
1600
|
+
The handler receives the recorded request dict: method, url, body, json, headers.
|
|
1601
|
+
It may return a FakeResponse directly or an awaitable.
|
|
1602
|
+
"""
|
|
1603
|
+
calls: list[RecordedCall] = []
|
|
1604
|
+
|
|
1605
|
+
async def fetch(url: str, method: str = 'GET', body: Any = None, headers: dict[str, str] | None = None, **_: Any) -> FakeResponse:
|
|
1606
|
+
call: RecordedCall = {
|
|
1607
|
+
'method': method.upper(),
|
|
1608
|
+
'url': url,
|
|
1609
|
+
'body': body,
|
|
1610
|
+
'json': _parse_json(body),
|
|
1611
|
+
'headers': dict(headers or {}),
|
|
1612
|
+
}
|
|
1613
|
+
calls.append(call)
|
|
1614
|
+
result = handler(call)
|
|
1615
|
+
if hasattr(result, '__await__'):
|
|
1616
|
+
return await result # type: ignore[return-value]
|
|
1617
|
+
return result # type: ignore[return-value]
|
|
1618
|
+
|
|
1619
|
+
return fetch, calls
|
|
1620
|
+
|
|
1621
|
+
|
|
1622
|
+
def contract_fetch() -> tuple[Callable[..., Awaitable[FakeResponse]], list[RecordedCall]]:
|
|
1623
|
+
"""Fake async fetch that answers every documented endpoint with its documented response
|
|
1624
|
+
and raises on anything else. Use it in the contract test:
|
|
1625
|
+
|
|
1626
|
+
fetch, calls = contract_fetch()
|
|
1627
|
+
await handle(SOURCE_EXAMPLE, { 'fetch': fetch, ... })
|
|
1628
|
+
assert len(calls) > 0
|
|
1629
|
+
"""
|
|
1630
|
+
|
|
1631
|
+
def _handler(req: RecordedCall) -> FakeResponse:
|
|
1632
|
+
ep = find_endpoint(req['method'], req['url'])
|
|
1633
|
+
if not ep:
|
|
1634
|
+
raise AssertionError(f"contract: {req['method']} {req['url']} is not a documented endpoint")
|
|
1635
|
+
return json_response(ep['response'])
|
|
1636
|
+
|
|
1637
|
+
return fake_fetch(_handler)
|
|
1638
|
+
|
|
1639
|
+
|
|
1640
|
+
_STATUS_TEXT: dict[int, str] = {
|
|
1641
|
+
200: 'OK', 201: 'Created', 202: 'Accepted', 204: 'No Content',
|
|
1642
|
+
400: 'Bad Request', 401: 'Unauthorized', 403: 'Forbidden', 404: 'Not Found', 409: 'Conflict',
|
|
1643
|
+
422: 'Unprocessable Entity', 429: 'Too Many Requests',
|
|
1644
|
+
500: 'Internal Server Error', 502: 'Bad Gateway', 503: 'Service Unavailable',
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
|
|
1648
|
+
def json_response(body: Any, status: int = 200, headers: dict[str, str] | None = None) -> FakeResponse:
|
|
1649
|
+
"""A JSON FakeResponse with the standard status text and content-type.
|
|
1650
|
+
|
|
1651
|
+
Extra response headers (Retry-After for 429 tests, Location for redirects, etc.) can be passed via
|
|
1652
|
+
the headers= kwarg and are merged on top of the content-type header.
|
|
1653
|
+
"""
|
|
1654
|
+
merged: dict[str, str] = {'Content-Type': 'application/json'}
|
|
1655
|
+
if headers:
|
|
1656
|
+
merged.update(headers)
|
|
1657
|
+
return FakeResponse(body, status, headers=merged)
|
|
1658
|
+
`;
|
|
1659
|
+
|
|
1291
1660
|
// src/contract.ts
|
|
1292
1661
|
var FIXTURES_PATH = "src/fixtures.ts";
|
|
1662
|
+
var FIXTURES_PATH_PY = "fixtures.py";
|
|
1293
1663
|
function renderFixtures(spec) {
|
|
1294
1664
|
const endpoints = spec.target.map((target) => ({
|
|
1295
1665
|
method: target.method.toUpperCase(),
|
|
@@ -1453,12 +1823,95 @@ function withContract(report, files) {
|
|
|
1453
1823
|
return { ...report, contract: "passed" };
|
|
1454
1824
|
}
|
|
1455
1825
|
function withFixtures(files, fixtures) {
|
|
1456
|
-
return [...files.filter((f) => f.path !==
|
|
1826
|
+
return [...files.filter((f) => f.path !== fixtures.path), fixtures];
|
|
1827
|
+
}
|
|
1828
|
+
function renderFixturesPy(spec) {
|
|
1829
|
+
const endpoints = spec.target.map((target) => ({
|
|
1830
|
+
method: target.method.toUpperCase(),
|
|
1831
|
+
url: target.url,
|
|
1832
|
+
response: target.responseExample ?? {}
|
|
1833
|
+
}));
|
|
1834
|
+
const docs = [...new Set([spec.source.docsUrl, ...spec.target.map((t) => t.docsUrl)].filter(Boolean))];
|
|
1835
|
+
const docsHeader = docs.length ? docs.map((url) => `Source: ${url}`).join("\n# ") : "generated from the research step";
|
|
1836
|
+
const content = FIXTURES_PY_TEMPLATE.replace("{{PATCHBAY_DOCS_HEADER}}", docsHeader).replace("{{PATCHBAY_SOURCE_EXAMPLE}}", pythonRepr(spec.source.example ?? {}, 0)).replace("{{PATCHBAY_TARGET_ENDPOINTS}}", pythonRepr(endpoints, 0));
|
|
1837
|
+
return { path: FIXTURES_PATH_PY, content };
|
|
1838
|
+
}
|
|
1839
|
+
function pythonRepr(value, indent) {
|
|
1840
|
+
const pad = (n) => " ".repeat(n);
|
|
1841
|
+
if (value === null || value === void 0) return "None";
|
|
1842
|
+
if (typeof value === "boolean") return value ? "True" : "False";
|
|
1843
|
+
if (typeof value === "number") return Number.isFinite(value) ? String(value) : "None";
|
|
1844
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
1845
|
+
if (Array.isArray(value)) {
|
|
1846
|
+
if (value.length === 0) return "[]";
|
|
1847
|
+
const items = value.map((v) => `${pad(indent + 1)}${pythonRepr(v, indent + 1)}`).join(",\n");
|
|
1848
|
+
return `[
|
|
1849
|
+
${items},
|
|
1850
|
+
${pad(indent)}]`;
|
|
1851
|
+
}
|
|
1852
|
+
if (typeof value === "object") {
|
|
1853
|
+
const entries = Object.entries(value);
|
|
1854
|
+
if (entries.length === 0) return "{}";
|
|
1855
|
+
const items = entries.map(([k, v]) => `${pad(indent + 1)}${JSON.stringify(k)}: ${pythonRepr(v, indent + 1)}`).join(",\n");
|
|
1856
|
+
return `{
|
|
1857
|
+
${items},
|
|
1858
|
+
${pad(indent)}}`;
|
|
1859
|
+
}
|
|
1860
|
+
return "None";
|
|
1861
|
+
}
|
|
1862
|
+
function contractMissingPy(files) {
|
|
1863
|
+
const tests = files.find((f) => f.path === "test_connector.py")?.content ?? "";
|
|
1864
|
+
if (!/from\s+fixtures\s+import/.test(tests) && !/import\s+fixtures/.test(tests)) {
|
|
1865
|
+
return "test_connector.py does not import from fixtures";
|
|
1866
|
+
}
|
|
1867
|
+
if (!/\bdef\s+test_contract/.test(tests)) {
|
|
1868
|
+
return "test_connector.py has no test_contract* function running handle on SOURCE_EXAMPLE";
|
|
1869
|
+
}
|
|
1870
|
+
if (!tests.includes("SOURCE_EXAMPLE")) {
|
|
1871
|
+
return "the contract tests never pass SOURCE_EXAMPLE to handle";
|
|
1872
|
+
}
|
|
1873
|
+
return null;
|
|
1874
|
+
}
|
|
1875
|
+
function contractMissingFor(lang, files) {
|
|
1876
|
+
return lang === "py" ? contractMissingPy(files) : contractMissing(files);
|
|
1877
|
+
}
|
|
1878
|
+
function renderFixturesFor(lang, spec) {
|
|
1879
|
+
return lang === "py" ? renderFixturesPy(spec) : renderFixtures(spec);
|
|
1880
|
+
}
|
|
1881
|
+
function withContractFor(lang, report, files) {
|
|
1882
|
+
const missing = contractMissingFor(lang, files);
|
|
1883
|
+
if (missing) {
|
|
1884
|
+
return {
|
|
1885
|
+
...report,
|
|
1886
|
+
contract: "missing",
|
|
1887
|
+
failed: report.failed + 1,
|
|
1888
|
+
total: report.total + 1,
|
|
1889
|
+
failures: [...report.failures, { name: "contract (static check)", message: missing }]
|
|
1890
|
+
};
|
|
1891
|
+
}
|
|
1892
|
+
const failed = report.failures.some((f) => /\bcontract\b/i.test(f.name) || /test_contract/.test(f.name));
|
|
1893
|
+
if (failed) return { ...report, contract: "failed" };
|
|
1894
|
+
const ran = (report.passedNames ?? []).some((name) => /\bcontract\b/i.test(name) || /test_contract/.test(name));
|
|
1895
|
+
if (!ran) {
|
|
1896
|
+
return {
|
|
1897
|
+
...report,
|
|
1898
|
+
contract: "failed",
|
|
1899
|
+
failed: report.failed + 1,
|
|
1900
|
+
total: report.total + 1,
|
|
1901
|
+
failures: [
|
|
1902
|
+
...report.failures,
|
|
1903
|
+
{ name: "contract (did not run)", message: "The contract test never passed; the test file probably failed to load." }
|
|
1904
|
+
]
|
|
1905
|
+
};
|
|
1906
|
+
}
|
|
1907
|
+
return { ...report, contract: "passed" };
|
|
1457
1908
|
}
|
|
1458
1909
|
|
|
1459
1910
|
// src/syntax-check.ts
|
|
1911
|
+
import { spawn } from "child_process";
|
|
1460
1912
|
import { transform } from "esbuild";
|
|
1461
|
-
async function preflightSyntax(files) {
|
|
1913
|
+
async function preflightSyntax(files, lang = "ts") {
|
|
1914
|
+
if (lang === "py") return preflightSyntaxPy(files);
|
|
1462
1915
|
const tsFiles = files.filter((f) => f.path.endsWith(".ts"));
|
|
1463
1916
|
const failures = [];
|
|
1464
1917
|
await Promise.all(
|
|
@@ -1499,6 +1952,68 @@ async function preflightSyntax(files) {
|
|
|
1499
1952
|
passedNames: []
|
|
1500
1953
|
};
|
|
1501
1954
|
}
|
|
1955
|
+
var pythonAvailable = null;
|
|
1956
|
+
async function preflightSyntaxPy(files) {
|
|
1957
|
+
const pyFiles = files.filter((f) => f.path.endsWith(".py"));
|
|
1958
|
+
if (pyFiles.length === 0) return null;
|
|
1959
|
+
if (pythonAvailable === null) pythonAvailable = await probePython();
|
|
1960
|
+
if (!pythonAvailable) return null;
|
|
1961
|
+
const failures = [];
|
|
1962
|
+
await Promise.all(
|
|
1963
|
+
pyFiles.map(async (f) => {
|
|
1964
|
+
const script = [
|
|
1965
|
+
"import sys, ast, json",
|
|
1966
|
+
"src = sys.stdin.read()",
|
|
1967
|
+
"try:",
|
|
1968
|
+
" ast.parse(src, filename=sys.argv[1])",
|
|
1969
|
+
" sys.exit(0)",
|
|
1970
|
+
"except SyntaxError as e:",
|
|
1971
|
+
` print(json.dumps({'line': e.lineno, 'col': e.offset, 'msg': str(e.msg)}))`,
|
|
1972
|
+
" sys.exit(1)"
|
|
1973
|
+
].join("\n");
|
|
1974
|
+
const result = await runPython(["-I", "-c", script, f.path], f.content);
|
|
1975
|
+
if (result.exit === 0) return;
|
|
1976
|
+
try {
|
|
1977
|
+
const parsed = JSON.parse(result.stdout.trim());
|
|
1978
|
+
failures.push({ file: f.path, line: parsed.line, message: `${parsed.msg ?? "SyntaxError"}${parsed.col ? ` (col ${parsed.col})` : ""}` });
|
|
1979
|
+
} catch {
|
|
1980
|
+
failures.push({ file: f.path, message: (result.stdout || result.stderr || "SyntaxError").slice(0, 400) });
|
|
1981
|
+
}
|
|
1982
|
+
})
|
|
1983
|
+
);
|
|
1984
|
+
if (failures.length === 0) return null;
|
|
1985
|
+
return {
|
|
1986
|
+
typecheckOk: false,
|
|
1987
|
+
typecheckOutput: failures.map((f) => `${f.file}${f.line ? `:${f.line}` : ""}: ${f.message}`).join("\n"),
|
|
1988
|
+
passed: 0,
|
|
1989
|
+
failed: failures.length,
|
|
1990
|
+
total: failures.length,
|
|
1991
|
+
failures: failures.map((f) => ({
|
|
1992
|
+
name: `syntax (${f.file}${f.line ? `:${f.line}` : ""})`,
|
|
1993
|
+
message: f.message
|
|
1994
|
+
})),
|
|
1995
|
+
durationMs: 0,
|
|
1996
|
+
ranOn: "preflight:ast",
|
|
1997
|
+
passedNames: []
|
|
1998
|
+
};
|
|
1999
|
+
}
|
|
2000
|
+
async function probePython() {
|
|
2001
|
+
const { exit } = await runPython(["-c", "import ast"], "");
|
|
2002
|
+
return exit === 0;
|
|
2003
|
+
}
|
|
2004
|
+
function runPython(args, stdin) {
|
|
2005
|
+
return new Promise((resolve) => {
|
|
2006
|
+
const cmd = process.platform === "win32" ? "python" : "python3";
|
|
2007
|
+
const child = spawn(cmd, args, { stdio: ["pipe", "pipe", "pipe"] });
|
|
2008
|
+
let stdout = "";
|
|
2009
|
+
let stderr = "";
|
|
2010
|
+
child.stdout.on("data", (b) => stdout += b.toString());
|
|
2011
|
+
child.stderr.on("data", (b) => stderr += b.toString());
|
|
2012
|
+
child.on("error", () => resolve({ exit: 127, stdout, stderr: stderr || "python not found" }));
|
|
2013
|
+
child.on("close", (code) => resolve({ exit: code ?? 1, stdout, stderr }));
|
|
2014
|
+
child.stdin.end(stdin);
|
|
2015
|
+
});
|
|
2016
|
+
}
|
|
1502
2017
|
|
|
1503
2018
|
// src/prompts.ts
|
|
1504
2019
|
var PLAN_SYSTEM = `You are the planning step of patchbay, an agent that builds API integrations.
|
|
@@ -1653,20 +2168,213 @@ ${JSON.stringify(plan, null, 2)}
|
|
|
1653
2168
|
Documentation pages:
|
|
1654
2169
|
${pages}`;
|
|
1655
2170
|
}
|
|
1656
|
-
|
|
2171
|
+
var GENERATE_SYSTEM_PY = `You are the code-generation step of patchbay. Write a production-quality Python 3.12 connector and its tests.
|
|
2172
|
+
|
|
2173
|
+
Hard rules:
|
|
2174
|
+
- Exactly three files: connector.py, test_connector.py, README.md. Output each as
|
|
2175
|
+
<file path="connector.py">
|
|
2176
|
+
...code...
|
|
2177
|
+
</file>
|
|
2178
|
+
with no markdown fences and no prose outside the file blocks.
|
|
2179
|
+
- No third-party runtime dependencies beyond the Python standard library \u2014 use httpx ONLY
|
|
2180
|
+
via dependency injection, never imported at module scope. Accept an injected transport in
|
|
2181
|
+
the config so tests can pass a fake.
|
|
2182
|
+
- Secrets come from the config dict, never hard-coded. Document the env var names from the spec in the README.
|
|
2183
|
+
- connector.py exports: a typed \`ConnectorConfig\` TypedDict (or dataclass), a typed \`SourcePayload\`
|
|
2184
|
+
TypedDict for the event, and one async function named \`handle(payload, config)\` that returns
|
|
2185
|
+
a typed result dict with the created ids. Prefer async def throughout \u2014 the fake transport is async.
|
|
2186
|
+
- Validate the input: raise a descriptive ValueError when a required field is missing.
|
|
2187
|
+
- Non-2xx responses from the target must raise an Exception that includes the status code and the endpoint URL.
|
|
2188
|
+
- Follow the spec exactly for URLs, HTTP methods, auth header/query names and body shapes.
|
|
2189
|
+
- test_connector.py uses pytest with \`pytest.mark.asyncio\` for async tests (import pytest; the
|
|
2190
|
+
sandbox has pytest-asyncio installed in auto mode so no decorator is strictly required but you
|
|
2191
|
+
may use it). Use a fake transport callable that records calls. Cover at least: happy path with
|
|
2192
|
+
the exact request bodies, the auth mechanism, the field mapping, a missing required field,
|
|
2193
|
+
and a non-2xx target response. Tests must be hermetic: no network, no sleeps, no real env vars.
|
|
2194
|
+
- fixtures.py is provided by patchbay, read-only, do not output it. It exports SOURCE_EXAMPLE (the
|
|
2195
|
+
documented source payload), TARGET_ENDPOINTS and contract_fetch(), a fake async transport that
|
|
2196
|
+
answers documented endpoints (URL placeholders like {id} or region hosts are handled) and raises on anything else.
|
|
2197
|
+
test_connector.py MUST contain this block, adapted to your types and config. Put it at the TOP LEVEL
|
|
2198
|
+
(not nested inside a class) and keep the function name starting with 'test_contract' so patchbay can detect it:
|
|
2199
|
+
import pytest
|
|
2200
|
+
from fixtures import SOURCE_EXAMPLE, contract_fetch
|
|
2201
|
+
from connector import handle
|
|
2202
|
+
|
|
2203
|
+
@pytest.mark.asyncio
|
|
2204
|
+
async def test_contract_handles_documented_example():
|
|
2205
|
+
fetch, calls = contract_fetch()
|
|
2206
|
+
result = await handle(SOURCE_EXAMPLE, { <a valid config dict>, 'fetch': fetch })
|
|
2207
|
+
assert len(calls) > 0
|
|
2208
|
+
Configure the connector in that test so the documented example is valid input. If the example lacks
|
|
2209
|
+
a field the connector needs, make the connector handle that case (skip, fallback or clear error)
|
|
2210
|
+
rather than editing the example.
|
|
2211
|
+
- Do NOT reject SOURCE_EXAMPLE based on wall-clock time. If the connector guards against stale webhooks
|
|
2212
|
+
(timestamp age, signature expiry, nonce window), either freeze time in the contract test with
|
|
2213
|
+
freezegun or by monkeypatching time.time, or make the guard window large enough that the documented
|
|
2214
|
+
example always validates. The contract test must never fail with "timestamp too old".
|
|
2215
|
+
- DON'T OVER-ENGINEER. Only add retry logic, pagination, HMAC verification, idempotency keys,
|
|
2216
|
+
or caching if the user prompt EXPLICITLY asks for it (words like "retry", "429", "backoff",
|
|
2217
|
+
"paginate", "cursor", "verify signature", "HMAC", "idempotent"). A simple "when X happens, do Y"
|
|
2218
|
+
prompt is one call per endpoint \u2014 do not invent retry tests the connector then has to pass.
|
|
2219
|
+
Fewer tests that pass > more tests that fail. The contract test is mandatory; everything else
|
|
2220
|
+
earns its keep by exercising something the prompt actually requested.
|
|
2221
|
+
- RETRY SEMANTICS (only when the prompt asks for retries). "Retry up to N times" means at most
|
|
2222
|
+
N retry attempts after the initial request, so total HTTP calls are at most N+1. On a 429 (or
|
|
2223
|
+
other retryable status), compute the next delay as
|
|
2224
|
+
\`delay = max(parse_retry_after(res), base_ms * 2 ** retry_index)\` where retry_index starts at 0
|
|
2225
|
+
for the first retry. Retry-After may be an integer-in-seconds OR an HTTP-date string \u2014 handle
|
|
2226
|
+
both and fall back to pure exponential backoff when the header is absent or unparseable.
|
|
2227
|
+
Worked pattern \u2014 connector sleeps via a monkeypatch-friendly sleep hook, tests patch it to a
|
|
2228
|
+
no-op so the full retry sequence completes instantly:
|
|
2229
|
+
|
|
2230
|
+
# connector.py
|
|
2231
|
+
import asyncio
|
|
2232
|
+
async def _sleep(seconds: float) -> None:
|
|
2233
|
+
await asyncio.sleep(seconds)
|
|
2234
|
+
async def handle(payload, config):
|
|
2235
|
+
max_retries = config.get('max_retries', 3)
|
|
2236
|
+
base_ms = config.get('base_backoff_ms', 100)
|
|
2237
|
+
fetch = config['fetch']
|
|
2238
|
+
for attempt in range(max_retries + 1): # N retries \u2192 N+1 total calls
|
|
2239
|
+
res = await fetch(url, method='POST', body=body, headers=auth_headers)
|
|
2240
|
+
if res.status_code < 400:
|
|
2241
|
+
return {'ok': True, 'response': res.json()}
|
|
2242
|
+
if res.status_code != 429 or attempt == max_retries:
|
|
2243
|
+
raise Exception(f'HTTP {res.status_code} from {url}')
|
|
2244
|
+
retry_after_ms = _parse_retry_after(res.headers.get('Retry-After'))
|
|
2245
|
+
await _sleep(max(retry_after_ms, base_ms * 2 ** attempt) / 1000)
|
|
2246
|
+
|
|
2247
|
+
# test_connector.py
|
|
2248
|
+
import connector as _c
|
|
2249
|
+
async def test_retry_on_429_then_success(monkeypatch):
|
|
2250
|
+
monkeypatch.setattr(_c, '_sleep', lambda s: _noop()) # see below
|
|
2251
|
+
calls_seen = []
|
|
2252
|
+
def handler(req):
|
|
2253
|
+
calls_seen.append(1)
|
|
2254
|
+
if len(calls_seen) == 1:
|
|
2255
|
+
return json_response({'error': 'rate limited'}, 429, headers={'Retry-After': '0'})
|
|
2256
|
+
return json_response({'id': 1}, 200)
|
|
2257
|
+
fetch, calls = fake_fetch(handler)
|
|
2258
|
+
result = await handle(SOURCE_EXAMPLE, { ..., 'fetch': fetch, 'max_retries': 2 })
|
|
2259
|
+
assert len(calls) == 2 # one 429, one success
|
|
2260
|
+
async def _noop():
|
|
2261
|
+
return None
|
|
2262
|
+
|
|
2263
|
+
The tests MUST monkeypatch the module-level \`_sleep\` symbol you expose, not asyncio.sleep itself
|
|
2264
|
+
(patching asyncio.sleep globally breaks pytest-asyncio). Call counts are the contract: success
|
|
2265
|
+
on attempt K uses K+1 total calls; exhaustion of N retries uses N+1 total calls, never more.
|
|
2266
|
+
- CURSOR PAGINATION (only when the prompt asks for pagination). Terminate pagination loops on the
|
|
2267
|
+
API's explicit end signal \u2014 for GraphQL check pageInfo.hasNextPage is False, for REST check the
|
|
2268
|
+
absence or None of the cursor field (next_cursor, after, starting_after, page_info). Never loop
|
|
2269
|
+
while "cursor is truthy" alone \u2014 many APIs return an empty-string or placeholder cursor on the
|
|
2270
|
+
final page. Pass the cursor under the exact field name the vendor documents. Hard-cap every
|
|
2271
|
+
pagination loop at 50 iterations as a safety net.
|
|
2272
|
+
- URL FIDELITY (contract-critical). Your fetch calls MUST hit URLs that match the entries in
|
|
2273
|
+
TARGET_ENDPOINTS from fixtures.py. patchbay wrote that file from the researched spec and the
|
|
2274
|
+
URL matcher accepts placeholder segments ({id}, :id, <id>) + region subdomains \u2014 but NOT a
|
|
2275
|
+
different base host, a different path shape, or a different HTTP method. If a documented
|
|
2276
|
+
endpoint uses PATCH to \`/sobjects/Account/Website__c/{website}\`, do not POST to
|
|
2277
|
+
\`/sobjects/Account\` \u2014 contract_fetch will raise because that's a different endpoint.
|
|
2278
|
+
Build each call's URL by taking TARGET_ENDPOINTS[i]['url'] as the template and substituting
|
|
2279
|
+
placeholders with payload values at the SAME POSITIONS; do not reconstruct URLs from scratch.
|
|
2280
|
+
- IDEMPOTENT UPSERT PATTERNS. Vendors use distinctive URL shapes for upserts; use the exact one:
|
|
2281
|
+
|
|
2282
|
+
# Mailchimp \u2014 PUT to subscriber-hash URL (md5(email.lower())):
|
|
2283
|
+
import hashlib
|
|
2284
|
+
subscriber_hash = hashlib.md5(email.lower().encode('utf-8')).hexdigest()
|
|
2285
|
+
url = f"https://{dc}.api.mailchimp.com/3.0/lists/{list_id}/members/{subscriber_hash}"
|
|
2286
|
+
await fetch(url, method='PUT', body={'email_address': email, 'status_if_new': 'subscribed', 'merge_fields': {...}}, headers=auth)
|
|
2287
|
+
|
|
2288
|
+
# Salesforce \u2014 external-id upsert via PATCH on the field value in the path:
|
|
2289
|
+
from urllib.parse import quote
|
|
2290
|
+
url = f"{instance}/services/data/v60.0/sobjects/Account/Website__c/{quote(website)}"
|
|
2291
|
+
await fetch(url, method='PATCH', body={'Name': name, 'Industry': industry}, headers=auth)
|
|
2292
|
+
# response 201 (created) or 204 (updated); check status_code to distinguish
|
|
2293
|
+
|
|
2294
|
+
# HubSpot \u2014 contact upsert by email (PATCH with idProperty=email):
|
|
2295
|
+
url = f"https://api.hubapi.com/crm/v3/objects/contacts/{quote(email)}?idProperty=email"
|
|
2296
|
+
await fetch(url, method='PATCH', body={'properties': {...}}, headers=auth)
|
|
2297
|
+
|
|
2298
|
+
- MULTI-STEP CRUD / UPSERT PATTERN. When the integration needs to create several related objects
|
|
2299
|
+
in sequence (Pipedrive org \u2192 person \u2192 deal, Salesforce account \u2192 contact \u2192 opportunity, HubSpot
|
|
2300
|
+
contact \u2192 company \u2192 association), do them inside one handle() in order, carrying the id from
|
|
2301
|
+
each response into the next request. Each POST uses its own \`url\` from TARGET_ENDPOINTS;
|
|
2302
|
+
contract_fetch answers each one with the documented response.
|
|
2303
|
+
|
|
2304
|
+
# connector.py \u2014 Pipedrive org/person/deal sequence
|
|
2305
|
+
org_res = await fetch(ORG_URL, method='POST', body={'name': org_name}, headers=auth)
|
|
2306
|
+
org_id = org_res.json()['data']['id']
|
|
2307
|
+
person_res = await fetch(PERSON_URL, method='POST',
|
|
2308
|
+
body={'name': person_name, 'org_id': org_id, 'email': [email]},
|
|
2309
|
+
headers=auth)
|
|
2310
|
+
person_id = person_res.json()['data']['id']
|
|
2311
|
+
deal_res = await fetch(DEAL_URL, method='POST',
|
|
2312
|
+
body={'title': title, 'person_id': person_id, 'org_id': org_id},
|
|
2313
|
+
headers=auth)
|
|
2314
|
+
return {'org_id': org_id, 'person_id': person_id, 'deal_id': deal_res.json()['data']['id']}
|
|
2315
|
+
|
|
2316
|
+
Field names and nesting come from the fixtures' response examples \u2014 if the documented response
|
|
2317
|
+
is {"data": {"id": 123}}, read \`.json()['data']['id']\`, not \`.json()['id']\`.
|
|
2318
|
+
- URL-ENCODED QUERY PARAMETERS. When the API uses GET with a formula or filter in the query string
|
|
2319
|
+
(Airtable filterByFormula, HubSpot search, Linear GraphQL variables), build the query with
|
|
2320
|
+
urllib.parse.quote on the value, not string concatenation. Preserve the quoting style the vendor
|
|
2321
|
+
documents (Airtable wants \`{Email}='x@y.co'\`, with the braces literal and the single quotes
|
|
2322
|
+
URL-encoded):
|
|
2323
|
+
|
|
2324
|
+
from urllib.parse import quote
|
|
2325
|
+
formula = "{Email}='" + email + "'"
|
|
2326
|
+
list_url = f"{BASE}/{table}?filterByFormula={quote(formula)}&maxRecords=1"
|
|
2327
|
+
list_res = await fetch(list_url, method='GET', headers=auth)
|
|
2328
|
+
records = list_res.json().get('records', [])
|
|
2329
|
+
if records:
|
|
2330
|
+
record_id = records[0]['id']
|
|
2331
|
+
patch_url = f"{BASE}/{table}/{record_id}"
|
|
2332
|
+
# PATCH only the fields the payload has
|
|
2333
|
+
only_set = {k: v for k, v in {'Status': status, 'Notes': notes}.items() if v}
|
|
2334
|
+
await fetch(patch_url, method='PATCH', body={'fields': only_set}, headers=auth)
|
|
2335
|
+
- For every other test use fake_fetch and json_response from fixtures instead of writing your own mock:
|
|
2336
|
+
from fixtures import fake_fetch, json_response
|
|
2337
|
+
def handler(req):
|
|
2338
|
+
if req['url'].endswith('/deals'):
|
|
2339
|
+
return json_response({'id': 2}, 201)
|
|
2340
|
+
return json_response({'error': 'x'}, 500)
|
|
2341
|
+
fetch, calls = fake_fetch(handler)
|
|
2342
|
+
Each recorded call is a dict: {'method', 'url', 'body' (bytes/str as sent), 'json' (parsed body or None),
|
|
2343
|
+
'headers' (dict)}. Assert on calls[i]['json'], do not json.loads it again.
|
|
2344
|
+
json_response signature is \`json_response(body, status=200, headers=None)\`. Pass response headers
|
|
2345
|
+
via the \`headers=\` kwarg when a retry test needs Retry-After or when the connector reads a header
|
|
2346
|
+
off the response (e.g. \`json_response({'ok': True}, 200, headers={'Retry-After': '2'})\`).
|
|
2347
|
+
- Code must be clean Python 3.12: type hints on every function signature, no unused imports, no
|
|
2348
|
+
bare except. Standard library modules only in top-level imports.
|
|
2349
|
+
- CHAINED FLOWS. When spec.target has MORE THAN ONE endpoint, call them in the documented order
|
|
2350
|
+
inside handle(), using data from one response to shape the next request. The contract test must
|
|
2351
|
+
exercise every documented endpoint \u2014 contract_fetch answers each one with its documented response,
|
|
2352
|
+
so a chained flow like "Zendesk ticket -> GitHub issue -> Zendesk ticket comment" means handle()
|
|
2353
|
+
POSTs the GitHub issue, reads its html_url (or id) from the response, then POSTs the Zendesk
|
|
2354
|
+
comment carrying that URL. One auth header per vendor \u2014 read each vendor's env var / secret from
|
|
2355
|
+
config under distinct keys (github_token vs zendesk_api_token). The response-example fields the
|
|
2356
|
+
fixtures hand you are the ONLY fields you may assume present; a documented field missing from the
|
|
2357
|
+
response example is fine to read with .get(... , None).
|
|
2358
|
+
- README.md: what it does, config/env vars, a usage example (async code), and the assumptions from the plan.`;
|
|
2359
|
+
function generateUser(plan, spec, fixtures, lang = "ts") {
|
|
2360
|
+
const fixturesLabel = lang === "py" ? "fixtures.py" : "src/fixtures.ts";
|
|
1657
2361
|
return `Plan:
|
|
1658
2362
|
${JSON.stringify(plan, null, 2)}
|
|
1659
2363
|
|
|
1660
2364
|
API spec:
|
|
1661
2365
|
${JSON.stringify(spec, null, 2)}
|
|
1662
2366
|
|
|
1663
|
-
|
|
2367
|
+
${fixturesLabel} (provided by patchbay, read-only):
|
|
1664
2368
|
${fixtures}
|
|
1665
2369
|
|
|
1666
2370
|
Write the three files now.`;
|
|
1667
2371
|
}
|
|
1668
|
-
function repairUser(filesBlock, report, context) {
|
|
1669
|
-
const
|
|
2372
|
+
function repairUser(filesBlock, report, context, lang = "ts") {
|
|
2373
|
+
const tcLabel = lang === "py" ? "py_compile" : "tsc --noEmit";
|
|
2374
|
+
const runnerLabel = lang === "py" ? "pytest" : "vitest";
|
|
2375
|
+
const tcPass = lang === "py" ? "Static check passed." : "Typecheck passed.";
|
|
2376
|
+
const tcFail = lang === "py" ? "Static check failed:" : "Typecheck failed:";
|
|
2377
|
+
const tcLine = report.typecheckOk ? tcPass : `${tcFail}
|
|
1670
2378
|
${report.typecheckOutput.slice(0, 4e3)}`;
|
|
1671
2379
|
const failures = report.failures.map((f) => `- ${f.name}
|
|
1672
2380
|
${f.message}`).join("\n\n");
|
|
@@ -1675,11 +2383,11 @@ ${f.message}`).join("\n\n");
|
|
|
1675
2383
|
Previous attempts:
|
|
1676
2384
|
${context.previousAttempts.map((a) => ` attempt ${a.attempt}: ${a.summary}`).join("\n")}
|
|
1677
2385
|
` : "";
|
|
1678
|
-
return `These files were run in an isolated sandbox (
|
|
2386
|
+
return `These files were run in an isolated sandbox (${tcLabel}, then ${runnerLabel}).
|
|
1679
2387
|
${header ? `
|
|
1680
2388
|
${header}
|
|
1681
2389
|
` : ""}
|
|
1682
|
-
${
|
|
2390
|
+
${tcLine}
|
|
1683
2391
|
|
|
1684
2392
|
Tests: ${report.passed} passed, ${report.failed} failed of ${report.total}.
|
|
1685
2393
|
${failures ? `Failures:
|
|
@@ -1692,6 +2400,56 @@ Fix the code so everything passes. Fix the connector when it contradicts the spe
|
|
|
1692
2400
|
Return all three files again in full, using the same <file path="..."> format.`;
|
|
1693
2401
|
}
|
|
1694
2402
|
|
|
2403
|
+
// src/lang.ts
|
|
2404
|
+
var TS = {
|
|
2405
|
+
label: "TypeScript",
|
|
2406
|
+
requiredFiles: ["src/connector.ts", "src/connector.test.ts", "README.md"],
|
|
2407
|
+
fixturesPath: FIXTURES_PATH,
|
|
2408
|
+
generateSystem: GENERATE_SYSTEM,
|
|
2409
|
+
defaultOpenFile: "src/connector.ts",
|
|
2410
|
+
typecheckLabel: "typecheck",
|
|
2411
|
+
testRunnerLabel: "vitest",
|
|
2412
|
+
sandbox: {
|
|
2413
|
+
registry: "docker://docker.io/library/node:22-slim",
|
|
2414
|
+
tag: "patchbay-node:22",
|
|
2415
|
+
prepareCommand: [
|
|
2416
|
+
"mkdir -p /work/src",
|
|
2417
|
+
"cd /work",
|
|
2418
|
+
`printf '%s' '{"name":"connector","private":true,"type":"module"}' > package.json`,
|
|
2419
|
+
"npm install --no-audit --no-fund --loglevel=error -D vitest@3.2.4 typescript@5.6.3 @types/node@22"
|
|
2420
|
+
].join(" && "),
|
|
2421
|
+
testCommand: TEST_COMMAND
|
|
2422
|
+
},
|
|
2423
|
+
renderFixtures,
|
|
2424
|
+
contractMissing,
|
|
2425
|
+
parseTestOutput
|
|
2426
|
+
};
|
|
2427
|
+
var PY = {
|
|
2428
|
+
label: "Python 3.12",
|
|
2429
|
+
requiredFiles: ["connector.py", "test_connector.py", "README.md"],
|
|
2430
|
+
fixturesPath: FIXTURES_PATH_PY,
|
|
2431
|
+
generateSystem: GENERATE_SYSTEM_PY,
|
|
2432
|
+
defaultOpenFile: "connector.py",
|
|
2433
|
+
typecheckLabel: "syntax check",
|
|
2434
|
+
testRunnerLabel: "pytest",
|
|
2435
|
+
sandbox: {
|
|
2436
|
+
registry: "docker://docker.io/library/python:3.12-slim",
|
|
2437
|
+
tag: "patchbay-python:3.12",
|
|
2438
|
+
prepareCommand: [
|
|
2439
|
+
"mkdir -p /work",
|
|
2440
|
+
"cd /work",
|
|
2441
|
+
"pip install --quiet --no-cache-dir pytest==8.3.3 pytest-asyncio==0.24.0 pytest-json-report==1.5.0",
|
|
2442
|
+
// %b interprets \n as a real newline; %s would write literal backslash-n.
|
|
2443
|
+
`printf '%b' '[pytest]\\nasyncio_mode = auto\\n' > pytest.ini`
|
|
2444
|
+
].join(" && "),
|
|
2445
|
+
testCommand: TEST_COMMAND_PY
|
|
2446
|
+
},
|
|
2447
|
+
renderFixtures: renderFixturesPy,
|
|
2448
|
+
contractMissing: contractMissingPy,
|
|
2449
|
+
parseTestOutput: parsePytestOutput
|
|
2450
|
+
};
|
|
2451
|
+
var LANG = { ts: TS, py: PY };
|
|
2452
|
+
|
|
1695
2453
|
// src/sources.ts
|
|
1696
2454
|
var THIRD_PARTY = [
|
|
1697
2455
|
"medium.com",
|
|
@@ -1739,15 +2497,15 @@ function isOfficial(url, app) {
|
|
|
1739
2497
|
}
|
|
1740
2498
|
|
|
1741
2499
|
// src/run.ts
|
|
1742
|
-
var REQUIRED_FILES = ["src/connector.ts", "src/connector.test.ts", "README.md"];
|
|
1743
2500
|
async function* runAgent(prompt, deps) {
|
|
1744
2501
|
const { config, llm, search, sandbox } = deps;
|
|
2502
|
+
const lang = config.lang ?? "ts";
|
|
1745
2503
|
const queue = [];
|
|
1746
2504
|
const log = (step) => (message) => queue.push({ type: "log", step, message });
|
|
1747
2505
|
const drain = function* () {
|
|
1748
2506
|
while (queue.length) yield queue.shift();
|
|
1749
2507
|
};
|
|
1750
|
-
yield { type: "mode", mode: "live" };
|
|
2508
|
+
yield { type: "mode", mode: "live", lang };
|
|
1751
2509
|
let current = "plan";
|
|
1752
2510
|
try {
|
|
1753
2511
|
yield { type: "step", step: "plan", status: "start" };
|
|
@@ -1764,7 +2522,7 @@ async function* runAgent(prompt, deps) {
|
|
|
1764
2522
|
yield { type: "step", step: "research", status: "done", detail: `${spec.target.length + 1} endpoints from ${sources.length} pages` };
|
|
1765
2523
|
current = "generate";
|
|
1766
2524
|
yield { type: "step", step: "generate", status: "start" };
|
|
1767
|
-
const fixtures = renderFixtures(spec);
|
|
2525
|
+
const fixtures = LANG[lang].renderFixtures(spec);
|
|
1768
2526
|
let files = withFixtures(await generate(plan, spec, fixtures.content, llm, config), fixtures);
|
|
1769
2527
|
yield { type: "files", attempt: 1, files };
|
|
1770
2528
|
yield { type: "step", step: "generate", status: "done", detail: files.map((f) => f.path).join(", ") };
|
|
@@ -1781,12 +2539,12 @@ async function* runAgent(prompt, deps) {
|
|
|
1781
2539
|
const history = [];
|
|
1782
2540
|
for (; ; ) {
|
|
1783
2541
|
if (!skipInitialTest) {
|
|
1784
|
-
const preflight = await preflightSyntax(files);
|
|
2542
|
+
const preflight = await preflightSyntax(files, lang);
|
|
1785
2543
|
if (preflight) {
|
|
1786
|
-
report =
|
|
2544
|
+
report = withContractFor(lang, preflight, files);
|
|
1787
2545
|
queue.push({ type: "log", step: "verify", message: `Preflight parse failed (${preflight.failed} file${preflight.failed > 1 ? "s" : ""}), skipping sandbox` });
|
|
1788
2546
|
} else {
|
|
1789
|
-
report =
|
|
2547
|
+
report = withContractFor(lang, await sandbox.runTests(baseImage, files, log("verify")), files);
|
|
1790
2548
|
}
|
|
1791
2549
|
yield* drain();
|
|
1792
2550
|
yield { type: "tests", attempt, report };
|
|
@@ -1805,6 +2563,7 @@ async function* runAgent(prompt, deps) {
|
|
|
1805
2563
|
sandbox,
|
|
1806
2564
|
config,
|
|
1807
2565
|
baseImage,
|
|
2566
|
+
lang,
|
|
1808
2567
|
withFixtures: (next) => withFixtures(next, fixtures),
|
|
1809
2568
|
context: repairCtx
|
|
1810
2569
|
}, log("verify"));
|
|
@@ -1837,7 +2596,8 @@ async function* runAgent(prompt, deps) {
|
|
|
1837
2596
|
}
|
|
1838
2597
|
async function* researchAgent(prompt, deps) {
|
|
1839
2598
|
const { config, llm, search } = deps;
|
|
1840
|
-
|
|
2599
|
+
const lang = config.lang ?? "ts";
|
|
2600
|
+
yield { type: "mode", mode: "live", lang };
|
|
1841
2601
|
let current = "plan";
|
|
1842
2602
|
try {
|
|
1843
2603
|
yield { type: "step", step: "plan", status: "start" };
|
|
@@ -1867,7 +2627,8 @@ async function* researchAgent(prompt, deps) {
|
|
|
1867
2627
|
}
|
|
1868
2628
|
async function* verifyAgent(files, deps) {
|
|
1869
2629
|
const { config, llm, sandbox } = deps;
|
|
1870
|
-
|
|
2630
|
+
const lang = config.lang ?? "ts";
|
|
2631
|
+
yield { type: "mode", mode: "live", lang };
|
|
1871
2632
|
const queue = [];
|
|
1872
2633
|
const log = (message) => queue.push({ type: "log", step: "verify", message });
|
|
1873
2634
|
const drain = function* () {
|
|
@@ -1886,7 +2647,7 @@ async function* verifyAgent(files, deps) {
|
|
|
1886
2647
|
const history = [];
|
|
1887
2648
|
for (; ; ) {
|
|
1888
2649
|
if (!skipInitialTest) {
|
|
1889
|
-
const preflight = await preflightSyntax(current);
|
|
2650
|
+
const preflight = await preflightSyntax(current, lang);
|
|
1890
2651
|
if (preflight) {
|
|
1891
2652
|
report = preflight;
|
|
1892
2653
|
queue.push({ type: "log", step: "verify", message: `Preflight parse failed (${preflight.failed} file${preflight.failed > 1 ? "s" : ""}), skipping sandbox` });
|
|
@@ -1910,6 +2671,7 @@ async function* verifyAgent(files, deps) {
|
|
|
1910
2671
|
sandbox,
|
|
1911
2672
|
config,
|
|
1912
2673
|
baseImage,
|
|
2674
|
+
lang,
|
|
1913
2675
|
withFixtures: (next) => next,
|
|
1914
2676
|
context: repairCtx
|
|
1915
2677
|
}, log);
|
|
@@ -1981,24 +2743,27 @@ async function research(plan, search, llm, config, log) {
|
|
|
1981
2743
|
return { spec, sources: picked.map((h) => ({ title: h.title, url: h.url, official: isOfficial(h.url, h.app) })) };
|
|
1982
2744
|
}
|
|
1983
2745
|
async function generate(plan, spec, fixtures, llm, config) {
|
|
2746
|
+
const lang = config.lang ?? "ts";
|
|
1984
2747
|
const answer = await llm.chat(config.models.strong, [
|
|
1985
|
-
{ role: "system", content:
|
|
1986
|
-
{ role: "user", content: generateUser(plan, spec, fixtures) }
|
|
1987
|
-
], { maxTokens:
|
|
1988
|
-
return requireFiles(parseFileBlocks(answer));
|
|
2748
|
+
{ role: "system", content: LANG[lang].generateSystem },
|
|
2749
|
+
{ role: "user", content: generateUser(plan, spec, fixtures, lang) }
|
|
2750
|
+
], { maxTokens: 24e3, temperature: 0.1 });
|
|
2751
|
+
return requireFiles(parseFileBlocks(answer), lang);
|
|
1989
2752
|
}
|
|
1990
2753
|
async function repair(files, report, llm, config, options = {}) {
|
|
1991
|
-
const
|
|
1992
|
-
|
|
1993
|
-
{ role: "
|
|
1994
|
-
|
|
2754
|
+
const lang = config.lang ?? "ts";
|
|
2755
|
+
const answer = await llm.chat(config.models.repair, [
|
|
2756
|
+
{ role: "system", content: LANG[lang].generateSystem },
|
|
2757
|
+
{ role: "user", content: repairUser(renderFileBlocks(files), report, options.context, lang) }
|
|
2758
|
+
], { maxTokens: 24e3, temperature: options.temperature ?? 0.1 });
|
|
1995
2759
|
const repaired = parseFileBlocks(answer);
|
|
1996
2760
|
const merged = new Map(files.map((f) => [f.path, f]));
|
|
1997
2761
|
for (const file of repaired) merged.set(file.path, file);
|
|
1998
|
-
return requireFiles([...merged.values()]);
|
|
2762
|
+
return requireFiles([...merged.values()], lang);
|
|
1999
2763
|
}
|
|
2000
2764
|
var REPAIR_TEMPERATURES = [0.1, 0.4, 0.7, 0.9];
|
|
2001
2765
|
async function repairParallel(files, report, deps, log) {
|
|
2766
|
+
const lang = deps.lang ?? deps.config.lang ?? "ts";
|
|
2002
2767
|
const n = Math.max(1, Math.min(REPAIR_TEMPERATURES.length, deps.config.parallelRepairs));
|
|
2003
2768
|
const temperatures = REPAIR_TEMPERATURES.slice(0, n);
|
|
2004
2769
|
log(`Repairing with ${n} parallel branch${n > 1 ? "es" : ""} (temperatures ${temperatures.join(", ")})`);
|
|
@@ -2010,12 +2775,13 @@ async function repairParallel(files, report, deps, log) {
|
|
|
2010
2775
|
);
|
|
2011
2776
|
const results = await Promise.all(
|
|
2012
2777
|
candidates.map(async (c) => {
|
|
2013
|
-
const preflight = await preflightSyntax(c.files);
|
|
2778
|
+
const preflight = await preflightSyntax(c.files, lang);
|
|
2014
2779
|
if (preflight) {
|
|
2015
2780
|
log(`[branch ${c.branch}] preflight parse failed (${preflight.failed}), skipping sandbox`);
|
|
2016
|
-
return { ...c, report:
|
|
2781
|
+
return { ...c, report: withContractFor(lang, preflight, c.files) };
|
|
2017
2782
|
}
|
|
2018
|
-
const branchReport =
|
|
2783
|
+
const branchReport = withContractFor(
|
|
2784
|
+
lang,
|
|
2019
2785
|
await deps.sandbox.runTests(deps.baseImage, c.files, (m) => log(`[branch ${c.branch}] ${m}`)),
|
|
2020
2786
|
c.files
|
|
2021
2787
|
);
|
|
@@ -2042,10 +2808,12 @@ async function repairParallel(files, report, deps, log) {
|
|
|
2042
2808
|
}))
|
|
2043
2809
|
};
|
|
2044
2810
|
}
|
|
2045
|
-
function requireFiles(files) {
|
|
2046
|
-
const
|
|
2811
|
+
function requireFiles(files, lang = "ts") {
|
|
2812
|
+
const required = LANG[lang].requiredFiles;
|
|
2813
|
+
const effective = process.env.PATCHBAY_OPTIONAL_README === "1" ? required.filter((p) => p !== "README.md") : required;
|
|
2814
|
+
const missing = effective.filter((path) => !files.some((f) => f.path === path));
|
|
2047
2815
|
if (missing.length) throw new Error(`Model output is missing ${missing.join(", ")}`);
|
|
2048
|
-
return
|
|
2816
|
+
return required.filter((p) => files.some((f) => f.path === p)).map((path) => files.find((f) => f.path === path));
|
|
2049
2817
|
}
|
|
2050
2818
|
async function parseJsonOrAsk(answer, llm, model, system) {
|
|
2051
2819
|
try {
|
|
@@ -2104,14 +2872,20 @@ function summariseReport(report) {
|
|
|
2104
2872
|
}
|
|
2105
2873
|
|
|
2106
2874
|
// src/sandbox.ts
|
|
2107
|
-
var NODE_IMAGE_TAG = "patchbay-node:22";
|
|
2108
|
-
var NODE_IMAGE_REGISTRY = "docker://docker.io/library/node:22-slim";
|
|
2109
2875
|
var PREPARE_COMMAND = [
|
|
2110
2876
|
"mkdir -p /work/src",
|
|
2111
2877
|
"cd /work",
|
|
2112
2878
|
`printf '%s' '{"name":"connector","private":true,"type":"module"}' > package.json`,
|
|
2113
2879
|
"npm install --no-audit --no-fund --loglevel=error -D vitest@3.2.4 typescript@5.6.3 @types/node@22"
|
|
2114
2880
|
].join(" && ");
|
|
2881
|
+
var PREPARE_COMMAND_PY = [
|
|
2882
|
+
"mkdir -p /work",
|
|
2883
|
+
"cd /work",
|
|
2884
|
+
"pip install --quiet --no-cache-dir pytest==8.3.3 pytest-asyncio==0.24.0 pytest-json-report==1.5.0",
|
|
2885
|
+
// printf '%b' interprets \n as a real newline; '%s' would write the literal \n characters
|
|
2886
|
+
// and pytest.ini would be a one-line garble ("unexpected value continuation").
|
|
2887
|
+
`printf '%b' '[pytest]\\nasyncio_mode = auto\\n' > pytest.ini`
|
|
2888
|
+
].join(" && ");
|
|
2115
2889
|
var TSCONFIG2 = JSON.stringify(
|
|
2116
2890
|
{
|
|
2117
2891
|
compilerOptions: {
|
|
@@ -2131,6 +2905,7 @@ var TSCONFIG2 = JSON.stringify(
|
|
|
2131
2905
|
function createNebiusSandbox(opts) {
|
|
2132
2906
|
const doFetch = opts.fetch ?? fetch;
|
|
2133
2907
|
const pollMs = opts.pollIntervalMs ?? 1500;
|
|
2908
|
+
const lang = opts.lang ?? "ts";
|
|
2134
2909
|
let cachedImage = opts.preparedImage;
|
|
2135
2910
|
const headers = (extra = {}) => ({
|
|
2136
2911
|
Authorization: `Bearer ${opts.apiKey}`,
|
|
@@ -2183,33 +2958,44 @@ function createNebiusSandbox(opts) {
|
|
|
2183
2958
|
if (!id) throw new Error("Sandbox did not return an operation id");
|
|
2184
2959
|
return id;
|
|
2185
2960
|
}
|
|
2186
|
-
async function
|
|
2961
|
+
async function ensureBaseImage(log) {
|
|
2962
|
+
const { tag, registry } = LANG[lang].sandbox;
|
|
2187
2963
|
const { body } = await request("/images?tagged=1&limit=200");
|
|
2188
|
-
const existing = (body.images ?? []).find((image2) => image2.tag ===
|
|
2964
|
+
const existing = (body.images ?? []).find((image2) => image2.tag === tag);
|
|
2189
2965
|
if (existing) return existing.uuid;
|
|
2190
|
-
log(
|
|
2966
|
+
log(`Importing ${registry.replace("docker://", "")} into the sandbox registry (one-time)`);
|
|
2191
2967
|
const started = await request("/images/import", {
|
|
2192
2968
|
method: "POST",
|
|
2193
2969
|
headers: { "Content-Type": "application/json" },
|
|
2194
|
-
body: JSON.stringify({ registry: { url:
|
|
2970
|
+
body: JSON.stringify({ registry: { url: registry }, tag, timeout: 600 })
|
|
2195
2971
|
});
|
|
2196
2972
|
const op = await waitFor(operationIdFrom(started.location, started.body), 66e4);
|
|
2197
2973
|
const image = op.result?.image ?? op.result_image_uuid;
|
|
2198
2974
|
if (op.status !== "SUCCESS" || !image) throw new Error(`Image import failed: ${op.error ?? op.status}`);
|
|
2199
2975
|
return image;
|
|
2200
2976
|
}
|
|
2977
|
+
function filesForRun(files) {
|
|
2978
|
+
if (lang === "py") {
|
|
2979
|
+
return files.filter((f) => f.path.endsWith(".py"));
|
|
2980
|
+
}
|
|
2981
|
+
return [{ path: "tsconfig.json", content: TSCONFIG2 }, ...files.filter((f) => f.path.endsWith(".ts"))];
|
|
2982
|
+
}
|
|
2983
|
+
function parse(stdout, operationId) {
|
|
2984
|
+
return LANG[lang].parseTestOutput(stdout, `nebius-sandbox:${operationId}`);
|
|
2985
|
+
}
|
|
2201
2986
|
return {
|
|
2202
2987
|
async prepare(log) {
|
|
2203
2988
|
if (cachedImage) {
|
|
2204
2989
|
log(`Reusing prepared sandbox image ${cachedImage.slice(0, 8)}`);
|
|
2205
2990
|
return cachedImage;
|
|
2206
2991
|
}
|
|
2207
|
-
const
|
|
2208
|
-
|
|
2992
|
+
const baseImage = await ensureBaseImage(log);
|
|
2993
|
+
const { prepareCommand } = LANG[lang].sandbox;
|
|
2994
|
+
log(`Installing ${lang === "py" ? "pytest + pytest-asyncio + pytest-json-report" : "typescript + vitest"} in a sandbox checkpoint`);
|
|
2209
2995
|
const started = await request("/instances", {
|
|
2210
2996
|
method: "POST",
|
|
2211
2997
|
headers: { "Content-Type": "application/json" },
|
|
2212
|
-
body: JSON.stringify({ image:
|
|
2998
|
+
body: JSON.stringify({ image: baseImage, command: prepareCommand, shell: true, timeout: 300, networking: { enabled: true } })
|
|
2213
2999
|
});
|
|
2214
3000
|
const op = await waitFor(operationIdFrom(started.location, started.body), 33e4);
|
|
2215
3001
|
const exit = op.metadata?.result?.state?.exit_code;
|
|
@@ -2221,7 +3007,7 @@ function createNebiusSandbox(opts) {
|
|
|
2221
3007
|
return cachedImage;
|
|
2222
3008
|
},
|
|
2223
3009
|
async runTests(baseImage, files, log) {
|
|
2224
|
-
const all =
|
|
3010
|
+
const all = filesForRun(files);
|
|
2225
3011
|
const mapped = {};
|
|
2226
3012
|
for (const file of all) {
|
|
2227
3013
|
mapped[`/work/${file.path}`] = { uuid: await uploadFile(file.content), mode: "0644" };
|
|
@@ -2232,7 +3018,7 @@ function createNebiusSandbox(opts) {
|
|
|
2232
3018
|
headers: { "Content-Type": "application/json" },
|
|
2233
3019
|
body: JSON.stringify({
|
|
2234
3020
|
image: baseImage,
|
|
2235
|
-
command:
|
|
3021
|
+
command: LANG[lang].sandbox.testCommand,
|
|
2236
3022
|
shell: true,
|
|
2237
3023
|
disposable: true,
|
|
2238
3024
|
timeout: 120,
|
|
@@ -2245,7 +3031,7 @@ function createNebiusSandbox(opts) {
|
|
|
2245
3031
|
if (op.status !== "SUCCESS" && !op.metadata?.result) {
|
|
2246
3032
|
throw new Error(`Sandbox test run ${op.status}: ${op.error ?? "no result"}`);
|
|
2247
3033
|
}
|
|
2248
|
-
return
|
|
3034
|
+
return parse(decode(op.metadata?.result?.stdout), operationId);
|
|
2249
3035
|
}
|
|
2250
3036
|
};
|
|
2251
3037
|
}
|
|
@@ -2290,17 +3076,44 @@ function createTavilyClient(opts) {
|
|
|
2290
3076
|
}
|
|
2291
3077
|
|
|
2292
3078
|
// src/index.ts
|
|
3079
|
+
function buildLlm(config) {
|
|
3080
|
+
const modelOverrides = {};
|
|
3081
|
+
if (config.strongOverride) {
|
|
3082
|
+
modelOverrides[config.models.strong.id] = {
|
|
3083
|
+
baseUrl: config.strongOverride.baseUrl,
|
|
3084
|
+
apiKey: config.strongOverride.apiKey,
|
|
3085
|
+
supportsNemotronThinking: config.strongOverride.provider === "nebius",
|
|
3086
|
+
// Fine-tune vLLM endpoints we deploy use --max-model-len 32768.
|
|
3087
|
+
contextWindow: 32768
|
|
3088
|
+
};
|
|
3089
|
+
}
|
|
3090
|
+
if (config.repairOverride) {
|
|
3091
|
+
modelOverrides[config.models.repair.id] = {
|
|
3092
|
+
baseUrl: config.repairOverride.baseUrl,
|
|
3093
|
+
apiKey: config.repairOverride.apiKey,
|
|
3094
|
+
supportsNemotronThinking: config.repairOverride.provider === "nebius",
|
|
3095
|
+
contextWindow: 32768
|
|
3096
|
+
};
|
|
3097
|
+
}
|
|
3098
|
+
return createNebiusClient({
|
|
3099
|
+
apiKey: config.nebiusApiKey,
|
|
3100
|
+
baseUrl: config.nebiusBaseUrl,
|
|
3101
|
+
supportsNemotronThinking: config.llmProvider === "nebius",
|
|
3102
|
+
modelOverrides: Object.keys(modelOverrides).length ? modelOverrides : void 0
|
|
3103
|
+
});
|
|
3104
|
+
}
|
|
2293
3105
|
function startRun(prompt, config = loadConfig()) {
|
|
2294
3106
|
if (config.mock || missingLiveKeys(config).length > 0) return replayRun();
|
|
2295
3107
|
return runAgent(prompt, {
|
|
2296
3108
|
config,
|
|
2297
|
-
llm:
|
|
3109
|
+
llm: buildLlm(config),
|
|
2298
3110
|
search: createTavilyClient({ apiKey: config.tavilyApiKey }),
|
|
2299
3111
|
sandbox: config.runner === "local" ? createLocalRunner() : createNebiusSandbox({
|
|
2300
3112
|
apiKey: config.nebiusApiKey,
|
|
2301
3113
|
project: config.nebiusProject,
|
|
2302
3114
|
baseUrl: config.sandboxUrl,
|
|
2303
|
-
preparedImage: config.baseImage
|
|
3115
|
+
preparedImage: config.baseImage,
|
|
3116
|
+
lang: config.lang
|
|
2304
3117
|
})
|
|
2305
3118
|
});
|
|
2306
3119
|
}
|
|
@@ -2308,7 +3121,7 @@ function startResearch(prompt, config = loadConfig()) {
|
|
|
2308
3121
|
if (config.mock || missingLiveKeys(config).length > 0) return truncateAt(replayRun(), "spec");
|
|
2309
3122
|
return researchAgent(prompt, {
|
|
2310
3123
|
config,
|
|
2311
|
-
llm:
|
|
3124
|
+
llm: buildLlm(config),
|
|
2312
3125
|
search: createTavilyClient({ apiKey: config.tavilyApiKey })
|
|
2313
3126
|
});
|
|
2314
3127
|
}
|
|
@@ -2318,12 +3131,13 @@ function startVerify(files, config = loadConfig()) {
|
|
|
2318
3131
|
}
|
|
2319
3132
|
return verifyAgent(files, {
|
|
2320
3133
|
config,
|
|
2321
|
-
llm:
|
|
3134
|
+
llm: buildLlm(config),
|
|
2322
3135
|
sandbox: config.runner === "local" ? createLocalRunner() : createNebiusSandbox({
|
|
2323
3136
|
apiKey: config.nebiusApiKey,
|
|
2324
3137
|
project: config.nebiusProject,
|
|
2325
3138
|
baseUrl: config.sandboxUrl,
|
|
2326
|
-
preparedImage: config.baseImage
|
|
3139
|
+
preparedImage: config.baseImage,
|
|
3140
|
+
lang: config.lang
|
|
2327
3141
|
})
|
|
2328
3142
|
});
|
|
2329
3143
|
}
|
|
@@ -2338,9 +3152,21 @@ export {
|
|
|
2338
3152
|
loadConfig,
|
|
2339
3153
|
missingLiveKeys,
|
|
2340
3154
|
parseTestOutput,
|
|
3155
|
+
parsePytestOutput,
|
|
2341
3156
|
isGreen,
|
|
2342
3157
|
REPLAY_RUN,
|
|
2343
3158
|
replayRun,
|
|
3159
|
+
FIXTURES_PATH,
|
|
3160
|
+
FIXTURES_PATH_PY,
|
|
3161
|
+
renderFixtures,
|
|
3162
|
+
contractMissing,
|
|
3163
|
+
withContract,
|
|
3164
|
+
withFixtures,
|
|
3165
|
+
renderFixturesPy,
|
|
3166
|
+
contractMissingPy,
|
|
3167
|
+
contractMissingFor,
|
|
3168
|
+
renderFixturesFor,
|
|
3169
|
+
withContractFor,
|
|
2344
3170
|
runAgent,
|
|
2345
3171
|
researchAgent,
|
|
2346
3172
|
verifyAgent,
|
|
@@ -2353,4 +3179,4 @@ export {
|
|
|
2353
3179
|
startResearch,
|
|
2354
3180
|
startVerify
|
|
2355
3181
|
};
|
|
2356
|
-
//# sourceMappingURL=chunk-
|
|
3182
|
+
//# sourceMappingURL=chunk-DZXK5OTN.js.map
|