gestalt-mobile 0.54.2 → 0.55.1

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.
Files changed (25) hide show
  1. package/README.md +19 -0
  2. package/dist/client/assets/index-BIVzBvo7.css +1 -0
  3. package/dist/client/assets/index-D6upDR7k.js +115 -0
  4. package/dist/client/index.html +2 -2
  5. package/dist/server/client/features/chat/chat-projection.js +11 -3
  6. package/dist/server/server/composition.contracts.js +383 -6
  7. package/dist/server/server/composition.js +40 -11
  8. package/dist/server/server/features/autopilot/application/service.js +174 -17
  9. package/dist/server/server/features/autopilot/domain/supervision-protocol.js +34 -0
  10. package/dist/server/server/features/plans/application/executor-model-evidence.js +192 -0
  11. package/dist/server/server/features/plans/application/parse-model-policy.js +77 -0
  12. package/dist/server/server/features/plans/application/parse-supervised-plan.js +23 -4
  13. package/dist/server/server/features/plans/application/supervised-plan-registry.js +16 -1
  14. package/dist/server/server/features/plans/domain/model-policy-admission.js +43 -0
  15. package/dist/server/server/features/self-debug/create-session.js +12 -0
  16. package/dist/server/server/platform/codex/codex-model-catalog.js +38 -0
  17. package/dist/server/server/platform/codex/executor-configuration.js +90 -0
  18. package/dist/server/server/platform/codex/session-runtime.js +33 -1
  19. package/dist/server/server/platform/plans/filesystem-plan-status-source.js +118 -13
  20. package/dist/server/server/platform/plans/org-plan-capabilities.js +2 -2
  21. package/gestalt-supervision-capabilities.json +8 -2
  22. package/package.json +1 -1
  23. package/scripts/watch-github-run.mjs +283 -51
  24. package/dist/client/assets/index-BRDqtTQi.css +0 -1
  25. package/dist/client/assets/index-CRxjJEwq.js +0 -113
@@ -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 { assessModelPolicyAdmission } from '../../plans/domain/model-policy-admission.js';
6
7
  import { requirePlanFingerprint } from '../domain/plan-fingerprint.js';
7
8
  import { AutopilotStateError } from '../domain/persisted-state-error.js';
8
9
  import { canTransitionExecutorCommand, retainExecutorCommands, } from '../domain/executor-commands.js';
@@ -101,6 +102,8 @@ export class AutopilotCoordinator {
101
102
  // coordinator. The plan-status watcher is authoritative and asynchronous,
102
103
  // so retaining enabled durable state until it supplies the projection is
103
104
  // safer than interpreting this short bootstrap gap as plan removal.
105
+ if (this.rejectModelPolicyExecution(sessionId))
106
+ return;
104
107
  const retained = this.deps.plan(sessionId);
105
108
  if (!retained)
106
109
  return;
@@ -305,12 +308,26 @@ export class AutopilotCoordinator {
305
308
  this.cancelWaitTimer(sessionId);
306
309
  this.parkedSubscriptions.delete(sessionId);
307
310
  const now = this.deps.now();
308
- this.persist({ ...state, supervision: nextProtocol, updatedAt: now }, undefined, [
311
+ this.persist({
312
+ ...state,
313
+ supervision: {
314
+ ...nextProtocol,
315
+ wakeCause: { condition: wake.condition, leaseId: wake.leaseId },
316
+ },
317
+ updatedAt: now,
318
+ }, undefined, [
309
319
  {
310
320
  sessionId,
311
321
  type: 'autopilot.wait-woken',
312
322
  occurredAt: now,
313
323
  payload: {
324
+ ...this.wakeAttribution({
325
+ ...state,
326
+ supervision: {
327
+ ...nextProtocol,
328
+ wakeCause: { condition: wake.condition, leaseId: wake.leaseId },
329
+ },
330
+ }),
314
331
  leaseId: wake.leaseId,
315
332
  reportId: state.supervision.lastReportId,
316
333
  wakeCondition: wake.condition,
@@ -379,6 +396,9 @@ export class AutopilotCoordinator {
379
396
  if (!session || !session.threadId || !['ready', 'turnActive'].includes(session.state))
380
397
  return { code: 'AUTOPILOT_SESSION_UNAVAILABLE' };
381
398
  const currentPlan = this.deps.plan(sessionId);
399
+ const admission = this.modelPolicyAdmission(sessionId);
400
+ if (!admission.allowed)
401
+ return admission;
382
402
  if (!currentPlan)
383
403
  return { code: 'AUTOPILOT_PLAN_REQUIRED' };
384
404
  const now = this.deps.now();
@@ -543,6 +563,8 @@ export class AutopilotCoordinator {
543
563
  const prior = this.deps.store.find(sessionId);
544
564
  if (!prior)
545
565
  return this.snapshot(sessionId);
566
+ if (this.rejectModelPolicyExecution(sessionId))
567
+ return this.snapshot(sessionId);
546
568
  const session = this.deps.session(sessionId);
547
569
  const decision = this.decision(sessionId, prior);
548
570
  const now = this.deps.now();
@@ -623,6 +645,7 @@ export class AutopilotCoordinator {
623
645
  type: 'autopilot.continuation-scheduled',
624
646
  payload: {
625
647
  controlId: scheduledControlId,
648
+ ...this.wakeAttribution(next),
626
649
  reportId: prior.supervision?.lastReportId ?? null,
627
650
  nextEvaluationAt: decision.at,
628
651
  ...(prior.checkpoints?.activeHandoffId
@@ -1075,6 +1098,8 @@ export class AutopilotCoordinator {
1075
1098
  const prior = this.deps.store.find(sessionId);
1076
1099
  if (!prior)
1077
1100
  return;
1101
+ if (this.rejectModelPolicyExecution(sessionId))
1102
+ return;
1078
1103
  const plan = this.deps.plan(sessionId);
1079
1104
  if (!plan) {
1080
1105
  if (prior.requestedEnabled)
@@ -1178,6 +1203,9 @@ export class AutopilotCoordinator {
1178
1203
  */
1179
1204
  supervisionStarted(sessionId) {
1180
1205
  const currentPlan = this.deps.plan(sessionId);
1206
+ const admission = this.modelPolicyAdmission(sessionId);
1207
+ if (!admission.allowed)
1208
+ return admission;
1181
1209
  if (!currentPlan)
1182
1210
  return { code: 'AUTOPILOT_PLAN_REQUIRED' };
1183
1211
  const now = this.deps.now();
@@ -1344,7 +1372,10 @@ export class AutopilotCoordinator {
1344
1372
  generation: prior.generation + 1,
1345
1373
  nextEvaluationAt: null,
1346
1374
  lastControlId: null,
1347
- supervision: consumeObsoleteWait(prior.supervision ?? startSupervisionProtocol(this.progressKey(sessionId)), this.progressKey(sessionId)),
1375
+ supervision: {
1376
+ ...consumeObsoleteWait(prior.supervision ?? startSupervisionProtocol(this.progressKey(sessionId)), this.progressKey(sessionId)),
1377
+ wakeCause: null,
1378
+ },
1348
1379
  updatedAt: now,
1349
1380
  }, cancelled);
1350
1381
  }
@@ -1375,6 +1406,7 @@ export class AutopilotCoordinator {
1375
1406
  type: 'autopilot.control-issued',
1376
1407
  payload: {
1377
1408
  controlId,
1409
+ ...this.wakeAttribution(prior),
1378
1410
  ...(prior.checkpoints?.activeHandoffId
1379
1411
  ? { traceId: prior.checkpoints.activeHandoffId }
1380
1412
  : {}),
@@ -1393,6 +1425,23 @@ export class AutopilotCoordinator {
1393
1425
  }
1394
1426
  persist(next, control, events = []) {
1395
1427
  const prior = this.deps.store.find(next.sessionId);
1428
+ // Protocol resets affect retry policy, not the last runtime acknowledgement.
1429
+ if (next.supervision && prior?.supervision)
1430
+ next = {
1431
+ ...next,
1432
+ supervision: {
1433
+ ...next.supervision,
1434
+ acknowledgedRevision: next.supervision.acknowledgedRevision ?? prior.supervision.acknowledgedRevision,
1435
+ wakeCause: !next.requestedEnabled ||
1436
+ !prior.requestedEnabled ||
1437
+ (next.supervision.waitLease &&
1438
+ next.supervision.waitLease.id !== prior.supervision.waitLease?.id)
1439
+ ? null
1440
+ : next.supervision.wakeCause === undefined
1441
+ ? prior.supervision.wakeCause
1442
+ : next.supervision.wakeCause,
1443
+ },
1444
+ };
1396
1445
  const previousLease = prior?.supervision?.waitLease;
1397
1446
  const nextLease = next.supervision?.waitLease;
1398
1447
  const diagnostics = [];
@@ -1402,6 +1451,7 @@ export class AutopilotCoordinator {
1402
1451
  type: 'autopilot.wait-registered',
1403
1452
  occurredAt: next.updatedAt,
1404
1453
  payload: {
1454
+ ...this.wakeAttribution(next),
1405
1455
  leaseId: nextLease.id,
1406
1456
  reportId: next.supervision?.lastReportId,
1407
1457
  resumeAt: nextLease.resumeAt ?? null,
@@ -1455,6 +1505,8 @@ export class AutopilotCoordinator {
1455
1505
  const current = this.deps.store.find(sessionId);
1456
1506
  if (!current || current.generation !== generation || !current.requestedEnabled)
1457
1507
  return;
1508
+ if (this.rejectModelPolicyExecution(sessionId))
1509
+ return;
1458
1510
  if (current.executor?.replacement &&
1459
1511
  !this.validReplacement(sessionId, current.executor.replacement)) {
1460
1512
  const now = this.deps.now();
@@ -1656,18 +1708,31 @@ export class AutopilotCoordinator {
1656
1708
  }
1657
1709
  this.parkedSubscriptions.delete(sessionId);
1658
1710
  const observedAt = this.deps.now();
1659
- this.persist({ ...state, supervision: protocol, updatedAt: observedAt }, undefined, [
1711
+ this.persist({
1712
+ ...state,
1713
+ supervision: { ...protocol, wakeCause: { condition: 'deadline', leaseId } },
1714
+ updatedAt: observedAt,
1715
+ }, undefined, [
1660
1716
  {
1661
1717
  sessionId,
1662
1718
  type: 'autopilot.wait-expired',
1663
1719
  occurredAt: observedAt,
1664
- payload: { leaseId, reportId: state.supervision.lastReportId, resumeAt, observedAt },
1720
+ payload: {
1721
+ ...this.wakeAttribution(state),
1722
+ wakeReason: 'deadline',
1723
+ leaseId,
1724
+ reportId: state.supervision.lastReportId,
1725
+ resumeAt,
1726
+ observedAt,
1727
+ },
1665
1728
  },
1666
1729
  ]);
1667
1730
  this.evaluate(sessionId);
1668
1731
  }, Math.max(0, Date.parse(resumeAt) - Date.parse(this.deps.now()))));
1669
1732
  }
1670
1733
  async enforceSupervisedLifecycle(sessionId, event) {
1734
+ if (this.rejectModelPolicyExecution(sessionId))
1735
+ return true;
1671
1736
  const controller = this.deps.executorController;
1672
1737
  const retained = this.deps.plan(sessionId);
1673
1738
  const state = this.deps.store.find(sessionId);
@@ -2497,6 +2562,8 @@ export class AutopilotCoordinator {
2497
2562
  }
2498
2563
  delivered = true;
2499
2564
  this.enqueue(sessionId, async () => {
2565
+ if (this.rejectModelPolicyExecution(sessionId))
2566
+ return;
2500
2567
  const current = this.deps.store.find(sessionId);
2501
2568
  const latestPlan = this.deps.plan(sessionId);
2502
2569
  const activity = this.deps.activity(sessionId);
@@ -2646,14 +2713,31 @@ export class AutopilotCoordinator {
2646
2713
  };
2647
2714
  const occurredAt = this.deps.now();
2648
2715
  const state = this.deps.store.find(sessionId);
2716
+ const accepted = status === 'started' && Boolean(turnId);
2717
+ const acceptanceState = accepted && state
2718
+ ? {
2719
+ ...state,
2720
+ supervision: {
2721
+ ...(state.supervision ?? startSupervisionProtocol(this.progressKey(sessionId))),
2722
+ acknowledgedRevision: {
2723
+ revision: this.supervisionRevision(state),
2724
+ controlId,
2725
+ turnId: turnId,
2726
+ },
2727
+ wakeCause: null,
2728
+ },
2729
+ updatedAt: occurredAt,
2730
+ }
2731
+ : undefined;
2649
2732
  this.commit({
2733
+ ...(acceptanceState ? { state: acceptanceState } : {}),
2650
2734
  ...(status === 'started' &&
2651
2735
  turnId &&
2652
2736
  state?.checkpoints?.rootContinuationFromTurnId &&
2653
2737
  turnId !== state.checkpoints.rootContinuationFromTurnId
2654
2738
  ? {
2655
2739
  state: {
2656
- ...state,
2740
+ ...(acceptanceState ?? state),
2657
2741
  checkpoints: { ...state.checkpoints, rootContinuationFromTurnId: undefined },
2658
2742
  stopReason: null,
2659
2743
  updatedAt: occurredAt,
@@ -2669,6 +2753,8 @@ export class AutopilotCoordinator {
2669
2753
  payload: eventType === 'autopilot.turn-failed'
2670
2754
  ? {
2671
2755
  controlId,
2756
+ ...(state ? this.wakeAttribution(state) : {}),
2757
+ trafficClass: 'control',
2672
2758
  code: failureCode,
2673
2759
  ...(this.deps.store.find(sessionId)?.checkpoints?.activeHandoffId
2674
2760
  ? { traceId: this.deps.store.find(sessionId).checkpoints.activeHandoffId }
@@ -2676,6 +2762,9 @@ export class AutopilotCoordinator {
2676
2762
  }
2677
2763
  : {
2678
2764
  controlId,
2765
+ ...(state ? this.wakeAttribution(state) : {}),
2766
+ trafficClass: accepted ? 'acceptedRootTurn' : 'unconfirmedStart',
2767
+ turnId,
2679
2768
  ...(this.deps.store.find(sessionId)?.checkpoints?.activeHandoffId
2680
2769
  ? { traceId: this.deps.store.find(sessionId).checkpoints.activeHandoffId }
2681
2770
  : {}),
@@ -2724,25 +2813,91 @@ export class AutopilotCoordinator {
2724
2813
  const plan = this.deps.plan(next.sessionId);
2725
2814
  const activity = this.deps.activity(next.sessionId);
2726
2815
  const now = this.deps.now();
2816
+ const admission = this.modelPolicyAdmission(next.sessionId);
2727
2817
  const control = prospectiveControl ??
2728
2818
  (next.lastControlId ? this.deps.store.findControl(next.sessionId, next.lastControlId) : null);
2729
- return autopilotSnapshot(next, this.deps.policy.retryLimit, {
2730
- activeTurn: Boolean(session?.activeTurnId),
2731
- executorActive: this.authoritativeExecutorActive(next, plan?.plan ?? null, activity),
2732
- control: control?.status ?? 'none',
2733
- timerArmed: this.timers.has(next.sessionId),
2734
- reconciling: this.reconciling.has(next.sessionId),
2735
- planMatches: Boolean(plan && plan.identity === next.planIdentity),
2736
- parkedSubscriptionActive: Boolean(next.supervision?.waitLease) &&
2737
- this.parkedSubscriptions.get(next.sessionId) === next.supervision?.waitLease?.id,
2738
- transitionFresh: Date.parse(now) - Date.parse(next.updatedAt) <= 120_000,
2739
- observedAt: now,
2740
- });
2819
+ return {
2820
+ ...autopilotSnapshot(next, this.deps.policy.retryLimit, {
2821
+ activeTurn: Boolean(session?.activeTurnId),
2822
+ executorActive: this.authoritativeExecutorActive(next, plan?.plan ?? null, activity),
2823
+ control: control?.status ?? 'none',
2824
+ timerArmed: this.timers.has(next.sessionId),
2825
+ reconciling: this.reconciling.has(next.sessionId),
2826
+ planMatches: Boolean(plan && plan.identity === next.planIdentity),
2827
+ parkedSubscriptionActive: Boolean(next.supervision?.waitLease) &&
2828
+ this.parkedSubscriptions.get(next.sessionId) === next.supervision?.waitLease?.id,
2829
+ transitionFresh: Date.parse(now) - Date.parse(next.updatedAt) <= 120_000,
2830
+ observedAt: now,
2831
+ }),
2832
+ supervisionDelta: this.supervisionDelta(next),
2833
+ ...(!admission.allowed ? { executionAdmission: admission } : {}),
2834
+ };
2835
+ }
2836
+ modelPolicyAdmission(sessionId) {
2837
+ return assessModelPolicyAdmission(this.deps.plan(sessionId)?.plan ?? null, this.deps.modelPolicyStatus?.(sessionId));
2838
+ }
2839
+ /** Retire future automatic authority without interrupting an already-running/manual turn. */
2840
+ rejectModelPolicyExecution(sessionId) {
2841
+ const admission = this.modelPolicyAdmission(sessionId);
2842
+ if (admission.allowed)
2843
+ return false;
2844
+ const prior = this.deps.store.find(sessionId);
2845
+ if (!prior?.requestedEnabled)
2846
+ return true;
2847
+ const now = this.deps.now();
2848
+ const cancelled = this.cancelScheduledControl(prior, now);
2849
+ this.cancelTimer(sessionId);
2850
+ this.cancelWaitTimer(sessionId);
2851
+ this.parkedSubscriptions.delete(sessionId);
2852
+ const current = this.deps.store.find(sessionId) ?? prior;
2853
+ this.persist({
2854
+ ...current,
2855
+ state: 'safetyPaused',
2856
+ requestedEnabled: false,
2857
+ generation: current.generation + 1,
2858
+ stopReason: 'safetyPaused',
2859
+ nextEvaluationAt: null,
2860
+ ...(cancelled ? { lastControlId: null } : {}),
2861
+ updatedAt: now,
2862
+ }, cancelled, [{ sessionId, type: 'autopilot.model-policy-rejected', payload: admission, occurredAt: now }]);
2863
+ return true;
2864
+ }
2865
+ supervisionRevision(state) {
2866
+ return `sha256:${createHash('sha256')
2867
+ .update(this.progressKeyFor(state.sessionId, state, this.deps.plan(state.sessionId)))
2868
+ .digest('hex')}`;
2869
+ }
2870
+ supervisionDelta(state) {
2871
+ const revision = this.supervisionRevision(state);
2872
+ const acknowledgement = state.supervision?.acknowledgedRevision;
2873
+ return {
2874
+ revision,
2875
+ acknowledgedRevision: acknowledgement?.revision ?? null,
2876
+ changed: acknowledgement ? acknowledgement.revision !== revision : null,
2877
+ acceptedControlId: acknowledgement?.controlId ?? null,
2878
+ acceptedTurnId: acknowledgement?.turnId ?? null,
2879
+ };
2880
+ }
2881
+ wakeAttribution(state) {
2882
+ return {
2883
+ trafficClass: 'control',
2884
+ supervisionDelta: this.supervisionDelta(state),
2885
+ wakeReason: state.checkpoints?.rootContinuationFromTurnId
2886
+ ? 'checkpointContinuation'
2887
+ : (state.supervision?.wakeCause?.condition ??
2888
+ (state.supervision?.outcome === 'probeRequired'
2889
+ ? 'probeRequired'
2890
+ : 'continuationPolicy')),
2891
+ leaseId: state.supervision?.wakeCause?.leaseId ?? state.supervision?.waitLease?.id ?? null,
2892
+ };
2741
2893
  }
2742
2894
  semanticSnapshot(next, control) {
2743
2895
  const snapshot = this.snapshotFor(next, control);
2744
2896
  return JSON.stringify({
2745
2897
  ...snapshot,
2898
+ // Diagnostic revisions are sampled on reads/existing publications. They
2899
+ // must not create status traffic or change the scheduling projection.
2900
+ supervisionDelta: undefined,
2746
2901
  updatedAt: '',
2747
2902
  health: { ...snapshot.health, observedAt: '', lastTransitionAt: '' },
2748
2903
  });
@@ -2759,6 +2914,8 @@ export class AutopilotCoordinator {
2759
2914
  {
2760
2915
  sessionId: next.sessionId,
2761
2916
  type: 'autopilot.updated',
2917
+ // Status events are observation traffic; only turn-started with a
2918
+ // concrete turnId is evidence of an accepted automatic root turn.
2762
2919
  payload: snapshot,
2763
2920
  occurredAt: next.updatedAt,
2764
2921
  },
@@ -293,12 +293,46 @@ export function parseSupervisionProtocolState(value) {
293
293
  const waitLease = parseWaitLease(candidate.waitLease);
294
294
  if (waitLease === undefined)
295
295
  return undefined;
296
+ const acknowledgement = candidate.acknowledgedRevision;
297
+ const wakeCause = candidate.wakeCause;
298
+ let parsedWake;
299
+ let parsedAcknowledgement;
300
+ if (wakeCause !== undefined && wakeCause !== null) {
301
+ if (typeof wakeCause !== 'object')
302
+ return undefined;
303
+ const wake = wakeCause;
304
+ if (![...observableWakeConditions, 'deadline'].includes(wake.condition) ||
305
+ typeof wake.leaseId !== 'string' ||
306
+ !wake.leaseId ||
307
+ wake.leaseId.length > 256)
308
+ return undefined;
309
+ parsedWake = {
310
+ condition: wake.condition,
311
+ leaseId: wake.leaseId,
312
+ };
313
+ }
314
+ if (acknowledgement !== undefined) {
315
+ if (!acknowledgement || typeof acknowledgement !== 'object')
316
+ return undefined;
317
+ const value = acknowledgement;
318
+ if (typeof value.revision !== 'string' ||
319
+ !/^sha256:[a-f0-9]{64}$/.test(value.revision) ||
320
+ ![value.controlId, value.turnId].every((id) => typeof id === 'string' && id.length > 0 && id.length <= 256))
321
+ return undefined;
322
+ parsedAcknowledgement = {
323
+ revision: value.revision,
324
+ controlId: value.controlId,
325
+ turnId: value.turnId,
326
+ };
327
+ }
296
328
  if (candidate.safetyPauseReason !== null &&
297
329
  candidate.safetyPauseReason !== 'retryRecurrence' &&
298
330
  candidate.safetyPauseReason !== 'invalidProbeReport')
299
331
  return undefined;
300
332
  return {
301
333
  ...base,
334
+ ...(wakeCause !== undefined ? { wakeCause: parsedWake ?? null } : {}),
335
+ ...(parsedAcknowledgement ? { acknowledgedRevision: parsedAcknowledgement } : {}),
302
336
  outcome: candidate.outcome,
303
337
  unchangedContinuations,
304
338
  probeKey,
@@ -0,0 +1,192 @@
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
+ import { createHash } from 'node:crypto';
7
+ import { parseOrgPlanAgentIdentity } from '../../../../shared/org-plan-position.js';
8
+ import { currentExecutableL1Index, } from '../domain/supervised-plan.js';
9
+ /** Same execution-only digest as Agents; lifecycle/prose changes do not select a new model. */
10
+ export function executionPolicyDigest(policy) {
11
+ const canonical = (value) => value && typeof value === 'object'
12
+ ? Array.isArray(value)
13
+ ? value.map(canonical)
14
+ : Object.fromEntries(Object.entries(value)
15
+ .sort(([a], [b]) => a.localeCompare(b))
16
+ .map(([key, item]) => [key, canonical(item)]))
17
+ : value;
18
+ return createHash('sha256')
19
+ .update(JSON.stringify(canonical({
20
+ version: policy.version,
21
+ model: policy.model,
22
+ reasoningEffort: policy.reasoningEffort,
23
+ necessity: policy.necessity,
24
+ })))
25
+ .digest('hex');
26
+ }
27
+ const small = (value, bytes = 96) => typeof value === 'string' &&
28
+ value.trim() &&
29
+ Buffer.byteLength(JSON.stringify(value), 'utf8') <= bytes
30
+ ? value
31
+ : null;
32
+ /** One current, bounded observation. It never admits work or changes runtime settings. */
33
+ export class ExecutorModelEvidenceReader {
34
+ context;
35
+ runtime;
36
+ constructor(context, runtime) {
37
+ this.context = context;
38
+ this.runtime = runtime;
39
+ }
40
+ async read(sessionId) {
41
+ const context = this.context(sessionId);
42
+ if (!context ||
43
+ context.sessionId !== sessionId ||
44
+ context.provider !== 'codex' ||
45
+ context.plan?.modelPolicyVersion !== 1)
46
+ return null;
47
+ const index = currentExecutableL1Index(context.plan);
48
+ const step = context.plan.steps[index];
49
+ const policy = step?.modelPolicy;
50
+ if (!policy)
51
+ return null;
52
+ const executor = context.executor;
53
+ const identity = executor ? parseOrgPlanAgentIdentity(executor.taskPath) : null;
54
+ const child = {
55
+ threadId: small(executor?.threadId),
56
+ generation: identity?.generation ?? null,
57
+ continuationCount: executor?.continuationCount ?? null,
58
+ observationId: null,
59
+ };
60
+ const report = {
61
+ version: 1,
62
+ l1: { id: small(step.id), position: index + 1 },
63
+ policyDigest: executionPolicyDigest(policy),
64
+ requested: { model: policy.model, reasoningEffort: policy.reasoningEffort },
65
+ effective: { model: null, reasoningEffort: null },
66
+ observedRole: null,
67
+ child,
68
+ source: null,
69
+ evidenceScope: 'configured-thread-settings',
70
+ observedAt: null,
71
+ settingsRevision: null,
72
+ settingsFingerprint: null,
73
+ catalogRevision: null,
74
+ catalogRevisionSource: 'normalized-content-digest',
75
+ configuration: 'unknown',
76
+ admitted: false,
77
+ runtimeEnforcement: false,
78
+ code: 'CHILD_BINDING_UNKNOWN',
79
+ unknown: [],
80
+ };
81
+ const finish = (code) => {
82
+ report.code = code;
83
+ report.unknown = [
84
+ ...Object.entries(report.effective)
85
+ .filter(([, value]) => value === null)
86
+ .map(([key]) => `effective.${key}`),
87
+ ...(report.child.threadId === null ? ['child.threadId'] : []),
88
+ ...(report.catalogRevision === null ? ['catalogRevision'] : []),
89
+ ...(report.observedAt === null ? ['observedAt'] : []),
90
+ 'settingsRevision',
91
+ ];
92
+ return report;
93
+ };
94
+ if (!executor ||
95
+ !identity ||
96
+ !context.rootThreadId ||
97
+ !context.planIdentity ||
98
+ !child.threadId ||
99
+ !report.l1.id ||
100
+ executor.threadId === context.rootThreadId ||
101
+ identity.canonicalPosition !== `L${index + 1}` ||
102
+ executor.canonicalPosition !== identity.canonicalPosition ||
103
+ executor.canonicalTaskName !== identity.canonicalTaskName ||
104
+ executor.continuationGeneration !== identity.generation ||
105
+ executor.assignment?.state === 'closed' ||
106
+ executor.assignment?.state === 'unavailable' ||
107
+ executor.replacement ||
108
+ (executor.assignment &&
109
+ (executor.assignment.threadId !== executor.threadId ||
110
+ executor.assignment.taskPath !== executor.taskPath ||
111
+ executor.assignment.generation !== identity.generation)))
112
+ return finish('CHILD_BINDING_UNKNOWN');
113
+ const fence = contextFence(context);
114
+ let result;
115
+ try {
116
+ result = await this.runtime.read({
117
+ sessionId,
118
+ rootThreadId: context.rootThreadId,
119
+ threadId: executor.threadId,
120
+ taskPath: executor.taskPath,
121
+ });
122
+ }
123
+ catch {
124
+ return finish('RUNTIME_UNAVAILABLE');
125
+ }
126
+ if (contextFence(this.context(sessionId)) !== fence)
127
+ return finish('CONTEXT_CHANGED');
128
+ if (result.kind !== 'observed')
129
+ return finish(result.code);
130
+ const observed = result.configuration;
131
+ report.source = observed.source;
132
+ report.child.observationId = small(observed.observationId);
133
+ report.observedAt = small(observed.observedAt, 40);
134
+ report.settingsFingerprint = small(observed.settingsFingerprint);
135
+ report.effective.model = small(observed.model, 64);
136
+ report.effective.reasoningEffort = small(observed.reasoningEffort, 24);
137
+ report.observedRole = small(observed.role, 64);
138
+ report.catalogRevision = small(observed.catalog?.revision);
139
+ if (report.observedRole && !['default', 'org-plan-executor'].includes(report.observedRole)) {
140
+ report.configuration = 'mismatch';
141
+ return finish('ROLE_MISMATCH');
142
+ }
143
+ if (report.effective.model !== null && report.effective.model !== policy.model) {
144
+ report.configuration = 'mismatch';
145
+ return finish('MODEL_MISMATCH');
146
+ }
147
+ if (report.effective.reasoningEffort !== null &&
148
+ report.effective.reasoningEffort !== policy.reasoningEffort) {
149
+ report.configuration = 'mismatch';
150
+ return finish('EFFORT_MISMATCH');
151
+ }
152
+ if (!observed.catalog)
153
+ return finish('CATALOG_UNKNOWN');
154
+ const available = observed.catalog.models.find((model) => model.id === policy.model);
155
+ if (!available || !available.reasoningEfforts.includes(policy.reasoningEffort)) {
156
+ report.configuration = 'mismatch';
157
+ return finish(!available ? 'MODEL_UNAVAILABLE' : 'EFFORT_UNSUPPORTED');
158
+ }
159
+ if (!report.effective.model ||
160
+ !report.effective.reasoningEffort ||
161
+ !report.observedAt ||
162
+ !report.child.observationId)
163
+ return finish('RUNTIME_SETTINGS_UNKNOWN');
164
+ report.configuration = 'match';
165
+ return finish('RUNTIME_REVISION_UNKNOWN');
166
+ }
167
+ }
168
+ function contextFence(context) {
169
+ const index = context?.plan ? currentExecutableL1Index(context.plan) : -1;
170
+ const step = context?.plan?.steps[index];
171
+ const executor = context?.executor;
172
+ return JSON.stringify({
173
+ sessionId: context?.sessionId,
174
+ provider: context?.provider,
175
+ root: context?.rootThreadId,
176
+ plan: context?.planIdentity,
177
+ policyVersion: context?.plan?.modelPolicyVersion,
178
+ l1: step?.id,
179
+ state: step?.state,
180
+ policy: step?.modelPolicy ? executionPolicyDigest(step.modelPolicy) : null,
181
+ child: executor
182
+ ? [
183
+ executor.threadId,
184
+ executor.taskPath,
185
+ executor.continuationGeneration,
186
+ executor.continuationCount,
187
+ executor.assignment,
188
+ executor.replacement,
189
+ ]
190
+ : null,
191
+ });
192
+ }
@@ -0,0 +1,77 @@
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
+ const models = ['gpt-6-luna', 'gpt-6-sol', 'gpt-6.1-sol'];
7
+ const required = [
8
+ 'MODEL',
9
+ 'REASONING_EFFORT',
10
+ 'MODEL_RATIONALE',
11
+ 'MODEL_FALLBACKS',
12
+ 'MODEL_ESCALATION_TRIGGER',
13
+ ];
14
+ const exception = [
15
+ 'EFFORT_NECESSITY_KIND',
16
+ 'EFFORT_NECESSITY',
17
+ 'EFFORT_EVIDENCE',
18
+ 'EFFORT_SCOPE',
19
+ 'RETURN_TO_MEDIUM',
20
+ 'SCOPE_REASSESSMENT',
21
+ ];
22
+ /** Same schema as Agents model-policy-v1; no catalog or effective-setting inference. */
23
+ export function parseModelPolicy(properties, level, version) {
24
+ const declared = [...required, ...exception].filter((key) => properties.has(key));
25
+ const invalid = () => {
26
+ throw new Error('INVALID_MODEL_POLICY');
27
+ };
28
+ if (properties.has('MODEL_POLICY_VERSION'))
29
+ invalid();
30
+ if (declared.length && (level === 2 || version === undefined))
31
+ invalid();
32
+ if (version === undefined || level === 2)
33
+ return undefined;
34
+ if (required.some((key) => !properties.get(key)?.trim()))
35
+ invalid();
36
+ if (declared.some((key) => !properties.get(key).trim() || Buffer.byteLength(properties.get(key), 'utf8') > 1024))
37
+ invalid();
38
+ const model = properties.get('MODEL');
39
+ const reasoningEffort = properties.get('REASONING_EFFORT');
40
+ if (!models.includes(model) || !['medium', 'high', 'xhigh'].includes(reasoningEffort))
41
+ invalid();
42
+ const rawFallbacks = properties.get('MODEL_FALLBACKS');
43
+ const allowedFallbacks = rawFallbacks === 'none' ? [] : rawFallbacks.split(' ');
44
+ if (allowedFallbacks.some((id) => !models.includes(id) || id === model) ||
45
+ new Set(allowedFallbacks).size !== allowedFallbacks.length)
46
+ invalid();
47
+ let necessity = null;
48
+ if (reasoningEffort === 'medium') {
49
+ if (exception.some((key) => properties.has(key)))
50
+ invalid();
51
+ }
52
+ else {
53
+ if (exception.slice(0, 5).some((key) => !properties.get(key)?.trim()) ||
54
+ !['unresolved-correctness', 'specific-reasoning'].includes(properties.get('EFFORT_NECESSITY_KIND')) ||
55
+ (reasoningEffort === 'xhigh' && !properties.get('SCOPE_REASSESSMENT')?.trim()))
56
+ invalid();
57
+ necessity = Object.freeze({
58
+ kind: properties.get('EFFORT_NECESSITY_KIND'),
59
+ whyMediumInadequate: properties.get('EFFORT_NECESSITY'),
60
+ evidence: properties.get('EFFORT_EVIDENCE'),
61
+ affectedScope: properties.get('EFFORT_SCOPE'),
62
+ returnToMedium: properties.get('RETURN_TO_MEDIUM'),
63
+ ...(properties.has('SCOPE_REASSESSMENT')
64
+ ? { scopeReassessment: properties.get('SCOPE_REASSESSMENT') }
65
+ : {}),
66
+ });
67
+ }
68
+ return Object.freeze({
69
+ version: 1,
70
+ model: model,
71
+ reasoningEffort: reasoningEffort,
72
+ rationale: properties.get('MODEL_RATIONALE'),
73
+ allowedFallbacks: Object.freeze(allowedFallbacks),
74
+ escalationTrigger: properties.get('MODEL_ESCALATION_TRIGGER'),
75
+ necessity,
76
+ });
77
+ }