Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Allow task-local runtime handoffs during cron fires (#1604)

Regan Bell269b7e235b90
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.ts
index 4c034cfb7..12e3b47ca 100644
--- a/src/core/orchestrator.ts
+++ b/src/core/orchestrator.ts
@@ -3382,7 +3382,16 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
active: RuntimeChoice,
request: import("../harness/runtime-types.ts").RuntimeRequest,
signal?: AbortSignal,
- ) => deps.runtime!(runtimeClaims, active, request, checkRuntimeAuth, !!userHarnessOverride, signal),
+ ) =>
+ deps.runtime!(
+ runtimeClaims,
+ active,
+ request,
+ checkRuntimeAuth,
+ !!userHarnessOverride,
+ signal,
+ automatedTurn && input.surface === "cron",
+ ),
}
: {}),
...(codexTurnAuth ? { codexAuth: codexTurnAuth } : {}),
diff --git a/src/harness/agent-tools.ts b/src/harness/agent-tools.ts
index 515ad216c..8a9a182f6 100644
--- a/src/harness/agent-tools.ts
+++ b/src/harness/agent-tools.ts
@@ -4017,7 +4017,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
name: "runtime",
label: "runtime",
description:
- "Inspect or change your model, harness, reasoning effort, and fast mode. Use get to see the actual active runtime, saved defaults, and available choices. Use set for requests such as 'switch to Astra and do this'. A successful change stops this runtime and resumes the unfinished task on the selected runtime with saved tool results. Omitted settings are preserved. lifetime defaults to task (this user request, including retries); scope changes the default for future requests in this scope too. inherit returns to the scope default, or clears the scope override when lifetime is scope. Never guess capabilities or claim you cannot switch before using this tool. Call a change by itself, after other tools finish.",
+ "Inspect or change your model, harness, reasoning effort, and fast mode. Use get to see the actual active runtime, saved defaults, and available choices. Use set for requests such as 'switch to Astra and do this'. A successful change stops this runtime and resumes the unfinished task on the selected runtime with saved tool results. Omitted settings are preserved. lifetime defaults to task (this user request or cron fire, including retries); scope changes the default for future requests in this scope too and requires a live user. Cron fires may change only their task runtime; the next fire keeps its configured runtime. inherit returns to the scope default, or clears the scope override when lifetime is scope. Never guess capabilities or claim you cannot switch before using this tool. Call a change by itself, after other tools finish.",
parameters: Type.Object({
action: Type.Union([Type.Literal("get"), Type.Literal("set"), Type.Literal("inherit")]),
model: Type.Optional(Type.String({ description: "Model ID or exact display name from get, such as Astra." })),
diff --git a/src/harness/runtime-control.ts b/src/harness/runtime-control.ts
index 2a2f32f43..b29bc1d2a 100644
--- a/src/harness/runtime-control.ts
+++ b/src/harness/runtime-control.ts
@@ -12,7 +12,7 @@ import { parseScopeId } from "../types.ts";
import { isHarnessId, thinkingLevelsForHarness } from "../model/pi-models.ts";
export function createRuntimeService(deps: RuntimeDeps, app: Pick<App, "authorizesCapabilityScope">): RuntimeService {
- return async (claims, active, request, authorizeChoice, individualAuth, signal) => {
+ return async (claims, active, request, authorizeChoice, individualAuth, signal, cronFire = false) => {
if (!deps.config) return { ok: false, error: "runtime_unavailable" };
const scope = parseScopeId(claims.scopeId);
if (
@@ -32,11 +32,12 @@ export function createRuntimeService(deps: RuntimeDeps, app: Pick<App, "authoriz
snapshot.approvedHarnesses.map((id) => [id, thinkingLevelsForHarness(id)]),
),
taskLifetime:
- "The current user request, including retries and runtime handoffs. Future requests use the scope default.",
+ "The current request or cron fire, including retries and runtime handoffs. Future requests and fires use their configured defaults.",
};
- if (!livePersonCapability(claims) || claims.triggered || claims.botActor)
- return { ok: false, error: "live_actor_required" };
const lifetime = request.lifetime ?? "task";
+ const cronTask = cronFire && claims.triggered === true && !claims.botActor && lifetime === "task";
+ if (!cronTask && (!livePersonCapability(claims) || claims.triggered || claims.botActor))
+ return { ok: false, error: "live_actor_required" };
if (request.action === "inherit") {
const choice = lifetime === "scope" ? snapshot.orgDefault : snapshot.effective;
const error = validateRuntimeChoice(choice);
@@ -46,6 +47,7 @@ export function createRuntimeService(deps: RuntimeDeps, app: Pick<App, "authoriz
!snapshot.modelsByHarness[choice.harnessId]?.includes(choice.modelId)
)
return { ok: false, error: "runtime_unavailable" };
+ if (!(await webuiModelEnabled({ deps }, choice.modelId))) return { ok: false, error: "model_not_enabled" };
const authError = await authorizeChoice?.(choice);
if (authError) return { ok: false, error: "account_runtime_unavailable", message: authError };
if (signal?.aborted) return { ok: false, error: "cancelled" };
diff --git a/src/harness/runtime-types.ts b/src/harness/runtime-types.ts
index 251e0ee40..46073abae 100644
--- a/src/harness/runtime-types.ts
+++ b/src/harness/runtime-types.ts
@@ -31,4 +31,5 @@ export type RuntimeService = (
authorizeChoice?: (choice: RuntimeChoice) => Promise<string | null>,
individualAuth?: boolean,
signal?: AbortSignal,
+ cronFire?: boolean,
) => Promise<RuntimeResult>;
diff --git a/test/context-compaction.test.ts b/test/context-compaction.test.ts
index 2df6d2b41..db9dc061b 100644
--- a/test/context-compaction.test.ts
+++ b/test/context-compaction.test.ts
@@ -17,6 +17,7 @@ import { createModelGateway } from "../src/model/model-gateway.ts";
import { createErrorLog } from "../src/admin/error-log.ts";
import { createAuditLog } from "../src/audit/audit-log.ts";
import { createRateLimiter } from "../src/ratelimit/rate-limiter.ts";
+import { createRuntimeService } from "../src/harness/runtime-control.ts";
import { createMockHarness } from "../src/harness/mock-harness.ts";
import { createDeployStore } from "../src/deploy/deploy-store.ts";
import { createDockerDeployProvider } from "../src/deploy/docker-deploy-provider.ts";
@@ -130,6 +131,10 @@ function buildOrchestrator(harness: Harness, maxContextTokens?: number, defaultT
acl,
maxContextTokens,
defaultTurnWallClockMs,
+ runtime: createRuntimeService(
+ { config, harnessId: "pi", baseModelDefault: "claude-sonnet-5" },
+ { authorizesCapabilityScope: async () => true },
+ ),
});
return { orch, sessions, errors };
}
@@ -1157,3 +1162,62 @@ test("a retry restores a committed runtime decision after reset crashes, without
await orch.handleTurn({ ...input, attempt: 2 });
assert.equal(calls, 2);
});
+
+test("only a cron automation receives task-runtime authority and each fire starts fresh", async () => {
+ const base = createMockHarness();
+ const active = { harnessId: "pi" as const, modelId: "claude-sonnet-5", effortLevel: "high", fastMode: false };
+ const outcomes: { surface: string; ok: boolean }[] = [];
+ let resumed = 0;
+ const harness: Harness = {
+ ...base,
+ turns: {
+ ...base.turns,
+ runTurn: async (input) => {
+ if (input.runtime?.modelId === "gpt-6-astra") {
+ resumed++;
+ return { reply: "handled" };
+ }
+ const result = await input.runtimeControl!(active, { action: "set", model: "Astra" });
+ outcomes.push({ surface: input.input, ok: result.ok });
+ if (result.ok && result.handoff) {
+ await input.emit({
+ type: "tool_result",
+ scopeLabel: input.scopeLabel,
+ payload: { tool: "runtime", runId: input.runId, actorId: actor.id, runtimeHandoff: result.handoff },
+ });
+ return { reply: "", runtimeHandoff: result.handoff };
+ }
+ return { reply: "denied" };
+ },
+ },
+ };
+ const { orch } = buildOrchestrator(harness);
+ for (const surface of ["cron", "webhook"]) {
+ await orch.handleTurn({
+ ...turn(surface),
+ surface,
+ origin: { kind: "automation" },
+ runId: surface + "-run",
+ surfaceTools: false,
+ });
+ }
+ await orch.handleTurn({
+ ...turn("ambient"),
+ surface: "cron",
+ origin: { kind: "ambient" },
+ runId: "ambient-run",
+ surfaceTools: false,
+ });
+ await orch.handleTurn({
+ ...turn("next fire"),
+ surface: "cron",
+ origin: { kind: "automation" },
+ runId: "next-cron-run",
+ surfaceTools: false,
+ });
+ assert.deepEqual(
+ outcomes.map((x) => x.ok),
+ [true, false, true],
+ );
+ assert.equal(resumed, 2);
+});
diff --git a/test/runtime-tool.test.ts b/test/runtime-tool.test.ts
index 705a6ed26..d93519512 100644
--- a/test/runtime-tool.test.ts
+++ b/test/runtime-tool.test.ts
@@ -157,3 +157,104 @@ test("cancellation during validation prevents a scope write", async () => {
assert.deepEqual(result, { ok: false, error: "cancelled" });
assert.equal(await config.getRuntimeSelectionDurable(claims.scopeId), null);
});
+
+test("cron fires may change their task runtime without changing saved defaults", async () => {
+ const { service, config } = await setup();
+ const cronClaims = { ...claims, liveActor: false, triggered: true };
+ const result = await service(
+ cronClaims,
+ active,
+ { action: "set", model: "Astra" },
+ undefined,
+ false,
+ undefined,
+ true,
+ );
+ assert.deepEqual(result, { ok: true, handoff: { lifetime: "task", choice: { ...active, modelId: "gpt-6-astra" } } });
+ assert.equal(await config.getRuntimeSelectionDurable(claims.scopeId), null);
+ for (const action of ["set", "inherit"] as const) {
+ assert.deepEqual(
+ await service(
+ cronClaims,
+ active,
+ { action, model: "Astra", lifetime: "scope" },
+ undefined,
+ false,
+ undefined,
+ true,
+ ),
+ { ok: false, error: "live_actor_required" },
+ );
+ }
+ assert.equal(await config.getRuntimeSelectionDurable(claims.scopeId), null);
+});
+
+test("cron task handoffs still enforce actor, access, model and account policy", async () => {
+ const { service, config } = await setup();
+ const cronClaims = { ...claims, liveActor: false, triggered: true };
+ assert.deepEqual(
+ await service(
+ { ...cronClaims, botActor: true },
+ active,
+ { action: "set", model: "Astra" },
+ undefined,
+ false,
+ undefined,
+ true,
+ ),
+ { ok: false, error: "live_actor_required" },
+ );
+ assert.deepEqual(
+ await service(
+ { ...cronClaims, scopeId: "personal:bob" },
+ active,
+ { action: "set", model: "Astra" },
+ undefined,
+ false,
+ undefined,
+ true,
+ ),
+ { ok: false, error: "forbidden" },
+ );
+ const denied = await setup(false);
+ assert.deepEqual(
+ await denied.service(cronClaims, active, { action: "set", model: "Astra" }, undefined, false, undefined, true),
+ { ok: false, error: "forbidden" },
+ );
+ const result = await service(
+ cronClaims,
+ active,
+ { action: "set", model: "Astra" },
+ async () => "unavailable account",
+ true,
+ undefined,
+ true,
+ );
+ assert.equal(result.ok, false);
+ await config.setRuntimeSelectionLatest(claims.scopeId, { ...active, modelId: "gpt-6-astra" });
+ config.setWebuiModels("org:default-org", ["claude-sonnet-5"]);
+ await config.flushScope("org:default-org");
+ assert.deepEqual(
+ await service(cronClaims, active, { action: "set", model: "Astra" }, undefined, false, undefined, true),
+ { ok: false, error: "model_not_enabled" },
+ );
+});
+
+test("cron inherit cannot select a saved model removed from policy", async () => {
+ const { service, config } = await setup();
+ await config.setRuntimeSelectionLatest(claims.scopeId, { ...active, modelId: "gpt-6-astra" });
+ config.setWebuiModels("org:default-org", ["claude-sonnet-5"]);
+ await config.flushScope("org:default-org");
+ assert.deepEqual(
+ await service(
+ { ...claims, liveActor: false, triggered: true },
+ active,
+ { action: "inherit" },
+ undefined,
+ false,
+ undefined,
+ true,
+ ),
+ { ok: false, error: "model_not_enabled" },
+ );
+});