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 OfficeCLI input cleanup and resident close diagnostics

wyt7fc306f36a60
diff --git a/src/main/features/office/office_engine.ts b/src/main/features/office/office_engine.ts
index ef35a45d..de2cc813 100644
--- a/src/main/features/office/office_engine.ts
+++ b/src/main/features/office/office_engine.ts
@@ -100,7 +100,7 @@ export function officeCliAvailable(): boolean {
/** Spawn the OfficeCLI binary with `args`. Resolves on process exit with the
* captured stdout/stderr (any exit code — callers inspect `code`); rejects
- * only on spawn failure or timeout. */
+ * on spawn/input failure, cancellation, timeout or output overflow. */
export function runOfficeCli(args: string[], opts: RunOfficeCliOpts): Promise<OfficeCliResult> {
const bin = officeCliBinaryPath();
if (!bin) {
@@ -155,6 +155,13 @@ export function runOfficeCli(args: string[], opts: RunOfficeCliOpts): Promise<Of
const terminate = () => {
try { killProcessTree(child, 'SIGKILL'); } catch { /* best effort */ }
};
+ const failStdin = (err: Error) => {
+ if (settled) return;
+ // Reject before termination so late pipe/close events cannot change the
+ // outcome. Clearing the deadline alone would leave the command running.
+ fail('E_OFFICE_STDIN', `OfficeCLI stdin failed: ${err.message}`);
+ terminate();
+ };
const onAbort = () => {
terminate();
fail('E_OFFICE_ABORTED', `OfficeCLI aborted: ${args[0]}`);
@@ -179,7 +186,7 @@ export function runOfficeCli(args: string[], opts: RunOfficeCliOpts): Promise<Of
child.stdout?.on('data', (c: Buffer | string) => capture(outChunks, c));
child.stderr?.on('data', (c: Buffer | string) => capture(errChunks, c));
- child.stdin?.on('error', (err: Error) => fail('E_OFFICE_STDIN', `OfficeCLI stdin failed: ${err.message}`));
+ child.stdin?.on('error', failStdin);
if (opts.signal) {
opts.signal.addEventListener('abort', onAbort, { once: true });
@@ -191,9 +198,9 @@ export function runOfficeCli(args: string[], opts: RunOfficeCliOpts): Promise<Of
if (!settled) {
try {
if (opts.stdin !== undefined) child.stdin?.write(opts.stdin);
- child.stdin?.end();
+ if (!settled) child.stdin?.end();
} catch (err) {
- fail('E_OFFICE_STDIN', `OfficeCLI stdin failed: ${(err as Error).message}`);
+ failStdin(err as Error);
}
}
@@ -218,7 +225,13 @@ export function runOfficeCli(args: string[], opts: RunOfficeCliOpts): Promise<Of
* throws — call in a `finally` after a tool finishes with a document. */
export async function closeOfficeFile(file: string, cwd: string): Promise<void> {
try {
- await runOfficeCli(['close', file], { cwd, timeoutMs: 10_000 });
+ const result = await runOfficeCli(['close', file], { cwd, timeoutMs: 10_000 });
+ if (result.code !== 0) {
+ log.warn('OfficeCLI resident close failed', {
+ file: logPathRef(file), exit_code: result.code,
+ stdout_chars: result.stdout.length, stderr_chars: result.stderr.length,
+ });
+ }
} catch (err) {
log.warn('OfficeCLI resident close failed', { file: logPathRef(file), error: logErrorSummary(err) });
}
diff --git a/test/main/features/office/office_engine.test.ts b/test/main/features/office/office_engine.test.ts
index a46a6239..a7015eba 100644
--- a/test/main/features/office/office_engine.test.ts
+++ b/test/main/features/office/office_engine.test.ts
@@ -206,6 +206,75 @@ describe('OfficeCLI engine', () => {
expect(mocks.killProcessTree).toHaveBeenCalledWith(child, 'SIGKILL');
});
+ it.each(['write', 'end', 'write-event', 'async-event'] as const)('terminates an unfinished command once when stdin fails through %s', async (failurePoint) => {
+ vi.useFakeTimers();
+ const child = new FakeChild();
+ mocks.spawn.mockReturnValue(child);
+ const controller = new AbortController();
+ const failure = new Error('input pipe failed');
+ if (failurePoint === 'write' || failurePoint === 'end') {
+ child.stdin[failurePoint].mockImplementation(() => { throw failure; });
+ } else if (failurePoint === 'write-event') {
+ child.stdin.write.mockImplementation(() => child.stdin.emit('error', failure));
+ }
+ const result = runOfficeCli(['batch', 'book.xlsx'], {
+ cwd: tmpDir, stdin: '{"ops":[]}', signal: controller.signal, timeoutMs: 25,
+ });
+ const rejected = expect(result).rejects.toMatchObject({
+ code: 'E_OFFICE_STDIN', message: 'OfficeCLI stdin failed: input pipe failed',
+ });
+ if (failurePoint === 'async-event') child.stdin.emit('error', failure);
+ await rejected;
+ expect(mocks.killProcessTree).toHaveBeenCalledExactlyOnceWith(child, 'SIGKILL');
+ if (failurePoint === 'write' || failurePoint === 'write-event') {
+ expect(child.stdin.end).not.toHaveBeenCalled();
+ }
+ // Late transport events must neither replay the command nor kill a recycled PID.
+ child.stdin.emit('error', failure);
+ child.emit('close', 0);
+ controller.abort();
+ await vi.advanceTimersByTimeAsync(30);
+ expect(mocks.killProcessTree).toHaveBeenCalledTimes(1);
+ expect(mocks.spawn).toHaveBeenCalledTimes(1);
+ expect(vi.getTimerCount()).toBe(0);
+ await expect(result).rejects.toMatchObject({ code: 'E_OFFICE_STDIN' });
+ });
+
+ it('does not terminate a completed command when a late stdin error arrives', async () => {
+ const child = new FakeChild();
+ mocks.spawn.mockReturnValue(child);
+ const result = runOfficeCli(['view', 'book.xlsx'], { cwd: tmpDir });
+ child.emit('close', 0);
+ child.stdin.emit('error', new Error('late pipe failure'));
+ await expect(result).resolves.toEqual({ code: 0, stdout: '', stderr: '' });
+ expect(mocks.killProcessTree).not.toHaveBeenCalled();
+ });
+
+ it.each([0, 1, null])('keeps resident close best-effort and reports only unsuccessful exit status %s', async (exitCode) => {
+ const file = path.join(tmpDir, 'confidential-acquisition.docx');
+ const privateOutput = 'private document contents';
+ const child = new FakeChild();
+ mocks.spawn.mockReturnValue(child);
+ const result = closeOfficeFile(file, tmpDir);
+ child.stdout.emit('data', privateOutput);
+ child.stderr.emit('data', privateOutput);
+ child.emit('close', exitCode);
+ await expect(result).resolves.toBeUndefined();
+ expect(mocks.spawn).toHaveBeenCalledTimes(1);
+ if (exitCode === 0) {
+ expect(mocks.warn).not.toHaveBeenCalled();
+ } else {
+ expect(mocks.warn).toHaveBeenCalledExactlyOnceWith('OfficeCLI resident close failed', {
+ file: expect.any(Object), exit_code: exitCode ?? -1,
+ stdout_chars: privateOutput.length, stderr_chars: privateOutput.length,
+ });
+ const diagnostic = JSON.stringify(mocks.warn.mock.calls);
+ expect(diagnostic).not.toContain(privateOutput);
+ expect(diagnostic).not.toContain(tmpDir);
+ expect(diagnostic).not.toContain(path.basename(file));
+ }
+ });
+
it.each(['close', 'sweep'] as const)('keeps %s failure diagnostics without leaking private file or error contents', async (operation) => {
const file = path.join(tmpDir, 'confidential-acquisition.docx');
const privateMessage = 'private document text and credentials from a failed native process';