zcode-acp-server 0.86.0 → 0.87.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.
Files changed (60) hide show
  1. package/README.md +10 -9
  2. package/README.zh-CN.md +7 -5
  3. package/dist/backend/index.d.ts +1 -1
  4. package/dist/backend/index.d.ts.map +1 -1
  5. package/dist/backend/index.js +1 -1
  6. package/dist/backend/index.js.map +1 -1
  7. package/dist/backend/resolve.d.ts +10 -0
  8. package/dist/backend/resolve.d.ts.map +1 -1
  9. package/dist/backend/resolve.js +60 -3
  10. package/dist/backend/resolve.js.map +1 -1
  11. package/dist/backend/sandbox.d.ts +85 -83
  12. package/dist/backend/sandbox.d.ts.map +1 -1
  13. package/dist/backend/sandbox.js +314 -222
  14. package/dist/backend/sandbox.js.map +1 -1
  15. package/dist/code-fingerprint.json +2 -2
  16. package/dist/config/deferred-probe-capability.d.ts +4 -0
  17. package/dist/config/deferred-probe-capability.d.ts.map +1 -1
  18. package/dist/config/deferred-probe-capability.js +110 -33
  19. package/dist/config/deferred-probe-capability.js.map +1 -1
  20. package/dist/config/options.d.ts.map +1 -1
  21. package/dist/config/options.js +71 -9
  22. package/dist/config/options.js.map +1 -1
  23. package/dist/config/settings.d.ts +1 -1
  24. package/dist/config/settings.d.ts.map +1 -1
  25. package/dist/config/settings.js +1 -1
  26. package/dist/config/settings.js.map +1 -1
  27. package/dist/config/user-config.d.ts +3 -3
  28. package/dist/config/user-config.d.ts.map +1 -1
  29. package/dist/handlers/account.d.ts +20 -1
  30. package/dist/handlers/account.d.ts.map +1 -1
  31. package/dist/handlers/account.js +29 -3
  32. package/dist/handlers/account.js.map +1 -1
  33. package/dist/handlers/dispatch.js +1 -1
  34. package/dist/handlers/dispatch.js.map +1 -1
  35. package/dist/handlers/sandbox-allow.d.ts +5 -4
  36. package/dist/handlers/sandbox-allow.d.ts.map +1 -1
  37. package/dist/handlers/sandbox-allow.js +22 -30
  38. package/dist/handlers/sandbox-allow.js.map +1 -1
  39. package/dist/handlers/session.d.ts +29 -0
  40. package/dist/handlers/session.d.ts.map +1 -1
  41. package/dist/handlers/session.js +75 -5
  42. package/dist/handlers/session.js.map +1 -1
  43. package/dist/i18n.js +14 -14
  44. package/dist/i18n.js.map +1 -1
  45. package/dist/quota/startplan.d.ts +100 -0
  46. package/dist/quota/startplan.d.ts.map +1 -0
  47. package/dist/quota/startplan.js +218 -0
  48. package/dist/quota/startplan.js.map +1 -0
  49. package/dist/server.d.ts +10 -10
  50. package/dist/server.d.ts.map +1 -1
  51. package/dist/server.js +22 -17
  52. package/dist/server.js.map +1 -1
  53. package/dist/translators/event-translator.d.ts.map +1 -1
  54. package/dist/translators/event-translator.js +12 -0
  55. package/dist/translators/event-translator.js.map +1 -1
  56. package/docs/SANDBOX.md +59 -35
  57. package/docs/TROUBLESHOOTING.md +1 -1
  58. package/docs/proposals/0002-plan-quota-usage.md +10 -0
  59. package/package.json +3 -2
  60. package/scripts/postinstall.mjs +27 -0
@@ -1408,6 +1408,10 @@ export async function resumeSession(server, params, cx) {
1408
1408
  const { zcodeSid, alreadyLive, origin } = await resolveResumeTarget(server, acpSid);
1409
1409
  // Resolving a durable alias may restore its recorded cwd; read it afterward.
1410
1410
  let cwd = server.serveMode ? process.cwd() : authoritativeSessionCwd(server, acpSid);
1411
+ // Same settle-before-read as loadSession (the TUI's deferred history replay
1412
+ // fetches right after this returns; an active notification turn's final
1413
+ // text only persists at its completion).
1414
+ await waitForBackgroundTurnIdle(server, zcodeSid);
1411
1415
  if (!alreadyLive) {
1412
1416
  // No runtimeModel pinning here: the session keeps its own persisted
1413
1417
  // model (see resumePreservingModel — an overlay is a FALLBACK only, when
@@ -1552,6 +1556,9 @@ export async function resumeIntoSession(server, cx, acpSid, zcodeTarget) {
1552
1556
  // flight's snapshot instead of re-querying mid-hydration.
1553
1557
  let settledHistory;
1554
1558
  try {
1559
+ // Same settle-before-read as loadSession: the resume flight's history
1560
+ // snapshot must not race an active background notification turn's tail.
1561
+ await waitForBackgroundTurnIdle(server, zcodeTarget);
1555
1562
  await syncProviderRegistry(server, cwd);
1556
1563
  const outcome = await resumePreservingModel(server,
1557
1564
  // Session-lifetime MCP set keeps riding every resume (#193).
@@ -1619,6 +1626,64 @@ export async function resumeIntoSession(server, cx, acpSid, zcodeTarget) {
1619
1626
  await emitInitialUsage(server, cx, acpSid, zcodeTarget, getOrCreateDiffer(server, zcodeTarget));
1620
1627
  return { ok: true, title: server.sessionTitles.get(acpSid) };
1621
1628
  }
1629
+ /** Poll cadence / cap for waitForBackgroundTurnIdle (see below). */
1630
+ const BG_TURN_SETTLE_POLL_MS = 250;
1631
+ const BG_TURN_SETTLE_TIMEOUT_MS = 45_000;
1632
+ /** Freshness bound for notifyTurnActiveSince, shared with the prompt path. */
1633
+ const BG_TURN_STALE_MS = 600_000;
1634
+ /**
1635
+ * Wait out an ACTIVE background notification turn (the model turn the backend
1636
+ * runs when a background task finishes) BEFORE a load/resume reads history.
1637
+ *
1638
+ * The turn's final reply persists only at COMPLETION — upstream writes the
1639
+ * assistant message row at request start but its text parts at finish
1640
+ * (turn-model-step.ts persistAssistantMessage/persistPart) — so a history
1641
+ * fetch racing the turn's tail reads a row with no text. The attach then
1642
+ * ships an empty-shell last reply, the client caches it as synced (same
1643
+ * message count), and the notification's text stays invisible until the next
1644
+ * prompt changes the count (observed 2026-11-15 on sess_70cd94db: the WeCom
1645
+ * "task done" push fires the moment the notification turn STARTS, so the
1646
+ * user's re-open reliably lands inside the generation window — every load in
1647
+ * the window replayed the reply-less shell and each follow-up metadata
1648
+ * attach saw matching counts, so it never re-fetched; the user had to ask a
1649
+ * new question to see the result). Waiting for the turn's terminal event
1650
+ * makes the subsequent read complete, and the wait runs BEFORE the client
1651
+ * attach takes effect (registerSession), so no live text-delta forwarding
1652
+ * interleaves with the replayed history either.
1653
+ *
1654
+ * Best-effort and bounded: on timeout the read proceeds as before
1655
+ * (incomplete tail) instead of blocking the attach forever, and a marker
1656
+ * older than BG_TURN_STALE_MS (the listener never saw the terminal event —
1657
+ * dropped stream) is ignored outright, mirroring the prompt path's
1658
+ * notify-turn busy budget in runPrompt.
1659
+ *
1660
+ * Exported for tests.
1661
+ */
1662
+ export async function waitForBackgroundTurnIdle(server, zcodeSid) {
1663
+ const activeAt = server.notifyTurnActiveSince.get(zcodeSid);
1664
+ if (activeAt === undefined || Date.now() - activeAt >= BG_TURN_STALE_MS)
1665
+ return true;
1666
+ const startedAt = Date.now();
1667
+ log(` [bg-settle] background notification turn active; waiting up to ` +
1668
+ `${Math.round(BG_TURN_SETTLE_TIMEOUT_MS / 1000)}s before reading history`);
1669
+ while (Date.now() - startedAt < BG_TURN_SETTLE_TIMEOUT_MS) {
1670
+ await new Promise((resolve) => setTimeout(resolve, BG_TURN_SETTLE_POLL_MS));
1671
+ const since = server.notifyTurnActiveSince.get(zcodeSid);
1672
+ if (since === undefined) {
1673
+ log(` [bg-settle] notification turn settled after ${Date.now() - startedAt}ms`);
1674
+ return true;
1675
+ }
1676
+ // A marker that went stale mid-wait (terminal lost) stops waiting; a
1677
+ // FRESH replacement marker (a second notification turn started) keeps
1678
+ // the wait alive under the same overall cap.
1679
+ if (Date.now() - since >= BG_TURN_STALE_MS)
1680
+ break;
1681
+ }
1682
+ warn(` [bg-settle] notification turn still active after ` +
1683
+ `${Math.round((Date.now() - startedAt) / 1000)}s; reading history anyway ` +
1684
+ `(the last reply may attach incomplete)`);
1685
+ return false;
1686
+ }
1622
1687
  /**
1623
1688
  * `session/load` → zcode `session/resume` + stream conversation history back as
1624
1689
  * `session/update` notifications (text/reasoning/简化 tool_call).
@@ -1635,6 +1700,10 @@ export async function loadSession(server, params, cx, opts = {}) {
1635
1700
  // Same placeholder resolution as resumeSession; alreadyLive targets skip the
1636
1701
  // backend resume RPC (the session is live in this subprocess).
1637
1702
  const { zcodeSid, alreadyLive, origin } = await resolveResumeTarget(server, acpSid);
1703
+ // BEFORE the resume flight and the history read: an active background
1704
+ // notification turn persists its final text only at completion, so a fetch
1705
+ // racing its tail ships an empty-shell last reply (see waitForBackgroundTurnIdle).
1706
+ await waitForBackgroundTurnIdle(server, zcodeSid);
1638
1707
  // Resolving a durable alias may restore its recorded cwd; read it afterward.
1639
1708
  let cwd = server.serveMode ? process.cwd() : authoritativeSessionCwd(server, acpSid);
1640
1709
  // Settled history from a resume flight THIS call took part in (performer or
@@ -2394,11 +2463,12 @@ export async function runOneTurn(server, opts) {
2394
2463
  }
2395
2464
  /** One prompt round: subscribe-before-send, run the event-driven turn loop. */
2396
2465
  async function runPrompt(server, params, cx, requestId, continuationRound = false, client) {
2397
- // Project-level sandbox flip (ADR-0011): a .zcode/acp/sandbox.json created
2398
- // after the backend spawned unsandboxed must arm on THIS prompt, not on the
2399
- // next bridge restart — kill the old process; the ensureBackend() below
2400
- // respawns under the profile and the subscribe-recovery path reloads the
2401
- // session. No-op unless the config appeared mid-run.
2466
+ // Global sandbox flip: `sandbox.enabled` turned on in the user config (or
2467
+ // ZCODE_ACP_SANDBOX=1 exported) after the backend spawned unsandboxed must
2468
+ // arm on THIS prompt, not on the next bridge restart — kill the old
2469
+ // process; the ensureBackend() below respawns under the profile and the
2470
+ // subscribe-recovery path reloads the session. No-op unless the switch
2471
+ // flipped mid-run.
2402
2472
  await server.applySandboxFlip();
2403
2473
  const backend = await server.ensureBackend();
2404
2474
  const { text, attachments } = await convertPromptContent(params.prompt, path.join(zcodeHomeDir(), "v2", "acp-attachments"));