| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- import type { PiStepEndHookInput } from '@mavis/agent-core/pi-turn-runner';
- import type {
- RunawayGuardProgressProjection,
- RunawayGuardToolPolicy,
- RunawayGuardToolPolicyKind,
- RunawayGuardToolStep,
- RunawayGuardTrustedToolProvenance,
- RunawayGuardVerifiedToolProgress,
- } from './contracts.js';
- import type { ShadowState } from './state.js';
- import { fingerprint, fingerprintWithinBudget, isRecord } from './fingerprint.js';
- export interface StepView {
- readonly detectActionKeys: readonly string[];
- readonly pollingActionKeys: readonly string[];
- readonly detectResultKeys: readonly string[];
- readonly errorFamilyKeys: readonly string[];
- readonly remindableErrorFamilyKeys: ReadonlySet<string>;
- readonly progress: readonly ProgressView[];
- readonly detectActionBatchKey?: string;
- }
- export type ProgressView = ProgressObservationView | ProgressResetView | ProgressInterruptView;
- interface ProgressObservationView {
- readonly type: 'observation';
- readonly kind: Exclude<RunawayGuardToolPolicyKind, 'exempt'>;
- readonly loopKey: string;
- readonly progressKey: string;
- readonly verifiedProgress: boolean;
- readonly continuousPolling: boolean;
- readonly reminderEligible: boolean;
- }
- interface ProgressResetView {
- readonly type: 'reset';
- readonly loopKey: string;
- }
- interface ProgressInterruptView {
- readonly type: 'interrupt';
- }
- interface ToolResultProjection {
- readonly toolName: string;
- readonly content: unknown;
- readonly details?: unknown;
- readonly isError: boolean;
- }
- export function projectStep(
- message: PiStepEndHookInput['message'],
- toolResults: PiStepEndHookInput['toolResults'],
- state: ShadowState,
- policies: ReadonlyMap<string, RunawayGuardToolPolicy>,
- maxBytes: number,
- verifiedProgressByCallId: ReadonlyMap<string, RunawayGuardVerifiedToolProgress>,
- trustedToolProvenanceByCallId: ReadonlyMap<string, RunawayGuardTrustedToolProvenance>,
- blockedToolCalls: PiStepEndHookInput['blockedToolCalls'] = [],
- ): StepView {
- const excludedCallIds = new Set(
- blockedToolCalls
- .filter((call) => call.blockedBy === 'permission')
- .map((call) => call.toolCallId),
- );
- const detectActionKeys: string[] = [];
- const pollingActionKeys: string[] = [];
- const detectResultKeys: string[] = [];
- const errorFamilyKeys: string[] = [];
- const remindableErrorFamilyKeys = new Set<string>();
- const progress: ProgressView[] = [];
- const resultsByCallId = new Map(toolResults.map((result) => [result.toolCallId, result]));
- const actionKeysByCallId = new Map<string, string>();
- const expectedResultCallIds = new Set<string>();
- if (message.role === 'assistant') {
- for (const block of message.content) {
- if (block.type !== 'toolCall') continue;
- const excluded = excludedCallIds.has(block.id);
- const configuredPolicy = policies.get(block.name);
- const policy = configuredPolicy?.kind ?? 'detect';
- const result = resultsByCallId.get(block.id);
- const projectedResult = excluded ? undefined : result;
- const verifiedProgress = excluded ? undefined : verifiedProgressByCallId.get(block.id);
- const trustedToolProvenance = trustedToolProvenanceByCallId.get(block.id);
- const toolStep: RunawayGuardToolStep = {
- toolCallId: block.id,
- toolName: block.name,
- arguments: block.arguments,
- ...(projectedResult
- ? {
- result: {
- toolName: projectedResult.toolName,
- content: projectedResult.content,
- ...(projectedResult.details === undefined
- ? {}
- : { details: projectedResult.details }),
- isError: projectedResult.isError,
- },
- }
- : {}),
- ...(verifiedProgress ? { verifiedProgress } : {}),
- ...(trustedToolProvenance ? { trustedToolProvenance } : {}),
- };
- const progressProjection = projectProgressBestEffort(configuredPolicy, toolStep);
- const pollingControl = policy === 'polling' ? progressProjection?.polling : undefined;
- appendPollingControl(
- progress,
- pollingControl,
- progressProjection,
- block.name,
- state,
- maxBytes,
- );
- if (!pollingControl) progress.push({ type: 'interrupt' });
- // Permission rejection stays excluded from the old detectors, but its
- // trusted task identity must still reset the new polling continuity.
- if (excluded || policy === 'exempt') continue;
- if (result?.isError && isExpectedResultBestEffort(configuredPolicy, toolStep)) {
- expectedResultCallIds.add(block.id);
- }
- let observedChange = false;
- if (progressProjection && pollingControl?.mode !== 'reset') {
- const loopKey = fingerprintWithinBudget(
- state.secret,
- { toolName: block.name, loopKey: progressProjection.loopKey },
- maxBytes,
- );
- const progressKey = fingerprintWithinBudget(
- state.secret,
- { toolName: block.name, progressKey: progressProjection.progressKey },
- maxBytes,
- );
- if (loopKey && progressKey) {
- observedChange =
- hasPositiveProgress(progressProjection) ||
- (pollingControl?.mode === 'continuous' && hasPositiveProgress(verifiedProgress));
- progress.push({
- type: 'observation',
- kind: policy,
- loopKey,
- progressKey,
- verifiedProgress: observedChange,
- continuousPolling: pollingControl?.mode === 'continuous',
- reminderEligible:
- pollingControl?.mode === 'continuous' && pollingControl.reminderEligible === true,
- });
- } else {
- state.fingerprintSkippedCount += Number(!loopKey) + Number(!progressKey);
- if (pollingControl?.mode === 'continuous') progress.push({ type: 'interrupt' });
- }
- }
- const actionValue = projectActionKeyBestEffort(configuredPolicy, block.arguments);
- const actionKey = fingerprintWithinBudget(
- state.secret,
- { toolName: block.name, actionKey: actionValue },
- maxBytes,
- );
- if (!actionKey) {
- state.fingerprintSkippedCount += 1;
- continue;
- }
- actionKeysByCallId.set(block.id, actionKey);
- if (observedChange) {
- // A real change invalidates a mechanical no-change hypothesis for this call only.
- expectedResultCallIds.add(block.id);
- } else if (policy === 'polling') {
- if (!progressProjection) pollingActionKeys.push(actionKey);
- } else {
- detectActionKeys.push(actionKey);
- }
- }
- }
- for (const result of toolResults) {
- if (excludedCallIds.has(result.toolCallId)) continue;
- const policy = policies.get(result.toolName)?.kind ?? 'detect';
- const projected: ToolResultProjection = {
- toolName: result.toolName,
- content: result.content,
- isError: result.isError,
- ...(result.details === undefined ? {} : { details: result.details }),
- };
- if (policy === 'detect') {
- const resultKey = fingerprintWithinBudget(state.secret, projected, maxBytes);
- if (resultKey) detectResultKeys.push(resultKey);
- else state.fingerprintSkippedCount += 1;
- }
- if (result.isError && policy !== 'exempt' && !expectedResultCallIds.has(result.toolCallId)) {
- const family = errorFamily(result.toolName, result.details, result.content);
- if (family) {
- const actionKey = actionKeysByCallId.get(result.toolCallId);
- const key = fingerprintWithinBudget(
- state.secret,
- { family, actionKey: actionKey ?? null },
- maxBytes,
- );
- if (!key) {
- state.fingerprintSkippedCount += 1;
- continue;
- }
- errorFamilyKeys.push(key);
- if (policy === 'detect' && actionKey) {
- remindableErrorFamilyKeys.add(key);
- }
- }
- }
- }
- detectActionKeys.sort();
- pollingActionKeys.sort();
- detectResultKeys.sort();
- errorFamilyKeys.sort();
- return {
- detectActionKeys,
- pollingActionKeys,
- detectResultKeys,
- errorFamilyKeys,
- remindableErrorFamilyKeys,
- progress,
- detectActionBatchKey:
- detectActionKeys.length > 0
- ? fingerprint(state.secret, `action-batch\u0000${detectActionKeys.join('\u0000')}`)
- : undefined,
- };
- }
- function appendPollingControl(
- progress: ProgressView[],
- control: RunawayGuardProgressProjection['polling'] | undefined,
- projection: RunawayGuardProgressProjection | undefined,
- toolName: string,
- state: ShadowState,
- maxBytes: number,
- ): void {
- if (control?.mode !== 'reset' || !projection) return;
- const loopKey = fingerprintWithinBudget(
- state.secret,
- { toolName, loopKey: projection.loopKey },
- maxBytes,
- );
- if (!loopKey) {
- state.fingerprintSkippedCount += 1;
- progress.push({ type: 'interrupt' });
- return;
- }
- progress.push({ type: 'reset', loopKey });
- }
- function hasPositiveProgress(projection: RunawayGuardProgressProjection | undefined): boolean {
- return (
- projection?.stateChanged === true ||
- projection?.artifactChanged === true ||
- (projection?.newFacts ?? 0) > 0
- );
- }
- function projectActionKeyBestEffort(
- policy: RunawayGuardToolPolicy | undefined,
- argumentsValue: unknown,
- ): unknown {
- if (!policy?.projectActionKey) return argumentsValue;
- try {
- return policy.projectActionKey(argumentsValue) ?? argumentsValue;
- } catch {
- return argumentsValue;
- }
- }
- function projectProgressBestEffort(
- policy: RunawayGuardToolPolicy | undefined,
- step: RunawayGuardToolStep,
- ): RunawayGuardProgressProjection | undefined {
- if (policy?.projectProgress) {
- try {
- const projection = policy.projectProgress(step);
- if (projection && validProgressProjection(projection)) return projection;
- } catch {
- // Fall through to independently verified host progress.
- }
- }
- if (!step.verifiedProgress || !validProgressProjection(step.verifiedProgress)) return undefined;
- // Host facts may veto a trusted polling observation, but cannot independently
- // create one: only the configured tool policy may opt into polling continuity.
- const { loopKey, progressKey, newFacts, stateChanged, artifactChanged } = step.verifiedProgress;
- return {
- loopKey,
- progressKey,
- ...(newFacts === undefined ? {} : { newFacts }),
- ...(stateChanged === undefined ? {} : { stateChanged }),
- ...(artifactChanged === undefined ? {} : { artifactChanged }),
- };
- }
- function isExpectedResultBestEffort(
- policy: RunawayGuardToolPolicy | undefined,
- step: RunawayGuardToolStep,
- ): boolean {
- if (!policy?.isExpectedResult) return false;
- try {
- return policy.isExpectedResult(step) === true;
- } catch {
- return false;
- }
- }
- export function toolCallIdsFromMessage(message: PiStepEndHookInput['message']): readonly string[] {
- if (message.role !== 'assistant') return [];
- return message.content.flatMap((block) => (block.type === 'toolCall' ? [block.id] : []));
- }
- export function verifiedProgressMap(
- values: readonly RunawayGuardVerifiedToolProgress[],
- expected: ReadonlySet<string>,
- ): ReadonlyMap<string, RunawayGuardVerifiedToolProgress> {
- const valid = values.filter(
- (value) =>
- isRecord(value) &&
- expected.has(value.toolCallId) &&
- typeof value.toolCallId === 'string' &&
- validProgressProjection(value),
- );
- return new Map(valid.map((value) => [value.toolCallId, value]));
- }
- export function verifiedProgressMapBestEffort(
- values: readonly RunawayGuardVerifiedToolProgress[],
- expected: ReadonlySet<string>,
- ): ReadonlyMap<string, RunawayGuardVerifiedToolProgress> {
- try {
- return verifiedProgressMap(values, expected);
- } catch {
- return new Map();
- }
- }
- export function trustedToolProvenanceMap(
- values: readonly RunawayGuardTrustedToolProvenance[],
- expected: ReadonlySet<string>,
- ): ReadonlyMap<string, RunawayGuardTrustedToolProvenance> {
- const valid = values.filter(
- (value) =>
- isRecord(value) &&
- expected.has(value.toolCallId) &&
- typeof value.toolCallId === 'string' &&
- typeof value.toolName === 'string' &&
- value.toolName.length > 0 &&
- (value.source === 'builtin' || value.source === 'captured-compatibility'),
- );
- return new Map(valid.map((value) => [value.toolCallId, value]));
- }
- export function trustedToolProvenanceMapBestEffort(
- values: readonly RunawayGuardTrustedToolProvenance[],
- expected: ReadonlySet<string>,
- ): ReadonlyMap<string, RunawayGuardTrustedToolProvenance> {
- try {
- return trustedToolProvenanceMap(values, expected);
- } catch {
- return new Map();
- }
- }
- function validProgressProjection(projection: RunawayGuardProgressProjection): boolean {
- return (
- isRecord(projection) &&
- projection.loopKey !== undefined &&
- projection.loopKey !== null &&
- projection.progressKey !== undefined &&
- projection.progressKey !== null &&
- (projection.newFacts === undefined ||
- (Number.isSafeInteger(projection.newFacts) && projection.newFacts >= 0)) &&
- (projection.stateChanged === undefined || typeof projection.stateChanged === 'boolean') &&
- (projection.artifactChanged === undefined || typeof projection.artifactChanged === 'boolean') &&
- validPollingControl(projection.polling)
- );
- }
- function validPollingControl(value: unknown): boolean {
- return (
- value === undefined ||
- (isRecord(value) &&
- (value.mode === 'continuous' || value.mode === 'reset') &&
- (value.reminderEligible === undefined || typeof value.reminderEligible === 'boolean'))
- );
- }
- function errorFamily(toolName: string, details: unknown, content: unknown): string | undefined {
- const structured = structuredErrorCode(details);
- if (structured) return `${toolName}\u0000code:${structured}`;
- const text = firstText(content);
- if (!text) return undefined;
- const normalized = text.toLowerCase();
- const category = ERROR_CATEGORIES.find(({ pattern }) => pattern.test(normalized))?.name;
- if (category) return `${toolName}\u0000category:${category}`;
- const bounded = normalized.replace(/\s+/g, ' ').trim().slice(0, 1_024);
- return bounded ? `${toolName}\u0000text:${bounded}` : undefined;
- }
- const ERROR_CATEGORIES: readonly { readonly name: string; readonly pattern: RegExp }[] = [
- { name: 'timeout', pattern: /timeout|timed out|deadline exceeded/ },
- { name: 'rate_limit', pattern: /rate.?limit|too many requests|\b429\b/ },
- { name: 'network', pattern: /network|econn|socket|dns|connection reset/ },
- { name: 'auth', pattern: /unauth|invalid api key|\b401\b/ },
- { name: 'permission', pattern: /permission|forbidden|access denied|\b403\b/ },
- { name: 'not_found', pattern: /not found|enoent|\b404\b/ },
- { name: 'invalid_argument', pattern: /invalid argument|validation failed|bad request|\b400\b/ },
- { name: 'process_exit', pattern: /exit code|non-zero|process failed/ },
- ];
- function structuredErrorCode(details: unknown): string | undefined {
- if (!isRecord(details)) return undefined;
- for (const key of ['error_code', 'errorCode', 'code', 'status']) {
- const value = details[key];
- if (typeof value === 'string' && value.length > 0 && value.length <= 128) return value;
- if (typeof value === 'number' && Number.isFinite(value)) return String(value);
- }
- return undefined;
- }
- function firstText(content: unknown): string | undefined {
- if (!Array.isArray(content)) return undefined;
- for (const block of content) {
- if (!isRecord(block) || block['type'] !== 'text') continue;
- const text = block['text'];
- if (typeof text === 'string' && text.length > 0 && text.length <= 4_096) return text;
- }
- return undefined;
- }
|