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.

Allow operation deadlines to observe caller cancellation

wyt8435736aa510
diff --git a/src/main/util/operation-timeout.ts b/src/main/util/operation-timeout.ts
index af8ec673..d064f5df 100644
--- a/src/main/util/operation-timeout.ts
+++ b/src/main/util/operation-timeout.ts
@@ -40,6 +40,7 @@ export async function withOperationTimeout<T>(
timeoutMs: number;
code: string;
stage: string;
+ signal?: AbortSignal;
onLateSettlement?: (operation: Promise<T>) => void | Promise<void>;
},
): Promise<T> {
@@ -48,6 +49,11 @@ export async function withOperationTimeout<T>(
}
let timer: NodeJS.Timeout | null = null;
let timedOut = false;
+ const abort = (): void => abortReject(opts.signal?.reason || new Error('operation cancelled'));
+ let abortReject!: (reason: unknown) => void;
+ const cancelled = new Promise<never>((_resolve, reject) => { abortReject = reject; });
+ opts.signal?.addEventListener('abort', abort, { once: true });
+ if (opts.signal?.aborted) abort();
const timeout = new Promise<never>((_resolve, reject) => {
timer = setTimeout(() => {
timedOut = true;
@@ -56,9 +62,10 @@ export async function withOperationTimeout<T>(
timer.unref?.();
});
try {
- return await Promise.race([operation, timeout]);
+ return await Promise.race([operation, timeout, cancelled]);
} finally {
if (timer) clearTimeout(timer);
+ opts.signal?.removeEventListener('abort', abort);
if (timedOut && opts.onLateSettlement) {
// Recovery bookkeeping must never replace or delay the stable timeout
// seen by the queue. Observe both synchronous and asynchronous callback
diff --git a/test/main/util/operation-timeout.test.ts b/test/main/util/operation-timeout.test.ts
index 7984fb38..9c85a3f6 100644
--- a/test/main/util/operation-timeout.test.ts
+++ b/test/main/util/operation-timeout.test.ts
@@ -49,6 +49,20 @@ describe('operation timeout settlement', () => {
vi.useFakeTimers();
});
+ it('lets an owner cancel a silent operation without a timeout or late recovery', async () => {
+ const controller = new AbortController();
+ const recovery = vi.fn();
+ const request = withOperationTimeout(new Promise(() => {}), {
+ timeoutMs: 60_000, code: 'E_TIMEOUT', stage: 'extract',
+ signal: controller.signal, onLateSettlement: recovery,
+ });
+ const rejected = expect(request).rejects.toThrow('owner stopped');
+ controller.abort(new Error('owner stopped'));
+ await rejected;
+ expect(vi.getTimerCount()).toBe(0);
+ expect(recovery).not.toHaveBeenCalled();
+ });
+
it('preserves a successful or failed operation that settles before its deadline', async () => {
await expect(withOperationTimeout(Promise.resolve('ready'), {
timeoutMs: 1_000,