Skip to content

Commit 9e843bc

Browse files
committed
fix(daemon): keep the ended-instance error when session teardown fails
The client stops its lease beat only on the typed provider-instance-ended reason, so a failed session teardown is now a diagnostic and no longer replaces that error.
1 parent 9d15844 commit 9e843bc

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎packages/daemon/src/sessions/handlers/__tests__/lease.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ for (const releaseThrows of [false, true])
9292
},
9393
endSessionsForLease: async (id, reason) => {
9494
ended.push([id, reason]);
95+
throw new Error('session teardown failed');
9596
},
9697
});
98+
// A failed session teardown must not replace the typed reason the client stops beating on.
9799
await assert.rejects(run(), (error) => error === failure);
98100
await cleanup;
99101
assert.deepEqual(releases, [lease.leaseId]);

‎packages/daemon/src/sessions/handlers/lease.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
12
import type { LeaseEndReason } from '../lease-lifecycle.ts';
23
import type { EnvMap, HostEnvironment } from '@agent-device/kernel/source-value';
34
import type { PluginInventoryEntry } from '@agent-device/contracts/plugin-inventory';
@@ -325,7 +326,19 @@ async function heartbeatLease(
325326
ended = error;
326327
}
327328
if (ended) {
328-
await endSessionsForLease?.(lease.leaseId, 'PROVIDER_INSTANCE_ENDED');
329+
// The client stops beating only on the typed reason, so a failed teardown must not replace it.
330+
try {
331+
await endSessionsForLease?.(lease.leaseId, 'PROVIDER_INSTANCE_ENDED');
332+
} catch (error) {
333+
emitDiagnostic({
334+
level: 'warn',
335+
phase: 'lease_provider_ended_session_teardown_failed',
336+
data: {
337+
leaseId: lease.leaseId,
338+
error: error instanceof Error ? error.message : String(error),
339+
},
340+
});
341+
}
329342
throw ended;
330343
}
331344
return {

0 commit comments

Comments
 (0)