Skip to content

Commit 0fa2373

Browse files
thymikeeclaude
andcommitted
fix(daemon-client): always name --state-dir in the not-running hint
The default-directory comparison ignored AGENT_DEVICE_STATE_DIR, so a caller passing the default directory while the shell exports that variable got a hint that starts a daemon elsewhere. The retirement-unconfirmed error factory moves to daemon-lifecycle-errors.ts. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 11200c0 commit 0fa2373

5 files changed

Lines changed: 27 additions & 32 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

5-
- The `daemon-not-running` hint now names `--state-dir <dir>` when the state directory is not the default, and a daemon force-retired for outlasting its drain is reported as `Replaced unreachable daemon` instead of `Replaced idle daemon`. `daemon-drain-timeout` and the post-drain force retire are documented in the client API and `help`.
5+
- The `daemon-not-running` hint now always names `--state-dir <dir>`, and a daemon force-retired for outlasting its drain is reported as `Replaced unreachable daemon` instead of `Replaced idle daemon`. `daemon-drain-timeout` and the post-drain force retire are documented in the client API and `help`.
66
- Fix `ensureDaemon()`, the CLI and MCP killing a daemon they had just asked to leave. After the daemon answers the quiesce request with `draining`, the client now waits up to 60 s for it to exit by itself and clean up its registration, so plugin disposal and lease finalization finish (on Windows a signal ends the process outright, and on POSIX a drain longer than 3 s was killed). A daemon still running at the deadline that fails its liveness probes is force-retired; one that still answers is left running and the call fails with `details.reason: 'daemon-drain-timeout'`, with a hint naming `agent-device daemon stop --state-dir <path>`.
77
- `@agent-device/testmu` is now a version 2 provider plugin. `import testmu from '@agent-device/testmu'` gives a factory, `testmu({ credentials: { username, accessKey } })`, that reads those credentials instead of `LT_USERNAME` and `LT_ACCESS_KEY`; without options it reads the variables as before. The package still declares the version 1 plugin entry, so `agent-device plugins add @agent-device/testmu` keeps working on hosts that predate the version 2 contract.
88
- Provider plugins have a new contract. A package exports a factory at `./agent-device` that returns `{ apiVersion: 2, provider, credentials, connection?, setup(host) }`, typed by `AgentDevicePluginFactory` from `agent-device/plugins`. The store calls the factory with the package's `config.json` options, and a JavaScript host can call it directly. Version 1 plugins (`agentDevicePlugin.apiVersion: 1`) keep loading.

‎src/daemon-client/__tests__/daemon-client-explicit-lifecycle.test.ts‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import path from 'node:path';
44
import { afterEach, test, vi } from 'vitest';
55
import { closeLoopbackServer, supportsLoopbackBind } from '@agent-device/test-kit';
66
import { AppError } from '@agent-device/kernel/errors';
7-
import {
8-
DAEMON_QUIESCE_COMMAND,
9-
resolveDaemonPaths,
10-
} from '@agent-device/daemon-contracts/daemon-resolution';
7+
import { DAEMON_QUIESCE_COMMAND } from '@agent-device/daemon-contracts/daemon-resolution';
118
import { DAEMON_STARTUP_EXIT_CODES } from '@agent-device/daemon-contracts/daemon-registration-owner';
129
import { isProcessAlive } from '@agent-device/host-kit/process';
1310
import { readVersion } from '@agent-device/host-kit/version';
@@ -42,7 +39,6 @@ import { sleep } from '@agent-device/host-kit/retry';
4239
import * as transport from '../daemon-client-transport.ts';
4340
import { sendToDaemon } from '../daemon-client.ts';
4441
import { ensureDaemon } from '../daemon-client-ensure.ts';
45-
import { daemonNotRunningError } from '../daemon-lifecycle-errors.ts';
4642

4743
// The explicit lifecycle: what an attach client may never do, and what `ensureDaemon` may do only
4844
// through the daemon's own answer. Daemons here are loopback stand-ins registered by real children,
@@ -116,12 +112,6 @@ test('attach reports a missing daemon without starting one', async (t) => {
116112
assert.equal(fs.existsSync(paths.infoPath), false);
117113
});
118114

119-
test('the not-running hint leaves out --state-dir for the default state directory', () => {
120-
const error = daemonNotRunningError(resolveDaemonPaths(undefined).baseDir);
121-
122-
assert.match(String(error.details?.hint), /\(agent-device devices, for example\)/);
123-
});
124-
125115
test('attach reports an incompatible daemon with both identities and leaves it running', async (t) => {
126116
if (!(await withLoopback(t))) return;
127117
const { stateDir, paths } = stateDirAndPaths();

‎src/daemon-client/daemon-client-ensure.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
assertDaemonPolicyMatches,
1212
observeLocalDaemon,
1313
resolveLocalDaemonSettings,
14-
daemonRetirementUnconfirmedError,
1514
retireDaemonForTakeover,
1615
stopDaemonForTakeover,
1716
startLocalDaemon,
@@ -26,6 +25,7 @@ import {
2625
daemonIdentityOf,
2726
daemonDrainTimeoutError,
2827
daemonIncompatibleError,
28+
daemonRetirementUnconfirmedError,
2929
} from './daemon-lifecycle-errors.ts';
3030

3131
// A racing `ensureDaemon` in another process can retire or replace the daemon this call judged, or

‎src/daemon-client/daemon-client-lifecycle.ts‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import type { DaemonWireRequest } from '@agent-device/contracts/command';
22
import type { DaemonLifecycle, EnsureDaemonOptions } from '@agent-device/contracts/client';
33
import type { DaemonResponse } from '@agent-device/kernel/contracts';
4-
import { daemonContendedError, daemonStopCommand } from './daemon-lifecycle-errors.ts';
4+
import {
5+
daemonContendedError,
6+
daemonRetirementUnconfirmedError,
7+
daemonStopCommand,
8+
} from './daemon-lifecycle-errors.ts';
59
import { attachActiveSessionAddressHint } from './daemon-client-address-hints.ts';
610
import fs from 'node:fs';
711
import { processLockHoldsDaemonIdentity } from '@agent-device/daemon-contracts/daemon-registration';
@@ -366,17 +370,6 @@ export async function stopDaemonForTakeover(
366370
});
367371
}
368372

369-
export function daemonRetirementUnconfirmedError(
370-
retirement: Extract<DaemonRetirementResult, { status: 'retained' }>,
371-
paths: DaemonPaths,
372-
): AppError {
373-
return new AppError('COMMAND_FAILED', 'Daemon replacement could not be confirmed.', {
374-
reason: 'daemon_retirement_unconfirmed',
375-
retirement,
376-
hint: retirement.error?.hint ?? resolveDaemonStartupHint(getDaemonMetadataState(paths), paths),
377-
});
378-
}
379-
380373
export async function retireDaemonForTakeover(
381374
existing: DaemonInfo,
382375
paths: DaemonPaths,

‎src/daemon-client/daemon-lifecycle-errors.ts‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import type { DaemonIdentity } from '@agent-device/contracts/client';
22
import {
33
DAEMON_QUIESCE_REASONS,
4-
resolveDaemonPaths,
4+
type DaemonPaths,
55
type DaemonQuiesceCensus,
66
} from '@agent-device/daemon-contracts/daemon-resolution';
7+
import type { DaemonRetirementResult } from '@agent-device/daemon-contracts/daemon-registration-owner';
78
import { AppError } from '@agent-device/kernel/errors';
89
import { shellQuoteIfNeeded } from '@agent-device/kernel/device-shell';
910
import { readVersion } from '@agent-device/host-kit/version';
1011
import type { DaemonClientSettings } from './daemon-client-lifecycle.ts';
11-
import type { DaemonInfo } from './daemon-client-metadata.ts';
12+
import {
13+
getDaemonMetadataState,
14+
resolveDaemonStartupHint,
15+
type DaemonInfo,
16+
} from './daemon-client-metadata.ts';
1217
import { resolveLocalDaemonCodeIdentity, type DaemonTakeoverCause } from './daemon-launch-spec.ts';
1318

1419
// The four ways an explicit lifecycle stops short of a usable daemon. Callers branch on
@@ -58,15 +63,22 @@ export function daemonContendedError(): AppError {
5863
}
5964

6065
export function daemonNotRunningError(stateDir: string, registered?: DaemonInfo): AppError {
61-
const stateDirFlag =
62-
stateDir === resolveDaemonPaths(undefined).baseDir
63-
? ''
64-
: ` --state-dir ${shellQuoteIfNeeded(stateDir)}`;
6566
return new AppError('COMMAND_FAILED', 'No daemon is running for this state directory.', {
6667
reason: DAEMON_LIFECYCLE_REASONS.notRunning,
6768
stateDir,
6869
...(registered ? { daemon: daemonIdentityOf(registered) } : {}),
69-
hint: `Start one with a local agent-device command that uses the daemon (agent-device devices${stateDirFlag}, for example), or with ensureDaemon({ stateDir: ${JSON.stringify(stateDir)} }) from the SDK, before attaching.${registered ? ` The registered daemon (pid ${registered.pid}) does not answer; ${daemonStopCommand(stateDir)} clears it.` : ''}`,
70+
hint: `Start one with a local agent-device command that uses the daemon (agent-device devices --state-dir ${shellQuoteIfNeeded(stateDir)}, for example), or with ensureDaemon({ stateDir: ${JSON.stringify(stateDir)} }) from the SDK, before attaching.${registered ? ` The registered daemon (pid ${registered.pid}) does not answer; ${daemonStopCommand(stateDir)} clears it.` : ''}`,
71+
});
72+
}
73+
74+
export function daemonRetirementUnconfirmedError(
75+
retirement: Extract<DaemonRetirementResult, { status: 'retained' }>,
76+
paths: DaemonPaths,
77+
): AppError {
78+
return new AppError('COMMAND_FAILED', 'Daemon replacement could not be confirmed.', {
79+
reason: 'daemon_retirement_unconfirmed',
80+
retirement,
81+
hint: retirement.error?.hint ?? resolveDaemonStartupHint(getDaemonMetadataState(paths), paths),
7082
});
7183
}
7284

0 commit comments

Comments
 (0)