File size: 4,460 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import type { OmegaSessionSelfState } from "./event-model.js";
import {
  shouldDispatchOmegaHeartbeatPrompt,
  syncOmegaExecutionControllerState,
  type OmegaExecutionControllerState,
} from "./execution-controller.js";
import { loadOmegaWSP, type OmegaWorldStatePersistent } from "./omega-wsp.js";
import {
  loadOmegaOperationalMemoryTail,
  summarizeOmegaOperationalMemory,
  type OmegaOperationalMemorySummary,
} from "./operational-memory.js";
import { deriveOmegaPolicySnapshot, type OmegaPolicySnapshot } from "./policy-engine.js";
import type { OmegaSelfTimeKernelState } from "./self-time-kernel.js";
import type { OmegaSessionTimelineEntry } from "./session-context.js";
import {
  deriveOmegaStateAuthoritySnapshot,
  type OmegaStateAuthoritySnapshot,
} from "./state-authority.js";

export type OmegaDecisionDegradedComponent = {
  component: string;
  reason: string;
};

export type OmegaDecisionContext = {
  timeline: OmegaSessionTimelineEntry[];
  sessionState?: OmegaSessionSelfState;
  kernel?: OmegaSelfTimeKernelState;
  wsp?: OmegaWorldStatePersistent;
  controllerState?: OmegaExecutionControllerState;
  operationalSummary: OmegaOperationalMemorySummary;
  stateAuthority: OmegaStateAuthoritySnapshot;
  policy: OmegaPolicySnapshot;
  shouldDispatchHeartbeatPrompt: boolean;
  degradedComponents: OmegaDecisionDegradedComponent[];
};

async function captureDecisionContextComponent<T>(
  component: string,
  degradedComponents: OmegaDecisionDegradedComponent[],
  operation: () => Promise<T>,
  fallback: () => Promise<T> | T,
): Promise<T> {
  try {
    return await operation();
  } catch (error) {
    degradedComponents.push({
      component,
      reason: error instanceof Error ? error.message : String(error),
    });
    return await fallback();
  }
}

export async function loadOmegaDecisionContext(params: {
  workspaceRoot: string;
  sessionKey: string;
  memoryCandidates?: string[];
  includeWorldSnapshot?: boolean | "urgent_maintenance";
  task?: string;
  expectedPaths?: string[];
  watchedPaths?: string[];
}): Promise<OmegaDecisionContext> {
  const degradedComponents: OmegaDecisionDegradedComponent[] = [];
  const [controllerState, fallbackOperationalTail, wsp] = await Promise.all([
    captureDecisionContextComponent(
      "controller_state",
      degradedComponents,
      () =>
        syncOmegaExecutionControllerState({
          workspaceRoot: params.workspaceRoot,
          sessionKey: params.sessionKey,
          includeOperationalSummary: true,
          includeWorldSnapshot: params.includeWorldSnapshot,
          task: params.task,
          expectedPaths: params.expectedPaths,
          watchedPaths: params.watchedPaths,
        }),
      () => undefined,
    ),
    captureDecisionContextComponent(
      "operational_memory_tail",
      degradedComponents,
      () => loadOmegaOperationalMemoryTail(params),
      () => [],
    ),
    captureDecisionContextComponent(
      "omega_wsp",
      degradedComponents,
      () => loadOmegaWSP(params.workspaceRoot, params.sessionKey),
      () => undefined,
    ),
  ]);
  const sessionSnapshot = controllerState?.executiveState.sourceSessionAuthority ?? {
    timeline: [],
    state: undefined,
    kernel: undefined,
    transactions: [],
  };
  const operationalSummary =
    controllerState?.operationalSummary ?? summarizeOmegaOperationalMemory(fallbackOperationalTail);
  const stateAuthority = deriveOmegaStateAuthoritySnapshot({
    kernel: sessionSnapshot.kernel,
    controllerState,
    operationalSummary,
    worldSnapshot: controllerState?.worldSnapshot,
    wsp,
  });
  const policy = deriveOmegaPolicySnapshot({
    kernel: sessionSnapshot.kernel,
    wsp,
    nowMs: Date.now(),
    memoryCandidates: params.memoryCandidates,
    operationalSummary,
    hasUrgentMaintenance: controllerState?.hasUrgentMaintenance,
    stateAuthority,
  });

  return {
    timeline: sessionSnapshot.timeline,
    sessionState: sessionSnapshot.state,
    kernel: sessionSnapshot.kernel,
    wsp,
    controllerState,
    operationalSummary,
    stateAuthority,
    policy,
    shouldDispatchHeartbeatPrompt: controllerState
      ? shouldDispatchOmegaHeartbeatPrompt({
          dispatchPlan: controllerState.dispatchPlan,
          wakeAction: policy.wakeAction,
          shouldRunAutonomy: policy.shouldRunAutonomy,
        })
      : policy.wakeAction.kind !== "heartbeat_ok" || policy.shouldRunAutonomy,
    degradedComponents,
  };
}