@adhdev/session-host-core 1.0.82-rc.31 → 1.0.82-rc.33

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.mjs CHANGED
@@ -14,6 +14,8 @@ var SESSION_HOST_SUPPORTED_REQUEST_TYPES = [
14
14
  "attach_session",
15
15
  "detach_session",
16
16
  "send_input",
17
+ "send_input_if_seq",
18
+ "get_input_state",
17
19
  "resize_session",
18
20
  "stop_session",
19
21
  "stop_mesh_writer",
@@ -30,6 +32,7 @@ var SESSION_HOST_SUPPORTED_REQUEST_TYPES = [
30
32
  "send_signal",
31
33
  "restart_session",
32
34
  "prune_duplicate_sessions",
35
+ "get_session",
33
36
  "list_sessions"
34
37
  ];
35
38
 
@@ -458,6 +461,13 @@ function formatRuntimeOwner(record) {
458
461
  // src/registry.ts
459
462
  var SessionHostRegistry = class {
460
463
  sessions = /* @__PURE__ */ new Map();
464
+ /** This registry's lifetime; every inputEpoch it mints starts with it. */
465
+ lifetimeId = generateUUID();
466
+ epochCounter = 0;
467
+ mintInputEpoch() {
468
+ this.epochCounter += 1;
469
+ return `${this.lifetimeId}:${this.epochCounter}`;
470
+ }
461
471
  createSession(payload) {
462
472
  const sessionId = payload.sessionId || generateUUID();
463
473
  if (this.sessions.has(sessionId)) {
@@ -489,6 +499,8 @@ var SessionHostRegistry = class {
489
499
  lifecycle: "starting",
490
500
  writeOwner: null,
491
501
  attachedClients: initialClient,
502
+ inputSeq: 0,
503
+ inputEpoch: this.mintInputEpoch(),
492
504
  buffer: {
493
505
  scrollbackBytes: 0,
494
506
  snapshotSeq: 0
@@ -508,6 +520,10 @@ var SessionHostRegistry = class {
508
520
  }
509
521
  restoreSession(record, snapshot) {
510
522
  const cloned = this.cloneRecord(record);
523
+ if (typeof cloned.inputSeq !== "number") cloned.inputSeq = 0;
524
+ const live = this.sessions.get(cloned.sessionId)?.record;
525
+ const sameLifetime = !!live && typeof live.inputEpoch === "string" && cloned.inputEpoch === live.inputEpoch && cloned.inputSeq >= (live.inputSeq ?? 0);
526
+ if (!sameLifetime) cloned.inputEpoch = this.mintInputEpoch();
511
527
  this.sessions.set(cloned.sessionId, {
512
528
  record: cloned,
513
529
  buffer: (() => {
@@ -518,6 +534,15 @@ var SessionHostRegistry = class {
518
534
  });
519
535
  return this.cloneRecord(cloned);
520
536
  }
537
+ /** Census projection: omit launch arguments/environment and summarize stopped
538
+ * sessions. Full details remain available through getSession. */
539
+ listSessionSummaries(activeOnly = false) {
540
+ return Array.from(this.sessions.values()).map((s) => s.record).filter((r) => !activeOnly || !r.termination && r.lifecycle !== "stopped" && r.lifecycle !== "failed").map((r) => this.cloneRecord({
541
+ ...r,
542
+ launchCommand: { command: r.launchCommand.command, args: [] },
543
+ meta: r.termination || r.lifecycle === "stopped" || r.lifecycle === "failed" ? {} : { ...r.meta, cliArgs: [], cliArgsOmitted: true }
544
+ })).sort((a, b) => b.lastActivityAt - a.lastActivityAt);
545
+ }
521
546
  listSessions() {
522
547
  return Array.from(this.sessions.values()).map((state) => this.cloneRecord(state.record)).sort((a, b) => b.lastActivityAt - a.lastActivityAt);
523
548
  }
@@ -569,6 +594,20 @@ var SessionHostRegistry = class {
569
594
  state.record.lastActivityAt = Date.now();
570
595
  return this.cloneRecord(state.record);
571
596
  }
597
+ /** Count one accepted input (see SessionHostRecord.inputSeq); returns its number. */
598
+ recordInput(sessionId) {
599
+ const state = this.requireSession(sessionId);
600
+ state.record.inputSeq = (typeof state.record.inputSeq === "number" ? state.record.inputSeq : 0) + 1;
601
+ if (typeof state.record.inputEpoch !== "string") state.record.inputEpoch = this.mintInputEpoch();
602
+ return state.record.inputSeq;
603
+ }
604
+ /** The current input counter (see SessionHostInputState). */
605
+ getInputState(sessionId) {
606
+ const state = this.requireSession(sessionId);
607
+ if (typeof state.record.inputSeq !== "number") state.record.inputSeq = 0;
608
+ if (typeof state.record.inputEpoch !== "string") state.record.inputEpoch = this.mintInputEpoch();
609
+ return { inputSeq: state.record.inputSeq, inputEpoch: state.record.inputEpoch };
610
+ }
572
611
  acquireWrite(payload) {
573
612
  const state = this.requireSession(payload.sessionId);
574
613
  if (state.record.writeOwner && state.record.writeOwner.clientId !== payload.clientId && !payload.force) {
@@ -689,6 +728,14 @@ import * as os2 from "os";
689
728
  import * as path2 from "path";
690
729
  import * as net from "net";
691
730
  import { StringDecoder } from "string_decoder";
731
+ function validateSessionHostEndpoint(endpoint, platform2 = process.platform) {
732
+ if (endpoint.kind !== "unix") return;
733
+ const limit = platform2 === "linux" ? 108 : 104;
734
+ const pathBytes = Buffer.byteLength(endpoint.path, "utf8");
735
+ if (pathBytes + 1 > limit) {
736
+ throw new Error(`session_host_unix_socket_path_too_long: path_bytes=${pathBytes} required_bytes=${pathBytes + 1} limit_bytes=${limit} platform=${platform2}`);
737
+ }
738
+ }
692
739
  function getDefaultSessionHostEndpoint(appName = "adhdev", options = {}) {
693
740
  const rawKey = typeof options.ipcKey === "string" ? options.ipcKey.trim() : "";
694
741
  if (rawKey && !/^[0-9a-f]{12}$/.test(rawKey)) {
@@ -712,23 +759,27 @@ function serializeEnvelope(envelope) {
712
759
  `;
713
760
  }
714
761
  function createLineParser(onEnvelope) {
715
- let buffer = "";
762
+ let fragments = [];
716
763
  const decoder = new StringDecoder("utf8");
717
764
  const parser = (chunk) => {
718
- buffer += typeof chunk === "string" ? chunk : decoder.write(chunk);
719
- let newlineIndex = buffer.indexOf("\n");
720
- while (newlineIndex >= 0) {
721
- const rawLine = buffer.slice(0, newlineIndex).trim();
722
- buffer = buffer.slice(newlineIndex + 1);
723
- if (rawLine) {
724
- onEnvelope(JSON.parse(rawLine));
765
+ const text = typeof chunk === "string" ? chunk : decoder.write(chunk);
766
+ let start = 0;
767
+ for (; ; ) {
768
+ const newline = text.indexOf("\n", start);
769
+ if (newline < 0) {
770
+ if (start < text.length) fragments.push(text.slice(start));
771
+ break;
725
772
  }
726
- newlineIndex = buffer.indexOf("\n");
773
+ fragments.push(text.slice(start, newline));
774
+ const rawLine = fragments.join("").trim();
775
+ fragments = [];
776
+ start = newline + 1;
777
+ if (rawLine) onEnvelope(JSON.parse(rawLine));
727
778
  }
728
779
  };
729
780
  parser.end = () => {
730
- const remainder = buffer + decoder.end();
731
- buffer = "";
781
+ const remainder = fragments.join("") + decoder.end();
782
+ fragments = [];
732
783
  return remainder;
733
784
  };
734
785
  return parser;
@@ -925,7 +976,12 @@ var SessionHostClient = class {
925
976
  return;
926
977
  }
927
978
  if (envelope.kind === "event") {
928
- for (const listener of this.eventListeners) listener(envelope.event);
979
+ for (const listener of this.eventListeners) {
980
+ try {
981
+ listener(envelope.event);
982
+ } catch {
983
+ }
984
+ }
929
985
  }
930
986
  });
931
987
  socket.on("data", parser);
@@ -1135,9 +1191,23 @@ import * as fs from "fs";
1135
1191
  import * as os3 from "os";
1136
1192
  import * as path3 from "path";
1137
1193
  var DEFAULT_CONFIG_DIR_NAME = ".adhdev";
1194
+ function assertTestStorageIsolated(destination) {
1195
+ const env = process.env;
1196
+ if (!env.VITEST && env.NODE_ENV !== "test" && !env.NODE_TEST_CONTEXT && !process.execArgv.some((arg) => arg === "--test" || arg.startsWith("--test="))) return;
1197
+ const target = canonicalizeInstancePath(destination);
1198
+ const home = os3.userInfo().homedir;
1199
+ for (const track of [".adhdev", ".adhdev-preview"]) {
1200
+ const live = canonicalizeInstancePath(path3.join(home, track));
1201
+ if (target === live || target.startsWith(live + path3.sep)) {
1202
+ throw new Error("LIVE runtime storage is forbidden in a test process; pin ADHDEV_CONFIG_DIR or storageRootDir to a temporary directory");
1203
+ }
1204
+ }
1205
+ }
1138
1206
  function resolveInstanceConfigDir(env = process.env, homeDir = os3.homedir()) {
1139
1207
  const override = typeof env.ADHDEV_CONFIG_DIR === "string" ? env.ADHDEV_CONFIG_DIR.trim() : "";
1140
- return override || path3.join(homeDir, DEFAULT_CONFIG_DIR_NAME);
1208
+ const resolved = override || path3.join(homeDir, DEFAULT_CONFIG_DIR_NAME);
1209
+ if (env === process.env) assertTestStorageIsolated(resolved);
1210
+ return resolved;
1141
1211
  }
1142
1212
  function realpathLongestExistingPrefix(resolved) {
1143
1213
  let current = resolved;
@@ -1304,6 +1374,7 @@ export {
1304
1374
  SessionHostRegistry,
1305
1375
  SessionRingBuffer,
1306
1376
  applyTerminalColorEnv,
1377
+ assertTestStorageIsolated,
1307
1378
  buildRuntimeDisplayName,
1308
1379
  buildRuntimeKey,
1309
1380
  canonicalizeInstancePath,
@@ -1336,6 +1407,7 @@ export {
1336
1407
  resolveSessionHostRows,
1337
1408
  sanitizeSpawnEnv,
1338
1409
  spawnCliChild,
1410
+ validateSessionHostEndpoint,
1339
1411
  writeEnvelope
1340
1412
  };
1341
1413
  //# sourceMappingURL=index.mjs.map