handoff-check 0.0.0-stage → 0.2.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/AGENTS.md +39 -0
- package/CHANGELOG.md +21 -0
- package/CONTRIBUTING.md +41 -0
- package/LICENSE +21 -0
- package/README.md +255 -2
- package/SECURITY.md +13 -0
- package/dist/cli.js +808 -0
- package/docs/agent-integration.md +86 -0
- package/docs/architecture.md +18 -0
- package/docs/decisions/0001-constraint-lifecycle.md +51 -0
- package/docs/decisions/0002-content-aware-handoffs.md +82 -0
- package/docs/development.md +33 -0
- package/docs/releasing.md +53 -0
- package/docs/roadmap.md +47 -0
- package/examples/changed-rule/constraints.json +12 -0
- package/examples/changed-rule/handoff.json +13 -0
- package/examples/changed-rule/original-constraints.json +12 -0
- package/examples/stale-handoff/constraints.json +18 -0
- package/examples/stale-handoff/corrected-handoff.json +5 -0
- package/examples/stale-handoff/handoff.json +5 -0
- package/package.json +58 -4
- package/schemas/config.v1.schema.json +16 -0
- package/schemas/constraints.v1.schema.json +47 -0
- package/schemas/constraints.v2.schema.json +75 -0
- package/schemas/handoff.v1.schema.json +19 -0
- package/schemas/handoff.v2.schema.json +50 -0
- package/skills/handoff-check/SKILL.md +70 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,808 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli.ts
|
|
4
|
+
import { open as open2 } from "node:fs/promises";
|
|
5
|
+
import { resolve as resolve3 } from "node:path";
|
|
6
|
+
import { parseArgs } from "node:util";
|
|
7
|
+
// package.json
|
|
8
|
+
var package_default = {
|
|
9
|
+
name: "handoff-check",
|
|
10
|
+
version: "0.2.0",
|
|
11
|
+
description: "A deterministic, local CLI that verifies active constraints before a coding agent resumes work.",
|
|
12
|
+
type: "module",
|
|
13
|
+
packageManager: "bun@1.2.10",
|
|
14
|
+
license: "MIT",
|
|
15
|
+
homepage: "https://github.com/Larusajs/Handoff-Check#readme",
|
|
16
|
+
bugs: {
|
|
17
|
+
url: "https://github.com/Larusajs/Handoff-Check/issues"
|
|
18
|
+
},
|
|
19
|
+
keywords: [
|
|
20
|
+
"cli",
|
|
21
|
+
"handoff",
|
|
22
|
+
"constraints",
|
|
23
|
+
"coding-agents",
|
|
24
|
+
"codex",
|
|
25
|
+
"claude-code"
|
|
26
|
+
],
|
|
27
|
+
repository: {
|
|
28
|
+
type: "git",
|
|
29
|
+
url: "git+https://github.com/Larusajs/Handoff-Check.git"
|
|
30
|
+
},
|
|
31
|
+
bin: {
|
|
32
|
+
"handoff-check": "dist/cli.js"
|
|
33
|
+
},
|
|
34
|
+
files: [
|
|
35
|
+
"dist",
|
|
36
|
+
"schemas",
|
|
37
|
+
"examples",
|
|
38
|
+
"docs",
|
|
39
|
+
"skills",
|
|
40
|
+
"AGENTS.md",
|
|
41
|
+
"CONTRIBUTING.md",
|
|
42
|
+
"CHANGELOG.md",
|
|
43
|
+
"SECURITY.md",
|
|
44
|
+
"README.md",
|
|
45
|
+
"LICENSE"
|
|
46
|
+
],
|
|
47
|
+
engines: {
|
|
48
|
+
node: ">=22"
|
|
49
|
+
},
|
|
50
|
+
scripts: {
|
|
51
|
+
dev: "bun src/cli.ts",
|
|
52
|
+
test: "bun run build && bun test",
|
|
53
|
+
typecheck: "tsc --noEmit",
|
|
54
|
+
lint: "biome check .",
|
|
55
|
+
format: "biome format --write .",
|
|
56
|
+
build: "bun build src/cli.ts --target node --outdir dist",
|
|
57
|
+
prepack: "bun run build",
|
|
58
|
+
check: "bun run lint && bun run typecheck && bun run test"
|
|
59
|
+
},
|
|
60
|
+
devDependencies: {
|
|
61
|
+
"@biomejs/biome": "2.6.8",
|
|
62
|
+
"@types/bun": "1.4.5",
|
|
63
|
+
ajv: "8.20.0",
|
|
64
|
+
"ajv-formats": "3.0.1",
|
|
65
|
+
typescript: "7.0.2"
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// src/config.ts
|
|
70
|
+
import { readFile } from "node:fs/promises";
|
|
71
|
+
import { dirname, resolve } from "node:path";
|
|
72
|
+
|
|
73
|
+
// src/validation.ts
|
|
74
|
+
class InputError extends Error {
|
|
75
|
+
name = "InputError";
|
|
76
|
+
}
|
|
77
|
+
function fail(message) {
|
|
78
|
+
throw new InputError(message);
|
|
79
|
+
}
|
|
80
|
+
function object(value, path) {
|
|
81
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
82
|
+
fail(`${path}: expected an object`);
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
function keys(value, allowed, path) {
|
|
87
|
+
for (const key of Object.keys(value).sort()) {
|
|
88
|
+
if (!allowed.includes(key))
|
|
89
|
+
fail(`${path}: unknown field '${key}'`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function string(value, path) {
|
|
93
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
94
|
+
fail(`${path}: expected a non-empty string`);
|
|
95
|
+
}
|
|
96
|
+
return value;
|
|
97
|
+
}
|
|
98
|
+
function identifier(value, path) {
|
|
99
|
+
const result = string(value, path);
|
|
100
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._-]*(?![\s\S])/.test(result)) {
|
|
101
|
+
fail(`${path}: use letters, digits, dots, underscores, or hyphens`);
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
function timestamp(value, path) {
|
|
106
|
+
const result = string(value, path);
|
|
107
|
+
const match = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,3}))?Z$/.exec(result);
|
|
108
|
+
if (!match)
|
|
109
|
+
fail(`${path}: expected a UTC timestamp ending in Z`);
|
|
110
|
+
const millis = Date.parse(result);
|
|
111
|
+
const canonical = `${match[1]}.${(match[2] ?? "").padEnd(3, "0")}Z`;
|
|
112
|
+
if (!Number.isFinite(millis) || new Date(millis).toISOString() !== canonical) {
|
|
113
|
+
fail(`${path}: invalid calendar date or time`);
|
|
114
|
+
}
|
|
115
|
+
return millis;
|
|
116
|
+
}
|
|
117
|
+
function version(value, path) {
|
|
118
|
+
if (value.schemaVersion !== 1 && value.schemaVersion !== 2)
|
|
119
|
+
fail(`${path}.schemaVersion: expected 1 or 2`);
|
|
120
|
+
}
|
|
121
|
+
function revision(value, path) {
|
|
122
|
+
if (!Number.isSafeInteger(value) || value < 1)
|
|
123
|
+
fail(`${path}: expected a positive safe integer`);
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
function unique(values, path) {
|
|
127
|
+
const seen = new Set;
|
|
128
|
+
for (const value of values) {
|
|
129
|
+
if (seen.has(value))
|
|
130
|
+
fail(`${path}: duplicate ID '${value}'`);
|
|
131
|
+
seen.add(value);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function parseManifest(input) {
|
|
135
|
+
const root = object(input, "manifest");
|
|
136
|
+
keys(root, ["schemaVersion", "constraints"], "manifest");
|
|
137
|
+
version(root, "manifest");
|
|
138
|
+
if (!Array.isArray(root.constraints))
|
|
139
|
+
fail("manifest.constraints: expected an array");
|
|
140
|
+
const constraints = root.constraints.map((entry, index) => {
|
|
141
|
+
const path = `manifest.constraints[${index}]`;
|
|
142
|
+
const rule = object(entry, path);
|
|
143
|
+
keys(rule, [
|
|
144
|
+
"id",
|
|
145
|
+
"scope",
|
|
146
|
+
"description",
|
|
147
|
+
"source",
|
|
148
|
+
"expiresAt",
|
|
149
|
+
"revokedAt",
|
|
150
|
+
"replacedBy",
|
|
151
|
+
"supersededAt",
|
|
152
|
+
...root.schemaVersion === 2 ? ["revision"] : []
|
|
153
|
+
], path);
|
|
154
|
+
const parsed = {
|
|
155
|
+
id: identifier(rule.id, `${path}.id`),
|
|
156
|
+
scope: identifier(rule.scope, `${path}.scope`),
|
|
157
|
+
description: string(rule.description, `${path}.description`),
|
|
158
|
+
source: string(rule.source, `${path}.source`)
|
|
159
|
+
};
|
|
160
|
+
if (root.schemaVersion === 2)
|
|
161
|
+
parsed.revision = revision(rule.revision, `${path}.revision`);
|
|
162
|
+
for (const field of ["expiresAt", "revokedAt", "supersededAt"]) {
|
|
163
|
+
if (Object.hasOwn(rule, field)) {
|
|
164
|
+
timestamp(rule[field], `${path}.${field}`);
|
|
165
|
+
parsed[field] = rule[field];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (Object.hasOwn(rule, "replacedBy")) {
|
|
169
|
+
parsed.replacedBy = identifier(rule.replacedBy, `${path}.replacedBy`);
|
|
170
|
+
}
|
|
171
|
+
if (parsed.replacedBy === undefined !== (parsed.supersededAt === undefined)) {
|
|
172
|
+
fail(`${path}: replacedBy and supersededAt must be provided together`);
|
|
173
|
+
}
|
|
174
|
+
return parsed;
|
|
175
|
+
});
|
|
176
|
+
unique(constraints.map((rule) => rule.id), "manifest.constraints");
|
|
177
|
+
const byId = new Map(constraints.map((rule) => [rule.id, rule]));
|
|
178
|
+
for (const rule of constraints) {
|
|
179
|
+
if (rule.replacedBy === undefined)
|
|
180
|
+
continue;
|
|
181
|
+
const replacement = byId.get(rule.replacedBy);
|
|
182
|
+
if (!replacement)
|
|
183
|
+
fail(`constraint '${rule.id}': replacement '${rule.replacedBy}' does not exist`);
|
|
184
|
+
if (replacement.scope !== rule.scope)
|
|
185
|
+
fail(`constraint '${rule.id}': replacement must have the same scope`);
|
|
186
|
+
}
|
|
187
|
+
const completed = new Set;
|
|
188
|
+
for (const rule of constraints) {
|
|
189
|
+
const path = new Set;
|
|
190
|
+
let current = rule;
|
|
191
|
+
while (current && !completed.has(current.id)) {
|
|
192
|
+
if (path.has(current.id))
|
|
193
|
+
fail(`constraint '${current.id}': replacement cycle`);
|
|
194
|
+
path.add(current.id);
|
|
195
|
+
current = current.replacedBy ? byId.get(current.replacedBy) : undefined;
|
|
196
|
+
}
|
|
197
|
+
for (const id of path)
|
|
198
|
+
completed.add(id);
|
|
199
|
+
}
|
|
200
|
+
return { schemaVersion: root.schemaVersion, constraints };
|
|
201
|
+
}
|
|
202
|
+
function parseHandoff(input) {
|
|
203
|
+
const root = object(input, "handoff");
|
|
204
|
+
version(root, "handoff");
|
|
205
|
+
keys(root, [
|
|
206
|
+
"schemaVersion",
|
|
207
|
+
"task",
|
|
208
|
+
"activeConstraints",
|
|
209
|
+
...root.schemaVersion === 2 ? ["scope", "capturedAt"] : []
|
|
210
|
+
], "handoff");
|
|
211
|
+
const task = string(root.task, "handoff.task");
|
|
212
|
+
if (!Array.isArray(root.activeConstraints))
|
|
213
|
+
fail("handoff.activeConstraints: expected an array");
|
|
214
|
+
if (root.schemaVersion === 2) {
|
|
215
|
+
const scope = identifier(root.scope, "handoff.scope");
|
|
216
|
+
timestamp(root.capturedAt, "handoff.capturedAt");
|
|
217
|
+
const activeConstraints2 = root.activeConstraints.map((entry, index) => {
|
|
218
|
+
const path = `handoff.activeConstraints[${index}]`;
|
|
219
|
+
const ref = object(entry, path);
|
|
220
|
+
keys(ref, ["id", "revision", "contentHash"], path);
|
|
221
|
+
const contentHash = string(ref.contentHash, `${path}.contentHash`);
|
|
222
|
+
if (!/^sha256:[a-f0-9]{64}(?![\s\S])/.test(contentHash))
|
|
223
|
+
fail(`${path}.contentHash: expected sha256 followed by 64 lowercase hex digits`);
|
|
224
|
+
return {
|
|
225
|
+
id: identifier(ref.id, `${path}.id`),
|
|
226
|
+
revision: revision(ref.revision, `${path}.revision`),
|
|
227
|
+
contentHash
|
|
228
|
+
};
|
|
229
|
+
});
|
|
230
|
+
unique(activeConstraints2.map((ref) => ref.id), "handoff.activeConstraints");
|
|
231
|
+
return {
|
|
232
|
+
schemaVersion: 2,
|
|
233
|
+
task,
|
|
234
|
+
scope,
|
|
235
|
+
capturedAt: root.capturedAt,
|
|
236
|
+
activeConstraints: activeConstraints2
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
const activeConstraints = root.activeConstraints.map((id, index) => identifier(id, `handoff.activeConstraints[${index}]`));
|
|
240
|
+
unique(activeConstraints, "handoff.activeConstraints");
|
|
241
|
+
return { schemaVersion: 1, task, activeConstraints };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// src/config.ts
|
|
245
|
+
async function readJson(path) {
|
|
246
|
+
let content;
|
|
247
|
+
try {
|
|
248
|
+
content = await readFile(path, "utf8");
|
|
249
|
+
} catch {
|
|
250
|
+
throw new InputError(`Cannot read input file '${path}'`);
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
return JSON.parse(content);
|
|
254
|
+
} catch {
|
|
255
|
+
throw new InputError(`Invalid JSON in '${path}'`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function parseConfig(input) {
|
|
259
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
260
|
+
throw new InputError("config: expected an object");
|
|
261
|
+
const value = input;
|
|
262
|
+
if (value.schemaVersion !== 1)
|
|
263
|
+
throw new InputError("config.schemaVersion: expected 1");
|
|
264
|
+
for (const key of Object.keys(value)) {
|
|
265
|
+
if (!["schemaVersion", "constraints", "handoff", "scope"].includes(key))
|
|
266
|
+
throw new InputError(`config: unknown field '${key}'`);
|
|
267
|
+
}
|
|
268
|
+
const config = { schemaVersion: 1 };
|
|
269
|
+
for (const key of ["constraints", "handoff"]) {
|
|
270
|
+
if (Object.hasOwn(value, key)) {
|
|
271
|
+
if (typeof value[key] !== "string" || !value[key].trim())
|
|
272
|
+
throw new InputError(`config.${key}: expected a non-empty path`);
|
|
273
|
+
config[key] = value[key];
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (Object.hasOwn(value, "scope"))
|
|
277
|
+
config.scope = identifier(value.scope, "config.scope");
|
|
278
|
+
return config;
|
|
279
|
+
}
|
|
280
|
+
async function loadConfig(cwd, explicit) {
|
|
281
|
+
const path = resolve(cwd, explicit ?? ".handoff-check.json");
|
|
282
|
+
let input;
|
|
283
|
+
try {
|
|
284
|
+
input = await readFile(path, "utf8");
|
|
285
|
+
} catch (error) {
|
|
286
|
+
if (explicit === undefined && error.code === "ENOENT")
|
|
287
|
+
return { config: { schemaVersion: 1 }, directory: cwd };
|
|
288
|
+
throw new InputError(`Cannot read config '${path}'`);
|
|
289
|
+
}
|
|
290
|
+
let parsed;
|
|
291
|
+
try {
|
|
292
|
+
parsed = JSON.parse(input);
|
|
293
|
+
} catch {
|
|
294
|
+
throw new InputError(`Invalid JSON in config '${path}'`);
|
|
295
|
+
}
|
|
296
|
+
return { config: parseConfig(parsed), directory: dirname(path) };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// src/initialize.ts
|
|
300
|
+
import { constants } from "node:fs";
|
|
301
|
+
import { lstat, mkdir, open, readFile as readFile2 } from "node:fs/promises";
|
|
302
|
+
import { dirname as dirname2, isAbsolute, relative, resolve as resolve2, sep } from "node:path";
|
|
303
|
+
var begin = "<!-- handoff-check:begin -->";
|
|
304
|
+
var end = "<!-- handoff-check:end -->";
|
|
305
|
+
var instruction = `${begin}
|
|
306
|
+
Before resuming a handoff or continuing after a context reset, use the handoff-check
|
|
307
|
+
skill and run the configured verifier from this repository root:
|
|
308
|
+
|
|
309
|
+
\`\`\`sh
|
|
310
|
+
npx --yes handoff-check@${package_default.version} verify --format json --require-content
|
|
311
|
+
\`\`\`
|
|
312
|
+
|
|
313
|
+
Use .handoff-check.json for paths and scope. If no scope is configured, request the
|
|
314
|
+
intended scope and pass --scope explicitly. Use the current clock for real work.
|
|
315
|
+
Resolve exit 1 mismatches or exit 2 input errors before resuming the affected task.
|
|
316
|
+
Do not change the manifest merely to obtain a pass. Empty rules verify no policy;
|
|
317
|
+
author and review real project constraints before relying on this check. A pass
|
|
318
|
+
checks declared IDs only and does not grant authorization or interpret summaries.
|
|
319
|
+
Capture a snapshot only at handoff creation or after explicitly reviewing changes;
|
|
320
|
+
never refresh the baseline before verifying it when resuming work.
|
|
321
|
+
${end}
|
|
322
|
+
`;
|
|
323
|
+
async function inspect(root, path) {
|
|
324
|
+
const rel = relative(root, path);
|
|
325
|
+
if (!rel || rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel))
|
|
326
|
+
throw new InputError(`Initialization target must be inside the project: '${path}'`);
|
|
327
|
+
let current = root;
|
|
328
|
+
const parts = rel.split(sep);
|
|
329
|
+
for (const [index, part] of parts.entries()) {
|
|
330
|
+
current = resolve2(current, part);
|
|
331
|
+
try {
|
|
332
|
+
const info = await lstat(current);
|
|
333
|
+
if (info.isSymbolicLink())
|
|
334
|
+
throw new InputError(`Refusing symlink initialization target: '${current}'`);
|
|
335
|
+
if (index < parts.length - 1 && !info.isDirectory())
|
|
336
|
+
throw new InputError(`Expected a directory: '${current}'`);
|
|
337
|
+
if (index === parts.length - 1 && !info.isFile())
|
|
338
|
+
throw new InputError(`Expected a regular file: '${current}'`);
|
|
339
|
+
} catch (error) {
|
|
340
|
+
if (error.code === "ENOENT")
|
|
341
|
+
return;
|
|
342
|
+
throw error;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
return readFile2(path, "utf8");
|
|
346
|
+
}
|
|
347
|
+
async function initialize(root, options) {
|
|
348
|
+
if (!["codex", "claude", "both", "none"].includes(options.agent))
|
|
349
|
+
throw new InputError("--agent: expected codex, claude, both, or none");
|
|
350
|
+
const plan = [];
|
|
351
|
+
const configPath = resolve2(root, ".handoff-check.json");
|
|
352
|
+
const previous = await inspect(root, configPath);
|
|
353
|
+
const config = previous === undefined ? { schemaVersion: 1 } : parseConfig(await readJson(configPath));
|
|
354
|
+
const scope = identifier(options.scope ?? config.scope, "init --scope (or config.scope)");
|
|
355
|
+
if (config.scope !== undefined && options.scope !== undefined && config.scope !== scope)
|
|
356
|
+
throw new InputError("Existing config scope differs; edit it explicitly before initializing another scope");
|
|
357
|
+
if (previous !== undefined && config.scope === undefined)
|
|
358
|
+
throw new InputError("Existing config has no scope; add it explicitly before init");
|
|
359
|
+
const json = (value) => `${JSON.stringify(value, null, 2)}
|
|
360
|
+
`;
|
|
361
|
+
plan.push({
|
|
362
|
+
path: configPath,
|
|
363
|
+
action: previous === undefined ? "create" : "preserve",
|
|
364
|
+
content: previous ?? json({
|
|
365
|
+
schemaVersion: 1,
|
|
366
|
+
constraints: "constraints.json",
|
|
367
|
+
handoff: "handoff.json",
|
|
368
|
+
scope
|
|
369
|
+
})
|
|
370
|
+
});
|
|
371
|
+
for (const [key, fallback, starter, validate] of [
|
|
372
|
+
[
|
|
373
|
+
"constraints",
|
|
374
|
+
"constraints.json",
|
|
375
|
+
{ schemaVersion: 2, constraints: [] },
|
|
376
|
+
parseManifest
|
|
377
|
+
],
|
|
378
|
+
[
|
|
379
|
+
"handoff",
|
|
380
|
+
"handoff.json",
|
|
381
|
+
{
|
|
382
|
+
schemaVersion: 2,
|
|
383
|
+
task: "Describe the task being handed off",
|
|
384
|
+
scope,
|
|
385
|
+
capturedAt: new Date().toISOString(),
|
|
386
|
+
activeConstraints: []
|
|
387
|
+
},
|
|
388
|
+
parseHandoff
|
|
389
|
+
]
|
|
390
|
+
]) {
|
|
391
|
+
const path = resolve2(root, config[key] ?? fallback);
|
|
392
|
+
const old = await inspect(root, path);
|
|
393
|
+
if (old !== undefined)
|
|
394
|
+
validate(await readJson(path));
|
|
395
|
+
plan.push({
|
|
396
|
+
path,
|
|
397
|
+
action: old === undefined ? "create" : "preserve",
|
|
398
|
+
content: old ?? json(starter)
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
const skill = options.agent === "none" ? "" : await readFile2(new URL("../skills/handoff-check/SKILL.md", import.meta.url), "utf8");
|
|
402
|
+
const selected = options.agent === "both" ? ["codex", "claude"] : options.agent === "none" ? [] : [options.agent];
|
|
403
|
+
for (const agent of selected) {
|
|
404
|
+
const skillPath = resolve2(root, agent === "codex" ? ".agents/skills/handoff-check/SKILL.md" : ".claude/skills/handoff-check/SKILL.md");
|
|
405
|
+
const oldSkill = await inspect(root, skillPath);
|
|
406
|
+
if (oldSkill !== undefined && oldSkill !== skill)
|
|
407
|
+
throw new InputError(`Existing skill differs; review it before replacing: '${skillPath}'`);
|
|
408
|
+
plan.push({
|
|
409
|
+
path: skillPath,
|
|
410
|
+
action: oldSkill === undefined ? "create" : "preserve",
|
|
411
|
+
content: skill
|
|
412
|
+
});
|
|
413
|
+
const path = resolve2(root, agent === "codex" ? "AGENTS.md" : "CLAUDE.md");
|
|
414
|
+
const before = await inspect(root, path);
|
|
415
|
+
if (before === undefined)
|
|
416
|
+
plan.push({ path, action: "create", content: instruction });
|
|
417
|
+
else {
|
|
418
|
+
const startCount = before.split(begin).length - 1;
|
|
419
|
+
const endCount = before.split(end).length - 1;
|
|
420
|
+
if (startCount || endCount) {
|
|
421
|
+
if (startCount !== 1 || endCount !== 1 || !before.includes(instruction))
|
|
422
|
+
throw new InputError(`Conflicting handoff-check instruction block in '${path}'`);
|
|
423
|
+
plan.push({ path, action: "preserve", content: before });
|
|
424
|
+
} else
|
|
425
|
+
plan.push({
|
|
426
|
+
path,
|
|
427
|
+
action: "append",
|
|
428
|
+
before,
|
|
429
|
+
content: `${before.endsWith(`
|
|
430
|
+
`) ? `
|
|
431
|
+
` : `
|
|
432
|
+
|
|
433
|
+
`}${instruction}`
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (options.ci) {
|
|
438
|
+
const path = resolve2(root, ".github/workflows/handoff-check.yml");
|
|
439
|
+
const content = `name: Handoff constraints
|
|
440
|
+
on: [push, pull_request]
|
|
441
|
+
permissions:
|
|
442
|
+
contents: read
|
|
443
|
+
jobs:
|
|
444
|
+
verify:
|
|
445
|
+
runs-on: ubuntu-latest
|
|
446
|
+
steps:
|
|
447
|
+
- uses: actions/checkout@v4
|
|
448
|
+
- uses: actions/setup-node@v4
|
|
449
|
+
with:
|
|
450
|
+
node-version: 22
|
|
451
|
+
- run: npx --yes handoff-check@${package_default.version} verify --format json --require-content
|
|
452
|
+
`;
|
|
453
|
+
const old = await inspect(root, path);
|
|
454
|
+
if (old !== undefined && old !== content)
|
|
455
|
+
throw new InputError(`Existing CI workflow differs: '${path}'`);
|
|
456
|
+
plan.push({
|
|
457
|
+
path,
|
|
458
|
+
action: old === undefined ? "create" : "preserve",
|
|
459
|
+
content
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
const pathKey = (path) => path.normalize("NFC").toLowerCase();
|
|
463
|
+
if (new Set(plan.map((file) => pathKey(file.path))).size !== plan.length)
|
|
464
|
+
throw new InputError("Initialization paths overlap; use separate config, manifest, handoff and integration files");
|
|
465
|
+
const written = [];
|
|
466
|
+
for (const file of plan) {
|
|
467
|
+
if (plan.some((other) => other !== file && pathKey(other.path).startsWith(`${pathKey(file.path)}${sep}`)))
|
|
468
|
+
throw new InputError("Initialization paths overlap as file and directory; choose separate paths");
|
|
469
|
+
}
|
|
470
|
+
if (!options.dryRun) {
|
|
471
|
+
for (const file of plan) {
|
|
472
|
+
if (file.action === "preserve")
|
|
473
|
+
continue;
|
|
474
|
+
try {
|
|
475
|
+
const current = await inspect(root, file.path);
|
|
476
|
+
if (file.action === "create" && current !== undefined || file.action === "append" && current !== file.before)
|
|
477
|
+
throw new InputError("Target changed after planning");
|
|
478
|
+
await mkdir(dirname2(file.path), { recursive: true });
|
|
479
|
+
await inspect(root, file.path);
|
|
480
|
+
const flags = file.action === "create" ? constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL : constants.O_WRONLY | constants.O_APPEND | (constants.O_NOFOLLOW ?? 0);
|
|
481
|
+
const handle = await open(file.path, flags, 420);
|
|
482
|
+
try {
|
|
483
|
+
await handle.writeFile(file.content, "utf8");
|
|
484
|
+
} finally {
|
|
485
|
+
await handle.close();
|
|
486
|
+
}
|
|
487
|
+
written.push(relative(root, file.path));
|
|
488
|
+
} catch (error) {
|
|
489
|
+
const message = error instanceof Error ? error.message : "write failed";
|
|
490
|
+
throw new InputError(`Initialization stopped at '${relative(root, file.path)}': ${message}. Written files: ${written.join(", ") || "none"}. Inspect these paths before retrying; filesystem failures are not transactional.`);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return {
|
|
495
|
+
schemaVersion: 1,
|
|
496
|
+
status: options.dryRun ? "preview" : "initialized",
|
|
497
|
+
scope,
|
|
498
|
+
files: plan.map((file) => ({
|
|
499
|
+
path: relative(root, file.path),
|
|
500
|
+
action: file.action,
|
|
501
|
+
...file.action === "preserve" ? {} : { content: file.content }
|
|
502
|
+
})),
|
|
503
|
+
notice: "Starter files may contain zero rules. Add reviewed project constraints, then capture a v2 snapshot. Existing v1 handoffs are preserved but cannot satisfy --require-content until explicitly migrated. Never refresh a baseline before verifying it on resume."
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// src/content.ts
|
|
508
|
+
import { createHash } from "node:crypto";
|
|
509
|
+
function reference(rule) {
|
|
510
|
+
const instant = (value) => value === undefined ? null : new Date(value).toISOString();
|
|
511
|
+
const content = JSON.stringify({
|
|
512
|
+
id: rule.id,
|
|
513
|
+
scope: rule.scope,
|
|
514
|
+
description: rule.description,
|
|
515
|
+
source: rule.source,
|
|
516
|
+
expiresAt: instant(rule.expiresAt),
|
|
517
|
+
revokedAt: instant(rule.revokedAt),
|
|
518
|
+
replacedBy: rule.replacedBy ?? null,
|
|
519
|
+
supersededAt: instant(rule.supersededAt)
|
|
520
|
+
});
|
|
521
|
+
return {
|
|
522
|
+
id: rule.id,
|
|
523
|
+
revision: rule.revision ?? 1,
|
|
524
|
+
contentHash: `sha256:${createHash("sha256").update(content, "utf8").digest("hex")}`
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// src/verifier.ts
|
|
529
|
+
function compare(a, b) {
|
|
530
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
531
|
+
}
|
|
532
|
+
function inactive(rule, scope, time) {
|
|
533
|
+
if (rule.scope !== "global" && rule.scope !== scope)
|
|
534
|
+
return "OUT_OF_SCOPE";
|
|
535
|
+
if (rule.revokedAt !== undefined && time >= Date.parse(rule.revokedAt))
|
|
536
|
+
return "REVOKED";
|
|
537
|
+
if (rule.supersededAt !== undefined && time >= Date.parse(rule.supersededAt))
|
|
538
|
+
return "SUPERSEDED";
|
|
539
|
+
if (rule.expiresAt !== undefined && time >= Date.parse(rule.expiresAt))
|
|
540
|
+
return "EXPIRED";
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
function verify(manifest, handoff, scope, asOf) {
|
|
544
|
+
identifier(scope, "scope");
|
|
545
|
+
if (handoff.schemaVersion === 2 && handoff.scope !== scope)
|
|
546
|
+
throw new InputError(`Handoff scope '${handoff.scope}' does not match selected scope '${scope}'`);
|
|
547
|
+
const time = timestamp(asOf, "asOf");
|
|
548
|
+
const byId = new Map(manifest.constraints.map((rule) => [rule.id, rule]));
|
|
549
|
+
const expectedConstraints = manifest.constraints.filter((rule) => inactive(rule, scope, time) === undefined).map((rule) => rule.id).sort(compare);
|
|
550
|
+
const refs = new Map(handoff.schemaVersion === 2 ? handoff.activeConstraints.map((ref) => [ref.id, ref]) : []);
|
|
551
|
+
const reportedConstraints = (handoff.schemaVersion === 1 ? [...handoff.activeConstraints] : handoff.activeConstraints.map((ref) => ref.id)).sort(compare);
|
|
552
|
+
const reported = new Set(reportedConstraints);
|
|
553
|
+
const findings = [];
|
|
554
|
+
for (const id of expectedConstraints) {
|
|
555
|
+
if (!reported.has(id))
|
|
556
|
+
findings.push({
|
|
557
|
+
code: "MISSING",
|
|
558
|
+
id,
|
|
559
|
+
message: `Include active constraint: ${byId.get(id)?.description}`
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
for (const id of reportedConstraints) {
|
|
563
|
+
const rule = byId.get(id);
|
|
564
|
+
const code = rule ? inactive(rule, scope, time) : "UNKNOWN";
|
|
565
|
+
if (code) {
|
|
566
|
+
const detail = code === "SUPERSEDED" ? `; replaced by '${rule?.replacedBy}'` : "";
|
|
567
|
+
findings.push({
|
|
568
|
+
code,
|
|
569
|
+
id,
|
|
570
|
+
message: `Remove ${code.toLowerCase().replaceAll("_", "-")} constraint${detail}${rule ? `: ${rule.description}` : ""}`
|
|
571
|
+
});
|
|
572
|
+
} else if (rule && handoff.schemaVersion === 2) {
|
|
573
|
+
const current = reference(rule);
|
|
574
|
+
const previous = refs.get(id);
|
|
575
|
+
if (previous && (current.revision !== previous.revision || current.contentHash !== previous.contentHash)) {
|
|
576
|
+
findings.push({
|
|
577
|
+
code: "CHANGED",
|
|
578
|
+
id,
|
|
579
|
+
message: `Review changed constraint: ${rule.description}; handoff revision ${previous.revision}, current revision ${current.revision}${current.contentHash !== previous.contentHash ? "; content hash differs" : ""}`,
|
|
580
|
+
expected: {
|
|
581
|
+
revision: current.revision,
|
|
582
|
+
contentHash: current.contentHash
|
|
583
|
+
},
|
|
584
|
+
reported: {
|
|
585
|
+
revision: previous.revision,
|
|
586
|
+
contentHash: previous.contentHash
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
findings.sort((a, b) => compare(a.id, b.id) || compare(a.code, b.code));
|
|
593
|
+
return {
|
|
594
|
+
schemaVersion: handoff.schemaVersion,
|
|
595
|
+
...handoff.schemaVersion === 2 ? { tracking: "content" } : {},
|
|
596
|
+
status: findings.length ? "mismatch" : "verified",
|
|
597
|
+
scope,
|
|
598
|
+
asOf: new Date(time).toISOString(),
|
|
599
|
+
expectedConstraints,
|
|
600
|
+
reportedConstraints,
|
|
601
|
+
findings
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
function snapshot(manifest, task, scope, asOf) {
|
|
605
|
+
if (!task.trim())
|
|
606
|
+
throw new InputError("--task: expected a non-empty task");
|
|
607
|
+
const result = verify(manifest, { schemaVersion: 1, task, activeConstraints: [] }, scope, asOf);
|
|
608
|
+
const byId = new Map(manifest.constraints.map((rule) => [rule.id, rule]));
|
|
609
|
+
return {
|
|
610
|
+
schemaVersion: 2,
|
|
611
|
+
task,
|
|
612
|
+
scope,
|
|
613
|
+
capturedAt: result.asOf,
|
|
614
|
+
activeConstraints: result.expectedConstraints.map((id) => reference(byId.get(id)))
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// src/cli.ts
|
|
619
|
+
var help = `Handoff Check
|
|
620
|
+
|
|
621
|
+
Usage: handoff-check verify --scope <scope> [options]
|
|
622
|
+
handoff-check init --scope <scope> [--agent both] [--dry-run] [--ci]
|
|
623
|
+
handoff-check snapshot --task <task> [--output <new-file>] [options]
|
|
624
|
+
|
|
625
|
+
Options:
|
|
626
|
+
--constraints <path> Manifest file (default: constraints.json)
|
|
627
|
+
--handoff <path> Handoff file (default: handoff.json)
|
|
628
|
+
--scope <scope> Selected scope, including global constraints
|
|
629
|
+
--config <path> Project config (default: cwd/.handoff-check.json)
|
|
630
|
+
--as-of <UTC time> Fixed evaluation time (default: current time)
|
|
631
|
+
--require-content verify: require a v2 content-aware handoff
|
|
632
|
+
--task <task> snapshot: task description (required)
|
|
633
|
+
--output <path> snapshot: create a new baseline file, never overwrite
|
|
634
|
+
--format text|json Output format (default: text)
|
|
635
|
+
--agent <agent> init: codex, claude, both (default), or none
|
|
636
|
+
--dry-run init: preview files and instruction changes without writes
|
|
637
|
+
--ci init: also create a pinned GitHub Actions check
|
|
638
|
+
--version Show package version
|
|
639
|
+
--help Show this help
|
|
640
|
+
|
|
641
|
+
Exit codes: 0 verified, 1 rule mismatch, 2 invalid input
|
|
642
|
+
`;
|
|
643
|
+
function terminalText(value) {
|
|
644
|
+
return Array.from(value, (character) => {
|
|
645
|
+
const code = character.charCodeAt(0);
|
|
646
|
+
if (code < 32 || code >= 127 && code <= 159 || code === 8232 || code === 8233) {
|
|
647
|
+
return `\\u${code.toString(16).padStart(4, "0")}`;
|
|
648
|
+
}
|
|
649
|
+
return character;
|
|
650
|
+
}).join("");
|
|
651
|
+
}
|
|
652
|
+
async function main() {
|
|
653
|
+
let format = "text";
|
|
654
|
+
try {
|
|
655
|
+
const { values, positionals } = parseArgs({
|
|
656
|
+
args: process.argv.slice(2),
|
|
657
|
+
allowPositionals: true,
|
|
658
|
+
strict: true,
|
|
659
|
+
options: {
|
|
660
|
+
constraints: { type: "string" },
|
|
661
|
+
handoff: { type: "string" },
|
|
662
|
+
scope: { type: "string" },
|
|
663
|
+
config: { type: "string" },
|
|
664
|
+
"as-of": { type: "string" },
|
|
665
|
+
format: { type: "string", default: "text" },
|
|
666
|
+
help: { type: "boolean" },
|
|
667
|
+
version: { type: "boolean" },
|
|
668
|
+
agent: { type: "string" },
|
|
669
|
+
"dry-run": { type: "boolean" },
|
|
670
|
+
ci: { type: "boolean" },
|
|
671
|
+
"require-content": { type: "boolean" },
|
|
672
|
+
task: { type: "string" },
|
|
673
|
+
output: { type: "string" }
|
|
674
|
+
}
|
|
675
|
+
});
|
|
676
|
+
format = values.format;
|
|
677
|
+
if (format !== "text" && format !== "json")
|
|
678
|
+
throw new InputError("--format: expected text or json");
|
|
679
|
+
if (values.help) {
|
|
680
|
+
process.stdout.write(help);
|
|
681
|
+
return 0;
|
|
682
|
+
}
|
|
683
|
+
if (values.version) {
|
|
684
|
+
process.stdout.write(`${package_default.version}
|
|
685
|
+
`);
|
|
686
|
+
return 0;
|
|
687
|
+
}
|
|
688
|
+
if (positionals.length !== 1 || !["verify", "init", "snapshot"].includes(positionals[0] ?? ""))
|
|
689
|
+
throw new InputError("Expected command: verify, snapshot or init (use --help for usage)");
|
|
690
|
+
const cwd = process.cwd();
|
|
691
|
+
if (positionals[0] === "init") {
|
|
692
|
+
for (const key of [
|
|
693
|
+
"constraints",
|
|
694
|
+
"handoff",
|
|
695
|
+
"config",
|
|
696
|
+
"as-of",
|
|
697
|
+
"require-content",
|
|
698
|
+
"task",
|
|
699
|
+
"output"
|
|
700
|
+
])
|
|
701
|
+
if (values[key] !== undefined)
|
|
702
|
+
throw new InputError(`--${key} is not supported by init`);
|
|
703
|
+
const result2 = await initialize(cwd, {
|
|
704
|
+
...values.scope === undefined ? {} : { scope: values.scope },
|
|
705
|
+
agent: values.agent ?? "both",
|
|
706
|
+
dryRun: values["dry-run"] ?? false,
|
|
707
|
+
ci: values.ci ?? false
|
|
708
|
+
});
|
|
709
|
+
if (format === "json")
|
|
710
|
+
process.stdout.write(`${JSON.stringify(result2, null, 2)}
|
|
711
|
+
`);
|
|
712
|
+
else {
|
|
713
|
+
for (const file of result2.files)
|
|
714
|
+
process.stdout.write(`${values["dry-run"] ? "PLAN " : ""}${file.action.toUpperCase()} ${terminalText(file.path)}
|
|
715
|
+
`);
|
|
716
|
+
if (values["dry-run"]) {
|
|
717
|
+
for (const file of result2.files)
|
|
718
|
+
if (file.content !== undefined)
|
|
719
|
+
process.stdout.write(`
|
|
720
|
+
--- ${terminalText(file.path)} ---
|
|
721
|
+
${file.content.split(`
|
|
722
|
+
`).map(terminalText).join(`
|
|
723
|
+
`)}
|
|
724
|
+
`);
|
|
725
|
+
}
|
|
726
|
+
process.stdout.write(`${result2.status.toUpperCase()} | scope ${result2.scope}
|
|
727
|
+
${result2.notice}
|
|
728
|
+
`);
|
|
729
|
+
}
|
|
730
|
+
return 0;
|
|
731
|
+
}
|
|
732
|
+
if (values.agent !== undefined || values["dry-run"] !== undefined || values.ci !== undefined)
|
|
733
|
+
throw new InputError("--agent, --dry-run and --ci are only supported by init");
|
|
734
|
+
const { config, directory } = await loadConfig(cwd, values.config);
|
|
735
|
+
const scope = identifier(values.scope ?? config.scope, "--scope (or config.scope)");
|
|
736
|
+
const asOf = values["as-of"] ?? new Date().toISOString();
|
|
737
|
+
timestamp(asOf, "--as-of");
|
|
738
|
+
for (const key of ["constraints", "handoff", "config"])
|
|
739
|
+
if (values[key] !== undefined && !values[key].trim())
|
|
740
|
+
throw new InputError(`--${key}: expected a non-empty path`);
|
|
741
|
+
const manifest = parseManifest(await readJson(values.constraints !== undefined ? resolve3(cwd, values.constraints) : resolve3(directory, config.constraints ?? "constraints.json")));
|
|
742
|
+
if (positionals[0] === "snapshot") {
|
|
743
|
+
if (values.handoff !== undefined || values["require-content"] !== undefined)
|
|
744
|
+
throw new InputError("--handoff and --require-content are only supported by verify");
|
|
745
|
+
if (values.task === undefined)
|
|
746
|
+
throw new InputError("snapshot requires --task");
|
|
747
|
+
const captured = snapshot(manifest, values.task, scope, asOf);
|
|
748
|
+
const content = `${JSON.stringify(captured, null, 2)}
|
|
749
|
+
`;
|
|
750
|
+
if (values.output === undefined)
|
|
751
|
+
process.stdout.write(content);
|
|
752
|
+
else {
|
|
753
|
+
if (!values.output.trim())
|
|
754
|
+
throw new InputError("--output: expected a non-empty path");
|
|
755
|
+
const path = resolve3(cwd, values.output);
|
|
756
|
+
let handle;
|
|
757
|
+
try {
|
|
758
|
+
handle = await open2(path, "wx", 420);
|
|
759
|
+
} catch {
|
|
760
|
+
throw new InputError(`Cannot create snapshot '${path}'; choose a new file with an existing parent directory`);
|
|
761
|
+
}
|
|
762
|
+
try {
|
|
763
|
+
await handle.writeFile(content, "utf8");
|
|
764
|
+
} finally {
|
|
765
|
+
await handle.close();
|
|
766
|
+
}
|
|
767
|
+
const saved = {
|
|
768
|
+
schemaVersion: 1,
|
|
769
|
+
status: "captured",
|
|
770
|
+
path,
|
|
771
|
+
constraints: captured.activeConstraints.length
|
|
772
|
+
};
|
|
773
|
+
process.stdout.write(format === "json" ? `${JSON.stringify(saved)}
|
|
774
|
+
` : `CAPTURED ${terminalText(path)}
|
|
775
|
+
`);
|
|
776
|
+
}
|
|
777
|
+
return 0;
|
|
778
|
+
}
|
|
779
|
+
if (values.task !== undefined || values.output !== undefined)
|
|
780
|
+
throw new InputError("--task and --output are only supported by snapshot");
|
|
781
|
+
const handoff = parseHandoff(await readJson(values.handoff !== undefined ? resolve3(cwd, values.handoff) : resolve3(directory, config.handoff ?? "handoff.json")));
|
|
782
|
+
if (values["require-content"] && handoff.schemaVersion !== 2)
|
|
783
|
+
throw new InputError("Content tracking requires a schemaVersion 2 handoff; review current rules and capture a snapshot before resuming");
|
|
784
|
+
const result = verify(manifest, handoff, scope, asOf);
|
|
785
|
+
if (format === "json") {
|
|
786
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}
|
|
787
|
+
`);
|
|
788
|
+
} else {
|
|
789
|
+
for (const finding of result.findings) {
|
|
790
|
+
process.stdout.write(`${finding.code.padEnd(12)} ${finding.id} ${terminalText(finding.message)}
|
|
791
|
+
`);
|
|
792
|
+
}
|
|
793
|
+
process.stdout.write(`${result.status === "verified" ? "PASS" : "FAIL"} ${result.findings.length} findings | scope ${scope} | as of ${result.asOf}${handoff.schemaVersion === 1 ? " | IDs only; content changes not checked" : " | content tracked"}
|
|
794
|
+
`);
|
|
795
|
+
}
|
|
796
|
+
return result.status === "verified" ? 0 : 1;
|
|
797
|
+
} catch (error) {
|
|
798
|
+
const message = error instanceof Error ? error.message : "Unexpected failure";
|
|
799
|
+
if (format === "json")
|
|
800
|
+
process.stderr.write(`${JSON.stringify({ schemaVersion: 1, status: "invalid", error: message })}
|
|
801
|
+
`);
|
|
802
|
+
else
|
|
803
|
+
process.stderr.write(`INVALID ${terminalText(message)}
|
|
804
|
+
`);
|
|
805
|
+
return 2;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
process.exitCode = await main();
|