gestalt-mobile 0.71.0 → 0.71.2
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/server/server/composition.js +6 -1
- package/dist/server/server/features/autopilot/application/takeover-supervision.js +18 -5
- package/dist/server/server/features/sessions/application/runtime-release-store.js +6 -0
- package/dist/server/server/platform/codex/session-runtime.js +87 -12
- package/dist/server/server/platform/persistence/migrate.js +1 -0
- package/dist/server/server/platform/persistence/sqlite-runtime-release-store.js +27 -0
- package/package.json +1 -1
|
@@ -73,6 +73,7 @@ import { RelaySession, } from './features/sessions/model/relay-session.js';
|
|
|
73
73
|
import { AgentActivityRegistry, } from './features/agent-activity/registry.js';
|
|
74
74
|
import { SupervisionTakeover } from './features/autopilot/application/takeover-supervision.js';
|
|
75
75
|
import { SqliteSupervisionTakeoverStore } from './platform/persistence/sqlite-supervision-takeover-store.js';
|
|
76
|
+
import { SqliteRuntimeReleaseStore } from './platform/persistence/sqlite-runtime-release-store.js';
|
|
76
77
|
import { isOrgPlanTakeoverCall } from '../shared/contracts/org-plan-takeover.js';
|
|
77
78
|
import { WriterAcquisitionError } from './features/sessions/application/writer-acquisition.js';
|
|
78
79
|
import { toAgentActivityDto } from './features/agent-activity/activity-dto.js';
|
|
@@ -876,6 +877,10 @@ export async function composeRelayApp(options) {
|
|
|
876
877
|
autopilot.planStatusChanged(id);
|
|
877
878
|
autopilot.restore(id);
|
|
878
879
|
},
|
|
880
|
+
failed: (sessionId, boundary) => {
|
|
881
|
+
const now = new Date().toISOString();
|
|
882
|
+
events.publish(journal.append(sessionId, 'autopilot.takeover-failed', { kind: boundary }, now));
|
|
883
|
+
},
|
|
879
884
|
});
|
|
880
885
|
const providerRecovery = new SessionProviderRecovery({
|
|
881
886
|
find: (id) => sessions.find(id),
|
|
@@ -1473,7 +1478,7 @@ export async function composeRelayApp(options) {
|
|
|
1473
1478
|
await runtime.validateExecutorPolicy(sessionId, report.requested);
|
|
1474
1479
|
assertCurrent();
|
|
1475
1480
|
return { ...report.requested, assertCurrent };
|
|
1476
|
-
}, sessionScratch)
|
|
1481
|
+
}, sessionScratch, new SqliteRuntimeReleaseStore(database))
|
|
1477
1482
|
: null;
|
|
1478
1483
|
const handleKimiNotification = (sessionId, event) => {
|
|
1479
1484
|
const occurredAt = new Date().toISOString();
|
|
@@ -19,10 +19,18 @@ export class SupervisionTakeover {
|
|
|
19
19
|
const prior = this.running.get(sessionId);
|
|
20
20
|
if (prior)
|
|
21
21
|
return prior;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}))
|
|
22
|
+
let boundary = 'context';
|
|
23
|
+
const run = this.run(sessionId, threadId, turnId, (value) => {
|
|
24
|
+
boundary = value;
|
|
25
|
+
}).catch(() => {
|
|
26
|
+
try {
|
|
27
|
+
this.deps.failed?.(sessionId, boundary);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
/* diagnostics must not change the takeover result */
|
|
31
|
+
}
|
|
32
|
+
return { accepted: false, code: 'TAKEOVER_RUNTIME_UNAVAILABLE', boundary };
|
|
33
|
+
});
|
|
26
34
|
this.running.set(sessionId, run);
|
|
27
35
|
void run.finally(() => {
|
|
28
36
|
if (this.running.get(sessionId) === run)
|
|
@@ -30,7 +38,7 @@ export class SupervisionTakeover {
|
|
|
30
38
|
});
|
|
31
39
|
return run;
|
|
32
40
|
}
|
|
33
|
-
async run(sessionId, threadId, turnId) {
|
|
41
|
+
async run(sessionId, threadId, turnId, boundary) {
|
|
34
42
|
const before = this.deps.context(sessionId);
|
|
35
43
|
if (!before ||
|
|
36
44
|
before.session.provider !== 'codex' ||
|
|
@@ -81,23 +89,27 @@ export class SupervisionTakeover {
|
|
|
81
89
|
return { accepted: false, code: 'PREVIOUS_SUPERVISOR_NOT_FOUND' };
|
|
82
90
|
const fingerprint = planFingerprint(before.plan);
|
|
83
91
|
const generation = before.state?.generation ?? 0;
|
|
92
|
+
boundary('ownershipReservation');
|
|
84
93
|
const claim = this.deps.store.claim(before.identity, sessionId);
|
|
85
94
|
if (!claim)
|
|
86
95
|
return { accepted: false, code: 'TAKEOVER_IN_PROGRESS' };
|
|
87
96
|
if (!this.deps.store.retire(claim, previous.map((p) => p.session.id)))
|
|
88
97
|
return { accepted: false, code: 'TAKEOVER_CONTEXT_CHANGED' };
|
|
89
98
|
// Fence every predecessor before the first asynchronous process operation.
|
|
99
|
+
boundary('fencePreviousSupervisor');
|
|
90
100
|
for (const p of previous)
|
|
91
101
|
if (!this.deps.store.stopped(before.identity, p.session.id))
|
|
92
102
|
this.deps.fence(p.session.id);
|
|
93
103
|
for (const p of previous) {
|
|
94
104
|
if (this.deps.store.stopped(before.identity, p.session.id))
|
|
95
105
|
continue;
|
|
106
|
+
boundary('quiescePreviousWriter');
|
|
96
107
|
if (!(await this.deps.stop(p)))
|
|
97
108
|
return { accepted: false, code: 'PREVIOUS_WRITER_NOT_STOPPED' };
|
|
98
109
|
if (!this.deps.store.markStopped(claim, p.session.id))
|
|
99
110
|
return { accepted: false, code: 'TAKEOVER_CONTEXT_CHANGED' };
|
|
100
111
|
}
|
|
112
|
+
boundary('revalidateContext');
|
|
101
113
|
const current = this.deps.context(sessionId);
|
|
102
114
|
if (!current ||
|
|
103
115
|
current.session.threadId !== threadId ||
|
|
@@ -172,6 +184,7 @@ export class SupervisionTakeover {
|
|
|
172
184
|
updatedAt: now,
|
|
173
185
|
};
|
|
174
186
|
const previousSessionIds = previous.map((p) => p.session.id);
|
|
187
|
+
boundary('commitOwnership');
|
|
175
188
|
if (!this.deps.store.complete(claim, state, [
|
|
176
189
|
{
|
|
177
190
|
sessionId,
|
|
@@ -55,6 +55,8 @@ class SessionResource {
|
|
|
55
55
|
explicitShutdown = false;
|
|
56
56
|
pendingRequests = new Map();
|
|
57
57
|
threadId;
|
|
58
|
+
/** Only locally created roots are known empty, until any root turn is attempted or observed. */
|
|
59
|
+
historyKnownEmpty = false;
|
|
58
60
|
pendingThreadName;
|
|
59
61
|
writtenThreadName;
|
|
60
62
|
capabilities = new Map();
|
|
@@ -126,6 +128,7 @@ export class CodexSessionRuntime {
|
|
|
126
128
|
retrieval;
|
|
127
129
|
requireExecutorAdmission;
|
|
128
130
|
scratch;
|
|
131
|
+
runtimeReleases;
|
|
129
132
|
/** Trusted read-only port for the current controller-bound child on its owning process. */
|
|
130
133
|
async readExecutorConfiguration(input) {
|
|
131
134
|
const resource = this.sessions.get(input.sessionId);
|
|
@@ -148,7 +151,7 @@ export class CodexSessionRuntime {
|
|
|
148
151
|
// correlated-map constructor shape. Live ownership is `sessions` only.
|
|
149
152
|
_legacyProcesses = undefined, onNotification, onServerRequest, onProcessExit, resolveSkills, planStatusSource, onPlanStatus, planMeasurementBaseUrl,
|
|
150
153
|
// Compatibility slot: blocking app-server requests wait for explicit input.
|
|
151
|
-
_legacyRequestTimeoutMs = undefined, maxPendingRequests = 64, readerCwd, resolveModelConfig, retrieval, requireExecutorAdmission, scratch) {
|
|
154
|
+
_legacyRequestTimeoutMs = undefined, maxPendingRequests = 64, readerCwd, resolveModelConfig, retrieval, requireExecutorAdmission, scratch, runtimeReleases) {
|
|
152
155
|
this.launch = launch;
|
|
153
156
|
this.onNotification = onNotification;
|
|
154
157
|
this.onServerRequest = onServerRequest;
|
|
@@ -163,6 +166,7 @@ export class CodexSessionRuntime {
|
|
|
163
166
|
this.retrieval = retrieval;
|
|
164
167
|
this.requireExecutorAdmission = requireExecutorAdmission;
|
|
165
168
|
this.scratch = scratch;
|
|
169
|
+
this.runtimeReleases = runtimeReleases;
|
|
166
170
|
void _legacyProcesses;
|
|
167
171
|
void _legacyRequestTimeoutMs;
|
|
168
172
|
}
|
|
@@ -218,6 +222,7 @@ export class CodexSessionRuntime {
|
|
|
218
222
|
if (resource.retrieval)
|
|
219
223
|
this.retrieval?.bound?.(resource.retrieval, startedThreadId);
|
|
220
224
|
await this.verifyRetrieval(resource, session, true, beforeThreadStart);
|
|
225
|
+
resource.historyKnownEmpty = true;
|
|
221
226
|
await this.scratch?.associateProviderThread(session, 'codex', resource.threadId);
|
|
222
227
|
assertCurrent();
|
|
223
228
|
this.sessions.set(session.id, resource);
|
|
@@ -293,8 +298,10 @@ export class CodexSessionRuntime {
|
|
|
293
298
|
await this.confirmResourceRelease(resource, true);
|
|
294
299
|
}
|
|
295
300
|
this.retrieval?.release?.(session.id);
|
|
296
|
-
if (observedResource && !this.liveResources.get(session.id)?.size)
|
|
301
|
+
if (observedResource && !this.liveResources.get(session.id)?.size) {
|
|
302
|
+
this.runtimeReleases?.confirm(session);
|
|
297
303
|
this.confirmedWorkspaceReleases.add(session.id);
|
|
304
|
+
}
|
|
298
305
|
}
|
|
299
306
|
confirmResourceRelease(resource, strict = false) {
|
|
300
307
|
const prior = this.resourceReleases.get(resource);
|
|
@@ -435,6 +442,8 @@ export class CodexSessionRuntime {
|
|
|
435
442
|
throw new Error('CODEX_SESSION_NOT_RUNNING');
|
|
436
443
|
if (mayStart && !mayStart())
|
|
437
444
|
throw new Error('CODEX_TURN_START_CANCELLED');
|
|
445
|
+
// Fence before the RPC: an uncertain or failed response may still have started a turn.
|
|
446
|
+
resource.historyKnownEmpty = false;
|
|
438
447
|
const result = decodeTurnStart(await resource.process.rpc.request('turn/start', {
|
|
439
448
|
threadId: session.threadId,
|
|
440
449
|
input: [
|
|
@@ -603,7 +612,8 @@ export class CodexSessionRuntime {
|
|
|
603
612
|
// A user-confirmed Close already quiesced this root. Reopening it solely
|
|
604
613
|
// for takeover would conflict with the incoming workspace's running slot.
|
|
605
614
|
if (session.desiredState === 'stopped' &&
|
|
606
|
-
this.confirmedWorkspaceReleases.has(session.id)
|
|
615
|
+
(this.confirmedWorkspaceReleases.has(session.id) ||
|
|
616
|
+
this.runtimeReleases?.confirmed(session)) &&
|
|
607
617
|
!this.liveResources.get(session.id)?.size &&
|
|
608
618
|
!recordedProcesses.some((claim) => ['running', 'detached-active'].includes(claim.state)))
|
|
609
619
|
return true;
|
|
@@ -627,7 +637,7 @@ export class CodexSessionRuntime {
|
|
|
627
637
|
}
|
|
628
638
|
}
|
|
629
639
|
async quiesceTakeoverActors(session, recordedProcesses, executor, allowed) {
|
|
630
|
-
const writer = await this.
|
|
640
|
+
const writer = await this.acquireTakeoverControl(session, allowed);
|
|
631
641
|
if (!allowed()) {
|
|
632
642
|
await this.release(session.id);
|
|
633
643
|
return false;
|
|
@@ -768,6 +778,41 @@ export class CodexSessionRuntime {
|
|
|
768
778
|
resource.dispose();
|
|
769
779
|
return true;
|
|
770
780
|
}
|
|
781
|
+
/** A fenced predecessor needs a stop-only connection, never execution admission.
|
|
782
|
+
* thread/resume loads its existing identity without starting a model turn.
|
|
783
|
+
* No missing-rollout replacement, capability preparation or plan watcher is allowed here.
|
|
784
|
+
*/
|
|
785
|
+
async acquireTakeoverControl(session, allowed) {
|
|
786
|
+
if (this.ownsWriter(session.id))
|
|
787
|
+
return { session, replacementCreated: false };
|
|
788
|
+
if (!session.threadId)
|
|
789
|
+
throw new Error('CODEX_THREAD_ID_MISSING');
|
|
790
|
+
const resource = await this.createResource(session, 'takeover');
|
|
791
|
+
try {
|
|
792
|
+
if (!allowed())
|
|
793
|
+
throw new Error('TAKEOVER_CANCELLED');
|
|
794
|
+
await resource.process.rpc.request('initialize', {
|
|
795
|
+
clientInfo: { name: 'gestalt-mobile', version: '0.1.0' },
|
|
796
|
+
capabilities: { experimentalApi: true },
|
|
797
|
+
});
|
|
798
|
+
if (!allowed())
|
|
799
|
+
throw new Error('TAKEOVER_CANCELLED');
|
|
800
|
+
await resource.process.rpc.request('thread/resume', {
|
|
801
|
+
threadId: session.threadId,
|
|
802
|
+
cwd: session.workspacePath,
|
|
803
|
+
...this.permissionParams(session),
|
|
804
|
+
});
|
|
805
|
+
if (!allowed())
|
|
806
|
+
throw new Error('TAKEOVER_CANCELLED');
|
|
807
|
+
resource.threadId = session.threadId;
|
|
808
|
+
this.sessions.set(session.id, resource);
|
|
809
|
+
return { session, replacementCreated: false };
|
|
810
|
+
}
|
|
811
|
+
catch (error) {
|
|
812
|
+
resource.dispose();
|
|
813
|
+
throw error;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
771
816
|
async queueTurnInput(session, turnId, text, clientUserMessageId) {
|
|
772
817
|
this.scratch?.assertAvailable?.(session.id);
|
|
773
818
|
const resource = this.sessions.get(session.id);
|
|
@@ -1295,8 +1340,32 @@ export class CodexSessionRuntime {
|
|
|
1295
1340
|
}
|
|
1296
1341
|
}
|
|
1297
1342
|
async decodeHistory(process, threadId, resource) {
|
|
1298
|
-
|
|
1343
|
+
let response;
|
|
1344
|
+
try {
|
|
1345
|
+
response = await process.rpc.request('thread/read', { threadId, includeTurns: true });
|
|
1346
|
+
}
|
|
1347
|
+
catch (error) {
|
|
1348
|
+
// Untouched roots may not have a rollout yet. Never infer empty history
|
|
1349
|
+
// from a generic provider failure, a restored thread, or a thread's age.
|
|
1350
|
+
const unmaterialized = error instanceof CodexJsonRpcError &&
|
|
1351
|
+
((error.code === -32601 && error.message === 'list_turns is not supported yet') ||
|
|
1352
|
+
(error.code === -32600 &&
|
|
1353
|
+
error.message ===
|
|
1354
|
+
`thread ${threadId} is not materialized yet; includeTurns is unavailable before first user message`));
|
|
1355
|
+
const knownEmpty = () => resource?.historyKnownEmpty === true &&
|
|
1356
|
+
resource.active &&
|
|
1357
|
+
resource.threadId === threadId &&
|
|
1358
|
+
this.sessions.get(resource.sessionId) === resource;
|
|
1359
|
+
if (!unmaterialized || !knownEmpty())
|
|
1360
|
+
throw error;
|
|
1361
|
+
const metadata = await process.rpc.request('thread/read', { threadId, includeTurns: false });
|
|
1362
|
+
const thread = asRecord(asRecord(metadata)?.thread);
|
|
1363
|
+
if (!knownEmpty() || thread?.id !== threadId || asRecord(thread.status)?.type !== 'idle')
|
|
1364
|
+
throw error;
|
|
1365
|
+
return { turns: [], activeTurnId: null };
|
|
1366
|
+
}
|
|
1299
1367
|
if (resource) {
|
|
1368
|
+
resource.historyKnownEmpty = false;
|
|
1300
1369
|
for (const [childId, model] of decodeSpawnedAgentModels(response))
|
|
1301
1370
|
resource.spawnedAgentModels.set(childId, model);
|
|
1302
1371
|
}
|
|
@@ -1368,6 +1437,7 @@ export class CodexSessionRuntime {
|
|
|
1368
1437
|
}
|
|
1369
1438
|
const replacementThreadId = await this.startThread(resource.process, session, resource.skillsConfig, resource.modelConfig);
|
|
1370
1439
|
result = rebindMissingRollout(session, error, replacementThreadId, now);
|
|
1440
|
+
resource.historyKnownEmpty = true;
|
|
1371
1441
|
}
|
|
1372
1442
|
resource.threadId = result.session.threadId;
|
|
1373
1443
|
if (resource.retrieval)
|
|
@@ -1477,9 +1547,9 @@ export class CodexSessionRuntime {
|
|
|
1477
1547
|
? { permissions: 'workspace-git' }
|
|
1478
1548
|
: { sandbox: selection };
|
|
1479
1549
|
}
|
|
1480
|
-
createResource(session) {
|
|
1550
|
+
createResource(session, purpose = 'execution') {
|
|
1481
1551
|
this.scratch?.assertAvailable?.(session.id);
|
|
1482
|
-
const task = this.createResourceInner(session);
|
|
1552
|
+
const task = this.createResourceInner(session, purpose);
|
|
1483
1553
|
const pending = this.resourceCreations.get(session.id) ?? new Set();
|
|
1484
1554
|
pending.add(task);
|
|
1485
1555
|
this.resourceCreations.set(session.id, pending);
|
|
@@ -1492,19 +1562,21 @@ export class CodexSessionRuntime {
|
|
|
1492
1562
|
.catch(() => undefined);
|
|
1493
1563
|
return task;
|
|
1494
1564
|
}
|
|
1495
|
-
async createResourceInner(session) {
|
|
1496
|
-
this.
|
|
1565
|
+
async createResourceInner(session, purpose) {
|
|
1566
|
+
this.runtimeReleases?.invalidate(session.id);
|
|
1567
|
+
if (purpose === 'execution')
|
|
1568
|
+
this.scratch?.reserveRuntime?.(session);
|
|
1497
1569
|
this.confirmedWorkspaceReleases.delete(session.id);
|
|
1498
1570
|
const deadline = xerjDeadline();
|
|
1499
|
-
const lease = this.planStatusSource
|
|
1571
|
+
const lease = purpose === 'execution' && this.planStatusSource
|
|
1500
1572
|
? await this.planStatusSource.open({ id: session.id, workspacePath: session.workspacePath }, (update) => this.onPlanStatus?.(session.id, update))
|
|
1501
1573
|
: undefined;
|
|
1502
1574
|
let borrowed;
|
|
1503
1575
|
let launched = false;
|
|
1504
1576
|
try {
|
|
1505
1577
|
const token = randomUUID();
|
|
1506
|
-
const skillsConfig = await this.resolveSkills?.(session);
|
|
1507
|
-
const modelConfig = (await this.resolveModelConfig?.(session)) ?? {};
|
|
1578
|
+
const skillsConfig = purpose === 'execution' ? await this.resolveSkills?.(session) : undefined;
|
|
1579
|
+
const modelConfig = purpose === 'execution' ? ((await this.resolveModelConfig?.(session)) ?? {}) : {};
|
|
1508
1580
|
const scratch = await this.scratch?.allocate(session);
|
|
1509
1581
|
const scratchLease = (borrowed = await this.scratch?.borrow?.(session));
|
|
1510
1582
|
this.scratch?.assertAvailable?.(session.id);
|
|
@@ -1556,6 +1628,9 @@ export class CodexSessionRuntime {
|
|
|
1556
1628
|
const notificationUnsubscribe = process.rpc.onNotification((notification) => {
|
|
1557
1629
|
if (!resource.active)
|
|
1558
1630
|
return;
|
|
1631
|
+
if (notification.method === 'turn/started' &&
|
|
1632
|
+
asRecord(notification.params)?.threadId === resource.threadId)
|
|
1633
|
+
resource.historyKnownEmpty = false;
|
|
1559
1634
|
this.retrieval?.observe?.(session.id, notification);
|
|
1560
1635
|
// Internal evidence may only be constructed by this resource's tracker.
|
|
1561
1636
|
if (notification.method === 'gestalt/mcpStartup')
|
|
@@ -28,6 +28,7 @@ function migrateWorkspaceGitDefaults(database) {
|
|
|
28
28
|
}
|
|
29
29
|
export function migrate(database) {
|
|
30
30
|
database.exec(schema);
|
|
31
|
+
database.exec('CREATE TABLE IF NOT EXISTS session_runtime_releases (session_id TEXT PRIMARY KEY REFERENCES relay_sessions(id) ON DELETE CASCADE, workspace_path TEXT NOT NULL, thread_id TEXT NOT NULL)');
|
|
31
32
|
database.exec(`CREATE TABLE IF NOT EXISTS org_plan_launches (
|
|
32
33
|
launch_id TEXT PRIMARY KEY,
|
|
33
34
|
canonical_path TEXT UNIQUE,
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2026 Dyne.org foundation
|
|
3
|
+
* Designed by Denis Roio <jaromil@dyne.org>
|
|
4
|
+
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
|
+
*/
|
|
6
|
+
export class SqliteRuntimeReleaseStore {
|
|
7
|
+
db;
|
|
8
|
+
constructor(db) {
|
|
9
|
+
this.db = db;
|
|
10
|
+
}
|
|
11
|
+
confirmed(session) {
|
|
12
|
+
return Boolean(session.threadId &&
|
|
13
|
+
this.db
|
|
14
|
+
.prepare('SELECT 1 FROM session_runtime_releases WHERE session_id=? AND workspace_path=? AND thread_id=?')
|
|
15
|
+
.get(session.id, session.workspacePath, session.threadId));
|
|
16
|
+
}
|
|
17
|
+
confirm(session) {
|
|
18
|
+
if (!session.threadId)
|
|
19
|
+
return;
|
|
20
|
+
this.db
|
|
21
|
+
.prepare('INSERT INTO session_runtime_releases (session_id,workspace_path,thread_id) VALUES (?,?,?) ON CONFLICT(session_id) DO UPDATE SET workspace_path=excluded.workspace_path,thread_id=excluded.thread_id')
|
|
22
|
+
.run(session.id, session.workspacePath, session.threadId);
|
|
23
|
+
}
|
|
24
|
+
invalidate(sessionId) {
|
|
25
|
+
this.db.prepare('DELETE FROM session_runtime_releases WHERE session_id=?').run(sessionId);
|
|
26
|
+
}
|
|
27
|
+
}
|