gestalt-mobile 0.44.0 → 0.44.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/README.md +22 -6
- package/dist/client/assets/{index-7g7-CxvF.js → index-6WgZCe9g.js} +1 -1
- package/dist/client/index.html +1 -1
- package/dist/server/server/composition.contracts.js +80 -3
- package/dist/server/server/composition.js +30 -6
- package/dist/server/server/features/agent-activity/registry.js +12 -2
- package/dist/server/server/features/autopilot/application/service.js +184 -61
- package/dist/server/server/features/autopilot/domain/executor-commands.js +28 -0
- package/dist/server/server/features/autopilot/domain/persisted-state-error.js +14 -0
- package/dist/server/server/features/autopilot/domain/plan-fingerprint.js +24 -0
- package/dist/server/server/features/autopilot/domain/supervised-lifecycle.js +33 -7
- package/dist/server/server/features/org-plan-checkpoint/application/validate.js +45 -16
- package/dist/server/server/platform/codex/session-runtime.js +39 -8
- package/dist/server/server/platform/persistence/migrate.js +1 -0
- package/dist/server/server/platform/persistence/sqlite-autopilot-store.js +73 -14
- package/dist/server/shared/contracts/org-plan-health.js +21 -0
- package/package.json +1 -1
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
* Designed by Denis Roio <jaromil@dyne.org>
|
|
4
4
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
5
5
|
*/
|
|
6
|
+
import { requirePlanFingerprint } from '../domain/plan-fingerprint.js';
|
|
7
|
+
import { AutopilotStateError } from '../domain/persisted-state-error.js';
|
|
8
|
+
import { canTransitionExecutorCommand, retainExecutorCommands, } from '../domain/executor-commands.js';
|
|
6
9
|
import { deriveSessionStatus } from '../../sessions/session-status.js';
|
|
7
10
|
import { createHash } from 'node:crypto';
|
|
8
11
|
import { AUTOPILOT_WAIT_MAX_MS, AUTOPILOT_WAIT_MIN_MS, } from '../../../../shared/contracts/autopilot-wait-lease.js';
|
|
@@ -25,11 +28,33 @@ export class AutopilotCoordinator {
|
|
|
25
28
|
reconciling = new Set();
|
|
26
29
|
/** Serializes asynchronous watchdog and timer work per relay session. */
|
|
27
30
|
operations = new Map();
|
|
31
|
+
storeRecoveryAttempts = new Map();
|
|
28
32
|
constructor(deps) {
|
|
29
33
|
this.deps = deps;
|
|
30
34
|
}
|
|
31
35
|
snapshot(sessionId) {
|
|
32
|
-
|
|
36
|
+
let state;
|
|
37
|
+
try {
|
|
38
|
+
state = this.deps.store.find(sessionId) ?? disabledAutopilot(sessionId, this.deps.now());
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (!(error instanceof AutopilotStateError))
|
|
42
|
+
throw error;
|
|
43
|
+
const snapshot = autopilotSnapshot({
|
|
44
|
+
...disabledAutopilot(sessionId, this.deps.now()),
|
|
45
|
+
state: 'safetyPaused',
|
|
46
|
+
stopReason: 'reconcileFailed',
|
|
47
|
+
}, this.deps.policy.retryLimit);
|
|
48
|
+
return {
|
|
49
|
+
...snapshot,
|
|
50
|
+
health: {
|
|
51
|
+
...snapshot.health,
|
|
52
|
+
phase: 'degraded',
|
|
53
|
+
degradationReason: 'controllerUnavailable',
|
|
54
|
+
nextExpectedAction: 'Controller state is unreadable (AUTOPILOT_STATE_INVALID). Set Autopilot Off to preserve the damaged record and recover a disabled controller; review the Org Plan before enabling.',
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
33
58
|
const control = state.lastControlId
|
|
34
59
|
? this.deps.store.findControl(sessionId, state.lastControlId)
|
|
35
60
|
: null;
|
|
@@ -44,7 +69,15 @@ export class AutopilotCoordinator {
|
|
|
44
69
|
/** Rehydrates only actionable durable state; terminal rows intentionally create no work. */
|
|
45
70
|
restore(sessionId) {
|
|
46
71
|
this.flushOutbox(sessionId);
|
|
47
|
-
|
|
72
|
+
let state;
|
|
73
|
+
try {
|
|
74
|
+
state = this.deps.store.find(sessionId);
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (!(error instanceof AutopilotStateError))
|
|
78
|
+
throw error;
|
|
79
|
+
return; // snapshot exposes controllerUnavailable; only explicit Off repairs it.
|
|
80
|
+
}
|
|
48
81
|
const session = this.deps.session(sessionId);
|
|
49
82
|
if (!state ||
|
|
50
83
|
!session ||
|
|
@@ -122,6 +155,7 @@ export class AutopilotCoordinator {
|
|
|
122
155
|
this.activityEventKeys.delete(sessionId);
|
|
123
156
|
this.reconciling.delete(sessionId);
|
|
124
157
|
this.parkedSubscriptions.delete(sessionId);
|
|
158
|
+
this.storeRecoveryAttempts.delete(sessionId);
|
|
125
159
|
}
|
|
126
160
|
/** Accepts a session-owned structured probe response; no transcript text is inspected. */
|
|
127
161
|
reportProbe(sessionId, report) {
|
|
@@ -391,6 +425,7 @@ export class AutopilotCoordinator {
|
|
|
391
425
|
return { accepted: true, identity };
|
|
392
426
|
}
|
|
393
427
|
disable(sessionId) {
|
|
428
|
+
this.deps.store.recoverInvalid?.(sessionId, this.deps.now());
|
|
394
429
|
const now = this.deps.now();
|
|
395
430
|
const prior = this.deps.store.find(sessionId) ?? disabledAutopilot(sessionId, now);
|
|
396
431
|
// Retain the current plan identity even when Autopilot has not previously
|
|
@@ -605,8 +640,10 @@ export class AutopilotCoordinator {
|
|
|
605
640
|
return this.snapshot(sessionId);
|
|
606
641
|
}
|
|
607
642
|
/** Returns whether an incomplete supervised plan may be treated as terminal by the relay. */
|
|
608
|
-
turnCompleted(sessionId) {
|
|
643
|
+
turnCompleted(sessionId, turnId) {
|
|
609
644
|
const state = this.deps.store.find(sessionId);
|
|
645
|
+
if (turnId && state?.checkpoints?.pendingTurnId && state.checkpoints.pendingTurnId !== turnId)
|
|
646
|
+
return false;
|
|
610
647
|
const plan = this.deps.plan(sessionId)?.plan;
|
|
611
648
|
const explicitlyStopped = state?.stopReason === 'manualDisabled' ||
|
|
612
649
|
state?.stopReason === 'planRemoved' ||
|
|
@@ -632,6 +669,7 @@ export class AutopilotCoordinator {
|
|
|
632
669
|
checkpoints: {
|
|
633
670
|
...state.checkpoints,
|
|
634
671
|
pendingTurnId: null,
|
|
672
|
+
pendingTarget: undefined,
|
|
635
673
|
pendingKind: null,
|
|
636
674
|
checkpointHandoffFailed: false,
|
|
637
675
|
},
|
|
@@ -687,7 +725,6 @@ export class AutopilotCoordinator {
|
|
|
687
725
|
return 'failed';
|
|
688
726
|
const reportedL1Ids = previous?.reportedL1Ids ?? [];
|
|
689
727
|
const reportedL2Ids = previous?.reportedL2Ids ?? [];
|
|
690
|
-
const canonicalPosition = checkpoint.kind === 'terminalReviewAccepted' ? 'terminal' : checkpoint.position;
|
|
691
728
|
const target = checkpoint.kind === 'l2Completed'
|
|
692
729
|
? checkpointTarget('l2', checkpoint.l1Id, checkpoint.l2Id)
|
|
693
730
|
: checkpoint.kind === 'l1Accepted'
|
|
@@ -696,7 +733,7 @@ export class AutopilotCoordinator {
|
|
|
696
733
|
const completionEpoch = previous?.completionEpochs?.find((entry) => entry.target === target);
|
|
697
734
|
const epoch = completionEpoch?.epoch ?? 0;
|
|
698
735
|
const key = createHash('sha256')
|
|
699
|
-
.update(JSON.stringify([retained.identity,
|
|
736
|
+
.update(JSON.stringify([retained.identity, target, epoch]))
|
|
700
737
|
.digest('hex');
|
|
701
738
|
const acceptedKeys = previous?.acceptedKeys ?? [];
|
|
702
739
|
if (acceptedKeys.includes(key))
|
|
@@ -706,6 +743,10 @@ export class AutopilotCoordinator {
|
|
|
706
743
|
return 'alreadyRecorded';
|
|
707
744
|
if (completionEpoch?.completed && !completionEpoch.reopened)
|
|
708
745
|
return 'alreadyRecorded';
|
|
746
|
+
// One turn owns one pending boundary. Retried acknowledgements above are
|
|
747
|
+
// idempotent; a distinct boundary must not overwrite its handoff slot.
|
|
748
|
+
if (previous?.pendingTurnId)
|
|
749
|
+
return 'failed';
|
|
709
750
|
const completionEpochs = [
|
|
710
751
|
...(previous?.completionEpochs ?? []).filter((entry) => entry.target !== target),
|
|
711
752
|
{ target, epoch, reopened: false, completed: true },
|
|
@@ -721,10 +762,16 @@ export class AutopilotCoordinator {
|
|
|
721
762
|
: reportedL1Ids,
|
|
722
763
|
acceptedKeys: boundedUnique([...acceptedKeys, key], 768),
|
|
723
764
|
pendingTurnId: turnId,
|
|
765
|
+
pendingTarget: target,
|
|
724
766
|
activeHandoffId: handoffId,
|
|
725
767
|
pendingKind: checkpoint.kind,
|
|
726
768
|
checkpointHandoffFailed: false,
|
|
727
769
|
terminalReviewAccepted: checkpoint.kind === 'terminalReviewAccepted' || previous?.terminalReviewAccepted === true,
|
|
770
|
+
...(checkpoint.kind === 'terminalReviewAccepted'
|
|
771
|
+
? { terminalReviewFingerprint: fingerprint(retained.plan) }
|
|
772
|
+
: previous?.terminalReviewFingerprint
|
|
773
|
+
? { terminalReviewFingerprint: previous.terminalReviewFingerprint }
|
|
774
|
+
: {}),
|
|
728
775
|
};
|
|
729
776
|
const checkpointed = {
|
|
730
777
|
...prior,
|
|
@@ -808,14 +855,17 @@ export class AutopilotCoordinator {
|
|
|
808
855
|
/** Handles only plan lifecycle safety; ordinary plan mutations are ignored. */
|
|
809
856
|
planStatusChanged(sessionId) {
|
|
810
857
|
const prior = this.deps.store.find(sessionId);
|
|
811
|
-
if (!prior
|
|
858
|
+
if (!prior)
|
|
812
859
|
return;
|
|
813
860
|
const plan = this.deps.plan(sessionId);
|
|
814
861
|
if (!plan) {
|
|
815
|
-
|
|
862
|
+
if (prior.requestedEnabled)
|
|
863
|
+
this.cancel(sessionId, 'planRemoved');
|
|
816
864
|
return;
|
|
817
865
|
}
|
|
818
866
|
if (prior.planIdentity && prior.planIdentity !== plan.identity) {
|
|
867
|
+
if (!prior.requestedEnabled)
|
|
868
|
+
return;
|
|
819
869
|
this.supersedeExecutorCommands(sessionId, plan.identity, plan.plan);
|
|
820
870
|
this.cancel(sessionId, 'planReplaced');
|
|
821
871
|
return;
|
|
@@ -825,12 +875,27 @@ export class AutopilotCoordinator {
|
|
|
825
875
|
// this update cannot restore commands from the stale pre-supersession row.
|
|
826
876
|
const current = this.deps.store.find(sessionId) ?? prior;
|
|
827
877
|
const checkpointEpochs = this.reopenCheckpointEpochs(current, plan.plan);
|
|
828
|
-
|
|
878
|
+
const staleTerminalReview = current.checkpoints?.terminalReviewAccepted &&
|
|
879
|
+
(current.checkpoints.terminalReviewFingerprint ?? current.planFingerprint) !==
|
|
880
|
+
fingerprint(plan.plan);
|
|
881
|
+
if (checkpointEpochs !== current.checkpoints?.completionEpochs || staleTerminalReview)
|
|
829
882
|
this.persist({
|
|
830
883
|
...current,
|
|
831
|
-
checkpoints: {
|
|
884
|
+
checkpoints: {
|
|
885
|
+
...current.checkpoints,
|
|
886
|
+
completionEpochs: staleTerminalReview
|
|
887
|
+
? checkpointEpochs?.map((entry) => entry.target === checkpointTarget('terminal') && !entry.reopened
|
|
888
|
+
? { ...entry, epoch: entry.epoch + 1, reopened: true, completed: false }
|
|
889
|
+
: entry)
|
|
890
|
+
: checkpointEpochs,
|
|
891
|
+
...(staleTerminalReview
|
|
892
|
+
? { terminalReviewAccepted: false, terminalReviewFingerprint: undefined }
|
|
893
|
+
: {}),
|
|
894
|
+
},
|
|
832
895
|
updatedAt: this.deps.now(),
|
|
833
896
|
});
|
|
897
|
+
if (!prior.requestedEnabled)
|
|
898
|
+
return;
|
|
834
899
|
if (this.semanticEvent(sessionId, 'planChanged') ||
|
|
835
900
|
this.semanticEvent(sessionId, 'reviewChanged'))
|
|
836
901
|
return;
|
|
@@ -1076,6 +1141,7 @@ export class AutopilotCoordinator {
|
|
|
1076
1141
|
...(control ? { control } : {}),
|
|
1077
1142
|
events: [...snapshotEvents, ...events],
|
|
1078
1143
|
});
|
|
1144
|
+
this.storeRecoveryAttempts.delete(next.sessionId);
|
|
1079
1145
|
if (snapshotEvents.length)
|
|
1080
1146
|
this.publishedSnapshots.set(next.sessionId, this.semanticSnapshot(next, control));
|
|
1081
1147
|
this.flushOutbox(next.sessionId);
|
|
@@ -1620,27 +1686,37 @@ export class AutopilotCoordinator {
|
|
|
1620
1686
|
updatedAt: now,
|
|
1621
1687
|
};
|
|
1622
1688
|
}
|
|
1623
|
-
|
|
1689
|
+
/** Common ownership fence for process actions and executor continuations. */
|
|
1690
|
+
executorCommandCurrent(sessionId, command, physicalGeneration) {
|
|
1624
1691
|
const state = this.deps.store.find(sessionId);
|
|
1625
1692
|
const retained = this.deps.plan(sessionId);
|
|
1626
1693
|
const executor = state?.executor;
|
|
1627
1694
|
const child = this.deps
|
|
1628
1695
|
.activity(sessionId)
|
|
1629
1696
|
?.subagents.find((candidate) => (candidate.threadId ?? candidate.id) === command.threadId);
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
executor.
|
|
1639
|
-
executor.
|
|
1640
|
-
executor.
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1697
|
+
return state?.requestedEnabled &&
|
|
1698
|
+
retained &&
|
|
1699
|
+
executor &&
|
|
1700
|
+
executor.commands?.some((entry) => entry.commandId === command.commandId &&
|
|
1701
|
+
(['scheduled', 'issued'].includes(entry.status) ||
|
|
1702
|
+
(command.processAction && entry.status === 'failed'))) &&
|
|
1703
|
+
retained.identity === command.planIdentity &&
|
|
1704
|
+
fingerprint(retained.plan) === command.planFingerprint &&
|
|
1705
|
+
executor.canonicalPosition === command.canonicalPosition &&
|
|
1706
|
+
executor.canonicalTaskName === command.canonicalTaskName &&
|
|
1707
|
+
executor.taskPath === command.taskPath &&
|
|
1708
|
+
executor.threadId === command.threadId &&
|
|
1709
|
+
(executor.assignment?.generation ?? executor.continuationGeneration) === physicalGeneration &&
|
|
1710
|
+
child?.canonicalPosition === command.canonicalPosition &&
|
|
1711
|
+
child.canonicalTaskName === command.canonicalTaskName &&
|
|
1712
|
+
(child.continuationGeneration ?? 1) === physicalGeneration
|
|
1713
|
+
? state
|
|
1714
|
+
: null;
|
|
1715
|
+
}
|
|
1716
|
+
processActionCurrent(sessionId, command) {
|
|
1717
|
+
const state = this.executorCommandCurrent(sessionId, command, command.generation);
|
|
1718
|
+
const executor = state?.executor;
|
|
1719
|
+
if (!state || !executor || !command.processAction)
|
|
1644
1720
|
return null;
|
|
1645
1721
|
const process = executor.ownedProcesses.find((candidate) => executorProcessKey(candidate) === command.processAction.processKey);
|
|
1646
1722
|
return process ? { state, executor, process } : null;
|
|
@@ -1723,8 +1799,11 @@ export class AutopilotCoordinator {
|
|
|
1723
1799
|
const state = this.deps.store.find(sessionId);
|
|
1724
1800
|
const executor = state?.executor;
|
|
1725
1801
|
const command = executor?.commands?.find((candidate) => candidate.commandId === commandId);
|
|
1726
|
-
if (!state ||
|
|
1727
|
-
|
|
1802
|
+
if (!state ||
|
|
1803
|
+
!executor ||
|
|
1804
|
+
!command ||
|
|
1805
|
+
!canTransitionExecutorCommand(command.status, status, Boolean(command.processAction)))
|
|
1806
|
+
return undefined;
|
|
1728
1807
|
const next = { ...command, status, updatedAt: this.deps.now() };
|
|
1729
1808
|
this.persist({
|
|
1730
1809
|
...state,
|
|
@@ -1744,7 +1823,11 @@ export class AutopilotCoordinator {
|
|
|
1744
1823
|
const existing = executor.commands?.find((candidate) => candidate.commandId === command.commandId);
|
|
1745
1824
|
if (existing)
|
|
1746
1825
|
return existing;
|
|
1747
|
-
const commands =
|
|
1826
|
+
const commands = retainExecutorCommands(state, command);
|
|
1827
|
+
if (!commands) {
|
|
1828
|
+
this.deps.diagnostic?.(sessionId, 'executorCommandCapacityExhausted');
|
|
1829
|
+
return undefined;
|
|
1830
|
+
}
|
|
1748
1831
|
this.persist({
|
|
1749
1832
|
...state,
|
|
1750
1833
|
executor: { ...executor, commands },
|
|
@@ -1978,6 +2061,12 @@ export class AutopilotCoordinator {
|
|
|
1978
2061
|
armStoreRecovery(sessionId) {
|
|
1979
2062
|
if (this.executorTimers.has(sessionId))
|
|
1980
2063
|
return;
|
|
2064
|
+
const attempts = this.storeRecoveryAttempts.get(sessionId) ?? 0;
|
|
2065
|
+
if (attempts >= this.deps.policy.retryLimit) {
|
|
2066
|
+
this.deps.diagnostic?.(sessionId, 'storeRecoveryExhausted');
|
|
2067
|
+
return;
|
|
2068
|
+
}
|
|
2069
|
+
this.storeRecoveryAttempts.set(sessionId, attempts + 1);
|
|
1981
2070
|
let delivered = false;
|
|
1982
2071
|
const cancel = this.deps.schedule(() => {
|
|
1983
2072
|
this.executorTimers.delete(sessionId);
|
|
@@ -1986,6 +2075,7 @@ export class AutopilotCoordinator {
|
|
|
1986
2075
|
delivered = true;
|
|
1987
2076
|
this.enqueue(sessionId, async () => {
|
|
1988
2077
|
const current = this.deps.store.find(sessionId);
|
|
2078
|
+
this.storeRecoveryAttempts.delete(sessionId);
|
|
1989
2079
|
if (!current?.requestedEnabled)
|
|
1990
2080
|
return;
|
|
1991
2081
|
this.evaluate(sessionId);
|
|
@@ -2042,10 +2132,16 @@ export class AutopilotCoordinator {
|
|
|
2042
2132
|
this.enqueue(sessionId, async () => {
|
|
2043
2133
|
const current = this.deps.store.find(sessionId);
|
|
2044
2134
|
const latestPlan = this.deps.plan(sessionId);
|
|
2135
|
+
const activity = this.deps.activity(sessionId);
|
|
2045
2136
|
const currentChild = this.deps
|
|
2046
2137
|
.activity(sessionId)
|
|
2047
2138
|
?.subagents.find((child) => (child.threadId ?? child.id) === fence.executorThreadId);
|
|
2048
|
-
const valid =
|
|
2139
|
+
const valid = this.executorCommandCurrent(sessionId, command, fence.executorGeneration) &&
|
|
2140
|
+
activity?.confidence === 'fresh' &&
|
|
2141
|
+
activity.root.state === 'idle' &&
|
|
2142
|
+
!activity.subagents.some((child) => child.canonicalPosition &&
|
|
2143
|
+
['working', 'awaitingAgent', 'awaitingHuman'].includes(child.state)) &&
|
|
2144
|
+
current?.requestedEnabled &&
|
|
2049
2145
|
!this.deps.pendingInteraction(sessionId) &&
|
|
2050
2146
|
current.generation === fence.sessionGeneration &&
|
|
2051
2147
|
current.state === fence.boundaryState &&
|
|
@@ -2063,7 +2159,12 @@ export class AutopilotCoordinator {
|
|
|
2063
2159
|
fingerprint(latestPlan.plan) !== fence.planFingerprint ||
|
|
2064
2160
|
currentChild?.canonicalPosition !== fence.canonicalPosition ||
|
|
2065
2161
|
(currentChild.continuationGeneration ?? 1) !== fence.executorGeneration;
|
|
2066
|
-
|
|
2162
|
+
if (current?.generation === fence.sessionGeneration) {
|
|
2163
|
+
if (ownershipChanged || !current.requestedEnabled)
|
|
2164
|
+
this.executorCommandTransition(sessionId, command.commandId, ownershipChanged ? 'superseded' : 'cancelled');
|
|
2165
|
+
else
|
|
2166
|
+
this.armExecutorRefresh(sessionId, this.deps.policy.executorContinuationMaxMs);
|
|
2167
|
+
}
|
|
2067
2168
|
this.audit(sessionId, 'autopilot.executor-continuation-stale', {
|
|
2068
2169
|
threadId,
|
|
2069
2170
|
generation,
|
|
@@ -2077,37 +2178,14 @@ export class AutopilotCoordinator {
|
|
|
2077
2178
|
return;
|
|
2078
2179
|
try {
|
|
2079
2180
|
await this.deps.executorController?.resume(sessionId, threadId, generation, trigger);
|
|
2080
|
-
this.executorCommandTransition(sessionId, command.commandId, 'accepted');
|
|
2081
|
-
this.audit(sessionId, 'autopilot.executor-resumed', {
|
|
2082
|
-
threadId,
|
|
2083
|
-
generation,
|
|
2084
|
-
trigger: trigger.kind,
|
|
2085
|
-
});
|
|
2086
|
-
const latest = this.deps.store.find(sessionId);
|
|
2087
|
-
if (latest?.requestedEnabled) {
|
|
2088
|
-
const progressKey = this.progressKey(sessionId);
|
|
2089
|
-
const supervision = recordAutomaticContinuation(latest.supervision ?? startSupervisionProtocol(progressKey), progressKey);
|
|
2090
|
-
this.persist({
|
|
2091
|
-
...latest,
|
|
2092
|
-
state: 'monitoring',
|
|
2093
|
-
supervision,
|
|
2094
|
-
consecutiveNoProgress: latest.consecutiveNoProgress + 1,
|
|
2095
|
-
...(latest.executor
|
|
2096
|
-
? {
|
|
2097
|
-
executor: {
|
|
2098
|
-
...latest.executor,
|
|
2099
|
-
outcome: 'partial',
|
|
2100
|
-
continuationCount: latest.executor.continuationCount + 1,
|
|
2101
|
-
lastActivityAt: this.deps.now(),
|
|
2102
|
-
},
|
|
2103
|
-
}
|
|
2104
|
-
: {}),
|
|
2105
|
-
nextEvaluationAt: null,
|
|
2106
|
-
updatedAt: this.deps.now(),
|
|
2107
|
-
});
|
|
2108
|
-
}
|
|
2109
2181
|
}
|
|
2110
2182
|
catch (error) {
|
|
2183
|
+
const current = this.executorCommandCurrent(sessionId, command, fence.executorGeneration);
|
|
2184
|
+
if (!current || current.generation !== fence.sessionGeneration) {
|
|
2185
|
+
if (this.deps.store.find(sessionId)?.generation === fence.sessionGeneration)
|
|
2186
|
+
this.executorCommandTransition(sessionId, command.commandId, 'superseded');
|
|
2187
|
+
return;
|
|
2188
|
+
}
|
|
2111
2189
|
// Only an explicit app-server rejection is safe to retry. A lost
|
|
2112
2190
|
// response may conceal accepted work, so retain its issued fence.
|
|
2113
2191
|
if (!explicitExecutorRejection(error)) {
|
|
@@ -2131,7 +2209,47 @@ export class AutopilotCoordinator {
|
|
|
2131
2209
|
this.scheduleExecutorReplacement(sessionId, next.executor);
|
|
2132
2210
|
else
|
|
2133
2211
|
this.armExecutorRefresh(sessionId, this.deps.policy.executorContinuationMaxMs);
|
|
2212
|
+
return;
|
|
2213
|
+
}
|
|
2214
|
+
// Transport rejection and local persistence/publication failures have
|
|
2215
|
+
// different recovery semantics. Commit acceptance, counters and its
|
|
2216
|
+
// outbox event together; let the queue contain any local failure.
|
|
2217
|
+
const latest = this.executorCommandCurrent(sessionId, command, fence.executorGeneration);
|
|
2218
|
+
if (!latest || latest.generation !== fence.sessionGeneration) {
|
|
2219
|
+
if (this.deps.store.find(sessionId)?.generation === fence.sessionGeneration)
|
|
2220
|
+
this.executorCommandTransition(sessionId, command.commandId, 'superseded');
|
|
2221
|
+
return;
|
|
2134
2222
|
}
|
|
2223
|
+
const executor = latest.executor;
|
|
2224
|
+
if (executor.commands?.find((entry) => entry.commandId === command.commandId)?.status !==
|
|
2225
|
+
'issued')
|
|
2226
|
+
return;
|
|
2227
|
+
const occurredAt = this.deps.now();
|
|
2228
|
+
const progressKey = this.progressKey(sessionId);
|
|
2229
|
+
this.persist({
|
|
2230
|
+
...latest,
|
|
2231
|
+
state: 'monitoring',
|
|
2232
|
+
supervision: recordAutomaticContinuation(latest.supervision ?? startSupervisionProtocol(progressKey), progressKey),
|
|
2233
|
+
consecutiveNoProgress: latest.consecutiveNoProgress + 1,
|
|
2234
|
+
executor: {
|
|
2235
|
+
...executor,
|
|
2236
|
+
outcome: 'partial',
|
|
2237
|
+
continuationCount: executor.continuationCount + 1,
|
|
2238
|
+
lastActivityAt: occurredAt,
|
|
2239
|
+
commands: executor.commands.map((entry) => entry.commandId === command.commandId
|
|
2240
|
+
? { ...entry, status: 'accepted', updatedAt: occurredAt }
|
|
2241
|
+
: entry),
|
|
2242
|
+
},
|
|
2243
|
+
nextEvaluationAt: null,
|
|
2244
|
+
updatedAt: occurredAt,
|
|
2245
|
+
}, undefined, [
|
|
2246
|
+
{
|
|
2247
|
+
sessionId,
|
|
2248
|
+
type: 'autopilot.executor-resumed',
|
|
2249
|
+
payload: { threadId, generation, trigger: trigger.kind },
|
|
2250
|
+
occurredAt,
|
|
2251
|
+
},
|
|
2252
|
+
]);
|
|
2135
2253
|
});
|
|
2136
2254
|
}, delayMs));
|
|
2137
2255
|
}
|
|
@@ -2484,7 +2602,12 @@ export class AutopilotCoordinator {
|
|
|
2484
2602
|
try {
|
|
2485
2603
|
current = this.deps.store.find(sessionId);
|
|
2486
2604
|
}
|
|
2487
|
-
catch {
|
|
2605
|
+
catch (readError) {
|
|
2606
|
+
if (readError instanceof AutopilotStateError) {
|
|
2607
|
+
this.executorTimers.get(sessionId)?.();
|
|
2608
|
+
this.executorTimers.delete(sessionId);
|
|
2609
|
+
return;
|
|
2610
|
+
}
|
|
2488
2611
|
try {
|
|
2489
2612
|
this.armStoreRecovery(sessionId);
|
|
2490
2613
|
}
|
|
@@ -2602,12 +2725,12 @@ function operationFailureCode(error) {
|
|
|
2602
2725
|
return 'OPERATION';
|
|
2603
2726
|
}
|
|
2604
2727
|
function fingerprint(plan) {
|
|
2605
|
-
return JSON.stringify(plan.steps.map((step) => [
|
|
2728
|
+
return requirePlanFingerprint(JSON.stringify(plan.steps.map((step) => [
|
|
2606
2729
|
step.id,
|
|
2607
2730
|
step.state,
|
|
2608
2731
|
step.reviewStatus,
|
|
2609
2732
|
step.children.map((child) => [child.id, child.state]),
|
|
2610
|
-
]));
|
|
2733
|
+
])));
|
|
2611
2734
|
}
|
|
2612
2735
|
function boundedUnique(values, limit) {
|
|
2613
2736
|
return [...new Set(values)].slice(-limit);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { MAX_AUTOPILOT_RECORD_BYTES } from './plan-fingerprint.js';
|
|
2
|
+
/** Terminal evidence cannot become dispatchable again. Issued resumes are never replayed. */
|
|
3
|
+
export function canTransitionExecutorCommand(from, to, idempotentProcessAction = false) {
|
|
4
|
+
if (from === 'failed' && idempotentProcessAction)
|
|
5
|
+
return to === 'issued';
|
|
6
|
+
return from === 'scheduled'
|
|
7
|
+
? ['issued', 'cancelled', 'superseded'].includes(to)
|
|
8
|
+
: from === 'issued' && ['accepted', 'failed', 'cancelled', 'superseded'].includes(to);
|
|
9
|
+
}
|
|
10
|
+
/** Keep unresolved work. Evict oldest terminal evidence by both count and bytes. */
|
|
11
|
+
export function retainExecutorCommands(state, command) {
|
|
12
|
+
const commands = [...(state.executor?.commands ?? []), command];
|
|
13
|
+
const fits = () => {
|
|
14
|
+
const candidate = { ...state, executor: { ...state.executor, commands } };
|
|
15
|
+
// SQLite stores lifecycle JSON inside its row. Double encoding the entire
|
|
16
|
+
// state is a conservative bound, with room for row names and status changes.
|
|
17
|
+
return (commands.length <= 32 &&
|
|
18
|
+
new TextEncoder().encode(JSON.stringify(JSON.stringify(candidate))).byteLength <=
|
|
19
|
+
MAX_AUTOPILOT_RECORD_BYTES - 4096);
|
|
20
|
+
};
|
|
21
|
+
while (!fits()) {
|
|
22
|
+
const index = commands.findIndex((entry) => !['scheduled', 'issued'].includes(entry.status));
|
|
23
|
+
if (index < 0)
|
|
24
|
+
return undefined;
|
|
25
|
+
commands.splice(index, 1);
|
|
26
|
+
}
|
|
27
|
+
return commands;
|
|
28
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
/** An existing invalid controller must not masquerade as an absent one. No persisted content. */
|
|
7
|
+
export class AutopilotStateError extends Error {
|
|
8
|
+
sessionId;
|
|
9
|
+
code = 'AUTOPILOT_STATE_INVALID';
|
|
10
|
+
constructor(sessionId) {
|
|
11
|
+
super('AUTOPILOT_STATE_INVALID');
|
|
12
|
+
this.sessionId = sessionId;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
/** Inspectable serialized plan state, not an identifier. Preserve bytes for comparisons.
|
|
7
|
+
* Legacy opaque fingerprints remain readable; new producers emit serialized JSON.
|
|
8
|
+
* The aggregate record budget also bounds repeated fingerprints in command history.
|
|
9
|
+
*/
|
|
10
|
+
export const MAX_PLAN_FINGERPRINT_BYTES = 64 * 1024;
|
|
11
|
+
export const MAX_AUTOPILOT_RECORD_BYTES = 1024 * 1024;
|
|
12
|
+
export function parsePlanFingerprint(value) {
|
|
13
|
+
return typeof value === 'string' &&
|
|
14
|
+
value.trim().length > 0 &&
|
|
15
|
+
new TextEncoder().encode(value).byteLength <= MAX_PLAN_FINGERPRINT_BYTES
|
|
16
|
+
? value
|
|
17
|
+
: undefined;
|
|
18
|
+
}
|
|
19
|
+
export function requirePlanFingerprint(value) {
|
|
20
|
+
const parsed = parsePlanFingerprint(value);
|
|
21
|
+
if (parsed === undefined)
|
|
22
|
+
throw new Error('AUTOPILOT_FINGERPRINT_INVALID');
|
|
23
|
+
return parsed;
|
|
24
|
+
}
|
|
@@ -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 { parsePlanFingerprint } from './plan-fingerprint.js';
|
|
6
7
|
import { orgPlanAgentDisplayName } from '../../../../shared/org-plan-position.js';
|
|
7
8
|
import { parseSupervisionProtocolState, } from './supervision-protocol.js';
|
|
8
9
|
export const blockingResumeConditions = {
|
|
@@ -71,6 +72,10 @@ export function parsePersistedSupervisedLifecycle(value) {
|
|
|
71
72
|
const root = record(value);
|
|
72
73
|
if (!root)
|
|
73
74
|
return undefined;
|
|
75
|
+
for (const key of ['blocking', 'executor', 'checkpoints']) {
|
|
76
|
+
if (root[key] !== undefined && !record(root[key]))
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
74
79
|
const blockingValue = record(root.blocking);
|
|
75
80
|
const blocking = blockingValue ? parseStructuredBlock(blockingValue) : undefined;
|
|
76
81
|
if (blockingValue && !blocking)
|
|
@@ -90,6 +95,10 @@ export function parsePersistedSupervisedLifecycle(value) {
|
|
|
90
95
|
...(supervision ? { supervision } : {}),
|
|
91
96
|
...(checkpoints ? { checkpoints } : {}),
|
|
92
97
|
};
|
|
98
|
+
for (const key of ['assignment', 'replacement', 'blocking']) {
|
|
99
|
+
if (executorValue[key] !== undefined && !record(executorValue[key]))
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
93
102
|
const processesValue = executorValue.ownedProcesses;
|
|
94
103
|
if (!Array.isArray(processesValue) || processesValue.length > 64)
|
|
95
104
|
return undefined;
|
|
@@ -127,6 +136,10 @@ export function parsePersistedSupervisedLifecycle(value) {
|
|
|
127
136
|
const osPid = nonNegativeInteger(process.osPid);
|
|
128
137
|
const exitStatus = integer(process.exitStatus);
|
|
129
138
|
const resultArtifact = boundedText(process.resultArtifact);
|
|
139
|
+
if ((process.osPid !== undefined && osPid === null) ||
|
|
140
|
+
(process.exitStatus !== undefined && exitStatus === null) ||
|
|
141
|
+
(process.resultArtifact !== undefined && !resultArtifact))
|
|
142
|
+
return [];
|
|
130
143
|
return [
|
|
131
144
|
{
|
|
132
145
|
processId,
|
|
@@ -174,7 +187,7 @@ export function parsePersistedSupervisedLifecycle(value) {
|
|
|
174
187
|
const replacementCanonicalTaskName = replacementValue && boundedText(replacementValue.canonicalTaskName);
|
|
175
188
|
const replacementCanonicalPosition = replacementValue && boundedText(replacementValue.canonicalPosition);
|
|
176
189
|
const replacementPlanIdentity = replacementValue && boundedText(replacementValue.planIdentity);
|
|
177
|
-
const replacementPlanFingerprint = replacementValue &&
|
|
190
|
+
const replacementPlanFingerprint = replacementValue && parsePlanFingerprint(replacementValue.planFingerprint);
|
|
178
191
|
const replacementPreviousValue = replacementValue && record(replacementValue.previous);
|
|
179
192
|
const replacementPrevious = replacementPreviousValue
|
|
180
193
|
? parseExecutorAssignment(replacementPreviousValue)
|
|
@@ -187,6 +200,7 @@ export function parsePersistedSupervisedLifecycle(value) {
|
|
|
187
200
|
!taskPath ||
|
|
188
201
|
!threadId ||
|
|
189
202
|
!l1State ||
|
|
203
|
+
(executorValue.l2State !== undefined && !l2State) ||
|
|
190
204
|
!lastActivityAt ||
|
|
191
205
|
!outcome ||
|
|
192
206
|
continuationGeneration === null ||
|
|
@@ -201,8 +215,7 @@ export function parsePersistedSupervisedLifecycle(value) {
|
|
|
201
215
|
!replacementPlanIdentity ||
|
|
202
216
|
!replacementPlanFingerprint ||
|
|
203
217
|
(replacementValue.previous !== undefined && !replacementPrevious) ||
|
|
204
|
-
(
|
|
205
|
-
!['explicitExecutorRejection', 'humanPermissionGranted'].includes(replacementReason)) ||
|
|
218
|
+
(replacementValue.reason !== undefined && !replacementReason) ||
|
|
206
219
|
(replacementValue.evidence !== undefined && !replacementEvidence))))
|
|
207
220
|
return undefined;
|
|
208
221
|
if (replacementValue) {
|
|
@@ -235,7 +248,7 @@ export function parsePersistedSupervisedLifecycle(value) {
|
|
|
235
248
|
'superseded',
|
|
236
249
|
]);
|
|
237
250
|
const planIdentity = command && boundedText(command.planIdentity);
|
|
238
|
-
const planFingerprint = command &&
|
|
251
|
+
const planFingerprint = command && parsePlanFingerprint(command.planFingerprint);
|
|
239
252
|
const commandPosition = command && boundedText(command.canonicalPosition);
|
|
240
253
|
const commandTaskName = command && boundedText(command.canonicalTaskName);
|
|
241
254
|
const commandTaskPath = command && boundedText(command.taskPath);
|
|
@@ -262,7 +275,8 @@ export function parsePersistedSupervisedLifecycle(value) {
|
|
|
262
275
|
trigger &&
|
|
263
276
|
createdAt &&
|
|
264
277
|
updatedAt &&
|
|
265
|
-
(
|
|
278
|
+
(command.processAction === undefined ||
|
|
279
|
+
(processActionValue && processActionKind && processActionKey))
|
|
266
280
|
? [
|
|
267
281
|
{
|
|
268
282
|
commandId,
|
|
@@ -339,7 +353,7 @@ function parseCheckpoints(value) {
|
|
|
339
353
|
const planIdentity = boundedText(value.planIdentity);
|
|
340
354
|
if (value.protocolVersion !== 1 || !planIdentity)
|
|
341
355
|
return undefined;
|
|
342
|
-
const rawCompletionEpochs = value.completionEpochs
|
|
356
|
+
const rawCompletionEpochs = value.completionEpochs === undefined ? [] : value.completionEpochs;
|
|
343
357
|
if (!Array.isArray(rawCompletionEpochs) || rawCompletionEpochs.length > 640)
|
|
344
358
|
return undefined;
|
|
345
359
|
const completionEpochs = rawCompletionEpochs.flatMap((candidate) => {
|
|
@@ -364,7 +378,7 @@ function parseCheckpoints(value) {
|
|
|
364
378
|
if (completionEpochs.length !== rawCompletionEpochs.length ||
|
|
365
379
|
new Set(completionEpochs.map((epoch) => epoch.target)).size !== completionEpochs.length)
|
|
366
380
|
return undefined;
|
|
367
|
-
const rawReportedL2Ids = value.reportedL2Ids
|
|
381
|
+
const rawReportedL2Ids = value.reportedL2Ids === undefined ? [] : value.reportedL2Ids;
|
|
368
382
|
if (!Array.isArray(rawReportedL2Ids) || rawReportedL2Ids.length > 512)
|
|
369
383
|
return undefined;
|
|
370
384
|
const reportedL2Ids = rawReportedL2Ids.map((id) => boundedText(id)).filter(Boolean);
|
|
@@ -390,6 +404,9 @@ function parseCheckpoints(value) {
|
|
|
390
404
|
const pendingTurnId = value.pendingTurnId === null ? null : boundedText(value.pendingTurnId);
|
|
391
405
|
if (pendingTurnId === undefined)
|
|
392
406
|
return undefined;
|
|
407
|
+
const pendingTarget = value.pendingTarget === undefined ? undefined : canonicalCheckpointTarget(value.pendingTarget);
|
|
408
|
+
if (value.pendingTarget !== undefined && (!pendingTarget || !pendingTurnId))
|
|
409
|
+
return undefined;
|
|
393
410
|
const activeHandoffId = value.activeHandoffId === undefined || value.activeHandoffId === null
|
|
394
411
|
? null
|
|
395
412
|
: boundedText(value.activeHandoffId);
|
|
@@ -400,6 +417,11 @@ function parseCheckpoints(value) {
|
|
|
400
417
|
return undefined;
|
|
401
418
|
if (typeof value.terminalReviewAccepted !== 'boolean')
|
|
402
419
|
return undefined;
|
|
420
|
+
const terminalReviewFingerprint = value.terminalReviewFingerprint === undefined
|
|
421
|
+
? undefined
|
|
422
|
+
: parsePlanFingerprint(value.terminalReviewFingerprint);
|
|
423
|
+
if (value.terminalReviewFingerprint !== undefined && !terminalReviewFingerprint)
|
|
424
|
+
return undefined;
|
|
403
425
|
const pendingKind = value.pendingKind === undefined
|
|
404
426
|
? pendingTurnId
|
|
405
427
|
? value.terminalReviewAccepted
|
|
@@ -410,6 +432,8 @@ function parseCheckpoints(value) {
|
|
|
410
432
|
if (pendingKind !== null &&
|
|
411
433
|
!['l2Completed', 'l1Accepted', 'terminalReviewAccepted'].includes(String(pendingKind)))
|
|
412
434
|
return undefined;
|
|
435
|
+
if ((pendingTurnId === null) !== (pendingKind === null))
|
|
436
|
+
return undefined;
|
|
413
437
|
const legacyTargets = [
|
|
414
438
|
...reportedL1Ids.map((id) => checkpointTarget('l1', id)),
|
|
415
439
|
...reportedL2Ids.flatMap((id) => {
|
|
@@ -442,12 +466,14 @@ function parseCheckpoints(value) {
|
|
|
442
466
|
reportedL1Ids,
|
|
443
467
|
acceptedKeys,
|
|
444
468
|
pendingTurnId,
|
|
469
|
+
...(pendingTarget ? { pendingTarget } : {}),
|
|
445
470
|
...(activeHandoffId ? { activeHandoffId } : {}),
|
|
446
471
|
pendingKind: pendingKind,
|
|
447
472
|
...(value.checkpointHandoffFailed === undefined
|
|
448
473
|
? {}
|
|
449
474
|
: { checkpointHandoffFailed: value.checkpointHandoffFailed }),
|
|
450
475
|
terminalReviewAccepted: value.terminalReviewAccepted,
|
|
476
|
+
...(terminalReviewFingerprint ? { terminalReviewFingerprint } : {}),
|
|
451
477
|
};
|
|
452
478
|
}
|
|
453
479
|
function parseExecutorAssignment(value) {
|