Skip to content

Commit 72f840a

Browse files
committed
perf(daemon): stop eagerly loading the CLI schema layer and screenshot surface
Plugin discovery imported the CLI config loader for one path helper, dragging option schemas and every command family into the daemon's eager closure; that plugins -> commands edge was the zone graph's only one. The helper needs only env and the home expansions, so it now lives beside them in host-kit, which both callers already imported. The generic dispatcher imported the screenshot runtime statically while awaiting it on a request-only branch. Behind that branch it owns the in-process command surface and the interaction implementations beneath it. Measured over value edges from src/daemon.ts: 651 files / 103,629 loc -> 468 files / 76,166 loc. Both cuts are needed for the full win; seven files drop only when neither path stays eager.
1 parent 8829a2e commit 72f840a

8 files changed

Lines changed: 81 additions & 18 deletions

File tree

‎packages/host-kit/src/file.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ export {
1111
openVerifiedFileForRead,
1212
openVerifiedFileForTruncate,
1313
} from './internal/verified-file.ts';
14-
export { expandUserHomePath, resolveUserPath } from './internal/path-resolution.ts';
14+
export {
15+
expandUserHomePath,
16+
resolveUserConfigPath,
17+
resolveUserPath,
18+
} from './internal/path-resolution.ts';
1519
export {
1620
acquireProcessLock,
1721
acquireProcessLockAcquisition,

‎packages/host-kit/src/internal/path-resolution.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { test } from 'vitest';
22
import assert from 'node:assert/strict';
33
import path from 'node:path';
4-
import { expandUserHomePath, resolveUserPath } from './path-resolution.ts';
4+
import { expandUserHomePath, resolveUserConfigPath, resolveUserPath } from './path-resolution.ts';
55

66
test('expandUserHomePath expands the current user home prefix', () => {
77
const env = { HOME: '/tmp/agent-device-home' };
@@ -23,3 +23,21 @@ test('resolveUserPath expands home-prefixed and absolute paths', () => {
2323
);
2424
assert.equal(resolveUserPath(absolutePath, { cwd: '/tmp/ignored', env }), absolutePath);
2525
});
26+
27+
// The plugin store and the CLI config loader both need this answer, so it is owned here rather
28+
// than behind either of them; these are the three shapes both consumers depend on.
29+
test('resolveUserConfigPath resolves the home, the relocated home, and rejects a relative one', () => {
30+
assert.equal(
31+
resolveUserConfigPath({ HOME: '/tmp/agent-device-home' }),
32+
path.join('/tmp/agent-device-home', '.agent-device', 'config.json'),
33+
);
34+
// `~/...` expands through this same env object, so HOME is pinned here too: omitting it would
35+
// let the expectation drift to `os.homedir()` while the expansion went elsewhere.
36+
assert.equal(
37+
resolveUserConfigPath({ HOME: '/tmp/agent-device-home', AGENT_DEVICE_HOME: '~/relocated' }),
38+
path.join('/tmp/agent-device-home', 'relocated', 'config.json'),
39+
);
40+
assert.throws(() => resolveUserConfigPath({ AGENT_DEVICE_HOME: './relative' }), {
41+
code: 'INVALID_ARGS',
42+
});
43+
});

‎packages/host-kit/src/internal/path-resolution.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os from 'node:os';
22
import path from 'node:path';
3+
import { AppError } from '@agent-device/kernel/errors';
34
import type { EnvMap } from '@agent-device/kernel/source-value';
45

56
type PathResolutionOptions = {
@@ -24,3 +25,21 @@ export function resolveUserPath(inputPath: string, options: PathResolutionOption
2425
if (path.isAbsolute(expandedPath)) return expandedPath;
2526
return path.resolve(options.cwd ?? process.cwd(), expandedPath);
2627
}
28+
29+
/**
30+
* The user's `config.json`, from `AGENT_DEVICE_HOME` or `~/.agent-device`.
31+
*
32+
* This lives beside the other expansions rather than in the CLI's config loader because it is the
33+
* same question those two answer — a location named by the environment becomes an absolute path —
34+
* and it answers it with nothing but `env`: no option schema, no flag grammar, no command facet.
35+
* Plugin discovery needs the answer at daemon start and must not acquire the CLI's schema layer to
36+
* get it (`src/plugins/store.ts`), which is a 153-file zone rather than a path helper.
37+
*/
38+
export function resolveUserConfigPath(env: EnvMap): string {
39+
const home = env.AGENT_DEVICE_HOME
40+
? expandUserHomePath(env.AGENT_DEVICE_HOME, { env })
41+
: path.join(expandUserHomePath('~', { env }), '.agent-device');
42+
if (!path.isAbsolute(home))
43+
throw new AppError('INVALID_ARGS', 'AGENT_DEVICE_HOME must be absolute or ~/...');
44+
return path.join(home, 'config.json');
45+
}

‎src/commands/schema/cli-config.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'node:fs';
33
import path from 'node:path';
44
import { test } from 'vitest';
55
import { pluginHome } from '../../plugins/plugin.fixtures.ts';
6-
import { resolveConfigBackedFlagDefaults, resolveUserConfigPath } from './cli-config.ts';
6+
import { resolveConfigBackedFlagDefaults } from './cli-config.ts';
77

88
test('AGENT_DEVICE_HOME relocates user defaults; project and explicit config cannot select plugins', () => {
99
const { home, env } = pluginHome();
@@ -22,5 +22,9 @@ test('AGENT_DEVICE_HOME relocates user defaults; project and explicit config can
2222
};
2323
assert.throws(() => resolveConfigBackedFlagDefaults(explicit), { code: 'INVALID_ARGS' });
2424
const relative = { AGENT_DEVICE_HOME: './relative' };
25-
assert.throws(() => resolveUserConfigPath(relative), { code: 'INVALID_ARGS' });
25+
// The rejection is owned by resolveUserConfigPath (host-kit); what this loader owes its own
26+
// callers is surfacing it, which the direct call below would not have proven.
27+
assert.throws(() => resolveConfigBackedFlagDefaults({ ...options, env: relative }), {
28+
code: 'INVALID_ARGS',
29+
});
2630
});

‎src/commands/schema/cli-config.ts‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { isRecord } from '@agent-device/kernel/record';
66
import { mergeDefinedFlags } from './merge-flags.ts';
77
import { type FlagKey } from '@agent-device/command-registry/flag-types';
88
import { projectConfigFlagKeys } from '@agent-device/command-registry/flag-registry';
9-
import { expandUserHomePath, resolveUserPath } from '@agent-device/host-kit/file';
9+
import { resolveUserConfigPath, resolveUserPath } from '@agent-device/host-kit/file';
1010
import {
1111
getConfigurableOptionSpecs,
1212
getOptionSpec,
@@ -56,15 +56,6 @@ function resolveConfigPaths(
5656
];
5757
}
5858

59-
export function resolveUserConfigPath(env: EnvMap): string {
60-
const home = env.AGENT_DEVICE_HOME
61-
? expandUserHomePath(env.AGENT_DEVICE_HOME, { env })
62-
: path.join(expandUserHomePath('~', { env }), '.agent-device');
63-
if (!path.isAbsolute(home))
64-
throw new AppError('INVALID_ARGS', 'AGENT_DEVICE_HOME must be absolute or ~/...');
65-
return path.join(home, 'config.json');
66-
}
67-
6859
function resolveInputPath(inputPath: string, cwd: string, env: EnvMap): string {
6960
return resolveUserPath(inputPath, { cwd, env });
7061
}

‎src/daemon/__tests__/generic-route-runtime-completeness.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,23 @@ test('the routing gap is what an unrouted command actually reaches', async () =>
6060
// Without this the test above would pass for a table that had no arms at all.
6161
expect(await reachesGenericRoutingGap('not-a-generic-route-command')).toBe(true);
6262
});
63+
64+
test('the screenshot arm reaches its leaf through the deferred import', async () => {
65+
// The leaf loads with `await import()` at the arm so the daemon starts without the
66+
// command surface beneath it. A typo'd specifier or a bundler that dropped the chunk
67+
// would fail here as a resolution error, and an arm that skipped the leaf would answer
68+
// with the routing gap — neither reaches the retired-flag refusal, which only the leaf
69+
// itself raises before any admission or device work.
70+
await expect(
71+
resolveGenericRuntimeExecution({
72+
req: {
73+
command: 'screenshot',
74+
token: '',
75+
positionals: [],
76+
flags: { screenshotMaxSize: 1024 },
77+
},
78+
session: { device: { id: 'device-1', platform: 'android', kind: 'device' } },
79+
context: { logPath: '' },
80+
} as unknown as Parameters<typeof resolveGenericRuntimeExecution>[0]),
81+
).rejects.toThrow(/--max-size was removed/);
82+
});

‎src/daemon/generic-runtime-execution.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { ResolvedGenericExecution } from './generic-leaf-execution.ts';
22
import { resolveBoundFocusRuntime } from './focus-runtime.ts';
3-
import { resolveScreenshotGenericExecution } from './screenshot-runtime.ts';
43
import { resolveBoundScrollRuntime } from './scroll-runtime.ts';
54
import type { ScreenshotRuntimeBindings } from './screenshot-runtime-binding.ts';
65
import type { DaemonCommandContext } from './context.ts';
@@ -41,8 +40,13 @@ export async function resolveGenericRuntimeExecution(
4140
});
4241
}
4342
switch (params.req.command) {
44-
case 'screenshot':
43+
case 'screenshot': {
44+
// This branch alone owns an in-process command surface and the interaction implementations
45+
// beneath it; the other leaves here are bound runtimes the dispatcher reaches directly, so
46+
// only this one's module is deferred out of the daemon's startup closure.
47+
const { resolveScreenshotGenericExecution } = await import('./screenshot-runtime.ts');
4548
return await resolveScreenshotGenericExecution(params);
49+
}
4650
case 'focus':
4751
return await resolveBoundFocusRuntime({
4852
device: params.session.device,

‎src/plugins/store.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import path from 'node:path';
44
import { AppError, normalizeError } from '@agent-device/kernel/errors';
55
import { isRecord } from '@agent-device/kernel/record';
66
import { runCmd } from '@agent-device/host-kit/command';
7-
import { acquireProcessLock, publishFileSync } from '@agent-device/host-kit/file';
7+
import {
8+
acquireProcessLock,
9+
publishFileSync,
10+
resolveUserConfigPath,
11+
} from '@agent-device/host-kit/file';
812
import { readCurrentOwnerIdentity } from '@agent-device/host-kit/process';
9-
import { resolveUserConfigPath } from '../commands/schema/cli-config.ts';
1013
import {
1114
readPluginManifest,
1215
assertUniquePluginProviders,

0 commit comments

Comments
 (0)