@threadbase-sh/streamer 1.41.2 → 1.43.0

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.js CHANGED
@@ -1148,7 +1148,10 @@ var CodexPtyRunner = class {
1148
1148
  const projectName = options.projectName ?? basename(options.projectPath);
1149
1149
  const proc = nodePty.spawn(
1150
1150
  resolveCodexExe(),
1151
- ["resume", sessionId, "--cd", options.projectPath, "--no-alt-screen"],
1151
+ // `sessionId` stays the runner's map key — only argv carries the
1152
+ // provider-side id, so a resumed Codex session keeps the placeholder id
1153
+ // its client already navigated to.
1154
+ ["resume", options.resumeId ?? sessionId, "--cd", options.projectPath, "--no-alt-screen"],
1152
1155
  {
1153
1156
  name: "xterm-256color",
1154
1157
  cols: PTY_COLS,
@@ -3073,7 +3076,7 @@ import {
3073
3076
  existsSync as existsSync11,
3074
3077
  watch as fsWatch,
3075
3078
  readdirSync as readdirSync6,
3076
- readFileSync as readFileSync8,
3079
+ readFileSync as readFileSync9,
3077
3080
  statSync as statSync9
3078
3081
  } from "fs";
3079
3082
  import { realpath as realpath2 } from "fs/promises";
@@ -4101,6 +4104,86 @@ function redactValue(value) {
4101
4104
  return value;
4102
4105
  }
4103
4106
 
4107
+ // src/services/sessions/resumeIdentity.ts
4108
+ function resumeIdForRow(row) {
4109
+ if (row.provider !== CODEX_CLI_PROVIDER) return row.session_id;
4110
+ return row.bound_conversation_id;
4111
+ }
4112
+
4113
+ // src/services/sessions/rehydrateSessions.ts
4114
+ var REHYDRATE_MAX = 25;
4115
+ var REHYDRATE_WINDOW_MS = 7 * 24 * 60 * 60 * 1e3;
4116
+ var AGENT_EXIT_SOURCES = /* @__PURE__ */ new Set(["exit", "process-exit"]);
4117
+ function rehydrateSkipReason(row, opts) {
4118
+ if (resumeIdForRow(row) == null) return "codex_unbound";
4119
+ if (!opts.projectExists(row.project_path)) return "project_missing";
4120
+ if (opts.now - row.status_updated_at > REHYDRATE_WINDOW_MS) return "too_old";
4121
+ if (AGENT_EXIT_SOURCES.has(row.status_source) && row.failure_reason == null) {
4122
+ return "agent_exited";
4123
+ }
4124
+ return null;
4125
+ }
4126
+ function rowToStubSession(row) {
4127
+ return {
4128
+ id: row.session_id,
4129
+ provider: row.provider,
4130
+ projectPath: row.project_path,
4131
+ projectName: row.project_name,
4132
+ branch: row.branch,
4133
+ // No PTY exists for a stub, so this is the only truthful status.
4134
+ status: "idle",
4135
+ startedAt: new Date(row.started_at),
4136
+ completedAt: row.completed_at != null ? new Date(row.completed_at) : null,
4137
+ promptCount: row.prompt_count,
4138
+ lastOutput: "",
4139
+ rehydrated: true,
4140
+ ...row.session_name != null && { sessionName: row.session_name },
4141
+ ...row.project_id != null && { projectId: row.project_id },
4142
+ ...row.bound_conversation_id != null && { boundConversationId: row.bound_conversation_id },
4143
+ ...row.resumed_from_conversation_id != null && {
4144
+ resumedFromConversationId: row.resumed_from_conversation_id
4145
+ },
4146
+ ...row.failure_reason != null && { failureReason: row.failure_reason },
4147
+ ...row.last_activity_at != null && { lastActivityAt: new Date(row.last_activity_at) },
4148
+ // Only `shutdown` crosses over. It is the one registry source that is also a
4149
+ // wire StatusSource *and* that genuinely describes the `idle` above — the
4150
+ // streamer stopped this session. A crashed row still says `transition` over
4151
+ // a `running` status, and copying that here would attach observed-confidence
4152
+ // provenance to a status we derived at boot, so leave it unset instead.
4153
+ ...row.status_source === "shutdown" && {
4154
+ statusSource: "shutdown",
4155
+ statusUpdatedAt: new Date(row.status_updated_at),
4156
+ // `status` above had to flatten to `idle`, which erases whether the agent
4157
+ // was mid-answer when we stopped it. Carried separately so a client can
4158
+ // say "interrupted mid-response" without a novel SessionStatus value.
4159
+ // Gated on the same `shutdown` source: a crashed row's `running` is a
4160
+ // frozen value nobody confirmed, not an observation.
4161
+ ...(row.status === "running" || row.status === "waiting_input") && {
4162
+ interruptedStatus: row.status
4163
+ }
4164
+ }
4165
+ };
4166
+ }
4167
+
4168
+ // src/utils/bootToken.ts
4169
+ import { readFileSync as readFileSync5 } from "fs";
4170
+ import os from "os";
4171
+ var cached2;
4172
+ function currentBootToken() {
4173
+ if (cached2 === void 0) cached2 = computeBootToken();
4174
+ return cached2;
4175
+ }
4176
+ function computeBootToken() {
4177
+ if (process.platform === "linux") {
4178
+ try {
4179
+ const bootId = readFileSync5("/proc/sys/kernel/random/boot_id", "utf8").trim();
4180
+ if (bootId) return bootId;
4181
+ } catch {
4182
+ }
4183
+ }
4184
+ return String(Math.round((Date.now() - os.uptime() * 1e3) / 1e4));
4185
+ }
4186
+
4104
4187
  // src/api/routes/diagnostics.routes.ts
4105
4188
  function providerCheck(name, resolve2) {
4106
4189
  try {
@@ -4180,6 +4263,49 @@ var createDiagnosticsRoutes = (deps) => {
4180
4263
  );
4181
4264
  return c.json(redactValue(buildReport(checks)));
4182
4265
  });
4266
+ app.get("/sessions", (c) => {
4267
+ const repo = deps.managedSessionsRepo();
4268
+ if (!repo) {
4269
+ return c.json(
4270
+ { error: "Session registry is unavailable", code: "REGISTRY_UNAVAILABLE" },
4271
+ 503
4272
+ );
4273
+ }
4274
+ const rows = repo.listAll();
4275
+ const verdicts = deps.sessionVerdicts();
4276
+ const bootToken = currentBootToken();
4277
+ const now = Date.now();
4278
+ const sessions = rows.map((row) => {
4279
+ const verdict = verdicts.get(row.session_id);
4280
+ const skip = rehydrateSkipReason(row, { now, projectExists: existsSync5 });
4281
+ return {
4282
+ sessionId: row.session_id,
4283
+ provider: row.provider,
4284
+ status: row.status,
4285
+ statusSource: row.status_source,
4286
+ statusUpdatedAt: new Date(row.status_updated_at).toISOString(),
4287
+ // Whether the recorded pid is probeable at all this boot, not whether
4288
+ // it is alive — a mismatch means the question was never asked.
4289
+ //
4290
+ // NOT named for the boot token it derives from: `redactValue`'s
4291
+ // SECRET_KEY_RE matches any key containing "token", so `bootTokenMatches`
4292
+ // was scrubbed to the string "[redacted]" and the field shipped useless.
4293
+ // The regex is deliberately over-broad for a payload meant to be pasted
4294
+ // into bug reports, so the field moved rather than the guard.
4295
+ recordedThisBoot: row.boot_token != null && row.boot_token === bootToken,
4296
+ // Absent when this boot never classified the row: a clean restart
4297
+ // stamps completed_at on the way out, which takes it out of the probe
4298
+ // set entirely. That absence is itself the answer.
4299
+ lifecycle: verdict?.lifecycle ?? null,
4300
+ lifecycleReason: verdict?.reason ?? null,
4301
+ rehydrated: skip == null,
4302
+ rehydrateSkipReason: skip,
4303
+ projectExists: existsSync5(row.project_path),
4304
+ projectPath: redactPath(row.project_path)
4305
+ };
4306
+ });
4307
+ return c.json(redactValue({ generatedAt: (/* @__PURE__ */ new Date()).toISOString(), sessions }));
4308
+ });
4183
4309
  return app;
4184
4310
  };
4185
4311
 
@@ -4330,7 +4456,7 @@ import { Hono as Hono11 } from "hono";
4330
4456
  import { hostname as hostname2 } from "os";
4331
4457
 
4332
4458
  // src/config/update-config.ts
4333
- import { readFileSync as readFileSync5 } from "fs";
4459
+ import { readFileSync as readFileSync6 } from "fs";
4334
4460
  import { homedir as homedir5 } from "os";
4335
4461
  import { join as join10 } from "path";
4336
4462
  import { parse as parseYaml } from "yaml";
@@ -4353,7 +4479,7 @@ function loadUpdateConfig(opts = {}) {
4353
4479
  const path = opts.path ?? DEFAULT_CONFIG_PATH;
4354
4480
  let raw;
4355
4481
  try {
4356
- raw = readFileSync5(path, "utf-8");
4482
+ raw = readFileSync6(path, "utf-8");
4357
4483
  } catch (err) {
4358
4484
  if (err.code === "ENOENT") return null;
4359
4485
  throw err;
@@ -5185,7 +5311,7 @@ import { dirname as dirname7 } from "path";
5185
5311
  import { setImmediate as yieldToEventLoop } from "timers/promises";
5186
5312
 
5187
5313
  // src/db/sqlite-migrate.ts
5188
- import { readdirSync as readdirSync2, readFileSync as readFileSync6 } from "fs";
5314
+ import { readdirSync as readdirSync2, readFileSync as readFileSync7 } from "fs";
5189
5315
  import { dirname as dirname6, join as join12 } from "path";
5190
5316
  import { fileURLToPath as fileURLToPath2 } from "url";
5191
5317
  function getMigrationsDir2() {
@@ -5217,7 +5343,7 @@ function runSqliteMigrations(db, migrationsDir) {
5217
5343
  skipped.push(file);
5218
5344
  continue;
5219
5345
  }
5220
- const sql = readFileSync6(join12(dir, file), "utf-8");
5346
+ const sql = readFileSync7(join12(dir, file), "utf-8");
5221
5347
  const tx = db.transaction(() => {
5222
5348
  db.exec(sql);
5223
5349
  recordApplied.run(file, (/* @__PURE__ */ new Date()).toISOString());
@@ -5247,8 +5373,8 @@ function isAgentLine(line, entrypoints = DEFAULT_AGENT_ENTRYPOINTS) {
5247
5373
  function isAgentFile(filePath, entrypoints = DEFAULT_AGENT_ENTRYPOINTS) {
5248
5374
  if (entrypoints.size === 0) return false;
5249
5375
  const key = cacheKey(filePath, entrypoints);
5250
- const cached2 = fileDecisionCache.get(key);
5251
- if (cached2 !== void 0) return cached2;
5376
+ const cached3 = fileDecisionCache.get(key);
5377
+ if (cached3 !== void 0) return cached3;
5252
5378
  let fd;
5253
5379
  try {
5254
5380
  fd = openSync2(filePath, "r");
@@ -5904,9 +6030,9 @@ var ConversationCache = class _ConversationCache {
5904
6030
  classifyAgentFile(filePath, mtimeMs, fileSize) {
5905
6031
  if (this.agentEntrypoints.size === 0) return false;
5906
6032
  const entrypointsKey = this.agentEntrypointsKey();
5907
- const cached2 = this.stmts.getFileMetadata.get(filePath);
5908
- if (cached2 && cached2.mtime_ms === mtimeMs && cached2.file_size === fileSize && cached2.agent_entrypoints_key === entrypointsKey) {
5909
- return cached2.is_agent === 1;
6033
+ const cached3 = this.stmts.getFileMetadata.get(filePath);
6034
+ if (cached3 && cached3.mtime_ms === mtimeMs && cached3.file_size === fileSize && cached3.agent_entrypoints_key === entrypointsKey) {
6035
+ return cached3.is_agent === 1;
5910
6036
  }
5911
6037
  const isAgent = isAgentFile(filePath, this.agentEntrypoints);
5912
6038
  this.stmts.upsertFileMetadata.run({
@@ -6611,11 +6737,17 @@ var ConversationsRepository = class {
6611
6737
  };
6612
6738
 
6613
6739
  // src/db/repositories/managed-sessions.repository.ts
6740
+ var PROBE_SET_MAX = 200;
6741
+ var DIAGNOSTICS_MAX = 200;
6742
+ var TERMINAL_RETENTION_MS = 30 * 24 * 60 * 60 * 1e3;
6614
6743
  var ManagedSessionsRepository = class {
6615
6744
  upsertStmt;
6616
6745
  updateStatusStmt;
6746
+ bindStmt;
6617
6747
  getStmt;
6618
6748
  listNonTerminalStmt;
6749
+ listAllStmt;
6750
+ pruneTerminalStmt;
6619
6751
  listRecoverableStmt;
6620
6752
  deleteStmt;
6621
6753
  constructor(db) {
@@ -6625,13 +6757,13 @@ var ManagedSessionsRepository = class {
6625
6757
  status, status_source, status_updated_at, started_at, completed_at,
6626
6758
  last_activity_at, prompt_count, session_name, project_id,
6627
6759
  bound_conversation_id, resumed_from_conversation_id, failure_reason,
6628
- streamer_instance_id
6760
+ streamer_instance_id, boot_token
6629
6761
  ) VALUES (
6630
6762
  @session_id, @provider, @pid, @cmdline, @project_path, @project_name, @branch,
6631
6763
  @status, @status_source, @status_updated_at, @started_at, @completed_at,
6632
6764
  @last_activity_at, @prompt_count, @session_name, @project_id,
6633
6765
  @bound_conversation_id, @resumed_from_conversation_id, @failure_reason,
6634
- @streamer_instance_id
6766
+ @streamer_instance_id, @boot_token
6635
6767
  )
6636
6768
  ON CONFLICT(session_id) DO UPDATE SET
6637
6769
  pid = excluded.pid,
@@ -6650,7 +6782,8 @@ var ManagedSessionsRepository = class {
6650
6782
  bound_conversation_id = excluded.bound_conversation_id,
6651
6783
  resumed_from_conversation_id = excluded.resumed_from_conversation_id,
6652
6784
  failure_reason = excluded.failure_reason,
6653
- streamer_instance_id = excluded.streamer_instance_id
6785
+ streamer_instance_id = excluded.streamer_instance_id,
6786
+ boot_token = excluded.boot_token
6654
6787
  `);
6655
6788
  this.updateStatusStmt = db.prepare(`
6656
6789
  UPDATE managed_sessions
@@ -6664,11 +6797,27 @@ var ManagedSessionsRepository = class {
6664
6797
  session_name = COALESCE(@session_name, session_name)
6665
6798
  WHERE session_id = @session_id
6666
6799
  `);
6800
+ this.bindStmt = db.prepare(`
6801
+ UPDATE managed_sessions
6802
+ SET bound_conversation_id = @bound_conversation_id
6803
+ WHERE session_id = @session_id
6804
+ `);
6667
6805
  this.getStmt = db.prepare("SELECT * FROM managed_sessions WHERE session_id = ?");
6668
6806
  this.listNonTerminalStmt = db.prepare(`
6669
6807
  SELECT * FROM managed_sessions
6670
6808
  WHERE completed_at IS NULL
6671
6809
  ORDER BY started_at ASC
6810
+ LIMIT @limit
6811
+ `);
6812
+ this.listAllStmt = db.prepare(`
6813
+ SELECT * FROM managed_sessions
6814
+ ORDER BY status_updated_at DESC
6815
+ LIMIT @limit
6816
+ `);
6817
+ this.pruneTerminalStmt = db.prepare(`
6818
+ DELETE FROM managed_sessions
6819
+ WHERE completed_at IS NOT NULL
6820
+ AND completed_at < @before
6672
6821
  `);
6673
6822
  this.listRecoverableStmt = db.prepare(`
6674
6823
  SELECT * FROM managed_sessions
@@ -6701,7 +6850,10 @@ var ManagedSessionsRepository = class {
6701
6850
  bound_conversation_id: session.boundConversationId ?? null,
6702
6851
  resumed_from_conversation_id: session.resumedFromConversationId ?? null,
6703
6852
  failure_reason: session.failureReason ?? null,
6704
- streamer_instance_id: streamerInstanceId
6853
+ streamer_instance_id: streamerInstanceId,
6854
+ // Recorded, never backfilled: the pid above is only probeable while this
6855
+ // token still matches the running machine.
6856
+ boot_token: currentBootToken()
6705
6857
  });
6706
6858
  }
6707
6859
  /**
@@ -6722,12 +6874,48 @@ var ManagedSessionsRepository = class {
6722
6874
  session_name: fields.sessionName ?? null
6723
6875
  });
6724
6876
  }
6877
+ /**
6878
+ * Persist the Codex rollout id discovered after spawn.
6879
+ *
6880
+ * Its own statement rather than a `recordSpawn` re-run: the binding arrives
6881
+ * while the session is live, and re-upserting would also rewrite `cmdline`
6882
+ * with an id that is *not* in a fresh Codex process's argv, turning the
6883
+ * reconciler's identity check into a false `orphaned`. Without this write the
6884
+ * binding lives only in memory and dies with the streamer — which is the
6885
+ * whole reason a restarted Codex session could not be resumed (G6).
6886
+ */
6887
+ recordBinding(sessionId, boundConversationId) {
6888
+ this.bindStmt.run({
6889
+ session_id: sessionId,
6890
+ bound_conversation_id: boundConversationId
6891
+ });
6892
+ }
6725
6893
  get(sessionId) {
6726
6894
  return this.getStmt.get(sessionId) ?? null;
6727
6895
  }
6728
- /** Rows with no recorded completion — the reconciler's probe set. */
6729
- listNonTerminal() {
6730
- return this.listNonTerminalStmt.all();
6896
+ /**
6897
+ * Rows with no recorded completion — the reconciler's probe set.
6898
+ *
6899
+ * Capped. Callers must compare the result length against the limit and say so
6900
+ * when it clips: a silently truncated probe set reads as "we checked
6901
+ * everything" when it did not.
6902
+ */
6903
+ listNonTerminal(limit = PROBE_SET_MAX) {
6904
+ return this.listNonTerminalStmt.all({ limit });
6905
+ }
6906
+ /** Every row, most recently touched first, for the diagnostics surface. */
6907
+ listAll(limit = DIAGNOSTICS_MAX) {
6908
+ return this.listAllStmt.all({ limit });
6909
+ }
6910
+ /**
6911
+ * Delete terminal rows older than `olderThanMs`, returning how many went.
6912
+ *
6913
+ * Only rows carrying a `completed_at` are eligible, so nothing the reconciler
6914
+ * or rehydrator might still want is reachable from here — a row without one
6915
+ * is by definition unfinished business, however old it looks.
6916
+ */
6917
+ pruneTerminal(olderThanMs = TERMINAL_RETENTION_MS) {
6918
+ return this.pruneTerminalStmt.run({ before: Date.now() - olderThanMs }).changes;
6731
6919
  }
6732
6920
  /**
6733
6921
  * Rows a restart could bring back: still open, or closed by our own shutdown,
@@ -7074,7 +7262,7 @@ import { createHash as createHash3 } from "crypto";
7074
7262
  import { existsSync as existsSync9 } from "fs";
7075
7263
 
7076
7264
  // src/services/cache-integrity/alertStore.ts
7077
- import { mkdirSync as mkdirSync4, readFileSync as readFileSync7, writeFileSync as writeFileSync3 } from "fs";
7265
+ import { mkdirSync as mkdirSync4, readFileSync as readFileSync8, writeFileSync as writeFileSync3 } from "fs";
7078
7266
  import { homedir as homedir7 } from "os";
7079
7267
  import { dirname as dirname8, join as join14 } from "path";
7080
7268
  function alertStatePath() {
@@ -7083,7 +7271,7 @@ function alertStatePath() {
7083
7271
  }
7084
7272
  function loadAlertState() {
7085
7273
  try {
7086
- const parsed = JSON.parse(readFileSync7(alertStatePath(), "utf-8"));
7274
+ const parsed = JSON.parse(readFileSync8(alertStatePath(), "utf-8"));
7087
7275
  return parsed && typeof parsed === "object" ? parsed : {};
7088
7276
  } catch {
7089
7277
  return {};
@@ -8593,6 +8781,34 @@ function paginate(results, offset, limit) {
8593
8781
  };
8594
8782
  }
8595
8783
 
8784
+ // src/services/sessions/autoResumeOnBoot.ts
8785
+ var AUTO_RESUME_WINDOW_MS = 15 * 60 * 1e3;
8786
+ var AUTO_RESUME_MAX = 5;
8787
+ var AUTO_RESUME_CONCURRENCY = 2;
8788
+ var AUTO_RESUME_STAGGER_MS = 500;
8789
+ function autoResumeSkipReason(row, opts) {
8790
+ if (row.status_source !== "shutdown") return "not_shutdown";
8791
+ if (row.status !== "running" && row.status !== "waiting_input") return "not_interrupted";
8792
+ if (opts.now - row.status_updated_at > AUTO_RESUME_WINDOW_MS) return "too_old";
8793
+ if (!opts.projectExists(row.project_path)) return "project_missing";
8794
+ if (resumeIdForRow(row) == null) return "resume_identity_missing";
8795
+ return null;
8796
+ }
8797
+ function planAutoResume(rows, opts) {
8798
+ const eligible = [];
8799
+ const skipped = [];
8800
+ for (const row of rows) {
8801
+ const reason = autoResumeSkipReason(row, opts);
8802
+ if (reason) skipped.push({ row, reason });
8803
+ else eligible.push(row);
8804
+ }
8805
+ return {
8806
+ attempts: eligible.slice(0, AUTO_RESUME_MAX),
8807
+ skipped,
8808
+ overflow: eligible.slice(AUTO_RESUME_MAX)
8809
+ };
8810
+ }
8811
+
8596
8812
  // src/services/sessions/conversationBusy.ts
8597
8813
  import { statSync as statSync8 } from "fs";
8598
8814
  var RESUME_BUSY_WINDOW_MS = 12e4;
@@ -8696,8 +8912,14 @@ function readIdempotencyKey(body) {
8696
8912
  }
8697
8913
 
8698
8914
  // src/services/sessions/reconcileSessions.ts
8699
- async function classifySession(row, probe, currentInstanceId) {
8915
+ var PRE_BOOT_REASON = "recorded before this machine boot";
8916
+ async function classifySession(row, probe, currentInstanceId, currentBootToken2 = null) {
8700
8917
  const { session_id: sessionId } = row;
8918
+ const resumable = (reason) => resumeIdForRow(row) == null ? {
8919
+ sessionId,
8920
+ lifecycle: "failed",
8921
+ reason: "Codex session ended before its rollout id was known"
8922
+ } : { sessionId, lifecycle: "resumable", reason };
8701
8923
  if (row.completed_at != null) {
8702
8924
  const clean = probe.endedCleanly?.(row) ?? row.failure_reason == null;
8703
8925
  return {
@@ -8707,18 +8929,19 @@ async function classifySession(row, probe, currentInstanceId) {
8707
8929
  };
8708
8930
  }
8709
8931
  if (row.pid == null) {
8710
- return { sessionId, lifecycle: "resumable", reason: "no pid recorded" };
8932
+ return resumable("no pid recorded");
8933
+ }
8934
+ if (currentBootToken2 != null && row.boot_token !== currentBootToken2) {
8935
+ return resumable(PRE_BOOT_REASON);
8711
8936
  }
8712
8937
  if (!probe.isPidAlive(row.pid)) {
8713
- const clean = probe.endedCleanly?.(row) ?? false;
8714
- if (clean) {
8938
+ if (probe.endedCleanly?.(row)) {
8715
8939
  return { sessionId, lifecycle: "completed", reason: "process gone, history ended cleanly" };
8716
8940
  }
8717
- return {
8718
- sessionId,
8719
- lifecycle: "resumable",
8720
- reason: "process gone, resumable from provider history"
8721
- };
8941
+ if (row.failure_reason != null) {
8942
+ return { sessionId, lifecycle: "failed", reason: "process gone, failure recorded" };
8943
+ }
8944
+ return resumable("process gone, resumable from provider history");
8722
8945
  }
8723
8946
  const args = await probe.getProcessArgs(row.pid);
8724
8947
  const token = row.cmdline;
@@ -8736,52 +8959,10 @@ async function classifySession(row, probe, currentInstanceId) {
8736
8959
  reason: sameRun ? "owned by this run" : "survived a previous streamer run"
8737
8960
  };
8738
8961
  }
8739
- async function reconcileSessions(rows, probe, currentInstanceId) {
8740
- return Promise.all(rows.map((row) => classifySession(row, probe, currentInstanceId)));
8741
- }
8742
-
8743
- // src/services/sessions/rehydrateSessions.ts
8744
- var REHYDRATE_MAX = 25;
8745
- var REHYDRATE_WINDOW_MS = 7 * 24 * 60 * 60 * 1e3;
8746
- var AGENT_EXIT_SOURCES = /* @__PURE__ */ new Set(["exit", "process-exit"]);
8747
- function shouldRehydrate(row, opts) {
8748
- if (!opts.projectExists(row.project_path)) return false;
8749
- if (opts.now - row.status_updated_at > REHYDRATE_WINDOW_MS) return false;
8750
- if (AGENT_EXIT_SOURCES.has(row.status_source) && row.failure_reason == null) return false;
8751
- return true;
8752
- }
8753
- function rowToStubSession(row) {
8754
- return {
8755
- id: row.session_id,
8756
- provider: row.provider,
8757
- projectPath: row.project_path,
8758
- projectName: row.project_name,
8759
- branch: row.branch,
8760
- // No PTY exists for a stub, so this is the only truthful status.
8761
- status: "idle",
8762
- startedAt: new Date(row.started_at),
8763
- completedAt: row.completed_at != null ? new Date(row.completed_at) : null,
8764
- promptCount: row.prompt_count,
8765
- lastOutput: "",
8766
- rehydrated: true,
8767
- ...row.session_name != null && { sessionName: row.session_name },
8768
- ...row.project_id != null && { projectId: row.project_id },
8769
- ...row.bound_conversation_id != null && { boundConversationId: row.bound_conversation_id },
8770
- ...row.resumed_from_conversation_id != null && {
8771
- resumedFromConversationId: row.resumed_from_conversation_id
8772
- },
8773
- ...row.failure_reason != null && { failureReason: row.failure_reason },
8774
- ...row.last_activity_at != null && { lastActivityAt: new Date(row.last_activity_at) },
8775
- // Only `shutdown` crosses over. It is the one registry source that is also a
8776
- // wire StatusSource *and* that genuinely describes the `idle` above — the
8777
- // streamer stopped this session. A crashed row still says `transition` over
8778
- // a `running` status, and copying that here would attach observed-confidence
8779
- // provenance to a status we derived at boot, so leave it unset instead.
8780
- ...row.status_source === "shutdown" && {
8781
- statusSource: "shutdown",
8782
- statusUpdatedAt: new Date(row.status_updated_at)
8783
- }
8784
- };
8962
+ async function reconcileSessions(rows, probe, currentInstanceId, currentBootToken2 = null) {
8963
+ return Promise.all(
8964
+ rows.map((row) => classifySession(row, probe, currentInstanceId, currentBootToken2))
8965
+ );
8785
8966
  }
8786
8967
 
8787
8968
  // src/types.ts
@@ -8972,7 +9153,8 @@ function managedToResponse(s, ptyAttached) {
8972
9153
  ...s.resumedFromConversationId != null && {
8973
9154
  resumedFromConversationId: s.resumedFromConversationId
8974
9155
  },
8975
- ...s.boundConversationId != null && { boundConversationId: s.boundConversationId }
9156
+ ...s.boundConversationId != null && { boundConversationId: s.boundConversationId },
9157
+ ...s.interruptedStatus != null && { interruptedStatus: s.interruptedStatus }
8976
9158
  };
8977
9159
  }
8978
9160
  function discoveredToResponse(d, conversationId) {
@@ -9468,6 +9650,7 @@ var StreamerServer = class {
9468
9650
  disableDb = false;
9469
9651
  // Skip the startup warm-up scan (test hook; see ServerConfig.skipStartupWarmup).
9470
9652
  skipStartupWarmup;
9653
+ autoResumeOnBoot;
9471
9654
  browseRoot = null;
9472
9655
  publicUrl = null;
9473
9656
  browserCors;
@@ -9517,7 +9700,7 @@ var StreamerServer = class {
9517
9700
  idempotency = new IdempotencyStore();
9518
9701
  // sessionId → lifecycle verdict from boot reconciliation. Only holds sessions
9519
9702
  // this run did NOT spawn; live ones derive their lifecycle from ptyAttached.
9520
- sessionLifecycles = /* @__PURE__ */ new Map();
9703
+ sessionVerdicts = /* @__PURE__ */ new Map();
9521
9704
  // Periodic sweep that releases PTYs no agent is using. Null until listen().
9522
9705
  idleReaperTimer = null;
9523
9706
  // Map of clientId → WS socket (populated by the "register" WS handshake)
@@ -9584,6 +9767,7 @@ var StreamerServer = class {
9584
9767
  this.verbose = config.verbose ?? false;
9585
9768
  this.disableDb = config.disableDb ?? false;
9586
9769
  this.skipStartupWarmup = config.skipStartupWarmup ?? false;
9770
+ this.autoResumeOnBoot = config.autoResumeOnBoot ?? false;
9587
9771
  this.scannerPersistenceDisabled = config.scannerPersistent === false;
9588
9772
  this.scanProfiles = config.scanProfiles;
9589
9773
  this.codexRoots = config.codexRoots ?? [join18(homedir9(), ".codex", "sessions")];
@@ -9890,6 +10074,8 @@ var StreamerServer = class {
9890
10074
  sessionsRepo: () => this.sessionsRepo,
9891
10075
  cacheMetadataRepo: () => this.cacheMetadataRepo,
9892
10076
  runtimeStore: () => this.runtimeStore,
10077
+ managedSessionsRepo: () => this.managedSessionsRepo,
10078
+ sessionVerdicts: () => this.sessionVerdicts,
9893
10079
  ptyAttachedIds: () => this.ptyAttachedIds(),
9894
10080
  handleListSessions: (url, res) => this.handleListSessions(url, res),
9895
10081
  handleSessionsCount: (res) => this.handleSessionsCount(res),
@@ -10062,16 +10248,19 @@ var StreamerServer = class {
10062
10248
  broadcastOrUnicastSessionList(req) {
10063
10249
  const clientId = req.headers["x-client-id"];
10064
10250
  const ws = typeof clientId === "string" ? this.clientIdToWs.get(clientId) : void 0;
10065
- const payload = {
10066
- type: "session_list",
10067
- sessions: this.withReconciledLifecycle(this.sessionStore.list(this.ptyAttachedIds()))
10068
- };
10251
+ const payload = this.sessionListPayload();
10069
10252
  if (ws) {
10070
10253
  this.wsHub.unicast(ws, payload);
10071
10254
  } else {
10072
10255
  this.wsHub.broadcast(payload);
10073
10256
  }
10074
10257
  }
10258
+ sessionListPayload() {
10259
+ return {
10260
+ type: "session_list",
10261
+ sessions: this.withReconciledLifecycle(this.sessionStore.list(this.ptyAttachedIds()))
10262
+ };
10263
+ }
10075
10264
  /**
10076
10265
  * Overlay boot-reconciliation verdicts onto session responses.
10077
10266
  *
@@ -10087,12 +10276,12 @@ var StreamerServer = class {
10087
10276
  * it.
10088
10277
  */
10089
10278
  withReconciledLifecycle(sessions) {
10090
- if (this.sessionLifecycles.size === 0) return sessions;
10279
+ if (this.sessionVerdicts.size === 0) return sessions;
10091
10280
  return sessions.map((s) => {
10092
10281
  if (s.ptyAttached) return s;
10093
- const verdict = this.sessionLifecycles.get(s.id);
10282
+ const verdict = this.sessionVerdicts.get(s.id);
10094
10283
  if (!verdict) return s;
10095
- return { ...s, lifecycle: verdict, lifecycleSource: "reconcile" };
10284
+ return { ...s, lifecycle: verdict.lifecycle, lifecycleSource: "reconcile" };
10096
10285
  });
10097
10286
  }
10098
10287
  addSessionSubscriber(sessionId, ws) {
@@ -10160,14 +10349,30 @@ var StreamerServer = class {
10160
10349
  let verdicts = [];
10161
10350
  try {
10162
10351
  const rows = this.managedSessionsRepo.listNonTerminal();
10352
+ if (rows.length === PROBE_SET_MAX) {
10353
+ this.log.warn(
10354
+ `[reconcile] probe set hit its cap of ${PROBE_SET_MAX} \u2014 older rows skipped`,
10355
+ {
10356
+ event: "registry.probe_truncated",
10357
+ limit: PROBE_SET_MAX
10358
+ }
10359
+ );
10360
+ }
10163
10361
  if (rows.length === 0) return [];
10164
10362
  verdicts = await reconcileSessions(
10165
10363
  rows,
10166
10364
  { isPidAlive, getProcessArgs },
10167
- this.streamerInstanceId
10365
+ this.streamerInstanceId,
10366
+ currentBootToken()
10168
10367
  );
10169
10368
  for (const v of verdicts) {
10170
- this.sessionLifecycles.set(v.sessionId, v.lifecycle);
10369
+ this.sessionVerdicts.set(v.sessionId, v);
10370
+ if (v.reason === PRE_BOOT_REASON) {
10371
+ this.log.info(`[reconcile] ${v.sessionId} predates this machine boot \u2014 pid not probed`, {
10372
+ event: "sessions.boot_token_mismatch",
10373
+ sessionId: v.sessionId
10374
+ });
10375
+ }
10171
10376
  if (v.lifecycle === "completed" || v.lifecycle === "failed") {
10172
10377
  this.managedSessionsRepo.recordStatus(v.sessionId, "idle", "reconcile", {
10173
10378
  completedAt: /* @__PURE__ */ new Date()
@@ -10189,6 +10394,30 @@ var StreamerServer = class {
10189
10394
  }
10190
10395
  return verdicts;
10191
10396
  }
10397
+ /**
10398
+ * Drop finished sessions the registry has held long enough (plan Phase 4).
10399
+ *
10400
+ * The registry is authoritative and never rebuilt from the cache, so nothing
10401
+ * else would ever remove a row: without this it grows for the life of the
10402
+ * install, and every boot pays for rows about sessions from months ago.
10403
+ */
10404
+ pruneTerminalSessions() {
10405
+ if (!this.managedSessionsRepo) return;
10406
+ try {
10407
+ const pruned = this.managedSessionsRepo.pruneTerminal();
10408
+ if (pruned > 0) {
10409
+ this.log.info(`[registry] pruned ${pruned} terminal session row(s)`, {
10410
+ event: "registry.pruned",
10411
+ pruned
10412
+ });
10413
+ }
10414
+ } catch (err) {
10415
+ this.log.warn("[registry] failed to prune terminal sessions", {
10416
+ event: "registry.prune_failed",
10417
+ err
10418
+ });
10419
+ }
10420
+ }
10192
10421
  /**
10193
10422
  * Seed the session list with what previous runs left behind (persistence plan
10194
10423
  * Phase 1, gaps G1/G2/G8).
@@ -10205,7 +10434,7 @@ var StreamerServer = class {
10205
10434
  * by id rather than duplicating it.
10206
10435
  */
10207
10436
  rehydratePreviousSessions(verdicts) {
10208
- if (!this.featureFlags.sessionRehydration || !this.managedSessionsRepo) return;
10437
+ if (!this.managedSessionsRepo) return [];
10209
10438
  try {
10210
10439
  const now = Date.now();
10211
10440
  const rows = this.managedSessionsRepo.listRecoverable({
@@ -10214,16 +10443,30 @@ var StreamerServer = class {
10214
10443
  });
10215
10444
  const truncated = rows.length > REHYDRATE_MAX;
10216
10445
  const candidates = truncated ? rows.slice(0, REHYDRATE_MAX) : rows;
10217
- if (candidates.length === 0) return;
10218
- const lifecycleByVerdict = new Map(verdicts.map((v) => [v.sessionId, v.lifecycle]));
10446
+ if (!this.featureFlags.sessionRehydration || candidates.length === 0) return candidates;
10447
+ const verdictById = new Map(verdicts.map((v) => [v.sessionId, v]));
10219
10448
  let rehydrated = 0;
10449
+ const skippedBy = {};
10220
10450
  for (const row of candidates) {
10221
10451
  if (this.sessionStore.getManaged(row.session_id)) continue;
10222
- if (!shouldRehydrate(row, { now, projectExists: existsSync11 })) continue;
10452
+ const skip = rehydrateSkipReason(row, { now, projectExists: existsSync11 });
10453
+ if (skip) {
10454
+ skippedBy[skip] = (skippedBy[skip] ?? 0) + 1;
10455
+ this.log.info(`[rehydrate] skipped ${row.session_id}: ${skip}`, {
10456
+ event: "sessions.rehydrate_skipped",
10457
+ sessionId: row.session_id,
10458
+ reason: skip
10459
+ });
10460
+ continue;
10461
+ }
10223
10462
  this.sessionStore.addManaged(rowToStubSession(row));
10224
- this.sessionLifecycles.set(
10463
+ this.sessionVerdicts.set(
10225
10464
  row.session_id,
10226
- lifecycleByVerdict.get(row.session_id) ?? "resumable"
10465
+ verdictById.get(row.session_id) ?? {
10466
+ sessionId: row.session_id,
10467
+ lifecycle: "resumable",
10468
+ reason: "recovered from the registry at boot"
10469
+ }
10227
10470
  );
10228
10471
  if (row.completed_at != null) this.selfPtyEndedAt.set(row.session_id, row.completed_at);
10229
10472
  rehydrated++;
@@ -10232,14 +10475,110 @@ var StreamerServer = class {
10232
10475
  event: "sessions.rehydrated",
10233
10476
  rehydrated,
10234
10477
  skipped: candidates.length - rehydrated,
10478
+ skippedBy,
10235
10479
  truncated
10236
10480
  });
10481
+ return candidates;
10237
10482
  } catch (err) {
10238
10483
  this.log.warn("[rehydrate] failed to rehydrate previous sessions", {
10239
10484
  event: "sessions.rehydrate_failed",
10240
10485
  err
10241
10486
  });
10487
+ return [];
10488
+ }
10489
+ }
10490
+ /** Resume only the recent sessions the user explicitly allowed us to start at boot. */
10491
+ async autoResumePreviousSessions(rows) {
10492
+ if (!this.autoResumeOnBoot) return;
10493
+ const plan = planAutoResume(rows, { now: Date.now(), projectExists: existsSync11 });
10494
+ const skippedBy = {};
10495
+ for (const { row, reason } of plan.skipped) {
10496
+ skippedBy[reason] = (skippedBy[reason] ?? 0) + 1;
10497
+ this.log.debug(`[auto-resume] skipped ${row.session_id}: ${reason}`, {
10498
+ event: "sessions.auto_resume_skipped",
10499
+ sessionId: row.session_id,
10500
+ reason
10501
+ });
10502
+ }
10503
+ if (plan.skipped.length > 0) {
10504
+ this.log.info(
10505
+ `[auto-resume] left ${plan.skipped.length} ineligible session(s) for manual resume`,
10506
+ {
10507
+ event: "sessions.auto_resume_skipped",
10508
+ skipped: plan.skipped.length,
10509
+ skippedBy
10510
+ }
10511
+ );
10512
+ }
10513
+ for (const row of plan.overflow) {
10514
+ this.log.info(`[auto-resume] left ${row.session_id} for manual resume: ceiling reached`, {
10515
+ event: "sessions.auto_resume_skipped",
10516
+ sessionId: row.session_id,
10517
+ reason: "ceiling_reached"
10518
+ });
10242
10519
  }
10520
+ let resumed = 0;
10521
+ let failed = 0;
10522
+ const inFlight = /* @__PURE__ */ new Set();
10523
+ let started = 0;
10524
+ const resume = async (row) => {
10525
+ try {
10526
+ const outcome = await this.resumeSession({
10527
+ sessionId: row.session_id,
10528
+ projectName: row.project_name,
10529
+ branch: row.branch
10530
+ });
10531
+ if (!outcome.ok) {
10532
+ failed++;
10533
+ this.log.info(`[auto-resume] skipped ${row.session_id}: ${outcome.reason}`, {
10534
+ event: "sessions.auto_resume_skipped",
10535
+ sessionId: row.session_id,
10536
+ reason: outcome.reason,
10537
+ ...outcome.reason === "conversation_busy" && {
10538
+ detectedBy: outcome.detectedBy,
10539
+ lastActivityMs: outcome.lastActivityMs,
10540
+ likelyOwner: outcome.likelyOwner
10541
+ }
10542
+ });
10543
+ return;
10544
+ }
10545
+ resumed++;
10546
+ this.log.info(`[auto-resume] resumed ${row.session_id}`, {
10547
+ event: "sessions.auto_resume_succeeded",
10548
+ sessionId: row.session_id,
10549
+ alreadyRunning: outcome.alreadyRunning
10550
+ });
10551
+ } catch (err) {
10552
+ failed++;
10553
+ this.log.warn(`[auto-resume] failed to resume ${row.session_id}`, {
10554
+ event: "sessions.auto_resume_failed",
10555
+ sessionId: row.session_id,
10556
+ err
10557
+ });
10558
+ }
10559
+ };
10560
+ for (const row of plan.attempts) {
10561
+ while (inFlight.size >= AUTO_RESUME_CONCURRENCY) {
10562
+ await Promise.race(inFlight);
10563
+ }
10564
+ if (started > 0) {
10565
+ await new Promise((resolve2) => setTimeout(resolve2, AUTO_RESUME_STAGGER_MS));
10566
+ }
10567
+ const task = resume(row);
10568
+ inFlight.add(task);
10569
+ void task.then(() => inFlight.delete(task));
10570
+ started++;
10571
+ }
10572
+ await Promise.all(inFlight);
10573
+ if (resumed > 0) this.wsHub.broadcast(this.sessionListPayload());
10574
+ this.log.info(`[auto-resume] completed boot recovery: ${resumed} resumed`, {
10575
+ event: "sessions.auto_resume_completed",
10576
+ attempted: plan.attempts.length,
10577
+ resumed,
10578
+ failed,
10579
+ ineligible: plan.skipped.length,
10580
+ overflow: plan.overflow.length
10581
+ });
10243
10582
  }
10244
10583
  /**
10245
10584
  * Pick a token guaranteed to appear in the spawned process's argv, for the
@@ -10323,7 +10662,7 @@ var StreamerServer = class {
10323
10662
  const now = /* @__PURE__ */ new Date();
10324
10663
  for (const session of this.ptyManager.listSessions()) {
10325
10664
  try {
10326
- this.managedSessionsRepo.recordStatus(session.id, "idle", "shutdown", {
10665
+ this.managedSessionsRepo.recordStatus(session.id, session.status, "shutdown", {
10327
10666
  completedAt: now,
10328
10667
  lastActivityAt: session.lastActivityAt ?? null,
10329
10668
  promptCount: session.promptCount
@@ -10561,7 +10900,11 @@ var StreamerServer = class {
10561
10900
  );
10562
10901
  this.scannerPersistenceDisabled = true;
10563
10902
  }
10564
- void this.reconcilePreviousSessions().then((v) => this.rehydratePreviousSessions(v));
10903
+ void this.reconcilePreviousSessions().then(async (v) => {
10904
+ const recoverableRows = this.rehydratePreviousSessions(v);
10905
+ await this.autoResumePreviousSessions(recoverableRows);
10906
+ this.pruneTerminalSessions();
10907
+ });
10565
10908
  if (this.skipStartupWarmup) {
10566
10909
  this.log.debug?.("startup warm-up scan skipped (skipStartupWarmup)", {
10567
10910
  event: "cache.warmup_skipped"
@@ -12129,32 +12472,85 @@ var StreamerServer = class {
12129
12472
  json(res, 400, { error: "Missing sessionId" });
12130
12473
  return;
12131
12474
  }
12475
+ const outcome = await this.resumeSession({
12476
+ sessionId,
12477
+ force: body.force === true,
12478
+ projectName: body.projectName,
12479
+ branch: body.branch
12480
+ });
12481
+ if (!outcome.ok) {
12482
+ switch (outcome.reason) {
12483
+ case "history_file_missing":
12484
+ json(res, 404, {
12485
+ error: "Conversation history file is missing; it can no longer be resumed",
12486
+ code: "history_file_missing"
12487
+ });
12488
+ return;
12489
+ case "no_project_path":
12490
+ json(res, 400, { error: "Could not determine project path" });
12491
+ return;
12492
+ case "conversation_busy":
12493
+ json(res, 409, {
12494
+ error: "This conversation looks active in another session",
12495
+ code: "CONVERSATION_BUSY",
12496
+ detectedBy: outcome.detectedBy,
12497
+ lastActivityMs: outcome.lastActivityMs,
12498
+ likelyOwner: outcome.likelyOwner
12499
+ });
12500
+ return;
12501
+ }
12502
+ }
12503
+ if (outcome.alreadyRunning) {
12504
+ json(res, 200, outcome.response);
12505
+ return;
12506
+ }
12507
+ this.broadcastOrUnicastSessionList(req);
12508
+ json(res, 201, outcome.response ?? outcome.session);
12509
+ }
12510
+ /**
12511
+ * Resume a session, from an HTTP request or from the boot path.
12512
+ *
12513
+ * Extracted from `handleResume` so both callers hit the **same collision
12514
+ * probe** (plan Phase 7c). The probe is what stops this streamer attaching to
12515
+ * a conversation an external terminal already owns; a second, hand-adapted
12516
+ * copy of this sequence in the boot path is how two agents end up appending
12517
+ * to one JSONL at 4am with nobody watching.
12518
+ *
12519
+ * Returns a typed reason rather than writing a response, so the HTTP caller
12520
+ * maps it to a status code and the boot caller logs it.
12521
+ */
12522
+ async resumeSession(opts) {
12523
+ const { sessionId } = opts;
12132
12524
  if (this.ptyManager.hasSession(sessionId)) {
12133
- const resp2 = this.sessionStore.get(sessionId, this.ptyAttachedIds());
12134
- if (resp2) {
12135
- json(res, 200, resp2);
12136
- return;
12525
+ const resp = this.sessionStore.get(sessionId, this.ptyAttachedIds());
12526
+ if (resp) {
12527
+ return { ok: true, alreadyRunning: true, session: null, response: resp };
12528
+ }
12529
+ }
12530
+ let jsonlPath = this.findJsonlPath(sessionId);
12531
+ let conv = await this.findConversationByUuid(sessionId);
12532
+ let historyId = sessionId;
12533
+ let registryProvider;
12534
+ if (!jsonlPath && !conv) {
12535
+ const row = this.managedSessionsRepo?.get(sessionId) ?? null;
12536
+ const boundId = row ? resumeIdForRow(row) : null;
12537
+ if (boundId != null && boundId !== sessionId) {
12538
+ historyId = boundId;
12539
+ registryProvider = row?.provider;
12540
+ jsonlPath = this.findJsonlPath(boundId);
12541
+ conv = await this.findConversationByUuid(boundId);
12137
12542
  }
12138
12543
  }
12139
- const jsonlPath = this.findJsonlPath(sessionId);
12140
12544
  const jsonlCwd = jsonlPath ? await this.readCwdFromJsonl(jsonlPath) : null;
12141
- const conv = await this.findConversationByUuid(sessionId);
12142
12545
  const projectPath = jsonlCwd ?? conv?.projectPath;
12143
12546
  if (!projectPath) {
12144
- if (!conv && !jsonlPath) {
12145
- json(res, 404, {
12146
- error: "Conversation history file is missing; it can no longer be resumed",
12147
- code: "history_file_missing"
12148
- });
12149
- return;
12150
- }
12151
- json(res, 400, { error: "Could not determine project path" });
12152
- return;
12547
+ if (!conv && !jsonlPath) return { ok: false, reason: "history_file_missing" };
12548
+ return { ok: false, reason: "no_project_path" };
12153
12549
  }
12154
12550
  let discovered = [];
12155
- const cached2 = this.discoveryCache;
12156
- if (cached2 && Date.now() - cached2.fetchedAt < DISCOVERY_TTL_MS) {
12157
- discovered = cached2.entries;
12551
+ const cached3 = this.discoveryCache;
12552
+ if (cached3 && Date.now() - cached3.fetchedAt < DISCOVERY_TTL_MS) {
12553
+ discovered = cached3.entries;
12158
12554
  } else {
12159
12555
  try {
12160
12556
  discovered = await Promise.race([
@@ -12170,45 +12566,50 @@ var StreamerServer = class {
12170
12566
  }
12171
12567
  }
12172
12568
  const busy = conversationBusy({
12173
- conversationId: sessionId,
12569
+ // The id another owner's argv would actually carry — for a placeholder
12570
+ // that is the bound rollout id, not the one the client asked for.
12571
+ conversationId: historyId,
12174
12572
  projectPath,
12175
12573
  jsonlPath,
12176
12574
  discovered,
12177
12575
  windowMs: resolveResumeBusyWindowMs(),
12178
12576
  selfPtyEndedAt: this.selfPtyEndedAt.get(sessionId) ?? null
12179
12577
  });
12180
- if (busy.busy && body.force !== true) {
12181
- json(res, 409, {
12182
- error: "This conversation looks active in another session",
12183
- code: "CONVERSATION_BUSY",
12578
+ if (busy.busy && opts.force !== true) {
12579
+ return {
12580
+ ok: false,
12581
+ reason: "conversation_busy",
12184
12582
  detectedBy: busy.detectedBy,
12185
12583
  lastActivityMs: busy.lastActivityMs,
12186
12584
  likelyOwner: busy.likelyOwner
12187
- });
12188
- return;
12585
+ };
12189
12586
  }
12190
12587
  if (busy.busy) {
12191
12588
  this.contendedSessions.add(sessionId);
12192
12589
  }
12193
- const cachedConvMeta = this.cache?.getMetaById(sessionId);
12194
- const provider = coerceProviderForRunner(conv?.provider ?? cachedConvMeta?.provider);
12590
+ const cachedConvMeta = this.cache?.getMetaById(historyId);
12591
+ const provider = coerceProviderForRunner(
12592
+ conv?.provider ?? cachedConvMeta?.provider ?? registryProvider
12593
+ );
12195
12594
  this.discoveryCache = null;
12196
12595
  const session = await this.ptyManager.start(sessionId, {
12197
12596
  provider,
12198
12597
  projectPath,
12199
- projectName: body.projectName,
12200
- branch: body.branch,
12598
+ projectName: opts.projectName,
12599
+ branch: opts.branch,
12600
+ // Omitted on every ordinary resume, so argv is unchanged there.
12601
+ ...historyId !== sessionId && { resumeId: historyId },
12201
12602
  claudeFlags: this.claudeFlags,
12202
12603
  claudeExtraArgs: this.claudeExtraArgs,
12203
12604
  ...this.spawnFlagOverrides()
12204
12605
  });
12606
+ if (historyId !== sessionId) session.boundConversationId = historyId;
12205
12607
  this.sessionStore.addManaged(session);
12206
12608
  this.recordSessionSpawn(session);
12207
- void this.watchConversationFile(sessionId);
12208
- const resp = this.sessionStore.get(session.id, this.ptyAttachedIds());
12209
- this.broadcastOrUnicastSessionList(req);
12210
- json(res, 201, resp ?? session);
12609
+ void this.watchConversationFile(sessionId, historyId);
12610
+ const response = this.sessionStore.get(session.id, this.ptyAttachedIds());
12211
12611
  this.enrichResumedSessionAsync(sessionId, projectPath, conv);
12612
+ return { ok: true, alreadyRunning: false, session, response };
12212
12613
  }
12213
12614
  enrichResumedSessionAsync(sessionId, projectPath, conv) {
12214
12615
  try {
@@ -12221,18 +12622,18 @@ var StreamerServer = class {
12221
12622
  session.filePath = conv.filePath ?? void 0;
12222
12623
  }
12223
12624
  if (!this.cache || !this.projectsRepo || !this.conversationsRepo) return;
12224
- const cached2 = this.cache.getMetaById(sessionId);
12225
- if (cached2) {
12226
- session.model = cached2.model ?? void 0;
12227
- session.preview = cached2.preview ?? void 0;
12228
- const first = cached2.firstMessage ? JSON.parse(cached2.firstMessage) : null;
12229
- const last = cached2.lastMessage ? JSON.parse(cached2.lastMessage) : null;
12625
+ const cached3 = this.cache.getMetaById(sessionId);
12626
+ if (cached3) {
12627
+ session.model = cached3.model ?? void 0;
12628
+ session.preview = cached3.preview ?? void 0;
12629
+ const first = cached3.firstMessage ? JSON.parse(cached3.firstMessage) : null;
12630
+ const last = cached3.lastMessage ? JSON.parse(cached3.lastMessage) : null;
12230
12631
  session.firstMessageText = first?.text ?? void 0;
12231
12632
  session.firstMessageAt = first?.timestamp ? new Date(first.timestamp).toISOString() : void 0;
12232
12633
  session.lastMessageText = last?.text ?? void 0;
12233
12634
  session.lastMessageAt = last?.timestamp ? new Date(last.timestamp).toISOString() : void 0;
12234
12635
  }
12235
- let resolvedProjectId = cached2?.projectId ?? null;
12636
+ let resolvedProjectId = cached3?.projectId ?? null;
12236
12637
  if (!resolvedProjectId) {
12237
12638
  const project = this.projectsRepo.upsertProjectByPath(projectPath);
12238
12639
  resolvedProjectId = project.id;
@@ -12804,9 +13205,12 @@ var StreamerServer = class {
12804
13205
  }
12805
13206
  }
12806
13207
  // ─── File Watcher Wiring ─────────────────────────────────────────
12807
- async watchConversationFile(sessionId) {
13208
+ // `historyId` is the id the provider filed the history under, which for a
13209
+ // fresh Codex session is its rollout id rather than our placeholder. The map
13210
+ // stays keyed by `sessionId` — that is what broadcasts resolve against.
13211
+ async watchConversationFile(sessionId, historyId = sessionId) {
12808
13212
  try {
12809
- const conversation = await this.findConversationByUuid(sessionId);
13213
+ const conversation = await this.findConversationByUuid(historyId);
12810
13214
  if (conversation?.filePath) {
12811
13215
  this.sessionFileMap.set(sessionId, conversation.filePath);
12812
13216
  this.fileWatcher.watch(conversation.filePath);
@@ -12820,7 +13224,7 @@ var StreamerServer = class {
12820
13224
  // file isn't slurped in full.
12821
13225
  readFirstLineSessionId(filePath) {
12822
13226
  try {
12823
- const content = readFileSync8(filePath, "utf8");
13227
+ const content = readFileSync9(filePath, "utf8");
12824
13228
  const nl = content.indexOf("\n");
12825
13229
  const firstLine = nl === -1 ? content : content.slice(0, nl);
12826
13230
  if (!firstLine.trim()) return null;
@@ -12870,7 +13274,7 @@ var StreamerServer = class {
12870
13274
  cleanup();
12871
13275
  this.sessionFileMap.set(sessionId, resolvedFilePath);
12872
13276
  try {
12873
- const existing = readFileSync8(resolvedFilePath, "utf8").split("\n").filter(Boolean);
13277
+ const existing = readFileSync9(resolvedFilePath, "utf8").split("\n").filter(Boolean);
12874
13278
  if (existing.length > 0) {
12875
13279
  this.broadcastConversationLines(sessionId, existing);
12876
13280
  }
@@ -12923,7 +13327,7 @@ var StreamerServer = class {
12923
13327
  };
12924
13328
  const matchesProjectPath = (candidatePath) => {
12925
13329
  try {
12926
- const firstLine = readFileSync8(candidatePath, "utf8").split("\n", 1)[0];
13330
+ const firstLine = readFileSync9(candidatePath, "utf8").split("\n", 1)[0];
12927
13331
  if (!firstLine) return null;
12928
13332
  const parsed = JSON.parse(firstLine);
12929
13333
  if (parsed?.type !== "session_meta") return null;
@@ -12969,10 +13373,19 @@ var StreamerServer = class {
12969
13373
  const codexSessionId = match.id;
12970
13374
  cleanup();
12971
13375
  this.sessionStore.updateManaged(sessionId, { boundConversationId: codexSessionId });
13376
+ try {
13377
+ this.managedSessionsRepo?.recordBinding(sessionId, codexSessionId);
13378
+ } catch (err) {
13379
+ this.log.warn("[registry] failed to record Codex rollout binding", {
13380
+ event: "registry.binding_write_failed",
13381
+ sessionId,
13382
+ err
13383
+ });
13384
+ }
12972
13385
  this.sessionFileMap.set(sessionId, candidatePath);
12973
13386
  this.fileWatcher.watch(candidatePath);
12974
13387
  try {
12975
- const existing = readFileSync8(candidatePath, "utf8").split("\n").filter(Boolean);
13388
+ const existing = readFileSync9(candidatePath, "utf8").split("\n").filter(Boolean);
12976
13389
  if (existing.length > 0) {
12977
13390
  this.broadcastConversationLines(sessionId, existing);
12978
13391
  }