zcode-acp-server 0.84.1 → 0.84.3

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.
Files changed (44) hide show
  1. package/README.md +1 -1
  2. package/dist/code-fingerprint.json +1 -1
  3. package/dist/config/user-config.d.ts +20 -0
  4. package/dist/config/user-config.d.ts.map +1 -1
  5. package/dist/config/user-config.js +24 -0
  6. package/dist/config/user-config.js.map +1 -1
  7. package/dist/handlers/background-tasks.d.ts +34 -0
  8. package/dist/handlers/background-tasks.d.ts.map +1 -1
  9. package/dist/handlers/background-tasks.js +69 -1
  10. package/dist/handlers/background-tasks.js.map +1 -1
  11. package/dist/handlers/io.d.ts +6 -3
  12. package/dist/handlers/io.d.ts.map +1 -1
  13. package/dist/handlers/io.js +6 -3
  14. package/dist/handlers/io.js.map +1 -1
  15. package/dist/handlers/session.d.ts.map +1 -1
  16. package/dist/handlers/session.js +54 -9
  17. package/dist/handlers/session.js.map +1 -1
  18. package/dist/remote/hub-server.d.ts.map +1 -1
  19. package/dist/remote/hub-server.js +139 -3
  20. package/dist/remote/hub-server.js.map +1 -1
  21. package/dist/remote/session-close-endpoint.d.ts.map +1 -1
  22. package/dist/remote/session-close-endpoint.js +5 -0
  23. package/dist/remote/session-close-endpoint.js.map +1 -1
  24. package/dist/remote/session-delete-endpoint.d.ts +15 -5
  25. package/dist/remote/session-delete-endpoint.d.ts.map +1 -1
  26. package/dist/remote/session-delete-endpoint.js +146 -11
  27. package/dist/remote/session-delete-endpoint.js.map +1 -1
  28. package/dist/remote/status-endpoint.d.ts +4 -1
  29. package/dist/remote/status-endpoint.d.ts.map +1 -1
  30. package/dist/remote/status-endpoint.js +6 -1
  31. package/dist/remote/status-endpoint.js.map +1 -1
  32. package/dist/server.d.ts +54 -0
  33. package/dist/server.d.ts.map +1 -1
  34. package/dist/server.js +145 -1
  35. package/dist/server.js.map +1 -1
  36. package/dist/tasks-index.d.ts +46 -1
  37. package/dist/tasks-index.d.ts.map +1 -1
  38. package/dist/tasks-index.js +136 -2
  39. package/dist/tasks-index.js.map +1 -1
  40. package/docs/ARCHITECTURE.md +1 -1
  41. package/docs/REMOTE-CLIENTS.md +58 -0
  42. package/docs/REMOTE.md +7 -0
  43. package/docs/REPLAY-GUIDE.md +8 -0
  44. package/package.json +1 -1
@@ -489,7 +489,15 @@ export async function newSession(server, params, client) {
489
489
  // mock factories throw on reading an export they do not define).
490
490
  try {
491
491
  const tasksIndexAtNew = await import("../tasks-index.js");
492
- void tasksIndexAtNew.reviveTombstonesOnActivity({ workspacePath: cwd }).catch(() => { });
492
+ // probeGone (#376) is safe here even though lazy session/new spawns no
493
+ // backend: the probe uses an EXISTING backend only and answers empty
494
+ // (blind revival) when there is none.
495
+ void tasksIndexAtNew
496
+ .reviveTombstonesOnActivity({
497
+ workspacePath: cwd,
498
+ probeGone: (ids) => server.probeGoneBackendSessions(ids),
499
+ })
500
+ .catch(() => { });
493
501
  }
494
502
  catch {
495
503
  // tasks-index unavailable — revival is best-effort.
@@ -588,6 +596,7 @@ async function ensureBackendResident(server, acpSid, zcodeSid) {
588
596
  }
589
597
  if (residency === "gone") {
590
598
  warn(`ensureRealSession: ${acpSid} → backend session ${zcodeSid} no longer exists`);
599
+ retireGoneSessionTasks(zcodeSid);
591
600
  throw new Error(messages().sessionEvicted(acpSid));
592
601
  }
593
602
  try {
@@ -597,6 +606,7 @@ async function ensureBackendResident(server, acpSid, zcodeSid) {
597
606
  catch (e) {
598
607
  if (isSessionGoneError(e)) {
599
608
  warn(`ensureRealSession: ${acpSid} → backend session ${zcodeSid} no longer exists`);
609
+ retireGoneSessionTasks(zcodeSid);
600
610
  throw new Error(messages().sessionEvicted(acpSid));
601
611
  }
602
612
  log(`ensureRealSession: reload failed, continuing with existing mapping ` +
@@ -912,7 +922,15 @@ export async function listSessions(server, params) {
912
922
  if (listCwd && listCwd !== "/") {
913
923
  try {
914
924
  const { reviveTombstonesOnActivity } = await import("../tasks-index.js");
915
- await reviveTombstonesOnActivity({ workspacePath: listCwd });
925
+ // probeGone (#376): before a mass revival, ids the backend can no
926
+ // longer serve (physically deleted upstream / soft-deleted) retire
927
+ // instead of resurrecting. The backend is alive in this scope (it just
928
+ // answered the listing); a probe failure answers empty → blind
929
+ // revival, the pre-#376 behavior.
930
+ await reviveTombstonesOnActivity({
931
+ workspacePath: listCwd,
932
+ probeGone: (ids) => server.probeGoneBackendSessions(ids),
933
+ });
916
934
  }
917
935
  catch {
918
936
  // revival unavailable — filter as-is.
@@ -1117,6 +1135,21 @@ async function replayResumeHistory(server, cx, acpSid, zcodeSid) {
1117
1135
  ` (tail ${MARTTY_RESUME_TAIL} of ${slice.meta.totalMessages} on record)`);
1118
1136
  });
1119
1137
  }
1138
+ /**
1139
+ * Fire-and-forget tombstone retirement (#376): a session the backend refuses
1140
+ * to serve (physically deleted upstream, or soft-deleted — resume answers
1141
+ * "Session not found" either way) must stop being revivable, or every future
1142
+ * `reviveTombstonesOnActivity` resurrects a zombie row whose only use is a
1143
+ * sessionEvicted error. Guarded typeof: test suites mock tasks-index
1144
+ * partially. Best-effort, never blocks the error path.
1145
+ */
1146
+ function retireGoneSessionTasks(zcodeSid) {
1147
+ if (!zcodeSid)
1148
+ return;
1149
+ void import("../tasks-index.js")
1150
+ .then((tasksIndex) => typeof tasksIndex.retireTasks === "function" ? tasksIndex.retireTasks([zcodeSid]) : 0)
1151
+ .catch(() => undefined);
1152
+ }
1120
1153
  /**
1121
1154
  * Surface a resume failure honestly, keyed on WHY the id is known. Always
1122
1155
  * throws. Two not-found shapes get an actionable message:
@@ -1127,7 +1160,7 @@ async function replayResumeHistory(server, cx, acpSid, zcodeSid) {
1127
1160
  * Any OTHER failure (transient timeout, lock, network) keeps the backend's
1128
1161
  * own error: guessing a cause for it would misreport it.
1129
1162
  */
1130
- function translateResumeFailure(methodLabel, acpSid, origin, e) {
1163
+ function translateResumeFailure(methodLabel, acpSid, origin, zcodeSid, e) {
1131
1164
  const raw = e instanceof Error ? e.message : String(e);
1132
1165
  if (!/不存在|not\s*found/i.test(raw))
1133
1166
  throw e;
@@ -1136,6 +1169,7 @@ function translateResumeFailure(methodLabel, acpSid, origin, e) {
1136
1169
  throw new Error(messages().loadUnknownAlias(acpSid));
1137
1170
  }
1138
1171
  warn(`${methodLabel}: ${acpSid} → backend session no longer exists (backend said: ${raw})`);
1172
+ retireGoneSessionTasks(zcodeSid);
1139
1173
  throw new Error(messages().sessionEvicted(acpSid));
1140
1174
  }
1141
1175
  /** `session/resume` → zcode `session/resume` (with runtimeModel overlay). */
@@ -1176,7 +1210,7 @@ export async function resumeSession(server, params, cx) {
1176
1210
  resumeResult = outcome.result;
1177
1211
  }
1178
1212
  catch (e) {
1179
- translateResumeFailure("session/resume", acpSid, origin, e);
1213
+ translateResumeFailure("session/resume", acpSid, origin, zcodeSid, e);
1180
1214
  }
1181
1215
  // The resume RPC succeeded — the session is now loaded in this backend.
1182
1216
  server.markBackendLoaded(acpSid);
@@ -1439,7 +1473,7 @@ export async function loadSession(server, params, cx, opts = {}) {
1439
1473
  settledHistory = outcome.history;
1440
1474
  }
1441
1475
  catch (e) {
1442
- translateResumeFailure("session/load", acpSid, origin, e);
1476
+ translateResumeFailure("session/load", acpSid, origin, zcodeSid, e);
1443
1477
  }
1444
1478
  // The resume RPC succeeded — the session is now loaded in this backend.
1445
1479
  server.markBackendLoaded(acpSid);
@@ -1551,8 +1585,11 @@ export async function loadSession(server, params, cx, opts = {}) {
1551
1585
  server.lastMode.set(acpSid, modes.currentModeId);
1552
1586
  // A turn from a prior client may still be in flight (the bridge runs it to
1553
1587
  // completion regardless of who prompted); flag it so re-attaching clients
1554
- // restore their running state. Editor-initiated turns land here too.
1555
- const turnActive = [...server.pendingTurns.values()].some((t) => t.zcodeSid === zcodeSid);
1588
+ // restore their running state. Editor-initiated turns land here too — as do
1589
+ // foreign turns the BACKEND started on its own (background-subagent wake,
1590
+ // subagent_message; tracked by the background listener).
1591
+ const turnActive = [...server.pendingTurns.values()].some((t) => t.zcodeSid === zcodeSid) ||
1592
+ server.foreignActiveTurns.has(zcodeSid);
1556
1593
  const result = {
1557
1594
  modes,
1558
1595
  configOptions: await buildConfigOptions(server, zcodeSid, clientConnectionRoot(cx)),
@@ -2109,10 +2146,14 @@ export async function runOneTurn(server, opts) {
2109
2146
  // catch up immediately instead of waiting for the 60s interval.
2110
2147
  void forceRefreshQuota();
2111
2148
  // Report "running" only while no other turn for the session took over
2112
- // (preempt): the preempting turn's own running:true must survive. The
2149
+ // (preempt): the preempting turn's own running:true must survive. A
2150
+ // foreign turn (background-subagent wake / subagent_message — tracked in
2151
+ // server.foreignActiveTurns) that started mid-loop also keeps the flag
2152
+ // up: this END transition must not clear a turn it doesn't own. The
2113
2153
  // usage extras ride along only when the turn reported usage — a bare
2114
2154
  // end keeps the exact pre-extras payload shape.
2115
- const stillBusy = [...server.pendingTurns.values()].some((t) => t.zcodeSid === zcodeSid);
2155
+ const stillBusy = [...server.pendingTurns.values()].some((t) => t.zcodeSid === zcodeSid) ||
2156
+ server.foreignActiveTurns.has(zcodeSid);
2116
2157
  await emitTurnState(stillBusy, lastUsage ? { usage: lastUsage, turnMs: Date.now() - turnStartedAt } : undefined);
2117
2158
  // Start the armed auto-compact ONLY now, after deregistration and the
2118
2159
  // running:false turnState: from here on a cancel or follow-up prompt finds
@@ -2676,6 +2717,10 @@ async function closeBackendSession(server, zcodeSid) {
2676
2717
  catch (e) {
2677
2718
  log(` [stop] session/close send failed (ignored): ${e instanceof Error ? e.message : String(e)}`);
2678
2719
  }
2720
+ // The runtime teardown kills any in-flight FOREIGN turn with no terminal
2721
+ // event ever arriving for it — drop the tracking entry or the loop's
2722
+ // finally would read the stale entry as stillBusy (false-running forever).
2723
+ server.foreignActiveTurns.delete(zcodeSid);
2679
2724
  }
2680
2725
  /**
2681
2726
  * Drain gate: a recent cancel/preempt means the backend side needs settling