@warpgogol/forge 9.4.1 → 9.5.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 +15 -0
- package/bin/cli.ts +152 -11
- package/dist/bin/cli.js +125 -5
- package/dist/bin/cli.js.map +1 -1
- package/dist/os/flow/dirty-classify.d.ts +7 -0
- package/dist/os/flow/dirty-classify.d.ts.map +1 -1
- package/dist/os/flow/dirty-classify.js +11 -0
- package/dist/os/flow/dirty-classify.js.map +1 -1
- package/dist/os/flow/flow.module.d.ts.map +1 -1
- package/dist/os/flow/flow.module.js +3 -2
- package/dist/os/flow/flow.module.js.map +1 -1
- package/dist/os/flow/handlers/flow-check.d.ts.map +1 -1
- package/dist/os/flow/handlers/flow-check.js +9 -9
- package/dist/os/flow/handlers/flow-check.js.map +1 -1
- package/dist/os/flow/handlers/flow-integrate.d.ts.map +1 -1
- package/dist/os/flow/handlers/flow-integrate.js +59 -26
- package/dist/os/flow/handlers/flow-integrate.js.map +1 -1
- package/dist/os/flow/handlers/flow-open.d.ts.map +1 -1
- package/dist/os/flow/handlers/flow-open.js +14 -19
- package/dist/os/flow/handlers/flow-open.js.map +1 -1
- package/dist/os/flow/index.d.ts +1 -0
- package/dist/os/flow/index.d.ts.map +1 -1
- package/dist/os/flow/index.js +5 -3
- package/dist/os/flow/index.js.map +1 -1
- package/dist/os/flow/preflight.d.ts +24 -0
- package/dist/os/flow/preflight.d.ts.map +1 -1
- package/dist/os/flow/preflight.js +354 -10
- package/dist/os/flow/preflight.js.map +1 -1
- package/dist/os/flow/registry.d.ts +2 -2
- package/dist/os/gate-run/detach.d.ts +27 -0
- package/dist/os/gate-run/detach.d.ts.map +1 -0
- package/dist/os/gate-run/detach.js +85 -0
- package/dist/os/gate-run/detach.js.map +1 -0
- package/dist/os/gate-run/gate-run.module.d.ts +3 -0
- package/dist/os/gate-run/gate-run.module.d.ts.map +1 -0
- package/dist/os/gate-run/gate-run.module.js +23 -0
- package/dist/os/gate-run/gate-run.module.js.map +1 -0
- package/dist/os/gate-run/index.d.ts +7 -0
- package/dist/os/gate-run/index.d.ts.map +1 -0
- package/dist/os/gate-run/index.js +5 -0
- package/dist/os/gate-run/index.js.map +1 -0
- package/dist/os/gate-run/record.d.ts +47 -0
- package/dist/os/gate-run/record.d.ts.map +1 -0
- package/dist/os/gate-run/record.js +157 -0
- package/dist/os/gate-run/record.js.map +1 -0
- package/dist/os/gate-run/status.d.ts +41 -0
- package/dist/os/gate-run/status.d.ts.map +1 -0
- package/dist/os/gate-run/status.js +142 -0
- package/dist/os/gate-run/status.js.map +1 -0
- package/dist/os/gate-run/types.d.ts +47 -0
- package/dist/os/gate-run/types.d.ts.map +1 -0
- package/dist/os/gate-run/types.js +24 -0
- package/dist/os/gate-run/types.js.map +1 -0
- package/dist/os/rfc/evidence-annotation.d.ts +26 -0
- package/dist/os/rfc/evidence-annotation.d.ts.map +1 -0
- package/dist/os/rfc/evidence-annotation.js +52 -0
- package/dist/os/rfc/evidence-annotation.js.map +1 -0
- package/dist/os/rfc/handlers/fixed-inputs.d.ts.map +1 -1
- package/dist/os/rfc/handlers/fixed-inputs.js +7 -1
- package/dist/os/rfc/handlers/fixed-inputs.js.map +1 -1
- package/dist/os/rfc/handlers/implement-stamp.d.ts.map +1 -1
- package/dist/os/rfc/handlers/implement-stamp.js +21 -28
- package/dist/os/rfc/handlers/implement-stamp.js.map +1 -1
- package/dist/os/rfc/handlers/validate-rules.d.ts.map +1 -1
- package/dist/os/rfc/handlers/validate-rules.js +127 -7
- package/dist/os/rfc/handlers/validate-rules.js.map +1 -1
- package/dist/os/rfc/rfc.module.d.ts.map +1 -1
- package/dist/os/rfc/rfc.module.js +3 -2
- package/dist/os/rfc/rfc.module.js.map +1 -1
- package/dist/src/cli-flags.d.ts.map +1 -1
- package/dist/src/cli-flags.js +6 -0
- package/dist/src/cli-flags.js.map +1 -1
- package/dist/src/types.d.ts +7 -0
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +2 -8
- package/dist/src/types.js.map +1 -1
- package/os/flow/dirty-classify.ts +13 -0
- package/os/flow/flow.module.ts +3 -2
- package/os/flow/handlers/flow-check.ts +17 -18
- package/os/flow/handlers/flow-integrate.ts +75 -35
- package/os/flow/handlers/flow-open.ts +24 -31
- package/os/flow/index.ts +5 -7
- package/os/flow/preflight.ts +351 -9
- package/os/flow/tests/flow-integrate.preflight.test.ts +26 -5
- package/os/flow/tests/flow-integrate.test.ts +68 -2
- package/os/flow/tests/lockfile-induced.test.ts +328 -0
- package/os/gate-run/detach.ts +106 -0
- package/os/gate-run/gate-run.module.ts +25 -0
- package/os/gate-run/index.ts +16 -0
- package/os/gate-run/record.ts +194 -0
- package/os/gate-run/status.ts +168 -0
- package/os/gate-run/tests/gate-run-detach.test.ts +144 -0
- package/os/gate-run/tests/gate-run-record.test.ts +200 -0
- package/os/gate-run/types.ts +73 -0
- package/os/rfc/evidence-annotation.ts +61 -0
- package/os/rfc/handlers/fixed-inputs.test.ts +70 -0
- package/os/rfc/handlers/fixed-inputs.ts +11 -1
- package/os/rfc/handlers/implement-stamp.ts +25 -28
- package/os/rfc/handlers/list-create.test.ts +14 -5
- package/os/rfc/handlers/validate-evidence-shape.test.ts +218 -0
- package/os/rfc/handlers/validate-probe-meta.test.ts +71 -0
- package/os/rfc/handlers/validate-rules.ts +169 -17
- package/os/rfc/handlers/validate-transient-probes.test.ts +222 -0
- package/os/rfc/rfc-0000-template.md +10 -10
- package/os/rfc/rfc.module.ts +3 -2
- package/package.json +17 -1
- package/skills/fo/fo-session-retro/SKILL.md +4 -2
- package/src/cli-flags.ts +7 -0
- package/src/types.ts +9 -8
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>RFC-1568 — `gate.run.status` handler: reports gate-run records from
|
|
4
|
+
`<git common dir>/forge/gate-runs/`. Selection: --id (one), --last
|
|
5
|
+
(newest, optional --command filter), default (10 newest). --wait polls up
|
|
6
|
+
to N seconds (max 600) until the run is no longer running. The command
|
|
7
|
+
exits 0 whenever records are readable — a gate's own outcome lives in
|
|
8
|
+
`data.runs[].status`, never in this command's exit code (DNA-106).</purpose>
|
|
9
|
+
<non-goals>
|
|
10
|
+
<item>No run lifecycle mutation — records are written by the runner.</item>
|
|
11
|
+
</non-goals>
|
|
12
|
+
</MODULE_CONTRACT>
|
|
13
|
+
<CHANGE_SUMMARY>
|
|
14
|
+
<item>RFC-1568: step 4 — gate.run.status handler</item>
|
|
15
|
+
<item>RFC-1568: durable gate-run records, --detach mechanics, and forge CLI wiring</item>
|
|
16
|
+
<item>RFC-1562: rfc-1562 acceptance evidence, regenerated manifests, gate contract tag</item>
|
|
17
|
+
</CHANGE_SUMMARY>
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type {
|
|
21
|
+
ForgeCommandInput,
|
|
22
|
+
ForgeCommandResult,
|
|
23
|
+
ForgeRuntimeContext,
|
|
24
|
+
} from "../../src/types.ts";
|
|
25
|
+
import { resolveIo } from "../../src/utils/io.ts";
|
|
26
|
+
import { commonGitDir } from "../flow/registry.ts";
|
|
27
|
+
import { classifyGateRun, listGateRuns } from "./record.ts";
|
|
28
|
+
import type { GateRunRecord, GateRunStatus } from "./types.ts";
|
|
29
|
+
|
|
30
|
+
const WAIT_MAX_SECONDS = 600;
|
|
31
|
+
const WAIT_POLL_MS = 2_000;
|
|
32
|
+
|
|
33
|
+
export interface GateRunStatusData {
|
|
34
|
+
runs: Array<GateRunRecord & { status: GateRunStatus }>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function flag(input: ForgeCommandInput, name: string): string | undefined {
|
|
38
|
+
const v = input.flags[name];
|
|
39
|
+
return typeof v === "string" ? v : undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function boolFlag(input: ForgeCommandInput, name: string): boolean {
|
|
43
|
+
return input.flags[name] === true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function withClassified(record: GateRunRecord): GateRunRecord & { status: GateRunStatus } {
|
|
47
|
+
return { ...record, status: classifyGateRun(record) };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function formatRunLine(record: GateRunRecord & { status: GateRunStatus }): string {
|
|
51
|
+
const bits: string[] = [record.status];
|
|
52
|
+
if (record.exitCode !== undefined) bits.push(`exit ${record.exitCode}`);
|
|
53
|
+
bits.push(record.startedAt);
|
|
54
|
+
if (record.endedAt) bits.push(`→ ${record.endedAt}`);
|
|
55
|
+
return `[gate-run] ${record.runId} ${record.command} ${bits.join(" ")}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function runGateRunStatus(
|
|
59
|
+
input: ForgeCommandInput,
|
|
60
|
+
context: ForgeRuntimeContext,
|
|
61
|
+
): Promise<ForgeCommandResult<GateRunStatusData>> {
|
|
62
|
+
const io = resolveIo(context.io);
|
|
63
|
+
const root = context.forgeRoot ?? context.workspaceRoot;
|
|
64
|
+
let common: string;
|
|
65
|
+
try {
|
|
66
|
+
common = await commonGitDir(io, root);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
return {
|
|
69
|
+
exitCode: 1,
|
|
70
|
+
summary: `[gate.run.status] cannot resolve the git common dir: ${(error as Error).message}`,
|
|
71
|
+
data: { runs: [] },
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const id = flag(input, "id");
|
|
76
|
+
const command = flag(input, "command");
|
|
77
|
+
const last = boolFlag(input, "last");
|
|
78
|
+
const waitRaw = flag(input, "wait");
|
|
79
|
+
const waitSeconds = waitRaw === undefined ? 0 : Math.min(Number(waitRaw) || 0, WAIT_MAX_SECONDS);
|
|
80
|
+
|
|
81
|
+
if (id !== undefined) {
|
|
82
|
+
const deadline = Date.now() + waitSeconds * 1_000;
|
|
83
|
+
for (;;) {
|
|
84
|
+
const all = await listGateRuns(common, io);
|
|
85
|
+
const found = all.find((r) => r.runId === id);
|
|
86
|
+
if (found === undefined) {
|
|
87
|
+
return {
|
|
88
|
+
exitCode: 1,
|
|
89
|
+
summary: `[gate.run.status] GATE-RUN-03 unknown run id ${id}`,
|
|
90
|
+
data: { runs: [] },
|
|
91
|
+
nextSteps: [
|
|
92
|
+
{ action: "gate.run.status --last — inspect the most recent run", kind: "optional" },
|
|
93
|
+
],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const classified = withClassified(found);
|
|
97
|
+
if (classified.status !== "running" || Date.now() >= deadline) {
|
|
98
|
+
return {
|
|
99
|
+
exitCode: 0,
|
|
100
|
+
summary: formatRunLine(classified),
|
|
101
|
+
data: { runs: [classified] },
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
await new Promise((r) =>
|
|
105
|
+
setTimeout(r, Math.min(WAIT_POLL_MS, Math.max(50, deadline - Date.now()))),
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (last) {
|
|
111
|
+
const all = await listGateRuns(common, io, { command, limit: 1 });
|
|
112
|
+
const runs = all.map(withClassified);
|
|
113
|
+
return {
|
|
114
|
+
exitCode: 0,
|
|
115
|
+
summary:
|
|
116
|
+
runs.length === 0
|
|
117
|
+
? `[gate.run.status] no gate runs recorded${command ? ` for ${command}` : ""}`
|
|
118
|
+
: formatRunLine(runs[0]!),
|
|
119
|
+
data: { runs },
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const all = await listGateRuns(common, io, { command, limit: 10 });
|
|
124
|
+
const runs = all.map(withClassified);
|
|
125
|
+
return {
|
|
126
|
+
exitCode: 0,
|
|
127
|
+
summary:
|
|
128
|
+
runs.length === 0
|
|
129
|
+
? `[gate.run.status] no gate runs recorded${command ? ` for ${command}` : ""}`
|
|
130
|
+
: `[gate.run.status] ${runs.length} recent run(s):\n${runs.map(formatRunLine).join("\n")}`,
|
|
131
|
+
data: { runs },
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export const gateRunStatusCommand = {
|
|
136
|
+
name: "gate.run.status",
|
|
137
|
+
description:
|
|
138
|
+
"Report durable gate-run records written by detachable commands " +
|
|
139
|
+
"(RFC-1568). `--id` one run, `--last` the most recent (optionally " +
|
|
140
|
+
"filtered by `--command`), default the 10 newest; `--wait <s>` blocks " +
|
|
141
|
+
"up to 600 s until the run leaves `running`. Classifies unfinished runs " +
|
|
142
|
+
"as `lost` via the same dead-holder liveness rule as flow locks. Exits " +
|
|
143
|
+
"0 whenever records are readable — the gate's outcome lives in " +
|
|
144
|
+
"`data.runs[].status`, never in this command's exit code (DNA-106).",
|
|
145
|
+
scope: "workspace" as const,
|
|
146
|
+
contract: "gate",
|
|
147
|
+
rules: [
|
|
148
|
+
{ id: "GATE-RUN-01", severity: "error" as const },
|
|
149
|
+
{ id: "GATE-RUN-02", severity: "error" as const },
|
|
150
|
+
{ id: "GATE-RUN-03", severity: "error" as const },
|
|
151
|
+
],
|
|
152
|
+
mutatesState: false,
|
|
153
|
+
execOnReadOnly: true,
|
|
154
|
+
reads: ["forge/gate-runs/**"],
|
|
155
|
+
flags: {
|
|
156
|
+
id: { kind: "string" as const, description: "One run id." },
|
|
157
|
+
last: {
|
|
158
|
+
kind: "boolean" as const,
|
|
159
|
+
description: "Most recent run (optionally filtered by --command).",
|
|
160
|
+
},
|
|
161
|
+
command: { kind: "string" as const, description: "Filter by command name." },
|
|
162
|
+
wait: {
|
|
163
|
+
kind: "string" as const,
|
|
164
|
+
description: "Block up to N seconds (max 600) until the run is no longer running.",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
execute: runGateRunStatus,
|
|
168
|
+
};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>RFC-1568 detach mechanics tests (AC-4, AC-5): detachSelf passes the
|
|
4
|
+
entry/argv/env contract to an injected spawner and returns the child's pid
|
|
5
|
+
+ log path; the detached child adopts the parent's record via
|
|
6
|
+
WERKSTATT_GATE_RUN_ID (startGateRun with runId) so a detached run leaves
|
|
7
|
+
exactly one record carrying the child's pid.</purpose>
|
|
8
|
+
<non-goals>
|
|
9
|
+
<item>No real spawned gates — SpawnLike is faked; one integration case may
|
|
10
|
+
spawn `node -e`.</item>
|
|
11
|
+
</non-goals>
|
|
12
|
+
</MODULE_CONTRACT>
|
|
13
|
+
<CHANGE_SUMMARY>
|
|
14
|
+
<item>RFC-1568: step 3 — detach tests</item>
|
|
15
|
+
<item>RFC-1568: durable gate-run records, --detach mechanics, and forge CLI wiring</item>
|
|
16
|
+
</CHANGE_SUMMARY>
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { existsSync } from "node:fs";
|
|
20
|
+
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
21
|
+
import { tmpdir } from "node:os";
|
|
22
|
+
import { join } from "node:path";
|
|
23
|
+
|
|
24
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
25
|
+
|
|
26
|
+
import { resolveIo } from "../../../src/utils/io.ts";
|
|
27
|
+
import { GATE_RUN_ID_ENV, detachPointerLines, detachSelf } from "../detach.ts";
|
|
28
|
+
import { gateRunsDir, listGateRuns, saveGateRun, startGateRun } from "../record.ts";
|
|
29
|
+
import type { GateRunRecord, SpawnLike } from "../types.ts";
|
|
30
|
+
import { GATE_RUN_SCHEMA } from "../types.ts";
|
|
31
|
+
|
|
32
|
+
const dirs: string[] = [];
|
|
33
|
+
afterEach(async () => {
|
|
34
|
+
for (const dir of dirs.splice(0)) await rm(dir, { recursive: true, force: true });
|
|
35
|
+
}, 20_000);
|
|
36
|
+
|
|
37
|
+
async function tempCommon(): Promise<string> {
|
|
38
|
+
const dir = await mkdtemp(join(tmpdir(), "gate-run-detach-"));
|
|
39
|
+
dirs.push(dir);
|
|
40
|
+
return dir;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const io = resolveIo(undefined);
|
|
44
|
+
|
|
45
|
+
function recordFixture(common: string): Promise<GateRunRecord> {
|
|
46
|
+
return startGateRun({
|
|
47
|
+
commonGitDir: common,
|
|
48
|
+
command: "flow.integrate",
|
|
49
|
+
argv: ["--files", "a.md"],
|
|
50
|
+
cwd: ".",
|
|
51
|
+
detached: true,
|
|
52
|
+
io,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
describe("detachSelf", () => {
|
|
57
|
+
it("AC-4: spawns the CLI detached with the run id env and returns the child pid", async () => {
|
|
58
|
+
const common = await tempCommon();
|
|
59
|
+
const record = await recordFixture(common);
|
|
60
|
+
const seen: {
|
|
61
|
+
file?: string;
|
|
62
|
+
args?: string[];
|
|
63
|
+
opts?: { env?: Record<string, string | undefined>; stdio?: unknown[]; detached?: boolean };
|
|
64
|
+
} = {};
|
|
65
|
+
const fakeSpawn: SpawnLike = (file, args, opts) => {
|
|
66
|
+
seen.file = file;
|
|
67
|
+
seen.args = args;
|
|
68
|
+
seen.opts = opts;
|
|
69
|
+
return { pid: 424_242, unref() {} };
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const result = await detachSelf({
|
|
73
|
+
entry: "/repo/packages/forge/bin/cli.ts",
|
|
74
|
+
argv: ["flow.integrate", "--files", "a.md"],
|
|
75
|
+
record,
|
|
76
|
+
commonGitDir: common,
|
|
77
|
+
spawn: fakeSpawn,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
expect(result.pid).toBe(424_242);
|
|
81
|
+
expect(result.runId).toBe(record.runId);
|
|
82
|
+
expect(seen.file).toBe(process.execPath);
|
|
83
|
+
// execArgv forwards the host's loader flags (tsx) so the child re-enters
|
|
84
|
+
// through the same module pipeline as the parent.
|
|
85
|
+
expect(seen.args).toEqual([
|
|
86
|
+
...process.execArgv,
|
|
87
|
+
"/repo/packages/forge/bin/cli.ts",
|
|
88
|
+
"flow.integrate",
|
|
89
|
+
"--files",
|
|
90
|
+
"a.md",
|
|
91
|
+
]);
|
|
92
|
+
expect(seen.opts?.detached).toBe(true);
|
|
93
|
+
expect(seen.opts?.env?.[GATE_RUN_ID_ENV]).toBe(record.runId);
|
|
94
|
+
// stdio: stdin ignored, stdout+stderr routed into the log fd.
|
|
95
|
+
expect(seen.opts?.stdio?.[0]).toBe("ignore");
|
|
96
|
+
expect(existsSync(result.logPath)).toBe(true);
|
|
97
|
+
expect(result.logPath).toBe(join(gateRunsDir(common), `${record.runId}.log`));
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("pointer lines name the run, the log, and the status command", async () => {
|
|
101
|
+
const common = await tempCommon();
|
|
102
|
+
const lines = detachPointerLines(
|
|
103
|
+
{ pid: 1, runId: "gr-x", logPath: join(gateRunsDir(common), "gr-x.log") },
|
|
104
|
+
common,
|
|
105
|
+
"pnpm exec werkstatt run gate.run.status",
|
|
106
|
+
);
|
|
107
|
+
expect(lines[0]).toContain("gr-x");
|
|
108
|
+
expect(lines[1]).toContain(".git/forge/gate-runs/gr-x.log");
|
|
109
|
+
expect(lines[2]).toContain("gate.run.status --id gr-x");
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("detached child adoption", () => {
|
|
114
|
+
it("AC-5: the child rewrites the parent's record — one record, child pid", async () => {
|
|
115
|
+
const common = await tempCommon();
|
|
116
|
+
// Parent writes the record before spawning.
|
|
117
|
+
const parent = await recordFixture(common);
|
|
118
|
+
const childPid = 515_151;
|
|
119
|
+
|
|
120
|
+
// Child side: WERKSTATT_GATE_RUN_ID set → startGateRun adopts the id.
|
|
121
|
+
const adopted = await startGateRun({
|
|
122
|
+
commonGitDir: common,
|
|
123
|
+
command: parent.command,
|
|
124
|
+
argv: parent.argv,
|
|
125
|
+
cwd: parent.cwd,
|
|
126
|
+
detached: true,
|
|
127
|
+
io,
|
|
128
|
+
runId: parent.runId,
|
|
129
|
+
});
|
|
130
|
+
adopted.pid = childPid;
|
|
131
|
+
await saveGateRun(common, adopted, io);
|
|
132
|
+
|
|
133
|
+
const runs = await listGateRuns(common, io);
|
|
134
|
+
expect(runs).toHaveLength(1);
|
|
135
|
+
expect(runs[0]?.runId).toBe(parent.runId);
|
|
136
|
+
|
|
137
|
+
const raw = JSON.parse(
|
|
138
|
+
await readFile(join(gateRunsDir(common), `${parent.runId}.json`), "utf8"),
|
|
139
|
+
) as GateRunRecord;
|
|
140
|
+
expect(raw.pid).toBe(childPid);
|
|
141
|
+
expect(raw.schema).toBe(GATE_RUN_SCHEMA);
|
|
142
|
+
expect(raw.status).toBe("running");
|
|
143
|
+
});
|
|
144
|
+
});
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>RFC-1568 gate-run record lifecycle tests (AC-2, AC-3): attached-mode
|
|
4
|
+
finish writes endedAt + exit code; a `running` record whose pid is dead
|
|
5
|
+
classifies `lost`; retention deletes beyond 50 per command but never
|
|
6
|
+
touches running records; corrupt records are skipped, not thrown.</purpose>
|
|
7
|
+
<non-goals>
|
|
8
|
+
<item>No real git repo — records live under a temp common dir.</item>
|
|
9
|
+
</non-goals>
|
|
10
|
+
</MODULE_CONTRACT>
|
|
11
|
+
<CHANGE_SUMMARY>
|
|
12
|
+
<item>RFC-1568: step 2 — record lifecycle tests</item>
|
|
13
|
+
<item>RFC-1568: durable gate-run records, --detach mechanics, and forge CLI wiring</item>
|
|
14
|
+
</CHANGE_SUMMARY>
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { spawnSync } from "node:child_process";
|
|
18
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
19
|
+
import { hostname, tmpdir } from "node:os";
|
|
20
|
+
import { join } from "node:path";
|
|
21
|
+
|
|
22
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
23
|
+
|
|
24
|
+
import { resolveIo } from "../../../src/utils/io.ts";
|
|
25
|
+
import {
|
|
26
|
+
GATE_RUN_RETENTION,
|
|
27
|
+
classifyGateRun,
|
|
28
|
+
finishGateRun,
|
|
29
|
+
gateRunsDir,
|
|
30
|
+
listGateRuns,
|
|
31
|
+
saveGateRun,
|
|
32
|
+
startGateRun,
|
|
33
|
+
} from "../record.ts";
|
|
34
|
+
import { GATE_RUN_SCHEMA, type GateRunRecord } from "../types.ts";
|
|
35
|
+
|
|
36
|
+
const dirs: string[] = [];
|
|
37
|
+
afterEach(async () => {
|
|
38
|
+
for (const dir of dirs.splice(0)) await rm(dir, { recursive: true, force: true });
|
|
39
|
+
}, 20_000);
|
|
40
|
+
|
|
41
|
+
async function tempCommon(): Promise<string> {
|
|
42
|
+
const dir = await mkdtemp(join(tmpdir(), "gate-run-"));
|
|
43
|
+
dirs.push(dir);
|
|
44
|
+
return dir;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const deadPid = (): number => {
|
|
48
|
+
const pid = spawnSync(process.execPath, ["-e", ""]).pid;
|
|
49
|
+
if (pid === undefined) throw new Error("spawn failed — cannot produce a dead pid");
|
|
50
|
+
return pid;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function fixtureRecord(over: Partial<GateRunRecord> = {}): GateRunRecord {
|
|
54
|
+
return {
|
|
55
|
+
schema: GATE_RUN_SCHEMA,
|
|
56
|
+
runId: over.runId ?? `gr-test-${Math.random().toString(36).slice(2, 8)}`,
|
|
57
|
+
command: "flow.integrate",
|
|
58
|
+
argv: [],
|
|
59
|
+
cwd: ".",
|
|
60
|
+
detached: false,
|
|
61
|
+
pid: process.pid,
|
|
62
|
+
host: hostname(),
|
|
63
|
+
startedAt: new Date().toISOString(),
|
|
64
|
+
status: "running",
|
|
65
|
+
...over,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const io = resolveIo(undefined);
|
|
70
|
+
|
|
71
|
+
describe("startGateRun / finishGateRun", () => {
|
|
72
|
+
it("AC-2: finish writes endedAt, exit code, and succeeded status", async () => {
|
|
73
|
+
const common = await tempCommon();
|
|
74
|
+
const record = await startGateRun({
|
|
75
|
+
commonGitDir: common,
|
|
76
|
+
command: "flow.integrate",
|
|
77
|
+
argv: ["--dry-run"],
|
|
78
|
+
cwd: ".",
|
|
79
|
+
detached: false,
|
|
80
|
+
io,
|
|
81
|
+
});
|
|
82
|
+
expect(record.status).toBe("running");
|
|
83
|
+
expect(record.pid).toBe(process.pid);
|
|
84
|
+
|
|
85
|
+
await finishGateRun(common, record, { exitCode: 0, summary: "[flow.integrate] done" }, io);
|
|
86
|
+
|
|
87
|
+
const raw = JSON.parse(
|
|
88
|
+
await readFile(join(gateRunsDir(common), `${record.runId}.json`), "utf8"),
|
|
89
|
+
) as GateRunRecord;
|
|
90
|
+
expect(raw.status).toBe("succeeded");
|
|
91
|
+
expect(raw.exitCode).toBe(0);
|
|
92
|
+
expect(raw.endedAt).toBeDefined();
|
|
93
|
+
expect(raw.summary).toBe("[flow.integrate] done");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("AC-2: a non-zero exit records failed", async () => {
|
|
97
|
+
const common = await tempCommon();
|
|
98
|
+
const record = await startGateRun({
|
|
99
|
+
commonGitDir: common,
|
|
100
|
+
command: "ecosystem.commit",
|
|
101
|
+
argv: [],
|
|
102
|
+
cwd: ".",
|
|
103
|
+
detached: false,
|
|
104
|
+
io,
|
|
105
|
+
});
|
|
106
|
+
await finishGateRun(common, record, { exitCode: 1 }, io);
|
|
107
|
+
const [read] = await listGateRuns(common, io);
|
|
108
|
+
expect(read?.status).toBe("failed");
|
|
109
|
+
expect(read?.exitCode).toBe(1);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("classifyGateRun", () => {
|
|
114
|
+
it("AC-3: running record with a dead pid classifies lost", () => {
|
|
115
|
+
const record = fixtureRecord({ pid: deadPid(), host: hostname() });
|
|
116
|
+
expect(classifyGateRun(record)).toBe("lost");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("running record with a live pid stays running", () => {
|
|
120
|
+
const record = fixtureRecord({ pid: process.pid, host: hostname() });
|
|
121
|
+
expect(classifyGateRun(record)).toBe("running");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("terminal statuses pass through unchanged", () => {
|
|
125
|
+
expect(classifyGateRun(fixtureRecord({ status: "succeeded" }))).toBe("succeeded");
|
|
126
|
+
expect(classifyGateRun(fixtureRecord({ status: "failed" }))).toBe("failed");
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("listGateRuns", () => {
|
|
131
|
+
it("skips corrupt records and returns newest first", async () => {
|
|
132
|
+
const common = await tempCommon();
|
|
133
|
+
const older = fixtureRecord({ runId: "gr-older", startedAt: "2026-01-01T00:00:00.000Z" });
|
|
134
|
+
const newer = fixtureRecord({ runId: "gr-newer", startedAt: "2026-01-02T00:00:00.000Z" });
|
|
135
|
+
await saveGateRun(common, older, io);
|
|
136
|
+
await saveGateRun(common, newer, io);
|
|
137
|
+
await writeFile(join(gateRunsDir(common), "gr-corrupt.json"), "{not json");
|
|
138
|
+
|
|
139
|
+
const runs = await listGateRuns(common, io);
|
|
140
|
+
expect(runs.map((r) => r.runId)).toEqual(["gr-newer", "gr-older"]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("filters by command and limit", async () => {
|
|
144
|
+
const common = await tempCommon();
|
|
145
|
+
await saveGateRun(common, fixtureRecord({ runId: "gr-a", command: "flow.integrate" }), io);
|
|
146
|
+
await saveGateRun(common, fixtureRecord({ runId: "gr-b", command: "ecosystem.commit" }), io);
|
|
147
|
+
|
|
148
|
+
const filtered = await listGateRuns(common, io, { command: "flow.integrate" });
|
|
149
|
+
expect(filtered).toHaveLength(1);
|
|
150
|
+
expect(filtered[0]?.runId).toBe("gr-a");
|
|
151
|
+
|
|
152
|
+
const limited = await listGateRuns(common, io, { limit: 1 });
|
|
153
|
+
expect(limited).toHaveLength(1);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("retention", () => {
|
|
158
|
+
it("deletes finished records beyond the per-command cap but never running ones", async () => {
|
|
159
|
+
const common = await tempCommon();
|
|
160
|
+
const dir = gateRunsDir(common);
|
|
161
|
+
// 51 finished + 1 running → after the next start, finished are trimmed
|
|
162
|
+
// to 50 and the running record survives.
|
|
163
|
+
for (let i = 0; i < GATE_RUN_RETENTION + 1; i++) {
|
|
164
|
+
await saveGateRun(
|
|
165
|
+
common,
|
|
166
|
+
fixtureRecord({
|
|
167
|
+
runId: `gr-old-${String(i).padStart(3, "0")}`,
|
|
168
|
+
status: "succeeded",
|
|
169
|
+
startedAt: new Date(2026, 0, i + 1).toISOString(),
|
|
170
|
+
}),
|
|
171
|
+
io,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
await saveGateRun(
|
|
175
|
+
common,
|
|
176
|
+
fixtureRecord({
|
|
177
|
+
runId: "gr-alive",
|
|
178
|
+
status: "running",
|
|
179
|
+
startedAt: "2026-06-01T00:00:00.000Z",
|
|
180
|
+
}),
|
|
181
|
+
io,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
await startGateRun({
|
|
185
|
+
commonGitDir: common,
|
|
186
|
+
command: "flow.integrate",
|
|
187
|
+
argv: [],
|
|
188
|
+
cwd: ".",
|
|
189
|
+
detached: false,
|
|
190
|
+
io,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const runs = await listGateRuns(common, io, { command: "flow.integrate" });
|
|
194
|
+
expect(runs.some((r) => r.runId === "gr-alive")).toBe(true);
|
|
195
|
+
expect(runs.filter((r) => r.status === "succeeded")).toHaveLength(GATE_RUN_RETENTION);
|
|
196
|
+
// oldest finished record was pruned, its file gone from disk.
|
|
197
|
+
const files = await readFile(join(dir, "gr-alive.json"), "utf8").then(() => true);
|
|
198
|
+
expect(files).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>RFC-1568 — durable gate-run record contract: one JSON record per
|
|
4
|
+
detachable-command execution under `<git common dir>/forge/gate-runs/`,
|
|
5
|
+
carrying lifecycle (running → succeeded|failed, `lost` derived), pid liveness
|
|
6
|
+
inputs, and pointers at the run's log/gate-report artifacts.</purpose>
|
|
7
|
+
<non-goals>
|
|
8
|
+
<item>No liveness rules of its own — classify delegates to os/core/lock-holder.</item>
|
|
9
|
+
<item>No remote/queued execution — records are written by the runner itself.</item>
|
|
10
|
+
</non-goals>
|
|
11
|
+
</MODULE_CONTRACT>
|
|
12
|
+
<CHANGE_SUMMARY>
|
|
13
|
+
<item>RFC-1568: step 1 — gate-run record contract + refusal ids</item>
|
|
14
|
+
<item>RFC-1568: durable gate-run records, --detach mechanics, and forge CLI wiring</item>
|
|
15
|
+
</CHANGE_SUMMARY>
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type GateRunStatus = "running" | "succeeded" | "failed" | "lost";
|
|
19
|
+
|
|
20
|
+
export const GATE_RUN_SCHEMA = "forge:gate-run/v1" as const;
|
|
21
|
+
|
|
22
|
+
export interface GateRunRecord {
|
|
23
|
+
schema: typeof GATE_RUN_SCHEMA;
|
|
24
|
+
/** gr-<UTC yyyymmddThhmmssZ>-<command>-<4 hex> */
|
|
25
|
+
runId: string;
|
|
26
|
+
command: string;
|
|
27
|
+
/** argv as invoked, without --detach. */
|
|
28
|
+
argv: string[];
|
|
29
|
+
/** repo-relative to the common-dir parent when possible. */
|
|
30
|
+
cwd: string;
|
|
31
|
+
detached: boolean;
|
|
32
|
+
pid: number;
|
|
33
|
+
host: string;
|
|
34
|
+
bootId?: string;
|
|
35
|
+
startedAt: string;
|
|
36
|
+
endedAt?: string;
|
|
37
|
+
exitCode?: number;
|
|
38
|
+
/** "running" on disk until the end write; "lost" is derived, never written. */
|
|
39
|
+
status: GateRunStatus;
|
|
40
|
+
/** the command result summary line. */
|
|
41
|
+
summary?: string;
|
|
42
|
+
/** detached runs: <common>/forge/gate-runs/<runId>.log */
|
|
43
|
+
logPath?: string;
|
|
44
|
+
/** RFC-1532 gate report, when the command produced one. */
|
|
45
|
+
reportPath?: string;
|
|
46
|
+
/** mission.validate */
|
|
47
|
+
missionId?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Minimal spawn surface detach.ts needs — injectable for tests. */
|
|
51
|
+
export interface SpawnLikeChild {
|
|
52
|
+
pid?: number;
|
|
53
|
+
unref(): void;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SpawnLike {
|
|
57
|
+
(
|
|
58
|
+
file: string,
|
|
59
|
+
args: string[],
|
|
60
|
+
opts: {
|
|
61
|
+
detached?: boolean;
|
|
62
|
+
stdio?: unknown[];
|
|
63
|
+
env?: Record<string, string | undefined>;
|
|
64
|
+
},
|
|
65
|
+
): SpawnLikeChild;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** GATE-RUN-01..03 — command-level refusal ids declared on gate.run.status. */
|
|
69
|
+
export const GATE_RUN_RULES = [
|
|
70
|
+
{ id: "GATE-RUN-01", severity: "error" as const },
|
|
71
|
+
{ id: "GATE-RUN-02", severity: "error" as const },
|
|
72
|
+
{ id: "GATE-RUN-03", severity: "error" as const },
|
|
73
|
+
];
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>RFC-1569 — the single evidence-annotation grammar shared by
|
|
4
|
+
rfc.implement.stamp (resolution) and rfc.validate V-53 (shape lint). Pure —
|
|
5
|
+
no IO; the stamp decides resolution, V-53 only asks "is this a form the
|
|
6
|
+
stamp can resolve".</purpose>
|
|
7
|
+
<non-goals>
|
|
8
|
+
<item>No file existence or probe-envelope checks — resolution stays the
|
|
9
|
+
stamp's job (RFC-1322).</item>
|
|
10
|
+
</non-goals>
|
|
11
|
+
</MODULE_CONTRACT>
|
|
12
|
+
<CHANGE_SUMMARY>
|
|
13
|
+
<item>RFC-1569: step 1 — extract the annotation grammar into a pure parser</item>
|
|
14
|
+
<item>RFC-1569: lint rfc evidence annotations and transient probe targets</item>
|
|
15
|
+
</CHANGE_SUMMARY>
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type EvidenceAnnotation =
|
|
19
|
+
| { kind: "probe"; criterion: string }
|
|
20
|
+
| { kind: "test" | "file"; path: string; lines?: string }
|
|
21
|
+
| { kind: "path-line"; path: string; lines: string }
|
|
22
|
+
| { kind: "invalid"; raw: string; reason: string };
|
|
23
|
+
|
|
24
|
+
const PROBE_RE = /^probe:\s*([A-Za-z][\w.:-]*)$/;
|
|
25
|
+
const TEST_FILE_RE = /^(test|file):\s*(\S+?)(?::(\d+(?:-\d+)?))?$/;
|
|
26
|
+
const PATH_LINE_RE = /^([^\s:]+):(\d+(?:-\d+)?)$/;
|
|
27
|
+
|
|
28
|
+
export const EVIDENCE_FORMS = "probe:AC-N | test:<path> | file:<path> | <path>:<line>";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Parse the `(evidence: …)` payload. Whitespace after the prefix colon is
|
|
32
|
+
* tolerated; paths carry no whitespace. The probe arm accepts the full
|
|
33
|
+
* criterion-id alphabet (`AC-N`, `design:<heading>`) — whether the named
|
|
34
|
+
* criterion exists and binds a passing probe is the caller's arbitration,
|
|
35
|
+
* not the regex's.
|
|
36
|
+
*/
|
|
37
|
+
export function parseEvidenceAnnotation(raw: string): EvidenceAnnotation {
|
|
38
|
+
const annotation = raw.trim();
|
|
39
|
+
if (annotation === "") return { kind: "invalid", raw, reason: "empty annotation" };
|
|
40
|
+
|
|
41
|
+
const probe = PROBE_RE.exec(annotation);
|
|
42
|
+
if (probe) return { kind: "probe", criterion: probe[1]! };
|
|
43
|
+
|
|
44
|
+
const testFile = TEST_FILE_RE.exec(annotation);
|
|
45
|
+
if (testFile) {
|
|
46
|
+
return {
|
|
47
|
+
kind: testFile[1] === "test" ? "test" : "file",
|
|
48
|
+
path: testFile[2]!,
|
|
49
|
+
...(testFile[3] !== undefined ? { lines: testFile[3] } : {}),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const pathLine = PATH_LINE_RE.exec(annotation);
|
|
54
|
+
if (pathLine) return { kind: "path-line", path: pathLine[1]!, lines: pathLine[2]! };
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
kind: "invalid",
|
|
58
|
+
raw,
|
|
59
|
+
reason: `not a resolvable form (expected ${EVIDENCE_FORMS})`,
|
|
60
|
+
};
|
|
61
|
+
}
|