fix(photon): resume unfinished handoff with its original custody binding

This commit is contained in:
Dotta committed 2026-11-11 18:24:28 -06:00
1 parent 9577d0570c
commit d5f520f2c1
3 files changed
+58 -2

No files matched your search

@@ -86,6 +86,42 @@ function delivery(state: PhotonExternalState, sequence = 3, previousSequence: nu
payload: { version: 1, binding: state.binding, sequence, previousSequence, acceptedAt: createdAt, rawFrame: raw.toString("base64") } };
}
describe("Photon normal external-owner handoff (controlled owner ports, not real SQL or gRPC)", () => {
it("recovers original custody with a fenced old lease without inventing unknown physical closure", async () => {
const f = fixture();
const descriptor = { id: f.resourceId,
controller: { bootId: "old", hostHash: "different-space", pid: 45678, processStartedAt: "2090-01-01T00:00:00.000Z" },
leaseDigest: photonHash("lease"), closed: false };
await f.create().prepare({ identity, config, lease: { token: "lease" }, localResourceId: f.resourceId, localResource: descriptor });
f.loseReply();
await expect(f.reconcile()).rejects.toThrow("lost enrollment reply");
const original = await f.read();
if (!original) throw new Error("Normal original intent absent");
expect(original.phase).toBe("enrolling");
f.replaceLease();
const replacement = f.create(new PhotonLocalResources());
const recovered = await replacement.reconcile({ identity, config: { ...config, intakeAfter: config.intakeAfter + 5_000 },
lease: { token: "next-lease" }, localResourceId: f.resourceId });
expect(recovered).toMatchObject({ phase: "external", binding: original.binding, handoffId: original.handoffId,
localResourceId: f.resourceId, localResource: descriptor,
businessHandoff: { sequence: null, leaseDigest: photonHash("next-lease") }, localResourceDebt: { nextAttemptAt: null } });
expect(recovered.localClosureConfirmed).toBeUndefined();
expect(recovered.joinedSequence).toBeNull();
expect(recovered.localResource?.closed).toBe(false);
expect(f.counts()).toEqual({ enrollments: 2, joins: 0 });
expect(f.requests[1]).toEqual(f.requests[0]);
let admitted = 0;
const event = delivery(recovered, 3, null);
await replacement.dispatch({ identity, lease: { token: "next-lease" }, delivery: event, admit: async () => { admitted++; } });
await replacement.dispatch({ identity, lease: { token: "next-lease" }, delivery: event, admit: async () => { admitted++; } });
expect(admitted).toBe(1);
const current = await f.read();
if (!current) throw new Error("Retained current custody absent");
expect(current).toMatchObject({ localResource: descriptor, localResourceDebt: { nextAttemptAt: null }, acceptedSequence: 3 });
expect(current.localClosureConfirmed).toBeUndefined();
await expect(replacement.retire(current)).rejects.toThrow("closure is unknown");
expect((await f.read())?.phase).toBe("retiring");
expect(f.requests.filter(request => request.kind === "retire")).toHaveLength(0);
});
it.each(["enrolling", "joining"] as const)("recovers the original %s binding after only the authorized admission cutoff advances", async (phase) => {
const f = fixture();
if (phase === "enrolling") f.loseReply();
@@ -123,7 +123,10 @@ export function createPhotonExternalOwner(options: { store: PhotonExternalStore;
|| input.localResource.leaseDigest !== photonHash(lease.token) || !readControllerProcess(input.localResource.controller))) throw new Error("Photon new allocation descriptor is invalid");
return options.store.current(identity, lease, async (state, current) => {
if (current && current.phase !== "retired") {
if (!isDeepStrictEqual(current.binding, binding)) throw new Error("Photon original enrollment must finish retirement");
// Admission uses the current authorized endpoint cutoff. Enrollment is
// durable custody: retries must finish its exact original input tuple.
if (!isDeepStrictEqual(current.binding, { ...binding, intakeAfter: current.binding.intakeAfter }))
throw new Error("Photon original enrollment must finish retirement");
if (input.localResource && current.localResourceId !== input.localResource.id)
throw new Error("Photon retained resource must recover before another receiver allocation");
return current;
@@ -234,8 +237,9 @@ export function createPhotonExternalOwner(options: { store: PhotonExternalStore;
});
}
async function reconcile(input: PhotonExternalHandoffInput): Promise<PhotonExternalState> {
const { identity, lease, config } = input, binding = bindingFor(identity, config);
const { identity, lease, config } = input;
let retained = await prepare(input);
const binding = retained.binding;
if (retained.phase === "external") return retained;
if (!["enrolling", "joining"].includes(retained.phase)) throw new Error("Photon receiver requires retirement reconciliation");
if (retained.phase === "enrolling") {
@@ -109,6 +109,22 @@ export function createPhotonExternalRuntimeOwner(db: Db, options: {
if (!await selected(record)) throw conflict("Photon prior external owner must retire before a replacement runtime");
return null; // Exact current external placement uses its separate sender descriptor.
}
if (prior.phase === "enrolling" || prior.phase === "joining") {
// Resume already owns the normal current renewable receiver lease.
// Finish the exact prior custody before constructing any replacement.
const owner = createPhotonExternalOwner({ store, broker, resources, notify: value => options.notify(value.endpointId) });
const input = { identity, lease, config, localResourceId: prior.localResourceId };
const prepared = await owner.prepare(input);
if (prepared.phase === "enrolling") await joinConnectionEventWakeEnrollment(db);
await owner.reconcile(input);
if (!await selected(record)) throw conflict("Photon recovered handoff has no current external authority");
await store.current(identity, lease, async (_state, current) => {
if (!current || current.phase !== "external" || current.handoffId !== prior.handoffId
|| current.localResourceId !== prior.localResourceId || !isDeepStrictEqual(current.binding, prior.binding))
throw conflict("Photon original receiver closure or current handoff remains held");
});
return null;
}
options.notify(record.endpoint.id);
throw conflict("Photon original handoff must recover before a replacement receiver allocation");
}