Skip to content

Commit 50998ac

Browse files
committed
fix(claims): record disposal failures before propagating cleanup errors
1 parent ccdc6d4 commit 50998ac

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎packages/contracts/src/platform-runtime.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ export type DeviceRuntimeGateway<Operations extends object> = Readonly<{
205205
inspectFacts(device: DeviceInfo): Promise<RuntimeFacts<Operations>>;
206206
bind(request: DeviceBindingRequest): Promise<DeviceBinding<Operations>>;
207207
/**
208-
* Attempts every owner. Owner failures reject with COMMAND_FAILED and
209-
* details.reason platform-owner-shutdown-failed; details.failures preserves each owner and error.
208+
* Attempts each owner once; repeated calls share the attempt and the same rejection.
209+
* Owner failures reject with COMMAND_FAILED and details.reason platform-owner-shutdown-failed;
210+
* details.failures preserves each owner and error. After shutdown starts, bind and inspectFacts
211+
* reject with COMMAND_FAILED and details.reason runtime-gateway-shut-down.
210212
*/
211213
shutdown(): Promise<void>;
212214
/** Package-coordinated durable lifecycle phases used for startup recovery and daemon shutdown. */

‎packages/daemon-contracts/src/device-claim-owner-recovery.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@ test.each([1, 2] as const)(
122122
await expect(settleVerifiedOrphanedClaim(f.claim, f.recovery, f.env)).rejects.toBe(
123123
f.cleanupError,
124124
);
125+
expect(f.emit).toHaveBeenCalledOnce();
126+
expect(f.emit).toHaveBeenCalledWith(
127+
expect.objectContaining({
128+
phase: 'device_claim_recovery_dispose_failed',
129+
data: expect.objectContaining({
130+
deviceKey: f.claim.deviceKey,
131+
ownerSession: f.claim.session,
132+
error: expect.objectContaining({
133+
details: {
134+
reason: 'platform-owner-shutdown-failed',
135+
failures: expectedFailures.slice(0, failureCount),
136+
},
137+
}),
138+
}),
139+
}),
140+
);
125141
const normalized = normalizeError(f.cleanupError);
126142
expect(JSON.parse(JSON.stringify(normalized))).toMatchObject({
127143
code: 'COMMAND_FAILED',
@@ -252,6 +268,9 @@ test.each([
252268
['retained', 'circular'],
253269
['retained', 'toJSON'],
254270
['retained', 'sink'],
271+
['reconciled', 'circular'],
272+
['reconciled', 'toJSON'],
273+
['reconciled', 'sink'],
255274
] as const)(
256275
'preserves %s outcome and claim when secondary diagnostics encounter %s',
257276
async (outcome, fault) => {
@@ -264,7 +283,7 @@ test.each([
264283
const f = fixture(
265284
async () => {
266285
if (outcome === 'primary-error') throw primary;
267-
return retained;
286+
return outcome === 'retained' ? retained : { status: 'reconciled' };
268287
},
269288
1,
270289
fault === 'circular'
@@ -283,6 +302,7 @@ test.each([
283302
});
284303
const recovery = settleVerifiedOrphanedClaim(f.claim, f.recovery, f.env);
285304
if (outcome === 'primary-error') await expect(recovery).rejects.toBe(primary);
305+
else if (outcome === 'reconciled') await expect(recovery).rejects.toBe(f.cleanupError);
286306
else await expect(recovery).resolves.toBe(retained);
287307
if (fault === 'circular') {
288308
expect(f.emit).toHaveBeenCalledOnce();

‎packages/daemon-contracts/src/device-claim-owner-recovery.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export function createOwnerScopedDeviceClaimReconciler(
7171
try {
7272
await recovery.dispose();
7373
} catch (error) {
74-
if (outcome.status === 'fulfilled' && outcome.result.status === 'reconciled') throw error;
7574
let failureDetails: JsonObject;
7675
try {
7776
failureDetails = { error: toDiagnosticJson(normalizeError(error)) };
@@ -94,6 +93,7 @@ export function createOwnerScopedDeviceClaimReconciler(
9493
} catch {
9594
// ADR 0010: secondary diagnostic failures must preserve the reconciliation outcome.
9695
}
96+
if (outcome.status === 'fulfilled' && outcome.result.status === 'reconciled') throw error;
9797
}
9898
if (outcome.status === 'rejected') throw outcome.error;
9999
return outcome.result;

0 commit comments

Comments
 (0)