These columns and the messages that plumbed them existed for the CLI-
adapter era — Gemini's resume id and provider-specific session params
rode the run-finished envelope back onto the session row so the next
turn could resume. Pi Mono dispatches every provider through pi-ai and
emits neither value. The worker has been posting literal null on every
finish, finalizeRun has been writing null into task_sessions, and
rotateSession has been carrying null forward.
Remove the wire, the writer, the carry-forward, and the columns:
- v43 migration rebuilds task_sessions without session_params_json /
session_display_id, and runs without session_display_id. Follows
the idempotent "create new, copy, swap, reindex" pattern used by
v41. context_snapshot_json is preserved (also unreferenced today
but outside this cleanup's scope).
- RunJob / EnqueueRunPayload / ChildMessage / worker-types zod
schema no longer carry the fields. finishRun() / createNewSession()
signatures tighten in lockstep.
- rotateSession starts the new session with fresh counters and no
carry-forward.
- turn-context resolveSession returns just the session id.
- TaskSessionRecord / RunRecord lose the dead fields.
498/498 unit tests pass. typecheck + build clean.
114 lines
4.3 KiB
TypeScript
114 lines
4.3 KiB
TypeScript
import { getConnectorExecutionReadinessIssue } from "@shared/connector-policy";
|
|
import type { ConnectorRecord } from "@shared/types";
|
|
import type { OrchestrationDependencies } from "./types";
|
|
import { buildRunPayload, type RunDispatchParams } from "./turn-context";
|
|
import { validateConnectorModelSelection } from "../connector-model-policy";
|
|
import { resolveWorkspaceWithLockMode } from "./resolve-workspace";
|
|
|
|
export function createRunDispatch(deps: OrchestrationDependencies) {
|
|
const {
|
|
db,
|
|
logger,
|
|
isQuitting,
|
|
hasWorkerReady,
|
|
getApiPort,
|
|
createRunToken,
|
|
enqueueWorkerRun,
|
|
listRuntimeSkillEntries,
|
|
budgetService,
|
|
getSessionHandoff,
|
|
evaluatePreRunSessionRotation,
|
|
} = deps;
|
|
|
|
function resolveWorkspace(
|
|
task: { workspaceId: string | null; projectId?: string | null } | null,
|
|
agent: { workspaceId: string | null },
|
|
companyId: string,
|
|
) {
|
|
return resolveWorkspaceWithLockMode(db, task, agent, companyId);
|
|
}
|
|
|
|
function getRunEligibilityFailure(
|
|
agent: { status: string; budgetMonthlyUsd: number; spentMonthlyUsd: number; name: string; connectorId: string; id: string; companyId: string; model?: string | null },
|
|
connector: Pick<ConnectorRecord, "id" | "label" | "status" | "capabilityMatrix">,
|
|
projectId: string | null = null,
|
|
) {
|
|
if (["terminated", "paused", "pending_approval"].includes(agent.status)) {
|
|
return "AGENT_NOT_RUNNABLE";
|
|
}
|
|
const gate = budgetService.evaluateBudgetGate(agent.companyId, agent.id, projectId);
|
|
if (!gate.allowed) {
|
|
logger.info(`[run] Agent ${agent.name} budget policy blocked: ${gate.blockedBy.map(b => `${b.scopeType}(${b.policyId})`).join(", ")}`);
|
|
return "BUDGET_POLICY_BLOCKED";
|
|
}
|
|
const connectorIssue = getConnectorExecutionReadinessIssue(connector);
|
|
if (connectorIssue) {
|
|
const issue = connectorIssue ?? `Connector ${connector.id} is not execution-ready.`;
|
|
logger.info(`[run] Agent ${agent.name} connector ${connector.id} blocked: ${issue}`);
|
|
return "CONNECTOR_NOT_READY";
|
|
}
|
|
const modelSelection = validateConnectorModelSelection(db, agent.companyId, connector.id, agent.model ?? null);
|
|
if (!modelSelection.ok) {
|
|
logger.info(`[run] Agent ${agent.name} connector model blocked: ${modelSelection.error}`);
|
|
return "CONNECTOR_MODEL_NOT_ALLOWED";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function dispatchRunToWorker(params: RunDispatchParams) {
|
|
if (isQuitting()) {
|
|
throw new Error("Application is shutting down — refusing to dispatch new run.");
|
|
}
|
|
if (!hasWorkerReady()) {
|
|
throw new Error("Orchestration worker is not ready.");
|
|
}
|
|
const connectorIssue = getConnectorExecutionReadinessIssue(params.connector);
|
|
if (connectorIssue) {
|
|
throw new Error(connectorIssue);
|
|
}
|
|
|
|
// D-02: Claim the workspace lock synchronously before enqueuing the run.
|
|
// Previously the worker claimed the lock only after it emitted `running`,
|
|
// so two runs could race between enqueue and startup and both read the
|
|
// same workspace mid-write. Main is the authoritative coordinator.
|
|
//
|
|
// On contention we mark the run `cancelled`, not `failed`. The Dashboard
|
|
// "Needs attention" card only surfaces failed/timed_out/interrupted runs
|
|
// (see src/renderer/components/dashboard/Dashboard.tsx), so the user is
|
|
// not alarmed by a transient backoff. The task row is rolled back to
|
|
// `todo` via the `finishRun` never-started branch, so heartbeat picks
|
|
// it up on the next cycle with no manual intervention.
|
|
if (
|
|
params.workspaceMode === "exclusive" &&
|
|
params.workspace &&
|
|
!db.claimWorkspaceLock(params.workspace.id, params.runId)
|
|
) {
|
|
db.finishRun({
|
|
runId: params.runId,
|
|
status: "cancelled",
|
|
summary: null,
|
|
errorMessage: `Workspace ${params.workspace.id} is in use by another run; will retry on the next heartbeat.`,
|
|
exitCode: null,
|
|
signal: null,
|
|
model: null,
|
|
unattributedCost: true,
|
|
});
|
|
return;
|
|
}
|
|
|
|
const payload = buildRunPayload(params, {
|
|
db,
|
|
logger,
|
|
getApiPort,
|
|
createRunToken,
|
|
listRuntimeSkillEntries,
|
|
getSessionHandoff,
|
|
evaluatePreRunSessionRotation,
|
|
});
|
|
|
|
enqueueWorkerRun(payload);
|
|
}
|
|
|
|
return { resolveWorkspace, getRunEligibilityFailure, dispatchRunToWorker };
|
|
}
|