File size: 4,033 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
import {
  deriveOmegaInterruptedGoalRecovery,
  OMEGA_AUTONOMOUS_RECOVERY_MAX_FAILURE_STREAK,
} from "../recovery.js";
import type { OmegaSelfTimeKernelState } from "../self-time-kernel.js";
import {
  deriveFocusedActiveTargets,
  deriveShadowedGoalTasks,
  deriveSupersededGoalTasks,
} from "../session-context.js";

export type OmegaWakeAction =
  | {
      kind: "heartbeat_ok";
      reason: string;
    }
  | {
      kind: "review_active_goal";
      reason: string;
      goalTask: string;
    }
  | {
      kind: "resume_interrupted_goal";
      reason: string;
      goalTask: string;
      goalTargets: string[];
      failureStreak: number;
      suggestedRoute: "omega_delegate" | "sessions_spawn";
      errorKind?: string;
    }
  | {
      kind: "abort_interrupted_goal";
      reason: string;
      goalTask: string;
      goalTargets: string[];
      failureStreak: number;
      suggestedRoute: "omega_delegate" | "sessions_spawn";
      errorKind?: string;
    }
  | {
      kind: "prune_stale_goals";
      reason: string;
      goalTasks: string[];
    }
  | {
      kind: "prune_superseded_goals";
      reason: string;
      goalTasks: string[];
    }
  | {
      kind: "focus_active_goal_targets";
      reason: string;
      goalTask: string;
      goalTargets: string[];
    }
  | {
      kind: "prune_shadowed_goals";
      reason: string;
      goalTasks: string[];
    };

export function decideOmegaWakeAction(params: {
  kernel?: OmegaSelfTimeKernelState;
}): OmegaWakeAction {
  const kernel = params.kernel;
  if (!kernel) {
    return {
      kind: "heartbeat_ok",
      reason: "no_kernel_state",
    };
  }

  const supersededGoalTasks = deriveSupersededGoalTasks(kernel);
  if (supersededGoalTasks.length > 0) {
    return {
      kind: "prune_superseded_goals",
      reason: "superseded_goal_gc_due",
      goalTasks: supersededGoalTasks,
    };
  }

  const shadowedGoalTasks = deriveShadowedGoalTasks(kernel);
  if (shadowedGoalTasks.length > 0) {
    return {
      kind: "prune_shadowed_goals",
      reason: "shadowed_goal_gc_due",
      goalTasks: shadowedGoalTasks,
    };
  }

  const focusedTargets = deriveFocusedActiveTargets(kernel);
  if (focusedTargets.length > 0 && kernel.activeGoalId) {
    const activeGoal = kernel.goals.find((goal) => goal.id === kernel.activeGoalId);
    if (activeGoal) {
      return {
        kind: "focus_active_goal_targets",
        reason: "verified_partial_progress_detected",
        goalTask: activeGoal.task,
        goalTargets: focusedTargets,
      };
    }
  }

  const recovery = deriveOmegaInterruptedGoalRecovery({ kernel });
  if (recovery) {
    if (recovery.failureStreak > OMEGA_AUTONOMOUS_RECOVERY_MAX_FAILURE_STREAK) {
      return {
        kind: "abort_interrupted_goal",
        reason: "failure_streak_too_high",
        goalTask: recovery.goalTask,
        goalTargets: recovery.remainingTargets,
        failureStreak: recovery.failureStreak,
        suggestedRoute: recovery.suggestedRoute,
        errorKind: recovery.lastErrorKind,
      };
    }
    return {
      kind: "resume_interrupted_goal",
      reason: recovery.reason,
      goalTask: recovery.goalTask,
      goalTargets: recovery.remainingTargets,
      failureStreak: recovery.failureStreak,
      suggestedRoute: recovery.suggestedRoute,
      errorKind: recovery.lastErrorKind,
    };
  }

  if (kernel.tension.pendingCorrection && kernel.activeGoalId) {
    const activeGoal = kernel.goals.find((goal) => goal.id === kernel.activeGoalId);
    if (activeGoal) {
      return {
        kind: "review_active_goal",
        reason: "verified_failure_requires_followup",
        goalTask: activeGoal.task,
      };
    }
  }

  const staleGoals = kernel.goals.filter((goal) => goal.status === "stale");
  if (staleGoals.length > 0) {
    return {
      kind: "prune_stale_goals",
      reason: "stale_goal_gc_due",
      goalTasks: staleGoals.map((goal) => goal.task),
    };
  }

  return {
    kind: "heartbeat_ok",
    reason: "no_verified_tension",
  };
}