File size: 10,078 Bytes
a36db1b
 
 
 
 
 
 
 
 
 
 
e2f4da8
 
 
c47715e
a36db1b
 
e2f4da8
 
a36db1b
e2f4da8
a36db1b
 
 
 
 
e03ae4e
a36db1b
 
 
 
 
 
 
e2f4da8
 
a36db1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e03ae4e
 
a36db1b
 
 
 
 
 
e03ae4e
 
 
a36db1b
e03ae4e
 
 
 
 
 
 
a36db1b
 
 
 
 
 
 
c47715e
a36db1b
 
 
 
c47715e
a36db1b
 
 
 
 
 
 
 
 
 
 
 
 
 
e03ae4e
 
 
a36db1b
 
 
 
 
e2f4da8
 
a36db1b
 
 
 
 
 
 
 
 
 
e03ae4e
 
 
 
 
a36db1b
 
 
 
 
 
 
 
 
 
 
 
e2f4da8
a36db1b
 
e2f4da8
a36db1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e03ae4e
 
 
a36db1b
 
 
 
 
 
 
 
 
 
e2f4da8
 
 
a36db1b
 
 
e2f4da8
a36db1b
 
 
 
 
 
e2f4da8
a36db1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2f4da8
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"use client";

import { useState, useEffect, useMemo, useCallback, useRef } from "react";
import type {
  ViewMode, TaskType, ActionType, AutoPolicy,
  StepResult, EventItem, EvaluationData,
  Observation,
} from "../lib/types";

/* ── helpers ──────────────────────────────────────────── */

const API_BASE = typeof window !== "undefined" 
  ? (process.env.NEXT_PUBLIC_API_URL || (window.location.port === "3000" || window.location.port === "3458" ? "http://127.0.0.1:7860" : ""))
  : "";

function bestSpec(obs: Observation | null): string {
  if (!obs) return "S0";
  const ids = obs.available_specialists || obs.available_workers || [];
  return [...ids].sort(
    (a, b) => (obs.trust_snapshot[b] ?? 0.5) - (obs.trust_snapshot[a] ?? 0.5),
  )[0] || "S0";
}

function heuristicMove(obs: Observation | null) {
  if (!obs) return { action: "delegate" as ActionType, specialist: "S0", trust: 0.5 };
  const sp = bestSpec(obs);
  const t = obs.trust_snapshot[sp] ?? 0.5;
  if (obs.stakes_level >= 0.7 && t < 0.65)
    return { action: "verify" as ActionType, specialist: sp, trust: t };
  return { action: "delegate" as ActionType, specialist: sp, trust: t };
}

function randomMove(obs: Observation | null) {
  if (!obs) return { action: "delegate" as ActionType, specialist: "S0", trust: 0.5 };
  const ids = obs.available_specialists || obs.available_workers || [];
  const sp = ids[Math.floor(Math.random() * ids.length)] || "S0";
  return { action: "delegate" as ActionType, specialist: sp, trust: obs.trust_snapshot[sp] ?? 0.5 };
}

type ReplayRow = {
  task_type: TaskType;
  seed: number;
  step: number;
  action: {
    action_type: ActionType;
    specialist_id?: string | null;
    reasoning?: string;
  };
};

function replayMove(
  obs: Observation | null,
  seed: number,
  replay: Map<string, ReplayRow>,
) {
  if (!obs) return heuristicMove(obs);
  const key = `${obs.task_type}:${seed}:${obs.step_count}`;
  const row = replay.get(key);
  if (!row) return { ...heuristicMove(obs), replayMiss: true };
  const action = row.action.action_type;
  const specialist = row.action.specialist_id || bestSpec(obs);
  return {
    action,
    specialist,
    trust: obs.trust_snapshot[specialist] ?? 0.5,
    replayMiss: false,
  };
}

function outcomeOf(reason: string): EventItem["outcome"] {
  const r = reason.toLowerCase();
  if (r.includes("poison") || r.includes("adversarial")) return "poisoned";
  if (r.includes("block") || r.includes("verif")) return "blocked";
  if (r.includes("skip")) return "skipped";
  return "success";
}

/* ── hook ─────────────────────────────────────────────── */

export function useSentinel() {
  const [view, setView] = useState<ViewMode>("landing");
  const [taskType, setTaskType] = useState<TaskType>("task3");
  const [seed, setSeed] = useState(42);
  const [sessionId, setSessionId] = useState<string | null>(null);
  const [result, setResult] = useState<StepResult | null>(null);
  const [running, setRunning] = useState(false);
  const [events, setEvents] = useState<EventItem[]>([]);
  const [lastReq, setLastReq] = useState<Record<string, unknown> | null>(null);
  const [lastRes, setLastRes] = useState<Record<string, unknown> | null>(null);
  const [evaluation, setEval] = useState<EvaluationData | null>(null);
  const [replay, setReplay] = useState<Map<string, ReplayRow>>(new Map());
  const [prevTrust, setPrevTrust] = useState<Record<string, number>>({});
  const [activeSpec, setActiveSpec] = useState<string | null>(null);

  const abortRef = useRef(false);

  /* load evaluation data once */
  useEffect(() => {
    fetch(`${API_BASE}/assets/evaluation_results.json`)
      .then((r) => r.json())
      .then(setEval)
      .catch(() => null);

    fetch(`${API_BASE}/assets/trained_policy_replay.jsonl`)
      .then((r) => r.ok ? r.text() : "")
      .then((txt) => {
        const table = new Map<string, ReplayRow>();
        for (const line of txt.split("\n")) {
          if (!line.trim()) continue;
          const row = JSON.parse(line) as ReplayRow;
          table.set(`${row.task_type}:${row.seed}:${row.step}`, row);
        }
        setReplay(table);
      })
      .catch(() => null);
  }, []);

  const observation = result?.observation ?? null;
  const info = result?.info;
  const reward = result?.reward;
  const done = result?.done ?? false;

  /* trust deltas */
  const trustDeltas = useMemo(() => {
    if (!observation) return {};
    const d: Record<string, number> = {};
    const ids = observation.available_specialists || observation.available_workers || [];
    for (const id of ids) {
      d[id] = (observation.trust_snapshot[id] ?? 0.5) - (prevTrust[id] ?? 0.5);
    }
    return d;
  }, [observation, prevTrust]);

  const recommended = useMemo(() => heuristicMove(observation), [observation]);

  const proof = useMemo(() => {
    if (!evaluation) return null;
    return {
      random: evaluation.summary.random,
      heuristic: evaluation.summary.heuristic,
      oracle: evaluation.summary.oracle_lite,
      trained: evaluation.summary.trained,
      task3Random: evaluation.by_task.task3.random,
      task3Heuristic: evaluation.by_task.task3.heuristic,
    };
  }, [evaluation]);

  /* ── API calls ──────────────────────────────────────── */

  const resetEpisode = useCallback(
    async (nextTask?: TaskType, nextSeed?: number): Promise<StepResult | null> => {
      const t = nextTask ?? taskType;
      const s = nextSeed ?? seed;
      setRunning(true);
      abortRef.current = false;
      const payload = { task_type: t, seed: s, mode: "cluster" };
      setLastReq({ method: "POST", path: "/reset", body: payload });
      try {
        const res = await fetch(`${API_BASE}/reset`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify(payload),
        });
        const data = (await res.json()) as StepResult;
        setPrevTrust({});
        setResult(data);
        setLastRes(data as unknown as Record<string, unknown>);
        setSessionId(data.info.session_id);
        setActiveSpec(null);
        setEvents([{
          step: 0, action: "reset", summary: "Episode initialized. Hidden profiles reshuffled.",
          reward: 0, outcome: "reset",
        }]);
        return data;
      } finally {
        setRunning(false);
      }
    },
    [taskType, seed],
  );

  const stepEpisode = useCallback(
    async (
      action: ActionType,
      specialistOverride?: string,
      ctx?: StepResult | null,
    ): Promise<StepResult | null> => {
      const active = ctx ?? result;
      const obs = active?.observation ?? observation;
      const sid = active?.info.session_id ?? sessionId;
      if (!sid || !obs || running || active?.done) return null;

      setRunning(true);
      const specialist =
        action === "delegate" || action === "verify"
          ? specialistOverride || bestSpec(obs)
          : null;

      setActiveSpec(specialist);

      const isCluster = active?.info?.environment_mode === "cluster" || sessionId === "cluster";
      const mappedAction = (isCluster && action === "delegate") ? "allocate" : action;

      const payload = {
        session_id: sid,
        task_type: obs.task_type,
        action_type: mappedAction,
        specialist_id: specialist,
        subtask_response: action === "solve_independently" ? "SELF_SOLVED" : null,
        reasoning: `ui-${action}${specialist ? `-${specialist}` : ""}`,
      };
      setLastReq({ method: "POST", path: `/step?session_id=${sid}`, body: payload });
      try {
        const res = await fetch(`${API_BASE}/step?session_id=${encodeURIComponent(sid)}`, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify(payload),
        });
        const data = (await res.json()) as StepResult;
        setPrevTrust(obs.trust_snapshot);
        setResult(data);
        setLastRes(data as unknown as Record<string, unknown>);
        setEvents((prev) => [
          ...prev,
          {
            step: data.info.step_count,
            action: action,
            specialist: specialist ?? undefined,
            summary: data.reward.reason,
            reward: data.reward.value,
            outcome: outcomeOf(data.reward.reason),
          },
        ]);
        return data;
      } finally {
        setRunning(false);
      }
    },
    [result, observation, sessionId, running],
  );

  const autoRun = useCallback(
    async (policy: AutoPolicy) => {
      abortRef.current = false;
      let local = result;
      if (!local || local.done) {
        local = await resetEpisode();
      }
      let guard = 0;
      while (!local?.done && guard < 70 && !abortRef.current) {
        const obs = local?.observation ?? null;
        const mv =
          policy === "random"
            ? randomMove(obs)
            : policy === "trained"
              ? replayMove(obs, seed, replay)
              : heuristicMove(obs);
        const next = await stepEpisode(mv.action, mv.specialist, local);
        guard += 1;
        await new Promise((r) => setTimeout(r, 140));
        if (!next) break;
        local = next;
      }
    },
    [result, resetEpisode, stepEpisode, replay, seed],
  );

  const stopAutoRun = useCallback(() => { abortRef.current = true; }, []);

  const swapProfiles = useCallback(async () => {
    const ns = seed + 1;
    setSeed(ns);
    await resetEpisode(taskType, ns);
  }, [seed, taskType, resetEpisode]);

  return {
    view, setView,
    taskType, setTaskType,
    seed, setSeed,
    sessionId, observation, info, reward, result, running, done,
    events, lastReq, lastRes, evaluation, proof,
    prevTrust, trustDeltas, recommended, activeSpec,
    resetEpisode, stepEpisode, autoRun, stopAutoRun, swapProfiles,
  };
}