@crewhaus/runtime-core 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +9 -0
- package/README.md +9 -0
- package/dist/alert-watchdog.d.ts +17 -1
- package/dist/alert-watchdog.js +91 -18
- package/dist/cli-markdown-lines.d.ts +20 -0
- package/dist/cli-markdown-lines.js +91 -0
- package/dist/cli-markdown.js +15 -13
- package/dist/flatten-union.d.ts +36 -0
- package/dist/flatten-union.js +65 -0
- package/dist/forged-footer.d.ts +51 -0
- package/dist/forged-footer.js +212 -0
- package/dist/identity.js +47 -21
- package/dist/incident-collector.js +25 -6
- package/dist/index.d.ts +261 -22
- package/dist/index.js +2327 -218
- package/dist/lineage-screen.d.ts +33 -0
- package/dist/lineage-screen.js +46 -0
- package/dist/observability.d.ts +11 -3
- package/dist/observability.js +46 -8
- package/dist/path-canonical.d.ts +74 -0
- package/dist/path-canonical.js +676 -0
- package/dist/pre-route.d.ts +5 -3
- package/dist/state-roots.d.ts +147 -0
- package/dist/state-roots.js +559 -0
- package/dist/tool-limit.d.ts +54 -0
- package/dist/tool-limit.js +66 -0
- package/package.json +52 -44
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A top-level union of object schemas, merged into one object schema.
|
|
3
|
+
*
|
|
4
|
+
* Anthropic forbids a top-level `anyOf`, but a nested `anyOf` on an
|
|
5
|
+
* individual property is fine — so each property becomes the union of its
|
|
6
|
+
* per-branch shapes, and `required` is the intersection across branches (in
|
|
7
|
+
* practice the shared discriminator). The tool's own zod schema still
|
|
8
|
+
* validates the real input at execute time, so the looser model-facing
|
|
9
|
+
* schema loses no safety.
|
|
10
|
+
*
|
|
11
|
+
* The schema can be an MCP server's, up to 32 KiB of it, read at every
|
|
12
|
+
* `runChatLoop` setup. Each property's shapes were kept by comparing every
|
|
13
|
+
* new one with every one kept so far, as JSON — about 900 branches of one
|
|
14
|
+
* key each made 880 000 `JSON.stringify` calls — and each branch's
|
|
15
|
+
* `required` list was searched once per name still in the intersection.
|
|
16
|
+
* Both are looked up in sets now; the schema produced is the same.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Merge `branches` into one object schema over `baseProps`. A property's
|
|
20
|
+
* shapes are told apart by their JSON, first one kept; a property with one
|
|
21
|
+
* shape keeps it, one with more becomes their `anyOf`.
|
|
22
|
+
*/
|
|
23
|
+
export function flattenUnionToObject(branches, baseProps) {
|
|
24
|
+
const perKey = new Map();
|
|
25
|
+
const requiredSets = [];
|
|
26
|
+
for (const b of branches) {
|
|
27
|
+
for (const [k, v] of Object.entries(b.properties ?? {})) {
|
|
28
|
+
let entry = perKey.get(k);
|
|
29
|
+
if (entry === undefined) {
|
|
30
|
+
entry = { shapes: [], json: null };
|
|
31
|
+
perKey.set(k, entry);
|
|
32
|
+
}
|
|
33
|
+
if (entry.shapes.length === 0) {
|
|
34
|
+
entry.shapes.push(v);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
// The first shape is turned into JSON when a second one arrives, and
|
|
38
|
+
// before it, as the comparison of each pair did: a shape JSON cannot
|
|
39
|
+
// represent throws at the same point it always did.
|
|
40
|
+
if (entry.json === null)
|
|
41
|
+
entry.json = new Set([JSON.stringify(entry.shapes[0])]);
|
|
42
|
+
const json = JSON.stringify(v);
|
|
43
|
+
if (!entry.json.has(json)) {
|
|
44
|
+
entry.shapes.push(v);
|
|
45
|
+
entry.json.add(json);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
requiredSets.push(Array.isArray(b.required) ? b.required : []);
|
|
49
|
+
}
|
|
50
|
+
const properties = { ...(baseProps ?? {}) };
|
|
51
|
+
for (const [k, { shapes }] of perKey) {
|
|
52
|
+
properties[k] = shapes.length === 1 ? shapes[0] : { anyOf: shapes };
|
|
53
|
+
}
|
|
54
|
+
const required = requiredSets.length > 0
|
|
55
|
+
? requiredSets.reduce((acc, r) => {
|
|
56
|
+
const listed = new Set(r);
|
|
57
|
+
return acc.filter((x) => listed.has(x));
|
|
58
|
+
})
|
|
59
|
+
: [];
|
|
60
|
+
return {
|
|
61
|
+
type: "object",
|
|
62
|
+
properties,
|
|
63
|
+
...(required.length > 0 ? { required } : {}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.8.0 (design/0.8.0-provability-release.md §1 principle 2, §5.4) — an
|
|
3
|
+
* answer cannot carry a line that passes for the gate's own verdict.
|
|
4
|
+
*
|
|
5
|
+
* Under `provability:` the gate renders its verdict as one line beside the
|
|
6
|
+
* answer (`── provability · grounded ✓ · 6/6 claims · …`). The model can
|
|
7
|
+
* write the same line into its answer: under `render: none` a failing answer
|
|
8
|
+
* then ends on a ✓ no gate gave, and under `render: footer` the forged ✓ sits
|
|
9
|
+
* directly above the real ✗. Every surface renders the same verdict only if
|
|
10
|
+
* no answer text can pass for that rendering — so a line of the answer that
|
|
11
|
+
* is shaped like the footer is served with a mark in front of it saying who
|
|
12
|
+
* wrote it, on the stream as it prints and in the text the run returns. The
|
|
13
|
+
* transcript keeps the model's own words (the gate grades them as they are:
|
|
14
|
+
* a forged footer is one more uncited sentence).
|
|
15
|
+
*
|
|
16
|
+
* A footer-shaped line is read the way a reader sees it, not as bytes: format
|
|
17
|
+
* characters (zero-width spaces and joiners) dropped, compatibility forms
|
|
18
|
+
* folded (NFKC), case folded, the Cyrillic and Greek letters that look Latin
|
|
19
|
+
* read as the Latin letters they look like ("рrovability" with a Cyrillic р
|
|
20
|
+
* is "provability" to anyone reading it), and whatever opens the line before
|
|
21
|
+
* its first letter or digit skipped — the markdown around it (quote markers,
|
|
22
|
+
* list bullets and numbers, backticks, emphasis), an HTML tag, and any
|
|
23
|
+
* symbol or punctuation (a bullet •, an emoji ✅, an arrow →, a middot, an
|
|
24
|
+
* ellipsis). It is a line whose text then opens with the word `provability`
|
|
25
|
+
* after a rule drawn in any dash (`──`, `——`, `--`, `══`) somewhere in that
|
|
26
|
+
* opening, or opens with the word and carries a verdict mark (✓ ✗ and their
|
|
27
|
+
* look-alikes). Prose that only mentions provability is left alone, and so
|
|
28
|
+
* is a sentence that merely claims to be verified, which no line filter can
|
|
29
|
+
* tell from an honest one.
|
|
30
|
+
*/
|
|
31
|
+
/** The mark a footer-shaped line of the answer is served with. */
|
|
32
|
+
export declare const FORGED_FOOTER_MARK = "(model-written, not the verdict) ";
|
|
33
|
+
/** Is this ONE line (no line break in it) shaped like the provability footer? */
|
|
34
|
+
export declare function isFooterShaped(line: string): boolean;
|
|
35
|
+
/** `text` with each footer-shaped line marked as the model's (see the module doc). */
|
|
36
|
+
export declare function neutralizeFooterLines(text: string): string;
|
|
37
|
+
/** A streaming filter over the answer's text: see {@link createFooterLineGuard}. */
|
|
38
|
+
export type FooterLineGuard = {
|
|
39
|
+
/** One chunk of streamed answer text. */
|
|
40
|
+
push(chunk: string): void;
|
|
41
|
+
/** The message ended: whatever the guard still holds is written, read as a whole line. */
|
|
42
|
+
end(): void;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* The stream's half of {@link neutralizeFooterLines}: text passes straight
|
|
46
|
+
* through to `write`, except the opening of a line that could still turn out
|
|
47
|
+
* footer-shaped, which is held until it cannot (the next character settles
|
|
48
|
+
* it for almost every line) or until the line ends, and then written marked
|
|
49
|
+
* or as it was. Only such a line is ever delayed, and never past its own end.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createFooterLineGuard(write: (text: string) => void): FooterLineGuard;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.8.0 (design/0.8.0-provability-release.md §1 principle 2, §5.4) — an
|
|
3
|
+
* answer cannot carry a line that passes for the gate's own verdict.
|
|
4
|
+
*
|
|
5
|
+
* Under `provability:` the gate renders its verdict as one line beside the
|
|
6
|
+
* answer (`── provability · grounded ✓ · 6/6 claims · …`). The model can
|
|
7
|
+
* write the same line into its answer: under `render: none` a failing answer
|
|
8
|
+
* then ends on a ✓ no gate gave, and under `render: footer` the forged ✓ sits
|
|
9
|
+
* directly above the real ✗. Every surface renders the same verdict only if
|
|
10
|
+
* no answer text can pass for that rendering — so a line of the answer that
|
|
11
|
+
* is shaped like the footer is served with a mark in front of it saying who
|
|
12
|
+
* wrote it, on the stream as it prints and in the text the run returns. The
|
|
13
|
+
* transcript keeps the model's own words (the gate grades them as they are:
|
|
14
|
+
* a forged footer is one more uncited sentence).
|
|
15
|
+
*
|
|
16
|
+
* A footer-shaped line is read the way a reader sees it, not as bytes: format
|
|
17
|
+
* characters (zero-width spaces and joiners) dropped, compatibility forms
|
|
18
|
+
* folded (NFKC), case folded, the Cyrillic and Greek letters that look Latin
|
|
19
|
+
* read as the Latin letters they look like ("рrovability" with a Cyrillic р
|
|
20
|
+
* is "provability" to anyone reading it), and whatever opens the line before
|
|
21
|
+
* its first letter or digit skipped — the markdown around it (quote markers,
|
|
22
|
+
* list bullets and numbers, backticks, emphasis), an HTML tag, and any
|
|
23
|
+
* symbol or punctuation (a bullet •, an emoji ✅, an arrow →, a middot, an
|
|
24
|
+
* ellipsis). It is a line whose text then opens with the word `provability`
|
|
25
|
+
* after a rule drawn in any dash (`──`, `——`, `--`, `══`) somewhere in that
|
|
26
|
+
* opening, or opens with the word and carries a verdict mark (✓ ✗ and their
|
|
27
|
+
* look-alikes). Prose that only mentions provability is left alone, and so
|
|
28
|
+
* is a sentence that merely claims to be verified, which no line filter can
|
|
29
|
+
* tell from an honest one.
|
|
30
|
+
*/
|
|
31
|
+
/** The mark a footer-shaped line of the answer is served with. */
|
|
32
|
+
export const FORGED_FOOTER_MARK = "(model-written, not the verdict) ";
|
|
33
|
+
const WORD = "provability";
|
|
34
|
+
/**
|
|
35
|
+
* What a line can open with before it says anything: every character that
|
|
36
|
+
* is not a letter or a digit (markdown, quoting, a bullet, an emoji, an
|
|
37
|
+
* arrow, a rule), an HTML tag, and a list item's number. Skipped, never part
|
|
38
|
+
* of what it says.
|
|
39
|
+
*/
|
|
40
|
+
const OPENING = /^(?:<\/?[a-z][^<>]{0,200}>|\d{1,3}[.)](?=\s)|[^\p{L}\p{N}])+/u;
|
|
41
|
+
/** A rule character: any dash punctuation, the box-drawing horizontals, the horizontal line extension, `=`. */
|
|
42
|
+
const RULE = /[\p{Pd}─━┄┅┈┉╌╍═⎯=]/u;
|
|
43
|
+
/**
|
|
44
|
+
* Cyrillic, Greek and other letters drawn as a Latin lowercase letter (after
|
|
45
|
+
* NFKC and lower-casing), and the letter they are read as.
|
|
46
|
+
*/
|
|
47
|
+
const LOOKALIKES = new Map([
|
|
48
|
+
// Cyrillic
|
|
49
|
+
"аa",
|
|
50
|
+
"бb",
|
|
51
|
+
"ьb",
|
|
52
|
+
"еe",
|
|
53
|
+
"ёe",
|
|
54
|
+
"һh",
|
|
55
|
+
"іi",
|
|
56
|
+
"їi",
|
|
57
|
+
"ӏl",
|
|
58
|
+
"јj",
|
|
59
|
+
"кk",
|
|
60
|
+
"оo",
|
|
61
|
+
"рp",
|
|
62
|
+
"ԛq",
|
|
63
|
+
"сc",
|
|
64
|
+
"ѕs",
|
|
65
|
+
"тt",
|
|
66
|
+
"уy",
|
|
67
|
+
"ўy",
|
|
68
|
+
"ѵv",
|
|
69
|
+
"ԝw",
|
|
70
|
+
"хx",
|
|
71
|
+
"ԁd",
|
|
72
|
+
// Greek
|
|
73
|
+
"αa",
|
|
74
|
+
"βb",
|
|
75
|
+
"εe",
|
|
76
|
+
"ιi",
|
|
77
|
+
"ϊi",
|
|
78
|
+
"ίi",
|
|
79
|
+
"κk",
|
|
80
|
+
"νv",
|
|
81
|
+
"οo",
|
|
82
|
+
"όo",
|
|
83
|
+
"ρp",
|
|
84
|
+
"ϲc",
|
|
85
|
+
"τt",
|
|
86
|
+
"υu",
|
|
87
|
+
"ύu",
|
|
88
|
+
"γy",
|
|
89
|
+
"χx",
|
|
90
|
+
"ϳj",
|
|
91
|
+
// Armenian, Latin variants
|
|
92
|
+
"օo",
|
|
93
|
+
"սu",
|
|
94
|
+
"ıi",
|
|
95
|
+
"ɩi",
|
|
96
|
+
"ʟl",
|
|
97
|
+
"ɑa",
|
|
98
|
+
"ɡg",
|
|
99
|
+
].map((pair) => [pair.slice(0, -1), pair.slice(-1)]));
|
|
100
|
+
const LOOKALIKE = new RegExp(`[${[...LOOKALIKES.keys()].join("")}]`, "gu");
|
|
101
|
+
/** A verdict mark: the ticks and crosses a footer prints, and the emoji and ballot-box look-alikes. */
|
|
102
|
+
const VERDICT_MARK = /[✓✔✅☑✗✘❌❎☒]/u;
|
|
103
|
+
const LETTER_OR_DIGIT = /[\p{L}\p{N}]/u;
|
|
104
|
+
/**
|
|
105
|
+
* The line as a reader sees it: no format characters, compatibility forms
|
|
106
|
+
* folded, lower case, look-alike letters read as the Latin ones they look
|
|
107
|
+
* like.
|
|
108
|
+
*/
|
|
109
|
+
function visible(line) {
|
|
110
|
+
return line
|
|
111
|
+
.replace(/\p{Cf}/gu, "")
|
|
112
|
+
.normalize("NFKC")
|
|
113
|
+
.toLowerCase()
|
|
114
|
+
.replace(LOOKALIKE, (ch) => LOOKALIKES.get(ch) ?? ch);
|
|
115
|
+
}
|
|
116
|
+
/** `text` past its opening (see {@link OPENING}): what it says, and whether a rule was drawn in it. */
|
|
117
|
+
function opening(text) {
|
|
118
|
+
const opened = OPENING.exec(text);
|
|
119
|
+
if (opened === null)
|
|
120
|
+
return { rest: text, ruled: false };
|
|
121
|
+
const skipped = opened[0].replace(/<[^<>]*>/g, "");
|
|
122
|
+
return { rest: text.slice(opened[0].length), ruled: RULE.test(skipped) };
|
|
123
|
+
}
|
|
124
|
+
/** Is this ONE line (no line break in it) shaped like the provability footer? */
|
|
125
|
+
export function isFooterShaped(line) {
|
|
126
|
+
const seen = visible(line);
|
|
127
|
+
const { rest, ruled } = opening(seen);
|
|
128
|
+
if (!rest.startsWith(WORD))
|
|
129
|
+
return false;
|
|
130
|
+
const next = rest.charAt(WORD.length);
|
|
131
|
+
if (next !== "" && LETTER_OR_DIGIT.test(next))
|
|
132
|
+
return false;
|
|
133
|
+
return ruled || VERDICT_MARK.test(rest);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Where a line's opening stands while it streams: `plain` once nothing that
|
|
137
|
+
* follows can make it footer-shaped, else `open` (the word, or the start of
|
|
138
|
+
* it, is all it says so far).
|
|
139
|
+
*/
|
|
140
|
+
function prefixState(prefix) {
|
|
141
|
+
const { rest } = opening(visible(prefix));
|
|
142
|
+
if (rest.length <= WORD.length)
|
|
143
|
+
return WORD.startsWith(rest) ? "open" : "plain";
|
|
144
|
+
if (!rest.startsWith(WORD))
|
|
145
|
+
return "plain";
|
|
146
|
+
return LETTER_OR_DIGIT.test(rest.charAt(WORD.length)) ? "plain" : "open";
|
|
147
|
+
}
|
|
148
|
+
const LINE_BREAK = /(\r\n|\r|\n|\u2028|\u2029)/;
|
|
149
|
+
/** `text` with each footer-shaped line marked as the model's (see the module doc). */
|
|
150
|
+
export function neutralizeFooterLines(text) {
|
|
151
|
+
if (!visible(text).includes(WORD))
|
|
152
|
+
return text;
|
|
153
|
+
const pieces = text.split(LINE_BREAK);
|
|
154
|
+
for (let i = 0; i < pieces.length; i += 2) {
|
|
155
|
+
const line = pieces[i];
|
|
156
|
+
if (isFooterShaped(line))
|
|
157
|
+
pieces[i] = `${FORGED_FOOTER_MARK}${line}`;
|
|
158
|
+
}
|
|
159
|
+
return pieces.join("");
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* The stream's half of {@link neutralizeFooterLines}: text passes straight
|
|
163
|
+
* through to `write`, except the opening of a line that could still turn out
|
|
164
|
+
* footer-shaped, which is held until it cannot (the next character settles
|
|
165
|
+
* it for almost every line) or until the line ends, and then written marked
|
|
166
|
+
* or as it was. Only such a line is ever delayed, and never past its own end.
|
|
167
|
+
*/
|
|
168
|
+
export function createFooterLineGuard(write) {
|
|
169
|
+
/** The current line's opening, held back; `undefined` once the line is known plain. */
|
|
170
|
+
let held = "";
|
|
171
|
+
const flushLine = (line) => {
|
|
172
|
+
write(isFooterShaped(line) ? `${FORGED_FOOTER_MARK}${line}` : line);
|
|
173
|
+
};
|
|
174
|
+
return {
|
|
175
|
+
push(chunk) {
|
|
176
|
+
let rest = chunk;
|
|
177
|
+
while (rest.length > 0) {
|
|
178
|
+
const at = rest.search(LINE_BREAK);
|
|
179
|
+
const body = at === -1 ? rest : rest.slice(0, at);
|
|
180
|
+
const breakLength = at === -1 ? 0 : rest.startsWith("\r\n", at) ? 2 : 1;
|
|
181
|
+
const lineBreak = at === -1 ? "" : rest.slice(at, at + breakLength);
|
|
182
|
+
rest = at === -1 ? "" : rest.slice(at + breakLength);
|
|
183
|
+
if (held === undefined) {
|
|
184
|
+
write(body + lineBreak);
|
|
185
|
+
}
|
|
186
|
+
else if (at !== -1) {
|
|
187
|
+
flushLine(held + body);
|
|
188
|
+
write(lineBreak);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
const opened = held + body;
|
|
192
|
+
if (prefixState(opened) === "plain") {
|
|
193
|
+
write(opened);
|
|
194
|
+
held = undefined;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
held = opened;
|
|
198
|
+
}
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
// A line break ends the line: the next one opens held.
|
|
202
|
+
if (at !== -1)
|
|
203
|
+
held = "";
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
end() {
|
|
207
|
+
if (held !== undefined && held !== "")
|
|
208
|
+
flushLine(held);
|
|
209
|
+
held = "";
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
package/dist/identity.js
CHANGED
|
@@ -14,8 +14,9 @@
|
|
|
14
14
|
* clobber each other's keypair (first writer wins; the loser re-reads it).
|
|
15
15
|
*/
|
|
16
16
|
import { createHash, generateKeyPairSync } from "node:crypto";
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
17
|
+
import { closeSync, mkdirSync, writeSync } from "node:fs";
|
|
18
|
+
import { resolve } from "node:path";
|
|
19
|
+
import { createExclusive, openForReadSync, writeFileSafe } from "@crewhaus/tool-safety/fs";
|
|
19
20
|
/** Default directory the identity file lives in (the project `.crewhaus` dir). */
|
|
20
21
|
export const DEFAULT_IDENTITY_DIR = ".crewhaus";
|
|
21
22
|
export const IDENTITY_FILENAME = "identity.json";
|
|
@@ -37,11 +38,20 @@ function mint(now) {
|
|
|
37
38
|
createdAt: now().toISOString(),
|
|
38
39
|
};
|
|
39
40
|
}
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
/** The largest identity file read: a keypair is a few hundred bytes. */
|
|
42
|
+
const MAX_IDENTITY_BYTES = 64 * 1024;
|
|
43
|
+
function safeParse(dir) {
|
|
44
|
+
// 0.7.1: never through a link at the name, so a planted
|
|
45
|
+
// `identity.json -> <elsewhere>` is never taken for (or replaced as) the
|
|
46
|
+
// agent's identity.
|
|
47
|
+
const read = openForReadSync(dir, IDENTITY_FILENAME, {
|
|
48
|
+
maxBytes: MAX_IDENTITY_BYTES,
|
|
49
|
+
followLeafSymlink: false,
|
|
50
|
+
});
|
|
51
|
+
if (!read.ok || read.truncated)
|
|
42
52
|
return undefined;
|
|
43
53
|
try {
|
|
44
|
-
const parsed = JSON.parse(
|
|
54
|
+
const parsed = JSON.parse(read.text);
|
|
45
55
|
if (typeof parsed.agentId === "string" &&
|
|
46
56
|
parsed.agentId.length > 0 &&
|
|
47
57
|
typeof parsed.publicKey === "string" &&
|
|
@@ -60,26 +70,42 @@ function safeParse(path) {
|
|
|
60
70
|
* injectable for deterministic tests.
|
|
61
71
|
*/
|
|
62
72
|
export function loadOrCreateAgentIdentity(dir = DEFAULT_IDENTITY_DIR, now = () => new Date()) {
|
|
63
|
-
const
|
|
64
|
-
const
|
|
73
|
+
const root = resolve(dir);
|
|
74
|
+
const path = resolve(root, IDENTITY_FILENAME);
|
|
75
|
+
mkdirSync(root, { recursive: true });
|
|
76
|
+
const existing = safeParse(root);
|
|
65
77
|
if (existing !== undefined)
|
|
66
78
|
return existing;
|
|
67
79
|
const identity = mint(now);
|
|
68
80
|
const body = `${JSON.stringify(identity, null, 2)}\n`;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return winner;
|
|
82
|
-
writeFileSync(path, body, { mode: 0o600 });
|
|
81
|
+
// create-exclusive: the first concurrent first-boot wins the keypair.
|
|
82
|
+
const created = createExclusive(root, IDENTITY_FILENAME, { mode: 0o600 });
|
|
83
|
+
if (created.ok) {
|
|
84
|
+
try {
|
|
85
|
+
const bytes = Buffer.from(body, "utf8");
|
|
86
|
+
let at = 0;
|
|
87
|
+
while (at < bytes.length)
|
|
88
|
+
at += writeSync(created.fd, bytes, at, bytes.length - at);
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
closeSync(created.fd);
|
|
92
|
+
}
|
|
83
93
|
return identity;
|
|
84
94
|
}
|
|
95
|
+
if (created.code !== "exists")
|
|
96
|
+
throw identityRefusal(path, created);
|
|
97
|
+
// Lost the create race, or a corrupt file squats the path.
|
|
98
|
+
const winner = safeParse(root);
|
|
99
|
+
if (winner !== undefined)
|
|
100
|
+
return winner;
|
|
101
|
+
// 0.7.1: replaced through a random O_EXCL|O_NOFOLLOW temp, never written
|
|
102
|
+
// through what squats the name. A link there is refused: 0.7.0 wrote the
|
|
103
|
+
// new keypair, private key included, through it to wherever it pointed.
|
|
104
|
+
const written = writeFileSafe(root, IDENTITY_FILENAME, body, { overwrite: true, mode: 0o600 });
|
|
105
|
+
if (!written.ok)
|
|
106
|
+
throw identityRefusal(path, written);
|
|
107
|
+
return identity;
|
|
108
|
+
}
|
|
109
|
+
function identityRefusal(path, failure) {
|
|
110
|
+
return new Error(`agent identity: refusing to write ${path}: ${failure.reason} (code ${failure.code})`);
|
|
85
111
|
}
|
|
@@ -15,8 +15,9 @@
|
|
|
15
15
|
* is best-effort (a capture failure is logged, never thrown — an incident
|
|
16
16
|
* writer must not turn one failure into two).
|
|
17
17
|
*/
|
|
18
|
-
import { mkdirSync
|
|
19
|
-
import { join } from "node:path";
|
|
18
|
+
import { mkdirSync } from "node:fs";
|
|
19
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
20
|
+
import { writeFileSafe } from "@crewhaus/tool-safety/fs";
|
|
20
21
|
/** How many judge-backed denials in the ring buffer constitute a "storm". */
|
|
21
22
|
export const DENY_STORM_THRESHOLD = 3;
|
|
22
23
|
/**
|
|
@@ -105,8 +106,26 @@ export function attachIncidentCollector(bus, runContext, env, options = {}) {
|
|
|
105
106
|
captured = true;
|
|
106
107
|
try {
|
|
107
108
|
const ts = now().toISOString();
|
|
108
|
-
const
|
|
109
|
-
|
|
109
|
+
const name = incidentDirName(ts, trigger.kind);
|
|
110
|
+
const dir = join(incidentsDir, name);
|
|
111
|
+
// 0.7.1: written rooted at the incidents directory's PARENT (the
|
|
112
|
+
// harness `.crewhaus`), with the path under it relative, so the lazily
|
|
113
|
+
// created `incidents/` directory is contained too. With a plain
|
|
114
|
+
// `mkdir -p` + `writeFileSync`, a link a model planted at
|
|
115
|
+
// `.crewhaus/incidents` (GitApplyPatch creates one) received the
|
|
116
|
+
// capture, ring-buffer events and all, wherever it pointed.
|
|
117
|
+
const root = dirname(resolve(incidentsDir));
|
|
118
|
+
const rel = `${basename(resolve(incidentsDir))}/${name}`;
|
|
119
|
+
mkdirSync(root, { recursive: true });
|
|
120
|
+
const write = (file, data) => {
|
|
121
|
+
const written = writeFileSafe(root, `${rel}/${file}`, data, {
|
|
122
|
+
overwrite: true,
|
|
123
|
+
createParents: true,
|
|
124
|
+
});
|
|
125
|
+
if (!written.ok) {
|
|
126
|
+
throw new Error(`refusing to write ${join(dir, file)}: ${written.reason} (code ${written.code})`);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
110
129
|
const ring = bus.recent();
|
|
111
130
|
const capture = {
|
|
112
131
|
version: 1,
|
|
@@ -119,8 +138,8 @@ export function attachIncidentCollector(bus, runContext, env, options = {}) {
|
|
|
119
138
|
spec: options.spec ?? null,
|
|
120
139
|
ringEventCount: ring.length,
|
|
121
140
|
};
|
|
122
|
-
|
|
123
|
-
|
|
141
|
+
write("incident.json", `${JSON.stringify(capture, null, 2)}\n`);
|
|
142
|
+
write("events.jsonl", ring.length === 0 ? "" : `${ring.map((e) => JSON.stringify(e)).join("\n")}\n`);
|
|
124
143
|
process.stderr.write(`[incident] ${trigger.kind} captured → ${dir} (run \`crewhaus incident collect --session ${runContext.sessionId}\` for the full bundle)\n`);
|
|
125
144
|
}
|
|
126
145
|
catch (err) {
|