Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 52 additions & 55 deletions src/core/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,15 @@ import type { GapWork, HarnessLlmRequestRecord, HarnessTurnResult, RuntimeChoice
import { forModelContext, forSearchView } from "../harness/context-compaction.ts";
import {
renderSecurityPolicyPrompt,
quarantineReleaseKey,
securityScreenChunks,
securityScreenPayload,
toolLabelOf,
UNSCREENED_REASON,
unscreenedNotice,
type SecurityScreenVerdict,
type ToolResultScreen,
type ToolResultScreenInput,
} from "../security/security-posture.ts";
import { commandApprovalId, inputApprovalId } from "./approval-id.ts";
import { createPerTurnStrategy } from "../memory/strategies/per-turn.ts";
Expand Down Expand Up @@ -2718,13 +2724,17 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
...(effectiveTurnWallClockMs !== undefined ? { turnWallClockMs: effectiveTurnWallClockMs } : {}),
...(securityPolicy.inboundScreening === "external"
? {
screenToolResult: async (
tool: string,
result: string,
unscreenable: boolean,
): Promise<boolean | "unscreened" | "quarantine_pending"> => {
const toolLabel = tool.replace(/[^A-Za-z0-9_-]/g, "_");
if (authorizeCommand(`quarantine:${toolLabel}`, `quarantine:${toolLabel}`)) {
screenToolResult: async ({
tool,
result,
unscreenable,
provenance,
source,
}: ToolResultScreenInput): Promise<ToolResultScreen> => {
if (provenance !== "external") return { outcome: "allow" };
const toolLabel = toolLabelOf(tool);
const sourceLabel = source ? `:${source.replace(/[^A-Za-z0-9_-]/g, "_")}` : "";
if (authorizeCommand(quarantineReleaseKey(tool), quarantineReleaseKey(tool))) {
deps.auditLog.record({
at: Date.now(),
principalId: actor.id,
Expand All @@ -2734,26 +2744,33 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
status: "allowed",
detail: JSON.stringify({ reason: "human_release", tool: toolLabel }),
});
return "unscreened";
return { outcome: "unscreened" };
}
const chunks = unscreenable
? []
: securityScreenChunks(`tool_result:${toolLabel}${sourceLabel}`, result);
if (!unscreenable && chunks.length === 0) return { outcome: "allow" };
const verdicts: Array<SecurityScreenVerdict | undefined> = [];
for (let i = 0; i < chunks.length && !verdicts.some((v) => v?.decision === "strict"); i += 4) {
verdicts.push(
...(await Promise.all(
chunks.slice(i, i + 4).map((chunk) =>
classifySecurityData(chunk, actor.id, scopeId, recordScreenRequest, {
hook: "tool_response",
surface: toolLabel,
origin: input.origin.kind,
}),
),
)),
);
}
const bounded = unscreenable
? null
: securityScreenPayload({
surface: `tool_result:${toolLabel}`,
text: "",
triggered: true,
securityScreenData: result,
});
if (!unscreenable && bounded === null) return true;
const verdict =
bounded && !bounded.truncated
? await classifySecurityData(bounded.content, actor.id, scopeId, recordScreenRequest, {
hook: "tool_response",
surface: toolLabel,
origin: input.origin.kind,
})
: undefined;
if (verdict?.decision === "auto" && !verdict.unscreened) return true;
verdicts.find((v) => v?.decision === "strict") ??
(verdicts.length === chunks.length &&
verdicts.every((v) => v?.decision === "auto" && !v.unscreened)
? verdicts[0]
: undefined);
if (verdict?.decision === "auto" && !verdict.unscreened) return { outcome: "allow" };
if (verdict?.decision === "strict") {
const releaseKey = `security-screen-release:${toolLabel}`;
if (authorizeCommand(releaseKey)) {
Expand All @@ -2766,7 +2783,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
status: "allowed",
detail: JSON.stringify({ reason: "human_release", tool: toolLabel }),
});
return true;
return { outcome: "allow" };
}
deps.auditLog.record({
at: Date.now(),
Expand All @@ -2775,7 +2792,12 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
resource: input.surface ?? "unknown",
scopeLabel: scopeId,
status: "refused",
detail: JSON.stringify({ reason: "screen_verdict", tool: toolLabel }),
detail: JSON.stringify({
reason: "screen_verdict",
tool: toolLabel,
...(source ? { source } : {}),
...(verdict.reason ? { verdict: verdict.reason } : {}),
}),
});
if (!quarantineReleaseApprovals.some((qa) => qa.approvalKey === releaseKey)) {
quarantineReleaseApprovals.push({
Expand All @@ -2790,7 +2812,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
grantModes: { session: false, always: false },
});
}
return "quarantine_pending";
return { outcome: "quarantine", ...(verdict.reason ? { reason: verdict.reason } : {}) };
}
deps.auditLog.record({
at: Date.now(),
Expand All @@ -2800,10 +2822,10 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
scopeLabel: scopeId,
status: "allowed",
detail: JSON.stringify({
reason: unscreenable || bounded?.truncated ? "unscreenable_payload" : UNSCREENED_REASON,
reason: unscreenable ? "unscreenable_payload" : UNSCREENED_REASON,
}),
});
return "unscreened";
return { outcome: "unscreened" };
},
}
: {}),
Expand All @@ -2814,31 +2836,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
tools,
...(tools.credentialExecServices ? { credentialExecServices: tools.credentialExecServices } : {}),
...(tools.commandCredentialHandles ? { commandCredentialHandles: tools.commandCredentialHandles } : {}),
...(securityPolicy.inboundScreening === "external" &&
(deps.securityScreener || deps.harness.models.screenSecurity)
? {
screenExternalContent: ({ content, tool }: { content: string; tool: string; source: string }) => {
const toolLabel = tool.replace(/[^A-Za-z0-9_-]/g, "_");
if (authorizeCommand(`quarantine:${toolLabel}`, `quarantine:${toolLabel}`)) {
deps.auditLog.record({
at: Date.now(),
principalId: actor.id,
action: "security_posture.tool_result_released",
resource: input.surface ?? "unknown",
scopeLabel: scopeId,
status: "allowed",
detail: JSON.stringify({ reason: "human_release", tool: toolLabel }),
});
return Promise.resolve({ decision: "auto" as const, unscreened: true });
}
return classifySecurityData(content, actor.id, scopeId, undefined, {
hook: "tool_response",
surface: tool,
origin: input.origin.kind,
});
},
}
: {}),
...(selectedTape
? {
tapeRows: selectedTape.rows,
Expand Down
Loading