Skip to content

Commit 1dd596a

Browse files
committed
fix(runtime): record owner shutdown failures before aggregate settlement
1 parent 58cf0e8 commit 1dd596a

3 files changed

Lines changed: 115 additions & 5 deletions

File tree

‎packages/daemon/src/server/http/daemon-runtime.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ async function startDaemonRuntimeOnHost(
861861
});
862862
expiredProviderLeaseReleaser.shutdown();
863863
const ownerClose = await settleWithinBudget(
864-
() => deviceRuntimeGateway.shutdown(),
864+
() => withDaemonDiagnosticsScope({ logPath }, () => deviceRuntimeGateway.shutdown()),
865865
options.shutdownBudgets?.ownerMs ?? DAEMON_PLATFORM_OWNER_SHUTDOWN_TIMEOUT_MS,
866866
);
867867
platformOwners =

‎src/platform-runtime-gateway.test.ts‎

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { screenshotRuntimeOperationFacts } from '@agent-device/contracts/screens
1515
import { viewportRuntimeOperationFacts } from '@agent-device/contracts/viewport-runtime';
1616
import type { DeviceInfo } from '@agent-device/kernel/device';
1717
import { AppError, normalizeError } from '@agent-device/kernel/errors';
18+
import { withDiagnosticsScope } from '@agent-device/host-kit/diagnostics';
1819
import { createLimrunRuntime } from '@agent-device/provider-limrun';
1920
import { describe, expect, expectTypeOf, test, vi } from 'vitest';
2021
import type { AgentDeviceHost } from './sdk/host-types.ts';
@@ -134,6 +135,97 @@ describe('composed platform runtime gateway', () => {
134135
expect(readyShutdown).toHaveBeenCalledOnce();
135136
expect(lateShutdown).toHaveBeenCalledOnce();
136137
});
138+
test('records a known owner failure while another owner load remains pending', async () => {
139+
const failedRef = providerRuntimeOwner('limrun', 'failed');
140+
const loadingRef = providerRuntimeOwner('limrun', 'loading');
141+
const failedShutdown = vi.fn(async () => {
142+
throw new AppError('COMMAND_FAILED', 'owner refused', {
143+
reason: 'owner-test-refusal',
144+
observedAtNs: 9_007_199_254_740_993n,
145+
});
146+
});
147+
const lateShutdown = vi.fn(async () => {});
148+
let finishLoad!: () => void;
149+
const loadReady = new Promise<void>((resolve) => {
150+
finishLoad = resolve;
151+
});
152+
const runtimeGateway = gateway([
153+
providerRuntime({
154+
ref: failedRef,
155+
load: async () => ({ ...runtimeOwner({ ref: failedRef }), shutdown: failedShutdown }),
156+
}),
157+
providerRuntime({
158+
ref: loadingRef,
159+
load: async () => {
160+
await loadReady;
161+
return { ...runtimeOwner({ ref: loadingRef }), shutdown: lateShutdown };
162+
},
163+
}),
164+
]);
165+
await runtimeGateway.bind({
166+
device,
167+
intent: { kind: 'exact-owner', owner: failedRef, fence: { token: 'failed', generation: 1 } },
168+
scope,
169+
});
170+
const loading = runtimeGateway.bind({
171+
device,
172+
intent: {
173+
kind: 'exact-owner',
174+
owner: loadingRef,
175+
fence: { token: 'loading', generation: 1 },
176+
},
177+
scope,
178+
});
179+
const refused = expect(loading).rejects.toMatchObject({
180+
details: { reason: 'runtime-gateway-shut-down' },
181+
});
182+
const logPath = `${mkdtempForTestSync('gateway-partial-close-')}/events.ndjson`;
183+
await withDiagnosticsScope({ debug: true, logPath }, async () => {
184+
const closing = runtimeGateway.shutdown();
185+
let settled = false;
186+
void closing.then(
187+
() => {
188+
settled = true;
189+
},
190+
() => {
191+
settled = true;
192+
},
193+
);
194+
const rejected = expect(closing).rejects.toMatchObject({
195+
details: { reason: 'platform-owner-shutdown-failed' },
196+
});
197+
try {
198+
await vi.waitFor(() => {
199+
expect(fs.existsSync(logPath)).toBe(true);
200+
const rows = fs
201+
.readFileSync(logPath, 'utf8')
202+
.trim()
203+
.split('\n')
204+
.map((line) => JSON.parse(line));
205+
expect(rows).toContainEqual(
206+
expect.objectContaining({
207+
phase: 'platform_runtime_owner_shutdown_failed',
208+
data: {
209+
owner: failedRef,
210+
error: expect.objectContaining({
211+
code: 'COMMAND_FAILED',
212+
details: { reason: 'owner-test-refusal', observedAtNs: '9007199254740993' },
213+
}),
214+
},
215+
}),
216+
);
217+
});
218+
expect(settled).toBe(false);
219+
expect(failedShutdown).toHaveBeenCalledOnce();
220+
expect(lateShutdown).not.toHaveBeenCalled();
221+
} finally {
222+
finishLoad();
223+
await refused;
224+
await rejected;
225+
}
226+
});
227+
expect(lateShutdown).toHaveBeenCalledOnce();
228+
});
137229
test('daemon reports a composed owner failure with bigint details and completes cleanup', async () => {
138230
const ref = providerRuntimeOwner('limrun', 'failed');
139231
const shutdown = vi.fn(async () => {
@@ -192,11 +284,19 @@ describe('composed platform runtime gateway', () => {
192284
expect(report.platformOwners).toBe('failed');
193285
expect(report.runners.stopped).toEqual(['runner']);
194286
const log = fs.readFileSync(paths.logPath, 'utf8');
195-
const diagnostic = log
287+
const diagnostics = log
196288
.split('\n')
197289
.filter(Boolean)
198-
.map((line) => JSON.parse(line))
199-
.find(({ phase }) => phase === 'daemon_shutdown_platform_owner_failed');
290+
.map((line) => JSON.parse(line));
291+
const diagnostic = diagnostics.find(
292+
({ phase }) => phase === 'daemon_shutdown_platform_owner_failed',
293+
);
294+
expect(diagnostics).toContainEqual(
295+
expect.objectContaining({
296+
phase: 'platform_runtime_owner_shutdown_failed',
297+
data: expect.objectContaining({ owner: ref }),
298+
}),
299+
);
200300
expect(diagnostic.data.error).toMatchObject({
201301
code: 'COMMAND_FAILED',
202302
details: {

‎src/platform-runtime-gateway.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
type DeviceInfo,
3030
type Platform,
3131
} from '@agent-device/kernel/device';
32-
import { AppError } from '@agent-device/kernel/errors';
32+
import { AppError, normalizeError } from '@agent-device/kernel/errors';
3333

3434
export type PlatformRuntimeProviderRegistration = Readonly<{
3535
runtime: ProviderDeviceRuntime;
@@ -210,6 +210,16 @@ export function createComposedPlatformRuntimeGateway(options: {
210210
try {
211211
await owner.shutdown();
212212
} catch (error) {
213+
try {
214+
const { emitDiagnostic } = await import('@agent-device/host-kit/diagnostics');
215+
emitDiagnostic({
216+
level: 'error',
217+
phase: 'platform_runtime_owner_shutdown_failed',
218+
data: { owner: owner.owner, error: normalizeError(error) },
219+
});
220+
} catch {
221+
// ADR 0010: diagnostics must not replace the owner shutdown failure.
222+
}
213223
return { owner: owner.owner, error };
214224
}
215225
},

0 commit comments

Comments
 (0)