Multiplayer agent harness for work.
Run turn_start decision rules when a model is already selected (#2329)
Joshua France2dd23825ae3d
diff --git a/src/decisions/hooks.ts b/src/decisions/hooks.tsindex fd46fc203..8c3e9d7b3 100644--- a/src/decisions/hooks.ts+++ b/src/decisions/hooks.ts@@ -4,6 +4,15 @@ import type { DecisionStore } from "./store.ts";const HOOK_TIMEOUT_MS = 5_000;+export async function routeTurnRuntime<R extends object>(+ requested: R | undefined,+ continueTurn: boolean | undefined,+ classify: () => Promise<{ runtime?: R } | undefined>,+): Promise<R | undefined> {+ if (continueTurn) return requested;+ return (await classify())?.runtime ?? requested;+}+export interface DecisionTurn {actorId?: string;scopeLabel: string;diff --git a/src/wiring.ts b/src/wiring.tsindex 2d6c8feb3..5cf544c20 100644--- a/src/wiring.ts+++ b/src/wiring.ts@@ -3,7 +3,7 @@ import { resolveEmbeddingModel } from "./model/pi-models.ts";import { createPostgresTeamStore, type TeamStore } from "./teams/teams.ts";import { brokerCredentialAuthHeader } from "./api/credential-broker.ts";import type { DecisionBackend } from "./decisions/decisions.ts";-import { createDecisionHooks } from "./decisions/hooks.ts";+import { createDecisionHooks, routeTurnRuntime } from "./decisions/hooks.ts";import { createDecisionBackend } from "./decisions/providers.ts";import { createPostgresDecisionStore, type DecisionStore } from "./decisions/store.ts";import type { SlackSessionStatusState } from "./slack/session-status.ts";@@ -1519,17 +1519,17 @@ export function buildApp(hydrateModelCatalog,input.runtimePurpose,);- if (input.runtime?.modelId || input.continueTurn) return resolve(input.runtime);- const routed = (- await decide("turn_start", input.input, {- ...(input.runtimeActorId ? { actorId: input.runtimeActorId } : {}),- scopeLabel: input.scopeLabel,- sessionId: input.session.id,- ...(input.runId ? { runId: input.runId } : {}),- ...(input.cancel ? { cancel: input.cancel } : {}),- })- )?.runtime;- return resolve(routed ? { ...input.runtime, ...routed } : input.runtime);+ return resolve(+ await routeTurnRuntime(input.runtime, input.continueTurn, () =>+ decide("turn_start", input.input, {+ ...(input.runtimeActorId ? { actorId: input.runtimeActorId } : {}),+ scopeLabel: input.scopeLabel,+ sessionId: input.session.id,+ ...(input.runId ? { runId: input.runId } : {}),+ ...(input.cancel ? { cancel: input.cancel } : {}),+ }),+ ),+ );});if (config.securityScreen === "off" || config.securityScreenClassifier !== "model") {diff --git a/test/decision-rules.test.ts b/test/decision-rules.test.tsindex 57e051a70..c1ef6218c 100644--- a/test/decision-rules.test.ts+++ b/test/decision-rules.test.ts@@ -11,6 +11,7 @@ import {type DecisionProvider,} from "../src/decisions/decisions.ts";import { createDecisionBackend } from "../src/decisions/providers.ts";+import { routeTurnRuntime } from "../src/decisions/hooks.ts";const PROVIDERS: DecisionProvider[] = [{ id: "gw", protocol: "chat-completions" }];const MODELS = new Set(["m-deep"]);@@ -402,3 +403,18 @@ test("decision hooks load active rules for the hook, decide, and audit every verundefined,);});++test("turn_start routing also applies when the turn already names a model", async () => {+ const web = { harnessId: "claude", modelId: "claude-opus", effortLevel: "max" };+ const routed = { modelId: "m-deep" };+ assert.deepEqual(await routeTurnRuntime(web, false, async () => ({ runtime: routed })), routed);+ assert.deepEqual(await routeTurnRuntime(web, false, async () => undefined), web);+ assert.deepEqual(await routeTurnRuntime(undefined, false, async () => ({})), undefined);+ let asked = false;+ const kept = await routeTurnRuntime(web, true, async () => {+ asked = true;+ return { runtime: routed };+ });+ assert.equal(kept, web);+ assert.equal(asked, false, "a continued turn is never reclassified");+});