harnessed 4.22.2 → 4.23.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.
@@ -13,7 +13,7 @@
13
13
  // Phase 15 repo-aware: resolves the active repo's slot from
14
14
  // workflows.json[repoKey(cwd)] (legacy current-workflow.json as a fallback).
15
15
  // Root: HARNESSED_ROOT_OVERRIDE if set, else <homedir>/.claude/harnessed.
16
- import { existsSync, readdirSync, readFileSync } from 'node:fs'
16
+ import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
17
17
  import { homedir } from 'node:os'
18
18
  import { dirname, join, resolve } from 'node:path'
19
19
 
@@ -56,12 +56,23 @@ function sessionIdEnvName() {
56
56
  function readWorkflow(root, keys) {
57
57
  let wf = null
58
58
  let intent = null
59
+ let ledgerAgeMs = null
60
+ const ageOf = (p) => {
61
+ try {
62
+ return Date.now() - statSync(p).mtimeMs
63
+ } catch {
64
+ return null
65
+ }
66
+ }
59
67
  try {
60
- const store = JSON.parse(readFileSync(join(root, 'workflows.json'), 'utf8'))
68
+ const storePath = join(root, 'workflows.json')
69
+ const store = JSON.parse(readFileSync(storePath, 'utf8'))
61
70
  if (store?.workflows) {
62
71
  for (const k of keys) {
63
72
  if (store.workflows[k]) {
64
73
  wf = store.workflows[k]
74
+ // 4.23.0 (issue #3 req 3) — ledger file age drives the STALE wording.
75
+ ledgerAgeMs = ageOf(storePath)
65
76
  break
66
77
  }
67
78
  }
@@ -77,10 +88,12 @@ function readWorkflow(root, keys) {
77
88
  } catch {}
78
89
  if (!wf) {
79
90
  try {
80
- wf = JSON.parse(readFileSync(join(root, 'current-workflow.json'), 'utf8'))
91
+ const legacyPath = join(root, 'current-workflow.json')
92
+ wf = JSON.parse(readFileSync(legacyPath, 'utf8'))
93
+ ledgerAgeMs = ageOf(legacyPath)
81
94
  } catch {}
82
95
  }
83
- return { wf, intent }
96
+ return { wf, intent, ledgerAgeMs }
84
97
  }
85
98
 
86
99
  // 4.22.0 — parity with injectState.ts INTENT_TTL_MS / formatIntentAge /
@@ -203,7 +216,10 @@ function forwardPointer(repoRoot, wf) {
203
216
  return { unit, remainingPhases: phases.filter((p) => !p.complete).length }
204
217
  }
205
218
 
206
- function workflowStateBlock(wf, forward) {
219
+ // 4.23.0 (issue #3 req 3) — parity with injectState.ts STALE_LEDGER_MS.
220
+ const STALE_LEDGER_MS = 24 * 60 * 60 * 1000
221
+
222
+ function workflowStateBlock(wf, forward, ledgerAgeMs) {
207
223
  const ledger = wf.sub_progress ?? []
208
224
  const next = ledger.find((e) => e.status === 'pending')?.sub ?? null
209
225
  const lines = [
@@ -213,7 +229,12 @@ function workflowStateBlock(wf, forward) {
213
229
  next ? `next: ${next}` : 'next: (none — all subs resolved)',
214
230
  ]
215
231
  // issue #2 (T2) — anti-freestyle enforcement (parity with injectState.ts).
216
- if (next) {
232
+ if (next && typeof ledgerAgeMs === 'number' && ledgerAgeMs > STALE_LEDGER_MS) {
233
+ const days = Math.floor(ledgerAgeMs / (24 * 60 * 60 * 1000))
234
+ lines.push(
235
+ `ENGINE: STALE state machine — 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>"\`.`,
236
+ )
237
+ } else if (next) {
217
238
  lines.push(
218
239
  `ENGINE: mid state-machine — drive sub '${next}' via \`harnessed prompt ${next}\` → spawn → \`harnessed checkpoint complete/fail\`. Do NOT freestyle the orchestration or skip the ledger; run \`harnessed status --recover\` if unsure where you are.`,
219
240
  )
@@ -321,7 +342,7 @@ try {
321
342
  const key = repoKey(process.cwd())
322
343
  const envName = sessionIdEnvName()
323
344
  const sid = envName ? process.env[envName]?.trim() : undefined
324
- const { wf, intent } = readWorkflow(root, sid ? [`${key}::${sid}`, key] : [key])
345
+ const { wf, intent, ledgerAgeMs } = readWorkflow(root, sid ? [`${key}::${sid}`, key] : [key])
325
346
  // 4.22.0 — freestyle nag: a fresh intent whose ledger was never seeded (start
326
347
  // absorbs the intent) keeps reminding until steps 2-3 (gates → start) run.
327
348
  const ledgerEmpty = (wf?.sub_progress ?? []).length === 0
@@ -336,7 +357,7 @@ try {
336
357
  // (parity with buildInjection). `key` is the repo root holding .planning/.
337
358
  const pending = (wf.sub_progress ?? []).find((e) => e.status === 'pending')?.sub ?? null
338
359
  const forward = pending === null ? forwardPointer(key, wf) : null
339
- const ws = workflowStateBlock(wf, forward)
360
+ const ws = workflowStateBlock(wf, forward, ledgerAgeMs)
340
361
 
341
362
  let learningsMd = ''
342
363
  try {