Files
loki/scripts/test/orangecat-task-door.ts
CatoandClaude Opus 5.5 b376ead370 feat(orangecat): a bot follows a run to its answer, and asks a one-off in a fresh session (#1658)
* feat(orangecat): a bot follows a run to its answer, and asks a one-off in a fresh session

A bot that sent a task could not reliably read the answer:
- the runner streams a conversation only while someone watches it, so a read
  a minute later found nothing, or an older conversation passed off as current
- a read by project took the project's latest open run, so an answer in a
  separate session could come back as the shared session's conversation
- a one-off session closes when it reports, leaving nothing to ask for after
- /api/orangecat/task could not ask for the fresh one-off session (#1640)

Now, on /api/orangecat/sessions:
- runId + watch (seconds, <= 300): the bot holds that run's session as a
  viewer for that long (lib/integrations/orangecat-watch.ts), so the runner
  streams the conversation while the agent works
- runId + items: the last items of THAT run's own session (its lane or
  one-off tab), on that run's builder
- every row carries the agent's handoff once it reports (lib/run-handoff):
  the answer survives the session being closed
- a read with nobody viewing waits for a snapshot newer than its join; an
  older copy alone comes back marked live: false

On /api/orangecat/task: session "fresh" (and separateSession as its alias)
gives each project a one-off session; a busy project is no reason to refuse it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmsQh5BSKKaJXnf4KjU

* fix(orangecat): a signed task outside its window is refused; a run whose track fails is never read as idle

From review of the rail:
- /api/orangecat/task had no freshness window, so a captured signed request
  could be replayed to start agent sessions later. It now takes issuedAt and
  refuses one older than the read doors' window (optional until every
  OrangeCat sends it; OrangeCat sends it from now on)
- a sessions read whose run track failed to load reported the run as Idle
  with no runId, which a follower took for settled. Such a run is now said
  from its own row: working while open, done or failed once closed, with
  its handoff

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmsQh5BSKKaJXnf4KjU

* fix(orangecat): a follower's poll never reaches GitHub; a transcript read waits for the whole snapshot

From review:
- a bot following a run reads it every few seconds, and every runId read
  loaded the owner's full track, which looks at GitHub. poll: true reads the
  run's own facts and cached ledger only
- the runner posts a snapshot in batches of 100; a read returned on the
  first batch, so a long conversation came back without its tail. It now
  waits until the batches stop arriving

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmsQh5BSKKaJXnf4KjU

* fix(orangecat): a session's task says what was asked, not the cross-project preamble

From review: for a task sent to several projects, the row's one-line task
was the first line of the preamble the agent is given ('Cross-project task.
The same request below was sent to ...'). askedOf takes the preamble off;
a prompt without one comes back as it is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmsQh5BSKKaJXnf4KjU

---------

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

99 lines
3.9 KiB
TypeScript

import assert from "node:assert/strict";
import { createHmac } from "node:crypto";
/**
* The task door (/api/orangecat/task): what it refuses with nothing else
* configured, and the words a caller gets back.
*
* The door starts agents in a person's repositories, so the refusals that
* must hold before any database is reached are under test: no secret, no
* signature, a body that names a builder that does not exist. The sentences
* a person reads (a busy project, where a run is followed) are pure and
* pinned here too.
*
* Run: npx tsx scripts/test/orangecat-task-door.ts
*/
async function main(): Promise<void> {
console.log("orangecat task door:");
process.env.DATABASE_URL ||= "postgres://test:test@127.0.0.1:5432/test";
const { busySentence, runStatusUrl } = await import("../../src/lib/multi-project-dispatch");
assert.equal(
busySentence(["orangecat"]),
"orangecat is already working on a run. Nothing was sent. Wait for it to finish, or send again saying either to queue it behind the open run or to ask it in a fresh session of its own.",
);
assert.match(busySentence(["loki", "solon"]), /^loki, solon are already working on a run\./);
console.log(" ✓ a busy project is named, and the person is told nothing was sent");
assert.match(
runStatusUrl("11111111-1111-4111-8111-111111111111"),
/^https:\/\/.+\/api\/orchestration\/runs\/11111111-1111-4111-8111-111111111111$/,
);
console.log(" ✓ a run's status address is the run route");
const { POST } = await import("../../src/app/api/orangecat/task/route");
const { NextRequest } = await import("next/server");
const secret = "task-door-test-secret-at-least-32-characters-long";
const sign = (raw: string) => "sha256=" + createHmac("sha256", secret).update(raw).digest("hex");
const request = (body: unknown, signature?: string) => {
const raw = JSON.stringify(body);
return new NextRequest("https://loki.orangecat.ch/api/orangecat/task", {
method: "POST",
headers: {
"content-type": "application/json",
...(signature ? { "x-orangecat-signature": signature } : {}),
},
body: raw,
});
};
const good = {
actorId: "00000000-0000-4000-8000-000000000000",
task: "git status --short --branch",
projects: ["orangecat"],
builderChannel: "cloud",
queueIfBusy: false,
};
const original = process.env.ORANGECAT_WEBHOOK_SECRET;
try {
delete process.env.ORANGECAT_WEBHOOK_SECRET;
assert.equal((await POST(request(good, sign(JSON.stringify(good))))).status, 503);
console.log(" ✓ unconfigured fails closed (503)");
process.env.ORANGECAT_WEBHOOK_SECRET = secret;
assert.equal((await POST(request(good, "sha256=deadbeef"))).status, 401);
assert.equal((await POST(request(good))).status, 401);
console.log(" ✓ bad or missing signature (401)");
for (const bad of [
{ ...good, builderChannel: "moon" },
{ ...good, queueIfBusy: "yes" },
{ ...good, separateSession: "yes" },
{ ...good, session: "lane" },
{ ...good, projects: [] },
{ ...good, task: " " },
]) {
assert.equal((await POST(request(bad, sign(JSON.stringify(bad))))).status, 400);
}
console.log(" ✓ an unknown builder, a non-boolean queue flag, no project, no task (400)");
const stale = { ...good, issuedAt: new Date(Date.now() - 10 * 60 * 1000).toISOString() };
const staleRes = await POST(request(stale, sign(JSON.stringify(stale))));
assert.equal(staleRes.status, 401);
assert.equal((await staleRes.json()).error, "stale request");
console.log(" ✓ a signed task dated outside the window is refused (replay)");
} finally {
if (original === undefined) delete process.env.ORANGECAT_WEBHOOK_SECRET;
else process.env.ORANGECAT_WEBHOOK_SECRET = original;
}
console.log("orangecat task door: all passed");
process.exit(0);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});