Files
loki/scripts/test/autopilot-preamble.ts
CatoandClaude Opus 5 3ba22707cc fix(activity): the feed spent its whole preview on a note to the agent (#808)
Three consecutive "Needs you" rows on prod, 2026-09-20 — three different runs
— each showing the same sentence, cut at the same word:

  Partial with no recorded reason — asked: Work on the project at
  /home/g/dev/loki. [autopilot · loop=next_best — this prompt was auto-injected
  by the local dispatch loop, NOT typed by a human. Treat it as a
  regularly-scheduled review task; if the conversation seems off, suspect th…

That header is written in config/prompt-library.ts and addressed to the AGENT.
It is ~230 characters; PREVIEW_MAX is 240. So the preview was the header and
nothing else, the three rows were indistinguishable, and the reader never
reached a word of what was actually asked.

Two things wrong, both fixed:

- The header is stripped from the PREVIEW, so the row shows the ask.
- "asked:" is a claim about who asked, and for these rows nobody did — the
  prompt's own first sentence says it was not typed by a human. An autopilot
  row now names its loop instead of implying the operator wrote it.

This is what `activity-status.ts` already exists to do: stripHarnessScaffolding
and extractOperatorTask are there so a feed shows "exactly the human ask". The
autopilot header was simply a gap in that set.

THE DISTINCTION THAT MATTERS: stripped from `preview` only. `task` is what a
re-dispatch sends and `full` is what the agent received — removing the header
there would re-run the loop's work without the instruction telling the agent a
loop sent it ("suspect the loop, not the human"). The test pins that both ways,
and mutation proves it: stripping `task` too fails the suite, and not stripping
the preview fails it.

verify: format:check, tsc, lint (0 errors, 2 pre-existing warnings),
check:design (11), check:desktop, test:home. test:unit 216/218 —
activity-time-zone-stable and telegram-allowlist need a live local server and
fail identically on pristine origin/main; this diff touches neither.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-20 10:07:21 -07:00

101 lines
4.4 KiB
TypeScript

/**
* The Activity feed spent its whole preview budget on a note to the agent.
*
* `config/prompt-library.ts` heads every autopilot template with
* `[autopilot · loop=next_best — this prompt was auto-injected by the local
* dispatch loop, NOT typed by a human …]`. That header is ~230 characters and
* PREVIEW_MAX is 240, so the preview was the header and nothing else.
*
* Seen on prod 2026-09-20: three consecutive "Needs you" rows, three different
* runs, each showing the same sentence cut at "suspect th…". The rows were
* indistinguishable and the reader never reached a word of the actual ask —
* under a label reading "asked:", for prompts whose own first sentence says no
* human asked.
*
* Run: npx tsx scripts/test/autopilot-preamble.ts
*/
import { readAutopilotLoop, stripAutopilotPreamble, promptDisplay } from "@/lib/activity-status";
let failures = 0;
function check(name: string, fn: () => void) {
try {
fn();
console.log(` ✓ ${name}`);
} catch (err) {
failures++;
console.log(` ✗ ${name}`);
console.log(` ${err instanceof Error ? err.message : String(err)}`);
}
}
function assert(cond: boolean, msg: string) {
if (!cond) throw new Error(msg);
}
const PREAMBLE =
"[autopilot · loop=next_best — this prompt was auto-injected by the local dispatch loop, " +
"NOT typed by a human. Treat it as a regularly-scheduled review task; if the conversation " +
"seems off, suspect the loop, not the human.]";
const REAL_ASK = "You are the Loki Autopilot. Pick the single highest-impact next action.";
console.log("autopilot-preamble:");
check("the loop name is recovered", () => {
assert(readAutopilotLoop(PREAMBLE) === "next_best", readAutopilotLoop(PREAMBLE) ?? "null");
assert(readAutopilotLoop(`[autopilot · loop=quality — x]`) === "quality", "quality");
assert(readAutopilotLoop(`[autopilot · loop=test_and_fix — x]`) === "test_and_fix", "underscore");
});
check("a human-typed prompt reports no loop", () => {
// Must not fire on ordinary text, or every row claims to be autopilot.
assert(readAutopilotLoop("Fix the login bug") === null, "plain text");
assert(readAutopilotLoop("[not an autopilot header]") === null, "other bracket");
});
check("stripping leaves the actual ask", () => {
const out = stripAutopilotPreamble(`${PREAMBLE}\n\n${REAL_ASK}`);
assert(!out.includes("auto-injected"), "header must be gone");
assert(out.includes("highest-impact next action"), `real ask must survive: ${out.slice(0, 60)}`);
assert(!out.startsWith(" "), "no leading gap");
});
check("the header is stripped even mid-string", () => {
// The recovered task reads "Work on the project at /home/g/dev/loki." and the
// template's header follows it — which is exactly how prod rendered it.
const out = stripAutopilotPreamble(
`Work on the project at /home/g/dev/loki. ${PREAMBLE} ${REAL_ASK}`,
);
assert(out.startsWith("Work on the project"), `lead kept: ${out.slice(0, 40)}`);
assert(!out.includes("NOT typed by a human"), "header gone");
assert(out.includes("highest-impact"), "tail kept");
});
check("text without a header passes through untouched", () => {
assert(stripAutopilotPreamble(REAL_ASK) === REAL_ASK, "verbatim");
});
check("the preview loses the header but task and full KEEP it", () => {
// THE DISTINCTION THAT MATTERS. `task` is what a re-dispatch sends: stripping
// the header there would re-run the loop's work without the instruction that
// tells the agent a loop sent it ("suspect the loop, not the human").
const raw = `${PREAMBLE}\n\n${REAL_ASK}`;
const d = promptDisplay({ customPrompt: raw, resolvedPrompt: null, intent: "custom" });
assert(!d.preview.includes("auto-injected"), "preview is clean");
assert(d.preview.includes("highest-impact"), `preview shows the ask: ${d.preview.slice(0, 50)}`);
assert((d.task ?? "").includes("auto-injected"), "task KEEPS the header");
assert((d.full ?? "").includes("auto-injected"), "full KEEPS the header");
assert(d.autopilotLoop === "next_best", `loop surfaced: ${d.autopilotLoop}`);
});
check("a human prompt still reads as asked, not as autopilot", () => {
const d = promptDisplay({
customPrompt: "Fix the login bug",
resolvedPrompt: null,
intent: "custom",
});
assert(d.autopilotLoop === null, "no loop");
assert(d.preview === "Fix the login bug", `preview: ${d.preview}`);
});
console.log(failures === 0 ? "\nall passed" : `\n${failures} failed`);
process.exit(failures === 0 ? 0 : 1);