@dzhechkov/harness-core 0.3.150 → 0.4.2

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 (111) hide show
  1. package/.dz-manifest.json +410 -62
  2. package/README.md +81 -3
  3. package/dist/agentdb-index.d.ts.map +1 -1
  4. package/dist/agentdb-index.js +10 -2
  5. package/dist/agentdb-index.js.map +1 -1
  6. package/dist/backlog-embed.d.ts +94 -0
  7. package/dist/backlog-embed.d.ts.map +1 -0
  8. package/dist/backlog-embed.js +138 -0
  9. package/dist/backlog-embed.js.map +1 -0
  10. package/dist/backlog.d.ts +180 -7
  11. package/dist/backlog.d.ts.map +1 -1
  12. package/dist/backlog.js +429 -26
  13. package/dist/backlog.js.map +1 -1
  14. package/dist/challenge-panel.d.ts +3 -0
  15. package/dist/challenge-panel.d.ts.map +1 -1
  16. package/dist/challenge-panel.js +3 -0
  17. package/dist/challenge-panel.js.map +1 -1
  18. package/dist/export-holdout.d.ts +149 -0
  19. package/dist/export-holdout.d.ts.map +1 -0
  20. package/dist/export-holdout.js +198 -0
  21. package/dist/export-holdout.js.map +1 -0
  22. package/dist/feature-adr-checkpoints.d.ts +127 -0
  23. package/dist/feature-adr-checkpoints.d.ts.map +1 -1
  24. package/dist/feature-adr-checkpoints.js +199 -1
  25. package/dist/feature-adr-checkpoints.js.map +1 -1
  26. package/dist/feature-adr-routing.d.ts +3 -0
  27. package/dist/feature-adr-routing.d.ts.map +1 -1
  28. package/dist/feature-adr-routing.js +3 -0
  29. package/dist/feature-adr-routing.js.map +1 -1
  30. package/dist/guard.d.ts +42 -0
  31. package/dist/guard.d.ts.map +1 -1
  32. package/dist/guard.js +73 -1
  33. package/dist/guard.js.map +1 -1
  34. package/dist/index.d.ts +16 -3
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +28 -2
  37. package/dist/index.js.map +1 -1
  38. package/dist/loop-blobs.generated.d.ts +33 -0
  39. package/dist/loop-blobs.generated.d.ts.map +1 -0
  40. package/dist/loop-blobs.generated.js +101 -0
  41. package/dist/loop-blobs.generated.js.map +1 -0
  42. package/dist/loop-lint.d.ts +63 -0
  43. package/dist/loop-lint.d.ts.map +1 -0
  44. package/dist/loop-lint.js +606 -0
  45. package/dist/loop-lint.js.map +1 -0
  46. package/dist/loop-plan.d.ts +416 -0
  47. package/dist/loop-plan.d.ts.map +1 -0
  48. package/dist/loop-plan.js +1151 -0
  49. package/dist/loop-plan.js.map +1 -0
  50. package/dist/loop-render.d.ts +104 -0
  51. package/dist/loop-render.d.ts.map +1 -0
  52. package/dist/loop-render.js +1068 -0
  53. package/dist/loop-render.js.map +1 -0
  54. package/dist/loop-trace.d.ts +229 -0
  55. package/dist/loop-trace.d.ts.map +1 -0
  56. package/dist/loop-trace.js +614 -0
  57. package/dist/loop-trace.js.map +1 -0
  58. package/dist/mutation-gate.d.ts +247 -0
  59. package/dist/mutation-gate.d.ts.map +1 -0
  60. package/dist/mutation-gate.js +535 -0
  61. package/dist/mutation-gate.js.map +1 -0
  62. package/dist/no-stubs.d.ts +53 -0
  63. package/dist/no-stubs.d.ts.map +1 -0
  64. package/dist/no-stubs.js +190 -0
  65. package/dist/no-stubs.js.map +1 -0
  66. package/dist/package-skill-layouts.d.ts +67 -0
  67. package/dist/package-skill-layouts.d.ts.map +1 -0
  68. package/dist/package-skill-layouts.js +81 -0
  69. package/dist/package-skill-layouts.js.map +1 -0
  70. package/dist/patterns.d.ts.map +1 -1
  71. package/dist/patterns.js +156 -75
  72. package/dist/patterns.js.map +1 -1
  73. package/dist/recall-domain-boost.d.ts.map +1 -1
  74. package/dist/recall-domain-boost.js +6 -0
  75. package/dist/recall-domain-boost.js.map +1 -1
  76. package/dist/statusline.d.ts +10 -2
  77. package/dist/statusline.d.ts.map +1 -1
  78. package/dist/statusline.js +122 -36
  79. package/dist/statusline.js.map +1 -1
  80. package/dist/store-lock.d.ts +108 -0
  81. package/dist/store-lock.d.ts.map +1 -0
  82. package/dist/store-lock.js +231 -0
  83. package/dist/store-lock.js.map +1 -0
  84. package/dist/workflows.d.ts +16 -22
  85. package/dist/workflows.d.ts.map +1 -1
  86. package/dist/workflows.js +17 -98
  87. package/dist/workflows.js.map +1 -1
  88. package/package.json +6 -4
  89. package/sbom.json +1073 -203
  90. package/src/agentdb-index.ts +10 -1
  91. package/src/backlog-embed.ts +156 -0
  92. package/src/backlog.ts +536 -28
  93. package/src/challenge-panel.ts +4 -0
  94. package/src/export-holdout.ts +235 -0
  95. package/src/feature-adr-checkpoints.ts +291 -1
  96. package/src/feature-adr-routing.ts +4 -0
  97. package/src/guard.ts +106 -1
  98. package/src/index.ts +62 -2
  99. package/src/loop-blobs.generated.ts +114 -0
  100. package/src/loop-lint.ts +643 -0
  101. package/src/loop-plan.ts +1419 -0
  102. package/src/loop-render.ts +1126 -0
  103. package/src/loop-trace.ts +727 -0
  104. package/src/mutation-gate.ts +701 -0
  105. package/src/no-stubs.ts +204 -0
  106. package/src/package-skill-layouts.ts +107 -0
  107. package/src/patterns.ts +135 -60
  108. package/src/recall-domain-boost.ts +6 -0
  109. package/src/statusline.ts +117 -30
  110. package/src/store-lock.ts +258 -0
  111. package/src/workflows.ts +18 -117
package/src/statusline.ts CHANGED
@@ -14,8 +14,8 @@
14
14
  * @packageDocumentation
15
15
  */
16
16
 
17
- import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
18
- import { dirname, join, resolve } from 'node:path';
17
+ import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
18
+ import { join, resolve } from 'node:path';
19
19
  import { createRequire } from 'node:module';
20
20
 
21
21
  import { listBrain } from './brain.js';
@@ -28,6 +28,8 @@ import { RECALL_USAGE_LOG_RELATIVE, aggregateRecallUsage, parseRecallUsageLog }
28
28
  * path (readonly, best-effort) by `readFeatureAdrState`.
29
29
  */
30
30
  export interface FeatureAdrState {
31
+ /** Producer of this panel state. Missing/invalid legacy values are treated as `feature-adr`. */
32
+ readonly kind?: 'feature-adr' | 'loop';
31
33
  /** The feature slug the pipeline is working on (kebab-case). */
32
34
  readonly slug: string;
33
35
  /** Human-readable step label (e.g. "Step 0", "Step 8 QE"). */
@@ -70,9 +72,29 @@ function consolidateWatermarkPath(projectRoot: string): string {
70
72
  return join(projectRoot, '.dz', 'memory', 'consolidate.json');
71
73
  }
72
74
 
73
- /** Path of the live `/feature-adr` learning-state file (per-run panel source). */
74
- export function featureAdrStatePath(projectRoot: string): string {
75
- return join(projectRoot, '.dz', 'feature-adr', 'learning-state.json');
75
+ /** Directory of the per-slug live `/feature-adr` learning-state slots. */
76
+ export function featureAdrStateDir(projectRoot: string): string {
77
+ return join(projectRoot, '.dz', 'feature-adr', 'learning-state');
78
+ }
79
+
80
+ /** Make a slug safe as one bounded filename component (never `/`, `..`, or leading dot/dash). */
81
+ function featureAdrStateSlug(slug: string): string {
82
+ const safe = slug
83
+ .replace(/[^A-Za-z0-9._-]/g, '_')
84
+ .replace(/^[.-]+/, '_')
85
+ .replace(/\.{2,}/g, '_')
86
+ .slice(0, 60);
87
+ return safe.length > 0 ? safe : '_unnamed';
88
+ }
89
+
90
+ /**
91
+ * Path of a live `/feature-adr` learning-state file. With a slug this is its namespaced slot;
92
+ * without one this remains the legacy single-slot path for backward-compatible readers/callers.
93
+ */
94
+ export function featureAdrStatePath(projectRoot: string, slug?: string): string {
95
+ return slug === undefined
96
+ ? join(projectRoot, '.dz', 'feature-adr', 'learning-state.json')
97
+ : join(featureAdrStateDir(projectRoot), `${featureAdrStateSlug(slug)}.json`);
76
98
  }
77
99
 
78
100
  /**
@@ -181,35 +203,84 @@ function consolidatedAgeHours(projectRoot: string, now: number): number | undefi
181
203
  * @param now Injectable clock (epoch ms) for the freshness check — defaults to `Date.now()`.
182
204
  */
183
205
  export function readFeatureAdrState(projectRoot: string, now: number = Date.now()): FeatureAdrState | undefined {
184
- const path = featureAdrStatePath(resolve(projectRoot));
185
- if (!existsSync(path)) return undefined;
206
+ const root = resolve(projectRoot);
207
+ const candidates: string[] = [];
208
+
209
+ // Keep the legacy single slot in the candidate set: an older dz may still be writing it while a
210
+ // newer statusline renders. Directory discovery is guarded separately because this is the hot,
211
+ // readonly ~300ms render path; it never performs housekeeping or any other write.
186
212
  try {
187
- const parsed = JSON.parse(readFileSync(path, 'utf-8')) as Partial<FeatureAdrState>;
188
- if (typeof parsed.slug !== 'string' || parsed.slug.length === 0) return undefined;
189
- if (typeof parsed.step !== 'string' || parsed.step.length === 0) return undefined;
190
- if (typeof parsed.ts !== 'string') return undefined;
191
- const tsMs = Date.parse(parsed.ts);
192
- if (Number.isNaN(tsMs)) return undefined;
193
- if (now - tsMs > FEATURE_ADR_FRESH_MS) return undefined; // stale run — do not surface a panel
194
- const num = (v: unknown): number => (typeof v === 'number' && Number.isFinite(v) ? v : 0);
195
- const state: FeatureAdrState = {
196
- slug: parsed.slug,
197
- step: parsed.step,
198
- pool: num(parsed.pool),
199
- recalled: num(parsed.recalled),
200
- stored: num(parsed.stored),
201
- ...(num(parsed.reinforced) > 0 ? { reinforced: num(parsed.reinforced) } : {}),
202
- ts: parsed.ts,
203
- ...(typeof parsed.mode === 'string' && parsed.mode.length > 0 ? { mode: parsed.mode } : {}),
204
- };
205
- return state;
206
- } catch {
207
- return undefined;
213
+ const legacyPath = featureAdrStatePath(root);
214
+ if (existsSync(legacyPath)) candidates.push(legacyPath);
215
+ } catch { /* best-effort candidate discovery */ }
216
+ try {
217
+ const remaining = 64 - candidates.length;
218
+ if (remaining > 0) {
219
+ const dir = featureAdrStateDir(root);
220
+ const names = readdirSync(dir)
221
+ .filter((name) => name.endsWith('.json'))
222
+ .map((name) => {
223
+ let mtimeMs = -Infinity;
224
+ try {
225
+ mtimeMs = statSync(join(dir, name)).mtimeMs;
226
+ } catch { /* a disappearing/unreadable entry sorts last */ }
227
+ return { name, mtimeMs };
228
+ })
229
+ // Truncation may only drop the least recent slots: kind-rank arbitration cannot rescue a non-candidate.
230
+ .sort((a, b) => b.mtimeMs - a.mtimeMs)
231
+ .slice(0, remaining);
232
+ for (const { name } of names) candidates.push(join(dir, name));
233
+ }
234
+ } catch { /* absent/unreadable per-slug directory is normal */ }
235
+
236
+ const parseCandidate = (path: string): { state: FeatureAdrState; tsMs: number; rank: number } | undefined => {
237
+ try {
238
+ const parsed = JSON.parse(readFileSync(path, 'utf-8')) as Partial<FeatureAdrState>;
239
+ if (typeof parsed.slug !== 'string' || parsed.slug.length === 0) return undefined;
240
+ if (typeof parsed.step !== 'string' || parsed.step.length === 0) return undefined;
241
+ if (typeof parsed.ts !== 'string') return undefined;
242
+ const tsMs = Date.parse(parsed.ts);
243
+ if (Number.isNaN(tsMs)) return undefined;
244
+ if (now - tsMs > FEATURE_ADR_FRESH_MS) return undefined; // stale run — do not surface a panel
245
+ const num = (v: unknown): number => (typeof v === 'number' && Number.isFinite(v) ? v : 0);
246
+ // The panel exists to surface the /feature-adr Pattern-memory loop. A generated loop writes
247
+ // zero recalled/stored counters far more often, so freshest-wins would recreate F5 by making
248
+ // a live pipeline's meaningful counters disappear. Missing/invalid legacy markers therefore
249
+ // retain the historical `feature-adr` rank, which outranks every loop slot regardless of ts.
250
+ const kind: 'feature-adr' | 'loop' = parsed.kind === 'loop' ? 'loop' : 'feature-adr';
251
+ const state: FeatureAdrState = {
252
+ kind,
253
+ slug: parsed.slug,
254
+ step: parsed.step,
255
+ pool: num(parsed.pool),
256
+ recalled: num(parsed.recalled),
257
+ stored: num(parsed.stored),
258
+ ...(num(parsed.reinforced) > 0 ? { reinforced: num(parsed.reinforced) } : {}),
259
+ ts: parsed.ts,
260
+ ...(typeof parsed.mode === 'string' && parsed.mode.length > 0 ? { mode: parsed.mode } : {}),
261
+ };
262
+ return { state, tsMs, rank: kind === 'feature-adr' ? 1 : 0 };
263
+ } catch {
264
+ return undefined;
265
+ }
266
+ };
267
+
268
+ let winner: ReturnType<typeof parseCandidate>;
269
+ for (const path of candidates) {
270
+ const candidate = parseCandidate(path);
271
+ if (candidate === undefined) continue;
272
+ if (winner === undefined
273
+ || candidate.rank > winner.rank
274
+ || (candidate.rank === winner.rank && candidate.tsMs > winner.tsMs)) {
275
+ winner = candidate;
276
+ }
208
277
  }
278
+ return winner?.state;
209
279
  }
210
280
 
211
281
  /** Fields the `/feature-adr` pipeline supplies when recording its live learning state. */
212
282
  export interface WriteFeatureAdrStateInput {
283
+ readonly kind?: 'feature-adr' | 'loop';
213
284
  readonly slug: string;
214
285
  readonly step: string;
215
286
  readonly recalled: number;
@@ -239,6 +310,7 @@ export function writeFeatureAdrState(
239
310
  pool = 0;
240
311
  }
241
312
  const state: FeatureAdrState = {
313
+ kind: input.kind === 'loop' ? 'loop' : 'feature-adr',
242
314
  slug: input.slug,
243
315
  step: input.step,
244
316
  pool,
@@ -249,8 +321,23 @@ export function writeFeatureAdrState(
249
321
  ...(input.mode !== undefined && input.mode.length > 0 ? { mode: input.mode } : {}),
250
322
  };
251
323
  try {
252
- const path = featureAdrStatePath(root);
253
- mkdirSync(dirname(path), { recursive: true });
324
+ const dir = featureAdrStateDir(root);
325
+ mkdirSync(dir, { recursive: true });
326
+
327
+ // Housekeeping belongs only on this write path, never the ~300ms render path. Every file is
328
+ // independently guarded so an unreadable/racing entry cannot prevent the live state write.
329
+ try {
330
+ const cutoff = Date.now() - 24 * 60 * 60 * 1_000;
331
+ for (const name of readdirSync(dir)) {
332
+ if (!name.endsWith('.json')) continue;
333
+ const stalePath = join(dir, name);
334
+ try {
335
+ if (statSync(stalePath).mtimeMs < cutoff) unlinkSync(stalePath);
336
+ } catch { /* best-effort per-file cleanup */ }
337
+ }
338
+ } catch { /* best-effort directory cleanup */ }
339
+
340
+ const path = featureAdrStatePath(root, input.slug);
254
341
  writeFileSync(path, `${JSON.stringify(state, null, 2)}\n`);
255
342
  } catch {
256
343
  return undefined;
@@ -0,0 +1,258 @@
1
+ /**
2
+ * Cross-process exclusive lock for the JSON pattern store — built on `proper-lockfile`.
3
+ *
4
+ * WHY THIS EXISTS. The JSON backend writes ATOMICALLY (temp file + rename), so no reader
5
+ * ever sees a torn file. That is a different property from the one needed here. A teach is
6
+ * a read-modify-write: load every record, add one, write them all back. Two teaches that
7
+ * overlap both load the same N records, and each writes back N+1 — the second rename wins
8
+ * and the first lesson is gone. BOTH processes report success, so a lost lesson is silent.
9
+ * Atomicity prevents corruption; only mutual exclusion prevents a lost update.
10
+ *
11
+ * The SQLite backend has its own transaction locking and does not go through here.
12
+ *
13
+ * WHY `proper-lockfile` AND NOT THE PREVIOUS HAND-ROLLED LOCK. An independent review
14
+ * (Codex gpt-5.6-sol) graded the hand-rolled `wx`-file lock F. Its findings, and how this
15
+ * implementation answers them:
16
+ *
17
+ * 1. *Stale-break deleted a successor's live lock* — the old code `rmSync`'d a lock file
18
+ * by PATHNAME after deciding it was stale, so two waiters breaking the same stale lock
19
+ * could delete each other's freshly created locks in a cascade. `proper-lockfile`
20
+ * acquires with an atomic `mkdir`, and a waiter that breaks a stale lock retries with
21
+ * staleness DISABLED, so per acquisition attempt it can remove at most the one lock it
22
+ * observed as stale — never a successor's fresh lock in a loop. The residual
23
+ * microsecond window (stat-stale → holder replaced → rmdir) is closed by DETECTION:
24
+ * every holder heartbeats its lock's mtime and treats a foreign mtime as ECOMPROMISED,
25
+ * which {@link withStoreLock} converts into a loud error instead of a silent lost
26
+ * update. (This is also the model the RVF spec uses for `.rvf` writer locks:
27
+ * single-writer advisory lock file + ownership verification before unlink —
28
+ * `ruvector/docs/research/rvf/spec/09-concurrency-versioning.md`.)
29
+ *
30
+ * 2. *"Unparseable means dead" deleted live locks* — there is no lock BODY to parse any
31
+ * more. The lock is a directory; its existence is the lock, so the empty-file /
32
+ * half-written-file misclassification class is structurally gone.
33
+ *
34
+ * 3. *Age was mistaken for liveness* — staleness is now filesystem mtime plus a HEARTBEAT:
35
+ * the holder refreshes the lock's mtime on a timer (every `staleMs / 2`, ≥ 1s), so a
36
+ * holder slower than `staleMs` is never broken while alive, and a crashed holder's lock
37
+ * stops being refreshed and is reclaimed after `staleMs`. Nothing trusts a
38
+ * self-reported timestamp, so a forged far-future `ts` can no longer block forever.
39
+ * NOTE: the heartbeat runs on the event loop, so a SYNCHRONOUS critical section longer
40
+ * than `staleMs` could still be observed as stale — keep {@link withStoreLockSync}
41
+ * bodies short (they are: single-file JSON rewrites) or raise `staleMs`.
42
+ *
43
+ * 4. *The timeout was not an acquisition deadline* — the retry loops below compute a real
44
+ * deadline (`now + timeoutMs`), re-check it after EVERY sleep, clamp the last sleep to
45
+ * the remaining budget, and never acquire after the deadline has passed.
46
+ *
47
+ * 5. *The lock was not store-wide* — every JSON-tier writer in `patterns.ts` now runs
48
+ * under this lock (see the writer inventory in that module).
49
+ *
50
+ * On timeout this THROWS ({@link StoreLockTimeoutError}) rather than proceeding unlocked.
51
+ * Proceeding would restore exactly the silent lost-update this exists to stop, and a teach
52
+ * that failed loudly can be retried — one that vanished cannot.
53
+ *
54
+ * MIGRATION: the pre-`proper-lockfile` implementation left a regular FILE at
55
+ * `.dz/store.lock`; the library's lock is a DIRECTORY at the same path. A leftover file
56
+ * from a crashed old process is removed only once it is older than `staleMs` — a fresh
57
+ * one is honoured as held, same as any other lock.
58
+ */
59
+
60
+ import { mkdirSync, rmSync, statSync } from 'node:fs';
61
+ import { join, resolve } from 'node:path';
62
+ import { lockSync } from 'proper-lockfile';
63
+
64
+ /** A lock whose HEARTBEAT stopped this long ago is presumed abandoned (crashed holder). */
65
+ export const STALE_LOCK_MS = 30_000;
66
+ /** Give up waiting after this long and say so, rather than blocking a CLI forever. */
67
+ export const LOCK_TIMEOUT_MS = 10_000;
68
+
69
+ /** `proper-lockfile` silently clamps `stale` up to this minimum. */
70
+ const MIN_STALE_MS = 2_000;
71
+
72
+ /** Path of the lock guarding a project's pattern store (a DIRECTORY when held). */
73
+ export function storeLockPath(projectRoot: string): string {
74
+ return join(projectRoot, '.dz', 'store.lock');
75
+ }
76
+
77
+ /** Options accepted by {@link withStoreLock} / {@link withStoreLockSync}. */
78
+ export interface StoreLockOptions {
79
+ /** Heartbeat-stopped threshold before a lock is presumed abandoned (min 2000, lib-enforced). */
80
+ readonly staleMs?: number;
81
+ /** Acquisition deadline. Also overridable via `DZ_STORE_LOCK_TIMEOUT_MS` (tests/ops). */
82
+ readonly timeoutMs?: number;
83
+ /** Delay between acquisition attempts while the lock is held by someone else. */
84
+ readonly pollMs?: number;
85
+ }
86
+
87
+ /** Acquisition gave up at the deadline. Nothing was written; the operation can be retried. */
88
+ export class StoreLockTimeoutError extends Error {
89
+ readonly code = 'ELOCKTIMEOUT';
90
+ constructor(lockPath: string, waitedMs: number) {
91
+ super(
92
+ `the pattern store at ${lockPath} stayed locked for ${waitedMs}ms — ` +
93
+ 'another dz process is still writing. Nothing was written; retry.',
94
+ );
95
+ this.name = 'StoreLockTimeoutError';
96
+ }
97
+ }
98
+
99
+ /**
100
+ * The holder detected that its lock was broken out from under it (ECOMPROMISED) — e.g. a
101
+ * waiter reclaimed the lock after this process was suspended past `staleMs`. The critical
102
+ * section DID run, but it may have raced another writer, so the operation must be treated
103
+ * as failed and retried (store writes are idempotent by deterministic record id).
104
+ */
105
+ export class StoreLockCompromisedError extends Error {
106
+ readonly code = 'ECOMPROMISED';
107
+ constructor(lockPath: string, cause: Error) {
108
+ super(`the pattern store lock at ${lockPath} was compromised while held (${cause.message}) — retry the operation.`);
109
+ this.name = 'StoreLockCompromisedError';
110
+ }
111
+ }
112
+
113
+ const defaultTimeoutMs = (): number => {
114
+ const env = Number(process.env['DZ_STORE_LOCK_TIMEOUT_MS']);
115
+ return Number.isFinite(env) && env > 0 ? env : LOCK_TIMEOUT_MS;
116
+ };
117
+
118
+ const sleep = (ms: number): Promise<void> => new Promise((r) => { setTimeout(r, ms); });
119
+
120
+ /** Synchronous sleep without spinning (Atomics.wait on a throwaway buffer). */
121
+ const sleepSync = (ms: number): void => {
122
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
123
+ };
124
+
125
+ /**
126
+ * Remove a REGULAR-FILE lock left by the pre-`proper-lockfile` implementation, but only
127
+ * once it is stale — a fresh legacy lock belongs to a live old-version process and is
128
+ * honoured. (Never deletes a directory: that is a live library lock.)
129
+ */
130
+ function clearStaleLegacyLockFile(lockPath: string, staleMs: number): void {
131
+ try {
132
+ const st = statSync(lockPath);
133
+ if (st.isFile() && Date.now() - st.mtimeMs >= staleMs) rmSync(lockPath, { force: true });
134
+ } catch { /* gone already — fine */ }
135
+ }
136
+
137
+ interface Acquired {
138
+ readonly release: () => void;
139
+ readonly compromise: () => Error | undefined;
140
+ }
141
+
142
+ /**
143
+ * One acquisition attempt. Returns `'held'` when someone else holds the lock (retry later);
144
+ * throws on real I/O errors. `mkdir`-based, so creation is atomic; staleness is heartbeat
145
+ * mtime, and the library's own stale-break retries with staleness disabled (see header §1).
146
+ */
147
+ function tryAcquire(projectRoot: string, staleMs: number, onCompromised: (e: Error) => void): Acquired | 'held' {
148
+ const lockPath = storeLockPath(projectRoot);
149
+ try {
150
+ const release = lockSync(resolve(projectRoot, '.dz', 'store'), {
151
+ lockfilePath: lockPath,
152
+ stale: staleMs,
153
+ realpath: false, // the guarded resource is a concept, not an existing file
154
+ onCompromised,
155
+ });
156
+ return {
157
+ release: () => {
158
+ try {
159
+ release();
160
+ } catch { /* ERELEASED after a compromise — the lock is no longer ours to remove */ }
161
+ },
162
+ compromise: () => undefined,
163
+ };
164
+ } catch (err) {
165
+ const code = (err as NodeJS.ErrnoException).code;
166
+ if (code === 'ELOCKED') return 'held';
167
+ if (code === 'ENOTDIR') {
168
+ // A stale legacy FILE lock: the library can only rmdir directories. Clear it (only
169
+ // if actually stale) and report 'held' so the caller's loop retries.
170
+ clearStaleLegacyLockFile(lockPath, staleMs);
171
+ return 'held';
172
+ }
173
+ throw err;
174
+ }
175
+ }
176
+
177
+ function resolveOpts(opts: StoreLockOptions): { staleMs: number; timeoutMs: number; pollMs: number } {
178
+ return {
179
+ staleMs: Math.max(opts.staleMs ?? STALE_LOCK_MS, MIN_STALE_MS),
180
+ timeoutMs: opts.timeoutMs ?? defaultTimeoutMs(),
181
+ pollMs: opts.pollMs ?? 25,
182
+ };
183
+ }
184
+
185
+ /**
186
+ * Run `fn` while holding the store lock, releasing it however `fn` ends.
187
+ *
188
+ * Throws {@link StoreLockTimeoutError} when the lock cannot be acquired by the deadline
189
+ * (nothing has run), and {@link StoreLockCompromisedError} when the lock was broken while
190
+ * `fn` ran (it DID run, but may have raced — retry; store writes are idempotent).
191
+ */
192
+ export async function withStoreLock<T>(
193
+ projectRoot: string,
194
+ fn: () => Promise<T>,
195
+ opts: StoreLockOptions = {},
196
+ ): Promise<T> {
197
+ const { staleMs, timeoutMs, pollMs } = resolveOpts(opts);
198
+ mkdirSync(join(projectRoot, '.dz'), { recursive: true });
199
+ const started = Date.now();
200
+ const deadline = started + timeoutMs;
201
+ let compromised: Error | undefined;
202
+ const onCompromised = (e: Error): void => { compromised = e; };
203
+ for (;;) {
204
+ const got = tryAcquire(projectRoot, staleMs, onCompromised);
205
+ if (got !== 'held') {
206
+ let result: T;
207
+ try {
208
+ result = await fn();
209
+ } finally {
210
+ got.release();
211
+ }
212
+ if (compromised !== undefined) throw new StoreLockCompromisedError(storeLockPath(projectRoot), compromised);
213
+ return result;
214
+ }
215
+ const now = Date.now();
216
+ if (now >= deadline) throw new StoreLockTimeoutError(storeLockPath(projectRoot), now - started);
217
+ await sleep(Math.min(pollMs, deadline - now));
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Synchronous {@link withStoreLock}, for the store's synchronous writers (prune / remove).
223
+ * Same deadline and failure semantics. The heartbeat cannot fire while `fn` blocks the
224
+ * event loop, so keep bodies well under `staleMs` (they are: single-file JSON rewrites).
225
+ *
226
+ * CAVEAT: do not contend with an ASYNC `withStoreLock` holder in the SAME process — the
227
+ * sync wait blocks the event loop that would release that holder, so the wait can only
228
+ * end in the loud {@link StoreLockTimeoutError} (never a lost update). Cross-process
229
+ * contention, the case this lock exists for, is unaffected.
230
+ */
231
+ export function withStoreLockSync<T>(
232
+ projectRoot: string,
233
+ fn: () => T,
234
+ opts: StoreLockOptions = {},
235
+ ): T {
236
+ const { staleMs, timeoutMs, pollMs } = resolveOpts(opts);
237
+ mkdirSync(join(projectRoot, '.dz'), { recursive: true });
238
+ const started = Date.now();
239
+ const deadline = started + timeoutMs;
240
+ let compromised: Error | undefined;
241
+ const onCompromised = (e: Error): void => { compromised = e; };
242
+ for (;;) {
243
+ const got = tryAcquire(projectRoot, staleMs, onCompromised);
244
+ if (got !== 'held') {
245
+ let result: T;
246
+ try {
247
+ result = fn();
248
+ } finally {
249
+ got.release();
250
+ }
251
+ if (compromised !== undefined) throw new StoreLockCompromisedError(storeLockPath(projectRoot), compromised);
252
+ return result;
253
+ }
254
+ const now = Date.now();
255
+ if (now >= deadline) throw new StoreLockTimeoutError(storeLockPath(projectRoot), now - started);
256
+ sleepSync(Math.min(pollMs, deadline - now));
257
+ }
258
+ }
package/src/workflows.ts CHANGED
@@ -1,123 +1,24 @@
1
1
  /**
2
- * Dynamic workflow templates for Opus 4.8+ orchestration.
2
+ * RETIRED — the ADR-005 workflow templates are gone (feature loop-designer, AM-6 / architecture
3
+ * §8.2). They emitted a pre-`meta`/`phases` format (`export default {tasks, maxConcurrency}`) the
4
+ * current Workflow runtime cannot execute — a broken command, not a feature.
3
5
  *
4
- * Per ADR-005: workflows live in the orchestration layer only.
5
- * They generate JS scripts that Claude Code's dynamic workflow engine executes.
6
- * Core schema, adapters, and skill content remain model-agnostic.
6
+ * Replacement: `dz workflow init` (scaffold a `loop-plan/1` plan), `dz workflow validate`,
7
+ * `dz workflow render` (plan executable region-delimited loop script), plus the sibling gates
8
+ * `dz workflow-lint` and `dz workflow-trace`. See `loop-plan.ts` / `loop-render.ts`.
7
9
  *
8
- * @packageDocumentation
10
+ * BREAKING (deliberate, channeled): removing `WorkflowTemplate`/`WORKFLOWS`/`getWorkflow` is a
11
+ * public API change for harness-core. harness-core is 0.x, where MINOR is this repo's breaking
12
+ * channel; the removal is named in the CHANGELOG and README — never silent. The only internal
13
+ * importer was `harness-cli`'s `cmdWorkflow` (MEASURED at plan time), rewritten in the same change.
14
+ *
15
+ * This module stays as a deprecation shim so stale imports fail LOUDLY with a pointer, not with a
16
+ * confusing "module not found".
9
17
  */
10
18
 
11
- /** A workflow template generates a JS orchestration script from parameters. */
12
- export interface WorkflowTemplate {
13
- readonly name: string;
14
- readonly description: string;
15
- readonly generate: (options: WorkflowOptions) => string;
16
- }
17
-
18
- /** Options passed to workflow generation. */
19
- export interface WorkflowOptions {
20
- readonly projectRoot: string;
21
- readonly dryRun?: boolean;
22
- readonly packages?: readonly string[];
23
- }
24
-
25
- const coverageLift: WorkflowTemplate = {
26
- name: 'coverage-lift',
27
- description: 'Lift Tier-A package coverage to ≥95% using parallel agents per package.',
28
- generate: (opts) => `
29
- // Dynamic Workflow: Coverage Lift
30
- // Generated for: ${opts.projectRoot}
31
- // Per ADR-005: orchestration only — no adapter/core changes
32
-
33
- const tierA = ${JSON.stringify(opts.packages ?? [
34
- 'core', 'memory', 'harness-core', 'harness-cli',
35
- 'harness-presets', 'mcp-server-tools',
36
- 'adapter-claude', 'adapter-codex', 'adapter-opencode', 'adapter-hermes',
37
- 'skills-qe',
38
- ])};
39
-
40
- const tasks = tierA.map(pkg => ({
41
- name: \`coverage-\${pkg}\`,
42
- description: \`Lift @dzhechkov/\${pkg} to ≥95% line coverage. Run: pnpm --filter @dzhechkov/\${pkg} test -- --coverage. Add tests for uncovered lines.\`,
43
- }));
44
-
45
- // Claude Code dynamic workflow engine will:
46
- // 1. Spawn one agent per task (up to 16 concurrent)
47
- // 2. Each agent reads coverage, writes tests, re-runs coverage
48
- // 3. Results aggregated when all agents complete
49
- export default { tasks, maxConcurrency: 4${opts.dryRun ? ', dryRun: true' : ''} };
50
- `.trim(),
51
- };
52
-
53
- const mutationKill: WorkflowTemplate = {
54
- name: 'mutation-kill',
55
- description: 'Kill surviving mutants across core packages using parallel Stryker runs.',
56
- generate: (opts) => `
57
- // Dynamic Workflow: Mutation Kill
58
- // Generated for: ${opts.projectRoot}
59
-
60
- const packages = ${JSON.stringify(opts.packages ?? ['core', 'memory', 'harness-core'])};
61
-
62
- const tasks = packages.map(pkg => ({
63
- name: \`mutate-\${pkg}\`,
64
- description: \`Run Stryker on @dzhechkov/\${pkg}, analyze survivors, write tests to kill them. Target: ≥80% mutation score.\`,
65
- }));
66
-
67
- export default { tasks, maxConcurrency: 3${opts.dryRun ? ', dryRun: true' : ''} };
68
- `.trim(),
69
- };
70
-
71
- const canonicalize: WorkflowTemplate = {
72
- name: 'canonicalize',
73
- description: 'Canonicalize packages from vendored directories into @dzhechkov/* namespace.',
74
- generate: (opts) => `
75
- // Dynamic Workflow: Canonicalize
76
- // Generated for: ${opts.projectRoot}
77
-
78
- const tasks = [
79
- { name: 'discover', description: 'Scan vendored directories for un-canonicalized packages.' },
80
- { name: 'copy', description: 'Copy discovered packages to packages/@dzhechkov/. Exclude node_modules, .git, runtime state.' },
81
- { name: 'metadata', description: 'Add publishConfig, repository, homepage to each package.json.' },
82
- { name: 'verify', description: 'Run byte-level diff between source and canonical. 0 missing, 0 changed.' },
83
- { name: 'test', description: 'Run canonical-packages structural tests. All must pass.' },
84
- ];
85
-
86
- export default { tasks, maxConcurrency: 2${opts.dryRun ? ', dryRun: true' : ''} };
87
- `.trim(),
88
- };
89
-
90
- const securityAudit: WorkflowTemplate = {
91
- name: 'security-audit',
92
- description: 'Run adversarial security audit with parallel boundary scanning.',
93
- generate: (opts) => `
94
- // Dynamic Workflow: Security Audit
95
- // Generated for: ${opts.projectRoot}
96
-
97
- const tasks = [
98
- { name: 'npm-audit', description: 'Run pnpm audit, capture baseline.' },
99
- { name: 'gitleaks', description: 'Run gitleaks detect, document findings.' },
100
- { name: 'boundaries', description: 'Verify all 8 input boundaries have runtime guards.' },
101
- { name: 'payloads', description: 'Test aidefence payloads against boundaries.' },
102
- { name: 'report', description: 'Generate security-audit.md with 6 H2 sections.' },
103
- ];
104
-
105
- export default { tasks, maxConcurrency: 3${opts.dryRun ? ', dryRun: true' : ''} };
106
- `.trim(),
107
- };
108
-
109
- /** All registered workflow templates. */
110
- export const WORKFLOWS: Record<string, WorkflowTemplate> = {
111
- 'coverage-lift': coverageLift,
112
- 'mutation-kill': mutationKill,
113
- 'canonicalize': canonicalize,
114
- 'security-audit': securityAudit,
115
- };
116
-
117
- /** Valid workflow names. */
118
- export const WORKFLOW_NAMES = Object.keys(WORKFLOWS);
19
+ /** The pinned retirement message the CLI prints (tested by workflow-legacy-shim.test.ts). */
20
+ export const WORKFLOW_TEMPLATES_RETIRED_MESSAGE =
21
+ 'dz workflow: the ADR-005 templates are retired (they emitted a pre-meta format the runtime cannot run) — use dz workflow init/render';
119
22
 
120
- /** Look up a workflow by name. */
121
- export function getWorkflow(name: string): WorkflowTemplate | undefined {
122
- return WORKFLOWS[name];
123
- }
23
+ /** Empty by design: no legacy template names remain. */
24
+ export const WORKFLOW_NAMES: string[] = [];