Skip to content

Commit 21e64ba

Browse files
committed
fix: align worker PWD with task cwd
1 parent 15528c0 commit 21e64ba

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All notable user-facing changes are recorded here. This project follows semantic versioning while it is in the `0.x` development series.
44

5+
## [0.1.37] - 2026-07-18
6+
7+
### Fixed
8+
9+
- Gateway-managed OpenCode runs now set `PWD` to the task working directory, preventing OpenCode server errors when PM2's saved `PWD` differs from the task's `cwd`.
10+
11+
[0.1.37]: https://github.com/vbgate/opencode-supertask/compare/v0.1.36...v0.1.37
12+
513
## [0.1.36] - 2026-07-18
614

715
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opencode-supertask",
3-
"version": "0.1.36",
3+
"version": "0.1.37",
44
"description": "AI Agent 任务调度系统 — OpenCode 插件 + CLI + Gateway 常驻进程",
55
"type": "module",
66
"main": "dist/plugin/supertask.js",

src/worker/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ export class WorkerEngine {
291291
const args = ['run', '--agent', task.agent, '--format', 'json'];
292292
if (model) args.push('-m', model);
293293
args.push(task.prompt);
294+
const cwd = task.cwd || process.cwd();
294295

295296
const child = spawn(process.execPath, [
296297
this.launcherEntry(),
@@ -299,9 +300,10 @@ export class WorkerEngine {
299300
this.opencodeBin,
300301
...args,
301302
], {
302-
cwd: task.cwd || process.cwd(),
303+
cwd,
303304
env: {
304305
...process.env,
306+
PWD: cwd,
305307
[MANAGED_RUN_ENV]: MANAGED_RUN_ENV_VALUE,
306308
},
307309
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
@@ -312,7 +314,7 @@ export class WorkerEngine {
312314
runId,
313315
launchIdentity,
314316
child,
315-
commandContext: runCommandContext(this.opencodeBin, args, task.cwd || process.cwd()),
317+
commandContext: runCommandContext(this.opencodeBin, args, cwd),
316318
output: '',
317319
sessionId: null,
318320
timeoutTimer: null,

tests/worker-engine.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ function createFakeOpencode(options: {
4141
const source = `#!/usr/bin/env bun
4242
const args = Bun.argv.slice(2);
4343
await Bun.write(${JSON.stringify(argsFile)}, JSON.stringify(args));
44-
await Bun.write(${JSON.stringify(envFile)}, JSON.stringify({ managedRun: process.env[${JSON.stringify(MANAGED_RUN_ENV)}] }));
44+
await Bun.write(${JSON.stringify(envFile)}, JSON.stringify({
45+
managedRun: process.env[${JSON.stringify(MANAGED_RUN_ENV)}],
46+
pwd: process.env.PWD,
47+
cwd: process.cwd(),
48+
}));
4549
console.log(JSON.stringify({ sessionID: "ses_worker_test", message: "任务执行完成" }));
4650
${options.ignoreSigterm ? "process.on('SIGTERM', () => {});" : ''}
4751
await Bun.sleep(${options.delayMs ?? 0});
@@ -170,21 +174,28 @@ describe('WorkerEngine', () => {
170174
model: 'test-model',
171175
prompt,
172176
maxRetries: 0,
177+
cwd: fake.dir,
173178
});
174179
const worker = new WorkerEngine(createConfig(), { opencodeBin: fake.executable });
175180
workers.push(worker);
176181

177182
worker.start();
178183
const completed = await waitForStatus(task.id, ['done']);
179184
const args = JSON.parse(readFileSync(fake.argsFile, 'utf-8')) as string[];
180-
const childEnv = JSON.parse(readFileSync(fake.envFile, 'utf-8')) as { managedRun?: string };
185+
const childEnv = JSON.parse(readFileSync(fake.envFile, 'utf-8')) as {
186+
managedRun?: string;
187+
pwd?: string;
188+
cwd: string;
189+
};
181190
const runs = await TaskRunService.listByTaskId(task.id);
182191

183192
expect(args).toEqual([
184193
'run', '--agent', 'test-agent', '--format', 'json',
185194
'-m', 'test-model', prompt,
186195
]);
187196
expect(childEnv.managedRun).toBe(MANAGED_RUN_ENV_VALUE);
197+
expect(childEnv.cwd).toBe(fake.dir);
198+
expect(childEnv.pwd).toBe(fake.dir);
188199
expect(existsSync(marker)).toBe(false);
189200
expect(completed.resultLog).toContain('任务执行完成');
190201
expect(runs).toHaveLength(1);

0 commit comments

Comments
 (0)