Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
05be6bf
feat(daemon): the runtime reports its shutdown instead of exiting the…
thymikee Oct 16, 2026
5789838
feat(daemon): per-runtime state for admission, resolution, artifacts,…
thymikee Oct 16, 2026
9793a87
feat(daemon): per-runtime Apple runner state and a shared PNG worker
thymikee Oct 16, 2026
0b83711
chore(daemon): drop re-exports nothing consumes yet
thymikee Oct 16, 2026
c3baaec
fix(daemon): a failed shutdown step releases the rest, and dispose re…
thymikee Oct 17, 2026
c047776
fix(daemon): assert registration release on a failed close and share …
thymikee Oct 17, 2026
22cea00
fix(apple): scope start admissions, prep builds and bad-artifact mark…
thymikee Oct 17, 2026
5c98812
refactor(move): keep daemon-runtime.ts under the size limit
thymikee Oct 17, 2026
817930f
chore: merge the host environment foundation into the runtime state b…
thymikee Oct 17, 2026
012b50d
Merge branch 'refactor/host-environment' into refactor/daemon-runtime…
thymikee Oct 17, 2026
2f29e99
test: drop the empty daemon-runtime imports left by the runtime-state…
thymikee Oct 17, 2026
5b26ca3
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
7eedaf6
test: run the Limrun public HTTP test on the runtime shutdown contract
thymikee Oct 17, 2026
1a86a19
fix(daemon): bound the runtime close and report work that outlives it
thymikee Oct 17, 2026
7b90408
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
b29df19
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
3aa6def
fix(daemon): record abandoned requests in the daemon log and print un…
thymikee Oct 17, 2026
5a20a1c
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
be191a3
refactor(daemon): report the close through one function the executabl…
thymikee Oct 17, 2026
fff0337
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
794c048
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
4ab64cc
Merge branch 'refactor/host-environment' into refactor/daemon-runtime…
thymikee Oct 17, 2026
9c64e47
refactor(android): own the snapshot-helper sessions, failed starts, r…
thymikee Oct 17, 2026
418b072
chore(layering): enumerate the Android helper runtime-state subpath b…
thymikee Oct 17, 2026
e445455
Merge remote-tracking branch 'origin/main' into refactor/daemon-runti…
thymikee Oct 17, 2026
29f3e33
refactor(daemon): split the plugin close into its budget race and its…
thymikee Oct 17, 2026
db08148
Merge commit 'db55a5dfbd' into refactor/daemon-runtime-no-process-glo…
thymikee Oct 17, 2026
b7a9d15
test: pin the close report beside the unfinished-work lines and drive…
thymikee Oct 17, 2026
631d640
Merge commit 'd8a9bd40c0' into refactor/daemon-runtime-no-process-glo…
thymikee Oct 17, 2026
2f4e815
refactor(android): keep the helper runtime state inside the adb modul…
thymikee Oct 17, 2026
8539b30
refactor(android): export the per-runtime helper state from the adb h…
thymikee Oct 17, 2026
0b2939c
Merge commit '870cf663c4' into refactor/daemon-runtime-no-process-glo…
thymikee Oct 17, 2026
2568e52
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
f8251da
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
45458ef
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
d6ba2d9
Merge remote-tracking branch 'origin/main' into refactor/daemon-runti…
thymikee Oct 17, 2026
d5682c7
Merge origin/main and adapt to the platform host migration
thymikee Oct 17, 2026
b6fd554
refactor(runtime): give the Android helper state to the root's slot s…
thymikee Oct 17, 2026
5a8f1cd
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
3ef9ace
refactor(runtime): move Apple runner state and request-cancel onto de…
thymikee Oct 17, 2026
967dcc7
refactor(daemon): keep the five per-runtime daemon states in defineRu…
thymikee Oct 17, 2026
0658db9
fix(gate): count an approved closure addition only when it is not a r…
thymikee Oct 17, 2026
66f0b27
refactor(daemon): take RuntimeStateHandle from host-kit's runtime-slo…
thymikee Oct 17, 2026
70c901d
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 17, 2026
d9ff517
refactor(android): define the helper runtime slots in the adb host mo…
thymikee Oct 18, 2026
d932926
chore(layering): let daemon composition import the adb host module fo…
thymikee Oct 18, 2026
029e82d
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 18, 2026
1c18440
test(android): drop the host stub the shell-protocol probe test no lo…
thymikee Oct 18, 2026
c58e2b0
Merge remote-tracking branch 'origin/refactor/host-environment' into …
thymikee Oct 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/capture-kit/src/png-worker-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
decodePngAsync,
decodeScreenshotImageAsync,
encodePngAsync,
retainPngWorker,
terminatePngWorker,
transcodeScreenshotToPngAsync,
} from './png-worker-client.ts';
Expand Down Expand Up @@ -225,3 +226,19 @@ test('decodeScreenshotImageAsync rejects a container it cannot read with the can
},
);
});

test('the worker serves every holder and stops only when the last one lets go', async () => {
const first = retainPngWorker();
const second = retainPngWorker();
const encoded = PNG.sync.write(buildPatternPng(5, 4, 2));

await first();
await first();
const stillServing = await runOnWorkerThread('decode after one release', () =>
decodePngAsync(encoded, 'fixture'),
);
assert.equal(stillServing.width, 5);

await second();
assert.equal(process.getActiveResourcesInfo().includes('MessagePort'), false);
});
18 changes: 16 additions & 2 deletions packages/capture-kit/src/png-worker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,23 @@ async function runPngJob<Kind extends PngWorkerJobKind>(
}
}

/** Daemon startup hook: spawns the worker before the first screenshot job. */
export function prewarmPngWorker(): void {
let pngWorkerHolds = 0;

/**
* Daemon startup hook: spawns the worker before the first screenshot job and holds it for the
* caller. The worker is shared by every runtime in the process, so it stops when the last holder
* lets go; the returned release is idempotent.
*/
export function retainPngWorker(): () => Promise<void> {
obtainWorker();
pngWorkerHolds += 1;
Comment thread
thymikee marked this conversation as resolved.
Resolved
let released = false;
return async () => {
if (released) return;
released = true;
pngWorkerHolds -= 1;
if (pngWorkerHolds === 0) await terminatePngWorker();
};
}

/** Stops the worker thread (used by tests and shutdown); later calls respawn it. */
Expand Down
10 changes: 6 additions & 4 deletions packages/contracts/src/application-lifecycle-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,10 @@ export type AppleApplicationTools = Readonly<{
applyRuntimeHints(device: DeviceInfo, input: RuntimeHintsApplicationInput): Promise<void>;
clearRuntimeHints(device: DeviceInfo, input: RuntimeHintsApplicationInput): Promise<void>;
dismissCloseAlerts(device: DeviceInfo, input: CloseApplicationFinalizationInput): Promise<void>;
detachRunnerSessionsForShutdown(): Promise<void>;
finalizeRunnerSessionsForShutdown(): Promise<void>;
/** The devices whose runner was handed to the next daemon instead of stopped. */
detachRunnerSessionsForShutdown(): Promise<readonly string[]>;
/** The devices whose runner this daemon stopped. */
finalizeRunnerSessionsForShutdown(): Promise<readonly string[]>;
}>;

/** Lazy Android mechanics used by the Android package's own lifecycle binding. */
Expand Down Expand Up @@ -413,6 +415,6 @@ export type AndroidApplicationTools = Readonly<{
/** Gateway-coordinated durable phases whose concrete mechanics stay package-owned. */
export type ApplicationLifecycleResourceLifecycle = Readonly<{
recoverStartupResources(input: Readonly<{ stateDir: string }>): Promise<void>;
detachForDaemonShutdown(): Promise<void>;
finalizeDaemonShutdown(): Promise<void>;
detachForDaemonShutdown(): Promise<readonly string[]>;
finalizeDaemonShutdown(): Promise<readonly string[]>;
}>;
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export function applicationLifecycleResourceFixture(
): ApplicationLifecycleResourceLifecycle {
return Object.freeze({
recoverStartupResources: vi.fn(async () => {}),
detachForDaemonShutdown: vi.fn(async () => {}),
finalizeDaemonShutdown: vi.fn(async () => {}),
detachForDaemonShutdown: vi.fn(async (): Promise<readonly string[]> => []),
finalizeDaemonShutdown: vi.fn(async (): Promise<readonly string[]> => []),
...overrides,
});
}
Expand Down Expand Up @@ -59,6 +59,7 @@ export function daemonRuntimeDepsFixture(
return async (input) => {
const resolved = typeof overrides === 'function' ? overrides(input) : overrides;
return Object.freeze({
runtimeStates: [],
ownedProcessRecords: createOwnedProcessRecordStore({
stateDir: input.baseDir,
sessionsDir: input.sessionsDir,
Expand Down
25 changes: 22 additions & 3 deletions packages/daemon/src/core/interactor-resolution.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AsyncLocalStorage } from 'node:async_hooks';
import type { RuntimeStateHandle } from '@agent-device/host-kit/request';
import type { Interactor, RunnerContext } from '@agent-device/contracts/interactor-types';
import type { DeviceInfo } from '@agent-device/kernel/device';
import { AppError } from '@agent-device/kernel/errors';
Expand Down Expand Up @@ -27,17 +29,34 @@ const NO_INTERACTOR_RESOLUTION: InteractorResolution = {
},
};

let installedInteractorResolution: InteractorResolution = NO_INTERACTOR_RESOLUTION;
type InteractorResolutionState = { resolution: InteractorResolution };

const interactorResolutionStorage = new AsyncLocalStorage<InteractorResolutionState>();
const standaloneInteractorResolution: InteractorResolutionState = {
resolution: NO_INTERACTOR_RESOLUTION,
};
const interactorResolutionState = (): InteractorResolutionState =>
interactorResolutionStorage.getStore() ?? standaloneInteractorResolution;

/** The resolution one daemon runtime owns; the runtime installs its composition's into it. */
export function createInteractorResolutionRuntimeState(): RuntimeStateHandle {
const owned: InteractorResolutionState = { resolution: NO_INTERACTOR_RESOLUTION };
return {
run: (fn) => interactorResolutionStorage.run(owned, fn),
enter: () => interactorResolutionStorage.enterWith(owned),
dispose: () => {},
};
}

/**
* Root composition's installation point, beside the provider-device admission that shares the
* same request-scoped provider scope: one change names both. The interactor graph stays behind
* the module root composition imports, and the daemon never loads it itself.
*/
export function installInteractorResolution(resolution: InteractorResolution): void {
installedInteractorResolution = resolution;
interactorResolutionState().resolution = resolution;
}

export function interactorResolution(): InteractorResolution {
return installedInteractorResolution;
return interactorResolutionState().resolution;
}
25 changes: 22 additions & 3 deletions packages/daemon/src/core/provider-device-admission.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AsyncLocalStorage } from 'node:async_hooks';
import type { RuntimeStateHandle } from '@agent-device/host-kit/request';
import type { DeviceInfo } from '@agent-device/kernel/device';

/**
Expand All @@ -18,17 +20,34 @@ const NO_PROVIDER_DEVICE_ADMISSION: ProviderDeviceAdmission = {
isActive: () => false,
};

let installedProviderDeviceAdmission: ProviderDeviceAdmission = NO_PROVIDER_DEVICE_ADMISSION;
type ProviderAdmissionState = { admission: ProviderDeviceAdmission };

const providerAdmissionStorage = new AsyncLocalStorage<ProviderAdmissionState>();
const standaloneProviderAdmission: ProviderAdmissionState = {
admission: NO_PROVIDER_DEVICE_ADMISSION,
};
const providerAdmissionState = (): ProviderAdmissionState =>
providerAdmissionStorage.getStore() ?? standaloneProviderAdmission;

/** The admission one daemon runtime owns; the runtime installs its composition's into it. */
export function createProviderDeviceAdmissionRuntimeState(): RuntimeStateHandle {
const owned: ProviderAdmissionState = { admission: NO_PROVIDER_DEVICE_ADMISSION };
return {
run: (fn) => providerAdmissionStorage.run(owned, fn),
enter: () => providerAdmissionStorage.enterWith(owned),
dispose: () => {},
};
}

/**
* Root composition's installation point. The provider runtime scope is ambient per request, so
* this is the one place the daemon names where the fact comes from, and the daemon never reads
* provider runtime internals itself.
*/
export function installProviderDeviceAdmission(admission: ProviderDeviceAdmission): void {
installedProviderDeviceAdmission = admission;
providerAdmissionState().admission = admission;
}

export function isActiveProviderDevice(device: DeviceInfo): boolean {
return installedProviderDeviceAdmission.isActive(device);
return providerAdmissionState().admission.isActive(device);
}
3 changes: 1 addition & 2 deletions packages/daemon/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
export { startDaemonRuntime } from './server/http/daemon-runtime.ts';
export { daemonStartupExitCode, startDaemonRuntime } from './server/http/daemon-runtime.ts';
export type {
ComposeDaemonRuntimeDeps,
DaemonRuntimeController,
DaemonRuntimeDeps,
DaemonRuntimeDepsInput,
} from './server/http/daemon-runtime.ts';
export type { DaemonInvokeFn, DaemonRequest } from './core/daemon-request.ts';
export type { DaemonPlatformServices } from './core/platform-services.ts';
Expand Down
32 changes: 32 additions & 0 deletions packages/daemon/src/resources/artifact-tracking.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from 'node:fs';
import path from 'node:path';
import { expect, test } from 'vitest';
import { mkdtempForTestSync } from '@agent-device/test-kit';
import {
createArtifactTrackingRuntimeState,
trackDownloadableArtifact,
trackUploadedArtifact,
} from './artifact-tracking.ts';

test('disposing a runtime removes the temporary files it still tracks', () => {
const root = mkdtempForTestSync('agent-device-artifact-dispose-');
const artifactPath = path.join(root, 'artifact.txt');
const uploadDir = path.join(root, 'upload');
fs.writeFileSync(artifactPath, 'artifact');
fs.mkdirSync(uploadDir);
fs.writeFileSync(path.join(uploadDir, 'payload'), 'payload');
const runtime = createArtifactTrackingRuntimeState();

runtime.run(() => {
trackDownloadableArtifact({
artifactPath,
artifactType: 'runtime-artifact',
fileName: 'artifact.txt',
});
trackUploadedArtifact({ artifactPath: path.join(uploadDir, 'payload'), tempDir: uploadDir });
});
runtime.dispose();

expect(fs.existsSync(uploadDir)).toBe(false);
expect(fs.existsSync(artifactPath)).toBe(false);
});
70 changes: 57 additions & 13 deletions packages/daemon/src/resources/artifact-tracking.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AsyncLocalStorage } from 'node:async_hooks';
import type { RuntimeStateHandle } from '@agent-device/host-kit/request';
import crypto from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
Expand Down Expand Up @@ -44,7 +46,43 @@ type ArtifactEntry = {
timer: ReturnType<typeof setTimeout>;
};

const pendingArtifacts = new Map<string, ArtifactEntry>();
type ArtifactTrackingState = {
artifacts: Map<string, ArtifactEntry>;
uploads: Map<string, UploadEntry>;
};

const artifactTrackingStorage = new AsyncLocalStorage<ArtifactTrackingState>();
const createArtifactTrackingState = (): ArtifactTrackingState => ({
artifacts: new Map(),
uploads: new Map(),
});
const standaloneArtifactTracking = createArtifactTrackingState();
const artifactTracking = (): ArtifactTrackingState =>
artifactTrackingStorage.getStore() ?? standaloneArtifactTracking;
const pendingArtifacts = () => artifactTracking().artifacts;
const pendingUploads = () => artifactTracking().uploads;

/** The artifacts and uploads one daemon runtime tracks; disposing it stops their expiry timers. */
export function createArtifactTrackingRuntimeState(): RuntimeStateHandle {
const owned = createArtifactTrackingState();
return {
run: (fn) => artifactTrackingStorage.run(owned, fn),
enter: () => artifactTrackingStorage.enterWith(owned),
dispose: () => {
for (const entry of owned.artifacts.values()) {
clearTimeout(entry.timer);
cleanupDirectoryArchive(entry);
if (entry.deleteAfterDownload) removeBestEffort(entry.artifactPath);
}
for (const entry of owned.uploads.values()) {
clearTimeout(entry.timer);
removeBestEffort(entry.tempDir);
}
owned.artifacts.clear();
Comment thread
thymikee marked this conversation as resolved.
Resolved
owned.uploads.clear();
},
};
}

/** What a finished request registers for each artifact it hands back to the client. */
export type DownloadableArtifactRegistration = {
Expand Down Expand Up @@ -73,7 +111,7 @@ export function trackDownloadableArtifact(
cleanupDownloadableArtifact(artifactId);
}, ARTIFACT_CLEANUP_TIMEOUT_MS);
timer.unref();
pendingArtifacts.set(artifactId, {
pendingArtifacts().set(artifactId, {
artifactPath: params.artifactPath,
tenantId: params.tenantId,
artifactType: params.artifactType,
Expand Down Expand Up @@ -105,7 +143,7 @@ export async function listDownloadableArtifacts(
tenantId?: string,
): Promise<DaemonArtifactInventoryEntry[]> {
const artifacts: DaemonArtifactInventoryEntry[] = [];
for (const [id, entry] of pendingArtifacts) {
for (const [id, entry] of pendingArtifacts()) {
if (!canReadArtifact(entry, tenantId)) continue;
let stat: fs.Stats;
try {
Expand Down Expand Up @@ -148,7 +186,7 @@ function readDownloadableArtifactEntry(
tenantId: string | undefined,
): { entry: ArtifactEntry; stat: fs.Stats } {
const entry = requireTenantOwnedEntry(
pendingArtifacts,
pendingArtifacts(),
artifactId,
tenantId,
DOWNLOADABLE_ARTIFACT_RESOURCE,
Expand Down Expand Up @@ -249,7 +287,7 @@ async function createDirectoryArchive(
sizeBytes: fs.statSync(archivePath).size,
};
// The artifact may be consumed or expire while tar is still archiving.
if (pendingArtifacts.get(artifactId) !== entry) {
if (pendingArtifacts().get(artifactId) !== entry) {
fs.rmSync(tempDir, { recursive: true, force: true });
throw expiredTenantOwnedEntryError(DOWNLOADABLE_ARTIFACT_RESOURCE, artifactId);
}
Expand Down Expand Up @@ -294,10 +332,10 @@ function hasUnsafeFilenameCharacter(fileName: string): boolean {
}

export function cleanupDownloadableArtifact(artifactId: string): void {
const entry = pendingArtifacts.get(artifactId);
const entry = pendingArtifacts().get(artifactId);
if (!entry) return;
clearTimeout(entry.timer);
pendingArtifacts.delete(artifactId);
pendingArtifacts().delete(artifactId);
cleanupDirectoryArchive(entry);
if (!entry.deleteAfterDownload) return;
try {
Expand All @@ -307,6 +345,14 @@ export function cleanupDownloadableArtifact(artifactId: string): void {
}
}

function removeBestEffort(targetPath: string): void {
Comment thread
thymikee marked this conversation as resolved.
Resolved
try {
fs.rmSync(targetPath, { recursive: true, force: true });
} catch {
// best-effort cleanup only
}
}

function cleanupDirectoryArchive(entry: ArtifactEntry): void {
if (!entry.directoryArchive) return;
try {
Expand Down Expand Up @@ -334,8 +380,6 @@ type UploadEntry = {
timer: ReturnType<typeof setTimeout>;
};

const pendingUploads = new Map<string, UploadEntry>();

export function trackUploadedArtifact(params: {
artifactPath: string;
tempDir: string;
Expand All @@ -346,7 +390,7 @@ export function trackUploadedArtifact(params: {
cleanupUploadedArtifact(uploadId);
}, UPLOAD_CLEANUP_TIMEOUT_MS);
timer.unref();
pendingUploads.set(uploadId, {
pendingUploads().set(uploadId, {
artifactPath: params.artifactPath,
tempDir: params.tempDir,
tenantId: params.tenantId,
Expand All @@ -357,7 +401,7 @@ export function trackUploadedArtifact(params: {

export function prepareUploadedArtifact(uploadId: string, tenantId?: string): string {
const entry = requireTenantOwnedEntry(
pendingUploads,
pendingUploads(),
uploadId,
tenantId,
UPLOADED_ARTIFACT_RESOURCE,
Expand All @@ -367,9 +411,9 @@ export function prepareUploadedArtifact(uploadId: string, tenantId?: string): st
}

export function cleanupUploadedArtifact(uploadId: string): void {
const entry = pendingUploads.get(uploadId);
const entry = pendingUploads().get(uploadId);
if (!entry) return;
clearTimeout(entry.timer);
pendingUploads.delete(uploadId);
pendingUploads().delete(uploadId);
fs.rmSync(entry.tempDir, { recursive: true, force: true });
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ test('daemon startup configures the Apple runner owner after acquiring the lock,
const source = fs.readFileSync(new URL('../http/daemon-runtime.ts', import.meta.url), 'utf8');
const acquiredLock = source.indexOf("if (acquisition.status !== 'acquired')");
const runnerOwnerConfigured = source.indexOf('await deps.ownerLifecycle.configureForDaemonLock(');
const lockFailureExit = source.indexOf('exit(', acquiredLock);
const lockFailureExit = source.indexOf("reason: 'daemon_registration_refused'", acquiredLock);

expect(acquiredLock).toBeGreaterThanOrEqual(0);
expect(lockFailureExit).toBeGreaterThan(acquiredLock);
expect(runnerOwnerConfigured).toBeGreaterThan(acquiredLock);
// The configure call sits inside the post-lock try block, after the failure branch that exits
// for a lock held by another process.
// The configure call comes after the failure branch that refuses a lock held by another process.
expect(runnerOwnerConfigured).toBeGreaterThan(lockFailureExit);
});

Expand Down
Loading
Loading