@dzhechkov/harness-core 0.8.33 → 0.8.35

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 (53) hide show
  1. package/.dz-manifest.json +52 -52
  2. package/README.md +228 -5
  3. package/dist/agentdb-index.d.ts +39 -7
  4. package/dist/agentdb-index.d.ts.map +1 -1
  5. package/dist/agentdb-index.js +217 -23
  6. package/dist/agentdb-index.js.map +1 -1
  7. package/dist/apply-leg.d.ts +197 -6
  8. package/dist/apply-leg.d.ts.map +1 -1
  9. package/dist/apply-leg.js +858 -46
  10. package/dist/apply-leg.js.map +1 -1
  11. package/dist/index.d.ts +7 -6
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +9 -4
  14. package/dist/index.js.map +1 -1
  15. package/dist/mutation-gate.d.ts +19 -0
  16. package/dist/mutation-gate.d.ts.map +1 -1
  17. package/dist/mutation-gate.js +37 -1
  18. package/dist/mutation-gate.js.map +1 -1
  19. package/dist/operations.d.ts +16 -1
  20. package/dist/operations.d.ts.map +1 -1
  21. package/dist/operations.js +115 -13
  22. package/dist/operations.js.map +1 -1
  23. package/dist/publish-sibling-drift.d.ts +72 -0
  24. package/dist/publish-sibling-drift.d.ts.map +1 -1
  25. package/dist/publish-sibling-drift.js +150 -4
  26. package/dist/publish-sibling-drift.js.map +1 -1
  27. package/dist/release.d.ts +72 -0
  28. package/dist/release.d.ts.map +1 -1
  29. package/dist/release.js +236 -19
  30. package/dist/release.js.map +1 -1
  31. package/dist/setup.d.ts.map +1 -1
  32. package/dist/setup.js +90 -14
  33. package/dist/setup.js.map +1 -1
  34. package/dist/skills.d.ts +87 -3
  35. package/dist/skills.d.ts.map +1 -1
  36. package/dist/skills.js +266 -15
  37. package/dist/skills.js.map +1 -1
  38. package/dist/vector-tier.d.ts +27 -2
  39. package/dist/vector-tier.d.ts.map +1 -1
  40. package/dist/vector-tier.js +117 -4
  41. package/dist/vector-tier.js.map +1 -1
  42. package/package.json +2 -2
  43. package/sbom.json +51 -51
  44. package/src/agentdb-index.ts +223 -24
  45. package/src/apply-leg.ts +875 -46
  46. package/src/index.ts +18 -2
  47. package/src/mutation-gate.ts +58 -2
  48. package/src/operations.ts +117 -14
  49. package/src/publish-sibling-drift.ts +209 -4
  50. package/src/release.ts +263 -17
  51. package/src/setup.ts +81 -16
  52. package/src/skills.ts +303 -14
  53. package/src/vector-tier.ts +157 -5
package/src/apply-leg.ts CHANGED
@@ -27,9 +27,89 @@
27
27
  * @packageDocumentation
28
28
  */
29
29
 
30
- import { existsSync, readFileSync } from 'node:fs';
31
- import { join } from 'node:path';
30
+ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
31
+ import { connect as netConnect } from 'node:net';
32
+ // `node:os` is NOT one of the modules `countIoImports` (core-boundary.ts) tracks — free to import
33
+ // (core-boundary.test.ts's ratchet only counts fs/child_process/https). `probeApplyLeg`'s temp probe
34
+ // cwd reuses the existing top-level 'node:fs' import above (mkdtempSync/rmSync added to that SAME
35
+ // import statement, not a new one) so the ratchet stays at its pinned files:63 imports:69.
36
+ import { tmpdir } from 'node:os';
37
+ import { join, resolve } from 'node:path';
32
38
  import { hookCommandsOf } from './managed-hooks.js';
39
+ import { patternRecordId, recordPattern, removePatternsByIds, loadStorePatternsSync, type PatternRecord, type RemovePatternsResult } from './patterns.js';
40
+
41
+ /**
42
+ * FR-6 (feature `hook-recall-hybrid-parity`, ADR-001 C-4): send ONE `op: recall` probe to a LIVE
43
+ * embed daemon socket and report the `engine` it answers with (`'hybrid'` | `'cosine-fallback'`) —
44
+ * `dz doctor` prints this so an operator can SEE which engine is actually serving prompts, rather
45
+ * than trusting the daemon's mere presence. Honest-degrade contract, matching every other doctor
46
+ * probe: a non-socket path (e.g. a plain file, as every non-live doctor fixture in this repo uses),
47
+ * a connection error, an unparsable reply, or a timeout all resolve to `undefined` — NEVER a thrown
48
+ * error, and never distinguishable from "no daemon" in the caller's output (the existing "socket
49
+ * present/absent" line already carries that half of the truth).
50
+ *
51
+ * `timeoutMs` defaults to 1000 ms — comfortably above the documented `HOOK_RECALL_BUDGET_MS` default
52
+ * (500 ms): under that default, a cold `recallHybrid` semantic leg routinely exceeds the budget in
53
+ * this environment (MEASURED — see the manifest's NFR-1 discussion), so the daemon's OWN answer
54
+ * time is closer to ~500-550 ms than to the socket round-trip cost alone; a shorter probe timeout
55
+ * would silently miss a live, correctly-answering daemon and report no engine at all.
56
+ *
57
+ * AM-8 (fix round 1): lives HERE, not in `operations.ts` — this module already owns the daemon's
58
+ * wire protocol (`recallHookSource`/`embedDaemonSource`'s generated `op: recall` handshake) and its
59
+ * socket-path resolution; `operations.ts`'s `runDoctor` reaches it via a dynamic `import()`
60
+ * (matching its existing `embed-socket-path.js` import one line above the call site) rather than
61
+ * duplicating a second, independent `node:net` IO surface in a file whose job is orchestration, not
62
+ * protocol.
63
+ */
64
+ export function probeRecallEngine(socketPath: string, timeoutMs = 1000): Promise<string | undefined> {
65
+ return new Promise((resolvePromise) => {
66
+ let settled = false;
67
+ const done = (v: string | undefined): void => {
68
+ if (settled) return;
69
+ settled = true;
70
+ try {
71
+ sock.destroy();
72
+ } catch {
73
+ /* already gone */
74
+ }
75
+ resolvePromise(v);
76
+ };
77
+ let sock: ReturnType<typeof netConnect>;
78
+ try {
79
+ sock = netConnect(socketPath);
80
+ } catch {
81
+ resolvePromise(undefined);
82
+ return;
83
+ }
84
+ const timer = setTimeout(() => done(undefined), timeoutMs);
85
+ timer.unref?.();
86
+ let buf = '';
87
+ sock.on('connect', () => {
88
+ try {
89
+ sock.write(`${JSON.stringify({ op: 'recall', prompt: 'dz doctor probe', limit: 1 })}\n`);
90
+ } catch {
91
+ done(undefined);
92
+ }
93
+ });
94
+ sock.on('data', (chunk: Buffer) => {
95
+ buf += chunk.toString('utf-8');
96
+ const nl = buf.indexOf('\n');
97
+ if (nl === -1) return;
98
+ clearTimeout(timer);
99
+ try {
100
+ const msg = JSON.parse(buf.slice(0, nl)) as { engine?: unknown };
101
+ // Codex round-3: only the protocol's own vocabulary is reported; anything else is "unknown" (undefined)
102
+ done(msg.engine === 'hybrid' || msg.engine === 'cosine-fallback' || msg.engine === 'none' ? msg.engine : undefined);
103
+ } catch {
104
+ done(undefined);
105
+ }
106
+ });
107
+ sock.on('error', () => {
108
+ clearTimeout(timer);
109
+ done(undefined);
110
+ });
111
+ });
112
+ }
33
113
 
34
114
  /**
35
115
  * Version stamped into BOTH generated helper files as `// dz-apply-leg-version: N` (line 2, right
@@ -48,8 +128,70 @@ import { hookCommandsOf } from './managed-hooks.js';
48
128
  * compiled module), the daemon writes a `.dz/embed.sock.path` pointer when it picks the tmpdir-short
49
129
  * branch, and `ready` is now printed only after `existsSync(SOCKET)` confirms the bind actually
50
130
  * landed (previously logged unconditionally, before `listen` even ran).
131
+ *
132
+ * Bumped 4→5 (feature `hook-recall-hybrid-parity`, ADR-001 D1/D2): the daemon's `op: recall`
133
+ * handler now tries core's `recallHybrid` FIRST — under a time budget (`HOOK_RECALL_BUDGET_MS`,
134
+ * default 500 ms) — via the SAME `CORE_DIST_DIR` + `loadCoreModule` mechanism the hook already
135
+ * used only for its policy modules; on budget overrun, engine error, or no resolvable core module
136
+ * it falls back to today's brute-force cosine, honestly labelled `engine: 'cosine-fallback'` with a
137
+ * `reason`. The hook now reads `engine`/`reason` off the daemon's reply (stderr-only, never
138
+ * context) and applies its relevance floor to the NEW `score` format when `engine === 'hybrid'`,
139
+ * preserving today's cosine-calibrated floor unchanged for the `cosine-fallback` path.
140
+ *
141
+ * Bumped 5→6 (`hook-recall-hybrid-parity`, fix round 1 — AM-1/AM-2/AM-3/AM-5): the daemon now
142
+ * (a) fires a fire-and-forget engine warm-up before `listen()` (AM-1) so the first REAL `op: recall`
143
+ * is less likely to pay a cold `resolveAgentdbEmbedder` init; (b) arms the budget timer BEFORE
144
+ * `loadCoreModule()`, not after (AM-2, wall clock from request receipt); (c) treats ANY failure
145
+ * past the budget race — a malformed hit, `patternRecordId()` throwing — as an honest cosine
146
+ * fallback rather than a bare protocol error (AM-3); (d) reports the RAW core RRF score, unchanged,
147
+ * instead of a locally re-normalized [0,1] value (AM-5) — the hook's own `HOOK_SCORE_FLOOR` default
148
+ * moved from `0.01` to `0.005` to match (see that constant's own comment for the measurement).
149
+ *
150
+ * Bumped 6→7 (feature `apply-leg-install-root`, ADR-001 D1): both generated files now resolve
151
+ * `PROJECT` install-root-first (`INSTALL_ROOT` = this file's own location, when it owns a `.dz/`)
152
+ * instead of trusting a foreign session's `CLAUDE_PROJECT_DIR`/cwd (issue #2) — the hook's own
153
+ * `[dz-recall]` diagnostic line also gains `root=<path> (install|env|cwd)`.
154
+ *
155
+ * Bumped 7→8 (`apply-leg-install-root`, fix round 1, AM-7 HIGH — real regression MEASURED via
156
+ * `retro-debt-hook.test.ts` going 4/5 red on the v7 hub helper): `PROJECT` install-root-first is
157
+ * correct for the STORE (pattern db, socket, daemon script) but WRONG for a per-session artifact —
158
+ * the narrated-error retro-debt sentinel (`retro-pending.json`) is written by the INVOKING
159
+ * SESSION's own Stop hook under `CLAUDE_PROJECT_DIR`, not under wherever the hook happens to be
160
+ * installed; a shared $HOME install made the hook look for a foreign session's sentinel under the
161
+ * install root and silently drop every session's own debt confrontation. Split: `PROJECT` (install-
162
+ * root-first) stays the STORE root; a new `SESSION_ROOT` (`CLAUDE_PROJECT_DIR || cwd()`, the
163
+ * pre-feature resolution, unchanged) is the root for `RETRO_PENDING` — the ONE per-session file this
164
+ * hook reads (every other `PROJECT`-derived path in this file names the store, the daemon, or the
165
+ * harness-core install, confirmed by grep against every `path.join(PROJECT, …)` site). The diag line
166
+ * gains `session=<path>` alongside the existing `root=<path> (…)`.
167
+ *
168
+ * Bumped 8→9 (feature `apply-leg-never-silent`, ADR-001 D2, FR-1): every early return in `main()`
169
+ * now prints `[dz-recall] skipped reason=<store-not-found|socket-absent|core-unavailable|
170
+ * empty-prompt|no-hits> root=<path> (…) session=<path>` on stderr before returning — the hook used
171
+ * to exit silently on every one of these paths, indistinguishable (from stderr alone) from a
172
+ * correctly-quiet "nothing relevant" outcome. `embedDaemonSource`'s own bytes are UNCHANGED by this
173
+ * bump; the shared version number still advances because both helpers are upgraded as one unit by
174
+ * `dz setup`/`applyLegStatus`.
175
+ *
176
+ * Bumped 9→10 (`apply-leg-never-silent`, fix round 1 — cross-model review AM-3/AM-6): (a) the hook
177
+ * now tags its `op: recall` request with `probe: <bool>` (true only when
178
+ * `DZ_HOOK_LIVENESS_PROBE=1`, the env {@link "./operations.js".probeHookLiveness} already stamps on
179
+ * every live-probe spawn) so the daemon can tell a genuine session prompt apart from
180
+ * `probeApplyLeg`'s own beacon query — AM-3: a beacon written for the ~8s of a doctor/parity probe
181
+ * used to be recallable by ANY concurrent real prompt in the SAME project, a probe-only fixture
182
+ * leaking into a real session's context; (b) `askDaemon`'s every failure path used to collapse into
183
+ * one `undefined`, forcing the hook's own `skip('socket-absent')` call regardless of what actually
184
+ * went wrong — AM-6: it now returns a tagged `{error: 'socket-absent'|'connect-refused'|
185
+ * 'daemon-timeout'|'bad-reply'}` so the stderr reason names the ACTUAL failure (no socket file vs a
186
+ * non-socket file at that path vs a listener that never replies vs a listener that replies with
187
+ * something unparseable/shapeless). `embedDaemonSource`'s bytes also change for AM-3: `loadPatterns`
188
+ * now reads each row's `domain` out of the SAME metadata JSON `dzIdOf`/`quarantinedOf` already
189
+ * parse (the vector mirror carries no separate domain column), and both `hybridRecall`'s hits and
190
+ * the cosine-fallback `scored` array are filtered to exclude `domain === 'apply-leg-probe'` unless
191
+ * the request carried `probe: true` — a probe's own beacon still needs to reach ITS query, only a
192
+ * REAL prompt must never see it.
51
193
  */
52
- export const APPLY_LEG_VERSION = 4;
194
+ export const APPLY_LEG_VERSION = 10;
53
195
 
54
196
  /**
55
197
  * Parse the `dz-apply-leg-version` stamp from a deployed helper file. Unlike
@@ -139,7 +281,24 @@ const crypto = require('node:crypto');
139
281
  const os = require('node:os');
140
282
  const { pathToFileURL } = require('node:url');
141
283
 
142
- const PROJECT = process.env.CLAUDE_PROJECT_DIR || process.cwd();
284
+ // FR-1 (ADR-001 D1, apply-leg-install-root): resolve OUR OWN store from where this hook is
285
+ // INSTALLED, not from whatever project the invoking session happens to be in — a hook installed at
286
+ // $HOME (a common single-machine layout: \`dz setup --target claude --memory agentdb --project
287
+ // $HOME\`, expecting the leg everywhere) used to silently look up a DIFFERENT project's \`.dz/\`
288
+ // whenever CLAUDE_PROJECT_DIR pointed elsewhere (issue #2, MEASURED). Precedent:
289
+ // claude-hooks-assets.ts's \`path.resolve(__dirname, '..', '..')\` for the destructive-guard hook.
290
+ // Order: INSTALL_ROOT (if it owns a \`.dz/\`) -> CLAUDE_PROJECT_DIR -> cwd.
291
+ const INSTALL_ROOT = path.resolve(__dirname, '..', '..');
292
+ const ROOT_SOURCE = fs.existsSync(path.join(INSTALL_ROOT, '.dz')) ? 'install' : (process.env.CLAUDE_PROJECT_DIR ? 'env' : 'cwd');
293
+ const PROJECT = ROOT_SOURCE === 'install' ? INSTALL_ROOT : (process.env.CLAUDE_PROJECT_DIR || process.cwd());
294
+ // AM-7 (fix round 1, apply-leg-install-root): PROJECT above is the STORE root (install-first) — a
295
+ // pattern db shared across every session at a $HOME install is correctly install-scoped. A
296
+ // per-session ARTIFACT is the opposite: the retro-debt sentinel is written by THIS SESSION's own
297
+ // Stop hook under its own CLAUDE_PROJECT_DIR, so looking it up under a foreign install root finds
298
+ // nothing (or, worse, another session's leftover file) and silently drops the confrontation.
299
+ // SESSION_ROOT is the pre-feature resolution, unchanged — the root for any file that belongs to the
300
+ // INVOKING session rather than to the store.
301
+ const SESSION_ROOT = process.env.CLAUDE_PROJECT_DIR || process.cwd();
143
302
  const CORE_DIST_DIR = ${coreDistDir === null ? 'null' : JSON.stringify(coreDistDir)};
144
303
 
145
304
  // embed-socket-short-path (FR-1/FR-2): byte-for-byte the same logic as \`resolveEmbedSocketPath\` /
@@ -175,9 +334,40 @@ function resolveEffectiveEmbedSocketPath(projectRoot, env) {
175
334
  return pointer !== undefined && fs.existsSync(pointer) ? { path: pointer, reason: 'tmpdir-short' } : resolved;
176
335
  }
177
336
  const SOCKET = resolveEffectiveEmbedSocketPath(PROJECT, process.env).path;
337
+ // AM-3 (fix round 1, apply-leg-never-silent): set ONLY by probeHookLiveness (operations.ts) on
338
+ // every live-probe spawn — never by a real Claude Code session. Rides into the daemon's op:recall
339
+ // request as \`probe\` so the daemon can admit the probe's OWN beacon (domain=apply-leg-probe)
340
+ // without ever surfacing it to a concurrent real prompt in the same project.
341
+ const IS_LIVENESS_PROBE = process.env.DZ_HOOK_LIVENESS_PROBE === '1';
178
342
  const USAGE_LOG = process.env.DZ_RECALL_USAGE_LOG || path.join(PROJECT, '.dz', 'recall-usage.jsonl');
343
+ // Measured (2026-09-14, apply-leg-socket.test.ts): an ordinary hook round-trip (spawn + one socket
344
+ // op) took 81-121 ms; 800 ms leaves a wide margin for a loaded daemon while still bounding the AM-2
345
+ // worst case — a daemon synchronously blocked never replies at all, so THIS timeout (not the
346
+ // daemon's own internal budget race, which cannot preempt synchronous work) is what actually
347
+ // rescues the hook from hanging.
179
348
  const TIMEOUT_MS = Number(process.env.DZ_RECALL_HOOK_TIMEOUT_MS || 800);
180
349
 
350
+ // FR-5 (hook-recall-hybrid-parity, ADR-001 D2): the RRF-based \`score\` the daemon returns for
351
+ // \`engine: 'hybrid'\` is NOT on the cosine scale DEFAULT_RECALL_FLOORS (recall-hook-policy.ts) was
352
+ // calibrated on — applying the cosine floor to an RRF score would either admit everything or cut
353
+ // everything, so the hybrid path gets its OWN floor, applied to BOTH languages alike (the RRF score
354
+ // carries no language-baseline shift the way raw cosine did).
355
+ //
356
+ // AM-5 (fix round 1), MEASURED not a placeholder: recallHybrid(RRF_K=60) over a live 14-lesson
357
+ // fixture (this environment, 2026-09-14 — reproducer in the manifest's Fix-round 1 section) shows
358
+ // raw RRF score is only WEAKLY discriminating per-hit: an exact-lexical-match hit scored 0.03252
359
+ // (both legs agree at rank 0), but a genuinely IRRELEVANT query ("xkcd banana quantum toaster
360
+ // nonsense") still returned a top hit at 0.01639 — HIGHER than several truly relevant tail hits in
361
+ // OTHER queries (0.01471-0.01538). This is structural, not a fixture artifact: RRF encodes RANK,
362
+ // not similarity, and a nearest-neighbor search always returns SOME top-1 even for a garbage query.
363
+ // A raw-score floor therefore cannot cleanly separate signal from noise at the per-hit level the
364
+ // way the cosine floor does — true filtering here has to come from \`limit\` and \`selectHookHits\`'s
365
+ // own budget, not from this number. The floor's honest job is only to reject a DEGENERATE score
366
+ // (zero/negative/NaN from a malformed hit), so it is set well BELOW the measured noise floor
367
+ // (0.01471) rather than attempting to rank-filter — deliberately permissive, matching ADR-001 D2's
368
+ // stated intent that an exact lexical match (FR-4) must never be defeated by an unmeasured cutoff.
369
+ const HOOK_SCORE_FLOOR = Number(process.env.DZ_RECALL_HOOK_SCORE_FLOOR || 0.005);
370
+
181
371
  const safe = (fn, fb) => {
182
372
  try {
183
373
  return fn();
@@ -241,7 +431,9 @@ async function loadPolicy() {
241
431
  * - stale / other-session sentinel ⇒ '' (the debt belongs to a dead session; retro collected it);
242
432
  * - core module absent or old (no directive exports) ⇒ '' — inert, NEVER-BLOCK.
243
433
  */
244
- const RETRO_PENDING = path.join(PROJECT, '.dz', 'retro-pending.json');
434
+ // AM-7 (fix round 1): SESSION_ROOT, not PROJECT — the sentinel is a per-session artifact (see the
435
+ // SESSION_ROOT comment above).
436
+ const RETRO_PENDING = path.join(SESSION_ROOT, '.dz', 'retro-pending.json');
245
437
  async function retroDebtDirective(payload) {
246
438
  if (!fs.existsSync(RETRO_PENDING)) return '';
247
439
  const sentinel = safe(() => JSON.parse(fs.readFileSync(RETRO_PENDING, 'utf-8')), undefined);
@@ -293,9 +485,16 @@ function readLogTail(chain, file) {
293
485
  }, chain && chain.EMPTY_LOG_TAIL);
294
486
  }
295
487
 
488
+ // FR-6 (hook-recall-hybrid-parity): the reply now carries \`engine\`/\`reason\` alongside \`hits\` —
489
+ // returned as a small object rather than the bare hit array, so the caller can apply the RIGHT
490
+ // floor (FR-5) and print the engine to stderr ONLY (never into the injected context, FR-2).
491
+ // AM-6 (fix round 1): every failure used to collapse into one \`undefined\`, forcing the caller's
492
+ // \`skip('socket-absent')\` regardless of whether the socket was truly absent, refused the connect,
493
+ // never replied, or replied with garbage. Each branch now tags its OWN reason so the stderr line
494
+ // (and, through it, \`probeApplyLeg\`'s parsed reason) names what actually happened.
296
495
  function askDaemon(prompt) {
297
496
  return new Promise((resolve) => {
298
- if (!fs.existsSync(SOCKET)) return resolve(undefined);
497
+ if (!fs.existsSync(SOCKET)) return resolve({ error: 'socket-absent' });
299
498
  let settled = false;
300
499
  const done = (v) => {
301
500
  if (settled) return;
@@ -304,21 +503,29 @@ function askDaemon(prompt) {
304
503
  resolve(v);
305
504
  };
306
505
  const sock = net.connect(SOCKET);
307
- const timer = setTimeout(() => done(undefined), TIMEOUT_MS);
506
+ const timer = setTimeout(() => done({ error: 'daemon-timeout' }), TIMEOUT_MS);
308
507
  timer.unref?.();
309
508
  let buf = '';
310
- sock.on('connect', () => sock.write(JSON.stringify({ op: 'recall', prompt, limit: 8 }) + '\\n'));
509
+ sock.on('connect', () => sock.write(JSON.stringify({ op: 'recall', prompt, limit: 8, probe: IS_LIVENESS_PROBE }) + '\\n'));
311
510
  sock.on('data', (chunk) => {
312
511
  buf += chunk.toString('utf8');
313
512
  const nl = buf.indexOf('\\n');
314
513
  if (nl === -1) return;
315
514
  clearTimeout(timer);
316
515
  const msg = safe(() => JSON.parse(buf.slice(0, nl)), undefined);
317
- done(msg && Array.isArray(msg.hits) ? msg.hits : undefined);
516
+ if (msg && Array.isArray(msg.hits)) {
517
+ done({
518
+ hits: msg.hits,
519
+ engine: typeof msg.engine === 'string' ? msg.engine : undefined,
520
+ reason: typeof msg.reason === 'string' ? msg.reason : undefined,
521
+ });
522
+ } else {
523
+ done({ error: 'bad-reply' });
524
+ }
318
525
  });
319
526
  sock.on('error', () => {
320
527
  clearTimeout(timer);
321
- done(undefined);
528
+ done({ error: 'connect-refused' });
322
529
  });
323
530
  });
324
531
  }
@@ -326,6 +533,13 @@ function askDaemon(prompt) {
326
533
  const REVIVE_LOCK = path.join(PROJECT, '.dz', 'embed-daemon.lock');
327
534
  const REVIVE_LOCK_FRESH_MS = 120_000; // model load takes ~45s; don't respawn while one is coming up
328
535
  function reviveDaemon() {
536
+ // AM-7 (fix round 1, test-only): a test that probes the SAME dead fixture multiple times in quick
537
+ // succession (probeApplyLeg directly, then again through runDoctor, then again through a
538
+ // 'dz parity' subprocess) used to race against this very self-heal — the first probe's revive
539
+ // could finish loading a real daemon before the second or third probe ran, flipping
540
+ // "socket-absent" into "hybrid"/"cosine-fallback" non-deterministically. No production session
541
+ // ever sets this.
542
+ if (process.env.DZ_RECALL_NO_REVIVE === '1') return;
329
543
  // CROSS-PROCESS lock: every prompt runs a fresh hook process, so a per-process flag let 20 queued
330
544
  // prompts spawn 20 daemons while the first was still loading its model (Codex #5). A lockfile with
331
545
  // a freshness window means at most one spawn per window, machine-wide.
@@ -491,6 +705,17 @@ function emitContext(context) {
491
705
  );
492
706
  }
493
707
 
708
+ // FR-1 (ADR-001 D2, apply-leg-never-silent): every silent early exit below now names WHY, on
709
+ // stderr, one line, same shape as the existing \`[dz-recall] engine=…\` diagnostic. Exit code stays
710
+ // 0 — a broken/empty/quiet hook must never fail a prompt (NEVER-BLOCK, unchanged). The reason is
711
+ // for TWO readers, neither of which is "the user watching Claude Code's own transcript" (FR-2's
712
+ // own manifest names why that channel does not apply to this hook): (1) \`dz doctor\`'s live probe,
713
+ // which spawns this exact command as a child process and reads ITS OWN child's stderr directly —
714
+ // unmediated by Claude Code's UI, so the redirect policy of any particular hook EVENT is moot; and
715
+ // (2) a human running the command by hand from a terminal, who sees stderr exactly as printed.
716
+ const skip = (reason) =>
717
+ safe(() => process.stderr.write(\`[dz-recall] skipped reason=\${reason} root=\${PROJECT} (\${ROOT_SOURCE}) session=\${SESSION_ROOT}\\n\`));
718
+
494
719
  async function main() {
495
720
  const raw = readStdin();
496
721
  const payload = safe(() => JSON.parse(String(raw || '').trim()), undefined);
@@ -501,13 +726,34 @@ async function main() {
501
726
  // sentinel is absent this is one existsSync and debt === '' (byte-identical outputs to before).
502
727
  const debt = await retroDebtDirective(payload);
503
728
 
504
- if (prompt === '') return emitContext(debt);
729
+ if (prompt === '') {
730
+ skip('empty-prompt');
731
+ return emitContext(debt);
732
+ }
733
+
734
+ // FR-1: the most fundamental silent failure (issue #2) — no \`.dz/\` at all under the resolved
735
+ // PROJECT root. Checked BEFORE the policy/daemon legs below: a missing store makes every
736
+ // downstream question ("is the daemon alive?") moot, and printing THIS reason first is what let
737
+ // the original issue's symptom (four green checks, a store that was never there) be diagnosed
738
+ // from stderr alone.
739
+ if (!fs.existsSync(path.join(PROJECT, '.dz'))) {
740
+ skip('store-not-found');
741
+ return emitContext(debt);
742
+ }
505
743
 
506
744
  const policy = await loadPolicy();
507
- if (!policy) return emitContext(debt);
745
+ if (!policy) {
746
+ skip('core-unavailable');
747
+ return emitContext(debt);
748
+ }
508
749
 
509
- const hits = await askDaemon(prompt);
510
- if (!hits) {
750
+ const daemonReply = await askDaemon(prompt);
751
+ // AM-6: the tagged reason IS the diagnosis now — 'socket-absent' (no file), 'connect-refused' (a
752
+ // file exists but nothing answers like a daemon), 'daemon-timeout' (something answers, never in
753
+ // time) and 'bad-reply' (answers, unparseable/shapeless) are four DIFFERENT defects with four
754
+ // different remedies; collapsing them back into one string is exactly the finding this fixes.
755
+ if (daemonReply.error) {
756
+ skip(daemonReply.error);
511
757
  // SELF-HEAL (2026-07-28): the daemon is started at SessionStart only, so when it dies mid-way
512
758
  // through a long-lived session NOTHING restarts it — the apply leg was silently dead for 19
513
759
  // days (MEASURED: recall-usage.jsonl last record 2026-07-09, socket absent). Spawn it
@@ -515,9 +761,22 @@ async function main() {
515
761
  reviveDaemon();
516
762
  return emitContext(debt);
517
763
  }
518
- if (hits.length === 0) return emitContext(debt); // daemon alive, nothing relevant — silence is correct
764
+ const { hits, engine, reason } = daemonReply;
765
+ // FR-6/FR-2: the engine (and, on fallback, why) is the caller's business, not the model's — it
766
+ // NEVER rides into additionalContext, only stderr, which Claude Code does not read as context.
767
+ if (typeof engine === 'string') {
768
+ safe(() => process.stderr.write(\`[dz-recall] engine=\${engine}\${reason ? \` reason=\${reason}\` : ''} root=\${PROJECT} (\${ROOT_SOURCE}) session=\${SESSION_ROOT}\\n\`));
769
+ }
770
+ if (hits.length === 0) {
771
+ skip('no-hits');
772
+ return emitContext(debt); // daemon alive, nothing relevant — silence is correct
773
+ }
519
774
 
520
- const selection = policy.selectHookHits(prompt, hits);
775
+ // FR-5 (ADR-001 D2): a hybrid-engine reply carries an RRF-based score — its OWN floor, applied to
776
+ // both languages. A cosine-fallback reply (or an old daemon that never sent \`engine\` at all)
777
+ // keeps today's cosine-calibrated DEFAULT_RECALL_FLOORS untouched.
778
+ const floorOpts = engine === 'hybrid' ? { floors: { ru: HOOK_SCORE_FLOOR, en: HOOK_SCORE_FLOOR } } : {};
779
+ const selection = policy.selectHookHits(prompt, hits, floorOpts);
521
780
  // lesson-quarantine AM-2: an excluded hypothesis is logged, never a silent context shrink.
522
781
  if (typeof selection.quarantinedExcluded === 'number' && selection.quarantinedExcluded > 0) {
523
782
  try {
@@ -577,13 +836,22 @@ export function resolveIdleMs(raw: number): ResolvedIdleMs {
577
836
 
578
837
  /**
579
838
  * Generate `.claude/helpers/dz-embed-daemon.mjs`. Behaviourally identical to the pre-existing
580
- * hand-committed hub file except for: the version stamp (new, line 2) and `resolveDeps()`, which
581
- * now tries `@huggingface/transformers` before falling back to `@xenova/transformers` — AM-3,
839
+ * hand-committed hub file except for: the version stamp (new, line 2), `resolveDeps()`, which
840
+ * tries `@huggingface/transformers` before falling back to `@xenova/transformers` — AM-3,
582
841
  * dz-harness-hub issue #10 defect 3: `agentdb >= 3.0.0-alpha` depends on the former, and an older
583
842
  * agentdb install still carries the latter, so probing only one name silently starved the daemon
584
- * on either side of that agentdb version boundary.
843
+ * on either side of that agentdb version boundary — and (feature `hook-recall-hybrid-parity`,
844
+ * ADR-001 D1) the `op: recall` handler, which now tries core's `recallHybrid` under a time budget
845
+ * before falling back to the brute-force cosine below.
846
+ *
847
+ * `coreDistDir` (new parameter, ADR-001 D1) is baked in exactly like {@link recallHookSource}'s own
848
+ * parameter of the same name — the FIRST resolve candidate for `loadCoreModule`. `null` (the
849
+ * default, and what every existing zero-arg call site gets) is the same PORTABLE marker
850
+ * `recallHookSource(null)` uses: `loadCoreModule` falls through to the project-relative fallback
851
+ * candidates, resolved from `DZ_PROJECT_ROOT`/`cwd()` at daemon RUNTIME, which is correct in any
852
+ * clone and for any consumer whose `harness-core` install is reachable under its own project tree.
585
853
  */
586
- export function embedDaemonSource(): string {
854
+ export function embedDaemonSource(coreDistDir: string | null = null): string {
587
855
  return `#!/usr/bin/env node
588
856
  // dz-apply-leg-version: ${APPLY_LEG_VERSION}
589
857
  /**
@@ -605,11 +873,18 @@ export function embedDaemonSource(): string {
605
873
  * - it exits on SIGINT/SIGTERM/SIGHUP and unlinks its socket.
606
874
  *
607
875
  * PROTOCOL — newline-delimited JSON over a unix socket:
608
- * → {"op":"recall","prompt":"…","limit":8} ← {"hits":[{"dzId","pattern","score","domain"}]}
876
+ * → {"op":"recall","prompt":"…","limit":8} ← {"hits":[{"dzId","pattern","score","domain"}],"engine":"hybrid"|"cosine-fallback","reason"?}
609
877
  * → {"op":"ping"} ← {"ok":true,"model":"…","uptimeMs":N}
610
878
  * → {"op":"stop"} ← {"ok":true} (then exits)
611
879
  *
612
- * \`score\` is COSINE RELEVANCE in [0,1] — never the teaching reward. The caller applies the floor.
880
+ * ADR-001 (feature \`hook-recall-hybrid-parity\`): \`op: recall\` first tries core's \`recallHybrid\`
881
+ * (same engine \`dz recall\` uses) under \`HOOK_RECALL_BUDGET_MS\` (default 500 ms, < the hook's own
882
+ * 800 ms timeout); on budget overrun, engine error, or no resolvable core module it falls back to
883
+ * the brute-force cosine below. \`score\` on \`engine:"hybrid"\` is the RAW core RRF score, UNCHANGED
884
+ * (AM-5, fix round 1) — the exact same number \`dz recall --json\` reports as \`relevance\`, never
885
+ * locally re-normalized; on \`engine:"cosine-fallback"\` it is COSINE RELEVANCE in [0,1] as before —
886
+ * two DIFFERENT scales, never the teaching reward either way. The caller applies the right floor
887
+ * for whichever scale \`engine\` names.
613
888
  */
614
889
 
615
890
  import { createServer } from 'node:net';
@@ -619,13 +894,77 @@ import { createRequire } from 'node:module';
619
894
  import { connect } from 'node:net';
620
895
  import { createHash } from 'node:crypto';
621
896
  import { tmpdir } from 'node:os';
897
+ import { pathToFileURL, fileURLToPath } from 'node:url';
622
898
 
623
899
  const log = (...a) => console.error('[dz-embed]', ...a);
624
900
 
625
901
  /** Never let a diagnostic reach stdout — the hook that spawns us may be parsing it. */
626
902
  console.log = (...a) => console.error(...a);
627
903
 
628
- const PROJECT = process.env['DZ_PROJECT_ROOT'] ?? process.cwd();
904
+ // FR-4 (ADR-001 D1, apply-leg-install-root): the SAME install-root-first order the hook uses (see
905
+ // recallHookSource's own PROJECT comment) — DZ_PROJECT_ROOT stays the TOP override for the daemon
906
+ // (a caller that explicitly names a project root always wins), then INSTALL_ROOT (this file's own
907
+ // location, when it owns a \`.dz/\`), then cwd.
908
+ const INSTALL_ROOT = dirname(dirname(fileURLToPath(import.meta.url)));
909
+ const PROJECT = process.env['DZ_PROJECT_ROOT'] ?? (existsSync(join(INSTALL_ROOT, '.dz')) ? INSTALL_ROOT : process.cwd());
910
+
911
+ // ADR-001 (hook-recall-hybrid-parity, D1): the SAME candidate-list resolution the hook uses for its
912
+ // own policy modules — the baked \`coreDistDir\` first (a real install's absolute dist path), then
913
+ // project-relative fallbacks resolved from PROJECT at RUNTIME. \`null\` (the hub's own portable
914
+ // marker, matching \`recallHookSource(null)\`) skips straight to the fallbacks. Loaded ONCE and
915
+ // memoized (\`coreModulePromise\`) — a fresh \`import()\` per recall would defeat FR-3's "opened once".
916
+ const CORE_DIST_DIR = ${coreDistDir === null ? 'null' : JSON.stringify(coreDistDir)};
917
+ let coreModulePromise;
918
+ function loadCoreModule() {
919
+ if (coreModulePromise !== undefined) return coreModulePromise;
920
+ coreModulePromise = (async () => {
921
+ const candidates = [
922
+ ...(CORE_DIST_DIR ? [join(CORE_DIST_DIR, 'index.js')] : []),
923
+ join(PROJECT, 'node_modules', '@dzhechkov', 'harness-core', 'dist', 'index.js'),
924
+ join(PROJECT, 'packages', '@dzhechkov', 'harness-core', 'dist', 'index.js'),
925
+ ];
926
+ for (const c of candidates) {
927
+ if (!existsSync(c)) continue;
928
+ try {
929
+ const mod = await import(pathToFileURL(c).href);
930
+ if (typeof mod.recallHybrid === 'function' && typeof mod.patternRecordId === 'function') return mod;
931
+ } catch {
932
+ /* try the next candidate */
933
+ }
934
+ }
935
+ return undefined;
936
+ })();
937
+ return coreModulePromise;
938
+ }
939
+
940
+ // FR-2 (hook-recall-hybrid-parity): the hybrid leg is time-boxed so a slow/cold engine can never make ONE prompt pay the full
941
+ // cost — it falls back to the warm cosine below instead. 500 ms leaves the hook's own 800 ms
942
+ // timeout (recallHookSource's TIMEOUT_MS) headroom for the socket round-trip itself.
943
+ // Measured 2026-09-14 (nfr1-measure.mjs, real 743-pattern store, warm-up on, n=100 x2): hybrid p50 60-65 ms,
944
+ // p95 100-180 ms, max 374 ms; 500 ms ≈ 3x p95 and stays under the hook's own 800 ms client timeout.
945
+ const HOOK_RECALL_BUDGET_MS = Number(process.env['HOOK_RECALL_BUDGET_MS'] || 500);
946
+ // Codex round-2 (2026-09-14): a hybrid attempt that LOST the race keeps running in the background —
947
+ // this cap keeps a burst of slow requests from stacking unbounded engine work; past it, requests answer
948
+ // with cosine at once, honestly labelled. Real cancellation needs worker isolation (backlog 14c1316b).
949
+ const HYBRID_MAX_IN_FLIGHT = (() => {
950
+ const raw = Number(process.env['HOOK_HYBRID_MAX_IN_FLIGHT'] || 2);
951
+ // Codex round-3: NaN/Infinity/0/negative must not silently disable the cap — fall back to 2.
952
+ return Number.isFinite(raw) && raw >= 1 ? Math.floor(raw) : 2;
953
+ })();
954
+ let hybridInFlight = 0;
955
+ // Test-only fault injection (AC-2): a positive value delays the hybrid leg so the budget can be
956
+ // PROVEN to fire without a real slow engine. Absent/0 in every real deployment.
957
+ const DZ_EMBED_HYBRID_DELAY_MS = Number(process.env['DZ_EMBED_HYBRID_DELAY_MS'] || 0);
958
+ // AM-5 (fix round 1): the daemon used to re-normalize recallHybrid's raw RRF score into [0,1] with
959
+ // its OWN copy of vector-tier.ts's RRF_K constant — two numbers that could silently drift apart
960
+ // (this file is standalone generated text and cannot \`import\` the compiled core constant), AND a
961
+ // scale \`dz recall --json\`'s own \`relevance\` field (cli.ts: \`relevance: … h.score …\`) never
962
+ // applies — so the hook's floor and the CLI's floor were never comparable numbers even though both
963
+ // ultimately came from the same recallHybrid() call. Fixed: the daemon now reports \`h.score\`
964
+ // UNCHANGED — the exact raw core score \`dz recall --json\` already reports as \`relevance\` — so a
965
+ // floor calibrated against one is valid against the other (AM-4's parity test asserts the two are
966
+ // literally equal, not merely proportional).
967
+
629
968
  // embed-socket-short-path (FR-1): a unix socket path has a hard platform limit on \`sun_path\`
630
969
  // (Linux 108 bytes incl. NUL, macOS 104) — a deeply nested project's \`.dz/embed.sock\` can exceed
631
970
  // it, and \`listen()\` then fails while every OTHER part of the daemon looks healthy. This mirrors
@@ -744,6 +1083,27 @@ function quarantinedOf(metadataJson) {
744
1083
  }
745
1084
  }
746
1085
 
1086
+ // AM-3 (fix round 1, apply-leg-never-silent): the vector mirror's own row carries no domain column
1087
+ // (see loadPatterns' own note below) — domain lives in the SAME metadata JSON dzIdOf/quarantinedOf
1088
+ // already parse, so this is the ONE extra field read off a column that's already in hand.
1089
+ function domainOf(metadataJson) {
1090
+ try {
1091
+ const d = JSON.parse(String(metadataJson || '{}'))?.domain;
1092
+ return typeof d === 'string' && d !== '' ? d : undefined;
1093
+ } catch {
1094
+ return undefined;
1095
+ }
1096
+ }
1097
+
1098
+ // AM-3: the ONE domain a genuine session must never see — probeApplyLeg's own beacon marker
1099
+ // (apply-leg.ts's PROBE_BEACON_DOMAIN, inlined here as text for the same reason every other shared
1100
+ // constant in this generated file is: a template string cannot import a compiled module).
1101
+ const PROBE_BEACON_DOMAIN = 'apply-leg-probe';
1102
+ /** Strip probe-domain hits from a real (non-probe) answer; a probe request passes through untouched. */
1103
+ function filterProbeHits(hits, probe) {
1104
+ return probe ? hits : hits.filter((h) => h.domain !== PROBE_BEACON_DOMAIN);
1105
+ }
1106
+
747
1107
  async function main() {
748
1108
  if (await socketAlive(SOCKET)) {
749
1109
  log('a daemon already owns', SOCKET, '— exiting');
@@ -793,7 +1153,9 @@ async function main() {
793
1153
  return rows.map((r) => {
794
1154
  const buf = r.embedding;
795
1155
  const vec = new Float32Array(buf.buffer, buf.byteOffset, buf.byteLength / 4);
796
- return { dzId: dzIdOf(r.metadata), pattern: r.pattern, vec, quarantined: quarantinedOf(r.metadata) };
1156
+ // AM-3: domain rides along so answerRecall can exclude the probe's own beacon from a real
1157
+ // session's cosine-fallback answer — the SAME metadata column dzIdOf/quarantinedOf already read.
1158
+ return { dzId: dzIdOf(r.metadata), pattern: r.pattern, vec, quarantined: quarantinedOf(r.metadata), domain: domainOf(r.metadata) };
797
1159
  });
798
1160
  } finally {
799
1161
  db.close();
@@ -803,6 +1165,120 @@ async function main() {
803
1165
  let patterns = loadPatterns();
804
1166
  let patternsAt = Date.now();
805
1167
 
1168
+ // ADR-001 (hook-recall-hybrid-parity, D1): try core's recallHybrid FIRST, budget-bounded.
1169
+ // AM-2 (fix round 1, wall clock from request receipt): the budget timer is armed BEFORE
1170
+ // \`loadCoreModule()\` runs, not after it resolves — the FIRST call's dynamic \`import()\` cost used
1171
+ // to be spent OUTSIDE the race, so a slow/cold module resolution could add its own latency on top
1172
+ // of the full \`HOOK_RECALL_BUDGET_MS\` window instead of eating into it.
1173
+ // NAMED LIMIT (AM-2): \`Promise.race\` cannot PREEMPT synchronous work — if \`core.recallHybrid\`
1174
+ // (or anything it calls) blocks the event loop synchronously, this race does not return until
1175
+ // that work finishes, budget or not; Node has no cooperative-preemption primitive for that. The
1176
+ // budget only bounds work that yields the event loop somewhere (every real I/O/await in
1177
+ // recallHybrid does). The hook's OWN client-side \`TIMEOUT_MS\` (recallHookSource, 800 ms) is the
1178
+ // actual backstop against a synchronously-blocked daemon: it times out the SOCKET, not the
1179
+ // daemon's internal race, so the hook always returns promptly even if this promise never does.
1180
+ //
1181
+ // \`hybridRecall\`'s own promise is left running past a timeout loss (never awaited a second time)
1182
+ // — its \`.catch\` below only silences a LATE rejection so a slow, eventually-failing engine call
1183
+ // can never become an unhandled-rejection crash for this long-lived process.
1184
+ async function hybridRecall(prompt, limit, probe) {
1185
+ if (hybridInFlight >= HYBRID_MAX_IN_FLIGHT) {
1186
+ return { ok: false, reason: \`hybrid saturated (\${hybridInFlight} attempt(s) still in flight, cap \${HYBRID_MAX_IN_FLIGHT})\` };
1187
+ }
1188
+ const TIMED_OUT = Symbol('timed-out');
1189
+ let timer;
1190
+ const budget = new Promise((resolve) => {
1191
+ timer = setTimeout(() => resolve(TIMED_OUT), HOOK_RECALL_BUDGET_MS);
1192
+ timer.unref?.();
1193
+ });
1194
+ hybridInFlight += 1;
1195
+ const attempt = (async () => {
1196
+ const core = await loadCoreModule();
1197
+ if (core === undefined) return { unavailable: true };
1198
+ if (DZ_EMBED_HYBRID_DELAY_MS > 0) await new Promise((r) => setTimeout(r, DZ_EMBED_HYBRID_DELAY_MS));
1199
+ const result = await core.recallHybrid(PROJECT, prompt, { limit, mode: 'hook', deferExposures: true });
1200
+ return { unavailable: false, result, core };
1201
+ })();
1202
+ // the in-flight count follows the UNDERLYING attempt, not the race: a timed-out attempt still
1203
+ // occupies its slot until it settles (that is the whole point of the cap)
1204
+ attempt.then(() => { hybridInFlight -= 1; }, () => { hybridInFlight -= 1; });
1205
+ attempt.catch(() => {}); // swallow a rejection that arrives AFTER the budget already won the race
1206
+ try {
1207
+ const raced = await Promise.race([attempt, budget]);
1208
+ clearTimeout(timer);
1209
+ if (raced === TIMED_OUT) return { ok: false, reason: \`budget exceeded (\${HOOK_RECALL_BUDGET_MS} ms)\` };
1210
+ if (raced.unavailable) return { ok: false, reason: 'core module unavailable' };
1211
+ // AM-3 (fix round 1): everything past the race — reading result.hits, a hit missing its
1212
+ // required fields, patternRecordId() throwing on a malformed pattern — is now INSIDE this
1213
+ // try, so any such failure falls back to cosine with an honest \`reason\` instead of reaching
1214
+ // the socket handler's outer catch, which used to turn it into a bare protocol {error} reply
1215
+ // (never engine:'cosine-fallback') — the exact defect this amendment fixes.
1216
+ const { result, core } = raced;
1217
+ const hits = (result.hits || []).map((h) => ({
1218
+ dzId: core.patternRecordId(h.pattern),
1219
+ pattern: h.pattern.pattern,
1220
+ score: h.score, // AM-5: raw core score, unchanged — the same number \`dz recall --json\` reports as \`relevance\`
1221
+ domain: h.pattern.domain,
1222
+ ...(h.quarantined ? { quarantined: true } : {}),
1223
+ }));
1224
+ // AM-3 (fix round 1): filter BEFORE slicing to \`limit\` — filtering after would let a probe
1225
+ // beacon that happened to rank in the top \`limit\` silently crowd out a real hit for a real
1226
+ // (non-probe) caller instead of simply being excluded from consideration.
1227
+ return { ok: true, hits: filterProbeHits(hits, probe).slice(0, limit) };
1228
+ } catch (err) {
1229
+ clearTimeout(timer);
1230
+ return { ok: false, reason: \`recallHybrid failed: \${err?.message ?? err}\` };
1231
+ }
1232
+ }
1233
+
1234
+ /** \`op: recall\`'s whole answer: hybrid first (budget-bounded), cosine fallback on ANY failure —
1235
+ * always honestly labelled with \`engine\`/\`reason\` (FR-2/FR-6). */
1236
+ async function answerRecall(prompt, limitRaw, probe) {
1237
+ const limit = Math.min(Number(limitRaw) || 8, 32);
1238
+ if (prompt.trim() === '') return { hits: [], engine: 'none', reason: 'empty prompt' }; // Codex round-2: every reply carries \`engine\`
1239
+ const hybrid = await hybridRecall(prompt, limit, probe);
1240
+ if (hybrid.ok) return { hits: hybrid.hits, engine: 'hybrid' };
1241
+ // Reload the cosine mirror if it changed on disk (a \`dz teach\` between turns) — the SAME
1242
+ // staleness window as before this feature, just checked only when actually falling back.
1243
+ if (Date.now() - patternsAt > 5000) {
1244
+ try {
1245
+ patterns = loadPatterns();
1246
+ } catch {
1247
+ /* keep the previous snapshot */
1248
+ }
1249
+ patternsAt = Date.now();
1250
+ }
1251
+ if (patterns.length === 0) return { hits: [], engine: 'cosine-fallback', reason: hybrid.reason };
1252
+ const qv = await embed(prompt);
1253
+ const scored = patterns.map((p) => ({ dzId: p.dzId, pattern: p.pattern, score: cos(qv, p.vec), domain: p.domain, ...(p.quarantined ? { quarantined: true } : {}) }));
1254
+ scored.sort((a, b) => b.score - a.score);
1255
+ // AM-3: same domain exclusion as the hybrid leg, applied before slicing for the same reason.
1256
+ return { hits: filterProbeHits(scored, probe).slice(0, limit), engine: 'cosine-fallback', reason: hybrid.reason };
1257
+ }
1258
+
1259
+ // AM-1 (fix round 1): warm resolveAgentdbEmbedder — cached PER PROCESS since db1521ba (cold
1260
+ // ~2-3.6 s, warm ~1 ms, MEASURED, see the manifest's T8/AM-1 discussion) — OFF the request path,
1261
+ // so the first REAL \`op: recall\` is not the one that pays the cold init. Fired fire-and-forget
1262
+ // right before \`listen()\` below, never awaited by startup: this is a best-effort head start, not
1263
+ // a guarantee — a request landing in the few-hundred-ms window before it completes still pays the
1264
+ // cold cost exactly as before this amendment, and a warm-up failure (no core module, engine
1265
+ // error) is silently swallowed — never-block applies to startup exactly as it does to a request.
1266
+ // Measured: the slowest cold resolveAgentdbEmbedder init observed in this environment was 3653 ms
1267
+ // (T8 log, 2026-09-14) — 10 s leaves a wide margin without risking an unbounded warm-up hang.
1268
+ const WARMUP_TIMEOUT_MS = 10_000;
1269
+ async function warmUpHybridEngine() {
1270
+ const core = await loadCoreModule();
1271
+ if (core === undefined) return;
1272
+ const guard = new Promise((resolve) => {
1273
+ const t = setTimeout(resolve, WARMUP_TIMEOUT_MS);
1274
+ t.unref?.();
1275
+ });
1276
+ // An empty-string query still exercises the FULL semantic leg (embed + engine.search), which is
1277
+ // exactly what needs warming; recallHybrid degrades any error inside it honestly, so nothing
1278
+ // here needs its own try/catch beyond the outer .catch(() => {}) at the call site below.
1279
+ await Promise.race([core.recallHybrid(PROJECT, '', { limit: 1, mode: 'hook', deferExposures: true }), guard]);
1280
+ }
1281
+
806
1282
  let idleTimer;
807
1283
  let lastActivityAt = Date.now();
808
1284
  const touch = () => {
@@ -837,24 +1313,8 @@ async function main() {
837
1313
  sock.write(JSON.stringify({ ok: true }) + '\\n');
838
1314
  return shutdown(0);
839
1315
  } else if (msg.op === 'recall') {
840
- // Reload the mirror if it changed on disk (a \`dz teach\` between turns).
841
- if (Date.now() - patternsAt > 5000) {
842
- try {
843
- patterns = loadPatterns();
844
- } catch {
845
- /* keep the previous snapshot */
846
- }
847
- patternsAt = Date.now();
848
- }
849
1316
  const prompt = typeof msg.prompt === 'string' ? msg.prompt : '';
850
- if (prompt.trim() === '' || patterns.length === 0) {
851
- reply = { hits: [] };
852
- } else {
853
- const qv = await embed(prompt);
854
- const scored = patterns.map((p) => ({ dzId: p.dzId, pattern: p.pattern, score: cos(qv, p.vec), ...(p.quarantined ? { quarantined: true } : {}) }));
855
- scored.sort((a, b) => b.score - a.score);
856
- reply = { hits: scored.slice(0, Math.min(Number(msg.limit) || 8, 32)) };
857
- }
1317
+ reply = await answerRecall(prompt, msg.limit, msg.probe === true);
858
1318
  } else {
859
1319
  reply = { error: \`unknown op \${String(msg.op)}\` };
860
1320
  }
@@ -893,6 +1353,9 @@ async function main() {
893
1353
 
894
1354
  for (const sig of ['SIGINT', 'SIGTERM', 'SIGHUP']) process.on(sig, () => shutdown(0));
895
1355
 
1356
+ // AM-1: fire-and-forget, never awaited — bind proceeds immediately regardless of warm-up outcome.
1357
+ warmUpHybridEngine().catch(() => {});
1358
+
896
1359
  // FR-3 ("absence of a receipt is not success"): \`ready\` is printed ONLY after \`listen\`'s callback
897
1360
  // AND a fresh \`existsSync(SOCKET)\` both confirm the socket file is actually on disk — a caller
898
1361
  // that greps stderr for "ready" must never see it for a socket that silently failed to bind.
@@ -952,25 +1415,104 @@ export interface ApplyLegHookEntry {
952
1415
  readonly hooks: readonly { readonly type: 'command'; readonly command: string }[];
953
1416
  }
954
1417
 
1418
+ /**
1419
+ * POSIX single-quote a value for safe interpolation into a shell command line: wraps it in `'`,
1420
+ * escaping every embedded `'` as the standard `'\''` sequence (close quote, literal escaped quote,
1421
+ * reopen quote). Single quotes disable EVERY shell expansion — `$`, backticks, `"`, another `'` —
1422
+ * unlike a bare `"..."` interpolation, which blocks only whitespace/globbing and still lets `$`/
1423
+ * backtick content run (AM-1, fix round 1, HIGH).
1424
+ */
1425
+ function shellQuote(value: string): string {
1426
+ return `'${value.split(`'`).join(`'\\''`)}'`;
1427
+ }
1428
+
955
1429
  /**
956
1430
  * The two hook-registry entries `runSetup` merges into `.claude/settings.json` (FR-1). Commands
957
1431
  * match the hub's own `.claude/settings.json` verbatim (`grep`-diffed against it at authoring time):
958
1432
  * the recall hook is invoked with a swallowed non-zero exit (`|| true`) so a broken hook body never
959
1433
  * fails a prompt, and the embed daemon is spawned detached via `nohup` + a backgrounding `sh -c`
960
1434
  * so `SessionStart` never waits on model load.
1435
+ *
1436
+ * `installRoot` (ADR-001 D2, feature `apply-leg-install-root`): when the caller (`dz setup`) knows
1437
+ * its own install root, the commands bake it in as an ABSOLUTE path — the deployed helper already
1438
+ * bakes an absolute `CORE_DIST_DIR`, so a `${CLAUDE_PROJECT_DIR:-.}`-relative command in
1439
+ * settings.json only masked that non-portability, and broke down to `Cannot find module` (swallowed
1440
+ * by `2>/dev/null || true`) whenever `project === $HOME` and a session's own `CLAUDE_PROJECT_DIR`
1441
+ * pointed elsewhere (issue #2). Omitting `installRoot` (every pre-existing zero-arg caller — status
1442
+ * fixtures, `applyLegStatus` regression tests) keeps the original `CLAUDE_PROJECT_DIR`-relative
1443
+ * form byte for byte; `hookCommandInvokes`/`applyLegStatus` (FR-3) recognize BOTH forms as wired,
1444
+ * and `runSetup`'s `addIfMissing` (setup.ts) REPLACES a stale form with the current one in place —
1445
+ * never a second entry — on re-setup.
1446
+ *
1447
+ * Fix round 1 corrections to the absolute (`installRoot`-given) branch — the legacy zero-arg branch
1448
+ * is UNCHANGED byte for byte:
1449
+ * - AM-1 (HIGH): a caller-controlled path was interpolated RAW into shell source. A `"`, `$`,
1450
+ * backtick, or `'` in `installRoot` altered or injected commands, and the SessionStart form broke
1451
+ * outright on a `'` (it cannot be escaped inside a `'...'` body by nesting `"`). Fixed:
1452
+ * {@link shellQuote} wraps every path; SessionStart passes them as POSITIONAL ARGS (`$1`/`$2`) to
1453
+ * an INNER `sh -c` whose script text is a FIXED literal with no caller-controlled bytes, so
1454
+ * nested-quote fragility cannot arise at all.
1455
+ * - AM-3 (HIGH): the daemon used to fall back to `DZ_PROJECT_ROOT ?? installLocal`, and nothing in
1456
+ * the SessionStart command ever SET that variable — a stale inherited `DZ_PROJECT_ROOT` in the
1457
+ * parent env could win over the install root the hook itself resolves to. Fixed: the SessionStart
1458
+ * command now sets `DZ_PROJECT_ROOT="$1"` (`$1` = installRoot) explicitly, so the daemon and the
1459
+ * hook agree by construction regardless of what the parent environment happens to carry.
1460
+ * - AM-4 (LOW): a relative `installRoot` used to produce a relative command, breaking the "every
1461
+ * baked path is absolute" invariant the module's own docs claim. Fixed: `resolve()`s its input.
961
1462
  */
962
- export function applyLegHookEntries(): { readonly userPromptSubmit: ApplyLegHookEntry; readonly sessionStart: ApplyLegHookEntry } {
1463
+ export function applyLegHookEntries(
1464
+ installRoot?: string,
1465
+ ): { readonly userPromptSubmit: ApplyLegHookEntry; readonly sessionStart: ApplyLegHookEntry } {
1466
+ if (installRoot === undefined) {
1467
+ // Legacy zero-arg form — byte-identical to every pre-fix-round build. Kept only for
1468
+ // `applyLegStatus`'s upgrade-recognition tests (a pre-feature install's settings.json) and for
1469
+ // seeding "stale entry" fixtures; every real `dz setup` caller passes `opts.projectRoot`.
1470
+ return {
1471
+ userPromptSubmit: {
1472
+ hooks: [{
1473
+ type: 'command',
1474
+ // FR-2 (apply-leg-never-silent, ADR-001 D2): `2>/dev/null` removed — the hook itself now
1475
+ // names every silent exit on stderr (FR-1), and swallowing that stream at the settings.json
1476
+ // level would defeat it at the source. `|| true` stays: a broken hook body must never fail
1477
+ // the prompt.
1478
+ command: 'node "${CLAUDE_PROJECT_DIR:-.}/.claude/helpers/recall-hook.cjs" || true',
1479
+ }],
1480
+ },
1481
+ sessionStart: {
1482
+ hooks: [{
1483
+ type: 'command',
1484
+ command: "sh -c 'nohup node \"${CLAUDE_PROJECT_DIR:-.}/.claude/helpers/dz-embed-daemon.mjs\" >/dev/null 2>&1 & exit 0'",
1485
+ }],
1486
+ },
1487
+ };
1488
+ }
1489
+ // AM-4: make a relative caller input absolute so the "every baked path is absolute" invariant
1490
+ // holds regardless of what the caller passed, not merely for callers that already resolve first.
1491
+ const root = resolve(installRoot);
1492
+ const recallHookPath = `${root}/.claude/helpers/recall-hook.cjs`;
1493
+ const daemonPath = `${root}/.claude/helpers/dz-embed-daemon.mjs`;
963
1494
  return {
964
1495
  userPromptSubmit: {
965
1496
  hooks: [{
966
1497
  type: 'command',
967
- command: 'node "${CLAUDE_PROJECT_DIR:-.}/.claude/helpers/recall-hook.cjs" 2>/dev/null || true',
1498
+ // AM-1: shellQuote the WHOLE path — a bare `"..."` interpolation only blocks whitespace and
1499
+ // globbing, it still lets `$`, backticks, and a literal `"` do damage; single-quoting blocks
1500
+ // every shell expansion at once.
1501
+ // FR-2 (apply-leg-never-silent, ADR-001 D2): `2>/dev/null` removed — see the zero-arg branch's
1502
+ // comment above for why.
1503
+ command: `node ${shellQuote(recallHookPath)} || true`,
968
1504
  }],
969
1505
  },
970
1506
  sessionStart: {
971
1507
  hooks: [{
972
1508
  type: 'command',
973
- command: "sh -c 'nohup node \"${CLAUDE_PROJECT_DIR:-.}/.claude/helpers/dz-embed-daemon.mjs\" >/dev/null 2>&1 & exit 0'",
1509
+ // AM-1/AM-3: the INNER script text (`'DZ_PROJECT_ROOT="$1" nohup node "$2" …'`) is a FIXED
1510
+ // literal — no caller-controlled byte ever sits inside it, so it can never itself contain an
1511
+ // unescaped `'` that would break the outer single-quoting. `root`/`daemonPath` instead arrive
1512
+ // as POSITIONAL ARGS (`$1`/`$2`), each independently shellQuote()d for the OUTER shell that
1513
+ // parses this whole command line. AM-3: `DZ_PROJECT_ROOT="$1"` pins the daemon to THIS
1514
+ // install root explicitly — a stale value already in the parent environment can never win.
1515
+ command: `sh -c 'DZ_PROJECT_ROOT="$1" nohup node "$2" >/dev/null 2>&1 & exit 0' sh ${shellQuote(root)} ${shellQuote(daemonPath)}`,
974
1516
  }],
975
1517
  },
976
1518
  };
@@ -1046,10 +1588,27 @@ function readHelperStatus(path: string): { exists: boolean; version: number; unr
1046
1588
  * Codex, third pass). A bare mention (`echo .claude/helpers/recall-hook.cjs`) is not an invocation:
1047
1589
  * the helper path must follow a `node` word — directly, or inside the daemon's
1048
1590
  * `sh -c 'nohup node "…"'` spawn. Forward slashes only: every command dz writes uses them.
1591
+ *
1592
+ * Fix round 1 (AM-1/AM-3, apply-leg-install-root): the SessionStart command now passes its daemon
1593
+ * path as a POSITIONAL ARG (`sh -c '… node "$2" …' sh <root> <daemonPath>`) rather than interpolating
1594
+ * it textually next to `node`, so the ORIGINAL adjacency regex alone no longer matches it. A SECOND
1595
+ * recognizer accepts that shape: the command invokes `node` with a `$N`-style positional argument
1596
+ * AND carries `markerPath` as one of its own (shellQuote()d) trailing arguments — both conditions
1597
+ * together, so a foreign command that merely echoes the marker path near an unrelated `node "$1"`
1598
+ * invocation still does not count.
1049
1599
  */
1050
1600
  export function hookCommandInvokes(command: string, markerPath: string): boolean {
1051
- const invoked = new RegExp('(^|[\\s;&|(])node\\s+[\'"]?[^\\s\'"]*' + markerPath.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&'));
1052
- return invoked.test(command);
1601
+ const escapedMarker = markerPath.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&');
1602
+ const direct = new RegExp('(^|[\\s;&|(])node\\s+[\'"]?[^\\s\'"]*' + escapedMarker);
1603
+ if (direct.test(command)) return true;
1604
+ // Codex round-2 (NEW): the absolute form is POSIX-single-quoted (`node '<root>/.claude/…'`), and a
1605
+ // root may contain whitespace or `'` (written as `'\''`) — the bare `[^\s'"]*` run above stops at
1606
+ // the first space, so such an entry read as "absent" and re-setup appended a duplicate.
1607
+ const quotedDirect = new RegExp("(^|[\\s;&|(])node\\s+'(?:[^']|'\\\\'')*" + escapedMarker);
1608
+ if (quotedDirect.test(command)) return true;
1609
+ const invokesNodeWithPositional = /\bnode\s+["']?\$\d/.test(command);
1610
+ const markerAsQuotedArg = new RegExp("'[^']*" + escapedMarker + "'");
1611
+ return invokesNodeWithPositional && markerAsQuotedArg.test(command);
1053
1612
  }
1054
1613
 
1055
1614
  /**
@@ -1157,3 +1716,273 @@ export function applyLegReasonMessage(status: ApplyLegStatus): string {
1157
1716
  }
1158
1717
  return 'not installed — run dz setup --target claude-code --memory agentdb';
1159
1718
  }
1719
+
1720
+ /**
1721
+ * The ACTUAL command `.claude/settings.json` carries for the wired `UserPromptSubmit` recall hook —
1722
+ * not a reconstruction. {@link probeApplyLeg} must run exactly what a real session would run,
1723
+ * `${CLAUDE_PROJECT_DIR:-.}`-relative legacy form and all: reconstructing our own `node <path> ||
1724
+ * true` would silently stop testing the shell-expansion half of the legacy form, the exact half
1725
+ * issue #2 broke. Mirrors {@link hookWiredUnder}'s traversal (kept in lock-step: both read
1726
+ * `hooks.UserPromptSubmit[*].hooks[*].command` and recognize it via {@link hookCommandInvokes}) but
1727
+ * returns the command TEXT instead of a boolean.
1728
+ */
1729
+ function findConfiguredRecallHookCommand(root: string): string | undefined {
1730
+ try {
1731
+ const settings = JSON.parse(readFileSync(join(root, '.claude', 'settings.json'), 'utf-8')) as unknown;
1732
+ const hooksSection = (settings as { hooks?: unknown })?.hooks;
1733
+ const list = hooksSection && typeof hooksSection === 'object' ? (hooksSection as Record<string, unknown>)['UserPromptSubmit'] : undefined;
1734
+ if (!Array.isArray(list)) return undefined;
1735
+ for (const entry of list) {
1736
+ for (const cmd of hookCommandsOf(entry)) {
1737
+ if (hookCommandInvokes(cmd, '.claude/helpers/recall-hook.cjs')) return cmd;
1738
+ }
1739
+ }
1740
+ } catch {
1741
+ /* settings.json absent, unreadable, or not valid JSON — nothing to probe */
1742
+ }
1743
+ return undefined;
1744
+ }
1745
+
1746
+ /**
1747
+ * AM-4 (fix round 1, apply-leg-never-silent): the LEGACY zero-arg form ({@link applyLegHookEntries}'s
1748
+ * no-installRoot branch) reads `${CLAUDE_PROJECT_DIR:-.}` — a shell expansion that only resolves to
1749
+ * something useful from a REAL session's own cwd. Spawning it from `probeApplyLeg`'s temp "foreign"
1750
+ * cwd can never find the deployed helper by construction (the file lives at `root`'s own
1751
+ * `.claude/helpers/`, never under the temp dir), so a probe against this form would spawn a doomed
1752
+ * command and report a confusing generic failure — not a fact about whether the leg injects, only a
1753
+ * fact about the fixture being unprobeable. The absolute form ({@link shellQuote}'d installRoot)
1754
+ * never contains this literal env-expansion syntax — it bakes a resolved path instead — so a plain
1755
+ * substring check distinguishes the two without re-parsing shell grammar.
1756
+ */
1757
+ export function isLegacyRelativeRecallCommand(command: string): boolean {
1758
+ return command.includes('${CLAUDE_PROJECT_DIR');
1759
+ }
1760
+
1761
+ /** {@link probeApplyLeg}'s result — the ONE measurement `dz doctor`'s new row and `dz parity`'s
1762
+ * Self-learning cell both read (ADR-001 Decision 3, extended by `apply-leg-never-silent` D1): green
1763
+ * means OBSERVED injection, never inferred file presence. */
1764
+ export interface ApplyLegProbeResult {
1765
+ /** True ONLY when the probe's own beacon lesson came back inside `additionalContext`. */
1766
+ readonly ok: boolean;
1767
+ /** Present exactly when `ok` is `false` — taken from the hook's own `[dz-recall] skipped
1768
+ * reason=…` stderr line when present, else a best-effort description of what went wrong. */
1769
+ readonly reason?: string;
1770
+ readonly elapsedMs: number;
1771
+ }
1772
+
1773
+ /** Words a real prompt needs to clear `hasEnoughSignal` (recall-hook-policy.ts: `MIN_PROMPT_CHARS`
1774
+ * 10, `MIN_CONTENT_TOKENS` 2) — a bare unique token alone is ONE token and would be silently
1775
+ * dropped by the very floor this probe means to exercise honestly. */
1776
+ const PROBE_PROMPT_WORDS = 'apply leg live probe';
1777
+ /** Doctor/parity probes share ONE domain tag so a leaked beacon (a failed removal) is trivially
1778
+ * findable and excludable — never `dz-teach`/`general`, which would blend it into real lessons. */
1779
+ const PROBE_BEACON_DOMAIN = 'apply-leg-probe';
1780
+
1781
+ /**
1782
+ * Live, end-to-end proof that the apply leg actually injects — ADR-001 Decision 1. `applyLegStatus`
1783
+ * only proves FILES exist and are STRUCTURALLY wired (issue #2's whole defect: four green checks,
1784
+ * a leg that injected nothing in every session but one). This spawns the REAL configured hook
1785
+ * command from a TEMPORARY cwd with `CLAUDE_PROJECT_DIR` pointing at that same temp dir — the exact
1786
+ * shape of a real Claude Code session, which never runs a hook from the project root itself — and
1787
+ * asks it to recall a throwaway "beacon" lesson written into the store for the duration of the call.
1788
+ * `ok: true` only when the beacon's own SECRET token (fix round 1, AM-1 — never sent as input, only
1789
+ * stored) comes back inside `additionalContext`; every other outcome is `ok: false` with a `reason`,
1790
+ * never a silent guess.
1791
+ *
1792
+ * The beacon is written via {@link recordPattern} (the SAME lexical-store seam `dz teach` uses) and
1793
+ * removed via {@link removePatternsByIds} in a `finally` — a probe that throws, times out, or never
1794
+ * finds the leg alive still leaves the store exactly as it found it (proven by a count-before ==
1795
+ * count-after test, not merely claimed).
1796
+ *
1797
+ * `timeoutMs` bounds `probeHookLiveness`'s spawn. Measured (this environment, 2026-09-14, T1): a
1798
+ * `store-not-found`/`socket-absent` early exit returns in well under 200 ms; a live-daemon probe
1799
+ * answers in ~100-200 ms, matching ADR-001's own estimate. 8000 ms leaves roughly a 40x margin for a
1800
+ * loaded daemon without ever approaching `probeHookLiveness`'s own un-overridden 20 000 ms ceiling —
1801
+ * a genuinely dead probe still returns to `dz doctor`/`dz parity` in bounded time.
1802
+ *
1803
+ * `env` is a TEST-ONLY escape hatch (never used by `dz doctor`/`dz parity`, both call this with
1804
+ * default opts): it lets a test widen the HOOK's OWN internal socket-connect timeout
1805
+ * (`DZ_RECALL_HOOK_TIMEOUT_MS`) against a genuinely cold daemon, matching the same widening
1806
+ * `apply-leg-recall-parity.test.ts`/`apply-leg-install-root.test.ts` already apply to the daemon's
1807
+ * `HOOK_RECALL_BUDGET_MS`. Merged BEFORE `CLAUDE_PROJECT_DIR`, so a caller can never override the
1808
+ * one env var this probe's own honesty depends on.
1809
+ */
1810
+ export async function probeApplyLeg(
1811
+ root: string,
1812
+ opts: {
1813
+ timeoutMs?: number;
1814
+ env?: Readonly<Record<string, string>>;
1815
+ /** TEST-ONLY seam (AM-2, fix round 1): a stand-in for {@link removePatternsByIds} so a test can
1816
+ * force cleanup to fail WITHOUT needing to corrupt the real store mid-call. Never set by
1817
+ * `dz doctor`/`dz parity` — both call with default opts, and the real function is the default. */
1818
+ removeBeacon?: (root: string, ids: ReadonlySet<string>) => RemovePatternsResult;
1819
+ } = {},
1820
+ ): Promise<ApplyLegProbeResult> {
1821
+ const started = Date.now();
1822
+ const elapsed = (): number => Date.now() - started;
1823
+
1824
+ const status = applyLegStatus(root);
1825
+ if (!status.installed) {
1826
+ return { ok: false, reason: 'apply-leg not installed', elapsedMs: elapsed() };
1827
+ }
1828
+ const command = findConfiguredRecallHookCommand(root);
1829
+ if (command === undefined) {
1830
+ return { ok: false, reason: 'no UserPromptSubmit entry invokes recall-hook.cjs (settings.json missing or unreadable)', elapsedMs: elapsed() };
1831
+ }
1832
+ // AM-4: the legacy relative form can never be reached from a foreign cwd by construction — see
1833
+ // isLegacyRelativeRecallCommand's own doc comment. Reported BEFORE any beacon is written: there is
1834
+ // nothing to clean up for a probe that never ran.
1835
+ if (isLegacyRelativeRecallCommand(command)) {
1836
+ return { ok: false, reason: 'legacy-relative-command', elapsedMs: elapsed() };
1837
+ }
1838
+
1839
+ // AM-1 (CRITICAL, fix round 1): two INDEPENDENT tokens, not one. `queryToken` rides the PROMPT the
1840
+ // probe sends the hook — a dead/stub hook that merely echoes its own stdin back into
1841
+ // `additionalContext` makes THIS token reappear too, so it alone can never prove genuine
1842
+ // injection. `secretToken` exists ONLY inside the beacon's STORED pattern text and is never sent
1843
+ // to the hook as input — only a hook that actually queried the store and returned a matched
1844
+ // pattern's own text can produce it. `ok: true` therefore requires the SECRET, never the query.
1845
+ const queryToken = `dzapplylegquery${process.pid}${Date.now()}${Math.random().toString(36).slice(2, 10)}`;
1846
+ const secretToken = `dzapplylegsecret${process.pid}${Date.now()}${Math.random().toString(36).slice(2, 10)}`;
1847
+ const beaconPattern: PatternRecord = {
1848
+ pattern: `${PROBE_PROMPT_WORDS} ${queryToken} — dz doctor / dz parity live-probe marker, safe to remove. probe-secret=${secretToken}`,
1849
+ type: 'lesson-learned',
1850
+ reward: 0,
1851
+ domain: PROBE_BEACON_DOMAIN,
1852
+ ts: new Date().toISOString(),
1853
+ source: 'apply-leg-probe',
1854
+ };
1855
+ // Deterministic content-hash id (patternRecordId), computed from the SAME object recordPattern is
1856
+ // about to write — the id is a pure function of {pattern, ts, reward, domain, type}, so the value
1857
+ // computed here and the value the store assigns are guaranteed equal without a round-trip read.
1858
+ const beaconId = patternRecordId(beaconPattern);
1859
+ const probePrompt = `${PROBE_PROMPT_WORDS} ${queryToken}`;
1860
+ const removeBeacon = opts.removeBeacon ?? removePatternsByIds;
1861
+
1862
+ // AM-2 (HIGH, fix round 1): `wrote` is armed BEFORE the write is even attempted, and cleanup below
1863
+ // runs off `wrote` alone — a `recordPattern` call that PARTIALLY lands and then rejects used to
1864
+ // skip cleanup entirely (the old code's `finally` only wrapped the code AFTER a successful
1865
+ // `await recordPattern`), leaking the beacon forever. A cleanup FAILURE (the store refuses the
1866
+ // delete) now overrides whatever `result` the probe body computed — `ok: true` is not honest if
1867
+ // the probe cannot even prove the store is clean afterward.
1868
+ let wrote = false;
1869
+ let cleanupFailed = false;
1870
+ let cleanupErrMsg = '';
1871
+ let tempCwd: string | undefined;
1872
+ let result!: ApplyLegProbeResult;
1873
+
1874
+ // Codex round-2: a process killed mid-probe bypasses `finally`, so a beacon can outlive its probe.
1875
+ // Every probe therefore starts by SCAVENGING any beacon left behind by an earlier one (the probe
1876
+ // domain is reserved for beacons, never for user lessons) — the store is clean before AND after.
1877
+ try {
1878
+ // a loaded pattern carries the STORE's own id (`dzId`); recomputing it from normalised fields
1879
+ // (type/ts round-trip) can diverge, so the store id wins and the recomputation is the fallback.
1880
+ const stale = loadStorePatternsSync(root).filter((p) => p.domain === PROBE_BEACON_DOMAIN).map((p) => p.dzId ?? patternRecordId(p));
1881
+ if (stale.length > 0) removeBeacon(root, new Set(stale));
1882
+ } catch { /* scavenging is best-effort; the probe's own cleanup below is the accountable path */ }
1883
+
1884
+ try {
1885
+ wrote = true;
1886
+ let writeFailed: string | undefined;
1887
+ try {
1888
+ await recordPattern(root, beaconPattern);
1889
+ } catch (err) {
1890
+ writeFailed = err instanceof Error ? err.message : String(err);
1891
+ }
1892
+
1893
+ if (writeFailed !== undefined) {
1894
+ result = { ok: false, reason: `beacon write failed: ${writeFailed}`, elapsedMs: elapsed() };
1895
+ } else {
1896
+ // AM-4: a bare empty temp dir does not model a FOREIGN session — a real foreign
1897
+ // CLAUDE_PROJECT_DIR names a DIFFERENT project with its own (empty-of-lessons, but present)
1898
+ // `.dz`/`.claude` tree, not "nothing at all". This closes the gap between "no project" and "a
1899
+ // different, empty project" a bare empty dir cannot distinguish, matching what the hook's own
1900
+ // SESSION_ROOT-derived reads (e.g. the retro-debt sentinel) would see in a real foreign session.
1901
+ tempCwd = mkdtempSync(join(tmpdir(), 'dz-apply-leg-probe-'));
1902
+ mkdirSync(join(tempCwd, '.dz'), { recursive: true });
1903
+ mkdirSync(join(tempCwd, '.claude'), { recursive: true });
1904
+
1905
+ const { probeHookLiveness } = await import('./operations.js');
1906
+ const timeoutMs = opts.timeoutMs ?? 8000;
1907
+ const probeResult = probeHookLiveness(command, JSON.stringify({ prompt: probePrompt }), {
1908
+ cwd: tempCwd,
1909
+ env: { ...(opts.env ?? {}), CLAUDE_PROJECT_DIR: tempCwd },
1910
+ timeoutMs,
1911
+ });
1912
+
1913
+ const stdoutLines = probeResult.stdout.split('\n').map((l) => l.trim()).filter((l) => l !== '');
1914
+ let additionalContext: unknown;
1915
+ for (const line of stdoutLines) {
1916
+ try {
1917
+ const parsed = JSON.parse(line) as { hookSpecificOutput?: { additionalContext?: unknown } };
1918
+ if (typeof parsed?.hookSpecificOutput?.additionalContext === 'string') {
1919
+ additionalContext = parsed.hookSpecificOutput.additionalContext;
1920
+ }
1921
+ } catch {
1922
+ /* not a JSON line — the hook only ever emits at most one, but tolerate stray output */
1923
+ }
1924
+ }
1925
+
1926
+ if (typeof additionalContext === 'string' && additionalContext.includes(secretToken)) {
1927
+ result = { ok: true, elapsedMs: elapsed() };
1928
+ } else if (typeof additionalContext === 'string' && additionalContext.includes(queryToken)) {
1929
+ // AM-1: the QUERY came back but the SECRET did not — the hook (or a stub standing in for
1930
+ // it) echoed its own input instead of genuinely querying the store. Named distinctly from
1931
+ // every other red reason so a dead leg and a FAKING one never read the same.
1932
+ result = { ok: false, reason: 'echo-not-injection', elapsedMs: elapsed() };
1933
+ } else {
1934
+ // FR-1's own reason line is the authoritative source — the hook names itself why it stayed
1935
+ // quiet. Falling back to a raw stderr/status summary keeps the probe honest even against an
1936
+ // OLDER deployed hook (pre-`apply-leg-never-silent`) that has not been upgraded yet.
1937
+ const skipMatch = /\[dz-recall\] skipped reason=(\S+)/.exec(probeResult.stderr);
1938
+ const skipReason = skipMatch?.[1];
1939
+ if (skipReason !== undefined) {
1940
+ result = { ok: false, reason: skipReason, elapsedMs: elapsed() };
1941
+ } else if (probeResult.status === null) {
1942
+ result = { ok: false, reason: `probe did not complete (timeout or spawn error after ${timeoutMs} ms)`, elapsedMs: elapsed() };
1943
+ } else {
1944
+ const stderrFirstLine = probeResult.stderr.trim().split('\n')[0];
1945
+ result = {
1946
+ ok: false,
1947
+ reason: stderrFirstLine && stderrFirstLine !== '' ? stderrFirstLine : 'no beacon in additionalContext (empty or non-matching reply)',
1948
+ elapsedMs: elapsed(),
1949
+ };
1950
+ }
1951
+ }
1952
+ }
1953
+ } finally {
1954
+ if (tempCwd !== undefined) {
1955
+ try {
1956
+ rmSync(tempCwd, { recursive: true, force: true });
1957
+ } catch {
1958
+ /* best-effort cleanup of the probe's own temp cwd */
1959
+ }
1960
+ }
1961
+ // Beacon removal is UNCONDITIONAL on `wrote` — success, failure, or a thrown probe all reach
1962
+ // here (AM-2). `removePatternsByIds` never throws (patterns.ts's own contract) — a failure is
1963
+ // reported through its RETURN VALUE's `.error`, checked below, never via a catch.
1964
+ if (wrote) {
1965
+ try {
1966
+ const removeResult = removeBeacon(root, new Set([beaconId]));
1967
+ if (removeResult.error !== undefined) {
1968
+ cleanupFailed = true;
1969
+ cleanupErrMsg = removeResult.error;
1970
+ }
1971
+ } catch (err) {
1972
+ // Codex round-2: a remover that THROWS (a foreign store implementation, a test seam) must not
1973
+ // escape past the cleanup accounting — it is a cleanup failure like any other.
1974
+ cleanupFailed = true;
1975
+ cleanupErrMsg = err instanceof Error ? err.message : String(err);
1976
+ }
1977
+ }
1978
+ }
1979
+
1980
+ if (cleanupFailed) {
1981
+ return {
1982
+ ok: false,
1983
+ reason: `beacon-cleanup-failed: beacon ${beaconId} could not be removed (${cleanupErrMsg})`,
1984
+ elapsedMs: elapsed(),
1985
+ };
1986
+ }
1987
+ return result;
1988
+ }