@bridge_gpt/mcp-server 0.2.72 → 0.2.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/build/agent-launchers/claude-executor-adapter.js +3 -0
- package/build/agents.generated.js +6 -6
- package/build/bridge-permissions.js +27 -0
- package/build/ci-system-detection.js +50 -0
- package/build/ci-trigger-adapters.js +631 -0
- package/build/ci-trigger-install.js +106 -0
- package/build/codex-launcher-pin.js +33 -2
- package/build/codex-skill-adapter.js +3 -1
- package/build/command-host.js +33 -0
- package/build/commands.generated.js +7 -6
- package/build/conductor/bridge-api-client.js +842 -35
- package/build/conductor/bring-up-facts.js +18 -5
- package/build/conductor/command-drift-production.js +42 -17
- package/build/conductor/command-drift.js +47 -0
- package/build/conductor/declare-blocked.js +554 -0
- package/build/conductor/doctor.js +106 -21
- package/build/conductor/driver-commands.js +141 -0
- package/build/conductor/driver-guard.js +277 -0
- package/build/conductor/driver-lapse.js +321 -0
- package/build/conductor/driver-register.js +176 -0
- package/build/conductor/driver-release.js +174 -0
- package/build/conductor/driver-session-state.js +315 -0
- package/build/conductor/epic-branch-ci-inspection.js +222 -29
- package/build/conductor/epic-implementer-cli.js +166 -15
- package/build/conductor/epic-server-database.js +162 -0
- package/build/conductor/epic-server-guidance.js +278 -0
- package/build/conductor/epic-server-journal.js +239 -0
- package/build/conductor/epic-server-processes.js +324 -0
- package/build/conductor/epic-server-recipe.js +397 -0
- package/build/conductor/epic-server-runtime.js +1207 -0
- package/build/conductor/epic-server-sfcc.js +107 -0
- package/build/conductor/epic-server-state.js +427 -0
- package/build/conductor/epic-server.js +594 -0
- package/build/conductor/gate-discovery.js +150 -5
- package/build/conductor/gate-settings-review.js +224 -4
- package/build/conductor/launch-need-probes.js +6 -53
- package/build/conductor/launch-plan.js +313 -156
- package/build/conductor/prerequisite-inputs.js +879 -45
- package/build/conductor/readiness-assessment.js +16 -1
- package/build/conductor/readiness-cli.js +20 -9
- package/build/conductor/readiness-prerequisites.js +28 -9
- package/build/conductor/readiness-sections.js +133 -126
- package/build/conductor/readiness.js +1 -1
- package/build/conductor/recovery-cli.js +13 -2
- package/build/conductor/tools.js +6 -5
- package/build/conductor/wait-run-attention.js +2 -2
- package/build/conductor/wait-run-cli.js +208 -22
- package/build/conductor/worker-database-doctor-inspector.js +33 -2
- package/build/conductor/worker-setup-tally.js +27 -0
- package/build/credential-store.js +71 -1
- package/build/dependency-install-declarations.js +55 -0
- package/build/docs.generated.js +2 -2
- package/build/doctor.js +54 -3
- package/build/drive-epic-help.js +21 -2
- package/build/drive-epic.js +1811 -351
- package/build/engine-paths.js +20 -0
- package/build/epic-branch-ci-summary.js +140 -0
- package/build/epic-capability-statement.js +107 -0
- package/build/epic-ci-workflow-drift.js +30 -15
- package/build/epic-ci-workflow-install.js +110 -42
- package/build/epic-ci-workflow-v2.js +274 -0
- package/build/epic-ci-workflow.js +38 -12
- package/build/epic-implementer-bin.js +2101 -44
- package/build/epic-launch-context.js +607 -0
- package/build/epic-server-record.js +273 -0
- package/build/epic-shared/bridge-client.js +11 -3
- package/build/escalation-reachability.js +43 -0
- package/build/executor/ci-fix-test-runs.js +334 -0
- package/build/executor/completion-contract.js +8 -1
- package/build/executor/env.js +59 -22
- package/build/executor/epic-server-context.js +141 -0
- package/build/executor/gate-qualification-job.js +127 -26
- package/build/executor/http-client.js +52 -5
- package/build/executor/job-runner.js +423 -69
- package/build/executor/observation.js +8 -0
- package/build/executor/pre-pr-checks.js +363 -0
- package/build/executor/prompt-spec.js +114 -2
- package/build/executor/reconcile-overlap-job.js +5 -3
- package/build/executor/required-work-diagnostics.js +4 -2
- package/build/executor/required-work-ledger.js +936 -38
- package/build/executor/results.js +58 -0
- package/build/executor/terminal-mutation.js +8 -0
- package/build/executor/worker-command.js +72 -3
- package/build/executor/worker-database-config.js +281 -0
- package/build/executor/worker-database-target.js +16 -12
- package/build/executor/worker-database.js +320 -101
- package/build/executor/worker-finalization.js +7 -1
- package/build/executor/worker-resources.js +25 -3
- package/build/executor/worker-verification-guidance.js +18 -3
- package/build/executor/worktree.js +20 -1
- package/build/executor-launch.js +54 -2
- package/build/generated-assets-catalog.js +2 -2
- package/build/index.js +664 -335
- package/build/init.js +26 -11
- package/build/install-bridge-conductor-answers.js +465 -0
- package/build/install-bridge-conductor-apply.js +259 -0
- package/build/install-bridge-conductor-assessment.js +895 -0
- package/build/install-bridge-conductor-compact.js +161 -0
- package/build/install-bridge-conductor-contracts.js +63 -0
- package/build/install-bridge-conductor-gate.js +1197 -196
- package/build/install-bridge-conductor-setup.js +535 -79
- package/build/install-bridge-conductor.js +2885 -560
- package/build/install-bridge.js +199 -21
- package/build/install-maintenance.js +26 -3
- package/build/launcher-config-inspection.js +162 -1
- package/build/mcp-grant-candidates.js +767 -0
- package/build/mcp-grant-doctor.js +63 -0
- package/build/mcp-grant.js +288 -0
- package/build/mcp-host-targets.js +8 -0
- package/build/mcp-identity.js +23 -0
- package/build/mcp-invoke.js +126 -13
- package/build/mcp-presets.js +107 -0
- package/build/mcp-provisioning.js +134 -2
- package/build/mcp-registration-doctor.js +31 -2
- package/build/mcp-server-invocation.js +6 -1
- package/build/merge-workflow-install.js +234 -109
- package/build/pipelines.generated.js +5 -5
- package/build/plan-discovery.js +97 -4
- package/build/plan-execution-ledger.js +100 -7
- package/build/plan-generation-io.js +262 -0
- package/build/plan-generation.js +424 -0
- package/build/plane/build-freshness.js +6 -1
- package/build/plane/cli.js +75 -9
- package/build/plane/plane-freshness.js +17 -0
- package/build/plane/preflight.js +50 -10
- package/build/plane/shutdown.js +19 -0
- package/build/plane/terminal-run-cleanup.js +18 -4
- package/build/printed-remedy-commands.js +206 -12
- package/build/readiness-check.js +77 -0
- package/build/readiness-prerequisites.generated.js +122 -14
- package/build/readme.generated.js +1 -1
- package/build/repository-state-paths.js +38 -0
- package/build/run-unit-tests-launcher.js +8 -1
- package/build/setup-epic.js +1190 -196
- package/build/setup-progress.js +44 -0
- package/build/setup-question.js +122 -4
- package/build/third-party-mcp-targets.js +30 -0
- package/build/ticket-delivery-enrollment.js +9 -0
- package/build/upgrade-cli.js +85 -2
- package/build/v2-readiness-requirements.js +28 -1
- package/build/version.generated.js +3 -3
- package/docs/EPIC-IMPLEMENTER-INSTALL.md +9 -1
- package/docs/epic-implementer/getting-started.md +3 -1
- package/docs/epic-implementer/self-hosting.md +14 -1
- package/package.json +2 -2
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Repository guidance for the epic server (BAPI-1546): `.bridge/epic-server.toml`.
|
|
3
|
+
*
|
|
4
|
+
* The guidance is INPUT TO THE DRIVER ONLY. It is free-form text plus optional
|
|
5
|
+
* hints the driver may adapt to the stack: `prepare`, `database`, `migrate`,
|
|
6
|
+
* `seed`, `start`, `ready`, and suggested timing values. The helper never reads
|
|
7
|
+
* it: the helper replays only the driver-written recipe in the private run-state
|
|
8
|
+
* directory (`epic-server-recipe.ts`), so a worker who edits this file on a
|
|
9
|
+
* branch cannot change what the helper runs.
|
|
10
|
+
*
|
|
11
|
+
* The file has its own name, so older `.bridge/config` parsers are unaffected.
|
|
12
|
+
* Parsing is tolerant: unknown keys and tables are ignored, and a missing or
|
|
13
|
+
* malformed file is "no guidance" (never an exception). The result has no
|
|
14
|
+
* facility to resolve a secret and is never a run recipe by itself.
|
|
15
|
+
*
|
|
16
|
+
* Only a small TOML subset is read: `key = value` pairs, `[table]` headers,
|
|
17
|
+
* `#` comments, basic and literal strings (single- and multi-line), integers,
|
|
18
|
+
* booleans, and (possibly nested, possibly multi-line) arrays.
|
|
19
|
+
*/
|
|
20
|
+
/** The repository-relative path of the guidance file. */
|
|
21
|
+
export const EPIC_SERVER_GUIDANCE_PATH = ".bridge/epic-server.toml";
|
|
22
|
+
/** Largest guidance file read. Anything bigger is "no guidance". */
|
|
23
|
+
export const EPIC_SERVER_GUIDANCE_MAX_BYTES = 64 * 1024;
|
|
24
|
+
class TomlSubsetError extends Error {
|
|
25
|
+
}
|
|
26
|
+
/** Parse the supported TOML subset. Throws {@link TomlSubsetError} on anything else. */
|
|
27
|
+
export function parseTomlSubset(text) {
|
|
28
|
+
const root = {};
|
|
29
|
+
let table = root;
|
|
30
|
+
let i = 0;
|
|
31
|
+
const n = text.length;
|
|
32
|
+
const skipSpaces = () => {
|
|
33
|
+
while (i < n && (text[i] === " " || text[i] === "\t"))
|
|
34
|
+
i++;
|
|
35
|
+
};
|
|
36
|
+
const skipWsCommentsNewlines = () => {
|
|
37
|
+
for (;;) {
|
|
38
|
+
while (i < n && /\s/.test(text[i]))
|
|
39
|
+
i++;
|
|
40
|
+
if (text[i] === "#") {
|
|
41
|
+
while (i < n && text[i] !== "\n")
|
|
42
|
+
i++;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const endOfLine = () => {
|
|
49
|
+
skipSpaces();
|
|
50
|
+
if (text[i] === "#")
|
|
51
|
+
while (i < n && text[i] !== "\n")
|
|
52
|
+
i++;
|
|
53
|
+
if (i < n && text[i] === "\r")
|
|
54
|
+
i++;
|
|
55
|
+
if (i < n && text[i] !== "\n")
|
|
56
|
+
throw new TomlSubsetError("trailing content");
|
|
57
|
+
if (i < n)
|
|
58
|
+
i++;
|
|
59
|
+
};
|
|
60
|
+
const readKey = () => {
|
|
61
|
+
const m = /^[A-Za-z0-9_-]+/.exec(text.slice(i, i + 128));
|
|
62
|
+
if (!m)
|
|
63
|
+
throw new TomlSubsetError("bad key");
|
|
64
|
+
i += m[0].length;
|
|
65
|
+
return m[0];
|
|
66
|
+
};
|
|
67
|
+
const readString = () => {
|
|
68
|
+
if (text.startsWith('"""', i) || text.startsWith("'''", i)) {
|
|
69
|
+
const quote = text.slice(i, i + 3);
|
|
70
|
+
i += 3;
|
|
71
|
+
if (text[i] === "\n")
|
|
72
|
+
i++;
|
|
73
|
+
else if (text.startsWith("\r\n", i))
|
|
74
|
+
i += 2;
|
|
75
|
+
const end = text.indexOf(quote, i);
|
|
76
|
+
if (end === -1)
|
|
77
|
+
throw new TomlSubsetError("unterminated string");
|
|
78
|
+
const raw = text.slice(i, end);
|
|
79
|
+
i = end + 3;
|
|
80
|
+
return quote === '"""' ? unescapeBasic(raw) : raw;
|
|
81
|
+
}
|
|
82
|
+
const quote = text[i];
|
|
83
|
+
i++;
|
|
84
|
+
let out = "";
|
|
85
|
+
while (i < n && text[i] !== quote) {
|
|
86
|
+
if (text[i] === "\n")
|
|
87
|
+
throw new TomlSubsetError("newline in string");
|
|
88
|
+
if (quote === '"' && text[i] === "\\") {
|
|
89
|
+
out += text.slice(i, i + 2);
|
|
90
|
+
i += 2;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
out += text[i];
|
|
94
|
+
i++;
|
|
95
|
+
}
|
|
96
|
+
if (i >= n)
|
|
97
|
+
throw new TomlSubsetError("unterminated string");
|
|
98
|
+
i++;
|
|
99
|
+
return quote === '"' ? unescapeBasic(out) : out;
|
|
100
|
+
};
|
|
101
|
+
const readValue = (depth) => {
|
|
102
|
+
if (depth > 4)
|
|
103
|
+
throw new TomlSubsetError("nesting too deep");
|
|
104
|
+
const c = text[i];
|
|
105
|
+
if (c === '"' || c === "'")
|
|
106
|
+
return readString();
|
|
107
|
+
if (c === "[") {
|
|
108
|
+
i++;
|
|
109
|
+
const items = [];
|
|
110
|
+
for (;;) {
|
|
111
|
+
skipWsCommentsNewlines();
|
|
112
|
+
if (text[i] === "]") {
|
|
113
|
+
i++;
|
|
114
|
+
return items;
|
|
115
|
+
}
|
|
116
|
+
items.push(readValue(depth + 1));
|
|
117
|
+
if (items.length > 256)
|
|
118
|
+
throw new TomlSubsetError("array too long");
|
|
119
|
+
skipWsCommentsNewlines();
|
|
120
|
+
if (text[i] === ",") {
|
|
121
|
+
i++;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (text[i] === "]") {
|
|
125
|
+
i++;
|
|
126
|
+
return items;
|
|
127
|
+
}
|
|
128
|
+
throw new TomlSubsetError("bad array");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const m = /^(true|false|[+-]?\d[\d_]*)(?=[\s,\]#]|$)/.exec(text.slice(i, i + 32));
|
|
132
|
+
if (!m)
|
|
133
|
+
throw new TomlSubsetError("unsupported value");
|
|
134
|
+
i += m[0].length;
|
|
135
|
+
if (m[1] === "true")
|
|
136
|
+
return true;
|
|
137
|
+
if (m[1] === "false")
|
|
138
|
+
return false;
|
|
139
|
+
const num = Number(m[1].replace(/_/g, ""));
|
|
140
|
+
if (!Number.isSafeInteger(num))
|
|
141
|
+
throw new TomlSubsetError("bad integer");
|
|
142
|
+
return num;
|
|
143
|
+
};
|
|
144
|
+
while (i < n) {
|
|
145
|
+
skipWsCommentsNewlines();
|
|
146
|
+
if (i >= n)
|
|
147
|
+
break;
|
|
148
|
+
if (text[i] === "[") {
|
|
149
|
+
i++;
|
|
150
|
+
skipSpaces();
|
|
151
|
+
const name = readKey();
|
|
152
|
+
skipSpaces();
|
|
153
|
+
if (text[i] !== "]")
|
|
154
|
+
throw new TomlSubsetError("bad table header");
|
|
155
|
+
i++;
|
|
156
|
+
endOfLine();
|
|
157
|
+
const existing = root[name];
|
|
158
|
+
if (existing !== undefined)
|
|
159
|
+
throw new TomlSubsetError("duplicate table");
|
|
160
|
+
const created = {};
|
|
161
|
+
root[name] = created;
|
|
162
|
+
table = created;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const key = readKey();
|
|
166
|
+
skipSpaces();
|
|
167
|
+
if (text[i] !== "=")
|
|
168
|
+
throw new TomlSubsetError("expected =");
|
|
169
|
+
i++;
|
|
170
|
+
skipSpaces();
|
|
171
|
+
const value = readValue(0);
|
|
172
|
+
if (Object.prototype.hasOwnProperty.call(table, key))
|
|
173
|
+
throw new TomlSubsetError("duplicate key");
|
|
174
|
+
table[key] = value;
|
|
175
|
+
endOfLine();
|
|
176
|
+
}
|
|
177
|
+
return root;
|
|
178
|
+
}
|
|
179
|
+
function unescapeBasic(raw) {
|
|
180
|
+
return raw.replace(/\\(u[0-9A-Fa-f]{4}|[btnfr"\\])/g, (_m, esc) => {
|
|
181
|
+
switch (esc[0]) {
|
|
182
|
+
case "b":
|
|
183
|
+
return "\b";
|
|
184
|
+
case "t":
|
|
185
|
+
return "\t";
|
|
186
|
+
case "n":
|
|
187
|
+
return "\n";
|
|
188
|
+
case "f":
|
|
189
|
+
return "\f";
|
|
190
|
+
case "r":
|
|
191
|
+
return "\r";
|
|
192
|
+
case "u":
|
|
193
|
+
return String.fromCharCode(parseInt(esc.slice(1), 16));
|
|
194
|
+
default:
|
|
195
|
+
return esc;
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
// Guidance projection
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
const HINT_KEYS = ["prepare", "database", "migrate", "seed", "start", "ready"];
|
|
203
|
+
const TIMING_KEYS = [
|
|
204
|
+
"turn_wait_seconds",
|
|
205
|
+
"lease_seconds",
|
|
206
|
+
"max_lifetime_seconds",
|
|
207
|
+
"ready_seconds",
|
|
208
|
+
"start_seconds",
|
|
209
|
+
"proof_seconds",
|
|
210
|
+
];
|
|
211
|
+
const MAX_GUIDANCE_CHARS = 16_000;
|
|
212
|
+
function isStringArray(value) {
|
|
213
|
+
return Array.isArray(value) && value.every((v) => typeof v === "string");
|
|
214
|
+
}
|
|
215
|
+
function projectHint(value) {
|
|
216
|
+
if (typeof value === "string")
|
|
217
|
+
return value;
|
|
218
|
+
if (isStringArray(value))
|
|
219
|
+
return [...value];
|
|
220
|
+
if (Array.isArray(value) && value.length > 0 && value.every((v) => isStringArray(v))) {
|
|
221
|
+
return value.map((argv) => [...argv]);
|
|
222
|
+
}
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
function isPlainObject(value) {
|
|
226
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Project parsed guidance onto the typed shape. Keys may sit at the top level
|
|
230
|
+
* or under `[hints]` and `[timing]` tables. Unknown keys are ignored; a known key
|
|
231
|
+
* with an unusable value is dropped rather than failing the whole file.
|
|
232
|
+
*/
|
|
233
|
+
export function projectEpicServerGuidance(doc) {
|
|
234
|
+
const hintsTable = isPlainObject(doc.hints) ? doc.hints : {};
|
|
235
|
+
const timingTable = isPlainObject(doc.timing) ? doc.timing : {};
|
|
236
|
+
const guidanceText = typeof doc.guidance === "string" ? doc.guidance.slice(0, MAX_GUIDANCE_CHARS) : null;
|
|
237
|
+
const hints = {};
|
|
238
|
+
for (const key of HINT_KEYS) {
|
|
239
|
+
const value = hintsTable[key] ?? doc[key];
|
|
240
|
+
const projected = projectHint(value);
|
|
241
|
+
if (projected !== undefined)
|
|
242
|
+
hints[key] = projected;
|
|
243
|
+
}
|
|
244
|
+
const timing = {};
|
|
245
|
+
for (const key of TIMING_KEYS) {
|
|
246
|
+
const value = timingTable[key] ?? doc[key];
|
|
247
|
+
if (typeof value === "number" && Number.isSafeInteger(value) && value > 0)
|
|
248
|
+
timing[key] = value;
|
|
249
|
+
}
|
|
250
|
+
return { guidance: guidanceText, hints, timing };
|
|
251
|
+
}
|
|
252
|
+
/** Parse guidance text. Malformed text is "no guidance". */
|
|
253
|
+
export function parseEpicServerGuidance(text) {
|
|
254
|
+
if (Buffer.byteLength(text, "utf8") > EPIC_SERVER_GUIDANCE_MAX_BYTES)
|
|
255
|
+
return { state: "none" };
|
|
256
|
+
let doc;
|
|
257
|
+
try {
|
|
258
|
+
doc = parseTomlSubset(text);
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
return { state: "none" };
|
|
262
|
+
}
|
|
263
|
+
const guidance = projectEpicServerGuidance(doc);
|
|
264
|
+
if (guidance.guidance === null && Object.keys(guidance.hints).length === 0)
|
|
265
|
+
return { state: "none" };
|
|
266
|
+
return { state: "ok", guidance };
|
|
267
|
+
}
|
|
268
|
+
/** Read the guidance file through an injected reader. Never throws. */
|
|
269
|
+
export async function readEpicServerGuidance(readFile) {
|
|
270
|
+
let text;
|
|
271
|
+
try {
|
|
272
|
+
text = await readFile(EPIC_SERVER_GUIDANCE_PATH);
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
return { state: "none" };
|
|
276
|
+
}
|
|
277
|
+
return parseEpicServerGuidance(text);
|
|
278
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The epic-server helper's bounded lifecycle journal (BAPI-1546).
|
|
3
|
+
*
|
|
4
|
+
* The helper serves in the background, after the driver's `start` returned. Its
|
|
5
|
+
* background events (turn grants and releases, lease expiry, serving failures,
|
|
6
|
+
* recovery outcomes, stop) go to one run-local JSONL file the driver reads
|
|
7
|
+
* through `epic-server status --since <seq>`. The DRIVER stays the only tally
|
|
8
|
+
* owner: it records each event once, deduplicating by the stable event id.
|
|
9
|
+
*
|
|
10
|
+
* Only background events are journaled. A driver command's own outcome (a failed
|
|
11
|
+
* `prove` or `start`) is reported by that command and tallied from there, so it
|
|
12
|
+
* is never counted a second time from the journal.
|
|
13
|
+
*
|
|
14
|
+
* Every record is closed-vocabulary: an event name, a sequence number, a safe
|
|
15
|
+
* timestamp, and optionally a generation, a ticket key, a 12-character SHA
|
|
16
|
+
* prefix, an outcome, and a reason token. It NEVER holds a login, seed output, a
|
|
17
|
+
* credential or credential reference, a raw command, or an ownership token.
|
|
18
|
+
*
|
|
19
|
+
* Retention is bounded. Events the driver has acknowledged (a `status --since`
|
|
20
|
+
* cursor) are compacted away first; if the file still exceeds its bound, the
|
|
21
|
+
* oldest events are dropped and one `journal_overflow` summary says how many.
|
|
22
|
+
* Journaling never blocks serving: a write failure is swallowed.
|
|
23
|
+
*/
|
|
24
|
+
import path from "node:path";
|
|
25
|
+
import { readPrivateJson, writePrivateFileAtomic } from "./epic-server-state.js";
|
|
26
|
+
export const EPIC_SERVER_JOURNAL_VERSION = 1;
|
|
27
|
+
/** The closed set of background events. */
|
|
28
|
+
export const EPIC_SERVER_JOURNAL_EVENTS = [
|
|
29
|
+
"helper_started",
|
|
30
|
+
"bootstrap_served",
|
|
31
|
+
"bootstrap_failed",
|
|
32
|
+
"turn_granted",
|
|
33
|
+
"turn_released",
|
|
34
|
+
"turn_lost",
|
|
35
|
+
"lease_expired",
|
|
36
|
+
"acquire_timed_out",
|
|
37
|
+
"serving_failed",
|
|
38
|
+
"app_exited",
|
|
39
|
+
"recovery_reclaimed",
|
|
40
|
+
"recovery_retained",
|
|
41
|
+
"helper_stopped",
|
|
42
|
+
"journal_overflow",
|
|
43
|
+
];
|
|
44
|
+
/** Closed reason tokens. */
|
|
45
|
+
export const EPIC_SERVER_REASONS = [
|
|
46
|
+
"run_terminal",
|
|
47
|
+
"cancelled",
|
|
48
|
+
"max_lifetime",
|
|
49
|
+
"signal",
|
|
50
|
+
"guarded_stop",
|
|
51
|
+
"env_file_guard_failed",
|
|
52
|
+
"worktree_failed",
|
|
53
|
+
"prepare_failed",
|
|
54
|
+
"database_unavailable",
|
|
55
|
+
"migrate_failed",
|
|
56
|
+
"seed_failed",
|
|
57
|
+
"seed_output_invalid",
|
|
58
|
+
"start_failed",
|
|
59
|
+
"ready_timeout",
|
|
60
|
+
"ready_redirect_refused",
|
|
61
|
+
"credential_unavailable",
|
|
62
|
+
"expired",
|
|
63
|
+
"fenced",
|
|
64
|
+
"app_failed",
|
|
65
|
+
"helper_replaced",
|
|
66
|
+
"released",
|
|
67
|
+
"wait_timeout",
|
|
68
|
+
"cleanup_incomplete",
|
|
69
|
+
"dropped_events",
|
|
70
|
+
];
|
|
71
|
+
/** Most events kept on disk before the oldest are dropped. */
|
|
72
|
+
export const EPIC_SERVER_JOURNAL_MAX_EVENTS = 500;
|
|
73
|
+
/** Most events returned by one status read. */
|
|
74
|
+
export const EPIC_SERVER_JOURNAL_MAX_READ = 100;
|
|
75
|
+
const TICKET_RE = /^[A-Z][A-Z0-9_]{0,19}-\d{1,9}$/;
|
|
76
|
+
const SHA_RE = /^[0-9a-f]{40}$/;
|
|
77
|
+
const RUN_ID_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{7,63}$/;
|
|
78
|
+
/** Build one sanitized entry. Fields outside the closed shapes are dropped. */
|
|
79
|
+
export function buildJournalEntry(runId, seq, at, event, fields = {}) {
|
|
80
|
+
const entry = {
|
|
81
|
+
v: EPIC_SERVER_JOURNAL_VERSION,
|
|
82
|
+
id: `${runId}:${seq}`,
|
|
83
|
+
seq,
|
|
84
|
+
at: at.toISOString(),
|
|
85
|
+
source: "helper",
|
|
86
|
+
event,
|
|
87
|
+
};
|
|
88
|
+
if (typeof fields.generation === "number" && Number.isSafeInteger(fields.generation) && fields.generation >= 0) {
|
|
89
|
+
entry.generation = fields.generation;
|
|
90
|
+
}
|
|
91
|
+
if (typeof fields.ticket === "string" && TICKET_RE.test(fields.ticket))
|
|
92
|
+
entry.ticket = fields.ticket;
|
|
93
|
+
if (typeof fields.sha === "string" && SHA_RE.test(fields.sha))
|
|
94
|
+
entry.sha12 = fields.sha.slice(0, 12);
|
|
95
|
+
if (fields.outcome !== undefined && ["ok", "failed", "retained", "lost"].includes(fields.outcome))
|
|
96
|
+
entry.outcome = fields.outcome;
|
|
97
|
+
if (fields.reason !== undefined && EPIC_SERVER_REASONS.includes(fields.reason))
|
|
98
|
+
entry.reason = fields.reason;
|
|
99
|
+
if (typeof fields.count === "number" && Number.isSafeInteger(fields.count) && fields.count >= 0)
|
|
100
|
+
entry.count = fields.count;
|
|
101
|
+
return entry;
|
|
102
|
+
}
|
|
103
|
+
/** Validate one parsed entry; anything outside the closed shape is null. */
|
|
104
|
+
export function validateJournalEntry(raw) {
|
|
105
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw))
|
|
106
|
+
return null;
|
|
107
|
+
const doc = raw;
|
|
108
|
+
if (doc.v !== EPIC_SERVER_JOURNAL_VERSION || doc.source !== "helper")
|
|
109
|
+
return null;
|
|
110
|
+
if (typeof doc.seq !== "number" || !Number.isSafeInteger(doc.seq) || doc.seq < 1)
|
|
111
|
+
return null;
|
|
112
|
+
if (typeof doc.id !== "string" || !doc.id.endsWith(`:${doc.seq}`) || !RUN_ID_RE.test(doc.id.slice(0, doc.id.lastIndexOf(":")))) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
if (typeof doc.at !== "string" || Number.isNaN(Date.parse(doc.at)))
|
|
116
|
+
return null;
|
|
117
|
+
if (typeof doc.event !== "string" || !EPIC_SERVER_JOURNAL_EVENTS.includes(doc.event))
|
|
118
|
+
return null;
|
|
119
|
+
const runId = doc.id.slice(0, doc.id.lastIndexOf(":"));
|
|
120
|
+
const rebuilt = buildJournalEntry(runId, doc.seq, new Date(doc.at), doc.event, {
|
|
121
|
+
generation: doc.generation,
|
|
122
|
+
ticket: doc.ticket,
|
|
123
|
+
outcome: doc.outcome,
|
|
124
|
+
reason: doc.reason,
|
|
125
|
+
count: doc.count,
|
|
126
|
+
});
|
|
127
|
+
if (typeof doc.sha12 === "string" && /^[0-9a-f]{12}$/.test(doc.sha12))
|
|
128
|
+
rebuilt.sha12 = doc.sha12;
|
|
129
|
+
return rebuilt;
|
|
130
|
+
}
|
|
131
|
+
function emptyDocument() {
|
|
132
|
+
return { v: EPIC_SERVER_JOURNAL_VERSION, next_seq: 1, acked_seq: 0, events: [] };
|
|
133
|
+
}
|
|
134
|
+
function parseDocument(raw) {
|
|
135
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw))
|
|
136
|
+
return null;
|
|
137
|
+
const doc = raw;
|
|
138
|
+
if (doc.v !== EPIC_SERVER_JOURNAL_VERSION)
|
|
139
|
+
return null;
|
|
140
|
+
if (typeof doc.next_seq !== "number" || !Number.isSafeInteger(doc.next_seq) || doc.next_seq < 1)
|
|
141
|
+
return null;
|
|
142
|
+
if (typeof doc.acked_seq !== "number" || !Number.isSafeInteger(doc.acked_seq) || doc.acked_seq < 0)
|
|
143
|
+
return null;
|
|
144
|
+
if (!Array.isArray(doc.events))
|
|
145
|
+
return null;
|
|
146
|
+
const events = doc.events.map(validateJournalEntry).filter((e) => e !== null);
|
|
147
|
+
return { v: EPIC_SERVER_JOURNAL_VERSION, next_seq: doc.next_seq, acked_seq: doc.acked_seq, events };
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The journal. One writer (the helper); readers may be the helper (for a status
|
|
151
|
+
* request) or the CLI after the helper exited. The document is rewritten
|
|
152
|
+
* atomically on every append, so a reader never sees a torn line.
|
|
153
|
+
*/
|
|
154
|
+
export class EpicServerJournal {
|
|
155
|
+
filePath;
|
|
156
|
+
runId;
|
|
157
|
+
fs;
|
|
158
|
+
now;
|
|
159
|
+
maxEvents;
|
|
160
|
+
doc = null;
|
|
161
|
+
constructor(filePath, runId, fs, now = () => new Date(), maxEvents = EPIC_SERVER_JOURNAL_MAX_EVENTS) {
|
|
162
|
+
this.filePath = filePath;
|
|
163
|
+
this.runId = runId;
|
|
164
|
+
this.fs = fs;
|
|
165
|
+
this.now = now;
|
|
166
|
+
this.maxEvents = maxEvents;
|
|
167
|
+
}
|
|
168
|
+
static pathFor(runDir) {
|
|
169
|
+
return path.join(runDir, "journal.json");
|
|
170
|
+
}
|
|
171
|
+
async load() {
|
|
172
|
+
if (this.doc !== null)
|
|
173
|
+
return this.doc;
|
|
174
|
+
const raw = await readPrivateJson(this.fs, this.filePath);
|
|
175
|
+
this.doc = parseDocument(raw) ?? emptyDocument();
|
|
176
|
+
return this.doc;
|
|
177
|
+
}
|
|
178
|
+
/** Append one background event. Never throws, never blocks serving for long. */
|
|
179
|
+
async append(event, fields = {}) {
|
|
180
|
+
try {
|
|
181
|
+
const doc = await this.load();
|
|
182
|
+
doc.events.push(buildJournalEntry(this.runId, doc.next_seq, this.now(), event, fields));
|
|
183
|
+
doc.next_seq += 1;
|
|
184
|
+
this.compact(doc);
|
|
185
|
+
await writePrivateFileAtomic(this.fs, this.filePath, `${JSON.stringify(doc)}\n`);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
/* journaling is best-effort; serving continues */
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/** Record the driver's acknowledgement (a status cursor). */
|
|
192
|
+
async acknowledge(cursor) {
|
|
193
|
+
try {
|
|
194
|
+
const doc = await this.load();
|
|
195
|
+
if (Number.isSafeInteger(cursor) && cursor > doc.acked_seq && cursor < doc.next_seq) {
|
|
196
|
+
doc.acked_seq = cursor;
|
|
197
|
+
this.compact(doc);
|
|
198
|
+
await writePrivateFileAtomic(this.fs, this.filePath, `${JSON.stringify(doc)}\n`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
/* best-effort */
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
compact(doc) {
|
|
206
|
+
if (doc.events.length <= this.maxEvents)
|
|
207
|
+
return;
|
|
208
|
+
// Acknowledged events go first.
|
|
209
|
+
doc.events = doc.events.filter((e) => e.seq > doc.acked_seq);
|
|
210
|
+
if (doc.events.length <= this.maxEvents)
|
|
211
|
+
return;
|
|
212
|
+
// One summary at most: an earlier unacknowledged summary is folded into the
|
|
213
|
+
// new one (its count carried forward) instead of piling up beside it.
|
|
214
|
+
let carried = 0;
|
|
215
|
+
const regular = [];
|
|
216
|
+
for (const e of doc.events) {
|
|
217
|
+
if (e.event === "journal_overflow")
|
|
218
|
+
carried += e.count ?? 0;
|
|
219
|
+
else
|
|
220
|
+
regular.push(e);
|
|
221
|
+
}
|
|
222
|
+
const overflow = Math.max(0, regular.length - (this.maxEvents - 1));
|
|
223
|
+
const kept = regular.slice(overflow);
|
|
224
|
+
const summary = buildJournalEntry(this.runId, doc.next_seq, this.now(), "journal_overflow", {
|
|
225
|
+
reason: "dropped_events",
|
|
226
|
+
count: carried + overflow,
|
|
227
|
+
});
|
|
228
|
+
doc.next_seq += 1;
|
|
229
|
+
doc.events = [...kept, summary];
|
|
230
|
+
}
|
|
231
|
+
/** Read unseen events after `since`, bounded. */
|
|
232
|
+
async read(since) {
|
|
233
|
+
const doc = this.doc ?? parseDocument(await readPrivateJson(this.fs, this.filePath)) ?? emptyDocument();
|
|
234
|
+
const unseen = doc.events.filter((e) => e.seq > since);
|
|
235
|
+
const events = unseen.slice(0, EPIC_SERVER_JOURNAL_MAX_READ);
|
|
236
|
+
const cursor = events.length > 0 ? events[events.length - 1].seq : Math.max(0, since);
|
|
237
|
+
return { events, cursor, more: unseen.length > events.length };
|
|
238
|
+
}
|
|
239
|
+
}
|