@adhdev/session-host-core 1.0.82-rc.36 → 1.0.82-rc.38
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.d.mts +90 -94
- package/dist/index.d.ts +90 -94
- package/dist/index.js +114 -131
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +107 -126
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/cleanup-expected.ts +114 -0
- package/src/control-plane.ts +58 -0
- package/src/index.ts +12 -2
- package/src/types.ts +40 -8
- package/src/operator-recovery.ts +0 -134
package/dist/index.mjs
CHANGED
|
@@ -5,129 +5,8 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
5
5
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
6
|
});
|
|
7
7
|
|
|
8
|
-
// src/operator-recovery.ts
|
|
9
|
-
import { createPublicKey, randomBytes, verify } from "crypto";
|
|
10
|
-
import { isAbsolute, basename } from "path";
|
|
11
|
-
import { performance as performance2 } from "perf_hooks";
|
|
12
|
-
var OPERATOR_RECOVERY_TARGET = Object.freeze({
|
|
13
|
-
association: "operator_asserted",
|
|
14
|
-
taskId: "a1cdf3c0-f029-4d17-a8e9-db64e0a55501",
|
|
15
|
-
meshId: "mesh_271444af883843e9b36c67155b87b22f",
|
|
16
|
-
attemptId: "mesh_queue:mesh_271444af883843e9b36c67155b87b22f:a1cdf3c0-f029-4d17-a8e9-db64e0a55501:0",
|
|
17
|
-
generation: 0,
|
|
18
|
-
dispatchNonce: 1,
|
|
19
|
-
deliveryId: "dlv-7e259b34-b049-47ee-a466-2ca9c12bcff4",
|
|
20
|
-
ownerDaemonId: "daemon_mach_4462a75330c548be9c2e74dd9f7f6ffb",
|
|
21
|
-
nodeId: "node_d82de0cef9b042b6948986609e32142f",
|
|
22
|
-
workerDaemonId: "daemon_mach_6733c865d60f432dbeb119a6875ee303",
|
|
23
|
-
sessionId: "de321185-4ebc-40aa-8c6e-b4919e87fae3",
|
|
24
|
-
providerSessionId: "60806c79-7d77-4614-9fa6-e628533478ba",
|
|
25
|
-
namespace: "6a2ef3162182"
|
|
26
|
-
});
|
|
27
|
-
var recoveryTargetBytes = JSON.stringify(OPERATOR_RECOVERY_TARGET);
|
|
28
|
-
function assertRecoveryLaunchIdentity(value) {
|
|
29
|
-
if (!value || typeof value !== "object") return;
|
|
30
|
-
const identities = /* @__PURE__ */ new Set([
|
|
31
|
-
"sessionId",
|
|
32
|
-
"runtimeId",
|
|
33
|
-
"runtimeSessionId",
|
|
34
|
-
"providerSessionId",
|
|
35
|
-
"presetSessionKey",
|
|
36
|
-
"resumeSessionId",
|
|
37
|
-
"key"
|
|
38
|
-
]);
|
|
39
|
-
const nested = /* @__PURE__ */ new Set(["options", "meta", "settings", "settingsOverride", "restartBinding", "sessionBinding", "launchCommand"]);
|
|
40
|
-
for (const [key, field] of Object.entries(value)) {
|
|
41
|
-
if (identities.has(key) || ["args", "cliArgs", "cliArgsWithBrain"].includes(key)) {
|
|
42
|
-
const bytes = JSON.stringify(field)?.toLowerCase();
|
|
43
|
-
if (bytes?.includes(OPERATOR_RECOVERY_TARGET.sessionId) || bytes?.includes(OPERATOR_RECOVERY_TARGET.providerSessionId)) {
|
|
44
|
-
throw new Error("operator_recovery_fenced");
|
|
45
|
-
}
|
|
46
|
-
} else if (nested.has(key)) assertRecoveryLaunchIdentity(field);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function isRecoveryEndpoint(endpoint) {
|
|
50
|
-
return isAbsolute(endpoint) && basename(endpoint) === `adhdev-preview-session-host-${OPERATOR_RECOVERY_TARGET.namespace}.sock`;
|
|
51
|
-
}
|
|
52
|
-
var RecoveryOwnerAdmission = class {
|
|
53
|
-
constructor(enrollment, clock = () => performance2.now()) {
|
|
54
|
-
this.clock = clock;
|
|
55
|
-
this.enrollment = Object.freeze({ ...enrollment });
|
|
56
|
-
if (!enrollment.enrollmentId || !enrollment.coordinatorBootId || !enrollment.authEpoch || typeof enrollment.verifyCurrent !== "function") throw new Error("recovery_owner_enrollment_required");
|
|
57
|
-
this.key = createPublicKey(enrollment.publicKey);
|
|
58
|
-
if (this.key.asymmetricKeyType !== "ed25519") throw new Error("recovery_owner_key_type");
|
|
59
|
-
}
|
|
60
|
-
key;
|
|
61
|
-
enrollment;
|
|
62
|
-
pending = /* @__PURE__ */ new Map();
|
|
63
|
-
grants = /* @__PURE__ */ new WeakMap();
|
|
64
|
-
lastNow = -Infinity;
|
|
65
|
-
now() {
|
|
66
|
-
const n = this.clock();
|
|
67
|
-
if (!Number.isFinite(n) || n < this.lastNow || this.enrollment.verifyCurrent() !== true) {
|
|
68
|
-
this.pending.clear();
|
|
69
|
-
this.lastNow = Infinity;
|
|
70
|
-
throw new Error("recovery_owner_revoked");
|
|
71
|
-
}
|
|
72
|
-
this.lastNow = n;
|
|
73
|
-
return n;
|
|
74
|
-
}
|
|
75
|
-
challenge(operation = "anchor_recover") {
|
|
76
|
-
if (!["anchor_recover", "fence_install", "witness_enroll"].includes(operation)) throw new Error("recovery_owner_operation_rejected");
|
|
77
|
-
const now = this.now();
|
|
78
|
-
for (const [message2, entry] of this.pending) if (entry.deadline <= now) this.pending.delete(message2);
|
|
79
|
-
if (this.pending.size >= 16) throw new Error("recovery_owner_challenge_limit");
|
|
80
|
-
const message = JSON.stringify([
|
|
81
|
-
"operator_recovery_v1",
|
|
82
|
-
operation,
|
|
83
|
-
recoveryTargetBytes,
|
|
84
|
-
this.enrollment.enrollmentId,
|
|
85
|
-
this.enrollment.coordinatorBootId,
|
|
86
|
-
this.enrollment.authEpoch,
|
|
87
|
-
randomBytes(32).toString("hex")
|
|
88
|
-
]);
|
|
89
|
-
this.pending.set(message, { deadline: now + 3e4, operation });
|
|
90
|
-
return Object.freeze({ message });
|
|
91
|
-
}
|
|
92
|
-
authorize(message, signature) {
|
|
93
|
-
const now = this.now();
|
|
94
|
-
const entry = this.pending.get(message);
|
|
95
|
-
this.pending.delete(message);
|
|
96
|
-
if (!entry || now >= entry.deadline || !/^[a-f0-9]{128}$/.test(signature) || !verify(null, Buffer.from(message), this.key, Buffer.from(signature, "hex"))) {
|
|
97
|
-
throw new Error("recovery_owner_signature_rejected");
|
|
98
|
-
}
|
|
99
|
-
const grant = Object.freeze({ purpose: "operator_recovery_v1", operation: entry.operation });
|
|
100
|
-
this.grants.set(grant, entry);
|
|
101
|
-
return grant;
|
|
102
|
-
}
|
|
103
|
-
assertGrant(grant) {
|
|
104
|
-
const now = this.now();
|
|
105
|
-
const entry = typeof grant === "object" && grant !== null ? this.grants.get(grant) : void 0;
|
|
106
|
-
if (!entry || now >= entry.deadline) throw new Error("recovery_owner_grant_rejected");
|
|
107
|
-
}
|
|
108
|
-
/** Burn synchronously before any work or await. The private continuation
|
|
109
|
-
* rechecks the ORIGINAL deadline and revocation; it cannot mint a new grant. */
|
|
110
|
-
reserve(grant, operation) {
|
|
111
|
-
this.assertGrant(grant);
|
|
112
|
-
const entry = this.grants.get(grant);
|
|
113
|
-
this.grants.delete(grant);
|
|
114
|
-
if (entry.operation !== operation) throw new Error("recovery_owner_operation_rejected");
|
|
115
|
-
return () => {
|
|
116
|
-
if (this.now() >= entry.deadline) throw new Error("recovery_owner_grant_rejected");
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
consume(grant) {
|
|
120
|
-
this.assertGrant(grant);
|
|
121
|
-
this.grants.delete(grant);
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
|
|
125
8
|
// src/types.ts
|
|
126
9
|
var SESSION_HOST_SUPPORTED_REQUEST_TYPES = [
|
|
127
|
-
"prepare_operator_recovery_fence_v1",
|
|
128
|
-
"install_operator_recovery_fence_v1",
|
|
129
|
-
"attest_operator_recovery_v1",
|
|
130
|
-
"get_operator_recovery_coverage_v1",
|
|
131
10
|
"get_upgrade_lease",
|
|
132
11
|
"acquire_upgrade_lease",
|
|
133
12
|
"release_upgrade_lease",
|
|
@@ -141,8 +20,10 @@ var SESSION_HOST_SUPPORTED_REQUEST_TYPES = [
|
|
|
141
20
|
"get_input_state",
|
|
142
21
|
"resize_session",
|
|
143
22
|
"stop_session",
|
|
23
|
+
"stop_session_if_expected",
|
|
144
24
|
"stop_mesh_writer",
|
|
145
25
|
"delete_session",
|
|
26
|
+
"delete_session_if_expected",
|
|
146
27
|
"resume_session",
|
|
147
28
|
"acquire_write",
|
|
148
29
|
"release_write",
|
|
@@ -160,6 +41,71 @@ var SESSION_HOST_SUPPORTED_REQUEST_TYPES = [
|
|
|
160
41
|
"list_sessions"
|
|
161
42
|
];
|
|
162
43
|
|
|
44
|
+
// src/cleanup-expected.ts
|
|
45
|
+
var CLEANUP_AFTER_STOP_SIDE_EFFECT_PREFIX = "cleanup_after_stop_side_effect:";
|
|
46
|
+
function tagCleanupAfterStopSideEffect(code) {
|
|
47
|
+
return `${CLEANUP_AFTER_STOP_SIDE_EFFECT_PREFIX}${code}`;
|
|
48
|
+
}
|
|
49
|
+
function stripCleanupAfterStopSideEffect(raw) {
|
|
50
|
+
if (raw.startsWith(CLEANUP_AFTER_STOP_SIDE_EFFECT_PREFIX)) {
|
|
51
|
+
return { afterStop: true, code: raw.slice(CLEANUP_AFTER_STOP_SIDE_EFFECT_PREFIX.length) };
|
|
52
|
+
}
|
|
53
|
+
return { afterStop: false, code: raw };
|
|
54
|
+
}
|
|
55
|
+
function hostBootIdFromStartedAt(hostStartedAt) {
|
|
56
|
+
return `hs:${hostStartedAt}`;
|
|
57
|
+
}
|
|
58
|
+
function resolveHostBootId(opts) {
|
|
59
|
+
if (typeof opts.upgradeBootId === "string" && opts.upgradeBootId) return opts.upgradeBootId;
|
|
60
|
+
if (!Number.isFinite(opts.hostStartedAt) || opts.hostStartedAt <= 0) return null;
|
|
61
|
+
return hostBootIdFromStartedAt(opts.hostStartedAt);
|
|
62
|
+
}
|
|
63
|
+
function refuseCleanupExpectedIdentity(record, expected, currentHostBootId) {
|
|
64
|
+
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) {
|
|
65
|
+
return "cleanup_expected_malformed";
|
|
66
|
+
}
|
|
67
|
+
if (!currentHostBootId) return "cleanup_host_boot_unknown";
|
|
68
|
+
if (expected.hostBootId !== currentHostBootId) return "cleanup_host_boot_mismatch";
|
|
69
|
+
if (!record) return "cleanup_session_not_running";
|
|
70
|
+
if (record.runtimeKey !== expected.runtimeKey || record.startedAt !== expected.startedAt) {
|
|
71
|
+
return "cleanup_native_incarnation_mismatch";
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
function refuseCleanupExpectedAssignment(record, expected, opts = {}) {
|
|
76
|
+
const active = record.meta?.meshActiveAssignment;
|
|
77
|
+
if (expected.taskId === null) {
|
|
78
|
+
if (active && typeof active.taskId === "string" && active.taskId) return "cleanup_expected_unbound_now_bound";
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
if (!active || typeof active.taskId !== "string" || !active.taskId) {
|
|
82
|
+
return opts.allowClearedBound ? null : "cleanup_expected_bound_now_unbound";
|
|
83
|
+
}
|
|
84
|
+
if (active.taskId !== expected.taskId || active.attemptId !== expected.attemptId || active.generation !== expected.generation || active.dispatchNonce !== expected.dispatchNonce) {
|
|
85
|
+
return "cleanup_assignment_mismatch";
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
function refuseCleanupExpectedStop(record, expected, currentHostBootId, hasRuntime) {
|
|
90
|
+
const identity = refuseCleanupExpectedIdentity(record, expected, currentHostBootId);
|
|
91
|
+
if (identity) return identity;
|
|
92
|
+
if (!record || record.termination || !hasRuntime || record.lifecycle !== "running") {
|
|
93
|
+
return "cleanup_session_not_running";
|
|
94
|
+
}
|
|
95
|
+
return refuseCleanupExpectedAssignment(record, expected);
|
|
96
|
+
}
|
|
97
|
+
function refuseCleanupExpectedDelete(record, expected, currentHostBootId, hasRuntime, force) {
|
|
98
|
+
if (!record) return "cleanup_session_not_running";
|
|
99
|
+
const identity = refuseCleanupExpectedIdentity(record, expected, currentHostBootId);
|
|
100
|
+
if (identity) return identity;
|
|
101
|
+
if (hasRuntime) {
|
|
102
|
+
if (!force) return "cleanup_session_still_running";
|
|
103
|
+
if (record.termination || record.lifecycle !== "running") return "cleanup_session_not_running";
|
|
104
|
+
return refuseCleanupExpectedAssignment(record, expected);
|
|
105
|
+
}
|
|
106
|
+
return refuseCleanupExpectedAssignment(record, expected, { allowClearedBound: true });
|
|
107
|
+
}
|
|
108
|
+
|
|
163
109
|
// src/termination.ts
|
|
164
110
|
function classifyTermination(input) {
|
|
165
111
|
const { exitCode, terminatedAt } = input;
|
|
@@ -1486,15 +1432,48 @@ function createSessionHostControlPlane(transport) {
|
|
|
1486
1432
|
getDiagnostics(payload = {}) {
|
|
1487
1433
|
return transport.request("get_host_diagnostics", payload);
|
|
1488
1434
|
},
|
|
1435
|
+
async resolveHostBootId() {
|
|
1436
|
+
let diagnostics;
|
|
1437
|
+
try {
|
|
1438
|
+
diagnostics = await transport.request("get_host_diagnostics", {});
|
|
1439
|
+
} catch {
|
|
1440
|
+
return null;
|
|
1441
|
+
}
|
|
1442
|
+
if (typeof diagnostics.hostBootId === "string" && diagnostics.hostBootId) {
|
|
1443
|
+
return diagnostics.hostBootId;
|
|
1444
|
+
}
|
|
1445
|
+
try {
|
|
1446
|
+
const proof = await transport.request("get_upgrade_lease", {});
|
|
1447
|
+
return resolveHostBootId({
|
|
1448
|
+
upgradeBootId: typeof proof?.bootId === "string" ? proof.bootId : null,
|
|
1449
|
+
hostStartedAt: diagnostics.hostStartedAt
|
|
1450
|
+
});
|
|
1451
|
+
} catch (error) {
|
|
1452
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
1453
|
+
if (msg.includes("native_host_upgrade_binding_unavailable") || /unsupported|unknown request/i.test(msg)) {
|
|
1454
|
+
return resolveHostBootId({ hostStartedAt: diagnostics.hostStartedAt });
|
|
1455
|
+
}
|
|
1456
|
+
return null;
|
|
1457
|
+
}
|
|
1458
|
+
},
|
|
1489
1459
|
listSessions() {
|
|
1490
1460
|
return transport.request("list_sessions", {});
|
|
1491
1461
|
},
|
|
1492
1462
|
stopSession(sessionId) {
|
|
1493
1463
|
return transport.request("stop_session", { sessionId });
|
|
1494
1464
|
},
|
|
1465
|
+
stopSessionIfExpected(sessionId, expected) {
|
|
1466
|
+
return transport.request("stop_session_if_expected", { sessionId, expected });
|
|
1467
|
+
},
|
|
1495
1468
|
deleteSession(sessionId, opts = {}) {
|
|
1496
1469
|
return transport.request("delete_session", { sessionId, force: opts.force === true });
|
|
1497
1470
|
},
|
|
1471
|
+
deleteSessionIfExpected(sessionId, expected, opts = {}) {
|
|
1472
|
+
return transport.request(
|
|
1473
|
+
"delete_session_if_expected",
|
|
1474
|
+
{ sessionId, expected, force: opts.force === true }
|
|
1475
|
+
);
|
|
1476
|
+
},
|
|
1498
1477
|
resumeSession(sessionId) {
|
|
1499
1478
|
return transport.request("resume_session", { sessionId });
|
|
1500
1479
|
},
|
|
@@ -1522,6 +1501,7 @@ function createSessionHostControlPlane(transport) {
|
|
|
1522
1501
|
};
|
|
1523
1502
|
}
|
|
1524
1503
|
export {
|
|
1504
|
+
CLEANUP_AFTER_STOP_SIDE_EFFECT_PREFIX,
|
|
1525
1505
|
CLI_CHILD_NICE_ENV,
|
|
1526
1506
|
CLI_CHILD_NICE_INCREMENT,
|
|
1527
1507
|
CLI_CHILD_SCHEDULING_ENV,
|
|
@@ -1529,14 +1509,11 @@ export {
|
|
|
1529
1509
|
DEFAULT_SESSION_HOST_COLS,
|
|
1530
1510
|
DEFAULT_SESSION_HOST_ROWS,
|
|
1531
1511
|
DEFAULT_SESSION_RING_BUFFER_MAX_BYTES,
|
|
1532
|
-
OPERATOR_RECOVERY_TARGET,
|
|
1533
|
-
RecoveryOwnerAdmission,
|
|
1534
1512
|
SESSION_HOST_SUPPORTED_REQUEST_TYPES,
|
|
1535
1513
|
SessionHostClient,
|
|
1536
1514
|
SessionHostRegistry,
|
|
1537
1515
|
SessionRingBuffer,
|
|
1538
1516
|
applyTerminalColorEnv,
|
|
1539
|
-
assertRecoveryLaunchIdentity,
|
|
1540
1517
|
assertTestStorageIsolated,
|
|
1541
1518
|
buildRuntimeDisplayName,
|
|
1542
1519
|
buildRuntimeKey,
|
|
@@ -1555,17 +1532,19 @@ export {
|
|
|
1555
1532
|
getSessionHostSurfaceKind,
|
|
1556
1533
|
getWorkspaceLabel,
|
|
1557
1534
|
hiddenSpawn as hiddenSpawnProcess,
|
|
1535
|
+
hostBootIdFromStartedAt,
|
|
1558
1536
|
isDefaultInstanceConfigDir,
|
|
1559
|
-
isRecoveryEndpoint,
|
|
1560
1537
|
isSessionHostLiveRuntime,
|
|
1561
1538
|
isSessionHostRecoverySnapshot,
|
|
1562
1539
|
isTerminalRecord,
|
|
1563
1540
|
niceLaunch,
|
|
1564
1541
|
partitionSessionHostDiagnosticsSessions,
|
|
1565
1542
|
partitionSessionHostRecords,
|
|
1566
|
-
|
|
1543
|
+
refuseCleanupExpectedDelete,
|
|
1544
|
+
refuseCleanupExpectedStop,
|
|
1567
1545
|
replayLaunchEnv,
|
|
1568
1546
|
resolveAttachableRuntimeRecord,
|
|
1547
|
+
resolveHostBootId,
|
|
1569
1548
|
resolveInstanceConfigDir,
|
|
1570
1549
|
resolveRuntimeRecord,
|
|
1571
1550
|
resolveSessionHostCols,
|
|
@@ -1573,6 +1552,8 @@ export {
|
|
|
1573
1552
|
resolveSessionHostRows,
|
|
1574
1553
|
sanitizeSpawnEnv,
|
|
1575
1554
|
spawnCliChild,
|
|
1555
|
+
stripCleanupAfterStopSideEffect,
|
|
1556
|
+
tagCleanupAfterStopSideEffect,
|
|
1576
1557
|
validateSessionHostEndpoint,
|
|
1577
1558
|
writeEnvelope
|
|
1578
1559
|
};
|