Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.
JavaScript agent-collaborationagent-harnessagent-orchestrationagent-teamagent-workflowai-assistantai-automationai-employeeai-productivityai-teamai-workflowautonomous-agentsclaude-code-guiclaude-coworkdesktop-agentlocal-firstmulti-agentmulti-agent-systemself-evolving-agentstask-automation
Harden local tool validation and runtime diagnostics
BlueSkyID666b9dd189eb9fe
diff --git a/src/core-agent/src/agent/runner.ts b/src/core-agent/src/agent/runner.tsindex 17959bf0..f80a7243 100644--- a/src/core-agent/src/agent/runner.ts+++ b/src/core-agent/src/agent/runner.ts@@ -1805,6 +1805,7 @@ export class AgentRunner {// Every exit point yields `{ type: "done", result }` then returns so the// consumer sees a terminal event no matter which branch wins.for (let attempt = 0; attempt <= maxRetries; attempt++) {+ let providerOutput: Extract<AgentRunEvent, { type: "provider_call" }>["output"];if (params.signal?.aborted) {const e = this.errorResult(startTime, modelId, provider.id, {kind: "timeout",@@ -1956,6 +1957,7 @@ export class AgentRunner {: persistedMessages;await this.session.flushPending(params.signal);activeProviderStartedAt = Date.now();+ providerOutput = { terminalSeen: false, textChars: 0, thinkingChars: 0, toolCalls: 0 };const streamIter = provider.stream({model: modelId,// Only the real provider turn injects per-turn ephemeral context;@@ -2004,6 +2006,7 @@ export class AgentRunner {for await (const ev of streamIter) {if (ev.type === "text_delta") {streamText += ev.text;+ providerOutput.textChars += ev.text.length;// The original partial text is already visible. Buffer each// continuation until its prefix overlap is removed, then emit only// genuinely new text so the UI never flashes duplicated prose.@@ -2016,6 +2019,7 @@ export class AgentRunner {} else if (ev.type === "thinking_delta") {const chars = Math.max(0, Math.round(Number(ev.chars) || 0));streamingThinkingChars += chars;+ providerOutput.thinkingChars += chars;yield { type: "thinking", phase: "progress", chars, text: ev.text };} else if (ev.type === "thinking_end") {yield { type: "thinking", phase: "end", chars: streamingThinkingChars };@@ -2041,6 +2045,7 @@ export class AgentRunner {inputBytes: streamingTool.inputBytes,};} else if (ev.type === "tool_use_end") {+ providerOutput.toolCalls += 1;const id = ev.id || streamingTool?.id || "";if (id || streamingTool) {yield {@@ -2084,6 +2089,18 @@ export class AgentRunner {};} else if (ev.type === "message_end") {streamStopReason = ev.stopReason;+ providerOutput.terminalSeen = true;+ providerOutput.stopReason = ev.stopReason;+ if (ev.content) {+ providerOutput.textChars = 0;+ providerOutput.thinkingChars = 0;+ providerOutput.toolCalls = 0;+ for (const block of ev.content) {+ if (block.type === "text") providerOutput.textChars += block.text.length;+ else if (block.type === "thinking") providerOutput.thinkingChars += block.thinking.length;+ else if (block.type === "tool_use") providerOutput.toolCalls += 1;+ }+ }if (ev.usage) {streamUsage = {inputTokens: ev.usage.inputTokens ?? streamUsage.inputTokens,@@ -2115,6 +2132,7 @@ export class AgentRunner {type: "provider_call",durationMs: providerCallDurationMs,outcome: "completed",+ output: providerOutput,model: streamModel,stopReason: streamStopReason,...((streamContent ? textFromContent(streamContent).length : streamText.length) > 0@@ -3208,6 +3226,7 @@ export class AgentRunner {type: "provider_call",durationMs: providerCallDurationMs,outcome: "failed",+ output: providerOutput,model: modelId,};}@@ -3262,7 +3281,7 @@ export class AgentRunner {sessionId: this.session.getSessionId(),model: modelId,tokensBefore: overflowEstimateBefore,- overflowError: formatError(err),+ overflowError: logErrorRef(err),};if (overflowRecoveryAttempted) {// Consecutive overflow with no completed call in between: thediff --git a/src/core-agent/src/agent/types.ts b/src/core-agent/src/agent/types.tsindex 00c4f863..969cefff 100644--- a/src/core-agent/src/agent/types.ts+++ b/src/core-agent/src/agent/types.ts@@ -252,6 +252,9 @@ export type AgentRunEvent =/** Low-cardinality timing for one main/final-summary provider request.* Internal telemetry only; host event mappers do not render it. */type: "provider_call";+ /** Last response at the adapter boundary; counts only, no wire content.+ * terminalSeen means message_end was observed, not a guessed default. */+ output?: { terminalSeen: boolean; stopReason?: StopReason; textChars: number; thinkingChars: number; toolCalls: number };durationMs: number;outcome: "completed" | "failed";model: string;diff --git a/src/core-agent/test/agent-runner.test.ts b/src/core-agent/test/agent-runner.test.tsindex 70b2b02d..ea70f011 100644--- a/src/core-agent/test/agent-runner.test.ts+++ b/src/core-agent/test/agent-runner.test.ts@@ -73,6 +73,64 @@ async function* streamCompletionResult(result: CompletionResult): AsyncIterable<}describe("runner error metadata", () => {+ it.each(["recovered", "unrecoverable", "exhausted"] as const)(+ "keeps provider error prose out of context-overflow logs (%s)", async mode => {+ const privateMarker = "[email protected]";+ const error = new ContextOverflowError(`request exceeds context window: ${privateMarker}`);+ const session = new Session();+ session.beginUserTurn([{ type: "text", text: "Inspect sources" }]);+ for (let round = 0; round < (mode === "unrecoverable" ? 1 : 2); round++) {+ session.addAssistantMessage([{ type: "tool_use", id: `source-${round}`, name: "inspect", input: {} }]);+ session.addToolResult(`source-${round}`, `${round}:${"x".repeat(2_000)}`);+ }+ let calls = 0;+ const provider: LLMProvider = {+ id: "mock", name: "Mock",+ async complete() { throw new Error("must not summarize"); },+ async *stream() {+ calls++;+ yield { type: "message_start" as const };+ if (calls === 1 || mode !== "recovered") throw error;+ yield { type: "message_end" as const, stopReason: "end_turn" as const,+ content: [{ type: "text" as const, text: "done" }], model: "mock-model" };+ },+ async validateAuth() { return true; },+ };+ const registry = new ProviderRegistry();+ registry.registerFactory("mock", () => provider);+ const runner = new AgentRunner({ session, providers: registry, tools: [],+ config: createConfig({ agent: { defaultProvider: "mock", defaultModel: "mock-model" },+ models: { catalog: { "mock-model": { provider: "mock", model: "mock-model",+ contextWindow: 32_000, maxOutputTokens: 4_096 } } } }),+ });+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});+ const failure = vi.spyOn(console, "error").mockImplementation(() => {});+ try {+ const events: AgentRunEvent[] = [];+ for await (const event of runner.runStream({ message: "Inspect sources", resumeActiveTurn: true })) events.push(event);+ const done = events.find((e): e is Extract<AgentRunEvent, { type: "done" }> => e.type === "done");+ expect(calls).toBe(mode === "unrecoverable" ? 1 : 2);+ if (mode === "recovered") expect(done?.result.text).toBe("done");+ else expect(done?.result.meta.error?.kind).toBe("context_overflow");+ const logs = [...warn.mock.calls, ...failure.mock.calls];+ const expected = mode === "recovered" ? "context overflow recovery applied"+ : mode === "unrecoverable" ? "context overflow with nothing to recover"+ : "context overflow after recovery";+ const entry = logs.find(args => args[1] === expected);+ expect(entry).toBeDefined();+ expect(JSON.stringify(logs)).not.toContain(privateMarker);+ expect(entry?.[2]).toMatchObject({ overflowError: {+ name: "ContextOverflowError", code: "CONTEXT_OVERFLOW",+ message_hash: createHash("sha256").update(error.message).digest("hex").slice(0, 12),+ message_chars: error.message.length,+ } });+ } finally {+ warn.mockRestore();+ failure.mockRestore();+ }+ },+ );+it("prefers a nested provider business code over a generic wrapper code", () => {const original = Object.assign(new Error("积分不足"), {code: "orkas_llm_quota_exceeded",@@ -5850,12 +5908,14 @@ describe("AgentRunner", () => {expect(collected.filter((event) => event.type === "provider_call")).toEqual([expect.objectContaining({outcome: "completed",+ output: { terminalSeen: true, stopReason: "tool_use", textChars: 0, thinkingChars: 0, toolCalls: 1 },model: "mock-model",usage: { inputTokens: 5, outputTokens: 3, totalTokens: 8 },reasoningBoundary: { structured: true, literalLeadingText: false },}),expect.objectContaining({outcome: "completed",+ output: expect.objectContaining({ terminalSeen: true, stopReason: "end_turn", toolCalls: 0 }),model: "mock-model",usage: { inputTokens: 5, outputTokens: 3, totalTokens: 8 },reasoningBoundary: { structured: false, literalLeadingText: true },@@ -9386,3 +9446,64 @@ it.each([vi.clearAllTimers(); vi.useRealTimers();}});++describe('terminal output evidence', () => {+ it.each(['text', 'reasoning', 'empty', 'eof'] as const)('records %s without inferring tools or terminal events', async (kind) => {+ const thought = '<dots_function_call><read_files>private</read_files></dots_function_call>';+ const provider = createMockProvider([{ content: [], stopReason: 'end_turn',+ model: 'mock-model', usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 } }]);+ provider.stream = async function* () {+ if (kind === 'text' || kind === 'eof') yield { type: 'text_delta', text: 'hello' };+ if (kind === 'reasoning') yield { type: 'thinking_delta', text: thought, chars: thought.length };+ if (kind !== 'eof') yield { type: 'message_end', stopReason: 'end_turn', content:+ kind === 'reasoning' ? [{ type: 'thinking', thinking: thought }] :+ kind === 'text' ? [{ type: 'text', text: 'hello' }] : [] };+ };+ const registry = new ProviderRegistry();+ registry.registerFactory('mock', () => provider);+ const runner = new AgentRunner({ providers: registry, tools: [], config: createConfig({+ agent: { defaultProvider: 'mock', defaultModel: 'mock-model' },+ }) });+ const events = await collectRunEvents(runner, 'synthetic diagnostic boundary');+ const calls = events.filter((event) => event.type === 'provider_call');+ expect(calls).toHaveLength(1);+ expect(calls[0]).toMatchObject({ output: {+ terminalSeen: kind !== 'eof', textChars: kind === 'text' || kind === 'eof' ? 5 : 0,+ thinkingChars: kind === 'reasoning' ? thought.length : 0, toolCalls: 0,+ ...(kind !== 'eof' ? { stopReason: 'end_turn' } : {}),+ } });+ if (kind === 'eof') expect(calls[0].output).not.toHaveProperty('stopReason');+ expect(events.filter((event) => event.type === 'tool_start')).toEqual([]);+ expect(events.filter((event) => event.type === 'done')).toHaveLength(1);+ });+});++ it('lets the production runner summarize after six executed rounds without executing the seventh proposal', async () => {+ const { AgentRunner, ProviderRegistry, Session, createConfig } = await import('../src/index');+ const config = createConfig({ agent: { defaultProvider: 'openai-codex', defaultModel: 'gpt-5.5', maxRetries: 0, maxToolLoops: 6 }, evolution: { enabled: false } });+ const providers = new ProviderRegistry(config);+ const diagnostics: string[] = [];+ const warn = vi.spyOn(console, 'warn').mockImplementation((...args) => diagnostics.push(args.map(String).join(' ')));+ let requests = 0;+ const execute = vi.fn(async () => ({ content: '{}' }));+ providers.registerFactory('openai-codex', () => ({ id: 'openai-codex', name: 'fixture', validateAuth: async () => true,+ async *stream(): AsyncIterable<StreamEvent> {+ requests++;+ yield { type: 'message_end', stopReason: 'tool_use', model: 'gpt-5.5', content: [{ type: 'tool_use', id: String(requests), name: 'probe', input: { page: requests } }] };+ },+ async complete(params) {+ requests++; expect(params.tools).toBeUndefined();+ return { content: [{ type: 'text', text: 'No supported operation was found.' }], model: 'gpt-5.5', stopReason: 'end_turn', usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 } };+ },+ }));+ try {+ const runner = new AgentRunner({ config, providers, session: new Session(), tools: [{ name: 'probe', description: 'Read a fixture page', inputSchema: { type: 'object' }, execute }] });+ let result: any;+ for await (const event of runner.runStream({ message: 'Inspect the fixture', systemPrompt: 'Fixture instructions' })) if (event.type === 'done') result = event.result;+ expect(requests).toBe(8); expect(execute).toHaveBeenCalledTimes(6);+ expect(result.text).toBe('No supported operation was found.');+ expect(result.meta.error).toBeUndefined();+ expect(diagnostics.length).toBeGreaterThan(0);+ expect(diagnostics.every(s => s.startsWith('[agent-runner] run_convergence: nudged model to finish near limit') || s.startsWith('[agent-runner] Run convergence tool-loop limit reached'))).toBe(true);+ } finally { warn.mockRestore(); }+ });diff --git a/src/main/features/chat_attachments.ts b/src/main/features/chat_attachments.tsindex de45aa4a..439816cc 100644--- a/src/main/features/chat_attachments.ts+++ b/src/main/features/chat_attachments.ts@@ -1026,7 +1026,7 @@ export function materializeLocalDisplaySvg(const decodedRef = ref.replace(/&/g, '&');const imageAbs = path.resolve(root, decodedRef);const rel = path.relative(root, imageAbs);- if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) {+ if (!rel || rel.startsWith('..') || path.isAbsolute(rel) || !isPathAllowed(imageAbs, [root])) {inlineError = { code: 'bad_input', error: 'SVG image reference escapes its directory' };return full;}diff --git a/src/main/features/marketplace_reconcile.ts b/src/main/features/marketplace_reconcile.tsindex 0c58ed6c..56846c94 100644--- a/src/main/features/marketplace_reconcile.ts+++ b/src/main/features/marketplace_reconcile.ts@@ -272,7 +272,9 @@ export async function checkServerUpdatesForInstalls(if (!_isInstallRowAppCompatible({ id: a.id, min_app_version: server.min_app_version }, 'agent')) {continue;}- const contentUpgrade = compareVersions(server.version, a.version) === 1;+ // Missing URLs are legacy responses; explicit empty URLs reject this release.+ const contentUpgrade = compareVersions(server.version, a.version) === 1+ && (server.agent_json_url === undefined || Boolean(server.agent_json_url));const defaultInstallChanged = typeof server.default_install === 'boolean'&& a.default_install !== server.default_install;const currentStatus = a.status || a.state;@@ -327,7 +329,9 @@ export async function checkServerUpdatesForInstalls(if (!_isInstallRowAppCompatible({ id: s.id, min_app_version: server.min_app_version }, 'skill')) {continue;}- const contentUpgrade = compareVersions(server.version, s.version) === 1;+ // Missing URLs are legacy responses; explicit empty URLs reject this release.+ const contentUpgrade = compareVersions(server.version, s.version) === 1+ && (server.bundle_url === undefined || Boolean(server.bundle_url));const defaultInstallChanged = typeof server.default_install === 'boolean'&& s.default_install !== server.default_install;const currentStatus = s.status || s.state;diff --git a/src/main/model/core-agent/client.ts b/src/main/model/core-agent/client.tsindex 0743b2ed..1ab18699 100644--- a/src/main/model/core-agent/client.ts+++ b/src/main/model/core-agent/client.ts@@ -1,3 +1,4 @@+import { terminalOutputSnapshot, type TerminalOutput } from '../../util/terminal-output-diagnostics';/*** core-agent-backed implementation of `chatWithModel` / `streamChatWithModel`.*@@ -502,6 +503,7 @@ type RunTimelineLogEntry = {};export interface ModelRunLogDiagnostics {+ terminalOutput?: TerminalOutput;startedAtMs: number;rawEventCount: number;streamEventCount: number;@@ -1097,6 +1099,11 @@ export function recordModelRawEventForLog(stats: ModelRunLogDiagnostics, ev: unkbreak;}case 'provider_call': {+ const output = e.output as Record<string, unknown> | undefined;+ stats.terminalOutput = terminalOutputSnapshot({ version: 1,+ provider_terminal_seen: output?.terminalSeen, provider_stop_reason: output?.stopReason,+ provider_text_chars: output?.textChars, provider_thinking_chars: output?.thinkingChars,+ provider_tool_calls: output?.toolCalls });const durationMs = Math.max(0, Math.round(finiteNumber(e.durationMs) || 0));const usage = safeUsageForLog(e.usage);stats.providerCallCount += 1;@@ -1222,6 +1229,7 @@ export function recordModelRawEventForLog(stats: ModelRunLogDiagnostics, ev: unkstats.resultTextChars = typeof (e.result as { text?: unknown } | undefined)?.text === 'string'? ((e.result as { text: string }).text.length): stats.resultTextChars;+ stats.terminalOutput = terminalOutputSnapshot({ ...stats.terminalOutput, version: 1, runner_text_chars: typeof (e.result as { text?: unknown } | undefined)?.text === 'string' ? stats.resultTextChars : undefined });stats.resultContentBlocks = Array.isArray((e.result as { content?: unknown } | undefined)?.content)? ((e.result as { content: unknown[] }).content.length): stats.resultContentBlocks;@@ -1267,6 +1275,7 @@ export function recordModelStreamEventForLog(stats: ModelRunLogDiagnostics, ev:stats.eventPayloads += 1;break;case 'final':+ stats.terminalOutput = terminalOutputSnapshot({ ...stats.terminalOutput, version: 1, mapped_text_chars: typeof ev.text === 'string' ? ev.text.length : 0 });stats.finalEvents += 1;noteRunTimelineForLog(stats, 'client_final', nowMs, `chars=${typeof ev.text === 'string' ? ev.text.length : 0}`);break;@@ -1275,6 +1284,13 @@ export function recordModelStreamEventForLog(stats: ModelRunLogDiagnostics, ev:noteRunTimelineForLog(stats, 'client_error', nowMs, `chars=${typeof ev.text === 'string' ? ev.text.length : 0} aborted=${ev.aborted ? 'true' : 'false'}`);break;case 'done':+ // A host abort/timeout may stop consuming a newer call before its receipt.+ // Do not attribute the previous completed response to that terminal.+ if (stats.doneRawEventMs === undefined) {+ stats.terminalOutput = terminalOutputSnapshot({ version: 1,+ mapped_text_chars: stats.terminalOutput?.mapped_text_chars });+ }+ stats.terminalOutput = terminalOutputSnapshot({ ...stats.terminalOutput, version: 1, mapped_text_chars: stats.terminalOutput?.mapped_text_chars ?? 0 });noteRunTimelineForLog(stats, 'client_done', nowMs);break;default:@@ -1348,6 +1364,7 @@ export function summarizeModelRunForLog(stats: ModelRunLogDiagnostics, nowMs = DstopReason: stats.stopReason,errorKind: stats.errorKind,usage: stats.usage,+ terminal_output: terminalOutputSnapshot(stats.terminalOutput),resultTextChars: stats.resultTextChars,resultContentBlocks: stats.resultContentBlocks,toolLoops: stats.toolLoops,diff --git a/src/main/model/core-agent/local-tools.ts b/src/main/model/core-agent/local-tools.tsindex d198f8f9..86ac3b9c 100644--- a/src/main/model/core-agent/local-tools.ts+++ b/src/main/model/core-agent/local-tools.ts@@ -101,6 +101,7 @@ import {type ExternalMutationFinding,} from './external-mutation-risk';import { requestBashDecision, type BashPermissionFact } from './bash-permissions';+import { librarySaveFieldError } from '../../util/library-save-input';import { markdownToPdf, htmlToPdf } from '../../util/md-to-pdf';import { uniquifyPathForWrite, renderRenameSignal } from '../../util/uniquify-path';import { isPathAllowed, resolveSandboxEntry, resolveSandboxRoot, type PathAllowedOptions } from '../../util/path-sandbox';@@ -5746,16 +5747,13 @@ function createLibrarySaveTool(opts: LocalToolsOpts): AgentTool {required: ['source_path'],},async execute(input, ctx): Promise<ToolResult> {- if (Object.keys(input).some((key) => !['source_path', 'name', 'action', 'expected_revision'].includes(key))- || (input.action !== undefined && input.action !== 'save' && input.action !== 'checkout')- || (input.expected_revision !== undefined && (typeof input.expected_revision !== 'string' || !input.expected_revision))) {- return { content: errText('E_BAD_INPUT', 'invalid library_save fields'), isError: true };- }+ const fieldError = librarySaveFieldError(input);+ if (fieldError) return { content: errText('E_BAD_INPUT', fieldError), isError: true };if (!opts.userId || !opts.projectId) {return { content: errText('E_NO_PROJECT', 'library_save is only available inside a project conversation'), isError: true };}const rawSource = typeof input.source_path === 'string' ? input.source_path.trim() : '';- if (!rawSource) return { content: errText('E_BAD_INPUT', '`source_path` is required'), isError: true };+ if (!rawSource) return { content: errText('E_BAD_INPUT', 'source_path must be a non-empty string'), isError: true };const sourceAbs = resolveAbs(ctx, rawSource);// Path sandbox at entry: only a file the agent produced inside its own// workspace may be copied into the shared, synced project library.@@ -5765,7 +5763,7 @@ function createLibrarySaveTool(opts: LocalToolsOpts): AgentTool {const name = typeof input.name === 'string' ? input.name.trim() : '';const targetName = name || path.basename(sourceAbs);if (input.action === 'checkout' && (!name || input.expected_revision !== undefined)) {- return { content: errText('E_BAD_INPUT', 'checkout requires name and does not accept expected_revision'), isError: true };+ return { content: errText('E_BAD_INPUT', !name ? 'name must be a non-empty string for checkout' : 'expected_revision is not accepted for checkout'), isError: true };}if (input.action === 'checkout' || input.expected_revision !== undefined) {const result = input.action === 'checkout'diff --git a/src/main/model/core-agent/pdf-tools.ts b/src/main/model/core-agent/pdf-tools.tsindex b4dae74d..ec22af2c 100644--- a/src/main/model/core-agent/pdf-tools.ts+++ b/src/main/model/core-agent/pdf-tools.ts@@ -452,7 +452,7 @@ function createEditPdfTool(opts: PdfToolsOpts): AgentTool {'merge', 'extract_pages', 'delete_pages', 'reorder_pages', 'rotate_pages','watermark', 'overlay_text', 'overlay_image', 'fill_form',]);- if (!validActions.has(action)) return errResult('E_BAD_INPUT', 'unsupported PDF action');+ if (!validActions.has(action)) return errResult('E_BAD_INPUT', `action must be one of: ${[...validActions].join(', ')}`);const unexpected = Object.keys(input).filter((key) => !PDF_ACTION_FIELDS[action].has(key)).sort();if (unexpected.length) {return errResult(diff --git a/src/main/util/library-save-input.ts b/src/main/util/library-save-input.tsnew file mode 100644index 00000000..a5193a9d--- /dev/null+++ b/src/main/util/library-save-input.ts@@ -0,0 +1,17 @@+/** Shared rejection facts for native and CLI Library saves; no input rewriting. */+const FIELDS = ['source_path', 'name', 'action', 'expected_revision'];++export function librarySaveFieldError(input: Record<string, unknown>): string | undefined {+ const unknown = Object.keys(input).find((key) => !FIELDS.includes(key));+ if (unknown !== undefined) {+ const field = unknown.length > 80 ? `${unknown.slice(0, 80)}…` : unknown;+ return `unsupported field ${JSON.stringify(field)}; allowed fields: ${FIELDS.join(', ')}`;+ }+ if (input.action !== undefined && input.action !== 'save' && input.action !== 'checkout') {+ return 'action must be "save" or "checkout"';+ }+ if (input.expected_revision !== undefined && (typeof input.expected_revision !== 'string' || !input.expected_revision)) {+ return 'expected_revision must be a non-empty string';+ }+ return undefined;+}diff --git a/src/main/util/terminal-output-diagnostics.ts b/src/main/util/terminal-output-diagnostics.tsnew file mode 100644index 00000000..ed7d1921--- /dev/null+++ b/src/main/util/terminal-output-diagnostics.ts@@ -0,0 +1,39 @@+/** Last-response metadata only. Missing observations must never become zeros. */+export type TerminalOutput = {+ version: 1;+ output_kind: string;+ provider_terminal_seen?: boolean;+ provider_stop_reason?: string;+ provider_text_chars?: number;+ provider_thinking_chars?: number;+ provider_tool_calls?: number;+ runner_text_chars?: number;+ mapped_text_chars?: number;+};++export const OUTPUT_KINDS = new Set(['text', 'reasoning_only', 'tool_calls', 'empty', 'unterminated', 'unknown']);+const STOP_REASONS = new Set(['end_turn', 'tool_use', 'max_tokens', 'stop_sequence', 'unknown']);++export function terminalOutputSnapshot(value: unknown): TerminalOutput {+ const raw = value && typeof value === 'object' ? value as Record<string, unknown> : {};+ const out: TerminalOutput = { version: 1, output_kind: 'unknown' };+ if (raw.version !== 1) return out;+ if (typeof raw.provider_terminal_seen === 'boolean') out.provider_terminal_seen = raw.provider_terminal_seen;+ if (typeof raw.provider_stop_reason === 'string' && STOP_REASONS.has(raw.provider_stop_reason)) {+ out.provider_stop_reason = raw.provider_stop_reason;+ }+ for (const key of ['provider_text_chars', 'provider_thinking_chars', 'provider_tool_calls', 'runner_text_chars', 'mapped_text_chars'] as const) {+ const n = raw[key];+ if (typeof n === 'number' && Number.isSafeInteger(n) && n >= 0) out[key] = Math.min(n, 1_000_000_000);+ }+ if (out.provider_terminal_seen === false) out.output_kind = 'unterminated';+ else if (out.provider_terminal_seen === true) {+ if ((out.provider_tool_calls ?? 0) > 0) out.output_kind = 'tool_calls';+ else if ((out.provider_text_chars ?? 0) > 0) out.output_kind = 'text';+ else if (out.provider_tool_calls === 0 && out.provider_text_chars === 0) {+ if ((out.provider_thinking_chars ?? 0) > 0) out.output_kind = 'reasoning_only';+ else if (out.provider_thinking_chars === 0) out.output_kind = 'empty';+ }+ }+ return out;+}diff --git a/test/main/features/chat_attachments.test.ts b/test/main/features/chat_attachments.test.tsindex 7434aba9..20cb1228 100644--- a/test/main/features/chat_attachments.test.ts+++ b/test/main/features/chat_attachments.test.ts@@ -1090,6 +1090,30 @@ describe('chat_attachments › resolveLocalMediaPath', () => {fs.rmSync(sandbox, { recursive: true, force: true });});+ it.skipIf(process.platform === 'win32')('rejects a legacy SVG raster symlink escape and recovers with a local image', async () => {+ const { mod, sandbox } = await setup();+ try {+ const folder = path.join(sandbox, 'sheet');+ fs.mkdirSync(folder);+ const outside = path.join(sandbox, 'outside.png');+ const bytes = await makePng();+ fs.writeFileSync(outside, bytes);+ const linked = path.join(folder, 'frame.png');+ fs.symlinkSync(outside, linked);+ const svg = path.join(folder, 'contact-sheet.svg');+ fs.writeFileSync(svg, '<svg xmlns="http://www.w3.org/2000/svg"><image href="frame.png"/></svg>');+ expect(mod.materializeLocalDisplaySvg(svg)).toEqual({+ ok: false, code: 'bad_input', error: 'SVG image reference escapes its directory',+ });+ expect(fs.readFileSync(outside)).toEqual(bytes);+ fs.unlinkSync(linked);+ fs.writeFileSync(linked, bytes);+ const recovered = mod.materializeLocalDisplaySvg(svg);+ expect(recovered.ok).toBe(true);+ if (recovered.ok) expect(recovered.body).toContain('href="data:image/png;base64,');+ } finally { fs.rmSync(sandbox, { recursive: true, force: true }); }+ });+it('does not let legacy SVG image references escape their directory', async () => {const { mod, sandbox } = await setup();const p = path.join(sandbox, 'contact-sheet.svg');diff --git a/test/main/features/group_chat/browser-tool.test.ts b/test/main/features/group_chat/browser-tool.test.tsindex 499612f9..697f04f2 100644--- a/test/main/features/group_chat/browser-tool.test.ts+++ b/test/main/features/group_chat/browser-tool.test.ts@@ -187,6 +187,20 @@ describe('browser model tool', () => {});});+ it.each([+ ['tab_id', ['bad', '0123456789AB', '0123456789abc'], { operation: 'observe' }, 'must contain exactly 12 lowercase hexadecimal characters'],+ ['element_ref', ['bad', 'e0', 'e01'], { operation: 'act', page_id: 'page-1', page_action: 'click' }, 'must be "e" followed by a positive integer without leading zeros'],+ ] as const)('reports the %s format before dispatch', async (field, values, input, message) => {+ const deps = callbacks();+ const tool = buildBrowserTool(deps);+ for (const value of values) {+ const { result, body } = await run(tool, { ...input, [field]: value });+ expect(result.isError).toBe(true);+ expect(body).toEqual({ ok: false, error: `${field}: ${message}` });+ }+ for (const callback of Object.values(deps)) expect(callback).not.toHaveBeenCalled();+ });+it('rejects missing conditional inputs before a browser callback runs', async () => {const deps = callbacks();const tool = buildBrowserTool(deps);diff --git a/test/main/features/html_preview.test.ts b/test/main/features/html_preview.test.tsindex 1c3e4d02..dc6bffec 100644--- a/test/main/features/html_preview.test.ts+++ b/test/main/features/html_preview.test.ts@@ -794,6 +794,32 @@ describe('file viewer check', () => {expect(result.evidence.ok).toBe(false);});+ it('confines linked media, documents and SVG image references to the entry directory', async () => {+ const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'orkas-preview-linked-'));+ try {+ for (const name of ['image.png', 'document.html']) {+ fs.writeFileSync(path.join(outside, name), name.endsWith('.png') ? png1x1 : 'outside document');+ fs.symlinkSync(path.join(outside, name), path.join(root, name));+ }+ fs.writeFileSync(path.join(root, 'sheet.svg'), '<svg xmlns="http://www.w3.org/2000/svg"><image href="image.png"/></svg>');+ fs.writeFileSync(path.join(root, 'inside.png'), png1x1);+ fs.symlinkSync(path.join(root, 'inside.png'), path.join(root, 'alias.png'));+ const linkedUrl = chatMediaLocalUrl(path.join(root, 'image.png'));+ const runtime = fakeRuntime({ extraRequests: [linkedUrl] });+ await renderResponsiveHtmlPreview(path.join(root, 'index.html'), desktop, runtime.deps, { fileViewer: true });+ expect(runtime.requestDecisions).toContainEqual({ url: linkedUrl, cancel: true });+ for (const name of ['image.png', 'document.html']) {+ expect((await servedBy(runtime, path.join(root, name))).status).toBe(403);+ }+ const svg = await servedBy(runtime, path.join(root, 'sheet.svg'));+ expect(svg.status).toBe(400);+ expect(await svg.text()).not.toContain(png1x1.toString('base64'));+ const inside = await servedBy(runtime, path.join(root, 'alias.png'));+ expect(inside.status).toBe(200);+ expect(Buffer.from(await inside.arrayBuffer())).toEqual(png1x1);+ } finally { fs.rmSync(outside, { recursive: true, force: true }); }+ });+it('keeps file:// loading by default for generated artifact checks', async () => {const runtime = fakeRuntime();await renderResponsiveHtmlPreview(path.join(root, 'index.html'), desktop, runtime.deps);diff --git a/test/main/features/marketplace_reconcile.test.ts b/test/main/features/marketplace_reconcile.test.tsindex a0fb3346..d1d90b80 100644--- a/test/main/features/marketplace_reconcile.test.ts+++ b/test/main/features/marketplace_reconcile.test.ts@@ -268,6 +268,30 @@ describe('marketplace reconcile', () => {});});+ it.each(['agent', 'skill'] as const)('preserves installed %s content when the catalog explicitly rejects its download URL', async (kind) => {+ const installs = await import('../../../src/main/features/marketplace_installs');+ const add = kind === 'agent' ? installs.addAgentInstall : installs.addSkillInstall;+ const urlKey = kind === 'agent' ? 'agent_json_url' : 'bundle_url';+ const previous = { id: 'item', version: '1.0.0', published_at: 100,+ updated_at: 100, [urlKey]: 'https://example.test/old-content', status: 'approved' };+ await add('u1', previous);+ postJsonMock.mockResolvedValue({ list: [{ id: 'item', version: '2.0.0',+ published_at: 200, updated_at: 200, [urlKey]: '', status: 'archived' }], total: 1 });+ const reconcile = await import('../../../src/main/features/marketplace_reconcile');+ await reconcile.checkServerUpdatesForInstalls('u1');+ const manifest = await installs.readInstalls('u1');+ expect(manifest[kind === 'agent' ? 'agents' : 'skills'][0]).toMatchObject({+ ...previous, status: 'archived',+ });+ // An unavailable new release must remain eligible for a later valid update.+ postJsonMock.mockResolvedValue({ list: [{ id: 'item', version: '2.0.0',+ published_at: 200, updated_at: 200, [urlKey]: 'https://example.test/new-content', status: 'approved' }], total: 1 });+ await reconcile.checkServerUpdatesForInstalls('u1');+ expect((await installs.readInstalls('u1'))[kind === 'agent' ? 'agents' : 'skills'][0]).toMatchObject({+ version: '2.0.0', [urlKey]: 'https://example.test/new-content', status: 'approved',+ });+ });+it('ignores catalog freshness and private bundle changes without a version bump', async () => {postJsonMock.mockImplementation(async (p: string) => {if (p === '/marketplace/agents/list') {diff --git a/test/main/model/core-agent/client.test.ts b/test/main/model/core-agent/client.test.tsindex ce801ad8..fd3e02a7 100644--- a/test/main/model/core-agent/client.test.ts+++ b/test/main/model/core-agent/client.test.ts@@ -831,3 +831,37 @@ describe('core-agent client skill sandbox env', () => {}]);});});++it('retains the last response boundary beyond the per-round evidence cap without inventing zeros', async () => {+ const client = await import('../../../../src/main/model/core-agent/client');+ const stats = client.createModelRunLogDiagnostics();+ for (let i = 0; i < 100; i++) client.recordModelRawEventForLog(stats, {+ type: 'provider_call', durationMs: 1, outcome: 'completed', model: 'synthetic',+ output: { terminalSeen: true, stopReason: 'end_turn', textChars: 12, thinkingChars: 0, toolCalls: 0 },+ });+ client.recordModelRawEventForLog(stats, {+ type: 'provider_call', durationMs: 1, outcome: 'completed', model: 'synthetic',+ output: { terminalSeen: true, stopReason: 'end_turn', textChars: 0, thinkingChars: 188, toolCalls: 0 },+ });+ client.recordModelRawEventForLog(stats, { type: 'done', result: { text: '', content: [], meta: {} } });+ client.recordModelStreamEventForLog(stats, { type: 'done' });+ const snapshot = client.summarizeModelRunForLog(stats).terminal_output;+ expect(snapshot).toMatchObject({ version: 1, provider_terminal_seen: true,+ provider_stop_reason: 'end_turn', provider_text_chars: 0, provider_thinking_chars: 188,+ provider_tool_calls: 0, runner_text_chars: 0, mapped_text_chars: 0, output_kind: 'reasoning_only' });+ client.recordModelRawEventForLog(stats, { type: 'provider_call', durationMs: 1, outcome: 'failed', model: 'legacy' });+ expect(client.summarizeModelRunForLog(stats).terminal_output).toMatchObject({ output_kind: 'unknown' });+ expect(client.summarizeModelRunForLog(stats).terminal_output).not.toHaveProperty('provider_text_chars');+});+++it('does not attribute a previous completed response to a host-interrupted run', async () => {+ const client = await import('../../../../src/main/model/core-agent/client');+ const stats = client.createModelRunLogDiagnostics(1000);+ client.recordModelRawEventForLog(stats, { type: 'provider_call', outcome: 'completed', durationMs: 5,+ output: { terminalSeen: true, stopReason: 'tool_use', textChars: 0, thinkingChars: 8, toolCalls: 1 } });+ // The next call never delivers its receipt or runner done; host ends the stream.+ client.recordModelStreamEventForLog(stats, { type: 'done' });+ expect(client.summarizeModelRunForLog(stats).terminal_output)+ .toEqual({ version: 1, output_kind: 'unknown', mapped_text_chars: 0 });+});diff --git a/test/main/model/core-agent/pdf-tools.test.ts b/test/main/model/core-agent/pdf-tools.test.tsindex 6127a9e3..e8e482de 100644--- a/test/main/model/core-agent/pdf-tools.test.ts+++ b/test/main/model/core-agent/pdf-tools.test.ts@@ -65,6 +65,18 @@ describe('PDF built-in tools', () => {expect(rejected.content).toContain('edit_pdf(overlay_text) does not accept: input_paths');});+ it.each([undefined, 'not-an-action'])('reports allowed PDF actions before writing for %s', async action => {+ const source = path.join(root, 'source.pdf');+ await makePdf(source);+ const before = fs.readFileSync(source);+ const result = await tool('edit_pdf').execute({ action, input_path: 'source.pdf', output_path: 'output.pdf' }, context());+ expect(result.isError).toBe(true);+ expect(result.content).toBe('E_BAD_INPUT: action must be one of: merge, extract_pages, delete_pages, reorder_pages, rotate_pages, watermark, overlay_text, overlay_image, fill_form');+ expect(written).toEqual([]);+ expect(fs.existsSync(path.join(root, 'output.pdf'))).toBe(false);+ expect(fs.readFileSync(source)).toEqual(before);+ });+it('keeps heavyweight PDF and canvas runtimes behind execution-time imports', () => {const source = fs.readFileSync(path.resolve(__dirname, '../../../../src/main/model/core-agent/pdf-tools.ts'),diff --git a/test/main/model/library-save-tool.test.ts b/test/main/model/library-save-tool.test.tsindex 4b00db97..53f74950 100644--- a/test/main/model/library-save-tool.test.ts+++ b/test/main/model/library-save-tool.test.ts@@ -123,15 +123,32 @@ describe('local-tools › library_save', () => {});it.each([- { action: 'checkout' },- { action: 'checkout', name: 'file.md', expected_revision: 'a'.repeat(64) },- { expected_revision: '' },- { action: 'overwrite' },- { project_id: 'p_forged' },- { action: 'checkout', name: 'file.md', source_path: '/etc/new.md' },- ])('rejects invalid editing arguments without calling storage: %j', async (args) => {+ [{ action: 'checkout' }, 'name must be a non-empty string for checkout'],+ [{ action: 'checkout', name: 'file.md', expected_revision: 'PRIVATE' }, 'expected_revision is not accepted for checkout'],+ [{ expected_revision: '' }, 'expected_revision must be a non-empty string'],+ [{ expected_revision: 7 }, 'expected_revision must be a non-empty string'],+ [{ action: 'overwrite' }, 'action must be "save" or "checkout"'],+ [{ project_id: 'PRIVATE' }, 'unsupported field "project_id"'],+ [{ action: 'checkout', name: 'file.md', source_path: '/etc/new.md' }, 'E_PATH_OUT_OF_SCOPE'],+ ])('identifies invalid editing arguments without calling storage: %j', async (args, message) => {const tool = await librarySaveTool(PID);- expect((await tool!.execute({ source_path: 'edit.md', ...args }, ctx())).isError).toBe(true);+ const result = await tool!.execute({ source_path: 'edit.md', ...args }, ctx());+ expect(result.isError).toBe(true);+ expect(result.content).toContain(message);+ expect(result.content).not.toContain('PRIVATE');+ expect(copyMock).not.toHaveBeenCalled();+ expect(checkoutMock).not.toHaveBeenCalled();+ expect(replaceMock).not.toHaveBeenCalled();+ });++ it('bounds and escapes an unknown field name without echoing its value', async () => {+ const tool = await librarySaveTool(PID);+ const result = await tool!.execute({ source_path: 'file.md', ['x\n'.repeat(5000)]: 'PRIVATE' }, ctx());+ expect(result.isError).toBe(true);+ expect(result.content).toContain('unsupported field "x\\nx');+ expect(result.content).toContain('allowed fields: source_path, name, action, expected_revision');+ expect(result.content).not.toContain('PRIVATE');+ expect(result.content.length).toBeLessThan(650);expect(copyMock).not.toHaveBeenCalled();expect(checkoutMock).not.toHaveBeenCalled();expect(replaceMock).not.toHaveBeenCalled();diff --git a/test/main/util/terminal-output-diagnostics.test.ts b/test/main/util/terminal-output-diagnostics.test.tsnew file mode 100644index 00000000..ef114781--- /dev/null+++ b/test/main/util/terminal-output-diagnostics.test.ts@@ -0,0 +1,26 @@+import { describe, expect, it } from 'vitest';+import { terminalOutputSnapshot } from '../../../src/main/util/terminal-output-diagnostics';++describe('last-response output evidence', () => {+ it.each([+ [false, 0, 0, 0, 'unterminated'],+ [true, 0, 0, 0, 'empty'],+ [true, 0, 188, 0, 'reasoning_only'],+ [true, 5, 188, 0, 'text'],+ [true, 5, 188, 1, 'tool_calls'],+ ])('classifies observed boundaries (%s, %s, %s, %s)', (seen, text, thinking, tools, kind) => {+ expect(terminalOutputSnapshot({ version: 1, provider_terminal_seen: seen,+ provider_text_chars: text, provider_thinking_chars: thinking, provider_tool_calls: tools,+ }).output_kind).toBe(kind);+ });+ it('keeps missing evidence unknown and rejects private or malformed fields', () => {+ expect(terminalOutputSnapshot(undefined)).toEqual({ version: 1, output_kind: 'unknown' });+ expect(terminalOutputSnapshot({ version: 1, provider_terminal_seen: true }).output_kind).toBe('unknown');+ expect(terminalOutputSnapshot({ version: 1, output_kind: 'reasoning_only',+ provider_stop_reason: 'PRIVATE', provider_text_chars: NaN, provider_thinking_chars: -1,+ provider_tool_calls: true, runner_text_chars: 'PRIVATE', mapped_text_chars: 0, text: 'PRIVATE',+ })).toEqual({ version: 1, output_kind: 'unknown', mapped_text_chars: 0 });+ expect(terminalOutputSnapshot({ version: 1, runner_text_chars: 2e9, mapped_text_chars: Infinity }))+ .toEqual({ version: 1, output_kind: 'unknown', runner_text_chars: 1e9 });+ });+});