@namzu/sdk 21.0.0 → 22.0.0

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.
Files changed (103) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/dist/advisory/executor.d.ts +9 -4
  3. package/dist/advisory/executor.d.ts.map +1 -1
  4. package/dist/advisory/executor.js +16 -9
  5. package/dist/advisory/executor.js.map +1 -1
  6. package/dist/constants/limits.d.ts +13 -0
  7. package/dist/constants/limits.d.ts.map +1 -1
  8. package/dist/constants/limits.js +14 -2
  9. package/dist/constants/limits.js.map +1 -1
  10. package/dist/manager/agent/lifecycle.d.ts +2 -2
  11. package/dist/manager/run/persistence.d.ts +37 -2
  12. package/dist/manager/run/persistence.d.ts.map +1 -1
  13. package/dist/manager/run/persistence.js +48 -7
  14. package/dist/manager/run/persistence.js.map +1 -1
  15. package/dist/pricing/catalogue.generated.d.ts +15 -0
  16. package/dist/pricing/catalogue.generated.d.ts.map +1 -0
  17. package/dist/pricing/catalogue.generated.js +293 -0
  18. package/dist/pricing/catalogue.generated.js.map +1 -0
  19. package/dist/pricing/index.d.ts +46 -0
  20. package/dist/pricing/index.d.ts.map +1 -0
  21. package/dist/pricing/index.js +70 -0
  22. package/dist/pricing/index.js.map +1 -0
  23. package/dist/public-runtime.d.ts +6 -1
  24. package/dist/public-runtime.d.ts.map +1 -1
  25. package/dist/public-runtime.js +21 -1
  26. package/dist/public-runtime.js.map +1 -1
  27. package/dist/public-types.d.ts +3 -1
  28. package/dist/public-types.d.ts.map +1 -1
  29. package/dist/run/LimitChecker.d.ts +10 -0
  30. package/dist/run/LimitChecker.d.ts.map +1 -1
  31. package/dist/run/LimitChecker.js +14 -2
  32. package/dist/run/LimitChecker.js.map +1 -1
  33. package/dist/run/command-gate.d.ts +107 -0
  34. package/dist/run/command-gate.d.ts.map +1 -0
  35. package/dist/run/command-gate.js +157 -0
  36. package/dist/run/command-gate.js.map +1 -0
  37. package/dist/run/index.d.ts +6 -0
  38. package/dist/run/index.d.ts.map +1 -1
  39. package/dist/run/index.js +3 -0
  40. package/dist/run/index.js.map +1 -1
  41. package/dist/run/memory-promoter.d.ts +70 -0
  42. package/dist/run/memory-promoter.d.ts.map +1 -0
  43. package/dist/run/memory-promoter.js +117 -0
  44. package/dist/run/memory-promoter.js.map +1 -0
  45. package/dist/run/workspace-fingerprint.d.ts +105 -0
  46. package/dist/run/workspace-fingerprint.d.ts.map +1 -0
  47. package/dist/run/workspace-fingerprint.js +147 -0
  48. package/dist/run/workspace-fingerprint.js.map +1 -0
  49. package/dist/runtime/query/checkpoint.d.ts +23 -2
  50. package/dist/runtime/query/checkpoint.d.ts.map +1 -1
  51. package/dist/runtime/query/checkpoint.js +24 -3
  52. package/dist/runtime/query/checkpoint.js.map +1 -1
  53. package/dist/runtime/query/guard.d.ts.map +1 -1
  54. package/dist/runtime/query/guard.js +1 -0
  55. package/dist/runtime/query/guard.js.map +1 -1
  56. package/dist/runtime/query/index.d.ts.map +1 -1
  57. package/dist/runtime/query/index.js +42 -0
  58. package/dist/runtime/query/index.js.map +1 -1
  59. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  60. package/dist/runtime/query/iteration/index.js +15 -2
  61. package/dist/runtime/query/iteration/index.js.map +1 -1
  62. package/dist/runtime/query/iteration/phases/advisory.d.ts.map +1 -1
  63. package/dist/runtime/query/iteration/phases/advisory.js +9 -1
  64. package/dist/runtime/query/iteration/phases/advisory.js.map +1 -1
  65. package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
  66. package/dist/runtime/query/iteration/phases/compaction.js +11 -2
  67. package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
  68. package/dist/store/run/conformance.d.ts.map +1 -1
  69. package/dist/store/run/conformance.js +8 -2
  70. package/dist/store/run/conformance.js.map +1 -1
  71. package/dist/types/common/index.d.ts +38 -2
  72. package/dist/types/common/index.d.ts.map +1 -1
  73. package/dist/types/run/stop-reason.d.ts +31 -1
  74. package/dist/types/run/stop-reason.d.ts.map +1 -1
  75. package/dist/utils/cost.d.ts +60 -2
  76. package/dist/utils/cost.d.ts.map +1 -1
  77. package/dist/utils/cost.js +126 -11
  78. package/dist/utils/cost.js.map +1 -1
  79. package/package.json +1 -1
  80. package/src/advisory/executor.ts +16 -9
  81. package/src/constants/limits.ts +14 -2
  82. package/src/manager/agent/lifecycle.ts +2 -2
  83. package/src/manager/run/persistence.ts +67 -7
  84. package/src/pricing/catalogue.generated.ts +308 -0
  85. package/src/pricing/index.ts +78 -0
  86. package/src/pricing/rates.source.json +206 -0
  87. package/src/public-runtime.ts +39 -0
  88. package/src/public-types.ts +3 -1
  89. package/src/run/LimitChecker.ts +24 -2
  90. package/src/run/command-gate.ts +234 -0
  91. package/src/run/index.ts +17 -0
  92. package/src/run/memory-promoter.ts +155 -0
  93. package/src/run/workspace-fingerprint.ts +193 -0
  94. package/src/runtime/query/checkpoint.ts +24 -3
  95. package/src/runtime/query/guard.ts +1 -0
  96. package/src/runtime/query/index.ts +42 -0
  97. package/src/runtime/query/iteration/index.ts +15 -2
  98. package/src/runtime/query/iteration/phases/advisory.ts +9 -1
  99. package/src/runtime/query/iteration/phases/compaction.ts +12 -2
  100. package/src/store/run/conformance.ts +8 -2
  101. package/src/types/common/index.ts +38 -2
  102. package/src/types/run/stop-reason.ts +30 -0
  103. package/src/utils/cost.ts +186 -16
@@ -8,8 +8,20 @@ export function checkLimitsDetailed(config, state) {
8
8
  if (config.tokenBudget > 0 && state.totalTokens >= config.tokenBudget) {
9
9
  return { type: 'hard_stop', reason: 'token_budget' };
10
10
  }
11
- if (config.costLimitUsd && config.costLimitUsd > 0 && state.totalCost >= config.costLimitUsd) {
12
- return { type: 'hard_stop', reason: 'cost_limit' };
11
+ if (config.costLimitUsd && config.costLimitUsd > 0) {
12
+ if (state.totalCost >= config.costLimitUsd) {
13
+ return { type: 'hard_stop', reason: 'cost_limit' };
14
+ }
15
+ // Checked BEFORE the comparison can pass, not after. `totalCost` is
16
+ // only the part of the run that had a rate, so a run whose spend is
17
+ // partly unknown can sit under any limit forever while spending without
18
+ // bound. Continuing here would be the check answering "the budget is
19
+ // satisfied" to the question "can the budget be evaluated?", which is
20
+ // the strongest available wrong answer — see
21
+ // docs/conventions/an-optional-dependency-may-not-degrade-a-check.md.
22
+ if (state.unpricedTokens > 0) {
23
+ return { type: 'hard_stop', reason: 'cost_unmeasurable' };
24
+ }
13
25
  }
14
26
  if (state.currentIteration >= config.maxIterations) {
15
27
  return { type: 'hard_stop', reason: 'max_iterations' };
@@ -1 +1 @@
1
- {"version":3,"file":"LimitChecker.js","sourceRoot":"","sources":["../../src/run/LimitChecker.ts"],"names":[],"mappings":"AAeA,MAAM,UAAU,mBAAmB,CAClC,MAA0B,EAC1B,KAAwB;IAExB,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;QACnB,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,EAAE,CAAA;IAClD,CAAC;IAED,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE,CAAC;QACrD,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,SAAS,EAAE,CAAA;IAChD,CAAC;IAED,IAAI,MAAM,CAAC,WAAW,GAAG,CAAC,IAAI,KAAK,CAAC,WAAW,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;QACvE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,cAAc,EAAE,CAAA;IACrD,CAAC;IAED,IAAI,MAAM,CAAC,YAAY,IAAI,MAAM,CAAC,YAAY,GAAG,CAAC,IAAI,KAAK,CAAC,SAAS,IAAI,MAAM,CAAC,YAAY,EAAE,CAAC;QAC9F,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,CAAA;IACnD,CAAC;IAED,IAAI,KAAK,CAAC,gBAAgB,IAAI,MAAM,CAAC,aAAa,EAA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 /**
2
+ * A {@link ReviewAnswer} that runs shell commands and hands the failure back.
3
+ *
4
+ * `reviewAnswer` was the seam for exactly this — judge the answer at the point
5
+ * the model stops calling tools, and return it with feedback instead of
6
+ * settling — and nothing shipped supplied one, so an operator who wanted
7
+ * "don't finish until the build passes" had to write TypeScript. This is the
8
+ * supplier. With it, `--gate 'pnpm test'` is the whole unattended story: the
9
+ * model works, stops, the tests run, and a failure comes back as the next user
10
+ * turn rather than as a green run somebody discovers in CI.
11
+ *
12
+ * The kernel already bounds it. The reviewer is consulted only when the model
13
+ * stopped calling tools, never on the forced-final turn, and a rejection
14
+ * budget stops the run with `answer_rejected` — a stop reason that names the
15
+ * reviewer rather than blaming a token budget. None of that is re-implemented
16
+ * here.
17
+ *
18
+ * ## The part that is not just "run a command"
19
+ *
20
+ * **Before re-running a command that already failed, the workspace is
21
+ * fingerprinted, and an identical fingerprint means the command is NOT run.**
22
+ *
23
+ * This is the difference between a bounded loop and one that spends its whole
24
+ * budget. A model that has run out of ideas answers again without editing
25
+ * anything; re-running the suite then costs a full execution — often the most
26
+ * expensive thing in the loop — to produce a failure already known character
27
+ * for character. Worse, the feedback is identical, so the model is handed the
28
+ * same prompt that just failed to help it. Saying instead "the workspace has
29
+ * not changed since that failure; edit something before trying to finish"
30
+ * is both cheaper and a different instruction.
31
+ *
32
+ * The attempt still advances. Skipping the command is a saving, not a pardon:
33
+ * an answer that changed nothing has been rejected, and the run's budget must
34
+ * see that or a stuck model loops forever for free.
35
+ *
36
+ * And it fails open on the cheap side. No fingerprint — a git invocation that
37
+ * errored, a timeout, output past the cap, a tree with no commits — means the
38
+ * command runs. See {@link fingerprintWorkspace}: the cost of re-running
39
+ * unnecessarily is one execution; the cost of wrongly skipping is a
40
+ * verification that silently did not happen.
41
+ */
42
+ import type { CommandOptions, CommandResult } from '../types/execution/index.js';
43
+ import type { ReviewAnswer } from '../types/run/answer-review.js';
44
+ /** How the gate runs a command. Injected so a test needs no shell. */
45
+ export type GateExec = (command: string, args: string[], options?: CommandOptions) => Promise<CommandResult>;
46
+ /** Default per-command deadline. A test suite is allowed to be slow. */
47
+ export declare const DEFAULT_GATE_TIMEOUT_MS = 600000;
48
+ /** How many attempts the gate will EXECUTE its commands for, by default. */
49
+ export declare const DEFAULT_GATE_MAX_RETRIES = 3;
50
+ /**
51
+ * Model-visible characters of a failing command's output.
52
+ *
53
+ * Head and tail, not head alone: a compiler names the file at the top and a
54
+ * test runner names the failure at the bottom, and a gate that only ever kept
55
+ * one end would be useless for one of them.
56
+ */
57
+ export declare const DEFAULT_GATE_OUTPUT_CHARS = 4000;
58
+ export interface CommandGateOptions {
59
+ /**
60
+ * Shell command lines, run in order, stopping at the first failure.
61
+ *
62
+ * In order and short-circuiting because that is what a person means by
63
+ * "typecheck then test": a type error makes the test output noise about
64
+ * the same cause, and handing the model both invites it to fix the
65
+ * symptom.
66
+ */
67
+ readonly commands: readonly string[];
68
+ /** Directory the commands run in, and the tree that is fingerprinted. */
69
+ readonly cwd: string;
70
+ /**
71
+ * How many attempts will actually EXECUTE the commands.
72
+ *
73
+ * Past it the gate rejects without running anything, naming the
74
+ * exhaustion. It does not accept: an answer that never passed the gate
75
+ * has not passed the gate, and a reviewer that gave up by accepting would
76
+ * hand back a green run over a red build — the exact outcome the gate
77
+ * exists to prevent. What ENDS the run is the kernel's rejection budget,
78
+ * so set that to the same number (the CLI does).
79
+ */
80
+ readonly maxRetries?: number;
81
+ /** Per-command deadline. See {@link DEFAULT_GATE_TIMEOUT_MS}. */
82
+ readonly timeoutMs?: number;
83
+ /** Override the executor. Defaults to a local shell in `cwd`. */
84
+ readonly exec?: GateExec;
85
+ /** See {@link DEFAULT_GATE_OUTPUT_CHARS}. */
86
+ readonly maxOutputChars?: number;
87
+ /**
88
+ * Override the change detector. Defaults to
89
+ * {@link fingerprintWorkspace} over `cwd`.
90
+ *
91
+ * Returning `null` means "cannot tell", and the gate then runs its
92
+ * commands. A detector that returned a constant would silence the gate
93
+ * after its first failure, so this seam exists for tests and for a host
94
+ * whose workspace is not a git tree — not as a way to turn the check off.
95
+ */
96
+ readonly fingerprint?: () => Promise<string | null>;
97
+ }
98
+ /** Head and tail of a command's output, with the middle marked as dropped. */
99
+ export declare function clipOutput(text: string, max: number): string;
100
+ /**
101
+ * Build a reviewer that accepts an answer only when every command passes.
102
+ *
103
+ * Stateful across calls within one run, deliberately: the whole point is that
104
+ * attempt N+1 can be compared with attempt N. Build one gate per run.
105
+ */
106
+ export declare function createCommandGate(options: CommandGateOptions): ReviewAnswer;
107
+ //# sourceMappingURL=command-gate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command-gate.d.ts","sourceRoot":"","sources":["../../src/run/command-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAChF,OAAO,KAAK,EAAgB,YAAY,EAAE,MAAM,+BAA+B,CAAA;AAG/E,sEAAsE;AACtE,MAAM,MAAM,QAAQ,GAAG,CACtB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EAAE,EACd,OAAO,CAAC,EAAE,cAAc,KACpB,OAAO,CAAC,aAAa,CAAC,CAAA;AAE3B,wEAAwE;AACxE,eAAO,MAAM,uBAAuB,SAAU,CAAA;AAE9C,4EAA4E;AAC5E,eAAO,MAAM,wBAAwB,IAAI,CAAA;AAEzC;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,OAAQ,CAAA;AAE9C,MAAM,WAAW,kBAAkB;IAClC;;;;;;;OAOG;IACH,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAA;IACpC,yEAAyE;IACzE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB;;;;;;;;;OASG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;IAC5B,iEAAiE;IACjE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAC3B,iEAAiE;IACjE,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAA;IACxB,6CAA6C;IAC7C,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAA;IAChC;;;;;;;;OAQG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;CACnD;AAED,8EAA8E;AAC9E,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAM5D;AA6CD;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,YAAY,CAiE3E"}
@@ -0,0 +1,157 @@
1
+ /**
2
+ * A {@link ReviewAnswer} that runs shell commands and hands the failure back.
3
+ *
4
+ * `reviewAnswer` was the seam for exactly this — judge the answer at the point
5
+ * the model stops calling tools, and return it with feedback instead of
6
+ * settling — and nothing shipped supplied one, so an operator who wanted
7
+ * "don't finish until the build passes" had to write TypeScript. This is the
8
+ * supplier. With it, `--gate 'pnpm test'` is the whole unattended story: the
9
+ * model works, stops, the tests run, and a failure comes back as the next user
10
+ * turn rather than as a green run somebody discovers in CI.
11
+ *
12
+ * The kernel already bounds it. The reviewer is consulted only when the model
13
+ * stopped calling tools, never on the forced-final turn, and a rejection
14
+ * budget stops the run with `answer_rejected` — a stop reason that names the
15
+ * reviewer rather than blaming a token budget. None of that is re-implemented
16
+ * here.
17
+ *
18
+ * ## The part that is not just "run a command"
19
+ *
20
+ * **Before re-running a command that already failed, the workspace is
21
+ * fingerprinted, and an identical fingerprint means the command is NOT run.**
22
+ *
23
+ * This is the difference between a bounded loop and one that spends its whole
24
+ * budget. A model that has run out of ideas answers again without editing
25
+ * anything; re-running the suite then costs a full execution — often the most
26
+ * expensive thing in the loop — to produce a failure already known character
27
+ * for character. Worse, the feedback is identical, so the model is handed the
28
+ * same prompt that just failed to help it. Saying instead "the workspace has
29
+ * not changed since that failure; edit something before trying to finish"
30
+ * is both cheaper and a different instruction.
31
+ *
32
+ * The attempt still advances. Skipping the command is a saving, not a pardon:
33
+ * an answer that changed nothing has been rejected, and the run's budget must
34
+ * see that or a stuck model loops forever for free.
35
+ *
36
+ * And it fails open on the cheap side. No fingerprint — a git invocation that
37
+ * errored, a timeout, output past the cap, a tree with no commits — means the
38
+ * command runs. See {@link fingerprintWorkspace}: the cost of re-running
39
+ * unnecessarily is one execution; the cost of wrongly skipping is a
40
+ * verification that silently did not happen.
41
+ */
42
+ import { LocalExecutionContext } from '../execution/local.js';
43
+ import { fingerprintWorkspace } from './workspace-fingerprint.js';
44
+ /** Default per-command deadline. A test suite is allowed to be slow. */
45
+ export const DEFAULT_GATE_TIMEOUT_MS = 600_000;
46
+ /** How many attempts the gate will EXECUTE its commands for, by default. */
47
+ export const DEFAULT_GATE_MAX_RETRIES = 3;
48
+ /**
49
+ * Model-visible characters of a failing command's output.
50
+ *
51
+ * Head and tail, not head alone: a compiler names the file at the top and a
52
+ * test runner names the failure at the bottom, and a gate that only ever kept
53
+ * one end would be useless for one of them.
54
+ */
55
+ export const DEFAULT_GATE_OUTPUT_CHARS = 4_000;
56
+ /** Head and tail of a command's output, with the middle marked as dropped. */
57
+ export function clipOutput(text, max) {
58
+ const trimmed = text.trimEnd();
59
+ if (trimmed.length <= max)
60
+ return trimmed;
61
+ const half = Math.floor(max / 2);
62
+ const dropped = trimmed.length - half * 2;
63
+ return `${trimmed.slice(0, half)}\n… ${dropped} characters omitted …\n${trimmed.slice(-half)}`;
64
+ }
65
+ function failureFeedback(command, attempt, result, maxOutputChars) {
66
+ const output = clipOutput(`${result.stdout}\n${result.stderr}`, maxOutputChars);
67
+ return [
68
+ `The answer was not accepted: \`${command}\` failed (attempt ${attempt}, exit ${result.exitCode}).`,
69
+ '',
70
+ 'Output:',
71
+ '```',
72
+ output || '(no output)',
73
+ '```',
74
+ '',
75
+ 'Fix the cause and then finish. Do not restate the failure back to me; change the code so the command passes.',
76
+ ].join('\n');
77
+ }
78
+ function unchangedFeedback(command, attempt) {
79
+ return [
80
+ `The answer was not accepted, and \`${command}\` was NOT re-run (attempt ${attempt}).`,
81
+ '',
82
+ 'The workspace is byte-for-byte identical to what it was when that command last failed — no file was created, edited or deleted since. Running it again would produce the failure you have already been shown.',
83
+ '',
84
+ 'Edit something before trying to finish again. If you believe the change you described was made, verify it by reading the file: it is not on disk.',
85
+ ].join('\n');
86
+ }
87
+ function exhaustedFeedback(command, maxRetries) {
88
+ return [
89
+ `The answer was not accepted: \`${command}\` has failed and this gate has spent its ${maxRetries} attempts.`,
90
+ '',
91
+ 'No further command will be run.',
92
+ ].join('\n');
93
+ }
94
+ /**
95
+ * Build a reviewer that accepts an answer only when every command passes.
96
+ *
97
+ * Stateful across calls within one run, deliberately: the whole point is that
98
+ * attempt N+1 can be compared with attempt N. Build one gate per run.
99
+ */
100
+ export function createCommandGate(options) {
101
+ const { commands, cwd } = options;
102
+ const timeoutMs = options.timeoutMs ?? DEFAULT_GATE_TIMEOUT_MS;
103
+ const maxRetries = options.maxRetries ?? DEFAULT_GATE_MAX_RETRIES;
104
+ const maxOutputChars = options.maxOutputChars ?? DEFAULT_GATE_OUTPUT_CHARS;
105
+ // Built once and reused: constructing a context per attempt would re-stat
106
+ // the directory for no gain, and the context holds nothing per-run.
107
+ const context = new LocalExecutionContext({ id: 'namzu-command-gate', cwd });
108
+ const exec = options.exec ?? ((command, args, opts) => context.executeCommand(command, args, opts));
109
+ const fingerprint = options.fingerprint ?? (() => fingerprintWorkspace({ cwd, exec, timeoutMs: 20_000 }));
110
+ let attempt = 0;
111
+ let last;
112
+ let executions = 0;
113
+ return async () => {
114
+ attempt += 1;
115
+ // A gate that already failed, over a tree nothing has touched since.
116
+ // The command is skipped, and the attempt still counts.
117
+ //
118
+ // The comparison is a bare `===` rather than `now !== null && now ===
119
+ // …`. The guard above already establishes that the recorded
120
+ // fingerprint is non-null, so a `null` from this call cannot match it,
121
+ // and the extra clause was a branch nothing could reach — a mutation
122
+ // that deleted it killed no test, which is what a dead condition looks
123
+ // like from the outside.
124
+ if (last && last.fingerprint !== null) {
125
+ const now = await fingerprint();
126
+ if (now === last.fingerprint) {
127
+ return { accept: false, feedback: unchangedFeedback(last.command, attempt) };
128
+ }
129
+ }
130
+ if (last && executions >= maxRetries) {
131
+ return { accept: false, feedback: exhaustedFeedback(last.command, maxRetries) };
132
+ }
133
+ executions += 1;
134
+ for (const command of commands) {
135
+ // `shell: true` because the operator handed over a command LINE —
136
+ // `pnpm test -- --run`, with its flags and its quoting — and taking
137
+ // that as an executable name plus literal arguments would fail on
138
+ // every gate anyone would actually write. Explicit, per the note on
139
+ // `LocalExecutionContext.executeCommand`: shell interpretation is
140
+ // opt-in, and this is the opt-in.
141
+ const result = await exec(command, [], { cwd, timeoutMs, shell: true });
142
+ if (result.exitCode === 0)
143
+ continue;
144
+ // Taken AFTER the failure, not before the run: the comparison next
145
+ // time is against the tree this verdict was formed over. A snapshot
146
+ // from before the command would miss anything the command itself
147
+ // wrote — a formatter, a snapshot updater, a lockfile.
148
+ last = { command, fingerprint: await fingerprint() };
149
+ return { accept: false, feedback: failureFeedback(command, attempt, result, maxOutputChars) };
150
+ }
151
+ // Cleared, so a later rejection by a DIFFERENT command is not compared
152
+ // against a tree this one failed over.
153
+ last = undefined;
154
+ return { accept: true };
155
+ };
156
+ }
157
+ //# sourceMappingURL=command-gate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"command-gate.js","sourceRoot":"","sources":["../../src/run/command-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAA;AAG7D,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAA;AASjE,wEAAwE;AACxE,MAAM,CAAC,MAAM,uBAAuB,GAAG,OAAO,CAAA;AAE9C,4EAA4E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AA;IACxB,CAAC,CAAA;AACF,CAAC"}
@@ -4,6 +4,12 @@ export { createRunReporter } from './reporter.js';
4
4
  export type { RunReporter } from './reporter.js';
5
5
  export { DEFAULT_DRAIN_PAGE_SIZE, drainRuns } from './drain.js';
6
6
  export type { DrainFailure, DrainRun, DrainRunsParams, DrainRunsResult } from './drain.js';
7
+ export { DEFAULT_GATE_MAX_RETRIES, DEFAULT_GATE_OUTPUT_CHARS, DEFAULT_GATE_TIMEOUT_MS, clipOutput, createCommandGate, } from './command-gate.js';
8
+ export type { CommandGateOptions, GateExec } from './command-gate.js';
9
+ export { FINGERPRINT_MAX_BYTES, FINGERPRINT_TIMEOUT_MS, fingerprintWorkspace, } from './workspace-fingerprint.js';
10
+ export type { FingerprintExec, WorkspaceFingerprintOptions } from './workspace-fingerprint.js';
7
11
  export { checkLimitsDetailed, buildLimitConfig } from './LimitChecker.js';
8
12
  export type { LimitCheckerState, LimitCheckResult } from './LimitChecker.js';
13
+ export { RUN_MEMORY_TAG, createMemoryPromoter } from './memory-promoter.js';
14
+ export type { MemoryPromoterOptions } from './memory-promoter.js';
9
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/run/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAA;AAE9D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAEhD,OAAO,EAAE,uBAAuB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAC/D,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAE1F,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACzE,YAAY,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/run/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAA;AAE9D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAEhD,OAAO,EAAE,uBAAuB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAC/D,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC1F,OAAO,EACN,wBAAwB,EACxB,yBAAyB,EACzB,uBAAuB,EACvB,UAAU,EACV,iBAAiB,GACjB,MAAM,mBAAmB,CAAA;AAC1B,YAAY,EAAE,kBAAkB,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACrE,OAAO,EACN,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,GACpB,MAAM,4BAA4B,CAAA;AACnC,YAAY,EAAE,eAAe,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAA;AAE9F,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACzE,YAAY,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAE5E,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAC3E,YAAY,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA"}
package/dist/run/index.js CHANGED
@@ -2,5 +2,8 @@ export { RunPersistence } from '../manager/run/persistence.js';
2
2
  export { RunDiskStore } from '../store/run/disk.js';
3
3
  export { createRunReporter } from './reporter.js';
4
4
  export { DEFAULT_DRAIN_PAGE_SIZE, drainRuns } from './drain.js';
5
+ export { DEFAULT_GATE_MAX_RETRIES, DEFAULT_GATE_OUTPUT_CHARS, DEFAULT_GATE_TIMEOUT_MS, clipOutput, createCommandGate, } from './command-gate.js';
6
+ export { FINGERPRINT_MAX_BYTES, FINGERPRINT_TIMEOUT_MS, fingerprintWorkspace, } from './workspace-fingerprint.js';
5
7
  export { checkLimitsDetailed, buildLimitConfig } from './LimitChecker.js';
8
+ export { RUN_MEMORY_TAG, createMemoryPromoter } from './memory-promoter.js';
6
9
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/run/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAA;AAE9D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AAGjD,OAAO,EAAE,uBAAuB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAG/D,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/run/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAA;AAE9D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AAGjD,OAAO,EAAE,uBAAuB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAE/D,OAAO,EACN,wBAAwB,EACxB,yBAAyB,EACzB,uBAAuB,EACvB,UAAU,EACV,iBAAiB,GACjB,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EACN,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,GACpB,MAAM,4BAA4B,CAAA;AAGnC,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAGzE,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA"}
@@ -0,0 +1,70 @@
1
+ /**
2
+ * The default {@link PromoteMemory}: write what a run learned into a
3
+ * {@link MemoryStore}, or write nothing at all.
4
+ *
5
+ * `promoteMemory` is called once at settle with the compaction extractor's
6
+ * already-structured output — decisions, discoveries, user requirements,
7
+ * failures, environment facts — and **nothing shipped supplied the hook**.
8
+ * So the structure the compaction pass had spent tokens producing was
9
+ * serialized into one system message and dropped on the floor when the run
10
+ * ended, exactly as its own module comment says. This is the supplier, and
11
+ * it is mostly a filter: the hard part — extracting facts from a transcript
12
+ * — already happened.
13
+ *
14
+ * ## The filter, which is the only decision here
15
+ *
16
+ * **A run that learned nothing must leave nothing.** Not an empty record,
17
+ * not a record whose body says "no decisions" — nothing. A promoter that
18
+ * wrote a row per run would fill the store with the runs least worth
19
+ * remembering, and `search_memory` would then return them: the model reads
20
+ * that store on later runs, so noise here is not merely wasted disk, it is
21
+ * context spent on a run that did nothing.
22
+ *
23
+ * What counts as having learned something is the five KNOWLEDGE categories —
24
+ * decisions, discoveries, user requirements, failures, environment. Not
25
+ * `task`, which every run has because it is the prompt restated. Not
26
+ * `files`, which every run that opened anything has, and which says what was
27
+ * touched rather than what was learned. A run whose only trace is "it read
28
+ * six files" is the exact record this filter exists to refuse.
29
+ *
30
+ * ## What it does NOT do
31
+ *
32
+ * Deduplicate against what is already stored, merge with a previous run's
33
+ * record, or expire anything. Each is a policy with real trade-offs and a
34
+ * host that wants one owns it — `promoteMemory` is a callback precisely so
35
+ * that the runtime does not decide this. This is the obvious default, not
36
+ * the only possible one.
37
+ */
38
+ import type { MemoryStore } from '../types/memory/index.js';
39
+ import type { PromoteMemory } from '../types/run/memory-promotion.js';
40
+ /** Tag every record this promoter writes, so a host can find or prune them. */
41
+ export declare const RUN_MEMORY_TAG = "run-memory";
42
+ export interface MemoryPromoterOptions {
43
+ /** Where records go. The same store `save_memory` writes through. */
44
+ readonly store: MemoryStore;
45
+ /**
46
+ * Extra tags on every record, beyond {@link RUN_MEMORY_TAG}.
47
+ *
48
+ * A host running several agents against one store uses this to tell whose
49
+ * memory is whose; without it a later search cannot.
50
+ */
51
+ readonly tags?: readonly string[];
52
+ /**
53
+ * Cap on entries rendered per category. Defaults to 20.
54
+ *
55
+ * The extractor already caps its lists, and this is the second cap for
56
+ * the same reason the first exists: a record nobody will read is a record
57
+ * that costs context every time it is retrieved.
58
+ */
59
+ readonly maxPerCategory?: number;
60
+ }
61
+ /**
62
+ * Build a promoter that writes one record per run that learned something.
63
+ *
64
+ * Never throws out to the runtime — but it does not swallow either: the
65
+ * runtime already catches and logs a promoter's failure at settle, and
66
+ * catching here as well would hide a broken store from the one place that
67
+ * reports it.
68
+ */
69
+ export declare function createMemoryPromoter(options: MemoryPromoterOptions): PromoteMemory;
70
+ //# sourceMappingURL=memory-promoter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory-promoter.d.ts","sourceRoot":"","sources":["../../src/run/memory-promoter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AAC3D,OAAO,KAAK,EAAE,aAAa,EAAsB,MAAM,kCAAkC,CAAA;AAkBzF,+EAA+E;AAC/E,eAAO,MAAM,cAAc,eAAe,CAAA;AAE1C,MAAM,WAAW,qBAAqB;IACrC,qEAAqE;IACrE,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAA;IAC3B;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IACjC;;;;;;OAMG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAA;CAChC;AA6CD;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,aAAa,CAuBlF"}
@@ -0,0 +1,117 @@
1
+ /**
2
+ * The default {@link PromoteMemory}: write what a run learned into a
3
+ * {@link MemoryStore}, or write nothing at all.
4
+ *
5
+ * `promoteMemory` is called once at settle with the compaction extractor's
6
+ * already-structured output — decisions, discoveries, user requirements,
7
+ * failures, environment facts — and **nothing shipped supplied the hook**.
8
+ * So the structure the compaction pass had spent tokens producing was
9
+ * serialized into one system message and dropped on the floor when the run
10
+ * ended, exactly as its own module comment says. This is the supplier, and
11
+ * it is mostly a filter: the hard part — extracting facts from a transcript
12
+ * — already happened.
13
+ *
14
+ * ## The filter, which is the only decision here
15
+ *
16
+ * **A run that learned nothing must leave nothing.** Not an empty record,
17
+ * not a record whose body says "no decisions" — nothing. A promoter that
18
+ * wrote a row per run would fill the store with the runs least worth
19
+ * remembering, and `search_memory` would then return them: the model reads
20
+ * that store on later runs, so noise here is not merely wasted disk, it is
21
+ * context spent on a run that did nothing.
22
+ *
23
+ * What counts as having learned something is the five KNOWLEDGE categories —
24
+ * decisions, discoveries, user requirements, failures, environment. Not
25
+ * `task`, which every run has because it is the prompt restated. Not
26
+ * `files`, which every run that opened anything has, and which says what was
27
+ * touched rather than what was learned. A run whose only trace is "it read
28
+ * six files" is the exact record this filter exists to refuse.
29
+ *
30
+ * ## What it does NOT do
31
+ *
32
+ * Deduplicate against what is already stored, merge with a previous run's
33
+ * record, or expire anything. Each is a policy with real trade-offs and a
34
+ * host that wants one owns it — `promoteMemory` is a callback precisely so
35
+ * that the runtime does not decide this. This is the obvious default, not
36
+ * the only possible one.
37
+ */
38
+ /**
39
+ * The categories that make a run worth remembering.
40
+ *
41
+ * Ordered as they are rendered. `userRequirements` first because it is the
42
+ * most durable of the five — a constraint the user stated outlives the run
43
+ * that heard it, whereas a discovery about a codebase expires when the
44
+ * codebase moves.
45
+ */
46
+ const KNOWLEDGE = [
47
+ ['userRequirements', 'What the user requires'],
48
+ ['decisions', 'Decisions'],
49
+ ['discoveries', 'Discoveries'],
50
+ ['failures', 'What did not work'],
51
+ ['environment', 'Environment'],
52
+ ];
53
+ /** Tag every record this promoter writes, so a host can find or prune them. */
54
+ export const RUN_MEMORY_TAG = 'run-memory';
55
+ /** Everything the candidate knows, as `[heading, items]`, empties dropped. */
56
+ function knowledge(candidate, cap) {
57
+ const out = [];
58
+ for (const [key, heading] of KNOWLEDGE) {
59
+ const items = candidate[key];
60
+ if (items.length > 0)
61
+ out.push([heading, items.slice(0, cap)]);
62
+ }
63
+ return out;
64
+ }
65
+ /** A one-line summary naming what kind of knowledge the record holds. */
66
+ function summarize(sections) {
67
+ return sections.map(([heading, items]) => `${heading.toLowerCase()} (${items.length})`).join(', ');
68
+ }
69
+ function render(candidate, sections) {
70
+ const body = sections.map(([heading, items]) => `## ${heading}\n\n${items.map((i) => `- ${i}`).join('\n')}`);
71
+ // The eviction counts, when there are any. Carried rather than hidden for
72
+ // the reason the candidate carries them: somebody reading this record
73
+ // should know they are reading a truncated account of the run, not a
74
+ // complete one.
75
+ const evicted = Object.entries(candidate.evicted).filter(([, n]) => n > 0);
76
+ if (evicted.length > 0) {
77
+ body.push(`## Dropped during the run\n\n${evicted
78
+ .map(([category, n]) => `- ${category}: ${n} entr${n === 1 ? 'y' : 'ies'} evicted`)
79
+ .join('\n')}`);
80
+ }
81
+ if (candidate.files.length > 0) {
82
+ body.push(`## Files touched\n\n${candidate.files.map((f) => `- ${f}`).join('\n')}`);
83
+ }
84
+ return `# ${candidate.task}\n\n${body.join('\n\n')}\n`;
85
+ }
86
+ /**
87
+ * Build a promoter that writes one record per run that learned something.
88
+ *
89
+ * Never throws out to the runtime — but it does not swallow either: the
90
+ * runtime already catches and logs a promoter's failure at settle, and
91
+ * catching here as well would hide a broken store from the one place that
92
+ * reports it.
93
+ */
94
+ export function createMemoryPromoter(options) {
95
+ const cap = options.maxPerCategory ?? 20;
96
+ const tags = [RUN_MEMORY_TAG, ...(options.tags ?? [])];
97
+ return async (candidate) => {
98
+ const sections = knowledge(candidate, cap);
99
+ // Nothing learned, nothing written. Not an empty record: a store full
100
+ // of rows describing runs that discovered nothing is a store whose
101
+ // search results are mostly noise, and the model reads that store.
102
+ if (sections.length === 0)
103
+ return;
104
+ await options.store.create({
105
+ title: candidate.task.trim() || `Run ${candidate.runId}`,
106
+ summary: summarize(sections),
107
+ content: render(candidate, sections),
108
+ tags,
109
+ format: 'markdown',
110
+ // The run id, so a record can be traced back to the run that formed
111
+ // it. Evidence rather than decoration: without it a surprising
112
+ // memory cannot be checked against what actually happened.
113
+ metadata: { runId: candidate.runId, source: RUN_MEMORY_TAG },
114
+ });
115
+ };
116
+ }
117
+ //# sourceMappingURL=memory-promoter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory-promoter.js","sourceRoot":"","sources":["../../src/run/memory-promoter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAKH;;;;;;;GAOG;AACH,MAAM,SAAS,GAAG;IACjB,CAAC,kBAAkB,EAAE,wBAAwB,CAAC;IAC9C,CAAC,WAAW,EAAE,WAAW,CAAC;IAC1B,CAAC,aAAa,EAAE,aAAa,CAAC;IAC9B,CAAC,UAAU,EAAE,mBAAmB,CAAC;IACjC,CAAC,aAAa,EAAE,aAAa,CAAC;CAC8C,CAAA;AAE7E,+EAA+E;AAC/E,MAAM,CAAC,MAAM,cAAc,GAAG,YAAY,CAAA;AAsB1C,8EAA8E;AAC9E,SAAS,SAAS,CACjB,SAA6B,EAC7B,GAAW;IAEX,MAAM,GAAG,GAA6C,EAAE,CAAA;IACxD,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,SAAS,EAAE,CAAC;QACxC,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAsB,CAAA;QACjD,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;YAAE,GAAG,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAA;IAC/D,CAA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