-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodex-worker.test.ts
More file actions
202 lines (173 loc) · 6.56 KB
/
Copy pathcodex-worker.test.ts
File metadata and controls
202 lines (173 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { chmod, mkdtemp, mkdir, readFile, writeFile } from 'fs/promises';
import { existsSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
import {
ensureDirs,
parseArgs,
processOne,
recoverStaleProcessing,
runCodex,
WorkerConfig,
} from '../../src/shims/codex-worker.js';
describe('Codex worker', () => {
let taskRoot: string;
let workdir: string;
beforeEach(async () => {
taskRoot = await mkdtemp(join(tmpdir(), 'dap-codex-worker-'));
workdir = await mkdtemp(join(tmpdir(), 'dap-codex-workdir-'));
});
afterEach(() => {
// Temporary directories live under /tmp and are unique per test.
});
function config(overrides: Partial<WorkerConfig> = {}): WorkerConfig {
return {
taskRoot,
workdir,
codexBin: 'codex',
pollIntervalMs: 1000,
timeoutMs: 1000,
killGraceMs: 1000,
outputLimitBytes: 200000,
allowedTypes: [],
once: true,
sandbox: 'workspace-write',
approval: 'never',
...overrides,
};
}
async function writeTask(type = 'allowed-task'): Promise<string> {
await ensureDirs(config());
const taskId = crypto.randomUUID();
await writeFile(join(taskRoot, 'incoming', `${taskId}.json`), JSON.stringify({
taskId,
description: 'do work',
type,
priority: 1,
context: { source: 'test' },
}, null, 2));
return taskId;
}
async function writeExecutable(name: string, source: string): Promise<string> {
const path = join(workdir, name);
await writeFile(path, `#!/usr/bin/env node\n${source}`);
await chmod(path, 0o755);
return path;
}
it('parses worker hardening options', () => {
const parsed = parseArgs([
'--timeout-ms', '50',
'--kill-grace-ms', '25',
'--output-limit', '10',
'--allow-type', 'a,b',
'--allow-type', 'c',
]);
expect(parsed.timeoutMs).toBe(50);
expect(parsed.killGraceMs).toBe(25);
expect(parsed.outputLimitBytes).toBe(10);
expect(parsed.allowedTypes).toEqual(['a', 'b', 'c']);
});
it('rejects tasks outside the allowlist without running Codex', async () => {
const taskId = await writeTask('blocked-task');
const processed = await processOne(config({
allowedTypes: ['allowed-task'],
}));
expect(processed).toBe(true);
const result = JSON.parse(await readFile(join(taskRoot, 'results', `${taskId}.json`), 'utf8'));
expect(result.success).toBe(false);
expect(result.error).toBe('Task type not allowed: blocked-task');
expect(existsSync(join(taskRoot, 'processing', `${taskId}.json`))).toBe(false);
});
it('limits captured Codex output', async () => {
const taskId = await writeTask();
const codexBin = await writeExecutable('fake-codex-output.js', "process.stdout.write('abcdefghijklmnopqrstuvwxyz');");
const processed = await processOne(config({
codexBin,
outputLimitBytes: 25,
}));
expect(processed).toBe(true);
const result = JSON.parse(await readFile(join(taskRoot, 'results', `${taskId}.json`), 'utf8'));
expect(result.success).toBe(true);
expect(result.data.stdout).toContain('...[truncated]');
expect(result.data.stdout.startsWith('abcdefgh')).toBe(true);
expect(Buffer.byteLength(result.data.stdout, 'utf8')).toBeLessThanOrEqual(25);
});
it('applies output limits as a byte cap for multi-byte output', async () => {
const taskId = await writeTask();
const codexBin = await writeExecutable('fake-codex-multibyte.js', "process.stdout.write('คคคคคคคคคค');");
const processed = await processOne(config({
codexBin,
outputLimitBytes: 20,
}));
expect(processed).toBe(true);
const result = JSON.parse(await readFile(join(taskRoot, 'results', `${taskId}.json`), 'utf8'));
expect(Buffer.byteLength(result.data.stdout, 'utf8')).toBeLessThanOrEqual(20);
});
it('passes approval policy through current Codex exec config', async () => {
const codexBin = await writeExecutable(
'fake-codex-args.js',
'process.stdout.write(JSON.stringify(process.argv.slice(2)));'
);
const result = await runCodex(config({ codexBin }), {
taskId: 'args-task',
description: 'inspect args',
type: 'allowed-task',
});
expect(result.exitCode).toBe(0);
const args = JSON.parse(result.stdout);
expect(args).toContain('--config');
expect(args).toContain('approval_policy="never"');
expect(args).not.toContain('--ask-for-approval');
});
it('times out long-running Codex executions', async () => {
const codexBin = await writeExecutable('fake-codex-timeout.js', 'setTimeout(() => {}, 5000);');
const result = await runCodex(config({
codexBin,
timeoutMs: 20,
}), {
taskId: 'timeout-task',
description: 'hang',
type: 'allowed-task',
});
expect(result.exitCode).toBeNull();
expect(result.error).toBe('Codex timed out after 20ms');
});
it('waits for stubborn Codex processes to be killed before resolving timeout', async () => {
const marker = join(workdir, 'sigkill-marker');
const codexBin = await writeExecutable('fake-codex-ignore-term.js', [
"const fs = require('fs');",
"process.on('SIGTERM', () => {});",
`process.on('exit', () => fs.writeFileSync(${JSON.stringify(marker)}, 'closed'));`,
'setTimeout(() => {}, 5000);',
].join('\n'));
const start = Date.now();
const result = await runCodex(config({
codexBin,
timeoutMs: 20,
killGraceMs: 20,
}), {
taskId: 'timeout-task',
description: 'hang',
type: 'allowed-task',
});
// Ensure timeout and kill grace works (it should be at least timeoutMs + some kill grace logic/duration).
// The previous 35ms assert is flaky in CI depending on runner speed.
expect(Date.now() - start).toBeGreaterThanOrEqual(20);
expect(result.exitCode).toBeNull();
expect(result.error).toBe('Codex timed out after 20ms');
expect(existsSync(marker)).toBe(false);
});
it('recovers stale processing tasks back to incoming', async () => {
await ensureDirs(config());
await mkdir(join(taskRoot, 'processing'), { recursive: true });
await writeFile(join(taskRoot, 'processing', 'stale-task.json'), JSON.stringify({
taskId: 'stale-task',
description: 'resume',
type: 'allowed-task',
}));
await recoverStaleProcessing(config());
expect(existsSync(join(taskRoot, 'incoming', 'stale-task.json'))).toBe(true);
expect(existsSync(join(taskRoot, 'processing', 'stale-task.json'))).toBe(false);
});
});