Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
406 changes: 403 additions & 3 deletions apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts

Large diffs are not rendered by default.

297 changes: 246 additions & 51 deletions apps/server/src/orchestration-v2/ModelAlertReactor.ts

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions apps/server/src/orchestration-v2/ThreadEventAlerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@t3tools/contracts";
import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
import { threadPullRequestKeyOf } from "../sourceControl/threadPullRequests.ts";
import type { LaneEvent } from "./laneEventRouting.ts";
import { formatThreadLink } from "@t3tools/shared/threadLinks";
import * as DateTime from "effect/DateTime";
import * as Schema from "effect/Schema";
Expand Down Expand Up @@ -111,6 +112,22 @@ const openPullRequests = (shell: WatchedShell) =>
(link) => link.snapshot === null || link.snapshot.state === "open",
);

/** The lane-routing view of this thread ending, as one recipient would receive it. */
export const laneEventOf = (shell: WatchedShell, recipient: string | null): LaneEvent => {
const failed = shell.status === "failed" || shell.status === "interrupted";
return {
child_thread_id: shell.id,
owner: recipient,
kind: "lane-ended",
condition: {
outcome: shell.status,
error: failed ? (shell.lastError ?? shell.status) : null,
open_prs: openPullRequests(shell).map(threadPullRequestKeyOf),
question: shell.pendingRuntimeRequest === null ? null : "pending-request",
},
};
};

/** The links a lane-ended re-judge should reread from the host: the ones it counts as open. */
export const openPullRequestLinks = (shell: Pick<WatchedShell, "pullRequests">) =>
openPullRequests(shell as WatchedShell);
Expand Down
8 changes: 7 additions & 1 deletion apps/server/src/orchestration-v2/eventSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ import type { PullRequestWatchChange } from "./pullRequestWatch.ts";
/** Dropped before a comment or review can wake. Match ignores case and a trailing `[bot]`. */
export const DEFAULT_PULL_REQUEST_IGNORE_AUTHORS = ["coderabbitai"] as const;

const THREAD_EVENT_NAMES = new Set(["lane-ended", "promise-without-wake", "stuck", "needs-person"]);
const THREAD_EVENT_NAMES = new Set([
"lane-ended",
"promise-without-wake",
"stuck",
"needs-person",
"wake-suppressed",
]);

const HOUR_MS = 3_600_000;
const CLOCK_RE = /^([01]\d|2[0-3]):([0-5]\d)$/;
Expand Down
126 changes: 126 additions & 0 deletions apps/server/src/orchestration-v2/laneEventRouting.conformance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"description": "Conformance contract for lane-event routing and standing-manager mailbox rules (CLO-1302, CLO-1303). scripts/lane_event_routing.py and the t3code port must both produce these decisions.",
"cases": [
{
"name": "replay-c964fe48-98fbc01a-settled-launcher-zero-wakes",
"comment": "Audit thread c964fe48 settled; child 98fbc01a (declarative factory RFC) ended repeatedly across 10 runs with no open PR and no failure, plus a later provider-limit failure repeated with different numbers. The launcher must never be woken.",
"world": {"threads": {"c964fe48": {"state": "settled"}, "98fbc01a": {"state": "idle"}}, "delivered": []},
"events": [
{"child_thread_id": "98fbc01a", "owner": "c964fe48", "kind": "lane-ended", "condition": {"outcome": "completed"}},
{"child_thread_id": "98fbc01a", "owner": "c964fe48", "kind": "lane-ended", "condition": {"outcome": "completed"}},
{"child_thread_id": "98fbc01a", "owner": "c964fe48", "kind": "lane-ended", "condition": {"outcome": "completed"}},
{"child_thread_id": "98fbc01a", "owner": "c964fe48", "kind": "lane-ended", "condition": {"outcome": "failed", "error": "Antigravity usage limit reached, resets at 2026-12-03T07:01:49Z"}},
{"child_thread_id": "98fbc01a", "owner": "c964fe48", "kind": "lane-ended", "condition": {"outcome": "failed", "error": "Antigravity usage limit reached, resets at 2026-12-03T07:01:50Z"}},
{"child_thread_id": "98fbc01a", "owner": "c964fe48", "kind": "lane-ended", "condition": {"outcome": "completed"}}
],
"expect_actions": ["digest", "drop", "drop", "digest", "drop", "drop"],
"expect_wakes": 0
},
{
"name": "archived-or-missing-launcher-is-detached",
"world": {"threads": {"child": {"state": "idle"}, "gone": {"state": "archived"}}, "delivered": []},
"events": [
{"child_thread_id": "child", "owner": "gone", "kind": "lane-ended", "condition": {"outcome": "failed", "error": "boom 3"}},
{"child_thread_id": "child", "owner": "never-existed", "kind": "lane-ended", "condition": {"outcome": "failed", "error": "boom 4"}}
],
"expect_actions": ["digest", "drop"],
"expect_wakes": 0
},
{
"name": "active-owner-one-alert-per-condition",
"comment": "The owner is active and the child ended with an open PR: wake once, then drop identical repeats; a changed open-PR set is a new condition.",
"world": {"threads": {"owner": {"state": "idle"}, "child": {"state": "idle"}}, "delivered": []},
"events": [
{"child_thread_id": "child", "owner": "owner", "kind": "lane-ended", "condition": {"outcome": "completed", "open_prs": ["org/repo#1"]}},
{"child_thread_id": "child", "owner": "owner", "kind": "lane-ended", "condition": {"outcome": "completed", "open_prs": ["org/repo#1"]}},
{"child_thread_id": "child", "owner": "owner", "kind": "lane-ended", "condition": {"outcome": "completed", "open_prs": ["org/repo#1", "org/repo#2"]}},
{"child_thread_id": "child", "owner": "owner", "kind": "lane-ended", "condition": {"outcome": "completed", "open_prs": ["org/repo#2", "org/repo#1"]}}
],
"expect_actions": ["wake", "drop", "wake", "drop"],
"expect_wakes": 2
},
{
"name": "same-failure-class-different-numbers-dedupes",
"world": {"threads": {"owner": {"state": "idle"}, "child": {"state": "idle"}}, "delivered": []},
"events": [
{"child_thread_id": "child", "owner": "owner", "kind": "lane-ended", "condition": {"outcome": "failed", "error": "HTTP 500 for request 1234"}},
{"child_thread_id": "child", "owner": "owner", "kind": "lane-ended", "condition": {"outcome": "failed", "error": "HTTP 500 for request 9876"}},
{"child_thread_id": "child", "owner": "owner", "kind": "lane-ended", "condition": {"outcome": "failed", "error": "disk full"}}
],
"expect_actions": ["wake", "drop", "wake"],
"expect_wakes": 2
},
{
"name": "target-running-drops",
"world": {"threads": {"owner": {"state": "idle"}, "child": {"state": "running"}}, "delivered": []},
"events": [
{"child_thread_id": "child", "owner": "owner", "kind": "lane-ended", "condition": {"outcome": "failed", "error": "x"}},
{"child_thread_id": "child", "owner": "owner", "kind": "pr-checks-failed", "superseded": true, "condition": {"outcome": "failed"}}
],
"expect_actions": ["drop", "drop"],
"expect_wakes": 0
},
{
"name": "pr-events-routing",
"comment": "Passing checks and merges go to the merge-queue code handler; only failures and review requests wake a model.",
"world": {"threads": {"owner": {"state": "idle"}, "child": {"state": "idle"}}, "delivered": []},
"events": [
{"child_thread_id": "child", "owner": "owner", "kind": "pr-checks-passed", "condition": {"outcome": "passed", "open_prs": ["org/repo#9"]}},
{"child_thread_id": "child", "owner": "owner", "kind": "pr-merged", "condition": {"outcome": "merged", "open_prs": []}},
{"child_thread_id": "child", "owner": "owner", "kind": "pr-checks-failed", "condition": {"outcome": "failed", "error": "lint failed", "open_prs": ["org/repo#9"]}},
{"child_thread_id": "child", "owner": "owner", "kind": "review-requested", "condition": {"outcome": "review", "open_prs": ["org/repo#9"]}}
],
"expect_actions": ["code-handler", "code-handler", "wake", "wake"],
"expect_wakes": 2
},
{
"name": "requester-is-used-when-no-owner",
"world": {"threads": {"req": {"state": "settled"}, "child": {"state": "idle"}}, "delivered": []},
"events": [
{"child_thread_id": "child", "requester": "req", "kind": "lane-ended", "condition": {"outcome": "failed", "error": "z"}}
],
"expect_actions": ["digest"],
"expect_wakes": 0
},
{
"name": "mailbox-founder-first-and-batched",
"comment": "Seven queued messages become one turn: founder first, then CEO, then the rest in arrival order. Digest items start no turn.",
"queued": [
{"id": "m1", "at": "2026-11-27T10:00:00Z", "sender_role": "agent"},
{"id": "m2", "at": "2026-11-27T10:01:00Z", "sender_role": "ceo"},
{"id": "m3", "at": "2026-11-27T10:02:00Z", "sender_role": "agent"},
{"id": "m4", "at": "2026-11-27T10:03:00Z", "sender_role": "founder"},
{"id": "d1", "at": "2026-11-27T10:04:00Z", "sender_role": "agent", "kind": "digest"}
],
"current_priority": null,
"expect": {"turns": 1, "batch": ["m4", "m2", "m1", "m3"], "digest": ["d1"], "preempt": false}
},
{
"name": "mailbox-digest-only-starts-no-turn",
"queued": [
{"id": "d1", "at": "2026-11-27T10:00:00Z", "sender_role": "agent", "kind": "digest"},
{"id": "d2", "at": "2026-11-27T10:01:00Z", "sender_role": "agent", "kind": "digest"}
],
"current_priority": null,
"expect": {"turns": 0, "batch": [], "digest": ["d1", "d2"], "preempt": false}
},
{
"name": "mailbox-founder-preempts-agent-turn",
"queued": [{"id": "f1", "at": "2026-11-27T10:00:00Z", "sender_role": "founder"}],
"current_priority": 2,
"expect": {"turns": 1, "batch": ["f1"], "digest": [], "preempt": true}
},
{
"name": "mailbox-ceo-preempts-agent-turn-not-founder-turn",
"queued": [{"id": "c1", "at": "2026-11-27T10:00:00Z", "sender_role": "ceo"}],
"current_priority": 0,
"expect": {"turns": 1, "batch": ["c1"], "digest": [], "preempt": false}
},
{
"name": "mailbox-agent-does-not-preempt",
"queued": [{"id": "a1", "at": "2026-11-27T10:00:00Z", "sender_role": "agent"}],
"current_priority": 2,
"expect": {"turns": 1, "batch": ["a1"], "digest": [], "preempt": false}
}
]
}
100 changes: 100 additions & 0 deletions apps/server/src/orchestration-v2/laneEventRouting.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// @effect-diagnostics nodeBuiltinImport:off -- Hashes the vendored contract bytes against the Home pin.
import * as NodeCrypto from "node:crypto";
import * as NodeFS from "node:fs";

import { assert, describe, it } from "@effect/vitest";

import {
laneFailureClass,
replayLaneEvents,
type LaneEvent,
type LaneWorld,
} from "./laneEventRouting.ts";

// sha256 of config/lane-event-routing/conformance.json in nullStack65/closura-agent-config at
// 40e39541c6. Update both together; the Python reference and this port must agree on one contract.
const HOME_CONFORMANCE_SHA256 = "e6460764fd173a4b7e4c3391f5b46f3e0ff8d0ac62a341ff41f025109c28a99f";

const raw = NodeFS.readFileSync(new URL("./laneEventRouting.conformance.json", import.meta.url));
const contract = JSON.parse(raw.toString("utf8")) as {
readonly cases: ReadonlyArray<{
readonly name: string;
readonly world?: LaneWorld;
readonly events?: ReadonlyArray<LaneEvent>;
readonly expect_actions?: ReadonlyArray<string>;
readonly expect_wakes?: number;
}>;
};
const sha256 = (bytes: Buffer) => NodeCrypto.createHash("sha256").update(bytes).digest("hex");

describe("lane event routing conformance", () => {
it("vendors the Home contract byte for byte", () => {
assert.strictEqual(sha256(raw), HOME_CONFORMANCE_SHA256);
// Set in a Home + t3code checkout to catch drift before the pin is bumped.
const homeCopy = process.env.CLOSURA_LANE_EVENT_CONFORMANCE;
if (homeCopy && NodeFS.existsSync(homeCopy)) {
assert.strictEqual(sha256(NodeFS.readFileSync(homeCopy)), HOME_CONFORMANCE_SHA256);
}
});

const eventCases = contract.cases.filter((c) => c.events !== undefined);

it("covers every lane-event case in the contract", () => {
assert.isAtLeast(eventCases.length, 7);
});

it.each(eventCases)("$name", (testCase) => {
const decisions = replayLaneEvents(
testCase.events ?? [],
testCase.world ?? { threads: {}, delivered: [] },
);
assert.deepStrictEqual(
decisions.map((decision) => decision.action),
testCase.expect_actions,
);
assert.strictEqual(
decisions.filter((decision) => decision.action === "wake").length,
testCase.expect_wakes,
);
});

it("routes every c964fe48 alert about 98fbc01a away from the settled launcher", () => {
const replay = contract.cases.find((c) => c.name.startsWith("replay-c964fe48-98fbc01a"));
assert.isDefined(replay);
const decisions = replayLaneEvents(replay?.events ?? [], replay?.world as LaneWorld);
assert.strictEqual(decisions.length, 6);
for (const decision of decisions) {
assert.notStrictEqual(decision.recipient, "c964fe48");
assert.notStrictEqual(decision.action, "wake");
}
});

it("classes provider limits together and strips digits and uuids", () => {
assert.strictEqual(laneFailureClass("HTTP 429 Too Many Requests"), "provider-limit");
assert.strictEqual(laneFailureClass("Out of usage until 7pm"), "provider-limit");
assert.strictEqual(
laneFailureClass("run 0b6d8a8e-1c1f-4d1e-9b0a-1a2b3c4d5e6f failed after 12 tries"),
laneFailureClass("run 9f9f9f9f-0000-4000-8000-000000000000 failed after 3 tries"),
);
assert.strictEqual(laneFailureClass(null), "none");
});

it("reports the same failure again after recovery, but suppresses repeats within an episode", () => {
const failed: LaneEvent = {
child_thread_id: "child",
owner: "owner",
condition: { outcome: "failed", error: "disk full" },
};
const recovered: LaneEvent = {
...failed,
condition: { outcome: "completed" },
};
assert.deepEqual(
replayLaneEvents([failed, failed, recovered, failed, failed], {
threads: { child: { state: "idle" }, owner: { state: "idle" } },
delivered: [],
}).map((decision) => decision.action),
["wake", "drop", "digest", "wake", "drop"],
);
});
});
Loading
Loading