Repository navigation
Expand file tree
/
Copy pathdaemon.ts
More file actions
153 lines (145 loc) · 6.76 KB
/
Copy pathdaemon.ts
File metadata and controls
153 lines (145 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution';
import {
readDaemonStopIdentity,
stopDaemon,
type DaemonStopResult,
} from '@agent-device/daemon-contracts/daemon-stop';
import { readDaemonStatus, type DaemonStatus } from '@agent-device/daemon-contracts/daemon-status';
import { readDaemonShutdownReport } from '@agent-device/daemon-contracts/daemon-shutdown-report';
import { AppError } from '@agent-device/kernel/errors';
import { writeCommandOutput } from './shared.ts';
import type { ClientCommandHandler } from './router-types.ts';
import { createDaemonOwnerCleanup } from '../../platform-runtime-daemon-owner-cleanup.ts';
const daemonOwnerCleanup = createDaemonOwnerCleanup();
export const daemonCommand: ClientCommandHandler = async ({ positionals, flags }) => {
const subcommand = positionals[0];
if ((subcommand !== 'stop' && subcommand !== 'status') || positionals.length !== 1) {
throw new AppError('INVALID_ARGS', 'daemon accepts only: stop, status');
}
const paths = resolveDaemonPaths(flags.stateDir);
if (subcommand === 'status') {
const status = readDaemonStatus(paths);
await writeCommandOutput(flags, status, () => renderDaemonStatus(status));
return true;
}
const identity = readDaemonStopIdentity(paths.infoPath);
const stopped = await stopDaemon({ paths });
const report = stopped.mode === 'graceful' ? readDaemonShutdownReport(paths.baseDir) : null;
const result = mergeShutdownReport(stopped, report);
const shouldClean = flags.clean === true && identity !== null && result.stopped;
if (shouldClean) {
await daemonOwnerCleanup.cleanup({
pid: identity.pid,
startTime: identity.processStartTime,
});
}
const data = { ...result, clean: shouldClean };
await writeCommandOutput(flags, data, () => renderDaemonStop(data));
return true;
};
function mergeShutdownReport(
stopped: DaemonStopResult,
report: ReturnType<typeof readDaemonShutdownReport>,
): DaemonStopResult {
if (stopped.mode !== 'graceful' || report) {
return report
? {
...stopped,
providerReleases: { status: 'completed', ...report.providerReleases },
claimsReleased: report.claims.released,
claimsOrphaned: report.claims.orphaned,
claimsSuperseded: report.claims.superseded,
claimsUnattributable: report.claims.unattributable,
warnings: [
...stopped.warnings,
...supersededClaimWarnings(report.claims.superseded),
...orphanedClaimWarnings(report.claims.orphaned),
...unattributableClaimWarnings(report.claims.unattributable),
],
}
: stopped;
}
return {
...stopped,
cleanupConfidence: 'unknown',
providerReleases: { status: 'unknown', released: [], pending: null },
warnings: [
...stopped.warnings,
'The graceful shutdown report is unavailable, so provider cleanup state is unknown. Provider allocations may remain active.',
],
};
}
/** A superseded claim is not a failure to report as one, but the operator's
* device is now owned elsewhere, so it must not pass silently. */
function supersededClaimWarnings(superseded: DaemonStopResult['claimsSuperseded']): string[] {
if (superseded.length === 0) return [];
const devices = superseded.map((claim) => claim.deviceId).join(', ');
return [
`Another owner had already claimed ${devices} before this daemon released it, so those devices are now owned elsewhere.`,
];
}
/** An orphaned claim keeps holding its device after the daemon is gone, and
* only `device release --stale` or the next open settles it - say so instead
* of leaving the block discoverable through --json alone.
*
* A record whose owner could not be attributed is NOT reported here. Those are what
* `--stale` hides and refuses, so this sentence would send the operator to a route that cannot
* succeed; {@link unattributableClaimWarnings} names the view that does show them. */
function orphanedClaimWarnings(orphaned: DaemonStopResult['claimsOrphaned']): string[] {
if (orphaned.length === 0) return [];
const devices = orphaned.map((claim) => claim.deviceId).join(', ');
return [
`Ownership of ${devices} was not released cleanly; the claim now blocks other owners until it is settled. Inspect with: agent-device device status --stale, then release with: agent-device device release --stale.`,
];
}
/**
* What the three provable buckets each get an exact remedy for, this one does not: settling a claim
* always rests on a proof about its owner, and this record names none. So the warning states only
* what is known, points at the view that lists these records — the default one, because `--stale`
* filters records without a decodable owner out — and does not promise a release that would clear
* them. `device release --stale` refuses every record that names no owner, and the next `open` refuses
* to overwrite one.
*/
function unattributableClaimWarnings(
unattributable: DaemonStopResult['claimsUnattributable'],
): string[] {
if (unattributable.length === 0) return [];
const devices = unattributable.map((claim) => claim.deviceId).join(', ');
return [
`A claim for ${devices} remains whose owner could not be read, so this daemon cannot say whether those devices are free. ` +
'No device release route settles it: --stale proves staleness from the recorded owner and refuses records that name none, ' +
'and open refuses to overwrite them. Inspect with: agent-device device status.',
];
}
function renderDaemonStop(
result: Pick<DaemonStopResult, 'stopped' | 'mode' | 'warnings'> & {
clean: boolean;
},
): string {
const headline = result.stopped ? `Daemon stopped (${result.mode}).` : 'No running daemon found.';
return [headline, result.clean ? 'Retained runner cleanup completed.' : null, ...result.warnings]
.filter((line): line is string => Boolean(line))
.join('\n');
}
function renderDaemonStatus(status: DaemonStatus): string {
if (!status.running) return `No running daemon for ${status.stateDir}.`;
const details = [`pid ${status.pid}`, status.version, status.transport].filter(Boolean);
return [
`Daemon running (${details.join(', ')}) for ${status.stateDir}.`,
...(status.plugins.length === 0
? ['No providers registered.']
: [
'Providers:',
...status.plugins.map((plugin) => {
const origin = plugin.package
? `${plugin.package}${plugin.version ? `@${plugin.version}` : ''}`
: plugin.source;
const outcome =
plugin.state === 'active'
? 'active'
: `failed: ${plugin.error?.message ?? 'unknown'}`;
return ` ${plugin.provider ?? '(unknown provider)'} [${plugin.source}] ${origin} - ${outcome}`;
}),
]),
].join('\n');
}