Files
loki/scripts/test/run-outcome-format.ts
CatoandClaude Fable 5.2 64e2c6b543 fix(builder, runs): a held task waits minutes and says so; a build says when it is live (#1663)
* docs(handoff): checkpoint for building with Loki and with a Cat bot

The objective, the two journeys that count as done, how production can be
reached from an agent session, and the state when the work began.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* docs(handoff): what the investigation found, and what it did not reach

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(builder): a task held for the runner's update waits minutes, not for ever, and says so

While the cloud runner is behind the deploy the commands route hands it no
work-starting command, so the idle moment its restart waits for comes. The
hold had no end, and on 2026-11-12 the restart did not come: two runner
releases landed 35 minutes apart, the drain read "5 agent(s) working" on
every look for an hour, and the builder claimed screen peeks and nothing
else (Sealed builds run 43165605499). Every task a person sent sat queued
while the chat said a runner would claim it shortly.

- runner-update-hold: a held command is handed over once it has waited
  five minutes, and the hold applies only within two hours of the newest
  runner release. Running on last week's runner is recoverable; a task
  that never starts is not.
- claimNextPendingCommand takes the overdue rule. While holding, an empty
  at-once list claims nothing (it used to claim everything).
- The person is told why and for how long: in the chat reply, in the
  /api/inject answer (builderUpdating, message) and as the run track's
  headline while the job is unclaimed.
- loki-loop-check.yml: the end-to-end check of the live site on demand,
  with an optional real no-change task that must come back into its
  thread. loki-e2e-check.sh --report takes the project, the wait and the
  dispatch choice from its caller.
- sealed-builds.yml mode=activity: read-only, what each agent process of
  the runner read and wrote over a minute, so the drain's "working" can be
  measured for sessions it cannot read a status for.
- pre-push: the watchdog's sleep no longer holds the caller's output open
  for fifteen minutes after the push has landed.

Server and box scripts only: no runner code, no runner version.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(runs): a build tells the truth after the agent finishes, and says when it is live

A task sent from the chat ended at "done, pull request opened". What it
said next was wrong in several ways, and the one thing a person waits for,
that it went live, was said to nobody.

- The "done" message reads the pull request the runner itself reported
  (payload.prUrl from closeRunShipped, read by nothing until now) and asks
  GitHub with the token that can read the repository. The person's own
  OAuth token is refused on the organisation's repositories, so a run that
  shipped could be posted as "No pull request or push was found". The
  reaper's evidence lookup asks the same way.
- "Live" is read from the site: a merged change is live when the commit in
  the site's /.well-known/fleet-release.json is the merge or comes after
  it (feedback/served-release). Workflow-run labels are wrong on this
  fleet: a Deploy run is labelled with main's tip at its start, a batched
  merge has no Deploy run of its own, and OrangeCat's deploy is named CD.
  It only upgrades; with no receipt the workflow-run verdict stands.
- A chat build is followed to live on the fifteen-minute cron that already
  follows feedback fixes (run-follow-notify): on the change to live, or to
  a failed deploy, one line in the thread the task came from and one push.
  GitHub and the release receipt only, no model. Old news is not announced.
- A task that waits behind the project's earlier run says so in the chat,
  the /api/inject answer (queuedBehind, message) and the track, instead of
  "will claim it shortly".
- Isolated runs ship through a pull request: worktreePromptNote told every
  lane and one-off session to push straight to main.
- The close message no longer prints "Live: <address>" beside a pull
  request that has not merged.

Server only, plus one prompt line the runner reads at its next start. No
file under desktop/, no runner version.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 09:48:29 +01:00

183 lines
7.2 KiB
TypeScript

/**
* Unit tests for the run → conversation write-back (pure half).
*
* Contract: a run that carries the conversation it came from gets a message
* back into that thread on close, naming the pull request that will merge on
* its own (or the absence of one), and the live URL. Runs without a
* conversation, or not yet closed, produce nothing.
*
* Run: npx tsx scripts/test/run-outcome-format.ts
*/
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { formatRunOutcomeMessage } from "@/lib/orchestration/run-outcome-format";
import { reportedPrEvidence } from "@/lib/repo-evidence";
import { ORCHESTRATION_OUTCOME } from "@/db/schema/orchestration-runs";
const base = {
id: "run-1",
projectKey: "loki",
outcome: ORCHESTRATION_OUTCOME.SUCCESS as string | null,
finishedAt: new Date("2026-09-14T00:00:00Z") as Date | null,
payload: { conversationId: "conv-1", resultText: "Renamed the product; PR opened." } as Record<
string,
unknown
>,
};
let pass = 0;
function check(name: string, fn: () => void) {
fn();
pass++;
console.log(` ✓ ${name}`);
}
check("no conversation → nothing to say", () => {
assert.equal(formatRunOutcomeMessage({ ...base, payload: {} } as never), null);
});
check("not closed yet → nothing to say", () => {
assert.equal(formatRunOutcomeMessage({ ...base, finishedAt: null } as never), null);
});
check("a successful run with a PR names it and says it merges on its own", () => {
const m = formatRunOutcomeMessage(base as never, {
evidence: { kind: "pr", url: "https://github.com/bitbaum/loki/pull/700", title: "x" },
liveUrl: "https://loki.orangecat.ch",
});
assert.ok(m);
assert.match(m.content, /✅ \*\*loki\*\* — done\./);
assert.match(m.content, /Renamed the product; PR opened\./);
assert.match(m.content, /Pull request: https:\/\/github\.com\/bitbaum\/loki\/pull\/700/);
assert.match(m.content, /merges on its own once its checks are green/);
assert.match(
m.content,
/The site: https:\/\/loki\.orangecat\.ch \(the change is there after the merge and the deploy\)/,
);
assert.doesNotMatch(m.content, /^Live:/m, "a pull request is not yet live");
assert.equal(m.meta.prUrl, "https://github.com/bitbaum/loki/pull/700");
assert.equal(m.meta.runId, "run-1");
});
check("a push without a PR is named as something that will NOT merge", () => {
const m = formatRunOutcomeMessage(base as never, {
evidence: { kind: "push", url: "https://github.com/bitbaum/loki/tree/feat-x", title: "x" },
});
assert.ok(m);
assert.match(m.content, /Pushed: /);
assert.match(m.content, /nothing will merge by itself/);
assert.equal(m.meta.prUrl, null);
});
check("success with no evidence at all is flagged, not celebrated", () => {
const m = formatRunOutcomeMessage(base as never);
assert.ok(m);
assert.match(m.content, /No pull request or push was found/);
});
check("evidence already on the run payload (reaper) is used", () => {
const m = formatRunOutcomeMessage({
...base,
outcome: ORCHESTRATION_OUTCOME.PARTIAL,
payload: {
conversationId: "conv-1",
evidence: {
kind: "pr",
url: "https://github.com/bitbaum/loki/pull/701",
title: "t",
atMs: 1,
},
},
} as never);
assert.ok(m);
assert.match(m.content, /🟡 \*\*loki\*\* — partly done\./);
assert.equal(m.meta.prUrl, "https://github.com/bitbaum/loki/pull/701");
});
check("a failed run reports the error text and the outcome", () => {
const m = formatRunOutcomeMessage({
...base,
outcome: "timeout",
payload: { conversationId: "conv-1", error: "Timed out — run exceeded maximum duration" },
} as never);
assert.ok(m);
assert.match(m.content, /❌ \*\*loki\*\* — did not finish \(timeout\)\./);
assert.match(m.content, /Timed out/);
});
check("the summary is capped so a wall of handoff text cannot flood the thread", () => {
const m = formatRunOutcomeMessage({
...base,
payload: { conversationId: "conv-1", resultText: "x".repeat(5000) },
} as never);
assert.ok(m);
assert.ok(m.content.length < 1200, `content is ${m.content.length} chars`);
});
check("a run closed from a session handoff carries the agent's own words", () => {
// The local PTY path — the one a person watches after typing into /loki —
// never sets payload.resultText: it closes from the handoff, and the words
// land in `summary`. This used to print a bare "🟡 partly done" and nothing
// else, which is the trip out of the product this message exists to prevent.
const m = formatRunOutcomeMessage({
...base,
outcome: "partial",
payload: { conversationId: "conv-1" },
summary: {
done: "e2e ok — no changes",
next: "Definition of done not yet met — include a committed and pushed change.",
tests: "not run (health probe)",
todos: "0",
health: "",
},
} as never);
assert.ok(m);
assert.match(m.content, /🟡 \*\*loki\*\* — partly done\./);
assert.match(m.content, /e2e ok — no changes/);
assert.match(m.content, /Next: Definition of done not yet met/);
});
check("a runner's own result text still wins over the handoff", () => {
const m = formatRunOutcomeMessage({
...base,
summary: { done: "handoff words", next: "", tests: "", todos: "", health: "" },
} as never);
assert.ok(m);
assert.match(m.content, /Renamed the product; PR opened\./);
assert.doesNotMatch(m.content, /handoff words/);
});
// ── Where the pull request comes from ────────────────────────────────────
// 2026-11-12: closeRunShipped stored the runner's own pull request address and
// nothing read it; the post then asked GitHub with the person's OAuth token,
// which the organisation's repositories refuse, and a run that shipped read
// "No pull request or push was found — the work may not have left the runner".
check("a pull request the runner reported is evidence without asking GitHub", () => {
const url = "https://github.com/bitbaum/loki/pull/1663";
assert.deepEqual(reportedPrEvidence({ prUrl: url }, 5), {
kind: "pr",
url,
title: "",
atMs: 5,
});
for (const bad of [null, {}, { prUrl: 7 }, { prUrl: "" }, { prUrl: "https://github.com/a/b" }])
assert.equal(reportedPrEvidence(bad, 5), null, `not a pull request: ${JSON.stringify(bad)}`);
const m = formatRunOutcomeMessage(base as never, {
evidence: reportedPrEvidence({ prUrl: url }, 5),
});
assert.ok(m);
assert.match(m.content, /Pull request: https:\/\/github\.com\/bitbaum\/loki\/pull\/1663/);
assert.doesNotMatch(m.content, /may not have left the runner/);
});
check("the post and the reaper ask GitHub with the token that can read the repository", () => {
const post = readFileSync("src/lib/orchestration/run-outcome-post.ts", "utf8");
assert.match(post, /evidence \?\?= reportedPrEvidence\(/, "the runner's report comes first");
assert.match(post, /repoTokenFor\(run\.userId, project\.gitUrl\)/);
const reap = readFileSync("src/lib/orchestration/reap-evidence.ts", "utf8");
assert.match(reap, /repoTokenFor\(run\.userId, gitUrl\)/);
assert.doesNotMatch(reap, /getGithubToken/, "never the person's own token alone");
});
console.log(`\nrun-outcome-format: ${pass} passed`);