@quikit/quikpilot-runner 9.3.0-rc.7 → 9.3.0-rc.9

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/dist/index.js CHANGED
@@ -2083,6 +2083,44 @@ function resolveCodegenMode(env2 = process.env, packetSetting) {
2083
2083
  return parse(packetSetting) ?? parse(env2[CODEGEN_MODE_ENV]) ?? "single";
2084
2084
  }
2085
2085
 
2086
+ // src/refusedReply.ts
2087
+ var REFUSED_OUTCOME = "REFUSED";
2088
+ var REFUSED_STREAK_LIMIT = 3;
2089
+ var MAX_REFUSED_RULES = 8;
2090
+ var MAX_REFUSED_SENTENCES = 8;
2091
+ var MAX_REFUSED_SENTENCE_CHARS = 300;
2092
+ var MAX_REFUSED_RULE_CHARS = 40;
2093
+ var REFUSED_RULE_IDS = [
2094
+ "reply_shape",
2095
+ "last_turn",
2096
+ "no_calls_left",
2097
+ "too_many_calls",
2098
+ "call_id",
2099
+ "unknown_tool",
2100
+ "bad_args",
2101
+ "bad_path",
2102
+ "protected_path",
2103
+ "file_state",
2104
+ "budget",
2105
+ "run_tests_rule",
2106
+ "repeat_call",
2107
+ "done_rule",
2108
+ "length"
2109
+ ];
2110
+ var KNOWN_RULES = new Set(REFUSED_RULE_IDS);
2111
+ function readRefusedAnswer(body) {
2112
+ const { rules, violations, message } = body;
2113
+ if (!Array.isArray(rules) || rules.length < 1 || rules.length > MAX_REFUSED_RULES) return null;
2114
+ if (!rules.every((r) => typeof r === "string" && r.length > 0 && r.length <= MAX_REFUSED_RULE_CHARS)) return null;
2115
+ if (!Array.isArray(violations) || violations.length < 1 || violations.length > MAX_REFUSED_SENTENCES) return null;
2116
+ if (!violations.every((v) => typeof v === "string" && v.length <= MAX_REFUSED_SENTENCE_CHARS)) return null;
2117
+ if (typeof message !== "string" || message.length > MAX_REFUSED_SENTENCE_CHARS) return null;
2118
+ return { rules: [...rules], violations: [...violations], message };
2119
+ }
2120
+ function recordRuleWord(word) {
2121
+ return typeof word === "string" && KNOWN_RULES.has(word) ? word : "other";
2122
+ }
2123
+
2086
2124
  // src/codegenLoopRecord.ts
2087
2125
  var MAX_RECORD_PATHS = 50;
2088
2126
  var MAX_RECORD_PATH_CHARS = 300;
@@ -2090,6 +2128,8 @@ var MAX_RECORD_REASONING_TURNS = 30;
2090
2128
  var MAX_RECORD_REASONING_CHARS = 500;
2091
2129
  var MAX_RECORD_FALLBACK_REASON_CHARS = 40;
2092
2130
  var MAX_RECORD_TEST_RUNS = 3;
2131
+ var MAX_RECORD_REFUSED_TURNS = 30;
2132
+ var MAX_RECORD_REFUSED_RULES = 8;
2093
2133
  var MAX_STATUS_KEYS = 20;
2094
2134
  var MAX_STATUS_KEY_CHARS = 40;
2095
2135
  function whole(n) {
@@ -2160,6 +2200,16 @@ function buildCodegenLoopRecord(loop) {
2160
2200
  seconds: whole(t.seconds)
2161
2201
  });
2162
2202
  }
2203
+ const refusedTurns = [];
2204
+ for (const t of loop.refusedTurns ?? []) {
2205
+ if (refusedTurns.length >= MAX_RECORD_REFUSED_TURNS) {
2206
+ cut = true;
2207
+ break;
2208
+ }
2209
+ if (t.rules.length > MAX_RECORD_REFUSED_RULES) cut = true;
2210
+ const rules = [...new Set(t.rules.map(recordRuleWord))].slice(0, MAX_RECORD_REFUSED_RULES);
2211
+ if (rules.length > 0) refusedTurns.push({ turn: whole(t.turn), rules });
2212
+ }
2163
2213
  return {
2164
2214
  turns: whole(loop.turns),
2165
2215
  toolCalls: whole(loop.toolCalls),
@@ -2174,6 +2224,7 @@ function buildCodegenLoopRecord(loop) {
2174
2224
  ...whole(loop.syntaxWarnings) > 0 ? { syntaxWarnings: whole(loop.syntaxWarnings) } : {},
2175
2225
  ...typeof loop.contractVersion === "string" && loop.contractVersion.length > 0 ? { contractVersion: loop.contractVersion.slice(0, 40) } : {},
2176
2226
  ...testRuns.length > 0 ? { testRuns } : {},
2227
+ ...refusedTurns.length > 0 ? { refusedTurns } : {},
2177
2228
  reasoning,
2178
2229
  ...cut ? { truncated: true } : {}
2179
2230
  };
@@ -12278,6 +12329,9 @@ var MAX_TEST_LIMIT_MS = 6e5;
12278
12329
  var DEFAULT_TEST_LIMIT_MS = 3e5;
12279
12330
  var MAX_LISTED_TESTS = 20;
12280
12331
  var MAX_TEST_NAME_CHARS = 200;
12332
+ var MAX_MESSAGE_CHARS = 120;
12333
+ var LINE_BREAK = /\r\n|\n|\r|&#0*10;|&#x0*a;/i;
12334
+ var CONTROL_CHARS = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F]/g;
12281
12335
  var FRESH_SLACK_MS = 2e3;
12282
12336
  function testTimeLimitMs(baselineMs) {
12283
12337
  if (baselineMs === null) return DEFAULT_TEST_LIMIT_MS;
@@ -12286,16 +12340,40 @@ function testTimeLimitMs(baselineMs) {
12286
12340
  }
12287
12341
  var MESSAGES = {
12288
12342
  tests_passed: "The tests passed.",
12289
- tests_failed: "Some tests failed. output lists their names, regressions first: a regression passed before your change.",
12343
+ tests_failed: "Some tests failed. output lists their names, regressions first: a regression passed before your change. For a test you wrote in this run, messages gives the first line of why it failed.",
12290
12344
  timed_out: "The tests did not finish within the time limit and were stopped.",
12291
12345
  no_report: "The test run produced no readable report, so there is nothing to tell you about the tests.",
12292
12346
  no_change_since_last_run: "Nothing has been changed since the last test run; change something first.",
12293
12347
  tests_exhausted: "No test runs are left. Finish with what you have, or say you cannot complete it.",
12294
- no_progress: "The same tests failed as in the last run. Another run would not help; say you cannot complete it if you cannot fix it.",
12348
+ no_progress: "The same tests failed in the same way as in the last run. Another run would not help; say you cannot complete it if you cannot fix it.",
12295
12349
  tests_unavailable: "The tests could not be run here.",
12296
12350
  tests_too_slow: "This project's test suite is too slow to run inside this loop."
12297
12351
  };
12298
12352
  var sha = (s) => (0, import_node_crypto2.createHash)("sha256").update(s).digest("hex");
12353
+ function firstLine(text) {
12354
+ const line = text.split(LINE_BREAK).map((l) => l.replace(CONTROL_CHARS, "").trim()).find((l) => l.length > 0);
12355
+ if (line === void 0) return "";
12356
+ const safe = scrubSecrets(line);
12357
+ return safe.length > MAX_MESSAGE_CHARS ? `${safe.slice(0, MAX_MESSAGE_CHARS - 1)}\u2026` : safe;
12358
+ }
12359
+ var normalisePath = (p) => p.replace(/\\/g, "/").replace(/^(\.\/)+/, "");
12360
+ function samePlace(a, b) {
12361
+ const x = normalisePath(a);
12362
+ const y = normalisePath(b);
12363
+ return x === y || x.endsWith(`/${y}`) || y.endsWith(`/${x}`);
12364
+ }
12365
+ function ownFailureIds(newFailures, changedPaths, requiredIds) {
12366
+ const own = /* @__PURE__ */ new Set();
12367
+ for (const id of newFailures) {
12368
+ const sep = id.indexOf("::");
12369
+ const file = sep >= 0 ? id.slice(0, sep) : "";
12370
+ const name = sep >= 0 ? id.slice(sep + 2) : id;
12371
+ const inChangedFile = file !== "" && changedPaths.some((p) => samePlace(file, p));
12372
+ const hasToken = requiredIds.some((t) => name.includes(`[${t}]`));
12373
+ if (inChangedFile || hasToken) own.add(id);
12374
+ }
12375
+ return own;
12376
+ }
12299
12377
  function versionOfFiles(files) {
12300
12378
  const h = (0, import_node_crypto2.createHash)("sha256");
12301
12379
  for (const f of [...files].sort((a, b) => a.path.localeCompare(b.path))) {
@@ -12358,6 +12436,8 @@ var RunTestsTool = class {
12358
12436
  failed: entry.failed,
12359
12437
  regressions: entry.regressions,
12360
12438
  newFailures: entry.newFailures,
12439
+ // How many failure messages the AI was shown: a COUNT, never the text.
12440
+ messages: parts.messageCount ?? 0,
12361
12441
  timedOut: entry.timedOut,
12362
12442
  seconds: entry.seconds
12363
12443
  });
@@ -12437,18 +12517,30 @@ var RunTestsTool = class {
12437
12517
  let regressions;
12438
12518
  let newFailures;
12439
12519
  let alreadyFailing;
12520
+ let own = /* @__PURE__ */ new Set();
12521
+ let failureText = /* @__PURE__ */ new Map();
12440
12522
  if (baseline?.snapshot.results) {
12441
12523
  const check = checkForRegressions(baseline.snapshot, after);
12442
12524
  regressions = check.regressions;
12443
12525
  newFailures = check.newFailures;
12444
12526
  alreadyFailing = check.preExisting;
12527
+ own = ownFailureIds(newFailures, files.map((f) => f.path), d.requiredAutomationIds ?? []);
12528
+ failureText = check.failureText;
12445
12529
  } else {
12446
12530
  regressions = [];
12447
12531
  newFailures = [...after.results.entries()].filter(([, ok]) => !ok).map(([id]) => id);
12448
12532
  alreadyFailing = [];
12449
12533
  }
12450
12534
  const ordered = [...regressions, ...newFailures, ...alreadyFailing];
12451
- const names = ordered.slice(0, MAX_LISTED_TESTS).map((n) => scrubSecrets(n).slice(0, MAX_TEST_NAME_CHARS));
12535
+ const listed = ordered.slice(0, MAX_LISTED_TESTS);
12536
+ const names = listed.map((n) => scrubSecrets(n).slice(0, MAX_TEST_NAME_CHARS));
12537
+ const messages = {};
12538
+ listed.forEach((id, i2) => {
12539
+ if (!own.has(id)) return;
12540
+ const line = firstLine(failureText.get(id) ?? "");
12541
+ if (line) messages[names[i2]] = line;
12542
+ });
12543
+ const messageCount = Object.keys(messages).length;
12452
12544
  const output = {
12453
12545
  passed,
12454
12546
  failed,
@@ -12457,18 +12549,20 @@ var RunTestsTool = class {
12457
12549
  newFailures: newFailures.length,
12458
12550
  alreadyFailing: alreadyFailing.length,
12459
12551
  failingTests: names,
12552
+ ...messageCount > 0 ? { messages } : {},
12460
12553
  timedOut: false,
12461
12554
  truncated: ordered.length > names.length
12462
12555
  };
12463
12556
  const counts = { passed, failed, regressions: regressions.length, newFailures: newFailures.length, alreadyFailing: alreadyFailing.length };
12464
12557
  const green = failed === 0 && ran.run.outcome === "success";
12465
- const failingKey = failed > 0 ? sha([...ordered].sort().join("\0")) : null;
12558
+ const messageKey = Object.keys(messages).sort().map((k) => `${k}${messages[k]}`).join("");
12559
+ const failingKey = failed > 0 ? sha(`${[...ordered].sort().join("\0")}${messageKey}`) : null;
12466
12560
  if (!green && failingKey !== null && failingKey === this.lastFailingKey) {
12467
12561
  this.locked = true;
12468
- return this.answer(callId, "no_progress", { output, counts, seconds });
12562
+ return this.answer(callId, "no_progress", { output, counts, seconds, messageCount });
12469
12563
  }
12470
12564
  this.lastFailingKey = failingKey;
12471
- return this.answer(callId, green ? "tests_passed" : "tests_failed", { output, counts, seconds });
12565
+ return this.answer(callId, green ? "tests_passed" : "tests_failed", { output, counts, seconds, messageCount });
12472
12566
  }
12473
12567
  };
12474
12568
  async function executeRunTestsCall(call, tool, ledger, limits) {
@@ -12825,7 +12919,15 @@ async function runCodegenLoop(args, deps) {
12825
12919
  const now = deps.now ?? Date.now;
12826
12920
  const log = deps.log ?? ((e) => console.log(JSON.stringify(e)));
12827
12921
  const filesOnDisk = () => overlay.results().map((r) => ({ path: r.targetPath, content: r.newContent }));
12828
- const tests = args.tests ? new RunTestsTool({ ...args.tests, overlayFiles: filesOnDisk, overlayVersion: () => versionOfFiles(filesOnDisk()), log, now }) : null;
12922
+ const tests = args.tests ? new RunTestsTool({
12923
+ ...args.tests,
12924
+ // The ids of the cases THIS run writes: a failing test named with one is shown to be the run's own (QUIKTR-1541).
12925
+ requiredAutomationIds: args.requiredAutomationIds,
12926
+ overlayFiles: filesOnDisk,
12927
+ overlayVersion: () => versionOfFiles(filesOnDisk()),
12928
+ log,
12929
+ now
12930
+ }) : null;
12829
12931
  let testMs = 0;
12830
12932
  const turnTimeoutMs = deps.turnTimeoutMs ?? 3e5;
12831
12933
  const deadlineMs = deps.deadlineMs ?? 15e5;
@@ -12835,6 +12937,8 @@ async function runCodegenLoop(args, deps) {
12835
12937
  const transcript = [];
12836
12938
  const toolResults = [];
12837
12939
  const usedIds = /* @__PURE__ */ new Set();
12940
+ const refusedTurns = [];
12941
+ let refusedStreak = 0;
12838
12942
  let turns = 0;
12839
12943
  let noProgressStreak = 0;
12840
12944
  let forced = null;
@@ -12847,6 +12951,8 @@ async function runCodegenLoop(args, deps) {
12847
12951
  maxTurns: limits.maxTurns,
12848
12952
  final: isFinal,
12849
12953
  ...isFinal ? { finalReason } : {},
12954
+ // Says this runner can carry a REFUSED answer (24.12a). Without it the runtime fails the request, as it always did.
12955
+ acceptsRefused: true,
12850
12956
  remaining: {
12851
12957
  toolCalls: Math.max(0, limits.maxToolCalls - ledger.toolCalls),
12852
12958
  readChars: Math.max(0, limits.readCharsTotal - ledger.readChars),
@@ -12868,6 +12974,7 @@ async function runCodegenLoop(args, deps) {
12868
12974
  statusCounts: { ...statusCounts },
12869
12975
  syntaxWarnings: ws.syntaxWarnings,
12870
12976
  ...tests ? { contractVersion: CODEGEN_LOOP_2_CONTRACT_VERSION, testRuns: tests.record() } : {},
12977
+ ...refusedTurns.length > 0 ? { refusedTurns: refusedTurns.map((t) => ({ turn: t.turn, rules: [...t.rules] })) } : {},
12871
12978
  reasoning: [...reasoning]
12872
12979
  });
12873
12980
  const makeRoom = (nextTurn, nextFinal, finalReason) => {
@@ -12892,6 +12999,17 @@ async function runCodegenLoop(args, deps) {
12892
12999
  }
12893
13000
  return fits();
12894
13001
  };
13002
+ const settleNextTurn = (turn, forcedNow) => {
13003
+ const nextTurn = turn + 1;
13004
+ if (nextTurn > limits.maxTurns) return forcedNow;
13005
+ const nextFinal = forcedNow !== null || nextTurn === limits.maxTurns;
13006
+ if (makeRoom(nextTurn, nextFinal, forcedNow ?? "max_turns")) return forcedNow;
13007
+ const reason = forcedNow ?? "read_budget";
13008
+ const fits = makeRoom(nextTurn, true, reason);
13009
+ log({ event: "codegen.loop_ceiling", turn, action: fits ? "forced_final" : "fallback", ceiling: limits.maxRequestTokens });
13010
+ if (!fits) throw new CodegenLoopFailure("token_ceiling");
13011
+ return reason;
13012
+ };
12895
13013
  log({ event: "codegen.loop_start", maxTurns: limits.maxTurns, maxToolCalls: limits.maxToolCalls, listingFiles: args.listing.length });
12896
13014
  try {
12897
13015
  for (let turn = 1; turn <= limits.maxTurns; turn++) {
@@ -12939,6 +13057,18 @@ async function runCodegenLoop(args, deps) {
12939
13057
  throw new CodegenLoopFailure("bad_reply", void 0, err);
12940
13058
  }
12941
13059
  const reasonText = typeof body.reasoning === "string" ? body.reasoning.slice(0, limits.maxReasoningChars) : void 0;
13060
+ if (body.outcome === REFUSED_OUTCOME) {
13061
+ const answer = readRefusedAnswer(body);
13062
+ if (!answer) throw new CodegenLoopFailure("bad_reply");
13063
+ refusedStreak += 1;
13064
+ refusedTurns.push({ turn, rules: answer.rules });
13065
+ log({ event: "codegen.loop_turn", turn, outcome: "refused", requestTokens, elapsedMs: now() - turnStarted, calls: [], rules: answer.rules.map(recordRuleWord), streak: refusedStreak });
13066
+ if (isFinal) throw new CodegenLoopFailure("final_not_produced");
13067
+ transcript.push({ turn, toolCalls: [], refused: answer });
13068
+ if (refusedStreak >= REFUSED_STREAK_LIMIT) forced = "refused_repeatedly";
13069
+ forced = settleNextTurn(turn, forced);
13070
+ continue;
13071
+ }
12942
13072
  if (body.outcome === "CANNOT_COMPLETE") {
12943
13073
  log({ event: "codegen.loop_turn", turn, outcome: "cannot_complete", requestTokens, elapsedMs: now() - turnStarted, calls: [] });
12944
13074
  const said = typeof body.summary === "string" ? body.summary.slice(0, MAX_SUMMARY_CHARS) : void 0;
@@ -13025,21 +13155,13 @@ async function runCodegenLoop(args, deps) {
13025
13155
  if (reasonText) reasoning.push({ turn, text: reasonText });
13026
13156
  transcript.push({ turn, ...reasonText ? { reasoning: reasonText } : {}, toolCalls: calls.map((c) => ({ id: c.id, name: c.name, args: c.args })) });
13027
13157
  toolResults.push(...results);
13158
+ refusedStreak = 0;
13028
13159
  noProgressStreak = results.every((r) => r.status === "duplicate") ? noProgressStreak + 1 : 0;
13029
13160
  if (noProgressStreak >= 2) forced = "no_progress";
13030
13161
  else if (ledger.toolCalls >= limits.maxToolCalls) forced = "tool_budget";
13031
13162
  else if (ledger.writeChars >= limits.writeCharsTotal) forced = "write_budget";
13032
13163
  else if (ledger.readChars >= limits.readCharsTotal || ledger.resultChars >= limits.resultCharsTotal) forced = "read_budget";
13033
- const nextTurn = turn + 1;
13034
- if (nextTurn <= limits.maxTurns) {
13035
- const nextFinal = forced !== null || nextTurn === limits.maxTurns;
13036
- if (!makeRoom(nextTurn, nextFinal, forced ?? "max_turns")) {
13037
- forced = forced ?? "read_budget";
13038
- const fits = makeRoom(nextTurn, true, forced);
13039
- log({ event: "codegen.loop_ceiling", turn, action: fits ? "forced_final" : "fallback", ceiling: limits.maxRequestTokens });
13040
- if (!fits) throw new CodegenLoopFailure("token_ceiling");
13041
- }
13042
- }
13164
+ forced = settleNextTurn(turn, forced);
13043
13165
  log({ event: "codegen.loop_turn", turn, outcome: "tool_calls", requestTokens, elapsedMs: now() - turnStarted, calls: logged });
13044
13166
  }
13045
13167
  throw new CodegenLoopFailure("final_not_produced");
@@ -13635,7 +13757,7 @@ var FAILURE_ARTIFACT_DIR = "quikpilot-failure";
13635
13757
  var REPORT_COPY = "final-test-report.xml";
13636
13758
  var MAX_REPORT_BYTES = 5 * 1024 * 1024;
13637
13759
  var MAX_FILES = 200;
13638
- var RUNNER_VERSION = true ? "9.3.0-rc.7" : "dev";
13760
+ var RUNNER_VERSION = true ? "9.3.0-rc.9" : "dev";
13639
13761
  function safeDestination(base, repoPath) {
13640
13762
  if (!repoPath || (0, import_node_path12.isAbsolute)(repoPath)) return null;
13641
13763
  const destination = (0, import_node_path12.resolve)(base, repoPath);