@warpgogol/forge 10.0.0 → 10.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 +12 -0
- package/dist/os/rfc/probe-batch.d.ts.map +1 -1
- package/dist/os/rfc/probe-batch.js +13 -6
- package/dist/os/rfc/probe-batch.js.map +1 -1
- package/dist/src/contract/core-nodes.d.ts +8 -0
- package/dist/src/contract/core-nodes.d.ts.map +1 -0
- package/dist/src/contract/core-nodes.js +101 -0
- package/dist/src/contract/core-nodes.js.map +1 -0
- package/dist/src/contract/evaluator.d.ts +48 -0
- package/dist/src/contract/evaluator.d.ts.map +1 -0
- package/dist/src/contract/evaluator.js +158 -0
- package/dist/src/contract/evaluator.js.map +1 -0
- package/dist/src/contract/evidence-store.d.ts +17 -0
- package/dist/src/contract/evidence-store.d.ts.map +1 -0
- package/dist/src/contract/evidence-store.js +113 -0
- package/dist/src/contract/evidence-store.js.map +1 -0
- package/dist/src/contract/executors/zero-deps.d.ts +3 -0
- package/dist/src/contract/executors/zero-deps.d.ts.map +1 -0
- package/dist/src/contract/executors/zero-deps.js +88 -0
- package/dist/src/contract/executors/zero-deps.js.map +1 -0
- package/dist/src/contract/fingerprint.d.ts +34 -0
- package/dist/src/contract/fingerprint.d.ts.map +1 -0
- package/dist/src/contract/fingerprint.js +153 -0
- package/dist/src/contract/fingerprint.js.map +1 -0
- package/dist/src/contract/graph.d.ts +23 -0
- package/dist/src/contract/graph.d.ts.map +1 -0
- package/dist/src/contract/graph.js +158 -0
- package/dist/src/contract/graph.js.map +1 -0
- package/dist/src/contract/migration-map.d.ts +51 -0
- package/dist/src/contract/migration-map.d.ts.map +1 -0
- package/dist/src/contract/migration-map.js +130 -0
- package/dist/src/contract/migration-map.js.map +1 -0
- package/dist/src/contract/types.d.ts +108 -0
- package/dist/src/contract/types.d.ts.map +1 -0
- package/dist/src/contract/types.js +23 -0
- package/dist/src/contract/types.js.map +1 -0
- package/dist/src/integration/drain.d.ts +31 -0
- package/dist/src/integration/drain.d.ts.map +1 -0
- package/dist/src/integration/drain.js +195 -0
- package/dist/src/integration/drain.js.map +1 -0
- package/dist/src/integration/enqueue.d.ts +30 -0
- package/dist/src/integration/enqueue.d.ts.map +1 -0
- package/dist/src/integration/enqueue.js +64 -0
- package/dist/src/integration/enqueue.js.map +1 -0
- package/dist/src/integration/gitops.d.ts +33 -0
- package/dist/src/integration/gitops.d.ts.map +1 -0
- package/dist/src/integration/gitops.js +128 -0
- package/dist/src/integration/gitops.js.map +1 -0
- package/dist/src/integration/lease.d.ts +16 -0
- package/dist/src/integration/lease.d.ts.map +1 -0
- package/dist/src/integration/lease.js +140 -0
- package/dist/src/integration/lease.js.map +1 -0
- package/dist/src/integration/materialize.d.ts +7 -0
- package/dist/src/integration/materialize.d.ts.map +1 -0
- package/dist/src/integration/materialize.js +34 -0
- package/dist/src/integration/materialize.js.map +1 -0
- package/dist/src/integration/store.d.ts +21 -0
- package/dist/src/integration/store.d.ts.map +1 -0
- package/dist/src/integration/store.js +131 -0
- package/dist/src/integration/store.js.map +1 -0
- package/dist/src/integration/types.d.ts +71 -0
- package/dist/src/integration/types.d.ts.map +1 -0
- package/dist/src/integration/types.js +18 -0
- package/dist/src/integration/types.js.map +1 -0
- package/dist/src/sdk/context.d.ts +13 -0
- package/dist/src/sdk/context.d.ts.map +1 -0
- package/dist/src/sdk/context.js +128 -0
- package/dist/src/sdk/context.js.map +1 -0
- package/dist/src/sdk/loader.d.ts +25 -0
- package/dist/src/sdk/loader.d.ts.map +1 -0
- package/dist/src/sdk/loader.js +155 -0
- package/dist/src/sdk/loader.js.map +1 -0
- package/dist/src/sdk/scan.d.ts +16 -0
- package/dist/src/sdk/scan.d.ts.map +1 -0
- package/dist/src/sdk/scan.js +126 -0
- package/dist/src/sdk/scan.js.map +1 -0
- package/dist/src/sdk/skills-gen.d.ts +27 -0
- package/dist/src/sdk/skills-gen.d.ts.map +1 -0
- package/dist/src/sdk/skills-gen.js +87 -0
- package/dist/src/sdk/skills-gen.js.map +1 -0
- package/dist/src/sdk/types.d.ts +124 -0
- package/dist/src/sdk/types.d.ts.map +1 -0
- package/dist/src/sdk/types.js +34 -0
- package/dist/src/sdk/types.js.map +1 -0
- package/os/rfc/probe-batch.ts +13 -6
- package/package.json +17 -1
- package/src/contract/core-nodes.ts +111 -0
- package/src/contract/evaluator.ts +207 -0
- package/src/contract/evidence-store.ts +119 -0
- package/src/contract/executors/zero-deps.ts +92 -0
- package/src/contract/fingerprint.ts +176 -0
- package/src/contract/graph.ts +181 -0
- package/src/contract/migration-map.ts +174 -0
- package/src/contract/types.ts +125 -0
- package/src/integration/drain.ts +271 -0
- package/src/integration/enqueue.ts +92 -0
- package/src/integration/gitops.ts +174 -0
- package/src/integration/lease.ts +144 -0
- package/src/integration/materialize.ts +40 -0
- package/src/integration/store.ts +153 -0
- package/src/integration/types.ts +79 -0
- package/src/sdk/context.ts +135 -0
- package/src/sdk/loader.ts +170 -0
- package/src/sdk/scan.ts +132 -0
- package/src/sdk/skills-gen.ts +104 -0
- package/src/sdk/types.ts +135 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>
|
|
4
|
+
Git operations substrate for the integration queue (Werkstatt RFC-1647) — pure
|
|
5
|
+
plumbing, no worktrees: commit-chain replay via merge-tree --write-tree with the
|
|
6
|
+
replayed commit's parent as merge-base (exact cherry-pick semantics), commit
|
|
7
|
+
materialization via commit-tree into the repo's own object store. Squash is
|
|
8
|
+
forbidden: the chain replays commit-by-commit preserving messages, authorship
|
|
9
|
+
and governance trailers. Objects never leave the main object store, so the
|
|
10
|
+
candidate survives independent of any scratch lifetime.
|
|
11
|
+
</purpose>
|
|
12
|
+
<non-goals>
|
|
13
|
+
<item>No evaluation or verdict logic — git mechanics only.</item>
|
|
14
|
+
</non-goals>
|
|
15
|
+
<CHANGE_SUMMARY>
|
|
16
|
+
<item>RFC-1647 (WP-4): plumbing-only — merge-tree/commit-tree, zero worktrees.</item>
|
|
17
|
+
</CHANGE_SUMMARY>
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
21
|
+
|
|
22
|
+
export interface GitExec {
|
|
23
|
+
(args: string[], cwd: string): string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const defaultGitExec: GitExec = (args, cwd) =>
|
|
27
|
+
execFileSync("git", args, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
|
|
28
|
+
|
|
29
|
+
export function resolveRef(git: GitExec, repoDir: string, ref: string): string {
|
|
30
|
+
return git(["rev-parse", ref], repoDir).trim();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function treeOfCommit(git: GitExec, repoDir: string, commitSha: string): string {
|
|
34
|
+
return git(["rev-parse", `${commitSha}^{tree}`], repoDir).trim();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function commitExists(git: GitExec, repoDir: string, sha: string): boolean {
|
|
38
|
+
try {
|
|
39
|
+
git(["cat-file", "-e", sha], repoDir);
|
|
40
|
+
return true;
|
|
41
|
+
} catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function commitTreeEquals(git: GitExec, repoDir: string, sha: string, treeDigest: string): boolean {
|
|
47
|
+
try {
|
|
48
|
+
return treeOfCommit(git, repoDir, sha) === treeDigest;
|
|
49
|
+
} catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ReplayResult {
|
|
55
|
+
ok: true;
|
|
56
|
+
candidateCommit: string;
|
|
57
|
+
candidateTree: string;
|
|
58
|
+
replayedCount: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface ReplayConflict {
|
|
62
|
+
ok: false;
|
|
63
|
+
conflictingPaths: string[];
|
|
64
|
+
atCommit: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface MergeTreeOutcome {
|
|
68
|
+
conflicted: boolean;
|
|
69
|
+
tree: string | null;
|
|
70
|
+
conflictedPaths: string[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* One merge-tree execution, exit status as the conflict signal. Output format:
|
|
75
|
+
* first line the tree oid; on conflict a section of conflicted-file records
|
|
76
|
+
* whose lines carry the path as the last whitespace-separated token.
|
|
77
|
+
*/
|
|
78
|
+
function runMergeTree(repoDir: string, base: string, ours: string, theirs: string): MergeTreeOutcome {
|
|
79
|
+
const res = spawnSync("git", ["merge-tree", "--write-tree", "--merge-base", base, ours, theirs], {
|
|
80
|
+
cwd: repoDir,
|
|
81
|
+
encoding: "utf8",
|
|
82
|
+
});
|
|
83
|
+
const out = res.stdout ?? "";
|
|
84
|
+
const lines = out.split("\n");
|
|
85
|
+
const tree = lines[0]?.trim() || null;
|
|
86
|
+
const conflicted = res.status !== 0;
|
|
87
|
+
const conflictedPaths = conflicted
|
|
88
|
+
? [
|
|
89
|
+
...new Set(
|
|
90
|
+
lines
|
|
91
|
+
.slice(1)
|
|
92
|
+
.map((l) => l.trim())
|
|
93
|
+
.filter((l) => /^\d+ \d+ [0-9a-f]{7,40} \d+\t?.+/i.test(l) || l.includes("\t"))
|
|
94
|
+
.map((l) => l.split(/\s+/).pop() ?? "")
|
|
95
|
+
.filter(Boolean),
|
|
96
|
+
),
|
|
97
|
+
]
|
|
98
|
+
: [];
|
|
99
|
+
return { conflicted, tree, conflictedPaths };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function commitMeta(git: GitExec, repoDir: string, commit: string): { message: string; authorName: string; authorEmail: string; authorDate: string } {
|
|
103
|
+
const sep = "@@META@@";
|
|
104
|
+
const message = git(["log", "-1", "--format=%B", commit], repoDir);
|
|
105
|
+
const meta = git(["log", "-1", `--format=${sep}%an${sep}%ae${sep}%aI`, commit], repoDir).trim().split(sep).filter(Boolean);
|
|
106
|
+
return {
|
|
107
|
+
message: message.trimEnd(),
|
|
108
|
+
authorName: meta[1] ?? "integration-queue",
|
|
109
|
+
authorEmail: meta[2] ?? "queue@localhost",
|
|
110
|
+
authorDate: meta[3] ?? new Date().toISOString(),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Rebase-materialize: replay the chain commit-by-commit onto the current head
|
|
116
|
+
* using merge-tree with the commit's parent as merge-base — exact cherry-pick
|
|
117
|
+
* tree semantics, no worktree, no checkout. Each step materializes its own
|
|
118
|
+
* commit object (never squashed). First conflict stops the replay and names
|
|
119
|
+
* the conflicting paths per-commit.
|
|
120
|
+
*/
|
|
121
|
+
export function replayChainOntoHead(
|
|
122
|
+
git: GitExec,
|
|
123
|
+
repoDir: string,
|
|
124
|
+
headSha: string,
|
|
125
|
+
commitChain: string[],
|
|
126
|
+
): ReplayResult | ReplayConflict {
|
|
127
|
+
let tip = headSha;
|
|
128
|
+
let replayed = 0;
|
|
129
|
+
|
|
130
|
+
for (const commit of commitChain) {
|
|
131
|
+
const parent = git(["rev-parse", `${commit}^`], repoDir).trim();
|
|
132
|
+
const outcome = runMergeTree(repoDir, parent, tip, commit);
|
|
133
|
+
if (outcome.conflicted || !outcome.tree) {
|
|
134
|
+
return {
|
|
135
|
+
ok: false,
|
|
136
|
+
conflictingPaths: outcome.conflictedPaths.length > 0 ? outcome.conflictedPaths : ["(conflict — paths unparsed)"],
|
|
137
|
+
atCommit: commit,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const meta = commitMeta(git, repoDir, commit);
|
|
142
|
+
const env = {
|
|
143
|
+
...process.env,
|
|
144
|
+
GIT_AUTHOR_NAME: meta.authorName,
|
|
145
|
+
GIT_AUTHOR_EMAIL: meta.authorEmail,
|
|
146
|
+
GIT_AUTHOR_DATE: meta.authorDate,
|
|
147
|
+
GIT_COMMITTER_NAME: meta.authorName,
|
|
148
|
+
GIT_COMMITTER_EMAIL: meta.authorEmail,
|
|
149
|
+
};
|
|
150
|
+
const newCommit = execFileSync("git", ["commit-tree", outcome.tree, "-p", tip, "-m", meta.message], {
|
|
151
|
+
cwd: repoDir,
|
|
152
|
+
encoding: "utf8",
|
|
153
|
+
env,
|
|
154
|
+
}).trim();
|
|
155
|
+
tip = newCommit;
|
|
156
|
+
replayed += 1;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const candidateTree = treeOfCommit(git, repoDir, tip);
|
|
160
|
+
return { ok: true, candidateCommit: tip, candidateTree, replayedCount: replayed };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Integrate the candidate into the governed ref — the single write point.
|
|
165
|
+
* update-ref is atomic; the candidate commits already live in this object store.
|
|
166
|
+
*/
|
|
167
|
+
export function integrateCandidate(
|
|
168
|
+
git: GitExec,
|
|
169
|
+
repoDir: string,
|
|
170
|
+
governedRef: string,
|
|
171
|
+
candidateCommit: string,
|
|
172
|
+
): void {
|
|
173
|
+
git(["update-ref", governedRef, candidateCommit], repoDir);
|
|
174
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>
|
|
4
|
+
Drain lease machinery (Werkstatt RFC-1647) — atomic acquisition, heartbeat, expiry
|
|
5
|
+
and reclaim, same-machine boot-id fencing inheriting the v9 machine-local trust
|
|
6
|
+
model. Lease is per-unit-processing, re-acquired with a fresh heartbeat at each
|
|
7
|
+
unit boundary; heartbeat cadence strictly less than half the TTL. Worst-case
|
|
8
|
+
duplicate work is one unit evaluation, never one corruption.
|
|
9
|
+
</purpose>
|
|
10
|
+
<non-goals>
|
|
11
|
+
<item>No multi-host fencing — v1 is single-machine scope by design.</item>
|
|
12
|
+
</non-goals>
|
|
13
|
+
<CHANGE_SUMMARY>
|
|
14
|
+
<item>RFC-1647 (WP-4): initial lease machinery.</item>
|
|
15
|
+
</CHANGE_SUMMARY>
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeSync } from "node:fs";
|
|
19
|
+
import { randomBytes } from "node:crypto";
|
|
20
|
+
import { join } from "node:path";
|
|
21
|
+
|
|
22
|
+
import type { DrainLease } from "./types.ts";
|
|
23
|
+
|
|
24
|
+
/** Lease TTL in ms — minutes-scale per RFC; heartbeat cadence must be < ttl/2. */
|
|
25
|
+
export const LEASE_TTL_MS = 5 * 60 * 1000;
|
|
26
|
+
export const HEARTBEAT_CADENCE_MS = 60 * 1000; // < LEASE_TTL_MS / 2 — fixed relationship
|
|
27
|
+
|
|
28
|
+
function hostBootId(): string {
|
|
29
|
+
try {
|
|
30
|
+
return readFileSync("/proc/sys/kernel/random/boot_id", "utf8").trim();
|
|
31
|
+
} catch {
|
|
32
|
+
// Non-Linux fallback: machine+process start is the best available fence in v1 scope.
|
|
33
|
+
return `unknown-${process.pid}`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface LeaseHandle {
|
|
38
|
+
lease: DrainLease;
|
|
39
|
+
heartbeat(): void;
|
|
40
|
+
release(): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function readLease(path: string): DrainLease | null {
|
|
44
|
+
try {
|
|
45
|
+
return JSON.parse(readFileSync(path, "utf8")) as DrainLease;
|
|
46
|
+
} catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isExpired(lease: DrainLease, now: number, ttlMs: number): boolean {
|
|
52
|
+
return new Date(lease.heartbeatAt).getTime() + ttlMs < now;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isLiveHolder(lease: DrainLease): boolean {
|
|
56
|
+
if (lease.hostBootId !== hostBootId()) return false; // different boot — holder is gone
|
|
57
|
+
try {
|
|
58
|
+
process.kill(lease.workerPid, 0);
|
|
59
|
+
return true;
|
|
60
|
+
} catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Acquire the drain lease atomically. Returns null when a live unexpired lease
|
|
67
|
+
* is held — the caller reports drain-in-progress and exits 0. Expired or dead
|
|
68
|
+
* leases are reclaimed; re-entry safety is the drain's idempotency contract.
|
|
69
|
+
*/
|
|
70
|
+
export function acquireLease(queueDir: string, ttlMs = LEASE_TTL_MS): LeaseHandle | null {
|
|
71
|
+
mkdirSync(queueDir, { recursive: true });
|
|
72
|
+
const leasePath = join(queueDir, "lease.json");
|
|
73
|
+
const now = Date.now();
|
|
74
|
+
|
|
75
|
+
const existing = readLease(leasePath);
|
|
76
|
+
if (existing && !isExpired(existing, now, ttlMs) && isLiveHolder(existing)) {
|
|
77
|
+
return null; // held by a live worker — never double-process
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Atomic take: write to a unique temp and rename — the rename is atomic on
|
|
81
|
+
// POSIX, and concurrent racers converge on one winner's file. Losers detect
|
|
82
|
+
// the loss by re-reading: the lease on disk names a different leaseId.
|
|
83
|
+
const lease: DrainLease = {
|
|
84
|
+
leaseId: randomBytes(8).toString("hex"),
|
|
85
|
+
workerPid: process.pid,
|
|
86
|
+
hostBootId: hostBootId(),
|
|
87
|
+
acquiredAt: new Date(now).toISOString(),
|
|
88
|
+
heartbeatAt: new Date(now).toISOString(),
|
|
89
|
+
};
|
|
90
|
+
const tmp = join(queueDir, `.lease-${lease.leaseId}`);
|
|
91
|
+
const fd = openSync(tmp, "w");
|
|
92
|
+
try {
|
|
93
|
+
writeSync(fd, JSON.stringify(lease, null, 2));
|
|
94
|
+
} finally {
|
|
95
|
+
closeSync(fd);
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
renameSync(tmp, leasePath);
|
|
99
|
+
} catch {
|
|
100
|
+
try {
|
|
101
|
+
unlinkSync(tmp);
|
|
102
|
+
} catch {
|
|
103
|
+
// best effort
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Verify the win — a concurrent racer's rename may have landed after ours.
|
|
107
|
+
const onDisk = readLease(leasePath);
|
|
108
|
+
if (!onDisk || onDisk.leaseId !== lease.leaseId) return null;
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
lease,
|
|
112
|
+
heartbeat() {
|
|
113
|
+
const current = readLease(leasePath);
|
|
114
|
+
if (!current || current.leaseId !== lease.leaseId) return; // lost — heartbeating another's lease is worse than expiry
|
|
115
|
+
const updated = { ...current, heartbeatAt: new Date().toISOString() };
|
|
116
|
+
const htmp = join(queueDir, `.lease-hb-${lease.leaseId}`);
|
|
117
|
+
const hfd = openSync(htmp, "w");
|
|
118
|
+
try {
|
|
119
|
+
writeSync(hfd, JSON.stringify(updated, null, 2));
|
|
120
|
+
} finally {
|
|
121
|
+
closeSync(hfd);
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
renameSync(htmp, leasePath);
|
|
125
|
+
} catch {
|
|
126
|
+
try {
|
|
127
|
+
unlinkSync(htmp);
|
|
128
|
+
} catch {
|
|
129
|
+
// best effort
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
release() {
|
|
134
|
+
const current = readLease(leasePath);
|
|
135
|
+
if (current?.leaseId === lease.leaseId) {
|
|
136
|
+
try {
|
|
137
|
+
unlinkSync(leasePath);
|
|
138
|
+
} catch {
|
|
139
|
+
// already gone
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>
|
|
4
|
+
Candidate tree materialization (Werkstatt RFC-1647) — the contract graph evaluates
|
|
5
|
+
the exact content that would integrate. The frozen candidate tree object is
|
|
6
|
+
expanded into an ephemeral filesystem root via git archive; callers own cleanup.
|
|
7
|
+
</purpose>
|
|
8
|
+
<non-goals>
|
|
9
|
+
<item>No caching of materializations — evaluation frequency is human-scale.</item>
|
|
10
|
+
</non-goals>
|
|
11
|
+
<CHANGE_SUMMARY>
|
|
12
|
+
<item>RFC-1647 (WP-4): initial materializer.</item>
|
|
13
|
+
</CHANGE_SUMMARY>
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { execFileSync } from "node:child_process";
|
|
17
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
18
|
+
import { tmpdir } from "node:os";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
|
|
21
|
+
export interface MaterializedTree {
|
|
22
|
+
root: string;
|
|
23
|
+
cleanup(): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Expand a git tree object into a fresh ephemeral directory. */
|
|
27
|
+
export function materializeTree(repoDir: string, treeDigest: string): MaterializedTree {
|
|
28
|
+
const root = mkdtempSync(join(tmpdir(), "forge-candidate-"));
|
|
29
|
+
// git archive writes a tar of the tree to stdout — expand in place.
|
|
30
|
+
execFileSync("sh", ["-c", `git archive "${treeDigest}" | tar -x -C "${root}"`], {
|
|
31
|
+
cwd: repoDir,
|
|
32
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
33
|
+
});
|
|
34
|
+
return {
|
|
35
|
+
root,
|
|
36
|
+
cleanup() {
|
|
37
|
+
rmSync(root, { recursive: true, force: true });
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>
|
|
4
|
+
Queue persistence (Werkstatt RFC-1647) — atomic enqueue via staging plus a single
|
|
5
|
+
journal append; append-only journal as the source of truth with bounded fold via
|
|
6
|
+
checkpoints; unit records immutable once terminal. A journal entry referencing a
|
|
7
|
+
missing or incomplete unit record is proof of crash-mid-enqueue and is discarded
|
|
8
|
+
as never-enqueued.
|
|
9
|
+
</purpose>
|
|
10
|
+
<non-goals>
|
|
11
|
+
<item>No lease or drain logic — persistence only.</item>
|
|
12
|
+
</non-goals>
|
|
13
|
+
<CHANGE_SUMMARY>
|
|
14
|
+
<item>RFC-1647 (WP-4): initial queue store.</item>
|
|
15
|
+
</CHANGE_SUMMARY>
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, unlinkSync, writeSync } from "node:fs";
|
|
19
|
+
import { randomBytes } from "node:crypto";
|
|
20
|
+
import { join } from "node:path";
|
|
21
|
+
|
|
22
|
+
import type { IntegrationUnit, QueueJournalEntry } from "./types.ts";
|
|
23
|
+
|
|
24
|
+
function atomicWrite(path: string, content: string): void {
|
|
25
|
+
mkdirSync(join(path, ".."), { recursive: true });
|
|
26
|
+
const tmp = join(join(path, ".."), `.tmp-${randomBytes(8).toString("hex")}`);
|
|
27
|
+
const fd = openSync(tmp, "w");
|
|
28
|
+
try {
|
|
29
|
+
writeSync(fd, content);
|
|
30
|
+
} finally {
|
|
31
|
+
closeSync(fd);
|
|
32
|
+
}
|
|
33
|
+
renameSync(tmp, path);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface QueueStore {
|
|
37
|
+
/**
|
|
38
|
+
* Atomic enqueue: the unit record is staged, then becomes visible through one
|
|
39
|
+
* atomic journal append. Either the unit exists complete in the queue or not
|
|
40
|
+
* at all — partial units are impossible by construction.
|
|
41
|
+
*/
|
|
42
|
+
enqueue(unit: IntegrationUnit): void;
|
|
43
|
+
appendJournal(entry: QueueJournalEntry): void;
|
|
44
|
+
readUnit(id: string): IntegrationUnit | null;
|
|
45
|
+
writeUnit(unit: IntegrationUnit): void;
|
|
46
|
+
/** All live units folded from journal + records — never-enqueued partial entries discarded. */
|
|
47
|
+
units(): IntegrationUnit[];
|
|
48
|
+
journal(): QueueJournalEntry[];
|
|
49
|
+
checkpoint(): void;
|
|
50
|
+
queueDir: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function createQueueStore(queueDir: string): QueueStore {
|
|
54
|
+
const unitsDir = join(queueDir, "units");
|
|
55
|
+
const journalPath = join(queueDir, "journal.log");
|
|
56
|
+
const checkpointPath = join(queueDir, "checkpoint.json");
|
|
57
|
+
mkdirSync(unitsDir, { recursive: true });
|
|
58
|
+
|
|
59
|
+
function unitPath(id: string): string {
|
|
60
|
+
return join(unitsDir, `${id}.json`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function appendJournal(entry: QueueJournalEntry): void {
|
|
64
|
+
mkdirSync(queueDir, { recursive: true });
|
|
65
|
+
const fd = openSync(journalPath, "a");
|
|
66
|
+
try {
|
|
67
|
+
writeSync(fd, JSON.stringify(entry) + "\n");
|
|
68
|
+
} finally {
|
|
69
|
+
closeSync(fd);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function readJournalRaw(): QueueJournalEntry[] {
|
|
74
|
+
if (!existsSync(journalPath)) return [];
|
|
75
|
+
return readFileSync(journalPath, "utf8")
|
|
76
|
+
.split("\n")
|
|
77
|
+
.filter((l) => l.trim().length > 0)
|
|
78
|
+
.map((l) => JSON.parse(l) as QueueJournalEntry);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function readUnit(id: string): IntegrationUnit | null {
|
|
82
|
+
try {
|
|
83
|
+
return JSON.parse(readFileSync(unitPath(id), "utf8")) as IntegrationUnit;
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function writeUnit(unit: IntegrationUnit): void {
|
|
90
|
+
atomicWrite(unitPath(unit.id), JSON.stringify(unit, null, 2));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function units(): IntegrationUnit[] {
|
|
94
|
+
// Fold the journal: a unit is live when its enqueued entry resolves to a
|
|
95
|
+
// complete record. Entries referencing missing/incomplete records are proof
|
|
96
|
+
// of crash-mid-enqueue — discarded as never-enqueued.
|
|
97
|
+
const entries = readJournalRaw();
|
|
98
|
+
const ids: string[] = [];
|
|
99
|
+
for (const e of entries) {
|
|
100
|
+
if (e.transition === "enqueued" && !ids.includes(e.unitId)) {
|
|
101
|
+
if (readUnit(e.unitId) != null) ids.push(e.unitId);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return ids.map((id) => readUnit(id)!).filter(Boolean);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
queueDir,
|
|
109
|
+
enqueue(unit) {
|
|
110
|
+
// Stage the complete record first, then the single atomic journal append
|
|
111
|
+
// that makes it visible. Reverse order would expose a referencing entry
|
|
112
|
+
// without a record; this order makes crash-mid-enqueue detectable and
|
|
113
|
+
// discardable.
|
|
114
|
+
writeUnit(unit);
|
|
115
|
+
appendJournal({
|
|
116
|
+
schema: "forge:queue-journal/v1",
|
|
117
|
+
unitId: unit.id,
|
|
118
|
+
transition: "enqueued",
|
|
119
|
+
at: new Date().toISOString(),
|
|
120
|
+
actor: `session:${unit.source.sessionId}`,
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
appendJournal,
|
|
124
|
+
readUnit,
|
|
125
|
+
writeUnit,
|
|
126
|
+
units,
|
|
127
|
+
journal: readJournalRaw,
|
|
128
|
+
checkpoint() {
|
|
129
|
+
// Fold-on-read stays bounded by checkpoint spacing, not journal age.
|
|
130
|
+
const snapshot = {
|
|
131
|
+
at: new Date().toISOString(),
|
|
132
|
+
units: units().map((u) => ({ id: u.id, state: u.state })),
|
|
133
|
+
};
|
|
134
|
+
atomicWrite(checkpointPath, JSON.stringify(snapshot, null, 2));
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Garbage-collect staging residue older than crash windows — safe because incomplete units are never visible. */
|
|
140
|
+
export function cleanStagingResidue(queueDir: string): number {
|
|
141
|
+
const staging = join(queueDir, "staging");
|
|
142
|
+
if (!existsSync(staging)) return 0;
|
|
143
|
+
let removed = 0;
|
|
144
|
+
for (const entry of readdirSync(staging)) {
|
|
145
|
+
try {
|
|
146
|
+
unlinkSync(join(staging, entry));
|
|
147
|
+
removed += 1;
|
|
148
|
+
} catch {
|
|
149
|
+
// concurrent writer — leave it
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return removed;
|
|
153
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>
|
|
4
|
+
Transactional integration queue types (Werkstatt RFC-1647) — immutable units,
|
|
5
|
+
the unit state machine, drain leases, and the append-only queue journal.
|
|
6
|
+
Mutable bookkeeping is separated from evidence per RFC-1646.
|
|
7
|
+
</purpose>
|
|
8
|
+
<non-goals>
|
|
9
|
+
<item>No drain/store logic — types only.</item>
|
|
10
|
+
</non-goals>
|
|
11
|
+
<CHANGE_SUMMARY>
|
|
12
|
+
<item>RFC-1647 (WP-4): initial queue contracts.</item>
|
|
13
|
+
</CHANGE_SUMMARY>
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** Immutable unit of integration — frozen at enqueue, never mutated. */
|
|
17
|
+
export interface IntegrationUnit {
|
|
18
|
+
schema: "forge:integration-unit/v1";
|
|
19
|
+
/** uuidv7-style time-ordered id. */
|
|
20
|
+
id: string;
|
|
21
|
+
/** Frozen session tree — git tree object id is the content digest. */
|
|
22
|
+
treeDigest: string;
|
|
23
|
+
/** Governed ref digest at enqueue time. */
|
|
24
|
+
baseRef: string;
|
|
25
|
+
source: {
|
|
26
|
+
sessionId: string;
|
|
27
|
+
/** Informational only. */
|
|
28
|
+
worktreePath: string;
|
|
29
|
+
/** RFC/ADR/friction anchors. */
|
|
30
|
+
governanceIds: string[];
|
|
31
|
+
};
|
|
32
|
+
/** Commit chain preserved for replay, oldest first — squash is forbidden. */
|
|
33
|
+
commitChain: string[];
|
|
34
|
+
enqueuedAt: string;
|
|
35
|
+
state: UnitState;
|
|
36
|
+
/** Crash-recovery counter, not retry-of-failure. */
|
|
37
|
+
attempts: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type UnitState =
|
|
41
|
+
| { status: "queued" }
|
|
42
|
+
| { status: "evaluating"; leaseId: string; since: string }
|
|
43
|
+
| { status: "integrated"; commitSha: string; evidenceKey: string }
|
|
44
|
+
| { status: "refused"; reason: RefusalReason; evidenceKey: string }
|
|
45
|
+
| { status: "conflicted"; conflictingPaths: string[] }
|
|
46
|
+
| { status: "cancelled"; by: string };
|
|
47
|
+
|
|
48
|
+
export interface RefusalReason {
|
|
49
|
+
/** The blocking contract node — one explanation path. */
|
|
50
|
+
nodeId: string;
|
|
51
|
+
summary: string;
|
|
52
|
+
violations: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface DrainLease {
|
|
56
|
+
leaseId: string;
|
|
57
|
+
workerPid: number;
|
|
58
|
+
/** Same-machine fencing, inheriting the v9 machine-local trust model. */
|
|
59
|
+
hostBootId: string;
|
|
60
|
+
acquiredAt: string;
|
|
61
|
+
/** Expiry = heartbeat + leaseTtl. */
|
|
62
|
+
heartbeatAt: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface QueueJournalEntry {
|
|
66
|
+
schema: "forge:queue-journal/v1";
|
|
67
|
+
unitId: string;
|
|
68
|
+
transition: "enqueued" | "evaluating" | "integrated" | "refused" | "conflicted" | "cancelled" | "requeued";
|
|
69
|
+
at: string;
|
|
70
|
+
/** drain:<leaseId> | session:<id> | operator */
|
|
71
|
+
actor: string;
|
|
72
|
+
detail?: Record<string, string>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type TerminalStatus = "integrated" | "refused" | "conflicted" | "cancelled";
|
|
76
|
+
|
|
77
|
+
export function isTerminal(state: UnitState): boolean {
|
|
78
|
+
return state.status === "integrated" || state.status === "refused" || state.status === "conflicted" || state.status === "cancelled";
|
|
79
|
+
}
|