@adhdev/session-host-core 1.0.82-rc.36 → 1.0.82-rc.37

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
@@ -141,8 +141,10 @@ var SESSION_HOST_SUPPORTED_REQUEST_TYPES = [
141
141
  "get_input_state",
142
142
  "resize_session",
143
143
  "stop_session",
144
+ "stop_session_if_expected",
144
145
  "stop_mesh_writer",
145
146
  "delete_session",
147
+ "delete_session_if_expected",
146
148
  "resume_session",
147
149
  "acquire_write",
148
150
  "release_write",
@@ -160,6 +162,71 @@ var SESSION_HOST_SUPPORTED_REQUEST_TYPES = [
160
162
  "list_sessions"
161
163
  ];
162
164
 
165
+ // src/cleanup-expected.ts
166
+ var CLEANUP_AFTER_STOP_SIDE_EFFECT_PREFIX = "cleanup_after_stop_side_effect:";
167
+ function tagCleanupAfterStopSideEffect(code) {
168
+ return `${CLEANUP_AFTER_STOP_SIDE_EFFECT_PREFIX}${code}`;
169
+ }
170
+ function stripCleanupAfterStopSideEffect(raw) {
171
+ if (raw.startsWith(CLEANUP_AFTER_STOP_SIDE_EFFECT_PREFIX)) {
172
+ return { afterStop: true, code: raw.slice(CLEANUP_AFTER_STOP_SIDE_EFFECT_PREFIX.length) };
173
+ }
174
+ return { afterStop: false, code: raw };
175
+ }
176
+ function hostBootIdFromStartedAt(hostStartedAt) {
177
+ return `hs:${hostStartedAt}`;
178
+ }
179
+ function resolveHostBootId(opts) {
180
+ if (typeof opts.upgradeBootId === "string" && opts.upgradeBootId) return opts.upgradeBootId;
181
+ if (!Number.isFinite(opts.hostStartedAt) || opts.hostStartedAt <= 0) return null;
182
+ return hostBootIdFromStartedAt(opts.hostStartedAt);
183
+ }
184
+ function refuseCleanupExpectedIdentity(record, expected, currentHostBootId) {
185
+ if (!expected || expected.sessionId !== (record?.sessionId ?? "") || typeof expected.hostBootId !== "string" || !expected.hostBootId || typeof expected.runtimeKey !== "string" || !expected.runtimeKey || !Number.isSafeInteger(expected.startedAt) || expected.startedAt <= 0) {
186
+ return "cleanup_expected_malformed";
187
+ }
188
+ if (!currentHostBootId) return "cleanup_host_boot_unknown";
189
+ if (expected.hostBootId !== currentHostBootId) return "cleanup_host_boot_mismatch";
190
+ if (!record) return "cleanup_session_not_running";
191
+ if (record.runtimeKey !== expected.runtimeKey || record.startedAt !== expected.startedAt) {
192
+ return "cleanup_native_incarnation_mismatch";
193
+ }
194
+ return null;
195
+ }
196
+ function refuseCleanupExpectedAssignment(record, expected, opts = {}) {
197
+ const active = record.meta?.meshActiveAssignment;
198
+ if (expected.taskId === null) {
199
+ if (active && typeof active.taskId === "string" && active.taskId) return "cleanup_expected_unbound_now_bound";
200
+ return null;
201
+ }
202
+ if (!active || typeof active.taskId !== "string" || !active.taskId) {
203
+ return opts.allowClearedBound ? null : "cleanup_expected_bound_now_unbound";
204
+ }
205
+ if (active.taskId !== expected.taskId || active.attemptId !== expected.attemptId || active.generation !== expected.generation || active.dispatchNonce !== expected.dispatchNonce) {
206
+ return "cleanup_assignment_mismatch";
207
+ }
208
+ return null;
209
+ }
210
+ function refuseCleanupExpectedStop(record, expected, currentHostBootId, hasRuntime) {
211
+ const identity = refuseCleanupExpectedIdentity(record, expected, currentHostBootId);
212
+ if (identity) return identity;
213
+ if (!record || record.termination || !hasRuntime || record.lifecycle !== "running") {
214
+ return "cleanup_session_not_running";
215
+ }
216
+ return refuseCleanupExpectedAssignment(record, expected);
217
+ }
218
+ function refuseCleanupExpectedDelete(record, expected, currentHostBootId, hasRuntime, force) {
219
+ if (!record) return "cleanup_session_not_running";
220
+ const identity = refuseCleanupExpectedIdentity(record, expected, currentHostBootId);
221
+ if (identity) return identity;
222
+ if (hasRuntime) {
223
+ if (!force) return "cleanup_session_still_running";
224
+ if (record.termination || record.lifecycle !== "running") return "cleanup_session_not_running";
225
+ return refuseCleanupExpectedAssignment(record, expected);
226
+ }
227
+ return refuseCleanupExpectedAssignment(record, expected, { allowClearedBound: true });
228
+ }
229
+
163
230
  // src/termination.ts
164
231
  function classifyTermination(input) {
165
232
  const { exitCode, terminatedAt } = input;
@@ -1486,15 +1553,48 @@ function createSessionHostControlPlane(transport) {
1486
1553
  getDiagnostics(payload = {}) {
1487
1554
  return transport.request("get_host_diagnostics", payload);
1488
1555
  },
1556
+ async resolveHostBootId() {
1557
+ let diagnostics;
1558
+ try {
1559
+ diagnostics = await transport.request("get_host_diagnostics", {});
1560
+ } catch {
1561
+ return null;
1562
+ }
1563
+ if (typeof diagnostics.hostBootId === "string" && diagnostics.hostBootId) {
1564
+ return diagnostics.hostBootId;
1565
+ }
1566
+ try {
1567
+ const proof = await transport.request("get_upgrade_lease", {});
1568
+ return resolveHostBootId({
1569
+ upgradeBootId: typeof proof?.bootId === "string" ? proof.bootId : null,
1570
+ hostStartedAt: diagnostics.hostStartedAt
1571
+ });
1572
+ } catch (error) {
1573
+ const msg = error instanceof Error ? error.message : String(error);
1574
+ if (msg.includes("native_host_upgrade_binding_unavailable") || /unsupported|unknown request/i.test(msg)) {
1575
+ return resolveHostBootId({ hostStartedAt: diagnostics.hostStartedAt });
1576
+ }
1577
+ return null;
1578
+ }
1579
+ },
1489
1580
  listSessions() {
1490
1581
  return transport.request("list_sessions", {});
1491
1582
  },
1492
1583
  stopSession(sessionId) {
1493
1584
  return transport.request("stop_session", { sessionId });
1494
1585
  },
1586
+ stopSessionIfExpected(sessionId, expected) {
1587
+ return transport.request("stop_session_if_expected", { sessionId, expected });
1588
+ },
1495
1589
  deleteSession(sessionId, opts = {}) {
1496
1590
  return transport.request("delete_session", { sessionId, force: opts.force === true });
1497
1591
  },
1592
+ deleteSessionIfExpected(sessionId, expected, opts = {}) {
1593
+ return transport.request(
1594
+ "delete_session_if_expected",
1595
+ { sessionId, expected, force: opts.force === true }
1596
+ );
1597
+ },
1498
1598
  resumeSession(sessionId) {
1499
1599
  return transport.request("resume_session", { sessionId });
1500
1600
  },
@@ -1522,6 +1622,7 @@ function createSessionHostControlPlane(transport) {
1522
1622
  };
1523
1623
  }
1524
1624
  export {
1625
+ CLEANUP_AFTER_STOP_SIDE_EFFECT_PREFIX,
1525
1626
  CLI_CHILD_NICE_ENV,
1526
1627
  CLI_CHILD_NICE_INCREMENT,
1527
1628
  CLI_CHILD_SCHEDULING_ENV,
@@ -1555,6 +1656,7 @@ export {
1555
1656
  getSessionHostSurfaceKind,
1556
1657
  getWorkspaceLabel,
1557
1658
  hiddenSpawn as hiddenSpawnProcess,
1659
+ hostBootIdFromStartedAt,
1558
1660
  isDefaultInstanceConfigDir,
1559
1661
  isRecoveryEndpoint,
1560
1662
  isSessionHostLiveRuntime,
@@ -1564,8 +1666,11 @@ export {
1564
1666
  partitionSessionHostDiagnosticsSessions,
1565
1667
  partitionSessionHostRecords,
1566
1668
  recoveryTargetBytes,
1669
+ refuseCleanupExpectedDelete,
1670
+ refuseCleanupExpectedStop,
1567
1671
  replayLaunchEnv,
1568
1672
  resolveAttachableRuntimeRecord,
1673
+ resolveHostBootId,
1569
1674
  resolveInstanceConfigDir,
1570
1675
  resolveRuntimeRecord,
1571
1676
  resolveSessionHostCols,
@@ -1573,6 +1678,8 @@ export {
1573
1678
  resolveSessionHostRows,
1574
1679
  sanitizeSpawnEnv,
1575
1680
  spawnCliChild,
1681
+ stripCleanupAfterStopSideEffect,
1682
+ tagCleanupAfterStopSideEffect,
1576
1683
  validateSessionHostEndpoint,
1577
1684
  writeEnvelope
1578
1685
  };