okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221merge f-split-agent3 c99896ae6 (status, goal and time-skip events and backend, time-skip keepalive and stop, atomic.py) into f-split-intf9bfe8bc1

main
2.7 KiB58 linesraw
1#!/usr/bin/env node
2/**
3 * One revision attempt for Time-skip, performed by the app's own assistant.
4 *
5 * Strata Control runs this as its `--revise-command`, with the candidate folder (an isolated copy of the user's work) as the working
6 * directory, the request in STRATA_TIMESKIP_PROMPT and the attempt number in STRATA_TIMESKIP_ATTEMPT. It asks the host (which owns the
7 * model and the Harness) for one agent turn inside that folder and returns when the turn ends. Exit 0 means "an attempt was made", not
8 * "it helped": whether it helped is decided by the checks and, failing those, by the person.
9 *
10 * The host answers with a STREAM of one-line JSON messages (`beat`, `activity`, then `result` or `error`) and sends the first one at once.
11 * A turn on a slow computer can take many minutes with nothing to say; an answer that sends no byte for five minutes is abandoned by
12 * Node's fetch, which is how a Time-skip used to end with "The assistant could not be reached".
13 */
14const base = process.env.STRATA_HOST_URL;
15const secret = process.env.STRATA_INTERNAL_SECRET;
16const prompt = process.env.STRATA_TIMESKIP_PROMPT ?? '';
17const attempt = Number(process.env.STRATA_TIMESKIP_ATTEMPT ?? 1);
18
19if (!base || !secret || !prompt) {
20  console.error('The revision step needs the Strata app to be running.');
21  process.exit(2);
22}
23
24try {
25  const res = await fetch(`${base}/api/internal/timeskip-revise`, {
26    method: 'POST',
27    headers: { 'content-type': 'application/json', 'x-strata-internal': secret },
28    body: JSON.stringify({ cwd: process.cwd(), prompt, attempt }),
29  });
30  if (!res.ok || !res.body) {
31    const body = await res.json().catch(() => ({}));
32    console.error(body.error ?? `The assistant could not make the change (HTTP ${res.status}).`);
33    process.exit(1);
34  }
35  const reader = res.body.getReader();
36  const decoder = new TextDecoder();
37  let buffer = '';
38  let result = null, failure = null;
39  const take = line => {
40    if (!line.trim()) return;
41    let message;
42    try { message = JSON.parse(line); } catch { return; }
43    if (message.type === 'result') result = String(message.text ?? '').trim();
44    else if (message.type === 'error') failure = String(message.message ?? 'The assistant could not make the change.');
45  };
46  for (;;) {
47    const { value, done } = await reader.read();
48    if (value) { buffer += decoder.decode(value, { stream: true }); const lines = buffer.split('\n'); buffer = lines.pop() ?? ''; lines.forEach(take); }
49    if (done) break;
50  }
51  take(buffer);
52  if (failure) { console.error(failure); process.exit(1); }
53  if (result === null) { console.error('The assistant stopped before it finished.'); process.exit(1); }
54  console.log(result);
55} catch (e) {
56  console.error(`The assistant could not be reached: ${e.message}`);
57  process.exit(1);
58}