@afokapu/atdd-flow 0.3.78 → 0.3.79
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 +1 -1
- package/src/handover.ts +2 -1
- package/src/stale-main-recovery.ts +92 -10
package/package.json
CHANGED
package/src/handover.ts
CHANGED
|
@@ -739,7 +739,8 @@ export async function requestHandoverReplacement(root: string, address: string,
|
|
|
739
739
|
const legacySource = intent.legacy ? await verifyLegacySource(intent.source, intent.target, authorization.hash, dependencies) : undefined;
|
|
740
740
|
const recovery = intent.recover ? await buildStaleMainRecovery(root, resolved, intent, { file, isLive: (prior) => replacementMatchesRuntime(intent.record.runtime, prior) },
|
|
741
741
|
(canonical, once) => verifyLegacySource(intent.relocation ? { ...intent.source, ...intent.target } : intent.source, intent.target, authorization.hash, once(dependencies), canonical),
|
|
742
|
-
() => (dependencies.command ?? runOutput)(herdr(intent.target.herdr_session, "agent", "list"))
|
|
742
|
+
() => (dependencies.command ?? runOutput)(herdr(intent.target.herdr_session, "agent", "list")),
|
|
743
|
+
{ paneList: () => (dependencies.command ?? runOutput)(herdr(intent.target.herdr_session, "pane", "list")), authorizationHash: authorization.hash, at: () => dependencies.at?.() ?? now() }) : undefined;
|
|
743
744
|
const request = { ...replacementRequest(resolved, intent, authorization, legacySource, dependencies), ...(recovery ? { stale_main_recovery: recovery } : {}) };
|
|
744
745
|
await (recovery?.prior_request ? replaceActiveRequest : dependencies.publish ?? publishCreateOnly)(file, yaml.print(request));
|
|
745
746
|
console.log(`Recorded Path-A replacement request for ${resolved}.`);
|
|
@@ -24,21 +24,26 @@ export type StaleMainRecovery = {
|
|
|
24
24
|
prior_receipt: { path: string; sha256: string; archive: string };
|
|
25
25
|
/** 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. */
|
|
26
26
|
prior_request?: { path: string; sha256: string; archive: string; source_exited?: SourceExited; live_source_exited?: SourceExited };
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
/** Absent only for a pane-gone relocation, whose source is proven absent rather than live. */
|
|
28
|
+
live_source?: LiveSourceProof;
|
|
29
|
+
/** Present only for a relocated main: an operator manual rebinding receipt, or a launcher receipt whose retired pane is gone. */
|
|
29
30
|
relocation?: Relocation;
|
|
30
31
|
};
|
|
31
32
|
|
|
32
33
|
export const MANUAL_REBINDING_SCHEMA = "atdd-flow/manual-pi-runtime-rebinding-receipt/v1";
|
|
33
34
|
export const MANUAL_REBINDING_MODE = "operator-authorized-manual-in-place-adoption";
|
|
34
35
|
|
|
35
|
-
/**
|
|
36
|
-
export
|
|
36
|
+
/** Relocation mode of a launcher-generated receipt whose canonical pane and exact Pi are both absent from Herdr. */
|
|
37
|
+
export const PANE_GONE_MODE = "launcher-receipt-pane-gone";
|
|
38
|
+
|
|
39
|
+
/** The retired placement named by the receipt; lineage only, never a live source. A pane-gone relocation also records the global absence proof. */
|
|
40
|
+
export type Relocation = { mode: string; retired: { herdr_session: string; pane: string }; absence?: { authorization_hash: string; verified_at: string } };
|
|
37
41
|
|
|
38
42
|
/** Proof that a runtime-matching prior request's recorded source Pi was absent from every live Pi agent when it was replaced. */
|
|
39
43
|
export type SourceExited = { pi_session: string; pi_session_path: string; herdr_session: string; pane: string; verified_at: string };
|
|
40
44
|
|
|
41
45
|
type Run = (argv: string[]) => Promise<string>;
|
|
46
|
+
type Recorded = { session: string; sessionPath: string; locator: { session: string; pane: string } };
|
|
42
47
|
type Source = HandoverReplacementRequest["source"];
|
|
43
48
|
type Target = HandoverReplacementRequest["target"];
|
|
44
49
|
type PriorReceipt = {
|
|
@@ -74,6 +79,7 @@ export function staleCanonicalSource(record: Seat, source: Source, target: Targe
|
|
|
74
79
|
|
|
75
80
|
/** The archived receipt must name exactly the recorded canonical seat, session, path and placement. */
|
|
76
81
|
export function receiptProvesCanonical(receipt: PriorReceipt, seat: string, canonical: StaleMainRecovery["canonical_source"], relocation?: Relocation) {
|
|
82
|
+
if (relocation?.mode === PANE_GONE_MODE) return launchReceiptProvesCanonical(receipt, seat, { ...canonical, ...relocation.retired });
|
|
77
83
|
return relocation ? manualReceiptProvesCanonical(receipt, seat, canonical, relocation) : launchReceiptProvesCanonical(receipt, seat, canonical);
|
|
78
84
|
}
|
|
79
85
|
|
|
@@ -201,14 +207,24 @@ function targetMatches(request: HandoverReplacementRequest, canonical: StaleMain
|
|
|
201
207
|
|
|
202
208
|
/** A relocated request names the exact recorded Pi at the retired pane, and moves to the current pane that Herdr observed it on. */
|
|
203
209
|
function relocatedTargetMatches(request: HandoverReplacementRequest, canonical: StaleMainRecovery["canonical_source"], relocation: Relocation) {
|
|
204
|
-
return relocation.mode
|
|
210
|
+
return [MANUAL_REBINDING_MODE, PANE_GONE_MODE].includes(relocation.mode)
|
|
205
211
|
&& canonical.cwd === request.target.cwd && canonical.herdr_session === request.target.herdr_session && canonical.pane === request.target.pane
|
|
206
212
|
&& request.source.pi_session === canonical.pi_session && request.source.pi_session_path === canonical.pi_session_path
|
|
207
213
|
&& request.source.herdr_session === relocation.retired.herdr_session && request.source.pane === relocation.retired.pane
|
|
208
214
|
&& (relocation.retired.herdr_session !== canonical.herdr_session || relocation.retired.pane !== canonical.pane);
|
|
209
215
|
}
|
|
210
216
|
|
|
211
|
-
|
|
217
|
+
/** A pane-gone relocation has no live source: its absence proof must be bound to this request's authorization. */
|
|
218
|
+
function absenceMatches(request: HandoverReplacementRequest, relocation: Relocation) {
|
|
219
|
+
return relocation.absence?.authorization_hash === request.authorization.hash && Boolean(relocation.absence.verified_at);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function snapshotMatches(request: HandoverReplacementRequest, live: LiveSourceProof | undefined, relocation?: Relocation) {
|
|
223
|
+
if (relocation?.mode === PANE_GONE_MODE) return !live && absenceMatches(request, relocation);
|
|
224
|
+
return live !== undefined && liveSnapshotMatches(request, live, relocation);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function liveSnapshotMatches(request: HandoverReplacementRequest, live: LiveSourceProof, relocation?: Relocation) {
|
|
212
228
|
const placement = relocation ? request.target : request.source;
|
|
213
229
|
return live.authorization_hash === request.authorization.hash && live.pi_session === request.source.pi_session
|
|
214
230
|
&& live.pi_session_path === request.source.pi_session_path && live.herdr_session === placement.herdr_session
|
|
@@ -218,7 +234,7 @@ function snapshotMatches(request: HandoverReplacementRequest, live: LiveSourcePr
|
|
|
218
234
|
function exitMatches(recovery: StaleMainRecovery) {
|
|
219
235
|
const exited = recovery.prior_request?.source_exited, replaced = recovery.prior_request?.live_source_exited;
|
|
220
236
|
return (!exited || (sourceIsCanonical({ ...exited }, recovery.canonical_source) && Boolean(exited.verified_at)))
|
|
221
|
-
&& (!replaced || (!sameSource(replaced, recovery.live_source) && !sourceIsCanonical({ ...replaced }, recovery.canonical_source) && Boolean(replaced.verified_at)));
|
|
237
|
+
&& (!replaced || (!(recovery.live_source && sameSource(replaced, recovery.live_source)) && !sourceIsCanonical({ ...replaced }, recovery.canonical_source) && Boolean(replaced.verified_at)));
|
|
222
238
|
}
|
|
223
239
|
|
|
224
240
|
function archiveNamed(prior: { sha256: string; archive: string } | undefined) {
|
|
@@ -236,7 +252,8 @@ export function staleRecoveryMatches(runtime: Runtime | undefined, request: Hand
|
|
|
236
252
|
}
|
|
237
253
|
|
|
238
254
|
/** Build the recovery record: verify the live source (injected), then archive the stale receipt and any stale active request by hash. */
|
|
239
|
-
export async function buildStaleMainRecovery(root: string, seat: string, intent: { record: Seat; source: Source; target: Target; relocation?: Relocation }, active: { file: string; isLive: (request: HandoverReplacementRequest) => boolean }, verify: (absent: Pick<Source, "pi_session" | "pi_session_path">, once: ReturnType<typeof agentListOnce>) => Promise<LiveSourceProof>, agentList: () => Promise<string
|
|
255
|
+
export async function buildStaleMainRecovery(root: string, seat: string, intent: { record: Seat; source: Source; target: Target; relocation?: Relocation }, active: { file: string; isLive: (request: HandoverReplacementRequest) => boolean }, verify: (absent: Pick<Source, "pi_session" | "pi_session_path">, once: ReturnType<typeof agentListOnce>) => Promise<LiveSourceProof>, agentList: () => Promise<string>, gone: PaneGoneObservation): Promise<StaleMainRecovery> {
|
|
256
|
+
if (intent.relocation?.mode === PANE_GONE_MODE) return buildPaneGoneStaleMainRecovery(root, seat, intent, intent.relocation, active, { agentList, ...gone });
|
|
240
257
|
if (intent.relocation) return buildRelocatedStaleMainRecovery(root, seat, intent, intent.relocation, active, { verify: () => verify(intent.source, agentListOnce()), agentList });
|
|
241
258
|
const canonical = staleCanonicalSource(intent.record, intent.source, intent.target);
|
|
242
259
|
const once = agentListOnce(), live_source = await verify(canonical, once);
|
|
@@ -366,7 +383,72 @@ export function assertRelocatedAgentList(raw: string, source: Pick<Source, "pi_s
|
|
|
366
383
|
if (recorded.length !== 1) throw new Error("The recorded Pi must be listed exactly once at the current pane.");
|
|
367
384
|
}
|
|
368
385
|
|
|
369
|
-
/** A target pane that differs from the source pane is a relocation only
|
|
386
|
+
/** A target pane that differs from the source pane is a relocation only for a manual binding or an exact pane-gone launcher receipt; otherwise undefined. */
|
|
370
387
|
export async function relocationFor(record: Seat, source: Source, target: Target, activeFile: string): Promise<Relocation | undefined> {
|
|
371
|
-
|
|
388
|
+
if (target.pane === source.pane) return undefined;
|
|
389
|
+
return await isManualRebindingReceipt(record) ? assertRelocatedBinding(record, source, target, activeFile) : assertPaneGoneBinding(record, source, target, activeFile);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
async function readLauncherReceipt(path: string) {
|
|
393
|
+
try {
|
|
394
|
+
const receipt = yaml.parse<PriorReceipt>(await readFile(path, "utf8"));
|
|
395
|
+
return receipt.schema === "atdd-flow/pi-runtime-launch-receipt/v1" ? receipt : undefined;
|
|
396
|
+
} catch { return undefined; }
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** The launcher receipt names this seat's exact recorded Pi at a pane other than the seat's current locator. */
|
|
400
|
+
function receiptNamesRetiredPane(receipt: PriorReceipt, record: Seat, recorded: Recorded) {
|
|
401
|
+
return receipt.seat === record.address && receipt.pi_session === recorded.session && receipt.pi_session_path === recorded.sessionPath
|
|
402
|
+
&& Boolean(receipt.herdr_session && receipt.pane) && (receipt.herdr_session !== recorded.locator.session || receipt.pane !== recorded.locator.pane);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** The request moves the exact recorded Pi from the receipt's retired pane to the seat's current locator. */
|
|
406
|
+
function requestMovesRetiredPi(receipt: PriorReceipt, source: Source, target: Target, recorded: Recorded) {
|
|
407
|
+
return source.pi_session === recorded.session && source.pi_session_path === recorded.sessionPath
|
|
408
|
+
&& source.herdr_session === receipt.herdr_session && source.pane === receipt.pane
|
|
409
|
+
&& target.herdr_session === recorded.locator.session && target.pane === recorded.locator.pane;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** A launcher-generated receipt whose retired canonical pane is the request source while the request target is the seat's current locator; otherwise undefined, so the ordinary released-source-pane guard refuses. */
|
|
413
|
+
async function assertPaneGoneBinding(record: Seat, source: Source, target: Target, activeFile: string): Promise<Relocation | undefined> {
|
|
414
|
+
const runtime = record.runtime, locator = runtime?.addresses.herdr;
|
|
415
|
+
if (!runtime?.launch_receipt || typeof locator === "string" || !locator) return undefined;
|
|
416
|
+
const receipt = await readLauncherReceipt(runtime.launch_receipt);
|
|
417
|
+
if (!receipt || !runtime.pi_session || !runtime.pi_session_path) return undefined;
|
|
418
|
+
const recorded = { session: runtime.pi_session, sessionPath: runtime.pi_session_path, locator };
|
|
419
|
+
if (!receiptNamesRetiredPane(receipt, record, recorded) || !requestMovesRetiredPi(receipt, source, target, recorded) || !await priorRequestIsRetiredLineage(activeFile, receipt)) return undefined;
|
|
420
|
+
return { mode: PANE_GONE_MODE, retired: { herdr_session: receipt.herdr_session!, pane: receipt.pane! } };
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** Pane ids from one complete global Herdr pane list. */
|
|
424
|
+
function listedPaneIds(raw: string) {
|
|
425
|
+
const parsed = JSON.parse(raw) as { result?: { panes?: unknown }; panes?: unknown };
|
|
426
|
+
const panes = (parsed.result ?? parsed).panes;
|
|
427
|
+
const ids = Array.isArray(panes) ? panes.map((pane) => (pane as { pane_id?: unknown } | null)?.pane_id) : undefined;
|
|
428
|
+
if (!ids || ids.some((id) => typeof id !== "string")) throw new Error("Pane-gone recovery requires complete global Herdr pane-list evidence.");
|
|
429
|
+
return ids as string[];
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const piSessionId = (path: string) => basename(path).match(/_([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i)?.[1];
|
|
433
|
+
|
|
434
|
+
/** Pane-gone recovery requires the retired pane and the exact recorded Pi to be absent from one complete global pane list and agent list. */
|
|
435
|
+
export function assertPaneGone(agentsRaw: string, panesRaw: string, source: Pick<Source, "pi_session" | "pi_session_path">, retired: { pane: string }) {
|
|
436
|
+
if (listedPaneIds(panesRaw).includes(retired.pane)) throw new Error("The retired canonical pane is still listed by Herdr.");
|
|
437
|
+
const agents = listedPiAgents(agentsRaw, "Pane-gone recovery");
|
|
438
|
+
if (agents.some((agent) => agent.pane === retired.pane)) throw new Error("A Pi is live at the retired canonical pane.");
|
|
439
|
+
if (agents.some(({ path }) => path === resolve(source.pi_session_path) || piSessionId(path) === source.pi_session)) throw new Error("The exact recorded Pi is still a live Pi agent.");
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** Herdr pane-list evidence plus the authorization and clock that bind the absence proof to one request. */
|
|
443
|
+
export type PaneGoneObservation = { paneList: () => Promise<string>; authorizationHash: string; at: () => string };
|
|
444
|
+
|
|
445
|
+
async function buildPaneGoneStaleMainRecovery(root: string, seat: string, intent: { record: Seat; source: Source; target: Target }, pane: Relocation, active: { file: string }, observe: { agentList: () => Promise<string> } & PaneGoneObservation): Promise<StaleMainRecovery> {
|
|
446
|
+
const { locator, session, sessionPath, receipt } = completeRuntime(intent.record.runtime);
|
|
447
|
+
const canonical = { pi_session: session, pi_session_path: sessionPath, herdr_session: locator.session, pane: locator.pane, cwd: intent.target.cwd, launch_receipt: receipt };
|
|
448
|
+
try { assertPaneGone(await observe.agentList(), await observe.paneList(), intent.source, pane.retired); }
|
|
449
|
+
catch (error) { throw new Error(`Path-A replacement target must be the exact released source pane; pane-gone recovery refused: ${(error as Error).message}`); }
|
|
450
|
+
const verified_at = observe.at(), relocation: Relocation = { ...pane, absence: { authorization_hash: observe.authorizationHash, verified_at } };
|
|
451
|
+
const prior_request = await archivePriorRequest(root, seat, active.file, () => false, { canonical, source: intent.source, verifiedAt: verified_at, assertAbsent: async () => {} });
|
|
452
|
+
const prior_receipt = await archiveStaleReceipt(root, seat, canonical, relocation);
|
|
453
|
+
return { canonical_source: canonical, prior_receipt, ...(prior_request ? { prior_request } : {}), relocation };
|
|
372
454
|
}
|