harnessed 4.32.18 → 4.32.20
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/bin/harnessed-inject-state.mjs +280 -258
- package/bin/harnessed-stop-hook.mjs +93 -82
- package/dist/cli.mjs +168 -6
- package/dist/cli.mjs.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/workflows/auto/SKILL.md +2 -2
- package/workflows/auto/SKILL.zh-Hans.md +2 -2
- package/workflows/plan/auto/SKILL.md +2 -2
- package/workflows/plan/auto/SKILL.zh-Hans.md +2 -2
- package/workflows/ship/auto/SKILL.md +2 -2
- package/workflows/ship/auto/SKILL.zh-Hans.md +2 -2
- package/workflows/task/auto/SKILL.md +2 -2
- package/workflows/task/auto/SKILL.zh-Hans.md +2 -2
- package/workflows/verify/auto/SKILL.md +2 -2
- package/workflows/verify/auto/SKILL.zh-Hans.md +2 -2
|
@@ -1,438 +1,460 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
// biome-ignore-all format: esbuild-generated (hook drift gate owns this file)
|
|
3
|
+
// biome-ignore-all lint: esbuild-generated
|
|
4
|
+
// biome-ignore-all assist/source/organizeImports: esbuild-generated
|
|
2
5
|
|
|
3
6
|
// src/checkpoint/injectStateMain.ts
|
|
4
7
|
import { createHash as createHash2 } from "node:crypto";
|
|
5
|
-
import { existsSync as existsSync3, readFileSync as readFileSync4, statSync } from
|
|
6
|
-
import { homedir } from
|
|
7
|
-
import { dirname, join as join4, resolve } from
|
|
8
|
+
import { existsSync as existsSync3, readFileSync as readFileSync4, statSync } from 'node:fs'
|
|
9
|
+
import { homedir } from 'node:os'
|
|
10
|
+
import { dirname, join as join4, resolve } from 'node:path'
|
|
8
11
|
|
|
9
12
|
// src/checkpoint/injectCache.ts
|
|
10
|
-
import { createHash } from
|
|
11
|
-
import { mkdirSync, readFileSync, writeFileSync } from
|
|
12
|
-
import { join } from
|
|
13
|
-
var DEFAULT_REFRESH_TURNS = 10
|
|
13
|
+
import { createHash } from 'node:crypto'
|
|
14
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
15
|
+
import { join } from 'node:path'
|
|
16
|
+
var DEFAULT_REFRESH_TURNS = 10
|
|
14
17
|
function decidePcEmission(cache, pcHash, refreshN, nowMs) {
|
|
15
18
|
if (cache && cache.pcHash === pcHash && cache.turns < refreshN) {
|
|
16
|
-
return { emit: false, next: { pcHash, ts: cache.ts, turns: cache.turns + 1 } }
|
|
19
|
+
return { emit: false, next: { pcHash, ts: cache.ts, turns: cache.turns + 1 } }
|
|
17
20
|
}
|
|
18
|
-
return { emit: true, next: { pcHash, ts: nowMs, turns: 0 } }
|
|
21
|
+
return { emit: true, next: { pcHash, ts: nowMs, turns: 0 } }
|
|
19
22
|
}
|
|
20
23
|
function parseRefreshTurns(raw) {
|
|
21
|
-
const n = Number.parseInt(raw ??
|
|
22
|
-
return Number.isInteger(n) && n > 0 ? n : DEFAULT_REFRESH_TURNS
|
|
24
|
+
const n = Number.parseInt(raw ?? '', 10)
|
|
25
|
+
return Number.isInteger(n) && n > 0 ? n : DEFAULT_REFRESH_TURNS
|
|
23
26
|
}
|
|
24
27
|
function injectCacheKey(repoKey2, sid) {
|
|
25
|
-
return createHash(
|
|
28
|
+
return createHash('sha256').update(`${repoKey2}::${sid}`).digest('hex').slice(0, 16)
|
|
26
29
|
}
|
|
27
30
|
function cacheFile(root, key) {
|
|
28
|
-
return join(root,
|
|
31
|
+
return join(root, 'inject-cache', `${key}.json`)
|
|
29
32
|
}
|
|
30
33
|
function readInjectCache(root, key) {
|
|
31
34
|
try {
|
|
32
|
-
const parsed = JSON.parse(readFileSync(cacheFile(root, key),
|
|
33
|
-
if (parsed && typeof parsed ===
|
|
34
|
-
const e = parsed
|
|
35
|
-
if (typeof e.pcHash ===
|
|
36
|
-
return { pcHash: e.pcHash, ts: e.ts, turns: e.turns }
|
|
35
|
+
const parsed = JSON.parse(readFileSync(cacheFile(root, key), 'utf8'))
|
|
36
|
+
if (parsed && typeof parsed === 'object') {
|
|
37
|
+
const e = parsed
|
|
38
|
+
if (typeof e.pcHash === 'string' && typeof e.ts === 'number' && typeof e.turns === 'number') {
|
|
39
|
+
return { pcHash: e.pcHash, ts: e.ts, turns: e.turns }
|
|
37
40
|
}
|
|
38
41
|
}
|
|
39
|
-
} catch {
|
|
40
|
-
|
|
41
|
-
return null;
|
|
42
|
+
} catch {}
|
|
43
|
+
return null
|
|
42
44
|
}
|
|
43
45
|
function writeInjectCache(root, key, entry) {
|
|
44
46
|
try {
|
|
45
|
-
mkdirSync(join(root,
|
|
46
|
-
writeFileSync(
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
mkdirSync(join(root, 'inject-cache'), { recursive: true })
|
|
48
|
+
writeFileSync(
|
|
49
|
+
cacheFile(root, key),
|
|
50
|
+
`${JSON.stringify(entry)}
|
|
51
|
+
`,
|
|
52
|
+
'utf8',
|
|
53
|
+
)
|
|
54
|
+
return true
|
|
49
55
|
} catch {
|
|
50
|
-
return false
|
|
56
|
+
return false
|
|
51
57
|
}
|
|
52
58
|
}
|
|
53
59
|
|
|
54
60
|
// src/checkpoint/injectState.ts
|
|
55
|
-
import {
|
|
56
|
-
|
|
61
|
+
import {
|
|
62
|
+
existsSync as existsSync2,
|
|
63
|
+
readdirSync as readdirSync2,
|
|
64
|
+
readFileSync as readFileSync3,
|
|
65
|
+
} from 'node:fs'
|
|
66
|
+
import { join as join3 } from 'node:path'
|
|
57
67
|
|
|
58
68
|
// src/checkpoint/breakLoop.ts
|
|
59
|
-
var LOOP_THRESHOLD = 3
|
|
69
|
+
var LOOP_THRESHOLD = 3
|
|
60
70
|
function detectLoop(ledger) {
|
|
61
|
-
return ledger
|
|
71
|
+
return ledger
|
|
72
|
+
.filter((e) => (e.fail_count ?? 0) >= LOOP_THRESHOLD)
|
|
73
|
+
.map((e) => ({ sub: e.sub, count: e.fail_count }))
|
|
62
74
|
}
|
|
63
75
|
|
|
64
76
|
// src/checkpoint/ledger.ts
|
|
65
77
|
function nextPending(entries) {
|
|
66
|
-
return entries.find((e) => e.status ===
|
|
78
|
+
return entries.find((e) => e.status === 'pending')?.sub ?? null
|
|
67
79
|
}
|
|
68
80
|
|
|
69
81
|
// src/checkpoint/deriveNext.ts
|
|
70
82
|
function deriveNext(snapshot) {
|
|
71
|
-
const pendingSub = nextPending(snapshot.subProgress)
|
|
72
|
-
if (pendingSub !== null) return { kind:
|
|
73
|
-
const failed = snapshot.subProgress.find((e) => e.status ===
|
|
83
|
+
const pendingSub = nextPending(snapshot.subProgress)
|
|
84
|
+
if (pendingSub !== null) return { kind: 'sub', sub: pendingSub }
|
|
85
|
+
const failed = snapshot.subProgress.find((e) => e.status === 'failed' && (e.fail_count ?? 0) > 0)
|
|
74
86
|
if (failed) {
|
|
75
87
|
return {
|
|
76
|
-
kind:
|
|
88
|
+
kind: 'blocked',
|
|
77
89
|
unit: failed.sub,
|
|
78
|
-
reason: `sub '${failed.sub}' failed ${failed.fail_count}x \u2014 resolve before advancing
|
|
79
|
-
}
|
|
90
|
+
reason: `sub '${failed.sub}' failed ${failed.fail_count}x \u2014 resolve before advancing`,
|
|
91
|
+
}
|
|
80
92
|
}
|
|
81
|
-
const firstIncomplete = snapshot.phases.find((p) => !p.complete)
|
|
93
|
+
const firstIncomplete = snapshot.phases.find((p) => !p.complete)
|
|
82
94
|
if (firstIncomplete) {
|
|
83
|
-
const task = firstIncomplete.tasks?.nextUnchecked
|
|
84
|
-
if (task) return { kind:
|
|
85
|
-
return { kind:
|
|
95
|
+
const task = firstIncomplete.tasks?.nextUnchecked
|
|
96
|
+
if (task) return { kind: 'task', phase: firstIncomplete.phase, task }
|
|
97
|
+
return { kind: 'phase', phase: firstIncomplete.phase, name: firstIncomplete.name }
|
|
86
98
|
}
|
|
87
|
-
return { kind:
|
|
99
|
+
return { kind: 'done' }
|
|
88
100
|
}
|
|
89
101
|
|
|
90
102
|
// src/checkpoint/forwardStep.ts
|
|
91
103
|
function describeUnit(unit) {
|
|
92
104
|
switch (unit.kind) {
|
|
93
|
-
case
|
|
94
|
-
return `phase ${unit.phase} '${unit.name}'
|
|
95
|
-
case
|
|
96
|
-
return `task '${unit.task}' in phase ${unit.phase}
|
|
97
|
-
case
|
|
98
|
-
return `sub '${unit.sub}'
|
|
99
|
-
case
|
|
100
|
-
return unit.unit
|
|
101
|
-
case
|
|
102
|
-
return null
|
|
105
|
+
case 'phase':
|
|
106
|
+
return `phase ${unit.phase} '${unit.name}'`
|
|
107
|
+
case 'task':
|
|
108
|
+
return `task '${unit.task}' in phase ${unit.phase}`
|
|
109
|
+
case 'sub':
|
|
110
|
+
return `sub '${unit.sub}'`
|
|
111
|
+
case 'blocked':
|
|
112
|
+
return unit.unit
|
|
113
|
+
case 'done':
|
|
114
|
+
return null
|
|
103
115
|
}
|
|
104
116
|
}
|
|
105
117
|
|
|
106
118
|
// src/checkpoint/planningScan.ts
|
|
107
|
-
import { existsSync, readdirSync, readFileSync as readFileSync2 } from
|
|
108
|
-
import { join as join2 } from
|
|
109
|
-
var PHASE_DIR = /^(\d+(?:\.\d+)?)-(.*)
|
|
119
|
+
import { existsSync, readdirSync, readFileSync as readFileSync2 } from 'node:fs'
|
|
120
|
+
import { join as join2 } from 'node:path'
|
|
121
|
+
var PHASE_DIR = /^(\d+(?:\.\d+)?)-(.*)$/
|
|
110
122
|
function scanPlanning(opts = {}) {
|
|
111
|
-
const repoRoot = opts.repoRoot ?? process.cwd()
|
|
112
|
-
const wf = opts.currentWorkflow ?? null
|
|
123
|
+
const repoRoot = opts.repoRoot ?? process.cwd()
|
|
124
|
+
const wf = opts.currentWorkflow ?? null
|
|
113
125
|
return {
|
|
114
126
|
subProgress: wf?.sub_progress ?? [],
|
|
115
127
|
currentPhase: wf?.phase ?? null,
|
|
116
|
-
phases: scanPhases(repoRoot, opts.includeTasks ?? false)
|
|
117
|
-
}
|
|
128
|
+
phases: scanPhases(repoRoot, opts.includeTasks ?? false),
|
|
129
|
+
}
|
|
118
130
|
}
|
|
119
131
|
function scanPhases(repoRoot, includeTasks) {
|
|
120
132
|
try {
|
|
121
|
-
const phasesDir = join2(repoRoot,
|
|
122
|
-
if (!existsSync(phasesDir)) return []
|
|
123
|
-
const out = []
|
|
133
|
+
const phasesDir = join2(repoRoot, '.planning', 'phases')
|
|
134
|
+
if (!existsSync(phasesDir)) return []
|
|
135
|
+
const out = []
|
|
124
136
|
for (const dir of readdirSync(phasesDir)) {
|
|
125
|
-
const m = PHASE_DIR.exec(dir)
|
|
126
|
-
const num = m?.[1]
|
|
127
|
-
if (!num) continue
|
|
128
|
-
const dirPath = join2(phasesDir, dir)
|
|
129
|
-
let entries
|
|
137
|
+
const m = PHASE_DIR.exec(dir)
|
|
138
|
+
const num = m?.[1]
|
|
139
|
+
if (!num) continue
|
|
140
|
+
const dirPath = join2(phasesDir, dir)
|
|
141
|
+
let entries
|
|
130
142
|
try {
|
|
131
|
-
entries = readdirSync(dirPath)
|
|
143
|
+
entries = readdirSync(dirPath)
|
|
132
144
|
} catch {
|
|
133
|
-
continue
|
|
145
|
+
continue
|
|
134
146
|
}
|
|
135
|
-
let plans = 0
|
|
136
|
-
let summaries = 0
|
|
137
|
-
const prefix = `${num}
|
|
147
|
+
let plans = 0
|
|
148
|
+
let summaries = 0
|
|
149
|
+
const prefix = `${num}-`
|
|
138
150
|
for (const f of entries) {
|
|
139
|
-
if (!f.startsWith(prefix)) continue
|
|
140
|
-
if (f.endsWith(
|
|
141
|
-
else if (f.endsWith(
|
|
151
|
+
if (!f.startsWith(prefix)) continue
|
|
152
|
+
if (f.endsWith('-PLAN.md')) plans++
|
|
153
|
+
else if (f.endsWith('-SUMMARY.md')) summaries++
|
|
142
154
|
}
|
|
143
|
-
const complete = plans > 0 && summaries >= plans
|
|
155
|
+
const complete = plans > 0 && summaries >= plans
|
|
144
156
|
const phase = {
|
|
145
157
|
phase: num,
|
|
146
|
-
name: m?.[2] ??
|
|
158
|
+
name: m?.[2] ?? '',
|
|
147
159
|
plans,
|
|
148
160
|
summaries,
|
|
149
161
|
complete,
|
|
150
|
-
order: Number.parseFloat(num)
|
|
151
|
-
}
|
|
152
|
-
if (includeTasks && !complete) phase.tasks = scanTasks(dirPath)
|
|
153
|
-
out.push(phase)
|
|
162
|
+
order: Number.parseFloat(num),
|
|
163
|
+
}
|
|
164
|
+
if (includeTasks && !complete) phase.tasks = scanTasks(dirPath)
|
|
165
|
+
out.push(phase)
|
|
154
166
|
}
|
|
155
|
-
out.sort((a, b) => a.order - b.order)
|
|
156
|
-
return out
|
|
167
|
+
out.sort((a, b) => a.order - b.order)
|
|
168
|
+
return out
|
|
157
169
|
} catch {
|
|
158
|
-
return []
|
|
170
|
+
return []
|
|
159
171
|
}
|
|
160
172
|
}
|
|
161
173
|
function scanTasks(dirPath) {
|
|
162
|
-
for (const fname of [
|
|
174
|
+
for (const fname of ['task_plan.md', 'progress.md']) {
|
|
163
175
|
try {
|
|
164
|
-
const p = join2(dirPath, fname)
|
|
165
|
-
if (!existsSync(p)) continue
|
|
166
|
-
const m = /^[ \t]*- \[ \]\s+(.+)$/m.exec(readFileSync2(p,
|
|
167
|
-
const task = m?.[1]
|
|
168
|
-
if (task) return { nextUnchecked: task.trim() }
|
|
169
|
-
} catch {
|
|
170
|
-
}
|
|
176
|
+
const p = join2(dirPath, fname)
|
|
177
|
+
if (!existsSync(p)) continue
|
|
178
|
+
const m = /^[ \t]*- \[ \]\s+(.+)$/m.exec(readFileSync2(p, 'utf8'))
|
|
179
|
+
const task = m?.[1]
|
|
180
|
+
if (task) return { nextUnchecked: task.trim() }
|
|
181
|
+
} catch {}
|
|
171
182
|
}
|
|
172
|
-
return { nextUnchecked: null }
|
|
183
|
+
return { nextUnchecked: null }
|
|
173
184
|
}
|
|
174
185
|
|
|
175
186
|
// src/checkpoint/injectState.ts
|
|
176
|
-
var INTENT_TTL_MS = 24 * 60 * 60 * 1e3
|
|
187
|
+
var INTENT_TTL_MS = 24 * 60 * 60 * 1e3
|
|
177
188
|
function formatIntentAge(ms) {
|
|
178
|
-
const m = Math.floor(ms / 6e4)
|
|
179
|
-
if (m < 1) return
|
|
180
|
-
if (m < 60) return `${m}m
|
|
181
|
-
return `${Math.floor(m / 60)}h
|
|
189
|
+
const m = Math.floor(ms / 6e4)
|
|
190
|
+
if (m < 1) return '<1m'
|
|
191
|
+
if (m < 60) return `${m}m`
|
|
192
|
+
return `${Math.floor(m / 60)}h`
|
|
182
193
|
}
|
|
183
194
|
function buildIntentBlock(intent, nowMs) {
|
|
184
|
-
if (!intent) return
|
|
185
|
-
const born = Date.parse(intent.ts)
|
|
186
|
-
if (Number.isNaN(born)) return
|
|
187
|
-
const age = nowMs - born
|
|
188
|
-
if (age < 0 || age > INTENT_TTL_MS) return
|
|
189
|
-
const m = intent.master
|
|
190
|
-
if (intent.kind ===
|
|
195
|
+
if (!intent) return ''
|
|
196
|
+
const born = Date.parse(intent.ts)
|
|
197
|
+
if (Number.isNaN(born)) return ''
|
|
198
|
+
const age = nowMs - born
|
|
199
|
+
if (age < 0 || age > INTENT_TTL_MS) return ''
|
|
200
|
+
const m = intent.master
|
|
201
|
+
if (intent.kind === 'leaf') {
|
|
191
202
|
return [
|
|
192
|
-
|
|
203
|
+
'<workflow-intent>',
|
|
193
204
|
`intent: /${m} invoked ${formatIntentAge(age)} ago \u2014 sub NOT checkpointed`,
|
|
194
205
|
`ENGINE: /${m} is registered but not engaged \u2014 freestyle risk. Run: \`harnessed prompt ${m} --task "<spec>" --json\` \u2192 spawn per SOP \u2192 \`harnessed checkpoint complete ${m}\` (or \`harnessed checkpoint fail ${m}\`).`,
|
|
195
|
-
|
|
196
|
-
].join(
|
|
206
|
+
'</workflow-intent>',
|
|
207
|
+
].join('\n')
|
|
197
208
|
}
|
|
198
209
|
return [
|
|
199
|
-
|
|
210
|
+
'<workflow-intent>',
|
|
200
211
|
`intent: /${m} invoked ${formatIntentAge(age)} ago \u2014 ledger NOT seeded`,
|
|
201
212
|
`ENGINE: /${m} is registered but not engaged \u2014 freestyle risk. Run: \`harnessed gates ${m} --task "<locked spec>"\` \u2192 \`harnessed checkpoint start ${m} --plan '<gates JSON>'\`; then drive each sub via \`harnessed prompt\` / \`harnessed checkpoint complete|fail\`.`,
|
|
202
|
-
|
|
203
|
-
].join(
|
|
213
|
+
'</workflow-intent>',
|
|
214
|
+
].join('\n')
|
|
204
215
|
}
|
|
205
|
-
var STALE_LEDGER_MS = 24 * 60 * 60 * 1e3
|
|
216
|
+
var STALE_LEDGER_MS = 24 * 60 * 60 * 1e3
|
|
206
217
|
function buildWorkflowStateBlock(wf, forward, ledgerAgeMs) {
|
|
207
|
-
if (!wf) return
|
|
208
|
-
const ledger = wf.sub_progress ?? []
|
|
209
|
-
const next = nextPending(ledger)
|
|
210
|
-
const loops = detectLoop(ledger)
|
|
218
|
+
if (!wf) return ''
|
|
219
|
+
const ledger = wf.sub_progress ?? []
|
|
220
|
+
const next = nextPending(ledger)
|
|
221
|
+
const loops = detectLoop(ledger)
|
|
211
222
|
const lines = [
|
|
212
|
-
|
|
223
|
+
'<workflow-state>',
|
|
213
224
|
`phase: ${wf.phase}`,
|
|
214
225
|
`status: ${wf.status}`,
|
|
215
|
-
next ? `next: ${next}` :
|
|
216
|
-
]
|
|
217
|
-
if (next && typeof ledgerAgeMs ===
|
|
218
|
-
const days = Math.floor(ledgerAgeMs / (24 * 60 * 60 * 1e3))
|
|
226
|
+
next ? `next: ${next}` : 'next: (none \u2014 all subs resolved)',
|
|
227
|
+
]
|
|
228
|
+
if (next && typeof ledgerAgeMs === 'number' && ledgerAgeMs > STALE_LEDGER_MS) {
|
|
229
|
+
const days = Math.floor(ledgerAgeMs / (24 * 60 * 60 * 1e3))
|
|
219
230
|
lines.push(
|
|
220
|
-
`ENGINE: STALE state machine \u2014 sub '${next}' has been pending with NO checkpoint activity for >${days}d. This ledger may belong to an abandoned/bypassed run (issue #3 class). Run \`harnessed status --recover\` to re-orient, then either resume the sub or close it out with \`harnessed checkpoint fail ${next} --summary "<why>"
|
|
221
|
-
)
|
|
231
|
+
`ENGINE: STALE state machine \u2014 sub '${next}' has been pending with NO checkpoint activity for >${days}d. This ledger may belong to an abandoned/bypassed run (issue #3 class). Run \`harnessed status --recover\` to re-orient, then either resume the sub or close it out with \`harnessed checkpoint fail ${next} --summary "<why>"\`.`,
|
|
232
|
+
)
|
|
222
233
|
} else if (next) {
|
|
223
234
|
lines.push(
|
|
224
|
-
`ENGINE: mid state-machine \u2014 drive sub '${next}' via \`harnessed prompt ${next}\` \u2192 spawn \u2192 \`harnessed checkpoint complete/fail\`. Do NOT freestyle the orchestration or skip the ledger; run \`harnessed status --recover\` if unsure where you are
|
|
225
|
-
)
|
|
235
|
+
`ENGINE: mid state-machine \u2014 drive sub '${next}' via \`harnessed prompt ${next}\` \u2192 spawn \u2192 \`harnessed checkpoint complete/fail\`. Do NOT freestyle the orchestration or skip the ledger; run \`harnessed status --recover\` if unsure where you are.`,
|
|
236
|
+
)
|
|
226
237
|
}
|
|
227
|
-
if (!next && forward && (forward.unit.kind ===
|
|
228
|
-
const n = forward.remainingPhases
|
|
238
|
+
if (!next && forward && (forward.unit.kind === 'phase' || forward.unit.kind === 'task')) {
|
|
239
|
+
const n = forward.remainingPhases
|
|
229
240
|
lines.push(
|
|
230
|
-
`NEXT-UNIT: current workflow complete \u2192 next is ${describeUnit(forward.unit)} (run /auto or \`harnessed advance\`); ${n} phase${n === 1 ?
|
|
231
|
-
)
|
|
241
|
+
`NEXT-UNIT: current workflow complete \u2192 next is ${describeUnit(forward.unit)} (run /auto or \`harnessed advance\`); ${n} phase${n === 1 ? '' : 's'} remain`,
|
|
242
|
+
)
|
|
232
243
|
}
|
|
233
244
|
for (const l of loops) {
|
|
234
245
|
lines.push(
|
|
235
|
-
`BREAK-LOOP: sub '${l.sub}' failed ${l.count}x \u2014 stop retrying, run break-loop skill
|
|
236
|
-
)
|
|
246
|
+
`BREAK-LOOP: sub '${l.sub}' failed ${l.count}x \u2014 stop retrying, run break-loop skill`,
|
|
247
|
+
)
|
|
237
248
|
}
|
|
238
249
|
if (wf.ship_ready) {
|
|
239
250
|
lines.push(
|
|
240
|
-
`SHIP-READY: ${wf.ship_commits ?? 0} commit(s) since the last release tag \u2014 consider shipping (harnessed release-preflight, then /ship)
|
|
241
|
-
)
|
|
251
|
+
`SHIP-READY: ${wf.ship_commits ?? 0} commit(s) since the last release tag \u2014 consider shipping (harnessed release-preflight, then /ship)`,
|
|
252
|
+
)
|
|
242
253
|
}
|
|
243
254
|
if (wf.retro_due) {
|
|
244
255
|
lines.push(
|
|
245
|
-
|
|
246
|
-
)
|
|
256
|
+
'RETRO-DUE: enough phases completed since the last retro \u2014 run /retro, then `harnessed retro --done`',
|
|
257
|
+
)
|
|
247
258
|
}
|
|
248
|
-
if (wf.verify_mode ===
|
|
259
|
+
if (wf.verify_mode === 'full') {
|
|
249
260
|
lines.push(
|
|
250
|
-
|
|
251
|
-
)
|
|
252
|
-
} else if (wf.verify_mode ===
|
|
253
|
-
lines.push(
|
|
261
|
+
'VERIFY-MODE: full \u2014 run full verification (large/risky change: >5 files / >4 subs / >3 reqs)',
|
|
262
|
+
)
|
|
263
|
+
} else if (wf.verify_mode === 'light') {
|
|
264
|
+
lines.push('VERIFY-MODE: light \u2014 scope verification to the changed surface (small change)')
|
|
254
265
|
}
|
|
255
|
-
lines.push(
|
|
256
|
-
return lines.join(
|
|
266
|
+
lines.push('</workflow-state>')
|
|
267
|
+
return lines.join('\n')
|
|
257
268
|
}
|
|
258
|
-
var DEFAULT_INJECT_BUDGET = 1500
|
|
259
|
-
var tok = (s) => Math.ceil(Buffer.byteLength(s,
|
|
269
|
+
var DEFAULT_INJECT_BUDGET = 1500
|
|
270
|
+
var tok = (s) => Math.ceil(Buffer.byteLength(s, 'utf8') / 4)
|
|
260
271
|
function parseLearnings(md) {
|
|
261
|
-
const blocks = md.split(/^### /m).slice(1)
|
|
272
|
+
const blocks = md.split(/^### /m).slice(1)
|
|
262
273
|
return blocks.map((b) => {
|
|
263
|
-
const raw = `### ${b}`.trimEnd()
|
|
264
|
-
const phase = /phase (\S+)/.exec(b)?.[1] ??
|
|
265
|
-
const subs = []
|
|
274
|
+
const raw = `### ${b}`.trimEnd()
|
|
275
|
+
const phase = /phase (\S+)/.exec(b)?.[1] ?? ''
|
|
276
|
+
const subs = []
|
|
266
277
|
for (const m of b.matchAll(/^- (?:looped|rejected|failed): (\S+)/gm)) {
|
|
267
|
-
if (m[1]) subs.push(m[1])
|
|
278
|
+
if (m[1]) subs.push(m[1])
|
|
268
279
|
}
|
|
269
|
-
return { raw, phase, subs }
|
|
270
|
-
})
|
|
280
|
+
return { raw, phase, subs }
|
|
281
|
+
})
|
|
271
282
|
}
|
|
272
283
|
function filterRelevantLearnings(entries, ctx) {
|
|
273
284
|
const rel = entries.filter(
|
|
274
|
-
(e) => e.phase === ctx.phase || e.subs.some((s) => ctx.ledgerSubs.includes(s))
|
|
275
|
-
)
|
|
276
|
-
const ordered = [...rel].reverse()
|
|
285
|
+
(e) => e.phase === ctx.phase || e.subs.some((s) => ctx.ledgerSubs.includes(s)),
|
|
286
|
+
)
|
|
287
|
+
const ordered = [...rel].reverse()
|
|
277
288
|
if (ordered.length === 0) {
|
|
278
|
-
const last = entries[entries.length - 1]
|
|
279
|
-
return last ? [last] : []
|
|
289
|
+
const last = entries[entries.length - 1]
|
|
290
|
+
return last ? [last] : []
|
|
280
291
|
}
|
|
281
|
-
return ordered
|
|
292
|
+
return ordered
|
|
282
293
|
}
|
|
283
294
|
function selectWithinBudget(entries, budget) {
|
|
284
|
-
const out = []
|
|
285
|
-
let acc = 0
|
|
295
|
+
const out = []
|
|
296
|
+
let acc = 0
|
|
286
297
|
for (const e of entries) {
|
|
287
|
-
const cost = tok(e.raw)
|
|
288
|
-
if (acc + cost > budget) break
|
|
289
|
-
acc += cost
|
|
290
|
-
out.push(e)
|
|
298
|
+
const cost = tok(e.raw)
|
|
299
|
+
if (acc + cost > budget) break
|
|
300
|
+
acc += cost
|
|
301
|
+
out.push(e)
|
|
291
302
|
}
|
|
292
|
-
return out
|
|
303
|
+
return out
|
|
293
304
|
}
|
|
294
305
|
function buildProjectContextBlock(input) {
|
|
295
|
-
const parts = []
|
|
296
|
-
for (const l of input.learnings) parts.push(l.raw.trim())
|
|
297
|
-
if (input.contextExcerpt) parts.push(input.contextExcerpt.trim())
|
|
298
|
-
if (parts.length === 0) return
|
|
299
|
-
return [
|
|
306
|
+
const parts = []
|
|
307
|
+
for (const l of input.learnings) parts.push(l.raw.trim())
|
|
308
|
+
if (input.contextExcerpt) parts.push(input.contextExcerpt.trim())
|
|
309
|
+
if (parts.length === 0) return ''
|
|
310
|
+
return ['<project-context>', ...parts, '</project-context>'].join('\n')
|
|
300
311
|
}
|
|
301
312
|
function findPhaseContextExcerpt(repoRoot, phase, budget) {
|
|
302
313
|
try {
|
|
303
|
-
const phasesDir = join3(repoRoot,
|
|
304
|
-
if (!existsSync2(phasesDir)) return null
|
|
314
|
+
const phasesDir = join3(repoRoot, '.planning', 'phases')
|
|
315
|
+
if (!existsSync2(phasesDir)) return null
|
|
305
316
|
for (const dir of readdirSync2(phasesDir)) {
|
|
306
|
-
const num = /^(\d+)/.exec(dir)?.[1]
|
|
307
|
-
if (!num || !phase.includes(num)) continue
|
|
308
|
-
const ctxFile = join3(phasesDir, dir, `${num}-CONTEXT.md`)
|
|
309
|
-
if (!existsSync2(ctxFile)) continue
|
|
310
|
-
const body = readFileSync3(ctxFile,
|
|
311
|
-
const goalIdx = body.indexOf(
|
|
312
|
-
const slice = goalIdx >= 0 ? body.slice(goalIdx) : body
|
|
313
|
-
const next = slice.indexOf(
|
|
314
|
-
const excerpt = (next > 0 ? slice.slice(0, next) : slice).trim()
|
|
315
|
-
return excerpt.length > budget * 4 ? excerpt.slice(0, budget * 4) : excerpt
|
|
317
|
+
const num = /^(\d+)/.exec(dir)?.[1]
|
|
318
|
+
if (!num || !phase.includes(num)) continue
|
|
319
|
+
const ctxFile = join3(phasesDir, dir, `${num}-CONTEXT.md`)
|
|
320
|
+
if (!existsSync2(ctxFile)) continue
|
|
321
|
+
const body = readFileSync3(ctxFile, 'utf8')
|
|
322
|
+
const goalIdx = body.indexOf('## Goal')
|
|
323
|
+
const slice = goalIdx >= 0 ? body.slice(goalIdx) : body
|
|
324
|
+
const next = slice.indexOf('\n## ', 1)
|
|
325
|
+
const excerpt = (next > 0 ? slice.slice(0, next) : slice).trim()
|
|
326
|
+
return excerpt.length > budget * 4 ? excerpt.slice(0, budget * 4) : excerpt
|
|
316
327
|
}
|
|
317
|
-
} catch {
|
|
318
|
-
|
|
319
|
-
return null;
|
|
328
|
+
} catch {}
|
|
329
|
+
return null
|
|
320
330
|
}
|
|
321
|
-
function buildInjection(
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
331
|
+
function buildInjection(
|
|
332
|
+
repoRoot,
|
|
333
|
+
wf,
|
|
334
|
+
learningsMd,
|
|
335
|
+
budget = DEFAULT_INJECT_BUDGET,
|
|
336
|
+
intent = null,
|
|
337
|
+
nowMs = Date.now(),
|
|
338
|
+
opts = {},
|
|
339
|
+
) {
|
|
340
|
+
const ledgerEmpty = (wf?.sub_progress ?? []).length === 0
|
|
341
|
+
const intentBlock = ledgerEmpty ? buildIntentBlock(intent, nowMs) : ''
|
|
342
|
+
if (!wf) return intentBlock
|
|
343
|
+
const forward = nextPending(wf.sub_progress ?? []) === null ? forwardPointer(repoRoot, wf) : null
|
|
344
|
+
const ws = buildWorkflowStateBlock(wf, forward, opts.ledgerAgeMs)
|
|
345
|
+
const ledgerSubs = (wf.sub_progress ?? []).map((e) => e.sub)
|
|
328
346
|
const rel = selectWithinBudget(
|
|
329
347
|
filterRelevantLearnings(parseLearnings(learningsMd), { phase: wf.phase, ledgerSubs }),
|
|
330
|
-
budget
|
|
331
|
-
)
|
|
332
|
-
const usedTokens = rel.reduce((a, e) => a + tok(e.raw), 0)
|
|
333
|
-
const ctx = findPhaseContextExcerpt(repoRoot, wf.phase, Math.max(0, budget - usedTokens))
|
|
334
|
-
const pcRaw = buildProjectContextBlock({ learnings: rel, contextExcerpt: ctx ?? void 0 })
|
|
335
|
-
const pc = pcRaw && opts.pcGate ? opts.pcGate(pcRaw) ? pcRaw :
|
|
336
|
-
const normal = pc
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
348
|
+
budget,
|
|
349
|
+
)
|
|
350
|
+
const usedTokens = rel.reduce((a, e) => a + tok(e.raw), 0)
|
|
351
|
+
const ctx = findPhaseContextExcerpt(repoRoot, wf.phase, Math.max(0, budget - usedTokens))
|
|
352
|
+
const pcRaw = buildProjectContextBlock({ learnings: rel, contextExcerpt: ctx ?? void 0 })
|
|
353
|
+
const pc = pcRaw && opts.pcGate ? (opts.pcGate(pcRaw) ? pcRaw : '') : pcRaw
|
|
354
|
+
const normal = pc
|
|
355
|
+
? `${ws}
|
|
356
|
+
${pc}`
|
|
357
|
+
: ws
|
|
358
|
+
return intentBlock
|
|
359
|
+
? `${intentBlock}
|
|
360
|
+
${normal}`
|
|
361
|
+
: normal
|
|
340
362
|
}
|
|
341
363
|
function forwardPointer(repoRoot, wf) {
|
|
342
|
-
const snapshot = scanPlanning({ repoRoot, currentWorkflow: wf, includeTasks: false })
|
|
343
|
-
const unit = deriveNext(snapshot)
|
|
344
|
-
if (unit.kind !==
|
|
345
|
-
return { unit, remainingPhases: snapshot.phases.filter((p) => !p.complete).length }
|
|
364
|
+
const snapshot = scanPlanning({ repoRoot, currentWorkflow: wf, includeTasks: false })
|
|
365
|
+
const unit = deriveNext(snapshot)
|
|
366
|
+
if (unit.kind !== 'phase' && unit.kind !== 'task') return null
|
|
367
|
+
return { unit, remainingPhases: snapshot.phases.filter((p) => !p.complete).length }
|
|
346
368
|
}
|
|
347
369
|
|
|
348
370
|
// src/checkpoint/injectStateMain.ts
|
|
349
371
|
function repoKey(cwd) {
|
|
350
|
-
let dir = resolve(cwd)
|
|
351
|
-
for (
|
|
352
|
-
if (existsSync3(join4(dir,
|
|
353
|
-
const parent = dirname(dir)
|
|
354
|
-
if (parent === dir) break
|
|
355
|
-
dir = parent
|
|
372
|
+
let dir = resolve(cwd)
|
|
373
|
+
for (;;) {
|
|
374
|
+
if (existsSync3(join4(dir, '.git'))) return dir
|
|
375
|
+
const parent = dirname(dir)
|
|
376
|
+
if (parent === dir) break
|
|
377
|
+
dir = parent
|
|
356
378
|
}
|
|
357
|
-
return resolve(cwd)
|
|
379
|
+
return resolve(cwd)
|
|
358
380
|
}
|
|
359
381
|
function harnessedRoot() {
|
|
360
|
-
const override = process.env.HARNESSED_ROOT_OVERRIDE
|
|
361
|
-
return override !== void 0 && override !==
|
|
382
|
+
const override = process.env.HARNESSED_ROOT_OVERRIDE
|
|
383
|
+
return override !== void 0 && override !== ''
|
|
384
|
+
? override
|
|
385
|
+
: join4(homedir(), '.claude', 'harnessed')
|
|
362
386
|
}
|
|
363
387
|
function sessionIdEnvName() {
|
|
364
|
-
const platform = (process.env.HARNESSED_PLATFORM ||
|
|
365
|
-
if (platform ===
|
|
366
|
-
return
|
|
388
|
+
const platform = (process.env.HARNESSED_PLATFORM || 'claude').trim()
|
|
389
|
+
if (platform === 'codex') return null
|
|
390
|
+
return 'CLAUDE_CODE_SESSION_ID'
|
|
367
391
|
}
|
|
368
392
|
function readWorkflow(root, keys) {
|
|
369
|
-
let wf = null
|
|
370
|
-
let intent = null
|
|
371
|
-
let ledgerAgeMs = null
|
|
393
|
+
let wf = null
|
|
394
|
+
let intent = null
|
|
395
|
+
let ledgerAgeMs = null
|
|
372
396
|
const ageOf = (p) => {
|
|
373
397
|
try {
|
|
374
|
-
return Date.now() - statSync(p).mtimeMs
|
|
398
|
+
return Date.now() - statSync(p).mtimeMs
|
|
375
399
|
} catch {
|
|
376
|
-
return null
|
|
400
|
+
return null
|
|
377
401
|
}
|
|
378
|
-
}
|
|
402
|
+
}
|
|
379
403
|
try {
|
|
380
|
-
const storePath = join4(root,
|
|
381
|
-
const store = JSON.parse(readFileSync4(storePath,
|
|
404
|
+
const storePath = join4(root, 'workflows.json')
|
|
405
|
+
const store = JSON.parse(readFileSync4(storePath, 'utf8'))
|
|
382
406
|
if (store?.workflows) {
|
|
383
407
|
for (const k of keys) {
|
|
384
408
|
if (store.workflows[k]) {
|
|
385
|
-
wf = store.workflows[k]
|
|
386
|
-
ledgerAgeMs = ageOf(storePath)
|
|
387
|
-
break
|
|
409
|
+
wf = store.workflows[k]
|
|
410
|
+
ledgerAgeMs = ageOf(storePath)
|
|
411
|
+
break
|
|
388
412
|
}
|
|
389
413
|
}
|
|
390
414
|
}
|
|
391
415
|
if (store?.intents) {
|
|
392
416
|
for (const k of keys) {
|
|
393
417
|
if (store.intents[k]) {
|
|
394
|
-
intent = store.intents[k]
|
|
395
|
-
break
|
|
418
|
+
intent = store.intents[k]
|
|
419
|
+
break
|
|
396
420
|
}
|
|
397
421
|
}
|
|
398
422
|
}
|
|
399
|
-
} catch {
|
|
400
|
-
}
|
|
401
|
-
return { wf, intent, ledgerAgeMs };
|
|
423
|
+
} catch {}
|
|
424
|
+
return { wf, intent, ledgerAgeMs }
|
|
402
425
|
}
|
|
403
426
|
function shouldEmitPc(root, repoRoot, sid, pc) {
|
|
404
427
|
try {
|
|
405
|
-
const key = injectCacheKey(repoRoot, sid)
|
|
406
|
-
const cache = readInjectCache(root, key)
|
|
407
|
-
const pcHash = createHash2(
|
|
408
|
-
const refreshN = parseRefreshTurns(process.env.HARNESSED_INJECT_REFRESH_TURNS)
|
|
409
|
-
const d = decidePcEmission(cache, pcHash, refreshN, Date.now())
|
|
410
|
-
if (!writeInjectCache(root, key, d.next)) return true
|
|
411
|
-
return d.emit
|
|
428
|
+
const key = injectCacheKey(repoRoot, sid)
|
|
429
|
+
const cache = readInjectCache(root, key)
|
|
430
|
+
const pcHash = createHash2('sha256').update(pc).digest('hex')
|
|
431
|
+
const refreshN = parseRefreshTurns(process.env.HARNESSED_INJECT_REFRESH_TURNS)
|
|
432
|
+
const d = decidePcEmission(cache, pcHash, refreshN, Date.now())
|
|
433
|
+
if (!writeInjectCache(root, key, d.next)) return true
|
|
434
|
+
return d.emit
|
|
412
435
|
} catch {
|
|
413
|
-
return true
|
|
436
|
+
return true
|
|
414
437
|
}
|
|
415
438
|
}
|
|
416
439
|
function main() {
|
|
417
440
|
try {
|
|
418
|
-
const root = harnessedRoot()
|
|
419
|
-
const key = repoKey(process.cwd())
|
|
420
|
-
const envName = sessionIdEnvName()
|
|
421
|
-
const sid = envName ? process.env[envName]?.trim() : void 0
|
|
422
|
-
const { wf, intent, ledgerAgeMs } = readWorkflow(root, sid ? [`${key}::${sid}`, key] : [key])
|
|
423
|
-
let learningsMd =
|
|
441
|
+
const root = harnessedRoot()
|
|
442
|
+
const key = repoKey(process.cwd())
|
|
443
|
+
const envName = sessionIdEnvName()
|
|
444
|
+
const sid = envName ? process.env[envName]?.trim() : void 0
|
|
445
|
+
const { wf, intent, ledgerAgeMs } = readWorkflow(root, sid ? [`${key}::${sid}`, key] : [key])
|
|
446
|
+
let learningsMd = ''
|
|
424
447
|
try {
|
|
425
|
-
learningsMd = readFileSync4(join4(key,
|
|
426
|
-
} catch {
|
|
427
|
-
|
|
428
|
-
const budget = Number(process.env.HARNESSED_INJECT_BUDGET) || DEFAULT_INJECT_BUDGET;
|
|
448
|
+
learningsMd = readFileSync4(join4(key, '.planning', 'LEARNINGS.md'), 'utf8')
|
|
449
|
+
} catch {}
|
|
450
|
+
const budget = Number(process.env.HARNESSED_INJECT_BUDGET) || DEFAULT_INJECT_BUDGET
|
|
429
451
|
const out = buildInjection(key, wf, learningsMd, budget, intent, Date.now(), {
|
|
430
452
|
ledgerAgeMs,
|
|
431
|
-
pcGate: sid ? (pc) => shouldEmitPc(root, key, sid, pc) : void 0
|
|
432
|
-
})
|
|
433
|
-
if (out)
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
}
|
|
453
|
+
pcGate: sid ? (pc) => shouldEmitPc(root, key, sid, pc) : void 0,
|
|
454
|
+
})
|
|
455
|
+
if (out)
|
|
456
|
+
process.stdout.write(`${out}
|
|
457
|
+
`)
|
|
458
|
+
} catch {}
|
|
437
459
|
}
|
|
438
|
-
main()
|
|
460
|
+
main()
|