@namzu/sdk 21.0.0 → 21.1.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.
- package/CHANGELOG.md +44 -0
- package/dist/public-runtime.d.ts +4 -0
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +15 -0
- package/dist/public-runtime.js.map +1 -1
- package/dist/run/command-gate.d.ts +107 -0
- package/dist/run/command-gate.d.ts.map +1 -0
- package/dist/run/command-gate.js +157 -0
- package/dist/run/command-gate.js.map +1 -0
- package/dist/run/index.d.ts +6 -0
- package/dist/run/index.d.ts.map +1 -1
- package/dist/run/index.js +3 -0
- package/dist/run/index.js.map +1 -1
- package/dist/run/memory-promoter.d.ts +70 -0
- package/dist/run/memory-promoter.d.ts.map +1 -0
- package/dist/run/memory-promoter.js +117 -0
- package/dist/run/memory-promoter.js.map +1 -0
- package/dist/run/workspace-fingerprint.d.ts +105 -0
- package/dist/run/workspace-fingerprint.d.ts.map +1 -0
- package/dist/run/workspace-fingerprint.js +147 -0
- package/dist/run/workspace-fingerprint.js.map +1 -0
- package/package.json +1 -1
- package/src/public-runtime.ts +31 -0
- package/src/run/command-gate.ts +234 -0
- package/src/run/index.ts +17 -0
- package/src/run/memory-promoter.ts +155 -0
- package/src/run/workspace-fingerprint.ts +193 -0
|
@@ -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;CA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AK,EAAE,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,OAAO,SAAS,CAAC,KAAK,EAAE;YACxD,OAAO,EAAE,SAAS,CAAC,QAAQ,CAAC;YAC5B,OAAO,EAAE,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC;YACpC,IAAI;YACJ,MAAM,EAAE,UAAU;YAClB,oEAAoE;YACpE,+DAA+D;YAC/D,2DAA2D;YAC3D,QAAQ,EAAE,EAAE,KAAK,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE;SAC5D,CAAC,CAAA;IACH,CAAC,CAAA;AACF,CAAC"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A hash of everything a run could have changed in its working tree.
|
|
3
|
+
*
|
|
4
|
+
* It exists to answer one question, asked between two attempts at the same
|
|
5
|
+
* verification: **did anything happen since it last failed?** A verify-then-fix
|
|
6
|
+
* loop that re-runs the build after a turn which edited nothing spends a full
|
|
7
|
+
* command execution to learn what a comparison already knew, and does it once
|
|
8
|
+
* per remaining attempt — so a model that has stopped making progress burns
|
|
9
|
+
* the entire budget confirming the same failure.
|
|
10
|
+
*
|
|
11
|
+
* ## What is hashed, and why each part
|
|
12
|
+
*
|
|
13
|
+
* Three sources, because no one of them is complete:
|
|
14
|
+
*
|
|
15
|
+
* 1. **`git status --porcelain`** — which paths differ from the index at all.
|
|
16
|
+
* Cheap, and it catches additions, deletions and mode changes. On its own
|
|
17
|
+
* it is not enough: editing a tracked file that was ALREADY modified
|
|
18
|
+
* leaves the status output byte-identical.
|
|
19
|
+
* 2. **`git diff --binary HEAD`** — the content of every tracked change.
|
|
20
|
+
* `--binary` so an edit to a file git treats as binary is a real diff
|
|
21
|
+
* rather than the constant line `Binary files … differ`, which would make
|
|
22
|
+
* every edit to such a file invisible.
|
|
23
|
+
* 3. **Untracked file contents**, which no `git diff` covers. A new file is
|
|
24
|
+
* named by `status` but its CONTENT is not, so successive edits to a
|
|
25
|
+
* brand-new file would otherwise look like no change at all.
|
|
26
|
+
*
|
|
27
|
+
* ### Symlinks are recorded as their target, not read through
|
|
28
|
+
*
|
|
29
|
+
* Reading a link follows it, so a link repointed from one file to another
|
|
30
|
+
* with identical contents hashes the same — while the thing the workspace
|
|
31
|
+
* actually resolves has changed. The link's target path is the fact that
|
|
32
|
+
* moved, so that is what goes in.
|
|
33
|
+
*
|
|
34
|
+
* ## Failing open, on the cheap side
|
|
35
|
+
*
|
|
36
|
+
* Every uncertainty returns `null`, meaning *no fingerprint*, and a caller
|
|
37
|
+
* that cannot fingerprint re-runs its command. That is the correct direction:
|
|
38
|
+
* the cost of a wrong `null` is one command execution, and the cost of a
|
|
39
|
+
* wrong MATCH is a verification silently skipped — the loop would report
|
|
40
|
+
* "nothing changed" about a workspace that did change, and the model would be
|
|
41
|
+
* told to edit something it had already edited.
|
|
42
|
+
*
|
|
43
|
+
* So: a non-zero exit from any git invocation, a repository with no commits,
|
|
44
|
+
* a timeout, or output past the size cap all produce `null` rather than a
|
|
45
|
+
* partial hash. A truncated diff that hashed successfully would be the worst
|
|
46
|
+
* outcome available here, because two different workspaces truncated at the
|
|
47
|
+
* same point collide.
|
|
48
|
+
*/
|
|
49
|
+
import type { CommandOptions, CommandResult } from '../types/execution/index.js';
|
|
50
|
+
/** How a fingerprint runs git. Injected so a test needs no repository. */
|
|
51
|
+
export type FingerprintExec = (command: string, args: string[], options?: CommandOptions) => Promise<CommandResult>;
|
|
52
|
+
/**
|
|
53
|
+
* The three filesystem reads an untracked entry needs.
|
|
54
|
+
*
|
|
55
|
+
* Injectable for one specific reason, written down because a seam that
|
|
56
|
+
* exists only for tests is usually a smell: **creating a symlink requires a
|
|
57
|
+
* privilege that is not granted by default on Windows**, so the symlink rule
|
|
58
|
+
* below — the one that says a repointed link changes the fingerprint even
|
|
59
|
+
* when the bytes behind it do not — cannot be exercised on a developer
|
|
60
|
+
* machine without it. A rule that can only be checked on some machines is a
|
|
61
|
+
* rule nobody checks.
|
|
62
|
+
*
|
|
63
|
+
* The default is `node:fs/promises` and every other test uses it against a
|
|
64
|
+
* real repository, so this is not a fixture standing in for production; it is
|
|
65
|
+
* one branch of one function reached without a privilege.
|
|
66
|
+
*/
|
|
67
|
+
export interface FingerprintFs {
|
|
68
|
+
lstat(path: string): Promise<{
|
|
69
|
+
isSymbolicLink(): boolean;
|
|
70
|
+
isFile(): boolean;
|
|
71
|
+
}>;
|
|
72
|
+
readlink(path: string): Promise<string>;
|
|
73
|
+
readFile(path: string): Promise<Buffer>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Cap on the bytes any single git invocation may produce.
|
|
77
|
+
*
|
|
78
|
+
* Past it the fingerprint is abandoned rather than hashed. A diff big enough
|
|
79
|
+
* to hit this is a diff nobody is going to iterate on anyway, and hashing a
|
|
80
|
+
* clipped one would let two different trees agree.
|
|
81
|
+
*/
|
|
82
|
+
export declare const FINGERPRINT_MAX_BYTES: number;
|
|
83
|
+
/** Default deadline per git invocation. */
|
|
84
|
+
export declare const FINGERPRINT_TIMEOUT_MS = 20000;
|
|
85
|
+
export interface WorkspaceFingerprintOptions {
|
|
86
|
+
/** Repository root, or any directory inside it. */
|
|
87
|
+
readonly cwd: string;
|
|
88
|
+
/** How to run git. */
|
|
89
|
+
readonly exec: FingerprintExec;
|
|
90
|
+
/** Per-invocation deadline. See {@link FINGERPRINT_TIMEOUT_MS}. */
|
|
91
|
+
readonly timeoutMs?: number;
|
|
92
|
+
/** See {@link FINGERPRINT_MAX_BYTES}. */
|
|
93
|
+
readonly maxBytes?: number;
|
|
94
|
+
/** Filesystem reads. See {@link FingerprintFs}. */
|
|
95
|
+
readonly fs?: FingerprintFs;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* A hash of the working tree's uncommitted state, or `null` when it cannot be
|
|
99
|
+
* established.
|
|
100
|
+
*
|
|
101
|
+
* **`null` is never "unchanged".** It means "I cannot tell", and the caller
|
|
102
|
+
* must treat it as a reason to do the work rather than to skip it.
|
|
103
|
+
*/
|
|
104
|
+
export declare function fingerprintWorkspace(options: WorkspaceFingerprintOptions): Promise<string | null>;
|
|
105
|
+
//# sourceMappingURL=workspace-fingerprint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workspace-fingerprint.d.ts","sourceRoot":"","sources":["../../src/run/workspace-fingerprint.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAMH,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAA;AAEhF,0EAA0E;AAC1E,MAAM,MAAM,eAAe,GAAG,CAC7B,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EAAE,EACd,OAAO,CAAC,EAAE,cAAc,KACpB,OAAO,CAAC,aAAa,CAAC,CAAA;AAE3B;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,aAAa;IAC7B,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,cAAc,IAAI,OAAO,CAAC;QAAC,MAAM,IAAI,OAAO,CAAA;KAAE,CAAC,CAAA;IAC9E,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IACvC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;CACvC;AAID;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,QAAkB,CAAA;AAEpD,2CAA2C;AAC3C,eAAO,MAAM,sBAAsB,QAAS,CAAA;AAE5C,MAAM,WAAW,2BAA2B;IAC3C,mDAAmD;IACnD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;IACpB,sBAAsB;IACtB,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAA;IAC9B,mEAAmE;IACnE,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAC3B,yCAAyC;IACzC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;IAC1B,mDAAmD;IACnD,QAAQ,CAAC,EAAE,CAAC,EAAE,aAAa,CAAA;CAC3B;AAwBD;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACzC,OAAO,EAAE,2BAA2B,GAClC,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAmDxB"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A hash of everything a run could have changed in its working tree.
|
|
3
|
+
*
|
|
4
|
+
* It exists to answer one question, asked between two attempts at the same
|
|
5
|
+
* verification: **did anything happen since it last failed?** A verify-then-fix
|
|
6
|
+
* loop that re-runs the build after a turn which edited nothing spends a full
|
|
7
|
+
* command execution to learn what a comparison already knew, and does it once
|
|
8
|
+
* per remaining attempt — so a model that has stopped making progress burns
|
|
9
|
+
* the entire budget confirming the same failure.
|
|
10
|
+
*
|
|
11
|
+
* ## What is hashed, and why each part
|
|
12
|
+
*
|
|
13
|
+
* Three sources, because no one of them is complete:
|
|
14
|
+
*
|
|
15
|
+
* 1. **`git status --porcelain`** — which paths differ from the index at all.
|
|
16
|
+
* Cheap, and it catches additions, deletions and mode changes. On its own
|
|
17
|
+
* it is not enough: editing a tracked file that was ALREADY modified
|
|
18
|
+
* leaves the status output byte-identical.
|
|
19
|
+
* 2. **`git diff --binary HEAD`** — the content of every tracked change.
|
|
20
|
+
* `--binary` so an edit to a file git treats as binary is a real diff
|
|
21
|
+
* rather than the constant line `Binary files … differ`, which would make
|
|
22
|
+
* every edit to such a file invisible.
|
|
23
|
+
* 3. **Untracked file contents**, which no `git diff` covers. A new file is
|
|
24
|
+
* named by `status` but its CONTENT is not, so successive edits to a
|
|
25
|
+
* brand-new file would otherwise look like no change at all.
|
|
26
|
+
*
|
|
27
|
+
* ### Symlinks are recorded as their target, not read through
|
|
28
|
+
*
|
|
29
|
+
* Reading a link follows it, so a link repointed from one file to another
|
|
30
|
+
* with identical contents hashes the same — while the thing the workspace
|
|
31
|
+
* actually resolves has changed. The link's target path is the fact that
|
|
32
|
+
* moved, so that is what goes in.
|
|
33
|
+
*
|
|
34
|
+
* ## Failing open, on the cheap side
|
|
35
|
+
*
|
|
36
|
+
* Every uncertainty returns `null`, meaning *no fingerprint*, and a caller
|
|
37
|
+
* that cannot fingerprint re-runs its command. That is the correct direction:
|
|
38
|
+
* the cost of a wrong `null` is one command execution, and the cost of a
|
|
39
|
+
* wrong MATCH is a verification silently skipped — the loop would report
|
|
40
|
+
* "nothing changed" about a workspace that did change, and the model would be
|
|
41
|
+
* told to edit something it had already edited.
|
|
42
|
+
*
|
|
43
|
+
* So: a non-zero exit from any git invocation, a repository with no commits,
|
|
44
|
+
* a timeout, or output past the size cap all produce `null` rather than a
|
|
45
|
+
* partial hash. A truncated diff that hashed successfully would be the worst
|
|
46
|
+
* outcome available here, because two different workspaces truncated at the
|
|
47
|
+
* same point collide.
|
|
48
|
+
*/
|
|
49
|
+
import { createHash } from 'node:crypto';
|
|
50
|
+
import { lstat, readFile, readlink } from 'node:fs/promises';
|
|
51
|
+
import { join } from 'node:path';
|
|
52
|
+
const NODE_FS = { lstat, readlink, readFile };
|
|
53
|
+
/**
|
|
54
|
+
* Cap on the bytes any single git invocation may produce.
|
|
55
|
+
*
|
|
56
|
+
* Past it the fingerprint is abandoned rather than hashed. A diff big enough
|
|
57
|
+
* to hit this is a diff nobody is going to iterate on anyway, and hashing a
|
|
58
|
+
* clipped one would let two different trees agree.
|
|
59
|
+
*/
|
|
60
|
+
export const FINGERPRINT_MAX_BYTES = 4 * 1024 * 1024;
|
|
61
|
+
/** Default deadline per git invocation. */
|
|
62
|
+
export const FINGERPRINT_TIMEOUT_MS = 20_000;
|
|
63
|
+
/** One untracked path's contribution, or `null` when it could not be read. */
|
|
64
|
+
async function untrackedEntry(cwd, rel, fs) {
|
|
65
|
+
const abs = join(cwd, rel);
|
|
66
|
+
try {
|
|
67
|
+
const stats = await fs.lstat(abs);
|
|
68
|
+
if (stats.isSymbolicLink()) {
|
|
69
|
+
// The TARGET, not what is behind it. Following the link would hash a
|
|
70
|
+
// repointed link to the same value whenever the new target happens
|
|
71
|
+
// to hold the same bytes, and a repoint is a change to the workspace
|
|
72
|
+
// by any reading that matters.
|
|
73
|
+
return `L ${rel}\0${await fs.readlink(abs)}`;
|
|
74
|
+
}
|
|
75
|
+
if (!stats.isFile())
|
|
76
|
+
return `? ${rel}`;
|
|
77
|
+
const body = await fs.readFile(abs);
|
|
78
|
+
return `F ${rel}\0${createHash('sha256').update(body).digest('hex')}`;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// Vanished between the listing and the read, or unreadable. Neither is
|
|
82
|
+
// a fingerprint this function may guess at.
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* A hash of the working tree's uncommitted state, or `null` when it cannot be
|
|
88
|
+
* established.
|
|
89
|
+
*
|
|
90
|
+
* **`null` is never "unchanged".** It means "I cannot tell", and the caller
|
|
91
|
+
* must treat it as a reason to do the work rather than to skip it.
|
|
92
|
+
*/
|
|
93
|
+
export async function fingerprintWorkspace(options) {
|
|
94
|
+
const { cwd, exec } = options;
|
|
95
|
+
const timeoutMs = options.timeoutMs ?? FINGERPRINT_TIMEOUT_MS;
|
|
96
|
+
const maxBytes = options.maxBytes ?? FINGERPRINT_MAX_BYTES;
|
|
97
|
+
const fs = options.fs ?? NODE_FS;
|
|
98
|
+
const git = async (args) => {
|
|
99
|
+
let result;
|
|
100
|
+
try {
|
|
101
|
+
result = await exec('git', args, { cwd, timeoutMs });
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
// A timeout surfaces here as a non-zero exit, and so does "not a
|
|
107
|
+
// repository" and "no commits yet". All three mean the same thing to
|
|
108
|
+
// this function: it has no basis for a comparison.
|
|
109
|
+
if (result.exitCode !== 0)
|
|
110
|
+
return null;
|
|
111
|
+
if (Buffer.byteLength(result.stdout, 'utf8') > maxBytes)
|
|
112
|
+
return null;
|
|
113
|
+
return result.stdout;
|
|
114
|
+
};
|
|
115
|
+
const status = await git(['status', '--porcelain']);
|
|
116
|
+
if (status === null)
|
|
117
|
+
return null;
|
|
118
|
+
const diff = await git(['diff', '--binary', 'HEAD']);
|
|
119
|
+
if (diff === null)
|
|
120
|
+
return null;
|
|
121
|
+
const untracked = await git(['ls-files', '--others', '--exclude-standard', '-z']);
|
|
122
|
+
if (untracked === null)
|
|
123
|
+
return null;
|
|
124
|
+
const parts = [`status ${status}`, `diff ${diff}`];
|
|
125
|
+
// Split on NUL, which is what `-z` is for: a path may contain a newline,
|
|
126
|
+
// and splitting on one would turn a single strange filename into two
|
|
127
|
+
// ordinary-looking ones.
|
|
128
|
+
//
|
|
129
|
+
// Sorted, because `ls-files` order is not part of any contract and a
|
|
130
|
+
// fingerprint that moved when the listing order did would report a change
|
|
131
|
+
// nobody made.
|
|
132
|
+
for (const rel of untracked.split('\0').filter(Boolean).sort()) {
|
|
133
|
+
const entry = await untrackedEntry(cwd, rel, fs);
|
|
134
|
+
if (entry === null)
|
|
135
|
+
return null;
|
|
136
|
+
parts.push(entry);
|
|
137
|
+
}
|
|
138
|
+
// Length-prefixed rather than delimiter-joined. A diff can contain any
|
|
139
|
+
// byte, so any separator is a separator the content can forge — and two
|
|
140
|
+
// different trees that agreed after forgery would be reported as
|
|
141
|
+
// unchanged, which is the one wrong answer this file is arranged to avoid.
|
|
142
|
+
const hash = createHash('sha256');
|
|
143
|
+
for (const part of parts)
|
|
144
|
+
hash.update(`${Buffer.byteLength(part, 'utf8')}:${part}`);
|
|
145
|
+
return hash.digest('hex');
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=workspace-fingerprint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workspace-fingerprint.js","sourceRoot":"","sources":["../../src/run/workspace-fingerprint.ts"],"names":[],"mappings":"AAAA;;;;;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S,GAAG,MAAM,GAAG,CAAC,CAAC,UAAU,EAAE,UAAU,EAAE,oBAAoB,EAAE,IAAI,CAAC,CAAC,CAAA;IACjF,IAAI,SAAS,KAAK,IAAI;QAAE,OAAO,IAAI,CAAA;IAEnC,MAAM,KAAK,GAAG,CAAC,UAAU,MAAM,EAAE,EAAE,QAAQ,IAAI,EAAE,CAAC,CAAA;IAClD,yEAAyE;IACzE,qEAAqE;IACrE,yBAAyB;IACzB,EAAE;IACF,qEAAqE;IACrE,0EAA0E;IAC1E,eAAe;IACf,KAAK,MAAM,GAAG,IAAI,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;QAChE,MAAM,KAAK,GAAG,MAAM,cAAc,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAA;QAChD,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO,IAAI,CAAA;QAC/B,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAClB,CAAC;IAED,uEAAuE;IACvE,wEAAwE;IACxE,iEAAiE;IACjE,2EAA2E;IAC3E,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAA;IACjC,KAAK,MAAM,IAAI,IAAI,KAAK;QAAE,IAAI,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC,CAAA;IACnF,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC1B,CAAC"}
|
package/package.json
CHANGED
package/src/public-runtime.ts
CHANGED
|
@@ -141,6 +141,37 @@ export type {
|
|
|
141
141
|
DrainRunsParams,
|
|
142
142
|
DrainRunsResult,
|
|
143
143
|
} from './run/index.js'
|
|
144
|
+
// A `ReviewAnswer` that runs shell commands, so "don't finish until the
|
|
145
|
+
// build passes" needs no TypeScript. `reviewAnswer` was the seam for this
|
|
146
|
+
// and nothing shipped supplied one. Skips re-running a command whose
|
|
147
|
+
// failure the workspace has not changed since — the difference between a
|
|
148
|
+
// bounded loop and one that spends its whole budget confirming a failure it
|
|
149
|
+
// already reported.
|
|
150
|
+
export {
|
|
151
|
+
DEFAULT_GATE_MAX_RETRIES,
|
|
152
|
+
DEFAULT_GATE_OUTPUT_CHARS,
|
|
153
|
+
DEFAULT_GATE_TIMEOUT_MS,
|
|
154
|
+
FINGERPRINT_MAX_BYTES,
|
|
155
|
+
FINGERPRINT_TIMEOUT_MS,
|
|
156
|
+
clipOutput,
|
|
157
|
+
createCommandGate,
|
|
158
|
+
fingerprintWorkspace,
|
|
159
|
+
} from './run/index.js'
|
|
160
|
+
export type {
|
|
161
|
+
CommandGateOptions,
|
|
162
|
+
FingerprintExec,
|
|
163
|
+
GateExec,
|
|
164
|
+
WorkspaceFingerprintOptions,
|
|
165
|
+
} from './run/index.js'
|
|
166
|
+
// The default `promoteMemory`: write what a run learned into a MemoryStore,
|
|
167
|
+
// or write NOTHING. The hook was invoked at settle with the compaction
|
|
168
|
+
// extractor's already-structured output and no shipped app supplied it, so
|
|
169
|
+
// that structure was serialized into one system message and dropped when
|
|
170
|
+
// the run ended. A run that learned nothing leaves no record at all — the
|
|
171
|
+
// model reads this store, so noise here is context spent on a run that did
|
|
172
|
+
// nothing.
|
|
173
|
+
export { RUN_MEMORY_TAG, createMemoryPromoter } from './run/index.js'
|
|
174
|
+
export type { MemoryPromoterOptions } from './run/index.js'
|
|
144
175
|
|
|
145
176
|
// ─── personas, skills, advisory ──────────────────────────────────────────
|
|
146
177
|
|
|
@@ -0,0 +1,234 @@
|
|
|
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
|
+
|
|
43
|
+
import { LocalExecutionContext } from '../execution/local.js'
|
|
44
|
+
import type { CommandOptions, CommandResult } from '../types/execution/index.js'
|
|
45
|
+
import type { AnswerReview, ReviewAnswer } from '../types/run/answer-review.js'
|
|
46
|
+
import { fingerprintWorkspace } from './workspace-fingerprint.js'
|
|
47
|
+
|
|
48
|
+
/** How the gate runs a command. Injected so a test needs no shell. */
|
|
49
|
+
export type GateExec = (
|
|
50
|
+
command: string,
|
|
51
|
+
args: string[],
|
|
52
|
+
options?: CommandOptions,
|
|
53
|
+
) => Promise<CommandResult>
|
|
54
|
+
|
|
55
|
+
/** Default per-command deadline. A test suite is allowed to be slow. */
|
|
56
|
+
export const DEFAULT_GATE_TIMEOUT_MS = 600_000
|
|
57
|
+
|
|
58
|
+
/** How many attempts the gate will EXECUTE its commands for, by default. */
|
|
59
|
+
export const DEFAULT_GATE_MAX_RETRIES = 3
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Model-visible characters of a failing command's output.
|
|
63
|
+
*
|
|
64
|
+
* Head and tail, not head alone: a compiler names the file at the top and a
|
|
65
|
+
* test runner names the failure at the bottom, and a gate that only ever kept
|
|
66
|
+
* one end would be useless for one of them.
|
|
67
|
+
*/
|
|
68
|
+
export const DEFAULT_GATE_OUTPUT_CHARS = 4_000
|
|
69
|
+
|
|
70
|
+
export interface CommandGateOptions {
|
|
71
|
+
/**
|
|
72
|
+
* Shell command lines, run in order, stopping at the first failure.
|
|
73
|
+
*
|
|
74
|
+
* In order and short-circuiting because that is what a person means by
|
|
75
|
+
* "typecheck then test": a type error makes the test output noise about
|
|
76
|
+
* the same cause, and handing the model both invites it to fix the
|
|
77
|
+
* symptom.
|
|
78
|
+
*/
|
|
79
|
+
readonly commands: readonly string[]
|
|
80
|
+
/** Directory the commands run in, and the tree that is fingerprinted. */
|
|
81
|
+
readonly cwd: string
|
|
82
|
+
/**
|
|
83
|
+
* How many attempts will actually EXECUTE the commands.
|
|
84
|
+
*
|
|
85
|
+
* Past it the gate rejects without running anything, naming the
|
|
86
|
+
* exhaustion. It does not accept: an answer that never passed the gate
|
|
87
|
+
* has not passed the gate, and a reviewer that gave up by accepting would
|
|
88
|
+
* hand back a green run over a red build — the exact outcome the gate
|
|
89
|
+
* exists to prevent. What ENDS the run is the kernel's rejection budget,
|
|
90
|
+
* so set that to the same number (the CLI does).
|
|
91
|
+
*/
|
|
92
|
+
readonly maxRetries?: number
|
|
93
|
+
/** Per-command deadline. See {@link DEFAULT_GATE_TIMEOUT_MS}. */
|
|
94
|
+
readonly timeoutMs?: number
|
|
95
|
+
/** Override the executor. Defaults to a local shell in `cwd`. */
|
|
96
|
+
readonly exec?: GateExec
|
|
97
|
+
/** See {@link DEFAULT_GATE_OUTPUT_CHARS}. */
|
|
98
|
+
readonly maxOutputChars?: number
|
|
99
|
+
/**
|
|
100
|
+
* Override the change detector. Defaults to
|
|
101
|
+
* {@link fingerprintWorkspace} over `cwd`.
|
|
102
|
+
*
|
|
103
|
+
* Returning `null` means "cannot tell", and the gate then runs its
|
|
104
|
+
* commands. A detector that returned a constant would silence the gate
|
|
105
|
+
* after its first failure, so this seam exists for tests and for a host
|
|
106
|
+
* whose workspace is not a git tree — not as a way to turn the check off.
|
|
107
|
+
*/
|
|
108
|
+
readonly fingerprint?: () => Promise<string | null>
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Head and tail of a command's output, with the middle marked as dropped. */
|
|
112
|
+
export function clipOutput(text: string, max: number): string {
|
|
113
|
+
const trimmed = text.trimEnd()
|
|
114
|
+
if (trimmed.length <= max) return trimmed
|
|
115
|
+
const half = Math.floor(max / 2)
|
|
116
|
+
const dropped = trimmed.length - half * 2
|
|
117
|
+
return `${trimmed.slice(0, half)}\n… ${dropped} characters omitted …\n${trimmed.slice(-half)}`
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface LastFailure {
|
|
121
|
+
readonly command: string
|
|
122
|
+
/** The tree as it stood when this failed. `null` = could not be taken. */
|
|
123
|
+
readonly fingerprint: string | null
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function failureFeedback(
|
|
127
|
+
command: string,
|
|
128
|
+
attempt: number,
|
|
129
|
+
result: CommandResult,
|
|
130
|
+
maxOutputChars: number,
|
|
131
|
+
): string {
|
|
132
|
+
const output = clipOutput(`${result.stdout}\n${result.stderr}`, maxOutputChars)
|
|
133
|
+
return [
|
|
134
|
+
`The answer was not accepted: \`${command}\` failed (attempt ${attempt}, exit ${result.exitCode}).`,
|
|
135
|
+
'',
|
|
136
|
+
'Output:',
|
|
137
|
+
'```',
|
|
138
|
+
output || '(no output)',
|
|
139
|
+
'```',
|
|
140
|
+
'',
|
|
141
|
+
'Fix the cause and then finish. Do not restate the failure back to me; change the code so the command passes.',
|
|
142
|
+
].join('\n')
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function unchangedFeedback(command: string, attempt: number): string {
|
|
146
|
+
return [
|
|
147
|
+
`The answer was not accepted, and \`${command}\` was NOT re-run (attempt ${attempt}).`,
|
|
148
|
+
'',
|
|
149
|
+
'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.',
|
|
150
|
+
'',
|
|
151
|
+
'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.',
|
|
152
|
+
].join('\n')
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function exhaustedFeedback(command: string, maxRetries: number): string {
|
|
156
|
+
return [
|
|
157
|
+
`The answer was not accepted: \`${command}\` has failed and this gate has spent its ${maxRetries} attempts.`,
|
|
158
|
+
'',
|
|
159
|
+
'No further command will be run.',
|
|
160
|
+
].join('\n')
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Build a reviewer that accepts an answer only when every command passes.
|
|
165
|
+
*
|
|
166
|
+
* Stateful across calls within one run, deliberately: the whole point is that
|
|
167
|
+
* attempt N+1 can be compared with attempt N. Build one gate per run.
|
|
168
|
+
*/
|
|
169
|
+
export function createCommandGate(options: CommandGateOptions): ReviewAnswer {
|
|
170
|
+
const { commands, cwd } = options
|
|
171
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_GATE_TIMEOUT_MS
|
|
172
|
+
const maxRetries = options.maxRetries ?? DEFAULT_GATE_MAX_RETRIES
|
|
173
|
+
const maxOutputChars = options.maxOutputChars ?? DEFAULT_GATE_OUTPUT_CHARS
|
|
174
|
+
|
|
175
|
+
// Built once and reused: constructing a context per attempt would re-stat
|
|
176
|
+
// the directory for no gain, and the context holds nothing per-run.
|
|
177
|
+
const context = new LocalExecutionContext({ id: 'namzu-command-gate', cwd })
|
|
178
|
+
const exec: GateExec =
|
|
179
|
+
options.exec ?? ((command, args, opts) => context.executeCommand(command, args, opts))
|
|
180
|
+
const fingerprint =
|
|
181
|
+
options.fingerprint ?? (() => fingerprintWorkspace({ cwd, exec, timeoutMs: 20_000 }))
|
|
182
|
+
|
|
183
|
+
let attempt = 0
|
|
184
|
+
let last: LastFailure | undefined
|
|
185
|
+
let executions = 0
|
|
186
|
+
|
|
187
|
+
return async (): Promise<AnswerReview> => {
|
|
188
|
+
attempt += 1
|
|
189
|
+
|
|
190
|
+
// A gate that already failed, over a tree nothing has touched since.
|
|
191
|
+
// The command is skipped, and the attempt still counts.
|
|
192
|
+
//
|
|
193
|
+
// The comparison is a bare `===` rather than `now !== null && now ===
|
|
194
|
+
// …`. The guard above already establishes that the recorded
|
|
195
|
+
// fingerprint is non-null, so a `null` from this call cannot match it,
|
|
196
|
+
// and the extra clause was a branch nothing could reach — a mutation
|
|
197
|
+
// that deleted it killed no test, which is what a dead condition looks
|
|
198
|
+
// like from the outside.
|
|
199
|
+
if (last && last.fingerprint !== null) {
|
|
200
|
+
const now = await fingerprint()
|
|
201
|
+
if (now === last.fingerprint) {
|
|
202
|
+
return { accept: false, feedback: unchangedFeedback(last.command, attempt) }
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (last && executions >= maxRetries) {
|
|
207
|
+
return { accept: false, feedback: exhaustedFeedback(last.command, maxRetries) }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
executions += 1
|
|
211
|
+
for (const command of commands) {
|
|
212
|
+
// `shell: true` because the operator handed over a command LINE —
|
|
213
|
+
// `pnpm test -- --run`, with its flags and its quoting — and taking
|
|
214
|
+
// that as an executable name plus literal arguments would fail on
|
|
215
|
+
// every gate anyone would actually write. Explicit, per the note on
|
|
216
|
+
// `LocalExecutionContext.executeCommand`: shell interpretation is
|
|
217
|
+
// opt-in, and this is the opt-in.
|
|
218
|
+
const result = await exec(command, [], { cwd, timeoutMs, shell: true })
|
|
219
|
+
if (result.exitCode === 0) continue
|
|
220
|
+
|
|
221
|
+
// Taken AFTER the failure, not before the run: the comparison next
|
|
222
|
+
// time is against the tree this verdict was formed over. A snapshot
|
|
223
|
+
// from before the command would miss anything the command itself
|
|
224
|
+
// wrote — a formatter, a snapshot updater, a lockfile.
|
|
225
|
+
last = { command, fingerprint: await fingerprint() }
|
|
226
|
+
return { accept: false, feedback: failureFeedback(command, attempt, result, maxOutputChars) }
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Cleared, so a later rejection by a DIFFERENT command is not compared
|
|
230
|
+
// against a tree this one failed over.
|
|
231
|
+
last = undefined
|
|
232
|
+
return { accept: true }
|
|
233
|
+
}
|
|
234
|
+
}
|