-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommandTarget.ts
More file actions
73 lines (64 loc) · 2.19 KB
/
commandTarget.ts
File metadata and controls
73 lines (64 loc) · 2.19 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
import type { SessionRecord } from '../protocol/schemas.js';
import { ERROR_CODES, makeCliError } from '../protocol/errors.js';
import { invariant } from '../util/assert.js';
import { readManifestIfExists } from '../storage/manifests.js';
import {
manifestPath as resolveManifestPath,
sessionDir,
socketPath as resolveSocketPath,
} from '../storage/sessionPaths.js';
import { assertSessionCommandable } from './sessionGuards.js';
export interface ResolveCommandTargetOptions {
home: string;
sessionId: string;
}
export type CommandTargetManifest = SessionRecord & { status: 'running' };
export interface CommandTarget {
sessionId: string;
sessionDirectory: string;
manifestPath: string;
socketPath: string;
manifest: CommandTargetManifest;
}
function assertRunningManifest(
manifest: SessionRecord,
): asserts manifest is CommandTargetManifest {
invariant(
manifest.status === 'running',
'command target manifest must be running after commandability check',
);
}
/**
* Resolve the live command target for input/control commands.
*
* Throws SESSION_NOT_FOUND when the manifest is missing,
* SESSION_ALREADY_DESTROYED for destroyed sessions, and SESSION_NOT_RUNNING
* for other non-commandable statuses. This deliberately does not check that
* the socket exists or is connectable; sendRpc() remains the host liveness
* boundary.
*/
export async function resolveCommandTarget(
options: ResolveCommandTargetOptions,
): Promise<CommandTarget> {
const sessionDirectory = sessionDir(options.home, options.sessionId);
const resolvedManifestPath = resolveManifestPath(sessionDirectory);
const manifest = await readManifestIfExists(resolvedManifestPath);
if (manifest === null) {
throw makeCliError(ERROR_CODES.SESSION_NOT_FOUND, {
message: `Session "${options.sessionId}" was not found.`,
details: {
sessionId: options.sessionId,
manifestPath: resolvedManifestPath,
},
});
}
assertSessionCommandable(manifest, options.sessionId);
assertRunningManifest(manifest);
return {
sessionId: options.sessionId,
sessionDirectory,
manifestPath: resolvedManifestPath,
socketPath: resolveSocketPath(sessionDirectory),
manifest,
};
}