File size: 4,347 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
import {
  loadOpenSkynetInternalProjectProfile,
  type OpenSkynetInternalProjectProfile,
} from "../omega/internal-project.js";
import { syncOpenSkynetLivingMemory, type OpenSkynetLivingState } from "../omega/living-memory.js";
import { loadOmegaWorldModelSnapshot, type OmegaWorldModelSnapshot } from "../omega/world-model.js";
import {
  syncSkynetCommitmentDecision,
  type SkynetCommitmentDecision,
} from "./commitment-engine.js";
import { syncSkynetExperimentPlan, type SkynetExperimentPlan } from "./experiment-cycle.js";

export type OpenSkynetRuntimeAuthority = {
  sessionKey: string;
  project: OpenSkynetInternalProjectProfile;
  snapshot: OmegaWorldModelSnapshot;
  recommendedAction: string;
  experimentPlan?: SkynetExperimentPlan;
  commitment?: SkynetCommitmentDecision;
  livingState: OpenSkynetLivingState;
  degradedComponents: Array<{
    component: string;
    reason: string;
  }>;
};

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

export function deriveOpenSkynetRecommendedAction(params: {
  focusTitle?: string;
  nucleusMode?: string;
  continuityScore?: number;
  topWorkItem?: string;
}): string {
  if (typeof params.continuityScore === "number" && params.continuityScore < 0.5) {
    return "Re-establish continuity before expanding the study scope.";
  }
  if (params.nucleusMode === "reframe") {
    return "Produce a structural reframe before insisting on the same work item.";
  }
  if (params.topWorkItem) {
    return `Execute or refine the top work item: ${params.topWorkItem}`;
  }
  if (params.focusTitle) {
    return `Advance the active focus: ${params.focusTitle}`;
  }
  return "No active internal project focus detected.";
}

export async function syncOpenSkynetRuntimeAuthority(params: {
  workspaceRoot: string;
  sessionKey: string;
  task?: string;
  expectedPaths?: string[];
  watchedPaths?: string[];
}): Promise<OpenSkynetRuntimeAuthority> {
  const project = await loadOpenSkynetInternalProjectProfile(params.workspaceRoot);
  const snapshot = await loadOmegaWorldModelSnapshot({
    workspaceRoot: params.workspaceRoot,
    sessionKey: params.sessionKey,
    task: params.task,
    expectedPaths: params.expectedPaths,
    watchedPaths: params.watchedPaths,
  });
  const degradedComponents = [...snapshot.degradedComponents];

  const nucleus = snapshot.internalProjectNucleus;
  const program = snapshot.internalProjectStudyProgram;
  const continuity = snapshot.internalProjectContinuity;
  const experimentPlan =
    nucleus && program
      ? await captureRuntimeComponent("skynet_experiment_plan", degradedComponents, () =>
          syncSkynetExperimentPlan({
            workspaceRoot: params.workspaceRoot,
            sessionKey: params.sessionKey,
            nucleus,
            program,
            continuity,
          }),
        )
      : undefined;
  const commitment =
    experimentPlan && nucleus && program
      ? await captureRuntimeComponent("skynet_commitment", degradedComponents, () =>
          syncSkynetCommitmentDecision({
            workspaceRoot: params.workspaceRoot,
            sessionKey: params.sessionKey,
            nucleus,
            program,
            experiment: experimentPlan,
            continuity,
          }),
        )
      : undefined;

  const recommendedAction = deriveOpenSkynetRecommendedAction({
    focusTitle: snapshot.studySupervisor?.focus.title,
    nucleusMode: snapshot.internalProjectNucleus?.mode,
    continuityScore: snapshot.internalProjectContinuity?.continuityScore,
    topWorkItem: snapshot.internalProjectStudyProgram?.items[0]?.title,
  });
  const livingState = await syncOpenSkynetLivingMemory({
    workspaceRoot: params.workspaceRoot,
    sessionKey: params.sessionKey,
    snapshot,
    recommendedAction,
    commitment,
    experiment: experimentPlan,
  });

  return {
    sessionKey: params.sessionKey,
    project,
    snapshot,
    recommendedAction,
    experimentPlan,
    commitment,
    livingState,
    degradedComponents,
  };
}