feat(FN-4973): complete Step 1 — add stale self-owned registry reconcile helper

Fusion-Task-Id: FN-4973
Fusion-Task-Lineage: 2edb5479-a01a-4775-b121-f4be32792e7a
This commit is contained in:
Fusion (runfusion.ai)
2026-05-17 17:51:19 -07:00
committed by gsxdsm
parent b53172befd
commit 3a53c6778c
2 changed files with 45 additions and 0 deletions

View File

@@ -35,4 +35,31 @@ describe("activeSessionRegistry", () => {
warnSpy.mockRestore();
});
it("reconcileStaleSelfOwned returns no-entry when path is unregistered", () => {
expect(activeSessionRegistry.reconcileStaleSelfOwned("/tmp/missing", "FN-1")).toEqual({
reconciled: false,
reason: "no-entry",
});
});
it("reconcileStaleSelfOwned returns foreign-task for mismatched owner", () => {
activeSessionRegistry.registerPath("/tmp/w1", { taskId: "FN-2", kind: "executor", ownerKey: "FN-2" });
expect(activeSessionRegistry.reconcileStaleSelfOwned("/tmp/w1", "FN-1")).toEqual({
reconciled: false,
reason: "foreign-task",
});
expect(activeSessionRegistry.lookupByPath("/tmp/w1")?.taskId).toBe("FN-2");
});
it("reconcileStaleSelfOwned unregisters matching self-owned entry", () => {
activeSessionRegistry.registerPath("/tmp/w1", { taskId: "FN-1", kind: "executor", ownerKey: "FN-1" });
expect(activeSessionRegistry.reconcileStaleSelfOwned("/tmp/w1", "FN-1")).toEqual({
reconciled: true,
reason: "reconciled",
});
expect(activeSessionRegistry.lookupByPath("/tmp/w1")).toBeNull();
});
});

View File

@@ -10,6 +10,11 @@ export interface ActiveSessionRecord extends ActiveSessionRegistration {
registeredAt: number;
}
export interface ReconcileStaleSelfOwnedResult {
reconciled: boolean;
reason: "no-entry" | "foreign-task" | "reconciled";
}
class ActiveSessionRegistry {
private readonly records = new Map<string, ActiveSessionRecord>();
@@ -45,6 +50,19 @@ class ActiveSessionRegistry {
return paths;
}
reconcileStaleSelfOwned(worktreePath: string, expectedTaskId: string): ReconcileStaleSelfOwnedResult {
const record = this.lookupByPath(worktreePath);
if (!record) {
return { reconciled: false, reason: "no-entry" };
}
if (record.taskId !== expectedTaskId) {
return { reconciled: false, reason: "foreign-task" };
}
this.unregisterPath(worktreePath);
return { reconciled: true, reason: "reconciled" };
}
clear(): void {
this.records.clear();
}