@adhdev/session-host-core 1.0.82-rc.30 → 1.0.82-rc.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +72 -2
- package/dist/index.d.ts +72 -2
- package/dist/index.js +87 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +85 -13
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/instance-key.ts +20 -1
- package/src/ipc.ts +32 -13
- package/src/registry.ts +46 -0
- package/src/types.ts +47 -1
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -82,6 +82,7 @@ export {
|
|
|
82
82
|
disconnectSessionHostClients,
|
|
83
83
|
findConnectedSessionHostClient,
|
|
84
84
|
getDefaultSessionHostEndpoint,
|
|
85
|
+
validateSessionHostEndpoint,
|
|
85
86
|
writeEnvelope,
|
|
86
87
|
} from './ipc.js';
|
|
87
88
|
export type {
|
|
@@ -93,6 +94,7 @@ export type {
|
|
|
93
94
|
} from './ipc.js';
|
|
94
95
|
export {
|
|
95
96
|
DEFAULT_CONFIG_DIR_NAME,
|
|
97
|
+
assertTestStorageIsolated,
|
|
96
98
|
canonicalizeInstancePath,
|
|
97
99
|
isDefaultInstanceConfigDir,
|
|
98
100
|
resolveInstanceConfigDir,
|
package/src/instance-key.ts
CHANGED
|
@@ -28,6 +28,23 @@ import * as path from 'path';
|
|
|
28
28
|
|
|
29
29
|
export const DEFAULT_CONFIG_DIR_NAME = '.adhdev';
|
|
30
30
|
|
|
31
|
+
/** Last-line storage guard, shared by session-host and mesh persistence.
|
|
32
|
+
* Node's test runner propagates NODE_TEST_CONTEXT to children; Vitest marks
|
|
33
|
+
* its workers separately. Explicit temporary roots remain valid. */
|
|
34
|
+
export function assertTestStorageIsolated(destination: string): void {
|
|
35
|
+
const env = process.env;
|
|
36
|
+
if (!env.VITEST && env.NODE_ENV !== 'test' && !env.NODE_TEST_CONTEXT
|
|
37
|
+
&& !process.execArgv.some(arg => arg === '--test' || arg.startsWith('--test='))) return;
|
|
38
|
+
const target = canonicalizeInstancePath(destination);
|
|
39
|
+
const home = os.userInfo().homedir;
|
|
40
|
+
for (const track of ['.adhdev', '.adhdev-preview']) {
|
|
41
|
+
const live = canonicalizeInstancePath(path.join(home, track));
|
|
42
|
+
if (target === live || target.startsWith(live + path.sep)) {
|
|
43
|
+
throw new Error('LIVE runtime storage is forbidden in a test process; pin ADHDEV_CONFIG_DIR or storageRootDir to a temporary directory');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
31
48
|
/**
|
|
32
49
|
* Resolve the instance config dir from the environment: ADHDEV_CONFIG_DIR
|
|
33
50
|
* when set (trimmed), else `<homeDir>/.adhdev`. Mirrors daemon-core's
|
|
@@ -49,7 +66,9 @@ export function resolveInstanceConfigDir(
|
|
|
49
66
|
homeDir: string = os.homedir(),
|
|
50
67
|
): string {
|
|
51
68
|
const override = typeof env.ADHDEV_CONFIG_DIR === 'string' ? env.ADHDEV_CONFIG_DIR.trim() : '';
|
|
52
|
-
|
|
69
|
+
const resolved = override || path.join(homeDir, DEFAULT_CONFIG_DIR_NAME);
|
|
70
|
+
if (env === process.env) assertTestStorageIsolated(resolved);
|
|
71
|
+
return resolved;
|
|
53
72
|
}
|
|
54
73
|
|
|
55
74
|
/**
|
package/src/ipc.ts
CHANGED
|
@@ -18,6 +18,20 @@ export interface SessionHostEndpoint {
|
|
|
18
18
|
path: string;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
/** Reserve the trailing NUL in sockaddr_un; named pipes have a separate namespace. */
|
|
22
|
+
export function validateSessionHostEndpoint(
|
|
23
|
+
endpoint: SessionHostEndpoint,
|
|
24
|
+
platform: NodeJS.Platform = process.platform,
|
|
25
|
+
): void {
|
|
26
|
+
if (endpoint.kind !== 'unix') return;
|
|
27
|
+
// Darwin/BSD use 104 bytes; Linux provides 108. Default conservatively elsewhere.
|
|
28
|
+
const limit = platform === 'linux' ? 108 : 104;
|
|
29
|
+
const pathBytes = Buffer.byteLength(endpoint.path, 'utf8');
|
|
30
|
+
if (pathBytes + 1 > limit) {
|
|
31
|
+
throw new Error(`session_host_unix_socket_path_too_long: path_bytes=${pathBytes} required_bytes=${pathBytes + 1} limit_bytes=${limit} platform=${platform}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
21
35
|
export interface SessionHostEndpointOptions {
|
|
22
36
|
/**
|
|
23
37
|
* Instance IPC namespace key (see instance-key.ts). '' / omitted yields the
|
|
@@ -86,18 +100,19 @@ function serializeEnvelope(envelope: SessionHostWireEnvelope): string {
|
|
|
86
100
|
function createLineParser<TEnvelope = SessionHostWireEnvelope>(
|
|
87
101
|
onEnvelope: (envelope: TEnvelope) => void,
|
|
88
102
|
) {
|
|
89
|
-
let
|
|
103
|
+
let fragments: string[] = [];
|
|
90
104
|
const decoder = new StringDecoder('utf8');
|
|
91
105
|
const parser = (chunk: Buffer | string) => {
|
|
92
|
-
|
|
93
|
-
let
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
106
|
+
const text = typeof chunk === 'string' ? chunk : decoder.write(chunk);
|
|
107
|
+
let start = 0;
|
|
108
|
+
for (;;) {
|
|
109
|
+
const newline = text.indexOf('\n', start);
|
|
110
|
+
if (newline < 0) { if (start < text.length) fragments.push(text.slice(start)); break; }
|
|
111
|
+
fragments.push(text.slice(start, newline));
|
|
112
|
+
const rawLine = fragments.join('').trim();
|
|
113
|
+
fragments = [];
|
|
114
|
+
start = newline + 1;
|
|
115
|
+
if (rawLine) onEnvelope(JSON.parse(rawLine) as TEnvelope);
|
|
101
116
|
}
|
|
102
117
|
};
|
|
103
118
|
/**
|
|
@@ -113,8 +128,8 @@ function createLineParser<TEnvelope = SessionHostWireEnvelope>(
|
|
|
113
128
|
* mid-write into a crash of the process observing it.
|
|
114
129
|
*/
|
|
115
130
|
parser.end = (): string => {
|
|
116
|
-
const remainder =
|
|
117
|
-
|
|
131
|
+
const remainder = fragments.join('') + decoder.end();
|
|
132
|
+
fragments = [];
|
|
118
133
|
return remainder;
|
|
119
134
|
};
|
|
120
135
|
return parser;
|
|
@@ -377,7 +392,11 @@ export class SessionHostClient {
|
|
|
377
392
|
}
|
|
378
393
|
|
|
379
394
|
if (envelope.kind === 'event') {
|
|
380
|
-
for (const listener of this.eventListeners)
|
|
395
|
+
for (const listener of this.eventListeners) {
|
|
396
|
+
// An observer must never abort frame draining and strand a response
|
|
397
|
+
// behind an event in the same socket chunk.
|
|
398
|
+
try { listener(envelope.event); } catch { /* observer owns its error */ }
|
|
399
|
+
}
|
|
381
400
|
}
|
|
382
401
|
});
|
|
383
402
|
socket.on('data', parser);
|
package/src/registry.ts
CHANGED
|
@@ -20,6 +20,14 @@ interface SessionRuntimeState {
|
|
|
20
20
|
|
|
21
21
|
export class SessionHostRegistry {
|
|
22
22
|
private sessions = new Map<string, SessionRuntimeState>();
|
|
23
|
+
/** This registry's lifetime; every inputEpoch it mints starts with it. */
|
|
24
|
+
private readonly lifetimeId = generateUUID();
|
|
25
|
+
private epochCounter = 0;
|
|
26
|
+
|
|
27
|
+
private mintInputEpoch(): string {
|
|
28
|
+
this.epochCounter += 1;
|
|
29
|
+
return `${this.lifetimeId}:${this.epochCounter}`;
|
|
30
|
+
}
|
|
23
31
|
|
|
24
32
|
createSession(payload: CreateSessionPayload): SessionHostRecord {
|
|
25
33
|
const sessionId = payload.sessionId || generateUUID();
|
|
@@ -55,6 +63,8 @@ export class SessionHostRegistry {
|
|
|
55
63
|
lifecycle: 'starting',
|
|
56
64
|
writeOwner: null,
|
|
57
65
|
attachedClients: initialClient,
|
|
66
|
+
inputSeq: 0,
|
|
67
|
+
inputEpoch: this.mintInputEpoch(),
|
|
58
68
|
buffer: {
|
|
59
69
|
scrollbackBytes: 0,
|
|
60
70
|
snapshotSeq: 0,
|
|
@@ -78,6 +88,16 @@ export class SessionHostRegistry {
|
|
|
78
88
|
|
|
79
89
|
restoreSession(record: SessionHostRecord, snapshot?: { seq: number; text: string } | null): SessionHostRecord {
|
|
80
90
|
const cloned = this.cloneRecord(record);
|
|
91
|
+
// A record persisted by a host that predates inputSeq: this host counts from here.
|
|
92
|
+
if (typeof cloned.inputSeq !== 'number') cloned.inputSeq = 0;
|
|
93
|
+
// A new counter lifetime unless this is the SAME live record restored in
|
|
94
|
+
// place with its count intact (resize does that): a record loaded into
|
|
95
|
+
// this process for the first time, or one whose count went backwards,
|
|
96
|
+
// cannot be compared with anything announced before (review R8-2).
|
|
97
|
+
const live = this.sessions.get(cloned.sessionId)?.record;
|
|
98
|
+
const sameLifetime = !!live && typeof live.inputEpoch === 'string' && cloned.inputEpoch === live.inputEpoch
|
|
99
|
+
&& cloned.inputSeq >= (live.inputSeq ?? 0);
|
|
100
|
+
if (!sameLifetime) cloned.inputEpoch = this.mintInputEpoch();
|
|
81
101
|
this.sessions.set(cloned.sessionId, {
|
|
82
102
|
record: cloned,
|
|
83
103
|
buffer: (() => {
|
|
@@ -89,6 +109,16 @@ export class SessionHostRegistry {
|
|
|
89
109
|
return this.cloneRecord(cloned);
|
|
90
110
|
}
|
|
91
111
|
|
|
112
|
+
/** Census projection: omit launch arguments/environment and summarize stopped
|
|
113
|
+
* sessions. Full details remain available through getSession. */
|
|
114
|
+
listSessionSummaries(activeOnly = false): SessionHostRecord[] {
|
|
115
|
+
return Array.from(this.sessions.values()).map(s => s.record)
|
|
116
|
+
.filter(r => !activeOnly || (!r.termination && r.lifecycle !== 'stopped' && r.lifecycle !== 'failed'))
|
|
117
|
+
.map(r => this.cloneRecord({ ...r, launchCommand: { command: r.launchCommand.command, args: [] },
|
|
118
|
+
meta: r.termination || r.lifecycle === 'stopped' || r.lifecycle === 'failed' ? {} : { ...r.meta, cliArgs: [], cliArgsOmitted: true } }))
|
|
119
|
+
.sort((a, b) => b.lastActivityAt - a.lastActivityAt);
|
|
120
|
+
}
|
|
121
|
+
|
|
92
122
|
listSessions(): SessionHostRecord[] {
|
|
93
123
|
return Array.from(this.sessions.values())
|
|
94
124
|
.map(state => this.cloneRecord(state.record))
|
|
@@ -153,6 +183,22 @@ export class SessionHostRegistry {
|
|
|
153
183
|
return this.cloneRecord(state.record);
|
|
154
184
|
}
|
|
155
185
|
|
|
186
|
+
/** Count one accepted input (see SessionHostRecord.inputSeq); returns its number. */
|
|
187
|
+
recordInput(sessionId: string): number {
|
|
188
|
+
const state = this.requireSession(sessionId);
|
|
189
|
+
state.record.inputSeq = (typeof state.record.inputSeq === 'number' ? state.record.inputSeq : 0) + 1;
|
|
190
|
+
if (typeof state.record.inputEpoch !== 'string') state.record.inputEpoch = this.mintInputEpoch();
|
|
191
|
+
return state.record.inputSeq;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** The current input counter (see SessionHostInputState). */
|
|
195
|
+
getInputState(sessionId: string): { inputSeq: number; inputEpoch: string } {
|
|
196
|
+
const state = this.requireSession(sessionId);
|
|
197
|
+
if (typeof state.record.inputSeq !== 'number') state.record.inputSeq = 0;
|
|
198
|
+
if (typeof state.record.inputEpoch !== 'string') state.record.inputEpoch = this.mintInputEpoch();
|
|
199
|
+
return { inputSeq: state.record.inputSeq, inputEpoch: state.record.inputEpoch };
|
|
200
|
+
}
|
|
201
|
+
|
|
156
202
|
acquireWrite(payload: AcquireWritePayload): SessionHostRecord {
|
|
157
203
|
const state = this.requireSession(payload.sessionId);
|
|
158
204
|
if (state.record.writeOwner && state.record.writeOwner.clientId !== payload.clientId && !payload.force) {
|
package/src/types.ts
CHANGED
|
@@ -113,6 +113,21 @@ export interface SessionHostRecord {
|
|
|
113
113
|
meta: Record<string, unknown>;
|
|
114
114
|
/** Termination tombstone, stamped when the PTY process exits. Retained for post-mortem inspection. */
|
|
115
115
|
termination?: SessionTermination;
|
|
116
|
+
/**
|
|
117
|
+
* Monotonic count of input accepted into this runtime by `send_input`, from
|
|
118
|
+
* any client (SUBMIT-UNCONFIRMED-RECLAIM: the authoritative input boundary).
|
|
119
|
+
* Every accepted input is also announced as a `session_input` event carrying
|
|
120
|
+
* this number and the writer. Absent on records from a host that predates it
|
|
121
|
+
* — a reader must then treat input provenance as unknown.
|
|
122
|
+
*/
|
|
123
|
+
inputSeq?: number;
|
|
124
|
+
/**
|
|
125
|
+
* Identifies one lifetime of `inputSeq`. It changes whenever the counter
|
|
126
|
+
* could have restarted or been rewound — a new host process loading the
|
|
127
|
+
* record, or a restore with a lower count — so equal (epoch, seq) pairs
|
|
128
|
+
* really are the same point in the input stream.
|
|
129
|
+
*/
|
|
130
|
+
inputEpoch?: string;
|
|
116
131
|
}
|
|
117
132
|
|
|
118
133
|
export interface CreateSessionPayload {
|
|
@@ -148,6 +163,29 @@ export interface SendInputPayload {
|
|
|
148
163
|
data: string;
|
|
149
164
|
}
|
|
150
165
|
|
|
166
|
+
/**
|
|
167
|
+
* `send_input_if_seq`: write `data` only if NOTHING else has been input since
|
|
168
|
+
* the caller last accounted the stream — the host's (inputEpoch, inputSeq) is
|
|
169
|
+
* still exactly (expectedInputEpoch, expectedInputSeq) — and the caller may
|
|
170
|
+
* write without taking ownership (no force). Atomic on the host, so no input
|
|
171
|
+
* can slip between the caller's check and this write.
|
|
172
|
+
*/
|
|
173
|
+
export interface SendInputIfSeqPayload extends SendInputPayload {
|
|
174
|
+
expectedInputSeq: number;
|
|
175
|
+
expectedInputEpoch: string;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface GetInputStatePayload {
|
|
179
|
+
sessionId: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** `get_input_state`: the host's current input counter. A host without it
|
|
183
|
+
* rejects the request type, which is how a client knows not to trust. */
|
|
184
|
+
export interface SessionHostInputState {
|
|
185
|
+
inputSeq: number;
|
|
186
|
+
inputEpoch: string;
|
|
187
|
+
}
|
|
188
|
+
|
|
151
189
|
export interface ResizeSessionPayload {
|
|
152
190
|
sessionId: string;
|
|
153
191
|
cols: number;
|
|
@@ -324,6 +362,8 @@ export type SessionHostRequest =
|
|
|
324
362
|
| { type: 'attach_session'; payload: AttachSessionPayload }
|
|
325
363
|
| { type: 'detach_session'; payload: DetachSessionPayload }
|
|
326
364
|
| { type: 'send_input'; payload: SendInputPayload }
|
|
365
|
+
| { type: 'send_input_if_seq'; payload: SendInputIfSeqPayload }
|
|
366
|
+
| { type: 'get_input_state'; payload: GetInputStatePayload }
|
|
327
367
|
| { type: 'resize_session'; payload: ResizeSessionPayload }
|
|
328
368
|
| { type: 'stop_session'; payload: StopSessionPayload }
|
|
329
369
|
| { type: 'stop_mesh_writer'; payload: StopMeshWriterPayload }
|
|
@@ -340,7 +380,8 @@ export type SessionHostRequest =
|
|
|
340
380
|
| { type: 'send_signal'; payload: SendSignalPayload }
|
|
341
381
|
| { type: 'restart_session'; payload: RestartSessionPayload }
|
|
342
382
|
| { type: 'prune_duplicate_sessions'; payload?: PruneDuplicateSessionsPayload }
|
|
343
|
-
| { type: '
|
|
383
|
+
| { type: 'get_session'; payload: { sessionId: string } }
|
|
384
|
+
| { type: 'list_sessions'; payload?: { activeOnly?: boolean } };
|
|
344
385
|
|
|
345
386
|
export type SessionHostRequestType = SessionHostRequest['type'];
|
|
346
387
|
|
|
@@ -352,6 +393,8 @@ export const SESSION_HOST_SUPPORTED_REQUEST_TYPES: readonly SessionHostRequestTy
|
|
|
352
393
|
'attach_session',
|
|
353
394
|
'detach_session',
|
|
354
395
|
'send_input',
|
|
396
|
+
'send_input_if_seq',
|
|
397
|
+
'get_input_state',
|
|
355
398
|
'resize_session',
|
|
356
399
|
'stop_session',
|
|
357
400
|
'stop_mesh_writer',
|
|
@@ -368,6 +411,7 @@ export const SESSION_HOST_SUPPORTED_REQUEST_TYPES: readonly SessionHostRequestTy
|
|
|
368
411
|
'send_signal',
|
|
369
412
|
'restart_session',
|
|
370
413
|
'prune_duplicate_sessions',
|
|
414
|
+
'get_session',
|
|
371
415
|
'list_sessions',
|
|
372
416
|
];
|
|
373
417
|
|
|
@@ -382,6 +426,8 @@ export type SessionHostEvent =
|
|
|
382
426
|
| { type: 'session_started'; sessionId: string; pid?: number }
|
|
383
427
|
| { type: 'session_resumed'; sessionId: string; pid?: number }
|
|
384
428
|
| { type: 'session_output'; sessionId: string; seq: number; data: string }
|
|
429
|
+
/** An input was accepted into the runtime (emitted BEFORE it reaches the PTY). */
|
|
430
|
+
| { type: 'session_input'; sessionId: string; seq: number; epoch: string; clientId: string }
|
|
385
431
|
| { type: 'session_cleared'; sessionId: string }
|
|
386
432
|
| { type: 'session_exit'; sessionId: string; exitCode: number | null; signal?: number | null; termination?: SessionTermination }
|
|
387
433
|
| { type: 'session_stopped'; sessionId: string }
|