@sockulags/agent-os 0.11.0 → 0.12.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.
Files changed (43) hide show
  1. package/README.md +155 -127
  2. package/cli/index.mjs +1022 -735
  3. package/cli/index.test.mjs +574 -300
  4. package/hooks/hooks.json +17 -0
  5. package/package.json +34 -32
  6. package/policy.md +47 -45
  7. package/skills/batch-work/SKILL.md +43 -43
  8. package/skills/batch-work/agents/openai.yaml +2 -2
  9. package/skills/batch-work/references/manifest.md +100 -100
  10. package/skills/batch-work/scripts/manifest-hash.mjs +263 -263
  11. package/skills/batch-work/scripts/test-manifest-hash.mjs +132 -132
  12. package/skills/chart-work/SKILL.md +37 -37
  13. package/skills/chart-work/references/elicitation.md +25 -25
  14. package/skills/chart-work/references/map.md +84 -84
  15. package/skills/chart-work/references/prototypes.md +21 -21
  16. package/skills/check-work/SKILL.md +61 -0
  17. package/skills/deliver-work/SKILL.md +12 -12
  18. package/skills/deliver-work/workflow.md +119 -113
  19. package/skills/diagnose-before-fix/SKILL.md +24 -24
  20. package/skills/dispatch-next/SKILL.md +28 -28
  21. package/skills/init-agent-os/SKILL.md +54 -39
  22. package/skills/init-agent-os/references/repository-init.md +74 -0
  23. package/skills/init-agent-os/scripts/policy-block.ps1 +87 -87
  24. package/skills/list-skills/SKILL.md +43 -42
  25. package/skills/list-skills/agents/openai.yaml +2 -2
  26. package/skills/plain-voice/SKILL.md +39 -0
  27. package/skills/plain-voice/references/tells.md +82 -0
  28. package/skills/proportional-testing/SKILL.md +30 -30
  29. package/skills/proportional-testing/references/strategy.md +75 -75
  30. package/skills/quality-ratchet/SKILL.md +50 -0
  31. package/skills/quality-ratchet/scripts/quality-delta.mjs +752 -0
  32. package/skills/quality-ratchet/scripts/quality-delta.test.mjs +514 -0
  33. package/skills/scope-guard/SKILL.md +21 -21
  34. package/skills/shape-work/SKILL.md +39 -39
  35. package/skills/shape-work/references/implementation-issues.md +69 -69
  36. package/skills/shape-work/references/mockups.md +23 -23
  37. package/skills/simplifier/SKILL.md +34 -34
  38. package/skills/simplifier/agents/openai.yaml +2 -2
  39. package/skills/simplifier-audit/SKILL.md +28 -28
  40. package/skills/simplifier-audit/agents/openai.yaml +2 -2
  41. package/skills/simplifier-review/SKILL.md +36 -29
  42. package/skills/verify-before-done/SKILL.md +29 -29
  43. package/skills/writing-skills/SKILL.md +68 -68
@@ -0,0 +1,514 @@
1
+ import assert from 'node:assert/strict'
2
+ import { execFileSync, spawnSync } from 'node:child_process'
3
+ import fs from 'node:fs'
4
+ import os from 'node:os'
5
+ import path from 'node:path'
6
+ import test, { after } from 'node:test'
7
+ import { fileURLToPath } from 'node:url'
8
+ import {
9
+ begin,
10
+ check,
11
+ clear,
12
+ hook
13
+ } from './quality-delta.mjs'
14
+
15
+ const temporaryRoots = []
16
+
17
+ function temporaryRoot() {
18
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-os-quality-'))
19
+ temporaryRoots.push(root)
20
+ return root
21
+ }
22
+
23
+ function git(root, ...args) {
24
+ return execFileSync('git', ['-C', root, ...args], { encoding: 'utf8' }).trim()
25
+ }
26
+
27
+ function initializedRepository() {
28
+ const root = temporaryRoot()
29
+ git(root, 'init', '-q')
30
+ git(root, 'config', 'user.email', 'quality@example.invalid')
31
+ git(root, 'config', 'user.name', 'Quality Test')
32
+ return root
33
+ }
34
+
35
+ function repository() {
36
+ const root = initializedRepository()
37
+ fs.mkdirSync(path.join(root, 'src'), { recursive: true })
38
+ fs.writeFileSync(path.join(root, 'src', 'legacy.js'), 'export function legacy() {\n return 1\n}\n')
39
+ fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({
40
+ name: 'quality-fixture',
41
+ version: '1.0.0',
42
+ dependencies: { before: '1.0.0' }
43
+ }, null, 2) + '\n')
44
+ git(root, 'add', '.')
45
+ git(root, 'commit', '-qm', 'fixture')
46
+ return root
47
+ }
48
+
49
+ function unavailableAnalyzers() {
50
+ return {
51
+ lizard: { status: 'unavailable', available: false, integrated: false, reason: 'test analyzer is absent' },
52
+ jscpd: { status: 'unavailable', available: false, integrated: false, reason: 'test analyzer is absent' }
53
+ }
54
+ }
55
+
56
+ function parseClaudeCodeStopResult(result) {
57
+ if (result.status === 0) return JSON.parse(result.stdout || '{}')
58
+ if (result.status === 2 && result.stderr.trim()) {
59
+ return { decision: 'block', reason: result.stderr.trim() }
60
+ }
61
+ throw new Error('Claude Code rejected the command hook result.')
62
+ }
63
+
64
+ function parseCodexStopResult(result) {
65
+ if (result.status === 0) return JSON.parse(result.stdout || '{}')
66
+ if (result.status === 2 && result.stderr.trim()) {
67
+ return { decision: 'block', reason: result.stderr.trim() }
68
+ }
69
+ throw new Error('Codex rejected the command hook result.')
70
+ }
71
+
72
+ after(() => {
73
+ for (const root of temporaryRoots) fs.rmSync(root, { recursive: true, force: true })
74
+ })
75
+
76
+ test('captures the exact dirty entry state and reports source/dependency deltas', () => {
77
+ const root = repository()
78
+ fs.writeFileSync(path.join(root, 'src', 'legacy.js'), 'export function legacy() {\n return 2\n}\n')
79
+ fs.writeFileSync(path.join(root, 'src', 'preexisting.js'), 'export const before = true\n')
80
+
81
+ const started = begin(root, { detect: unavailableAnalyzers })
82
+ const entry = JSON.parse(fs.readFileSync(started.statePath, 'utf8'))
83
+ assert.ok(entry.entry.files.some((file) => file.path === 'src/preexisting.js'))
84
+ assert.deepEqual(entry.entry.dirtyPaths, ['src/legacy.js', 'src/preexisting.js'])
85
+
86
+ fs.writeFileSync(path.join(root, 'src', 'legacy.js'), 'export function legacy() {\n return 3\n}\n')
87
+ fs.writeFileSync(path.join(root, 'src', 'new.js'), 'export const added = true\n')
88
+ fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({
89
+ name: 'quality-fixture',
90
+ version: '1.0.0',
91
+ dependencies: { before: '1.0.0', after: '2.0.0' }
92
+ }, null, 2) + '\n')
93
+
94
+ const evidence = check(root, { detect: unavailableAnalyzers })
95
+ assert.deepEqual(evidence.sourceFiles.added, ['src/new.js'])
96
+ assert.deepEqual(evidence.sourceFiles.changed, ['src/legacy.js'])
97
+ assert.deepEqual(evidence.sourceFiles.deleted, [])
98
+ assert.equal(evidence.sourceFiles.added.includes('src/preexisting.js'), false)
99
+ assert.equal(evidence.touchedLegacy.before.status, 'touched')
100
+ assert.deepEqual(evidence.touchedLegacy.before.files, ['src/legacy.js'])
101
+ assert.deepEqual(evidence.packageDependencies.added, [{
102
+ section: 'dependencies',
103
+ name: 'after',
104
+ version: '2.0.0'
105
+ }])
106
+ assert.deepEqual(evidence.packageDependencies.removed, [])
107
+ assert.equal(evidence.optionalAnalyzers.candidate.lizard.status, 'unavailable')
108
+ assert.match(evidence.optionalAnalyzers.candidate.lizard.reason, /absent/)
109
+ })
110
+
111
+ test('reports the first committed source delta from an unborn baseline', () => {
112
+ const root = initializedRepository()
113
+ const started = begin(root, { detect: unavailableAnalyzers })
114
+ fs.writeFileSync(path.join(root, 'src.js'), 'export const first = true\n')
115
+ git(root, 'add', 'src.js')
116
+ git(root, 'commit', '-qm', 'first commit')
117
+
118
+ const evidence = check(root, { detect: unavailableAnalyzers })
119
+ assert.deepEqual(evidence.sourceFiles.added, ['src.js'])
120
+ assert.deepEqual(evidence.sourceFiles.changed, [])
121
+ assert.deepEqual(evidence.sourceFiles.deleted, [])
122
+ assert.deepEqual(hook(root, { stop_hook_active: false }), {})
123
+ assert.equal(fs.existsSync(started.statePath), false)
124
+ })
125
+
126
+ test('classifies a tracked file-to-missing transition as deleted', () => {
127
+ const root = repository()
128
+ begin(root, { detect: unavailableAnalyzers })
129
+ fs.rmSync(path.join(root, 'src', 'legacy.js'))
130
+
131
+ const evidence = check(root, { detect: unavailableAnalyzers })
132
+ assert.deepEqual(evidence.sourceFiles.added, [])
133
+ assert.deepEqual(evidence.sourceFiles.changed, [])
134
+ assert.deepEqual(evidence.sourceFiles.deleted, ['src/legacy.js'])
135
+ assert.deepEqual(evidence.touchedLegacy.after, { status: 'none', files: [] })
136
+ })
137
+
138
+ test('keeps staged deletion and rename present at begin in the baseline', () => {
139
+ const scenarios = [
140
+ {
141
+ name: 'staged deletion',
142
+ prepare(root) {
143
+ git(root, 'rm', '-q', 'src/legacy.js')
144
+ }
145
+ },
146
+ {
147
+ name: 'staged rename',
148
+ prepare(root) {
149
+ git(root, 'mv', 'src/legacy.js', 'src/renamed.js')
150
+ }
151
+ }
152
+ ]
153
+
154
+ for (const scenario of scenarios) {
155
+ const root = repository()
156
+ scenario.prepare(root)
157
+ begin(root, { detect: unavailableAnalyzers })
158
+ const evidence = check(root, { detect: unavailableAnalyzers })
159
+ assert.deepEqual(evidence.sourceFiles, { added: [], changed: [], deleted: [] }, scenario.name)
160
+ }
161
+ })
162
+
163
+ test('begin refuses to overwrite an active baseline and check requires a baseline', () => {
164
+ const root = repository()
165
+ assert.throws(() => check(root), /No active quality ratchet baseline/)
166
+ const started = begin(root, { detect: unavailableAnalyzers })
167
+ assert.throws(() => begin(root, { detect: unavailableAnalyzers }), /already exists/)
168
+ clear(root)
169
+ assert.equal(fs.existsSync(started.statePath), false)
170
+ })
171
+
172
+ test('optional analyzer crashes remain explicit advisory unavailability', () => {
173
+ const root = repository()
174
+ const crashed = () => { throw new Error('analyzer process failed') }
175
+ begin(root, { detect: crashed })
176
+ const evidence = check(root, { detect: crashed })
177
+ assert.equal(evidence.optionalAnalyzers.candidate.jscpd.status, 'unavailable')
178
+ assert.match(evidence.optionalAnalyzers.candidate.jscpd.reason, /Capability detection failed/)
179
+ })
180
+
181
+ test('corrupt active state remains a lifecycle violation on reentry', () => {
182
+ const root = repository()
183
+ const started = begin(root, { detect: unavailableAnalyzers })
184
+ const state = JSON.parse(fs.readFileSync(started.statePath, 'utf8'))
185
+ state.entry.fingerprint = 'corrupt'
186
+ fs.writeFileSync(started.statePath, JSON.stringify(state))
187
+ assert.equal(hook(root, { stop_hook_active: false }).decision, 'block')
188
+ assert.equal(hook(root, { stop_hook_active: true }).decision, 'block')
189
+ })
190
+
191
+ test('rejects unbound or noncanonical dirty paths before Stop inspection', () => {
192
+ const scenarios = [
193
+ {
194
+ name: 'unbound full-tree list',
195
+ mutate(state) {
196
+ state.entry.dirtyPaths = state.entry.files.map((file) => file.path).sort()
197
+ }
198
+ },
199
+ {
200
+ name: 'duplicate path',
201
+ mutate(state) {
202
+ state.entry.dirtyPaths = ['src/legacy.js', 'src/legacy.js']
203
+ }
204
+ },
205
+ {
206
+ name: 'traversal path',
207
+ mutate(state) {
208
+ state.entry.dirtyPaths = ['../outside.js']
209
+ }
210
+ }
211
+ ]
212
+
213
+ for (const scenario of scenarios) {
214
+ const root = repository()
215
+ const started = begin(root, { detect: unavailableAnalyzers })
216
+ const state = JSON.parse(fs.readFileSync(started.statePath, 'utf8'))
217
+ scenario.mutate(state)
218
+ fs.writeFileSync(started.statePath, JSON.stringify(state))
219
+ const blocked = hook(root, { stop_hook_active: false })
220
+ assert.equal(blocked.decision, 'block', scenario.name)
221
+ assert.match(blocked.reason, /invalid/, scenario.name)
222
+ }
223
+ })
224
+
225
+ test('rejects corrupted entry heads before Stop inspection', () => {
226
+ for (const [name, head] of [
227
+ ['null head', null],
228
+ ['unbound plausible head', '1'.repeat(40)]
229
+ ]) {
230
+ const root = repository()
231
+ const started = begin(root, { detect: unavailableAnalyzers })
232
+ const state = JSON.parse(fs.readFileSync(started.statePath, 'utf8'))
233
+ state.entry.head = head
234
+ fs.writeFileSync(started.statePath, JSON.stringify(state))
235
+
236
+ const cleanPath = path.join(root, 'src', 'legacy.js')
237
+ const originalReadFileSync = fs.readFileSync
238
+ let inspected = false
239
+ fs.readFileSync = (target, ...args) => {
240
+ if (typeof target === 'string' && path.resolve(target) === cleanPath) {
241
+ inspected = true
242
+ throw new Error('clean file should not be inspected')
243
+ }
244
+ return originalReadFileSync.call(fs, target, ...args)
245
+ }
246
+ let blocked
247
+ try {
248
+ blocked = hook(root, { stop_hook_active: false })
249
+ } finally {
250
+ fs.readFileSync = originalReadFileSync
251
+ }
252
+ assert.equal(blocked.decision, 'block', name)
253
+ assert.match(blocked.reason, /invalid/, name)
254
+ assert.equal(inspected, false, name)
255
+ }
256
+ })
257
+
258
+ test('Stop hook remains fail-closed until a fresh check, then clears state', () => {
259
+ const root = repository()
260
+ const started = begin(root, { detect: unavailableAnalyzers })
261
+ const blocked = hook(root, { stop_hook_active: false })
262
+ assert.equal(blocked.decision, 'block')
263
+ assert.match(blocked.reason, /no check/)
264
+ const reentered = hook(root, { stop_hook_active: true })
265
+ assert.equal(reentered.decision, 'block')
266
+ assert.match(reentered.reason, /no check/)
267
+ assert.equal(fs.existsSync(started.statePath), true)
268
+
269
+ check(root, { detect: unavailableAnalyzers })
270
+ fs.writeFileSync(path.join(root, 'src', 'new.js'), 'export const changed_after_check = true\n')
271
+ assert.equal(hook(root, { stop_hook_active: false }).decision, 'block')
272
+ assert.equal(hook(root, { stop_hook_active: true }).decision, 'block')
273
+ assert.equal(fs.existsSync(started.statePath), true)
274
+
275
+ check(root, { detect: unavailableAnalyzers })
276
+ assert.deepEqual(hook(root, { stop_hook_active: true }), {})
277
+ assert.equal(fs.existsSync(started.statePath), false)
278
+ assert.deepEqual(hook(root, { stop_hook_active: false }), {})
279
+ })
280
+
281
+ test('Stop hook resolves current Codex and Claude payload session identities', () => {
282
+ const scenarios = [
283
+ {
284
+ name: 'Codex',
285
+ env: { CODEX_THREAD_ID: 'codex-payload-session' },
286
+ payload: { session_id: 'codex-payload-session', turn_id: 'codex-payload-turn' }
287
+ },
288
+ {
289
+ name: 'Claude',
290
+ env: { CLAUDE_CODE_SESSION_ID: 'claude-payload-session' },
291
+ payload: { session_id: 'claude-payload-session' }
292
+ }
293
+ ]
294
+
295
+ for (const scenario of scenarios) {
296
+ const root = repository()
297
+ const started = begin(root, { detect: unavailableAnalyzers, env: scenario.env })
298
+ const first = hook(root, { ...scenario.payload, stop_hook_active: false }, { env: {} })
299
+ assert.equal(first.decision, 'block', scenario.name)
300
+ assert.match(first.reason, /no check/, scenario.name)
301
+ const reentered = hook(root, { ...scenario.payload, stop_hook_active: true }, { env: {} })
302
+ assert.equal(reentered.decision, 'block', scenario.name)
303
+ assert.equal(fs.existsSync(started.statePath), true, scenario.name)
304
+
305
+ check(root, { detect: unavailableAnalyzers, env: scenario.env })
306
+ assert.deepEqual(
307
+ hook(root, { ...scenario.payload, stop_hook_active: true }, { env: {} }),
308
+ {},
309
+ scenario.name
310
+ )
311
+ assert.equal(fs.existsSync(started.statePath), false, scenario.name)
312
+ }
313
+ })
314
+
315
+ test('Codex takes precedence when both host environment IDs are present', () => {
316
+ const root = repository()
317
+ const env = {
318
+ CLAUDE_CODE_SESSION_ID: 'inherited-claude-session',
319
+ CODEX_THREAD_ID: 'active-codex-session'
320
+ }
321
+ const payload = {
322
+ session_id: 'active-codex-session',
323
+ turn_id: 'active-codex-turn'
324
+ }
325
+ const started = begin(root, { detect: unavailableAnalyzers, env })
326
+ const state = JSON.parse(fs.readFileSync(started.statePath, 'utf8'))
327
+ assert.equal(state.session.source, 'codex')
328
+ assert.match(path.basename(started.statePath), /\.codex-[0-9a-f]{32}$/)
329
+
330
+ const blocked = hook(root, { ...payload, stop_hook_active: false }, {
331
+ env: { CLAUDE_CODE_SESSION_ID: 'inherited-claude-session' }
332
+ })
333
+ assert.equal(blocked.decision, 'block')
334
+ assert.equal(fs.existsSync(started.statePath), true)
335
+
336
+ check(root, { detect: unavailableAnalyzers, env })
337
+ assert.deepEqual(hook(root, { ...payload, stop_hook_active: false }, { env: {} }), {})
338
+ assert.equal(fs.existsSync(started.statePath), false)
339
+ })
340
+
341
+ test('state files are isolated between linked worktrees', () => {
342
+ const root = repository()
343
+ const worktree = path.join(temporaryRoot(), 'linked')
344
+ git(root, 'worktree', 'add', '-q', '-b', 'quality-linked', worktree)
345
+ const first = begin(root, { detect: unavailableAnalyzers })
346
+ const second = begin(worktree, { detect: unavailableAnalyzers })
347
+ assert.notEqual(first.statePath, second.statePath)
348
+ assert.equal(fs.existsSync(first.statePath), true)
349
+ assert.equal(fs.existsSync(second.statePath), true)
350
+ clear(root)
351
+ clear(worktree)
352
+ git(root, 'worktree', 'remove', '-f', worktree)
353
+ })
354
+
355
+ test('state files are isolated between host sessions in the same worktree', () => {
356
+ const root = repository()
357
+ const claudeSession = { CLAUDE_CODE_SESSION_ID: 'claude-session' }
358
+ const codexSession = { CODEX_THREAD_ID: 'codex-session' }
359
+ const first = begin(root, { detect: unavailableAnalyzers, env: claudeSession })
360
+
361
+ assert.deepEqual(hook(root, { stop_hook_active: false }, { env: codexSession }), {})
362
+ assert.equal(fs.existsSync(first.statePath), true)
363
+ clear(root, { env: codexSession })
364
+ assert.equal(fs.existsSync(first.statePath), true)
365
+
366
+ const second = begin(root, { detect: unavailableAnalyzers, env: codexSession })
367
+ assert.notEqual(first.statePath, second.statePath)
368
+ assert.equal(hook(root, { stop_hook_active: false }, { env: claudeSession }).decision, 'block')
369
+ assert.equal(hook(root, { stop_hook_active: false }, { env: codexSession }).decision, 'block')
370
+
371
+ check(root, { detect: unavailableAnalyzers, env: claudeSession })
372
+ assert.deepEqual(hook(root, { stop_hook_active: false }, { env: claudeSession }), {})
373
+ assert.equal(fs.existsSync(first.statePath), false)
374
+ assert.equal(fs.existsSync(second.statePath), true)
375
+
376
+ clear(root, { env: codexSession })
377
+ assert.equal(fs.existsSync(second.statePath), false)
378
+ })
379
+
380
+ test('uses a deterministic standalone state and bounds untrusted session path input', () => {
381
+ const root = repository()
382
+ const standalone = {}
383
+ const first = begin(root, { detect: unavailableAnalyzers, env: standalone })
384
+ clear(root, { env: standalone })
385
+ const second = begin(root, { detect: unavailableAnalyzers, env: standalone })
386
+ assert.equal(first.statePath, second.statePath)
387
+ clear(root, { env: standalone })
388
+
389
+ const hostile = { CODEX_THREAD_ID: '../..\\session with spaces/and separators' }
390
+ const started = begin(root, { detect: unavailableAnalyzers, env: hostile })
391
+ assert.equal(path.dirname(started.statePath), path.resolve(root, '.git'))
392
+ assert.match(path.basename(started.statePath), /^\.agent-os-quality-ratchet\.json\.codex-[0-9a-f]{32}$/)
393
+ const state = JSON.parse(fs.readFileSync(started.statePath, 'utf8'))
394
+ assert.deepEqual(state.session, {
395
+ source: 'codex',
396
+ key: path.basename(started.statePath).slice('.agent-os-quality-ratchet.json.'.length)
397
+ })
398
+ state.session.key = 'codex-' + '0'.repeat(32)
399
+ fs.writeFileSync(started.statePath, JSON.stringify(state))
400
+ assert.equal(hook(root, { stop_hook_active: false }, { env: hostile }).decision, 'block')
401
+ })
402
+
403
+ test('hook is a cheap no-op without an active baseline', () => {
404
+ const root = repository()
405
+ assert.deepEqual(hook(root, { stop_hook_active: false }), {})
406
+ const result = spawnSync(process.execPath, [
407
+ path.join(path.dirname(fileURLToPath(import.meta.url)), 'quality-delta.mjs'),
408
+ 'hook',
409
+ '--root',
410
+ root
411
+ ], { encoding: 'utf8', input: '{}' })
412
+ assert.equal(result.status, 0)
413
+ assert.deepEqual(JSON.parse(result.stdout), {})
414
+ })
415
+
416
+ test('Stop hook does not read unchanged tracked files', () => {
417
+ const root = repository()
418
+ begin(root, { detect: unavailableAnalyzers })
419
+ check(root, { detect: unavailableAnalyzers })
420
+ const cleanPath = path.join(root, 'src', 'legacy.js')
421
+ const originalReadFileSync = fs.readFileSync
422
+ fs.readFileSync = (target, ...args) => {
423
+ if (typeof target === 'string' && path.resolve(target) === cleanPath) {
424
+ throw new Error('unchanged tracked file should not be read by Stop')
425
+ }
426
+ return originalReadFileSync.call(fs, target, ...args)
427
+ }
428
+ try {
429
+ assert.deepEqual(hook(root, { stop_hook_active: false }), {})
430
+ } finally {
431
+ fs.readFileSync = originalReadFileSync
432
+ }
433
+ })
434
+
435
+ test('Stop accepts and clears a fresh check after a committed source change', () => {
436
+ const root = repository()
437
+ const started = begin(root, { detect: unavailableAnalyzers })
438
+ fs.writeFileSync(path.join(root, 'src', 'legacy.js'), 'export function legacy() {\n return 2\n}\n')
439
+ git(root, 'add', 'src/legacy.js')
440
+ git(root, 'commit', '-qm', 'committed source change')
441
+
442
+ const evidence = check(root, { detect: unavailableAnalyzers })
443
+ assert.deepEqual(evidence.sourceFiles.changed, ['src/legacy.js'])
444
+ assert.deepEqual(hook(root, { stop_hook_active: false }), {})
445
+ assert.equal(fs.existsSync(started.statePath), false)
446
+ })
447
+
448
+ test('reports both sides of a committed source rename after begin', () => {
449
+ const root = repository()
450
+ const started = begin(root, { detect: unavailableAnalyzers })
451
+ git(root, 'mv', 'src/legacy.js', 'src/renamed.js')
452
+ git(root, 'commit', '-qm', 'committed source rename')
453
+
454
+ const evidence = check(root, { detect: unavailableAnalyzers })
455
+ assert.deepEqual(evidence.sourceFiles.added, ['src/renamed.js'])
456
+ assert.deepEqual(evidence.sourceFiles.changed, [])
457
+ assert.deepEqual(evidence.sourceFiles.deleted, ['src/legacy.js'])
458
+ assert.deepEqual(hook(root, { stop_hook_active: false }), {})
459
+ assert.equal(fs.existsSync(started.statePath), false)
460
+ })
461
+
462
+ test('Claude Code Stop contract accepts structured exit 0 and uses stderr on exit 2', () => {
463
+ const root = repository()
464
+ begin(root, { detect: unavailableAnalyzers })
465
+ const result = spawnSync(process.execPath, [
466
+ path.join(path.dirname(fileURLToPath(import.meta.url)), 'quality-delta.mjs'),
467
+ 'hook',
468
+ '--root',
469
+ root
470
+ ], { encoding: 'utf8', input: JSON.stringify({ stop_hook_active: false }) })
471
+ assert.equal(result.status, 0)
472
+ assert.equal(result.stderr, '')
473
+ const output = JSON.parse(result.stdout)
474
+ assert.equal(output.decision, 'block')
475
+ assert.deepEqual(parseClaudeCodeStopResult(result), output)
476
+
477
+ const exitTwo = {
478
+ status: 2,
479
+ stdout: JSON.stringify({ decision: 'block', reason: 'Claude Code ignores this stdout on exit 2' }),
480
+ stderr: 'Claude Code stop feedback'
481
+ }
482
+ assert.deepEqual(parseClaudeCodeStopResult(exitTwo), {
483
+ decision: 'block',
484
+ reason: exitTwo.stderr
485
+ })
486
+ assert.throws(() => parseClaudeCodeStopResult({ ...exitTwo, stderr: '' }), /Claude Code rejected/)
487
+ })
488
+
489
+ test('Codex Stop contract accepts structured exit 0 and uses stderr on exit 2', () => {
490
+ const root = repository()
491
+ begin(root, { detect: unavailableAnalyzers })
492
+ const result = spawnSync(process.execPath, [
493
+ path.join(path.dirname(fileURLToPath(import.meta.url)), 'quality-delta.mjs'),
494
+ 'hook',
495
+ '--root',
496
+ root
497
+ ], { encoding: 'utf8', input: JSON.stringify({ stop_hook_active: false }) })
498
+ assert.equal(result.status, 0)
499
+ assert.equal(result.stderr, '')
500
+ const output = JSON.parse(result.stdout)
501
+ assert.equal(output.decision, 'block')
502
+ assert.deepEqual(parseCodexStopResult(result), output)
503
+
504
+ const exitTwo = {
505
+ status: 2,
506
+ stdout: JSON.stringify({ decision: 'block', reason: 'Codex ignores this stdout on exit 2' }),
507
+ stderr: 'Codex stop feedback'
508
+ }
509
+ assert.deepEqual(parseCodexStopResult(exitTwo), {
510
+ decision: 'block',
511
+ reason: exitTwo.stderr
512
+ })
513
+ assert.throws(() => parseCodexStopResult({ ...exitTwo, stderr: '' }), /Codex rejected/)
514
+ })
@@ -1,21 +1,21 @@
1
- ---
2
- name: scope-guard
3
- description: Detects and contains task drift while implementation or review is already in progress. It activates when unrelated work, opportunistic cleanup, extra dependencies, or broadened requirements appear beyond the agreed request or spec, before adding them to the diff. It skips initial scoping and planning, root-cause diagnosis, and verification of work already inside scope.
4
- ---
5
-
6
- # Guard scope
7
-
8
- Keep the active change aligned with the developer's requested outcome.
9
-
10
- The direct request, accepted spec, and project policy define the boundary. When new work appears,
11
- classify it:
12
-
13
- - **Required:** the requested outcome cannot work or be verified without it. Include it and explain
14
- the dependency.
15
- - **Adjacent:** useful cleanup or hardening that is not needed now. Leave it out and mention it only
16
- when it is actionable.
17
- - **Conflicting:** it changes product behavior, architecture, dependencies, a public interface, or
18
- delivery risk. Ask the developer before crossing that boundary.
19
-
20
- Before delivery, inspect the diff for unrelated edits. Preserve pre-existing developer work; scope
21
- control never grants permission to revert it.
1
+ ---
2
+ name: scope-guard
3
+ description: Detects and contains task drift while implementation or review is already in progress. It activates when unrelated work, opportunistic cleanup, extra dependencies, or broadened requirements appear beyond the agreed request or spec, before adding them to the diff. It skips initial scoping and planning, root-cause diagnosis, and verification of work already inside scope.
4
+ ---
5
+
6
+ # Guard scope
7
+
8
+ Keep the active change aligned with the developer's requested outcome.
9
+
10
+ The direct request, accepted spec, and project policy define the boundary. When new work appears,
11
+ classify it:
12
+
13
+ - **Required:** the requested outcome cannot work or be verified without it. Include it and explain
14
+ the dependency.
15
+ - **Adjacent:** useful cleanup or hardening that is not needed now. Leave it out and mention it only
16
+ when it is actionable.
17
+ - **Conflicting:** it changes product behavior, architecture, dependencies, a public interface, or
18
+ delivery risk. Ask the developer before crossing that boundary.
19
+
20
+ Before delivery, inspect the diff for unrelated edits. Preserve pre-existing developer work; scope
21
+ control never grants permission to revert it.
@@ -1,39 +1,39 @@
1
- ---
2
- name: shape-work
3
- description: Shapes a bounded idea into implementation-ready issues backed by settled product decisions. User-invoked while material product choices remain. Not for small fixes or work whose outcome is already clear.
4
- disable-model-invocation: true
5
- ---
6
-
7
- # shape-work
8
-
9
- Produce the smallest set of implementation-ready issues that lets delivery proceed without inventing
10
- product intent.
11
-
12
- Read [references/implementation-issues.md](references/implementation-issues.md) before shaping.
13
-
14
- ## Loop
15
-
16
- Read the repository, project policy, and any linked chart-work decisions before asking questions.
17
- Resolve facts from those sources. For each remaining material choice, recommend an answer, explain
18
- the consequence, and ask only what the developer must decide.
19
-
20
- Use a diagram or mockup when it makes behavior, layout, states, or boundaries materially easier to
21
- judge. Frontend proposals follow [references/mockups.md](references/mockups.md). Do not create a
22
- visualization merely to satisfy the workflow.
23
-
24
- Deliver the coherent product shape: outcome, boundaries, acceptance examples, affected seams,
25
- ground truth, settled decisions, and open risks. Then create or reuse its implementation issues on
26
- the configured planning surface. With no configured tracker, use the local-file fallback in the
27
- reference. Link dependencies and expose the first claimable delivery frontier.
28
-
29
- One shape may produce one or several sequential or parallel issues. Multiple issues do not imply
30
- `batch-work`: each ready issue can be delivered with `deliver-work`, while `batch-work` remains an
31
- explicit developer choice for integrated parallel execution. Do not start either workflow unless
32
- the request includes implementation.
33
-
34
- When shaping started from a chart-work handoff, reconcile the issue links and readiness back into
35
- the origin handoff and map. A handoff alone is not delivery-ready. Shape-work is complete only when
36
- every selected branch points to implementation-ready issues or is explicitly parked or rejected.
37
-
38
- The invocation authorizes planning artifacts, not product code. A deferred product choice remains
39
- explicit; a reversible implementation choice does not need to become a question.
1
+ ---
2
+ name: shape-work
3
+ description: Shapes a bounded idea into implementation-ready issues backed by settled product decisions. User-invoked while material product choices remain. Not for small fixes or work whose outcome is already clear.
4
+ disable-model-invocation: true
5
+ ---
6
+
7
+ # shape-work
8
+
9
+ Produce the smallest set of implementation-ready issues that lets delivery proceed without inventing
10
+ product intent.
11
+
12
+ Read [references/implementation-issues.md](references/implementation-issues.md) before shaping.
13
+
14
+ ## Loop
15
+
16
+ Read the repository, project policy, and any linked chart-work decisions before asking questions.
17
+ Resolve facts from those sources. For each remaining material choice, recommend an answer, explain
18
+ the consequence, and ask only what the developer must decide.
19
+
20
+ Use a diagram or mockup when it makes behavior, layout, states, or boundaries materially easier to
21
+ judge. Frontend proposals follow [references/mockups.md](references/mockups.md). Do not create a
22
+ visualization merely to satisfy the workflow.
23
+
24
+ Deliver the coherent product shape: outcome, boundaries, acceptance examples, affected seams,
25
+ ground truth, settled decisions, and open risks. Then create or reuse its implementation issues on
26
+ the configured planning surface. With no configured tracker, use the local-file fallback in the
27
+ reference. Link dependencies and expose the first claimable delivery frontier.
28
+
29
+ One shape may produce one or several sequential or parallel issues. Multiple issues do not imply
30
+ `batch-work`: each ready issue can be delivered with `deliver-work`, while `batch-work` remains an
31
+ explicit developer choice for integrated parallel execution. Do not start either workflow unless
32
+ the request includes implementation.
33
+
34
+ When shaping started from a chart-work handoff, reconcile the issue links and readiness back into
35
+ the origin handoff and map. A handoff alone is not delivery-ready. Shape-work is complete only when
36
+ every selected branch points to implementation-ready issues or is explicitly parked or rejected.
37
+
38
+ The invocation authorizes planning artifacts, not product code. A deferred product choice remains
39
+ explicit; a reversible implementation choice does not need to become a question.