Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

13.8 KB
Raw Blame
import type { ScopedConfigStore, RuntimePurpose } from "../resolution/config-store.ts";
import {
  defaultModelForHarness,
  fastModeModelIds,
  harnessSupportsFastMode,
  isHarnessId,
  modelSupportedByHarness,
  resolveModel,
  safeModelMetadata,
  thinkingLevelsForHarness,
  modelUnavailableReason,
  type HarnessId,
} from "../model/pi-models.ts";
import type { ScopeId, SessionEntry } from "../types.ts";
import type { Harness, HarnessTurnInput, HarnessTurnResult, RuntimeChoice } from "./harness.ts";
import { NON_INTERACTIVE_THINKING_LEVEL, NON_INTERACTIVE_FAST_MODE } from "../core/turn-options.ts";
import { NonRetryableTurnError } from "../core/turn-error.ts";
import { createGrindMeter, meterGrindSpend } from "./grind.ts";
import {
  bankGoalTurn,
  enforceGoal,
  governGoal,
  goalWorkFromEntries,
  type GoalGovernorInput,
  type GoalRecord,
  type GovernorVerdict,
} from "./goal.ts";

const GOAL_ROUND_MIN_WALL_MS = 30_000;

function turnCompleted(result: HarnessTurnResult): boolean {
  return !result.stopped && !result.runtimeHandoff && !result.pausedOnApproval && !result.pendingApprovals?.length;
}

function inputTokens(result: HarnessTurnResult): number {
  const usage = result.cacheUsage;
  return usage ? usage.cacheRead + usage.cacheWrite + usage.uncachedInput : 0;
}

async function runTurnEnforcingGoal(
  adapter: Harness,
  input: HarnessTurnInput,
  harnessId: HarnessId,
): Promise<HarnessTurnResult> {
  const emitted: SessionEntry[] = [];
  let saved: GoalRecord | undefined;
  const startedAt = Date.now();
  const meter = createGrindMeter(startedAt);
  let segment = { goal: input.goal ?? undefined, assignedAt: input.goal?.createdAt };
  const replacedThisSegment = () => !!segment.goal && segment.goal.createdAt !== segment.assignedAt;
  const dispatched: HarnessTurnInput = {
    ...input,
    goalMeter: meter,
    recordLlmRequest: (rec, signal) => {
      if (!replacedThisSegment()) meterGrindSpend(meter, rec.usage ?? null);
      return input.recordLlmRequest?.(rec, signal);
    },
    emit: async (entry) => {
      const stored = await input.emit(entry);
      emitted.push(stored);
      return stored;
    },
    saveGoal: async (goal) => {
      saved = goal;
      await input.saveGoal?.(goal);
    },
  };
  let result = await adapter.turns.runTurn(dispatched);
  const goal: GoalRecord | null = saved ?? input.goal ?? null;
  if (!goal) return result;
  const account = () => {
    if (replacedThisSegment()) return;
    meter.turns += result.modelCalls ?? 1;
    const tokens = inputTokens(result);
    meter.tokens += tokens;
    if (goal.status !== "paused") goal.tokensUsed += tokens;
  };
  account();
  const remainingWallMs = () =>
    input.turnWallClockMs && input.turnWallClockMs > 0 ? input.turnWallClockMs - (Date.now() - startedAt) : undefined;
  const blocked = () => {
    const remaining = remainingWallMs();
    return (
      !!input.cancel?.aborted ||
      !turnCompleted(result) ||
      (remaining !== undefined && remaining < GOAL_ROUND_MIN_WALL_MS)
    );
  };
  await enforceGoal<"ok" | "halted">({
    goal,
    meter,
    outcome: blocked() ? "halted" : "ok",
    ok: "ok",
    blocked,
    ...(input.governGoal
      ? {
          govern: (previous: GovernorVerdict | undefined) =>
            input.governGoal!({
              objective: goal.objective,
              trigger: "checkpoint",
              recentWork: goalWorkFromEntries([...input.history.slice(-40), ...emitted]),
              ...(previous ? { previous } : {}),
            }),
        }
      : {}),
    ...(input.goalInbox ? { inbox: input.goalInbox } : {}),
    beforePrompt: () => {
      console.error(`[goal] continuation session=${input.session.id} harness=${harnessId} turns=${meter.turns}`);
    },
    prompt: async (note) => {
      const remaining = remainingWallMs();
      segment = { goal, assignedAt: goal.createdAt };
      result = await adapter.turns.runTurn({
        ...dispatched,
        continueTurn: undefined,
        input: note,
        history: [...input.history, ...emitted],
        goal,
        goalContinuation: true,
        ...(remaining !== undefined ? { turnWallClockMs: Math.max(1, remaining) } : {}),
      });
      account();
      return blocked() ? "halted" : "ok";
    },
  });
  if (saved && saved !== goal) return result;
  bankGoalTurn(goal, startedAt);
  if (result.stopped && (result.stoppedByUser || !input.cancel?.aborted) && goal.status === "active") {
    goal.status = "paused";
    goal.updatedAt = Date.now();
  }
  await dispatched.saveGoal!(goal);
  return result;
}

function normalizeRuntimeChoice(choice: RuntimeChoice): RuntimeChoice {
  if (
    (choice.effortLevel === "adaptive" || choice.effortLevel === "default") &&
    !thinkingLevelsForHarness(choice.harnessId, choice.modelId).includes(choice.effortLevel)
  )
    throw new NonRetryableTurnError(
      `${choice.effortLevel} reasoning is not supported by ${choice.harnessId}/${choice.modelId}`,
    );
  return {
    harnessId: choice.harnessId,
    modelId: choice.modelId,
    ...(choice.effortLevel && thinkingLevelsForHarness(choice.harnessId, choice.modelId).includes(choice.effortLevel)
      ? { effortLevel: choice.effortLevel }
      : {}),
    ...(typeof choice.fastMode === "boolean"
      ? {
          fastMode:
            choice.fastMode && harnessSupportsFastMode(choice.harnessId) && fastModeModelIds().includes(choice.modelId),
        }
      : {}),
  };
}

export function resolveRuntimeChoice(
  config: Pick<ScopedConfigStore, "getApprovedHarnesses" | "getRuntimeSelection" | "getBaseModel"> &
    Partial<Pick<ScopedConfigStore, "getPurposeRuntime">>,
  orgScopeId: ScopeId,
  scope: ScopeId,
  fallback: RuntimeChoice,
  requested?: Partial<RuntimeChoice>,
  purpose?: RuntimePurpose,
): RuntimeChoice {
  const approved = config.getApprovedHarnesses() ?? [fallback.harnessId];
  if (approved.length === 0) throw new NonRetryableTurnError("No harnesses are approved");
  const purposeRuntime = purpose ? config.getPurposeRuntime?.(purpose) : undefined;
  if (purposeRuntime) {
    const explicit = Object.fromEntries(Object.entries(requested ?? {}).filter(([, value]) => value !== undefined));
    const choice = { ...purposeRuntime, ...explicit };
    if (
      !isHarnessId(choice.harnessId) ||
      !approved.includes(choice.harnessId) ||
      !modelSupportedByHarness(choice.modelId, choice.harnessId)
    )
      throw new NonRetryableTurnError(`runtime ${choice.harnessId}/${choice.modelId} is not approved`);
    const unavailable = modelUnavailableReason(choice.modelId);
    if (unavailable) throw new NonRetryableTurnError(`${choice.modelId}: ${unavailable}`);
    if (
      choice.effortLevel !== undefined &&
      !thinkingLevelsForHarness(choice.harnessId, choice.modelId).includes(choice.effortLevel)
    )
      throw new NonRetryableTurnError(
        `${choice.effortLevel} reasoning is not supported by ${choice.harnessId}/${choice.modelId}`,
      );
    if (choice.fastMode && (!harnessSupportsFastMode(choice.harnessId) || !fastModeModelIds().includes(choice.modelId)))
      throw new NonRetryableTurnError(`fast mode is not supported by ${choice.harnessId}/${choice.modelId}`);
    return { ...choice, harnessId: choice.harnessId };
  }
  if (purpose === "cron")
    requested = { effortLevel: NON_INTERACTIVE_THINKING_LEVEL, fastMode: NON_INTERACTIVE_FAST_MODE, ...requested };

  const orgStored = config.getRuntimeSelection(orgScopeId);
  const orgLegacy = config.getBaseModel(orgScopeId);
  const configuredOrg: RuntimeChoice =
    orgStored && isHarnessId(orgStored.harnessId)
      ? {
          harnessId: orgStored.harnessId,
          modelId: orgStored.modelId,
          ...(orgStored.effortLevel ? { effortLevel: orgStored.effortLevel } : {}),
          ...(typeof orgStored.fastMode === "boolean" ? { fastMode: orgStored.fastMode } : {}),
        }
      : { harnessId: fallback.harnessId, modelId: orgLegacy ?? fallback.modelId };
  const configuredId =
    requested?.modelId ??
    (scope !== orgScopeId ? (config.getRuntimeSelection(scope)?.modelId ?? config.getBaseModel(scope)) : null) ??
    configuredOrg.modelId;
  const unavailableReason = modelUnavailableReason(configuredId);
  if (unavailableReason) throw new NonRetryableTurnError(`${configuredId}: ${unavailableReason}`);
  const firstApproved = approved.find(isHarnessId) ?? fallback.harnessId;
  const safeFallback =
    approved.includes(fallback.harnessId) && modelSupportedByHarness(fallback.modelId, fallback.harnessId)
      ? fallback
      : { harnessId: firstApproved, modelId: defaultModelForHarness(firstApproved, fallback.modelId) };
  const org =
    approved.includes(configuredOrg.harnessId) &&
    modelSupportedByHarness(configuredOrg.modelId, configuredOrg.harnessId)
      ? configuredOrg
      : safeFallback;
  const scopedStored = scope === orgScopeId ? null : config.getRuntimeSelection(scope);
  const scopedLegacy = scope === orgScopeId ? null : config.getBaseModel(scope);
  let inherited: RuntimeChoice = org;
  if (scopedStored && isHarnessId(scopedStored.harnessId)) {
    inherited = {
      harnessId: scopedStored.harnessId,
      modelId: scopedStored.modelId,
      ...(scopedStored.effortLevel ? { effortLevel: scopedStored.effortLevel } : {}),
      ...(typeof scopedStored.fastMode === "boolean" ? { fastMode: scopedStored.fastMode } : {}),
    };
  } else if (scopedLegacy) {
    inherited = { harnessId: fallback.harnessId, modelId: scopedLegacy };
  }
  const choice = { ...inherited, ...requested };
  if (!approved.includes(choice.harnessId) || !modelSupportedByHarness(choice.modelId, choice.harnessId)) {
    if (requested?.harnessId || requested?.modelId)
      throw new NonRetryableTurnError(`runtime ${choice.harnessId}/${choice.modelId} is not approved`);
    return normalizeRuntimeChoice({ ...org, ...requested });
  }
  return normalizeRuntimeChoice(choice);
}

export async function resolveRuntimeChoiceDurable(
  config: ScopedConfigStore,
  orgScopeId: ScopeId,
  scope: ScopeId,
  fallback: RuntimeChoice,
  requested?: Partial<RuntimeChoice>,
  hydrateModelCatalog?: () => Promise<unknown>,
  purpose?: RuntimePurpose,
): Promise<RuntimeChoice> {
  const approved = (await config.getApprovedHarnessesDurable()) ?? [fallback.harnessId];
  const [orgStored, scopedStored, orgLegacy, scopedLegacy, purposeRuntime] = await Promise.all([
    config.getRuntimeSelectionDurable(orgScopeId),
    scope === orgScopeId ? null : config.getRuntimeSelectionDurable(scope),
    config.getBaseModelOwnDurable(orgScopeId),
    scope === orgScopeId ? null : config.getBaseModelOwnDurable(scope),
    purpose ? config.getPurposeRuntimeDurable(purpose) : undefined,
  ]);
  if (hydrateModelCatalog) {
    const candidates = [requested?.modelId, purposeRuntime?.modelId, scopedStored?.modelId, orgStored?.modelId];
    if (candidates.some((modelId) => modelId && !resolveModel(modelId))) await hydrateModelCatalog();
  }
  const view: Pick<
    ScopedConfigStore,
    "getApprovedHarnesses" | "getRuntimeSelection" | "getBaseModel" | "getPurposeRuntime"
  > = {
    getApprovedHarnesses: () => approved,
    getPurposeRuntime: () => purposeRuntime,
    getRuntimeSelection: (id: ScopeId) => {
      if (id === orgScopeId) return orgStored;
      return id === scope ? scopedStored : null;
    },
    getBaseModel: (id: ScopeId) => {
      if (id === orgScopeId) return orgLegacy;
      return id === scope ? scopedLegacy : null;
    },
  };
  return resolveRuntimeChoice(view, orgScopeId, scope, fallback, requested, purpose);
}

export function createHarnessRouter(
  adapters: ReadonlyMap<HarnessId, Harness>,
  utility: Harness,
  resolve: (input: HarnessTurnInput, routedBy: (ruleId: string) => void) => RuntimeChoice | Promise<RuntimeChoice>,
): Harness {
  const lastHarness = new Map<string, HarnessId>();
  return {
    profile: utility.profile,
    models: {
      ...utility.models,
      async screenSecurity(input) {
        const adapter = input.harnessId && isHarnessId(input.harnessId) ? adapters.get(input.harnessId) : utility;
        return adapter?.models.screenSecurity?.(input);
      },
    },
    tools: utility.tools,
    turns: {
      async runTurn(input) {
        let routedBy: string | undefined;
        const choice = await resolve(input, (ruleId) => {
          routedBy = ruleId;
        });
        const label = safeModelMetadata(choice.modelId)?.label;
        input.onRuntime?.({
          harnessId: choice.harnessId,
          modelId: choice.modelId,
          ...(label ? { label } : {}),
          ...(routedBy ? { change: { rule: routedBy } } : {}),
        });
        const adapter = adapters.get(choice.harnessId);
        if (!adapter) throw new Error(`harness ${choice.harnessId} is unavailable`);
        const prior = lastHarness.get(input.session.id);
        if (prior && prior !== choice.harnessId) {
          await adapters.get(prior)?.turns.resetSession?.(input.session.id);
          await adapter.turns.resetSession?.(input.session.id);
        }
        lastHarness.set(input.session.id, choice.harnessId);
        const judge = adapter.models?.judge;
        const dispatched: HarnessTurnInput = {
          ...input,
          runtime: choice,
          tools: input.runtimeControl
            ? { ...input.tools, runtime: (request, signal) => input.runtimeControl!(choice, request, signal) }
            : input.tools,
          ...(judge
            ? {
                governGoal: (goalInput: GoalGovernorInput) => governGoal(judge, goalInput, input.cancel),
              }
            : {}),
        };
        return adapter.profile.capabilities.has("goal-enforcement")
          ? adapter.turns.runTurn(dispatched)
          : runTurnEnforcingGoal(adapter, dispatched, choice.harnessId);
      },
      async resetSession(sessionId) {
        lastHarness.delete(sessionId);
        await Promise.all([...adapters.values()].map((adapter) => adapter.turns.resetSession?.(sessionId)));
      },
      async close() {
        await Promise.all([...new Set(adapters.values())].map((adapter) => adapter.turns.close?.()));
      },
    },
  };
}