File size: 8,174 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import type { OmegaExecutiveDispatchPlan } from "./executive-runtime.js";
import type { OmegaInterruptedGoalRecovery } from "./types.js";
import type { OmegaWorldModelSnapshot } from "./world-model.js";

export type OmegaExecutionRoute = "omega_delegate" | "sessions_spawn";
export type OmegaWorkPolicyRoute = OmegaExecutionRoute | "sessions_send";

export type OmegaEmpiricalRoutingPreference = {
  route: OmegaExecutionRoute;
  confidence: number;
  source: "active" | "generalized" | "locality" | "locality_guard";
};

export type OmegaExecutiveRoutingDirective = {
  route: OmegaExecutionRoute;
  reason:
    | "executive_recover_single_target"
    | "executive_recover_multi_target"
    | "executive_recover_isolated_bias"
    | "executive_execute_validated_goal";
  selectedAction: OmegaExecutiveDispatchPlan["selectedAction"];
  queueKind: OmegaExecutiveDispatchPlan["queueKind"];
};

export type OmegaRecoveryRouteDecision = {
  route: OmegaExecutionRoute;
  reason:
    | "suggested_delegate"
    | "suggested_isolation"
    | "stalled_recently"
    | "multi_target_write_repair"
    | "structured_contract_repair"
    | "single_target_local_retry"
    | "empirical_delegate_bias"
    | "empirical_isolation_bias";
};

export const OMEGA_STRONG_ROUTING_CONFIDENCE = 0.7;

export function buildOmegaRecoveryStrategyKey(params: {
  recovery: OmegaInterruptedGoalRecovery;
  route: OmegaExecutionRoute;
}): string {
  const scope =
    params.recovery.remainingTargets.length > 1
      ? "multi_target"
      : params.recovery.remainingTargets.length === 1
        ? "single_target"
        : params.recovery.expectsJson
          ? "structured_only"
          : "general";
  const collateral = "contained";
  const errorKind = params.recovery.lastErrorKind ?? "unknown";
  return `${errorKind}|${scope}|${collateral}|${params.route}`;
}

export function deriveOmegaEmpiricalRoutingPreference(params: {
  snapshot?: OmegaWorldModelSnapshot;
  requiresValidation: boolean;
  expectedPathCount: number;
  watchedPathCount?: number;
}): OmegaEmpiricalRoutingPreference | undefined {
  if (!params.requiresValidation || params.expectedPathCount > 1 || !params.snapshot) {
    return undefined;
  }

  const localityGuard = params.snapshot.localityExecutionGuard;
  if (
    localityGuard?.shouldIsolate &&
    localityGuard.confidence >= OMEGA_STRONG_ROUTING_CONFIDENCE &&
    (params.watchedPathCount ?? 0) > params.expectedPathCount
  ) {
    return {
      route: "sessions_spawn",
      confidence: localityGuard.confidence,
      source: "locality_guard",
    };
  }

  const active = params.snapshot.activeRecoveryPreference;
  if (active && active.confidence >= OMEGA_STRONG_ROUTING_CONFIDENCE) {
    return {
      route: active.preferredRoute,
      confidence: active.confidence,
      source: "active",
    };
  }

  const generalized = params.snapshot.generalizedRecoveryPreference;
  if (generalized && generalized.confidence >= OMEGA_STRONG_ROUTING_CONFIDENCE) {
    return {
      route: generalized.preferredRoute,
      confidence: generalized.confidence,
      source: "generalized",
    };
  }

  const locality = params.snapshot.localityRoutingPreference;
  if (locality && locality.confidence >= OMEGA_STRONG_ROUTING_CONFIDENCE) {
    return {
      route: locality.preferredRoute,
      confidence: locality.confidence,
      source: "locality",
    };
  }

  return undefined;
}

export function deriveOmegaExecutiveRoutingDirective(params: {
  dispatchPlan?: OmegaExecutiveDispatchPlan;
  requiresValidation: boolean;
  expectedPathCount: number;
  matchedRecoverySuggestedRoute?: OmegaExecutionRoute;
  preferredValidatedRoute?: OmegaEmpiricalRoutingPreference;
}): OmegaExecutiveRoutingDirective | undefined {
  const plan = params.dispatchPlan;
  if (!params.requiresValidation || !plan || !plan.shouldDispatchLlmTurn) {
    return undefined;
  }

  if (plan.selectedAction === "recover") {
    if (params.expectedPathCount > 1) {
      return {
        route: "sessions_spawn",
        reason: "executive_recover_multi_target",
        selectedAction: plan.selectedAction,
        queueKind: plan.queueKind,
      };
    }
    if (params.preferredValidatedRoute?.route === "sessions_spawn") {
      return {
        route: "sessions_spawn",
        reason: "executive_recover_isolated_bias",
        selectedAction: plan.selectedAction,
        queueKind: plan.queueKind,
      };
    }
    return {
      route: params.matchedRecoverySuggestedRoute ?? "omega_delegate",
      reason: "executive_recover_single_target",
      selectedAction: plan.selectedAction,
      queueKind: plan.queueKind,
    };
  }

  if (
    plan.selectedAction === "direct_execute" &&
    plan.queueKind === "goal" &&
    params.expectedPathCount <= 1
  ) {
    return {
      route: params.preferredValidatedRoute?.route ?? "omega_delegate",
      reason: "executive_execute_validated_goal",
      selectedAction: plan.selectedAction,
      queueKind: plan.queueKind,
    };
  }

  return undefined;
}

export function decideOmegaWorkPolicyRoute(params: {
  isolated: boolean;
  runtime?: string;
  requiresValidation: boolean;
  expectedPathCount: number;
  interactionKind?: string;
  timeoutSeconds?: number;
  preferredValidatedRoute?: OmegaEmpiricalRoutingPreference;
}): OmegaWorkPolicyRoute {
  if (params.isolated || params.runtime === "acp") {
    return "sessions_spawn";
  }
  if (params.expectedPathCount > 1) {
    return "sessions_spawn";
  }
  if (
    params.requiresValidation &&
    params.expectedPathCount <= 1 &&
    params.preferredValidatedRoute &&
    params.preferredValidatedRoute.confidence >= OMEGA_STRONG_ROUTING_CONFIDENCE
  ) {
    return params.preferredValidatedRoute.route;
  }
  if (params.requiresValidation) {
    return "omega_delegate";
  }
  if (
    (params.interactionKind === "verification_request" ||
      params.interactionKind === "analysis_request" ||
      params.interactionKind === "corrective_feedback" ||
      params.interactionKind === "mixed_turn") &&
    typeof params.timeoutSeconds === "number" &&
    params.timeoutSeconds > 0
  ) {
    return "omega_delegate";
  }
  return "sessions_send";
}

export function chooseOmegaRecoveryRoute(params: {
  recovery: OmegaInterruptedGoalRecovery;
  recentStalledTurns: number;
  delegateStats?: { attempts: number; successes: number; failures: number };
  isolatedStats?: { attempts: number; successes: number; failures: number };
}): OmegaRecoveryRouteDecision {
  if (
    params.delegateStats &&
    params.delegateStats.attempts >= 2 &&
    params.delegateStats.successes > params.delegateStats.failures &&
    (!params.isolatedStats || params.delegateStats.successes >= params.isolatedStats.successes)
  ) {
    return { route: "omega_delegate", reason: "empirical_delegate_bias" };
  }
  if (
    params.isolatedStats &&
    params.isolatedStats.attempts >= 2 &&
    params.isolatedStats.successes > params.isolatedStats.failures &&
    (!params.delegateStats || params.isolatedStats.successes > params.delegateStats.successes)
  ) {
    return { route: "sessions_spawn", reason: "empirical_isolation_bias" };
  }
  if (params.recovery.suggestedRoute === "omega_delegate") {
    return params.recentStalledTurns >= 1
      ? { route: "sessions_spawn", reason: "stalled_recently" }
      : { route: "omega_delegate", reason: "suggested_delegate" };
  }

  if (params.recovery.expectsJson && params.recovery.remainingTargets.length === 0) {
    return params.recentStalledTurns >= 1
      ? { route: "sessions_spawn", reason: "structured_contract_repair" }
      : { route: "omega_delegate", reason: "suggested_delegate" };
  }

  if (
    params.recovery.remainingTargets.length === 1 &&
    params.recentStalledTurns === 0 &&
    params.recovery.failureStreak <= 1
  ) {
    return { route: "omega_delegate", reason: "single_target_local_retry" };
  }

  if (params.recovery.remainingTargets.length > 1) {
    return { route: "sessions_spawn", reason: "multi_target_write_repair" };
  }

  return params.recentStalledTurns >= 1
    ? { route: "sessions_spawn", reason: "stalled_recently" }
    : { route: "sessions_spawn", reason: "suggested_isolation" };
}