@afokapu/atdd-flow 0.3.73 → 0.3.74

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@afokapu/atdd-flow",
3
- "version": "0.3.73",
3
+ "version": "0.3.74",
4
4
  "description": "Filesystem-first Desk registry for projects, seats, tasks, threads, and handoffs.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/handover.ts CHANGED
@@ -745,7 +745,7 @@ export async function requestHandoverReplacement(root: string, address: string,
745
745
  source: { herdr_session: intent.source.herdr_session, pane: intent.source.pane }, target: intent.target,
746
746
  });
747
747
  const legacySource = intent.legacy ? await verifyLegacySource(intent.source, intent.target, authorization.hash, dependencies) : undefined;
748
- const recovery = intent.recover ? await buildStaleMainRecovery(root, resolved, intent, { file, isLive: (prior) => replacementMatchesRuntime(intent.record.runtime, prior) }, (canonical) => verifyLegacySource(intent.source, intent.target, authorization.hash, dependencies, canonical)) : undefined;
748
+ const recovery = intent.recover ? await buildStaleMainRecovery(root, resolved, intent, { file, isLive: (prior) => replacementMatchesRuntime(intent.record.runtime, prior) }, (canonical, once) => verifyLegacySource(intent.source, intent.target, authorization.hash, once(dependencies), canonical)) : undefined;
749
749
  const request = { ...replacementRequest(resolved, intent, authorization, legacySource, dependencies), ...(recovery ? { stale_main_recovery: recovery } : {}) };
750
750
  await (recovery?.prior_request ? replaceActiveRequest : dependencies.publish ?? publishCreateOnly)(file, yaml.print(request));
751
751
  console.log(`Recorded Path-A replacement request for ${resolved}.`);
@@ -3,13 +3,14 @@
3
3
  // - test:atdd-flow:stale-main-binding-recovery
4
4
  // - test:atdd-flow:stale-main-recovery-resumed
5
5
  // - test:atdd-flow:stale-main-source-exited-path-a
6
+ // - test:atdd-flow:stale-main-s1-exited-recovery
6
7
  // Runtime: bun
7
8
  // Purpose: Evidence rules for operator-authorized stale-main Pi binding recovery.
8
9
 
9
10
  import { createHash } from "node:crypto";
10
11
  import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
11
12
  import { basename, dirname, join } from "node:path";
12
- import { type Runtime, type Seat, yaml } from "./core";
13
+ import { type Runtime, type Seat, runOutput, yaml } from "./core";
13
14
  import type { HandoverReplacementRequest, LiveSourceProof } from "./handover";
14
15
 
15
16
  /**
@@ -20,14 +21,15 @@ import type { HandoverReplacementRequest, LiveSourceProof } from "./handover";
20
21
  export type StaleMainRecovery = {
21
22
  canonical_source: { pi_session: string; pi_session_path: string; herdr_session: string; pane: string; cwd: string; launch_receipt: string };
22
23
  prior_receipt: { path: string; sha256: string; archive: string };
23
- /** A superseded active request that no longer matches the recorded runtime, preserved by hash. */
24
- prior_request?: { path: string; sha256: string; archive: string; source_exited?: SourceExited };
24
+ /** A superseded active request, preserved by hash; `live_source_exited` proves an active recovery request's own live source Pi was absent from every live Pi agent when a newer source replaced it. */
25
+ prior_request?: { path: string; sha256: string; archive: string; source_exited?: SourceExited; live_source_exited?: SourceExited };
25
26
  live_source: LiveSourceProof;
26
27
  };
27
28
 
28
29
  /** Proof that a runtime-matching prior request's recorded source Pi was absent from every live Pi agent when it was replaced. */
29
30
  export type SourceExited = { pi_session: string; pi_session_path: string; herdr_session: string; pane: string; verified_at: string };
30
31
 
32
+ type Run = (argv: string[]) => Promise<string>;
31
33
  type Source = HandoverReplacementRequest["source"];
32
34
  type Target = HandoverReplacementRequest["target"];
33
35
  type PriorReceipt = { schema?: string; seat?: string; pi_session?: string; pi_session_path?: string; herdr_session?: string; pane?: string };
@@ -94,14 +96,36 @@ async function archiveBytes(root: string, folder: string, seat: string, bytes: s
94
96
  * matches is replaceable only when its recorded source is exactly the stale canonical source, which the caller has
95
97
  * already proven absent from every live Pi agent. The old bytes are archived by hash with that source-exit evidence.
96
98
  */
97
- async function archivePriorRequest(root: string, seat: string, file: string, isLive: (request: HandoverReplacementRequest) => boolean, exit: { canonical: StaleMainRecovery["canonical_source"]; verifiedAt: string }) {
99
+ async function archivePriorRequest(root: string, seat: string, file: string, isLive: (request: HandoverReplacementRequest) => boolean, exit: { canonical: StaleMainRecovery["canonical_source"]; source: Source; verifiedAt: string; assertAbsent: (source: Source) => Promise<void> }) {
98
100
  let bytes: string;
99
101
  try { bytes = await readFile(file, "utf8"); } catch { return undefined; }
100
102
  const prior = yaml.parse<HandoverReplacementRequest>(bytes);
101
- const matching = isLive(prior), exited = matching && sourceIsCanonical(prior.source, exit.canonical);
102
- if (prior.seat !== seat || (matching && !exited)) throw new Error("A live Path-A replacement request already exists; stale main recovery only replaces a request that no longer matches the recorded runtime or whose matching source has exited.");
103
- const source_exited = exited ? sourceExited(exit.canonical, exit.verifiedAt) : undefined;
104
- return { path: file, ...await archiveBytes(root, "stale-main-request-archive", seat, bytes), ...(source_exited ? { source_exited } : {}) };
103
+ const kind = priorKind(prior, isLive, exit);
104
+ if (prior.seat !== seat || kind === "live") throw new Error("A live Path-A replacement request already exists; stale main recovery only replaces a request that no longer matches the recorded runtime or whose matching source has exited.");
105
+ if (kind === "live_source_exited") await exit.assertAbsent(prior.source);
106
+ const proof = kind === "source_exited" ? { source_exited: sourceExited(exit.canonical, exit.verifiedAt) }
107
+ : kind === "live_source_exited" ? { live_source_exited: sourceExited(prior.source, exit.verifiedAt) } : {};
108
+ return { path: file, ...await archiveBytes(root, "stale-main-request-archive", seat, bytes), ...proof };
109
+ }
110
+
111
+ /** How an active request relates to the recorded runtime: replaceable as-is, by proven exit of a named source, or still live. */
112
+ function priorKind(prior: HandoverReplacementRequest, isLive: (request: HandoverReplacementRequest) => boolean, exit: { canonical: StaleMainRecovery["canonical_source"]; source: Source }) {
113
+ if (!isLive(prior)) return "unmatched";
114
+ if (sourceIsCanonical(prior.source, exit.canonical)) return "source_exited";
115
+ return prior.stale_main_recovery && !sameSource(prior.source, exit.source) ? "live_source_exited" : "live";
116
+ }
117
+
118
+ /** Stale-main recovery proves S0 and any replaced S1 absent from one complete global agent-list snapshot, read once. */
119
+ function agentListOnce() {
120
+ let list: Promise<string> | undefined;
121
+ return <T extends { command?: Run }>(dependencies: T): T => {
122
+ const run = dependencies.command ?? runOutput;
123
+ return { ...dependencies, command: (argv: string[]) => argv.slice(3, 5).join(" ") === "agent list" ? (list ??= run(argv)) : run(argv) };
124
+ };
125
+ }
126
+
127
+ function sameSource(a: Source, b: Source) {
128
+ return a.pi_session === b.pi_session && a.pi_session_path === b.pi_session_path;
105
129
  }
106
130
 
107
131
  function sourceIsCanonical(source: Source, canonical: StaleMainRecovery["canonical_source"]) {
@@ -109,8 +133,8 @@ function sourceIsCanonical(source: Source, canonical: StaleMainRecovery["canonic
109
133
  && source.herdr_session === canonical.herdr_session && source.pane === canonical.pane;
110
134
  }
111
135
 
112
- const sourceExited = (canonical: StaleMainRecovery["canonical_source"], verified_at: string): SourceExited =>
113
- ({ pi_session: canonical.pi_session, pi_session_path: canonical.pi_session_path, herdr_session: canonical.herdr_session, pane: canonical.pane, verified_at });
136
+ const sourceExited = (source: Source, verified_at: string): SourceExited =>
137
+ ({ pi_session: source.pi_session, pi_session_path: source.pi_session_path, herdr_session: source.herdr_session, pane: source.pane, verified_at });
114
138
 
115
139
  /** Swap the active request through a staged file so readers never observe a partial request. */
116
140
  export async function replaceActiveRequest(file: string, content: string) {
@@ -157,8 +181,9 @@ function snapshotMatches(request: HandoverReplacementRequest, live: LiveSourcePr
157
181
  }
158
182
 
159
183
  function exitMatches(recovery: StaleMainRecovery) {
160
- const exited = recovery.prior_request?.source_exited;
161
- return !exited || (sourceIsCanonical({ ...exited }, recovery.canonical_source) && Boolean(exited.verified_at));
184
+ const exited = recovery.prior_request?.source_exited, replaced = recovery.prior_request?.live_source_exited;
185
+ return (!exited || (sourceIsCanonical({ ...exited }, recovery.canonical_source) && Boolean(exited.verified_at)))
186
+ && (!replaced || (!sameSource(replaced, recovery.live_source) && !sourceIsCanonical({ ...replaced }, recovery.canonical_source) && Boolean(replaced.verified_at)));
162
187
  }
163
188
 
164
189
  function archiveNamed(prior: { sha256: string; archive: string } | undefined) {
@@ -176,9 +201,9 @@ export function staleRecoveryMatches(runtime: Runtime | undefined, request: Hand
176
201
  }
177
202
 
178
203
  /** Build the recovery record: verify the live source (injected), then archive the stale receipt and any stale active request by hash. */
179
- export async function buildStaleMainRecovery(root: string, seat: string, intent: { record: Seat; source: Source; target: Target }, active: { file: string; isLive: (request: HandoverReplacementRequest) => boolean }, verify: (canonical: StaleMainRecovery["canonical_source"]) => Promise<LiveSourceProof>): Promise<StaleMainRecovery> {
204
+ export async function buildStaleMainRecovery(root: string, seat: string, intent: { record: Seat; source: Source; target: Target }, active: { file: string; isLive: (request: HandoverReplacementRequest) => boolean }, verify: (absent: Pick<Source, "pi_session" | "pi_session_path">, once: ReturnType<typeof agentListOnce>) => Promise<LiveSourceProof>): Promise<StaleMainRecovery> {
180
205
  const canonical = staleCanonicalSource(intent.record, intent.source, intent.target);
181
- const live_source = await verify(canonical);
182
- const prior_request = await archivePriorRequest(root, seat, active.file, active.isLive, { canonical, verifiedAt: live_source.verified_at });
206
+ const once = agentListOnce(), live_source = await verify(canonical, once);
207
+ const prior_request = await archivePriorRequest(root, seat, active.file, active.isLive, { canonical, source: intent.source, verifiedAt: live_source.verified_at, assertAbsent: async (source) => { await verify(source, once); } });
183
208
  return { canonical_source: canonical, prior_receipt: await archiveStaleReceipt(root, seat, canonical), ...(prior_request ? { prior_request } : {}), live_source };
184
209
  }