Skip to content

armgit / Orkas

Public Imported from GitHub
0

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.

fix(video): clean up frame analysis and redact diagnostics

CloudTianTian1346965a4a4f
diff --git a/src/main/features/video_vision.ts b/src/main/features/video_vision.ts
index 1deacf5a..dc45dcb6 100644
--- a/src/main/features/video_vision.ts
+++ b/src/main/features/video_vision.ts
@@ -1,6 +1,6 @@
/**
* video_vision — provider-agnostic visual reading for the video pipeline
- * (ingest "据观察不据假设" frame reads + QA visual spot-checks). The seam is a
+ * (observed frame reads + QA visual spot-checks). The seam is a
* single `analyzeFrames(frames, question) -> text` so callers never bake in a
* specific model/provider.
*
@@ -32,6 +32,7 @@ import * as fs from 'node:fs/promises';
import { chatWithModel } from '../model/client';
import { toCompressedGrayJpeg } from '../util/image-transform';
import { createLogger } from '../logger';
+import { logErrorSummary, logPathRef } from '../util/log-redact';
const log = createLogger('video-vision');
@@ -141,7 +142,7 @@ async function loadFrame(p: string): Promise<LoadedImage | null> {
const compressed = await toCompressedGrayJpeg(raw, { maxDim: FRAME_MAX_DIM, quality: FRAME_JPEG_QUALITY, grayscale: false });
return { data: compressed.buf.toString('base64'), mediaType: 'image/jpeg' };
} catch (err) {
- log.warn(`load frame ${p}: ${(err as Error).message}`);
+ log.warn('frame load failed', { frame: logPathRef(p), error: logErrorSummary(err) });
return null;
}
}
@@ -173,9 +174,10 @@ export async function analyzeFrames(params: AnalyzeFramesParams): Promise<Analyz
const chat = params.chat ?? chatWithModel;
const controller = new AbortController();
+ const abortFromCaller = () => controller.abort();
if (params.signal) {
if (params.signal.aborted) controller.abort();
- else params.signal.addEventListener('abort', () => controller.abort(), { once: true });
+ else params.signal.addEventListener('abort', abortFromCaller, { once: true });
}
let timer: NodeJS.Timeout | null = null;
const timeout = new Promise<{ ok: boolean; text: string; error: string; aborted: boolean }>((resolve) => {
@@ -185,25 +187,29 @@ export async function analyzeFrames(params: AnalyzeFramesParams): Promise<Analyz
}, VISION_TIMEOUT_MS);
});
- const call = Promise.resolve(
- chat({
- userId: params.userId,
- message: buildVisionMessage(params.question, loaded.length),
- images: loaded,
- skillList: [],
- idleTimeout: Math.ceil(VISION_TIMEOUT_MS / 1000),
- abortSignal: controller.signal,
- ...(params.sessionId ? { sessionId: params.sessionId } : {}),
- ...(params.agentId ? { agentId: params.agentId } : {}),
- }),
- ).catch((err) => ({ ok: false, text: '', error: (err as Error).message || String(err), aborted: false }));
-
- const res = await Promise.race([call, timeout]);
- if (timer) clearTimeout(timer);
-
- const out = interpretVisionResponse(res, loaded.length, route);
- if (out.ok === false && out.reason !== 'aborted') {
- log.warn(`analyzeFrames degraded (${out.reason}): ${out.message}`);
+ try {
+ const call = Promise.resolve(
+ chat({
+ userId: params.userId,
+ message: buildVisionMessage(params.question, loaded.length),
+ images: loaded,
+ skillList: [],
+ idleTimeout: Math.ceil(VISION_TIMEOUT_MS / 1000),
+ abortSignal: controller.signal,
+ ...(params.sessionId ? { sessionId: params.sessionId } : {}),
+ ...(params.agentId ? { agentId: params.agentId } : {}),
+ }),
+ ).catch((err) => ({ ok: false, text: '', error: (err as Error).message || String(err), aborted: false }));
+
+ const res = await Promise.race([call, timeout]);
+
+ const out = interpretVisionResponse(res, loaded.length, route);
+ if (out.ok === false && out.reason !== 'aborted') {
+ log.warn('frame analysis degraded', { reason: out.reason, error: logErrorSummary(out.message) });
+ }
+ return out;
+ } finally {
+ if (timer) clearTimeout(timer);
+ params.signal?.removeEventListener('abort', abortFromCaller);
}
- return out;
}
diff --git a/test/main/features/video_vision.test.ts b/test/main/features/video_vision.test.ts
index 0684c057..5a216fba 100644
--- a/test/main/features/video_vision.test.ts
+++ b/test/main/features/video_vision.test.ts
@@ -1,3 +1,6 @@
+import { getEventListeners } from 'node:events';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
import { describe, expect, it, vi } from 'vitest';
import {
@@ -9,6 +12,15 @@ import {
type LoadedImage,
} from '../../../src/main/features/video_vision';
+const visionWarnings = vi.hoisted(() => vi.fn());
+vi.mock('../../../src/main/logger', async importOriginal => {
+ const actual = await importOriginal<typeof import('../../../src/main/logger')>();
+ return { ...actual, createLogger: (name: string) => {
+ const logger = actual.createLogger(name);
+ return name === 'video-vision' ? { ...logger, warn: visionWarnings } : logger;
+ } };
+});
+
const okRes = (text: string) => ({ ok: true, text, error: '', aborted: false });
// Injected frame loader — returns one dummy decoded image per path, so the
@@ -69,6 +81,74 @@ describe('resolveVisionRoute', () => {
});
describe('analyzeFrames — orchestration', () => {
+ it.each([false, true])('preserves caller cancellation (already aborted=%s)', async alreadyAborted => {
+ const caller = new AbortController();
+ if (alreadyAborted) caller.abort();
+ const chat = vi.fn((options: { abortSignal?: AbortSignal }) => new Promise(resolve => {
+ const finish = () => resolve({ ok: false, text: '', error: 'cancelled', aborted: true });
+ if (options.abortSignal?.aborted) finish();
+ else options.abortSignal?.addEventListener('abort', finish, { once: true });
+ }));
+ const pending = analyzeFrames({
+ userId: 'u1', framePaths: ['frame.png'], question: 'describe', signal: caller.signal,
+ loadFrames: fakeLoader, chat: chat as never,
+ });
+ await vi.waitFor(() => expect(chat).toHaveBeenCalledOnce());
+ caller.abort();
+ expect(await pending).toMatchObject({ ok: false, reason: 'aborted' });
+ expect(chat.mock.calls[0][0].abortSignal?.aborted).toBe(true);
+ expect(getEventListeners(caller.signal, 'abort')).toEqual([]);
+ });
+
+ it.each(['success', 'failure', 'timeout'] as const)('releases its caller abort listener after %s without removing other listeners', async outcome => {
+ const caller = new AbortController();
+ const otherListener = vi.fn();
+ caller.signal.addEventListener('abort', otherListener);
+ const chat = vi.fn(() => outcome === 'timeout'
+ ? new Promise<ReturnType<typeof okRes>>(() => {})
+ : Promise.resolve(outcome === 'success' ? okRes('Visible frame')
+ : { ok: false, text: '', error: 'controlled failure', aborted: false }));
+ if (outcome === 'timeout') vi.useFakeTimers();
+ try {
+ const pending = analyzeFrames({
+ userId: 'u1', framePaths: ['frame.png'], question: 'describe', signal: caller.signal,
+ loadFrames: fakeLoader, chat: chat as never,
+ });
+ if (outcome === 'timeout') await vi.advanceTimersByTimeAsync(180_000);
+ const result = await pending;
+ expect(result).toMatchObject(outcome === 'success' ? { ok: true }
+ : { ok: false, reason: outcome === 'timeout' ? 'aborted' : 'error' });
+ expect(chat).toHaveBeenCalledOnce();
+ expect(getEventListeners(caller.signal, 'abort')).toEqual([otherListener]);
+ caller.abort();
+ expect(otherListener).toHaveBeenCalledOnce();
+ if (outcome === 'timeout') expect(vi.getTimerCount()).toBe(0);
+ } finally {
+ caller.signal.removeEventListener('abort', otherListener);
+ if (outcome === 'timeout') vi.useRealTimers();
+ }
+ });
+
+ it.each(['frame', 'provider'] as const)('keeps private %s failure details out of the logger boundary', async failure => {
+ visionWarnings.mockClear();
+ const privateDetail = failure === 'frame'
+ ? join(tmpdir(), 'private-client-document', 'unpublished-frame.png')
+ : 'Private provider diagnostic with user request content';
+ const chat = vi.fn(async () => { throw new Error(privateDetail); });
+ const result = await analyzeFrames({
+ userId: 'u1', framePaths: [privateDetail], question: 'describe', chat: chat as never,
+ ...(failure === 'provider' ? { loadFrames: fakeLoader } : {}),
+ });
+ expect(result).toMatchObject({ ok: false, reason: failure === 'frame' ? 'no-frames' : 'error' });
+ expect(chat).toHaveBeenCalledTimes(failure === 'frame' ? 0 : 1);
+ expect(visionWarnings).toHaveBeenCalledOnce();
+ const diagnostic = visionWarnings.mock.calls[0];
+ expect(JSON.stringify(diagnostic)).not.toContain(privateDetail);
+ expect(diagnostic[0]).toBe(failure === 'frame' ? 'frame load failed' : 'frame analysis degraded');
+ expect(diagnostic[1].error).toMatchObject({ message_hash: expect.stringMatching(/^[a-f0-9]{12}$/) });
+ if (failure === 'frame') expect(diagnostic[1].frame).toMatchObject({ path_hash: expect.stringMatching(/^[a-f0-9]{12}$/) });
+ });
+
it('degrades to no-frames when no paths are supplied (no model call)', async () => {
const chat = vi.fn();
const r = await analyzeFrames({ userId: 'u1', framePaths: [], question: 'what?', chat: chat as never });