Skip to content

Commit 0c81506

Browse files
thymikeeclaude
andcommitted
refactor(daemon): one predicate for an unreadable or invalid daemon record
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 0ffb8d0 commit 0c81506

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

‎packages/daemon-contracts/src/daemon-registration.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ export type DaemonStatus = Readonly<{
132132
plugins?: unknown;
133133
}>;
134134

135+
/** Whether the record exists but cannot say if a daemon runs: it is `unreadable` or `invalid`. */
136+
export function isUnusableRecord(status: Pick<DaemonStatus, 'state'>): boolean {
137+
return status.state === 'unreadable' || status.state === 'invalid';
138+
}
139+
135140
/**
136141
* Reads the daemon's published record without contacting it. The record counts while its pid names
137142
* a live process, unless a readable start time differs from the recorded one: a record a crashed

‎src/cli/commands/__tests__/daemon.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ vi.mock('@agent-device/daemon-contracts/daemon-shutdown-report', () => ({
2626
vi.mock('../../../platform-runtime-daemon-owner-cleanup.ts', () => ({
2727
createDaemonOwnerCleanup: () => ({ cleanup: mocks.cleanupDaemonOwner }),
2828
}));
29-
vi.mock('@agent-device/daemon-contracts/daemon-registration', () => ({
29+
vi.mock('@agent-device/daemon-contracts/daemon-registration', async (importOriginal) => ({
30+
...(await importOriginal<typeof import('@agent-device/daemon-contracts/daemon-registration')>()),
3031
readDaemonStatus: mocks.readDaemonStatus,
3132
}));
3233
vi.mock('../shared.ts', () => ({ writeCommandOutput: mocks.writeCommandOutput }));

‎src/cli/commands/daemon.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
type DaemonStopResult,
1010
} from '@agent-device/daemon-contracts/daemon-stop';
1111
import {
12+
isUnusableRecord,
1213
readDaemonStatus,
1314
type DaemonStatus,
1415
} from '@agent-device/daemon-contracts/daemon-registration';
@@ -140,7 +141,7 @@ function renderDaemonStop(
140141
function renderDaemonStatus(
141142
status: Omit<DaemonStatus, 'plugins'> & { plugins: readonly PluginInventoryEntry[] },
142143
): string {
143-
if (status.state === 'unreadable' || status.state === 'invalid') {
144+
if (isUnusableRecord(status)) {
144145
return `The daemon record for ${status.stateDir} is ${status.state === 'invalid' ? 'not a valid record' : 'unreadable'}, so whether a daemon runs there is unknown.`;
145146
}
146147
if (status.state === 'stopped') return `No running daemon for ${status.stateDir}.`;

‎src/cli/commands/plugins.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution';
22
import { readPluginInventory } from '@agent-device/contracts/plugin-inventory';
3-
import { readDaemonStatus } from '@agent-device/daemon-contracts/daemon-registration';
3+
import {
4+
isUnusableRecord,
5+
readDaemonStatus,
6+
} from '@agent-device/daemon-contracts/daemon-registration';
47
import { AppError } from '@agent-device/kernel/errors';
58
import { changePlugin, listPlugins } from '@agent-device/plugins/store';
69
import { BUILTIN_PROVIDER_IDS } from '../../builtin-provider-ids.ts';
@@ -36,9 +39,7 @@ export const pluginsCommand: ClientCommandHandler = async ({ positionals, flags
3639
...(daemon.state === 'running'
3740
? { daemon: { pid: daemon.pid, stateDir: daemon.stateDir, plugins: daemonPlugins } }
3841
: {}),
39-
...(daemon.state === 'unreadable' || daemon.state === 'invalid'
40-
? { daemonRecord: daemon.state }
41-
: {}),
42+
...(isUnusableRecord(daemon) ? { daemonRecord: daemon.state } : {}),
4243
restartRequired: action !== 'list',
4344
};
4445
await writeCommandOutput(flags, data, () =>
@@ -54,7 +55,7 @@ export const pluginsCommand: ClientCommandHandler = async ({ positionals, flags
5455
? ['Restart the local daemon after closing active sessions to use the changed plugin set.']
5556
: []),
5657
...(plugins.length === 0 ? ['No provider plugins installed.'] : []),
57-
...(daemon.state === 'unreadable' || daemon.state === 'invalid'
58+
...(isUnusableRecord(daemon)
5859
? [
5960
`The daemon record for ${daemon.stateDir} is ${daemon.state}, so its providers are unknown.`,
6061
]

0 commit comments

Comments
 (0)