diff --git a/apps/host/server/agent/agent-bridge.mjs b/apps/host/server/agent/agent-bridge.mjs index 80c4ce13..5d122d21 100644 --- a/apps/host/server/agent/agent-bridge.mjs +++ b/apps/host/server/agent/agent-bridge.mjs @@ -610,6 +610,34 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir = return runTurn({ ...options, goal: { action: 'create', objective: text } }); } + /** + * The person changed the chat's mode while a turn (a goal and its helpers included) is running. The new preset + * is put on the session at once, the running turn's own bookkeeping follows, and questions already waiting are + * settled the way the new mode would have settled them: Bypass allows them, Plan rejects them. + */ + async function setMode(chatId, mode) { + const want = modeOf(mode); + const c = chats.get(chatId); + const sessionId = sessionMap[chatId]?.sessionId; + if (!rt || !sessionId) return { applied: false, mode: want, reason: 'no session yet' }; + if (c?.turn && c.turn.mode === want) return { applied: true, mode: want, settled: 0 }; + await applyMode(rt, sessionId, want); + let settled = 0; + if (c?.turn) { + c.turn.mode = want; + c.turn.projector.policy = MODE_PRESETS[want].approval; + if (want !== 'manual') { + const helpers = c.turn.children; + for (const [key, entry] of [...pending.entries()]) { + if (entry.sessionId !== sessionId && !helpers?.has(entry.sessionId)) continue; + pending.delete(key); + try { await rt.gw.answerEvent({ clientId: entry.clientId ?? rt.clientId, eventId: entry.eventId, outcome: want === 'bypass' ? 'allowed-once' : 'rejected' }); settled++; } catch { /* already answered */ } + } + } + } + return { applied: true, mode: want, settled }; + } + /** Answer one pending approval. `decision` is `approve` or `deny`. */ async function approve({ chatId, callId, decision }) { const sessionId = sessionMap[chatId]?.sessionId; @@ -680,7 +708,7 @@ export function createAgentBridge({ dataDir, componentsDir = null, bundledDir = /** Let go of a chat's Harness session mapping (a task starts every run in a session of its own). */ function forget(chatId) { if (chats.get(chatId)?.turn) return; delete sessionMap[chatId]; chats.delete(chatId); persist(); } - return { goalState, goalControl, harnessDir, status, runTurn, goalCommand, forget, approve, stop, pendingFor, close, sessionOf: chatId => sessionMap[chatId]?.sessionId ?? null, isRunning: chatId => !!chats.get(chatId)?.turn, _debug: () => ({ rt, sessionMap }) }; + return { goalState, goalControl, harnessDir, status, runTurn, goalCommand, forget, approve, setMode, stop, pendingFor, close, sessionOf: chatId => sessionMap[chatId]?.sessionId ?? null, isRunning: chatId => !!chats.get(chatId)?.turn, _debug: () => ({ rt, sessionMap }) }; } /** A goal that stopped by itself, in words. */ diff --git a/apps/host/server/agent/plugins/strata-gate.mjs b/apps/host/server/agent/plugins/strata-gate.mjs index b8640bf4..1b421c7e 100644 --- a/apps/host/server/agent/plugins/strata-gate.mjs +++ b/apps/host/server/agent/plugins/strata-gate.mjs @@ -166,6 +166,11 @@ export function apply(ctx) { if (!agent || exec.name === 'run_code') return next(); let preset; try { preset = effectivePreset(agent.session); } catch { return next(); } + // A helper that was set to ask while the chat was in Manual must stop asking the moment the chat leaves Manual, + // so a mode change applies to the very next tool call of a running goal and its helpers. + if (preset !== MANUAL_PRESET && agent.session.header?.origin === 'subagent' && ctx.approval.overrideOf(agent.session) === 'ask') { + agent.session.append('approval/policy', { policy: 'never' }); + } if (preset === PLAN_PRESET) return planDecision(exec) ?? next(); if (preset !== MANUAL_PRESET) return next(); const gate = manualDecision(exec); diff --git a/apps/host/server/index.mjs b/apps/host/server/index.mjs index 5914dde0..acc8802d 100644 --- a/apps/host/server/index.mjs +++ b/apps/host/server/index.mjs @@ -1633,6 +1633,12 @@ async function api(req, res, path, method, url) { try { return json(res, 200, await agent.approve({ chatId: b.sessionId, callId: b.toolId, decision: b.decision })); } catch (e) { return error(res, e.code === 'approval-gone' ? 409 : 400, e.message); } } + /* The person changed the chat's mode while a turn runs: it applies to the next tool call, goal and helpers included. */ + if (path === '/api/chat/mode' && method === 'POST') { + const b = await readBody(req); + if (typeof b.sessionId !== 'string' || !['manual', 'plan', 'bypass'].includes(b.mode)) return error(res, 400, 'sessionId and mode (manual, plan or bypass) are required'); + try { return json(res, 200, await agent.setMode(b.sessionId, b.mode)); } catch (e) { return error(res, e.code === 'mode-unavailable' ? 409 : 502, e.message); } + } /* Stop one chat's turn. Resolves once the turn has really ended, not when the cancel was admitted. */ if (path === '/api/chat/stop' && method === 'POST') { const b = await readBody(req); diff --git a/apps/host/src/components/TimeSkipCompare.tsx b/apps/host/src/components/TimeSkipCompare.tsx index f29ecffc..c0c4a8a8 100644 --- a/apps/host/src/components/TimeSkipCompare.tsx +++ b/apps/host/src/components/TimeSkipCompare.tsx @@ -1,7 +1,7 @@ import { useEffect, useMemo, useState } from 'react'; import { tr } from '../lib/i18n'; import type { TimeSkipFile } from '../lib/time-skip'; -import { diffStats, fileKind, htmlPreviewDocument, lineDiff, segmentLines, timeSkipImageUrl, wordDiff, type Segment } from '../lib/time-skip-compare'; +import { HTML_PREVIEW_SANDBOX, diffStats, fileKind, htmlPreviewDocument, needsOutsideFiles, lineDiff, segmentLines, timeSkipImageUrl, wordDiff, type Segment } from '../lib/time-skip-compare'; import { Btn, Dialog, Icon } from './ui'; const baseName = (path: string) => path.split('/').pop() || path; @@ -77,10 +77,12 @@ function FileView({ jobId, file }: { jobId: string; file: TimeSkipFile }) { if (kind === 'html') return <>
setCode(v => !v)}>{tr(code ? 'View preview' : 'View code')}
{code ? : <>{heads}
- {/* No permissions at all: no scripts, no forms, no navigation. The page also carries a policy that blocks every outside request. */} -