gestalt-mobile 0.71.3 → 0.71.4

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.
@@ -2567,7 +2567,7 @@ describe('production composition', () => {
2567
2567
  expect(accepted.handles.flatMap((handle) => handle.calls)).not.toContain('turn/start');
2568
2568
  await accepted.app.close();
2569
2569
  });
2570
- it('lets a user-directed new session take over the unfinished plan without an approval or an unreachable old-root executor', async () => {
2570
+ it.each(['Close', 'Forget'])('lets a user-directed new session take over after %s without an approval or an unreachable old-root executor', async (operation) => {
2571
2571
  const handles = [];
2572
2572
  let previousRoot = '';
2573
2573
  const launch = liveAppServer(handles);
@@ -2609,6 +2609,14 @@ describe('production composition', () => {
2609
2609
  const old = store.find(fixture.sessionId);
2610
2610
  store.save({
2611
2611
  ...old,
2612
+ checkpoints: {
2613
+ protocolVersion: 1,
2614
+ planIdentity: old.planIdentity,
2615
+ acceptedKeys: ['retained-boundary'],
2616
+ reportedL1Ids: [],
2617
+ pendingTurnId: null,
2618
+ terminalReviewAccepted: false,
2619
+ },
2612
2620
  executor: {
2613
2621
  canonicalPosition: 'L1',
2614
2622
  canonicalTaskName: 'l1',
@@ -2622,10 +2630,19 @@ describe('production composition', () => {
2622
2630
  continuationCount: 2,
2623
2631
  },
2624
2632
  });
2625
- await fixture.app.inject({
2633
+ const closed = await fixture.app.inject({
2626
2634
  method: 'POST',
2627
2635
  url: `/api/sessions/${fixture.sessionId}/release`,
2628
2636
  });
2637
+ expect(closed.statusCode).toBe(202);
2638
+ if (operation === 'Forget') {
2639
+ const forgotten = await fixture.app.inject({
2640
+ method: 'DELETE',
2641
+ url: `/api/sessions/${fixture.sessionId}`,
2642
+ });
2643
+ expect(forgotten.statusCode).toBe(204);
2644
+ expect(store.find(fixture.sessionId)).toBeNull();
2645
+ }
2629
2646
  const incoming = await createComposedSession(fixture.app);
2630
2647
  await installAutopilotPlan(fixture.app, incoming, fixture.workspacePath, 'autopilot');
2631
2648
  const started = await fixture.app.inject({
@@ -2660,6 +2677,7 @@ describe('production composition', () => {
2660
2677
  expect(store.find(incoming)).toMatchObject({
2661
2678
  requestedEnabled: true,
2662
2679
  executor: { replacement: { reason: 'userDirectedTakeover', taskName: 'l1_g2' } },
2680
+ checkpoints: { acceptedKeys: ['retained-boundary'] },
2663
2681
  });
2664
2682
  expect(db
2665
2683
  .prepare("SELECT count(*) AS n FROM pending_interactions WHERE kind='orgPlanAttention'")
@@ -2716,7 +2734,8 @@ describe('production composition', () => {
2716
2734
  taskPath: '/root/l1_g2',
2717
2735
  continuationGeneration: 2,
2718
2736
  });
2719
- expect(store.find(fixture.sessionId).requestedEnabled).toBe(false);
2737
+ if (operation === 'Close')
2738
+ expect(store.find(fixture.sessionId).requestedEnabled).toBe(false);
2720
2739
  });
2721
2740
  it.each(['checkpoint', 'wait expiry'])('continues the durable root after %s when startup observed a provisional disconnected thread', async (boundary) => {
2722
2741
  const timers = [];
@@ -81,19 +81,21 @@ export class SupervisionTakeover {
81
81
  p.session.provider === 'codex' &&
82
82
  p.session.workspacePath === before.session.workspacePath &&
83
83
  p.state.planIdentity === before.identity);
84
- if (!previous.length)
85
- return { accepted: false, code: 'PREVIOUS_SUPERVISOR_NOT_FOUND' };
86
- if (existing &&
87
- existing.sessionId !== sessionId &&
88
- !previous.some((p) => p.session.id === existing.sessionId))
89
- return { accepted: false, code: 'PREVIOUS_SUPERVISOR_NOT_FOUND' };
84
+ const recovery = this.deps.store.recovery(before.identity, before.session.workspacePath, [
85
+ sessionId,
86
+ ...previous.map((p) => p.session.id),
87
+ ]);
88
+ if (!recovery.verified)
89
+ return { accepted: false, code: 'PREVIOUS_WRITER_STATUS_UNKNOWN' };
90
90
  const fingerprint = planFingerprint(before.plan);
91
91
  const generation = before.state?.generation ?? 0;
92
92
  boundary('ownershipReservation');
93
- const claim = this.deps.store.claim(before.identity, sessionId);
93
+ const claim = this.deps.store.claim(before.identity, sessionId, recovery.epoch);
94
94
  if (!claim)
95
95
  return { accepted: false, code: 'TAKEOVER_IN_PROGRESS' };
96
- if (!this.deps.store.retire(claim, previous.map((p) => p.session.id)))
96
+ if (!this.deps.store.retire(claim, [
97
+ ...new Set([...previous.map((p) => p.session.id), ...recovery.sessionIds]),
98
+ ]))
97
99
  return { accepted: false, code: 'TAKEOVER_CONTEXT_CHANGED' };
98
100
  // Fence every predecessor before the first asynchronous process operation.
99
101
  boundary('fencePreviousSupervisor');
@@ -109,6 +111,9 @@ export class SupervisionTakeover {
109
111
  if (!this.deps.store.markStopped(claim, p.session.id))
110
112
  return { accepted: false, code: 'TAKEOVER_CONTEXT_CHANGED' };
111
113
  }
114
+ for (const id of recovery.sessionIds)
115
+ if (!this.deps.store.markStopped(claim, id))
116
+ return { accepted: false, code: 'TAKEOVER_CONTEXT_CHANGED' };
112
117
  boundary('revalidateContext');
113
118
  const current = this.deps.context(sessionId);
114
119
  if (!current ||
@@ -122,8 +127,9 @@ export class SupervisionTakeover {
122
127
  !current.admission)
123
128
  return { accepted: false, code: 'TAKEOVER_CONTEXT_CHANGED' };
124
129
  const canonicalTaskName = `l${index + 1}`;
125
- const executors = previous
126
- .map((p) => p.state.executor)
130
+ const recoveredStates = [...previous.map((p) => p.state), ...recovery.states];
131
+ const executors = recoveredStates
132
+ .map((state) => state.executor)
127
133
  .filter((e) => e?.canonicalTaskName === canonicalTaskName);
128
134
  const source = executors.sort((a, b) => (b.replacement?.generation ?? b.continuationGeneration) -
129
135
  (a.replacement?.generation ?? a.continuationGeneration))[0];
@@ -133,9 +139,9 @@ export class SupervisionTakeover {
133
139
  : 1;
134
140
  const identity = executorIdentity(canonicalTaskName, nextGeneration);
135
141
  const history = current.state?.checkpoints ??
136
- [...previous]
137
- .filter((p) => p.state.checkpoints?.planIdentity === before.identity)
138
- .sort((a, b) => b.state.updatedAt.localeCompare(a.state.updatedAt))[0]?.state.checkpoints;
142
+ recoveredStates
143
+ .filter((state) => state.checkpoints?.planIdentity === before.identity)
144
+ .sort((a, b) => b.updatedAt.localeCompare(a.updatedAt))[0]?.checkpoints;
139
145
  const now = this.deps.now();
140
146
  const state = {
141
147
  ...(current.state ?? disabledAutopilot(sessionId, now)),
@@ -183,7 +189,9 @@ export class SupervisionTakeover {
183
189
  : undefined,
184
190
  updatedAt: now,
185
191
  };
186
- const previousSessionIds = previous.map((p) => p.session.id);
192
+ const previousSessionIds = [
193
+ ...new Set([...previous.map((p) => p.session.id), ...recovery.sessionIds]),
194
+ ];
187
195
  boundary('commitOwnership');
188
196
  if (!this.deps.store.complete(claim, state, [
189
197
  {
@@ -84,6 +84,8 @@ export function migrate(database) {
84
84
  ON CONFLICT(workspace_path) DO NOTHING; END;`);
85
85
  database.exec('CREATE TABLE IF NOT EXISTS org_plan_retired_supervisors (plan_identity TEXT NOT NULL, session_id TEXT NOT NULL, stopped INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(plan_identity, session_id))');
86
86
  database.exec('CREATE TABLE IF NOT EXISTS org_plan_takeovers (plan_identity TEXT PRIMARY KEY, session_id TEXT NOT NULL, epoch INTEGER NOT NULL, phase TEXT NOT NULL)');
87
+ // Recovery metadata outlives Forget; it contains no chat, prompts or model output.
88
+ database.exec('CREATE TABLE IF NOT EXISTS org_plan_supervision_recovery (plan_identity TEXT NOT NULL, session_id TEXT NOT NULL, workspace_path TEXT NOT NULL, state_json TEXT NOT NULL, PRIMARY KEY(plan_identity, session_id))');
87
89
  database.exec('CREATE TABLE IF NOT EXISTS tool_efficiency (session_id TEXT PRIMARY KEY REFERENCES relay_sessions(id) ON DELETE CASCADE, state_json TEXT NOT NULL)');
88
90
  database.exec(`CREATE TABLE IF NOT EXISTS chat_transport_diagnostics (
89
91
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -3,6 +3,7 @@
3
3
  * Designed by Denis Roio <jaromil@dyne.org>
4
4
  * SPDX-License-Identifier: AGPL-3.0-or-later
5
5
  */
6
+ import { parsePersistedSupervisedLifecycle } from '../../features/autopilot/domain/supervised-lifecycle.js';
6
7
  /** A durable plan-scoped reservation also fences a superseded supervisor after relay restart. */
7
8
  export class SqliteSupervisionTakeoverStore {
8
9
  db;
@@ -21,9 +22,105 @@ export class SqliteSupervisionTakeoverStore {
21
22
  }
22
23
  /** Keep the epoch tombstone and retired-writer fences after confirmed runtime release. */
23
24
  releaseSession(sessionId) {
24
- this.db
25
- .prepare("UPDATE org_plan_takeovers SET phase='released',epoch=epoch+1 WHERE session_id=? AND phase!='released'")
26
- .run(sessionId);
25
+ this.db.exec('BEGIN IMMEDIATE');
26
+ try {
27
+ const state = this.autopilot.find(sessionId);
28
+ const session = this.db
29
+ .prepare('SELECT workspace_path FROM relay_sessions WHERE id=?')
30
+ .get(sessionId);
31
+ if (state?.planIdentity && session) {
32
+ const recovery = {
33
+ sessionId,
34
+ updatedAt: state.updatedAt,
35
+ executor: state.executor,
36
+ checkpoints: state.checkpoints,
37
+ };
38
+ this.db
39
+ .prepare('INSERT INTO org_plan_supervision_recovery (plan_identity,session_id,workspace_path,state_json) VALUES (?,?,?,?) ON CONFLICT(plan_identity,session_id) DO UPDATE SET workspace_path=excluded.workspace_path,state_json=excluded.state_json')
40
+ .run(state.planIdentity, sessionId, session.workspace_path, JSON.stringify(recovery));
41
+ }
42
+ this.db
43
+ .prepare("UPDATE org_plan_takeovers SET phase='released',epoch=epoch+1 WHERE session_id=? AND phase!='released'")
44
+ .run(sessionId);
45
+ this.db
46
+ .prepare('UPDATE org_plan_retired_supervisors SET stopped=1 WHERE session_id=?')
47
+ .run(sessionId);
48
+ this.db.exec('COMMIT');
49
+ }
50
+ catch (error) {
51
+ this.db.exec('ROLLBACK');
52
+ throw error;
53
+ }
54
+ }
55
+ /** Missing roster rows are recoverable only with durable stop evidence. */
56
+ recovery(planIdentity, workspacePath, knownSessionIds) {
57
+ const retained = this.db
58
+ .prepare('SELECT session_id,epoch,phase FROM org_plan_takeovers WHERE plan_identity=?')
59
+ .get(planIdentity);
60
+ const retired = this.db
61
+ .prepare('SELECT session_id,stopped FROM org_plan_retired_supervisors WHERE plan_identity=?')
62
+ .all(planIdentity);
63
+ const snapshots = this.db
64
+ .prepare('SELECT session_id,workspace_path,state_json FROM org_plan_supervision_recovery WHERE plan_identity=?')
65
+ .all(planIdentity);
66
+ const ids = new Set([
67
+ ...(retained ? [retained.session_id] : []),
68
+ ...retired.map((row) => row.session_id),
69
+ ...snapshots.map((row) => row.session_id),
70
+ ]);
71
+ const sessionIds = [];
72
+ const states = [];
73
+ let verified = knownSessionIds.length > 1 || [...ids].some((id) => id !== knownSessionIds[0]);
74
+ for (const id of ids) {
75
+ if (knownSessionIds.includes(id))
76
+ continue;
77
+ const session = this.db
78
+ .prepare('SELECT workspace_path,thread_id,state,desired_state FROM relay_sessions WHERE id=?')
79
+ .get(id);
80
+ const snapshot = snapshots.find((row) => row.session_id === id);
81
+ const forgotten = Boolean(this.db
82
+ .prepare("SELECT 1 FROM session_forget_operations WHERE session_id=? AND phase='completed'")
83
+ .get(id));
84
+ const stopped = (retained?.session_id === id && retained.phase === 'released') ||
85
+ retired.some((row) => row.session_id === id && row.stopped === 1) ||
86
+ Boolean(snapshot) ||
87
+ forgotten;
88
+ if (!stopped ||
89
+ (snapshot && snapshot.workspace_path !== workspacePath) ||
90
+ (session &&
91
+ (session.workspace_path !== workspacePath ||
92
+ session.desired_state !== 'stopped' ||
93
+ !['stopped', 'released'].includes(session.state) ||
94
+ !this.db
95
+ .prepare('SELECT 1 FROM session_runtime_releases WHERE session_id=? AND workspace_path=? AND thread_id=?')
96
+ .get(id, workspacePath, session.thread_id)))) {
97
+ verified = false;
98
+ continue;
99
+ }
100
+ sessionIds.push(id);
101
+ if (snapshot) {
102
+ let value;
103
+ try {
104
+ value = JSON.parse(snapshot.state_json);
105
+ }
106
+ catch {
107
+ verified = false;
108
+ continue;
109
+ }
110
+ const lifecycle = parsePersistedSupervisedLifecycle(value);
111
+ if (!lifecycle || value.sessionId !== id || typeof value.updatedAt !== 'string') {
112
+ verified = false;
113
+ continue;
114
+ }
115
+ states.push({
116
+ sessionId: id,
117
+ updatedAt: value.updatedAt,
118
+ executor: lifecycle.executor,
119
+ checkpoints: lifecycle.checkpoints,
120
+ });
121
+ }
122
+ }
123
+ return { epoch: retained?.epoch ?? null, verified, sessionIds, states };
27
124
  }
28
125
  retired(planIdentity, sessionId) {
29
126
  return Boolean(this.db
@@ -64,10 +161,17 @@ export class SqliteSupervisionTakeoverStore {
64
161
  throw error;
65
162
  }
66
163
  }
67
- claim(planIdentity, sessionId) {
164
+ claim(planIdentity, sessionId, expectedEpoch) {
68
165
  this.db.exec('BEGIN IMMEDIATE');
69
166
  try {
70
167
  const previous = this.owner(planIdentity);
168
+ const retained = this.db
169
+ .prepare('SELECT epoch FROM org_plan_takeovers WHERE plan_identity=?')
170
+ .get(planIdentity);
171
+ if (expectedEpoch !== undefined && (retained?.epoch ?? null) !== expectedEpoch) {
172
+ this.db.exec('ROLLBACK');
173
+ return null;
174
+ }
71
175
  if (previous?.phase === 'stopping' && previous.sessionId !== sessionId) {
72
176
  const owner = this.db
73
177
  .prepare('SELECT state,active_turn_id FROM relay_sessions WHERE id=?')
@@ -77,9 +181,6 @@ export class SqliteSupervisionTakeoverStore {
77
181
  return null;
78
182
  }
79
183
  }
80
- const retained = this.db
81
- .prepare('SELECT epoch FROM org_plan_takeovers WHERE plan_identity=?')
82
- .get(planIdentity);
83
184
  const epoch = previous?.sessionId === sessionId ? previous.epoch : (retained?.epoch ?? 0) + 1;
84
185
  const phase = previous?.sessionId === sessionId ? previous.phase : 'stopping';
85
186
  this.db
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gestalt-mobile",
3
- "version": "0.71.3",
3
+ "version": "0.71.4",
4
4
  "description": "Mobile-first web relay for durable Codex development sessions",
5
5
  "keywords": [
6
6
  "codex",