@online-tools/work-agent-runner 0.0.3-alpha-1 → 0.0.4-alpha-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -65,7 +65,7 @@ work-agent-runner service install # 可选:注册为后台服务,开机
65
65
  └── runs/r7x9k2m/ 一个任务一个,结束即删
66
66
  ├── work-agent/ ← claude 的工作目录:上面那个克隆的完整副本
67
67
  ├── .home/ 任务的 HOME(.work-agent 软链到 ~/.work-agent)
68
- ├── .tmp/ TMPDIR
68
+ ├── .tmp/ TMPDIR;其中 work-agent/ 是 Skill 的临时目录(被检视仓的克隆等)
69
69
  ├── claude-home/ CLAUDE_CONFIG_DIR
70
70
  └── settings.json 沙箱设置(任务改不了)
71
71
  ```
@@ -77,12 +77,12 @@ work-agent 自己的家是独立的真目录,和你手工使用 work-agent 时
77
77
  ├── ascend-repos/ 知识仓(任务只读)
78
78
  ├── essential-repos.txt 初始化下载哪些仓,可自己编辑
79
79
  ├── logs/ Skill 日志(任务跑完还在,可以查历史)
80
- ├── tmp/ docs/ Skill 的中间产物和交付件
80
+ ├── tmp/ docs/ 任务也可以写的两个目录(交付件等)
81
81
  └── work-agent.py init 建的软链,指向克隆里的脚本
82
82
  ```
83
83
 
84
84
  - **两个仓库的位置都写在 `config.json` 里**(`workAgentRepoDir`、`ascendReposDir`,首次运行自动填上默认值)。要放到别的盘就直接改这两个值(绝对路径),程序按配置文件去找和更新,不用别的开关;知识仓换盘时 `~/.work-agent/ascend-repos` 会变成指向新位置的软链。
85
- - **沙箱只能写** `~/.work-agent/{logs,tmp,docs}` 和本次任务的目录。`~/.work-agent` 其余部分和知识仓都是只读的 —— 任务删不掉知识仓,也改不了 `essential-repos.txt`。
85
+ - **任务只能写** `~/.work-agent/{logs,tmp,docs}` 和本次任务的目录(沙箱里的命令和 claude 自己的文件工具都一样)。`~/.work-agent` 其余部分和知识仓都是只读的 —— 任务删不掉知识仓,也改不了 `essential-repos.txt`。
86
86
  - **任务目录**默认在 `~/.work-agent-runner/runs/`,用 `--work-dir <目录>` 可以换到别的盘(代码检视会克隆被检视的仓,可能占几百 MB),其余结构不变。
87
87
  - Linux 上程序目录(或 `--work-dir`)的路径不能超过 54 字节:沙箱的通信 socket 建在每个任务的 `.tmp` 里,而 socket 路径最多 107 字节。超了程序会直接报错。
88
88
  - 每个任务的工作目录是克隆的**副本**(不含 `.git`):任务在一个 work-agent 仓库里干活,但改不到真仓库,也影响不到下一个任务,结束时整个目录删掉。
@@ -130,7 +130,7 @@ work-agent-runner update # 立刻更新(平时程序每小时自己更)
130
130
  每个任务:
131
131
 
132
132
  1. 在工作目录下建立独立的运行目录
133
- 2. 把本机 work-agent 仓库里的 `.claude/skills` 复制进去(`/tmp/work-agent` 改写到运行目录内;有知识仓时软链进本次任务的 `WORK_AGENT_HOME`)
133
+ 2. 把本机 work-agent 仓库复制进去(Skill 里的 `/tmp/work-agent/logs` 改写到 `~/.work-agent/logs`,其余 `/tmp/work-agent` 改写到本次任务自己的临时目录,任务结束一起删掉)
134
134
  3. 以独立进程组启动 `claude -p`,带上沙箱设置(`--settings`),超时由本程序执行
135
135
  4. 把 claude 的输出按顺序回传网站,结束后删除运行目录
136
136
 
@@ -147,13 +147,15 @@ work-agent-runner update # 立刻更新(平时程序每小时自己更)
147
147
 
148
148
  - 沙箱必须可用(`failIfUnavailable`),起不来时任务失败,**不会退化为不进沙箱运行**;模型要求「不进沙箱运行命令」一律被忽略
149
149
  - 网络只允许 `gitcode.com`、`*.gitcode.com`(claude 自己访问模型 API 不受影响)
150
- - 不能读写你的主目录(PATH 里位于主目录的目录除外,只读;初始化过知识仓时,`ascend-repos` 也是只读),不能改写 Skill 副本和设置;只能写本次任务的工作目录
150
+ - 沙箱里的命令只能写本次任务的目录和 `~/.work-agent/{logs,tmp,docs}`,不能改写 Skill 副本和设置
151
+ - claude 自己的文件工具(读、写、搜索文件)只能访问本次任务的目录和 `~/.work-agent`(其中只有 logs、tmp、docs 可写,其余和知识仓只读);你主目录里的其他文件、`/tmp` 等其他位置一律访问不到,任务在自己目录里建的软链接也绕不出去
152
+ - **已知缺口**:沙箱里的命令(Bash)**读取**还没有限制到上面这些目录:除了凭证类目录(`.ssh`、`.gnupg`、`.aws`、`.config` 等)、本程序的令牌和克隆,命令仍能读到你主目录里的其他文件,以及系统里的可读文件。请用专用的低权限账号运行本程序
151
153
  - 沙箱里的命令看不到模型 API Key;hooks 一律关闭
152
154
 
153
155
  ## 安全须知
154
156
 
155
157
  - 选择执行机器运行任务时,你的**模型 API Key 和 GitCode 授权会发送到这台机器**,只注入本次任务的 claude 进程,不写入文件或日志。交互执行的回答也只交给本次任务的 claude,日志里只记「收到网页上的回答」,不记内容。
156
- - **建议用一个专用的低权限账号运行本程序**。沙箱之外、主目录之外的可读文件仍然可能被读到;GitCode 本身在网络白名单里。
158
+ - **建议用一个专用的低权限账号运行本程序**:沙箱里的命令仍能读到这个账号能读的文件(凭证类目录除外,见「沙箱设置」);GitCode 本身在网络白名单里。
157
159
  - 程序拒绝以 root 运行(容器里确有需要时加 `--allow-root`)。
158
160
  - 接入令牌只能用来接入机器,不能登录网页。**令牌泄露时在网页上删除它**,用它接入的机器会立即断开;之后新建令牌,在需要的机器上运行 `work-agent-runner login`。网页上的「封禁」只针对单台机器,拿到令牌的人可以换个标识重新接入,所以防泄露要靠删除令牌。
159
161
  - 有新机器接入时网页会提示;不是你的机器请封禁或删除令牌。
@@ -165,7 +167,7 @@ work-agent-runner update # 立刻更新(平时程序每小时自己更)
165
167
  |---|---|
166
168
  | 更新程序 | `npm i -g @online-tools/work-agent-runner@latest`,然后重启程序或服务(`systemctl --user restart work-agent-runner`)。网页上会提示「有新版本」或「版本过低」 |
167
169
  | 更新 Skill / 知识仓 | 程序自己定时更新(Skill 每天 08:00,知识仓每小时);要立刻更新就运行 `work-agent-runner update`(服务正在用仓库时会让你稍后再试) |
168
- | 查看 Skill 日志 | `~/.work-agent-runner/.work-agent/logs/`;启动时会清掉 30 天前的日志和 `tmp/` 里的中间产物 |
170
+ | 查看 Skill 日志 | `~/.work-agent/logs/`;启动时会清掉 30 天前的日志和 `~/.work-agent/tmp/` |
169
171
  | 令牌泄露 | 在网页上删除该令牌,新建令牌后运行 `work-agent-runner login` |
170
172
  | 复制了配置 / 克隆了虚拟机 | 在复制出来的那台上运行 `work-agent-runner login --new-id` |
171
173
  | 退出登录后服务停止(Linux) | 让管理员执行一次 `loginctl enable-linger <用户名>` |
@@ -193,6 +195,8 @@ rm -rf ~/.local/share/gitcode-agent-runner # 旧版自动下载的 Node(如
193
195
  0.0.2 起 Skill 不再由网站下发,而是本程序自己克隆 work-agent 仓库,所以:
194
196
 
195
197
  - 0.0.1 的机器收不到任务(网页显示「版本过低」),必须升级到 0.0.2 以上。
198
+
199
+ 0.0.4 起 claude 自己的文件工具只能访问本次任务的目录(见「沙箱设置」),Skill 的克隆也改放在任务自己的临时目录里。更早的版本没有这层限制,网站不再给它们派任务(网页显示「版本过低」),请运行 `npm i -g @online-tools/work-agent-runner@latest` 后重启程序或服务。
196
200
  - 升级后首次运行会把配置搬到 `~/.work-agent-runner/config.json`(令牌和机器标识不变),下载 work-agent 仓库(约 2.5 MB),把自动下载的 Node 一并搬过来,并删掉旧的 `~/.config/work-agent-runner/`、`~/.cache/work-agent-runner/`(网站下发 Skill 时代的缓存)和 `/var/tmp/work-agent-runner-<uid>/`。
197
201
 
198
202
  ## 卸载
@@ -5,7 +5,15 @@
5
5
  // Only node: built-ins and files in this directory may be imported here, and
6
6
  // importing must have no side effects.
7
7
 
8
+ // Every tool a job may use.
8
9
  export const CLAUDE_TOOLS = 'Bash,Read,Glob,Grep,Edit,Write,Skill,Agent';
10
+ // Pre-allowed: Bash (confined by the operating system sandbox), Skill and
11
+ // Agent. The file tools are deliberately left out: pre-allowing them lets them
12
+ // read and write any path not on the deny list. Without it they reach the
13
+ // working directory and the paths the run's settings allow (sandbox.mjs
14
+ // runAllowances: toolRead / toolWrite); any other path asks, which a silent job
15
+ // refuses and lib/interaction.mjs denies.
16
+ export const CLAUDE_ALLOWED_TOOLS = 'Bash,Skill,Agent';
9
17
 
10
18
  const DEFAULT_PATH = '/usr/local/bin:/usr/bin:/bin';
11
19
 
@@ -16,9 +24,12 @@ const DEFAULT_PATH = '/usr/local/bin:/usr/bin:/bin';
16
24
  * deliberately not pre-allowed, so each call becomes a prompt.
17
25
  *
18
26
  * `settingsFile` (execution machines): appended as `--settings <file>`, which
19
- * carries the sandbox settings.
27
+ * carries the sandbox settings and the file tools' path rules.
28
+ *
29
+ * There is no `--add-dir`: under acceptEdits it makes a whole directory tree
30
+ * writable for the file tools; the settings name exact paths instead.
20
31
  */
21
- export function claudeArgs({ addDirs = [], interactive = false, settingsFile = '' } = {}) {
32
+ export function claudeArgs({ interactive = false, settingsFile = '' } = {}) {
22
33
  const args = [
23
34
  '-p',
24
35
  '--output-format',
@@ -27,21 +38,21 @@ export function claudeArgs({ addDirs = [], interactive = false, settingsFile = '
27
38
  ...(interactive
28
39
  ? ['--input-format', 'stream-json', '--permission-prompt-tool', 'stdio']
29
40
  : []),
30
- // The service runs as root, where bypassPermissions is refused; an explicit
31
- // allowlist plus acceptEdits runs unattended (anything else is auto-denied).
41
+ // Never bypassPermissions (refused as root anyway): acceptEdits plus the
42
+ // allowlist and the settings' path rules run unattended, and anything else
43
+ // is refused.
32
44
  '--permission-mode',
33
45
  'acceptEdits',
34
46
  '--tools',
35
47
  interactive ? `${CLAUDE_TOOLS},AskUserQuestion` : CLAUDE_TOOLS,
36
48
  '--allowedTools',
37
- CLAUDE_TOOLS,
49
+ CLAUDE_ALLOWED_TOOLS,
38
50
  // Only the per-run project (.claude/skills copy); never the host's user settings.
39
51
  '--setting-sources',
40
52
  'project',
41
53
  '--strict-mcp-config',
42
54
  '--no-session-persistence',
43
55
  ];
44
- for (const dir of addDirs) args.push('--add-dir', dir);
45
56
  if (settingsFile) args.push('--settings', settingsFile);
46
57
  return args;
47
58
  }
@@ -32,7 +32,9 @@ export const INPUT_TURN_IDLE_CLOSE_MS = 11 * 60 * 1000;
32
32
  export const QUESTION_TIMEOUT_MESSAGE =
33
33
  '用户未在 5 分钟内回答。如果这个问题是在确认写操作(提交 PR / Issue、发表评论等),视为不同意,不要执行该操作;'
34
34
  + '其他问题按推荐选项继续。不要再次询问同一问题。';
35
- const DENY_TOOL_MESSAGE = '在线执行环境不允许使用该工具或执行该操作。';
35
+ const DENY_TOOL_MESSAGE =
36
+ '在线执行环境不允许这个操作:文件工具只能读写本次任务的工作目录、$HOME、$TMPDIR 和 $WORK_AGENT_HOME 下的 logs / tmp / docs'
37
+ + '($WORK_AGENT_HOME 的其余部分只读)。';
36
38
 
37
39
  // Lines this program adds to an interactive job's stdout, where claude's own
38
40
  // control lines were (claude never prints these types). The website turns
package/lib/jobs.mjs CHANGED
@@ -412,15 +412,16 @@ export class JobManager {
412
412
  return;
413
413
  }
414
414
 
415
- // A copy of this machine's work-agent clone as the working directory, and
416
- // the knowledge pool read-only if there is one.
415
+ // A copy of this machine's work-agent clone as the working directory (the
416
+ // Skills' logs pointed at $WORK_AGENT_HOME/logs, their scratch at the run's
417
+ // own), and the knowledge pool read-only if there is one: the settings name
418
+ // every path the job may reach (sandbox.mjs).
417
419
  try {
418
- const { commit } = await this.workAgent.copyRepoInto(layout.workDir, this.paths.workAgentHome);
420
+ const { commit } = await this.workAgent.copyRepoInto(layout.workDir, {
421
+ logs: path.join(this.paths.workAgentHome, 'logs'),
422
+ scratch: layout.scratchDir,
423
+ });
419
424
  const pool = this.workAgent.mountablePool();
420
- // Claude Code limits its file tools to the working directory and whatever
421
- // --add-dir names; the pool is outside it (the sandbox still decides what
422
- // may be written).
423
- entry.addDirs = pool ? [this.paths.workAgentHome] : [];
424
425
  writeSandboxSettings(layout.settingsFile, {
425
426
  workDir: fs.realpathSync(layout.workDir),
426
427
  realHome: this.realHome,
@@ -469,7 +470,7 @@ export class JobManager {
469
470
  const interactive = job.interactive === true;
470
471
  let child;
471
472
  try {
472
- child = spawn(this.claudePath, claudeArgs({ settingsFile: layout.settingsFile, addDirs: entry.addDirs || [], interactive }), {
473
+ child = spawn(this.claudePath, claudeArgs({ settingsFile: layout.settingsFile, interactive }), {
473
474
  cwd: layout.workDir,
474
475
  detached: true,
475
476
  stdio: ['pipe', 'pipe', 'pipe'],
package/lib/sandbox.mjs CHANGED
@@ -12,14 +12,29 @@
12
12
  // `~/.work-agent/...` and $WORK_AGENT_HOME are the
13
13
  // same directory (linkWorkAgentHome)
14
14
  // <runDir>/.tmp/ TMPDIR (the sandbox's bridge sockets live here)
15
+ // <runDir>/.tmp/work-agent the Skills' `/tmp/work-agent` scratch (clones,
16
+ // intermediate files); their logs go to
17
+ // $WORK_AGENT_HOME/logs (skill-text.mjs)
15
18
  //
16
- // Jobs now run inside the program's home (~/.work-agent-runner), so the
17
- // sandbox is told exactly which subtrees to open instead of being kept out of
18
- // the home directory wholesale: the four writable directories above, the
19
- // persistent WORK_AGENT_HOME, and the knowledge pool read-only. Everything
20
- // else in the home directory stays closed, and the paths that must never be
21
- // read by a job (the real clone, config.json with the token) are named in
22
- // permissions.deny as well.
19
+ // Jobs run inside the program's home (~/.work-agent-runner). Two layers
20
+ // decide what a job reaches, one per kind of tool:
21
+ // - Bash commands run in the operating system sandbox: writable are exactly
22
+ // the four directories above and $WORK_AGENT_HOME/{logs,tmp,docs}
23
+ // (filesystem.allowWrite). Reading is NOT confined to them:
24
+ // filesystem.allowRead only re-opens paths inside a denyRead region, and
25
+ // none is set — closing the home for reads would also hide toolchains
26
+ // installed there (pyenv, nvm, conda) and has to be tried on real machines
27
+ // first. Bash reads are closed only for the deny list below.
28
+ // - claude's own file tools (Read, Glob, Grep, Edit, Write) run outside that
29
+ // sandbox. They are not pre-allowed (claude-cli.mjs), so they reach the
30
+ // working directory and exactly the paths in permissions.allow (toolRead /
31
+ // toolWrite from runAllowances); any other path is a permission prompt,
32
+ // which a silent job refuses and lib/interaction.mjs denies. claude checks
33
+ // a symlink's target as well as the link (verified with 2.1.322), so a
34
+ // link in a writable directory does not widen this.
35
+ // permissions.deny beats any allow, for both kinds (claude passes Read / Edit
36
+ // denies on to the sandbox): credentials in the home, the token, the real
37
+ // clone, writes to the pool and to this run's Skill copy.
23
38
  //
24
39
  // Paths are real paths: claude's HOME is the run's, so `~` cannot be used,
25
40
  // and on macOS /var is a symlink to /private/var.
@@ -36,9 +51,8 @@ function fromRoot(absolute) {
36
51
  }
37
52
 
38
53
  // Directories in the home that a job must never read, whatever else it may
39
- // reach: credentials, keys and shell history. The operating system sandbox is
40
- // the real boundary (nothing is readable unless it is in allowRead); this list
41
- // is the second one, at the tool layer, where deny beats any allow.
54
+ // reach: credentials, keys and shell history. A deny beats any allow, and
55
+ // claude passes these on to the sandbox, so Bash cannot read them either.
42
56
  export const SENSITIVE_HOME_PATHS = [
43
57
  '.ssh',
44
58
  '.gnupg',
@@ -58,12 +72,12 @@ export const SENSITIVE_HOME_PATHS = [
58
72
  ];
59
73
 
60
74
  /**
61
- * The settings object. `readAllow` / `writeAllow` are real paths (see
62
- * runAllowances); `deny` keeps the Read and Edit tools away from credentials,
63
- * from the program's own files (the token, the real clone) and from the Skill
64
- * copy of this run.
75
+ * The settings object. `readAllow` / `writeAllow` (the sandbox, for Bash) and
76
+ * `toolRead` / `toolWrite` (the file tools) are real paths, see
77
+ * runAllowances; `deny` keeps both away from credentials, from the program's
78
+ * own files (the token, the real clone) and from the Skill copy of this run.
65
79
  */
66
- export function sandboxSettings({ workDir, realHome, readAllow = [], writeAllow = [], deny = [] }) {
80
+ export function sandboxSettings({ workDir, realHome, readAllow = [], writeAllow = [], deny = [], toolRead = [], toolWrite = [] }) {
67
81
  return {
68
82
  sandbox: {
69
83
  enabled: true,
@@ -76,6 +90,11 @@ export function sandboxSettings({ workDir, realHome, readAllow = [], writeAllow
76
90
  credentials: { envVars: [{ name: 'ANTHROPIC_AUTH_TOKEN', mode: 'deny' }] },
77
91
  },
78
92
  permissions: {
93
+ // Where the file tools may go besides the working directory: nowhere else.
94
+ allow: [
95
+ ...toolRead.map((dir) => `Read(//${fromRoot(dir)}/**)`),
96
+ ...toolWrite.map((dir) => `Edit(//${fromRoot(dir)}/**)`),
97
+ ],
79
98
  deny: [
80
99
  ...SENSITIVE_HOME_PATHS.flatMap((name) => [
81
100
  `Read(//${fromRoot(realHome)}/${name})`,
@@ -93,11 +112,19 @@ export function sandboxSettings({ workDir, realHome, readAllow = [], writeAllow
93
112
  }
94
113
 
95
114
  /**
96
- * What one job may read and write: the four directories of its run, the
97
- * program's persistent WORK_AGENT_HOME (the Skills' logs, scratch and
98
- * deliverables), the knowledge pool read-only, and — inside the home directory
99
- * — the PATH entries and CA bundles a command needs to run at all. Also the
100
- * `deny` entries for the program's own files. All as real paths.
115
+ * What one job may read and write, all as real paths:
116
+ * writeAllow / readAllow the sandbox (Bash): the four directories of its
117
+ * run, $WORK_AGENT_HOME/{logs,tmp,docs}; read also
118
+ * all of $WORK_AGENT_HOME, the knowledge pool and —
119
+ * inside the home — the PATH entries and CA bundles
120
+ * a command needs (they only matter inside a
121
+ * denyRead region, see the top of this file)
122
+ * toolWrite / toolRead the file tools: the working directory, HOME and
123
+ * TMPDIR of the run (scratch included) and
124
+ * $WORK_AGENT_HOME/{logs,tmp,docs}; read also all of
125
+ * $WORK_AGENT_HOME and the pool. Not claude-home:
126
+ * claude's own state is none of the model's business.
127
+ * deny the program's own files and pool writes.
101
128
  */
102
129
  export function runAllowances(layout, { realHome, pathEnv = '', caPaths = [], pool = '', paths = null }) {
103
130
  const runDirs = [layout.workDir, layout.homeDir, layout.tmpDir, layout.configDir].map((dir) => realpathOr(dir));
@@ -115,6 +142,15 @@ export function runAllowances(layout, { realHome, pathEnv = '', caPaths = [], po
115
142
  ...(pool ? [realpathOr(pool)] : []),
116
143
  ...homeReadAllow({ realHome, pathEnv, caPaths }),
117
144
  ];
145
+ const toolWrite = [
146
+ ...[layout.workDir, layout.homeDir, layout.tmpDir].map((dir) => realpathOr(dir)),
147
+ ...(workAgentHome ? ['logs', 'tmp', 'docs'].map((name) => path.join(workAgentHome, name)) : []),
148
+ ];
149
+ const toolRead = [
150
+ ...toolWrite,
151
+ ...(workAgentHome ? [workAgentHome] : []),
152
+ ...(pool ? [realpathOr(pool)] : []),
153
+ ];
118
154
  const deny = [];
119
155
  if (paths) {
120
156
  // The access token, and the clone every job copies from.
@@ -125,7 +161,13 @@ export function runAllowances(layout, { realHome, pathEnv = '', caPaths = [], po
125
161
  }
126
162
  // The pool is shared with the user's own work-agent: read it, never write it.
127
163
  if (pool) deny.push(`Edit(//${fromRoot(realpathOr(pool))}/**)`);
128
- return { readAllow: [...new Set(readAllow)], writeAllow: [...new Set(writeAllow)], deny };
164
+ return {
165
+ readAllow: [...new Set(readAllow)],
166
+ writeAllow: [...new Set(writeAllow)],
167
+ toolRead: [...new Set(toolRead)],
168
+ toolWrite: [...new Set(toolWrite)],
169
+ deny,
170
+ };
129
171
  }
130
172
 
131
173
  function realpathOr(value) {
@@ -144,8 +186,9 @@ export function writeSandboxSettings(file, input) {
144
186
  * Directories / files inside HOME that sandboxed commands still need to read:
145
187
  * PATH entries (e.g. ~/.local/bin, nvm), CA bundles passed through, and
146
188
  * `extra` (the knowledge pool, which lives in ~/.work-agent by default).
147
- * Returns their real paths. Entries outside HOME are dropped — they need no
148
- * exception, since the sandbox only closes HOME.
189
+ * Returns their real paths, only those inside HOME: a denyRead on the home
190
+ * (not set yet, see the top of this file) would have to give exactly these
191
+ * back; everything outside it is readable anyway.
149
192
  */
150
193
  export function homeReadAllow({ realHome, pathEnv = '', caPaths = [], extra = [] }) {
151
194
  const allowed = new Set();
@@ -207,7 +250,9 @@ export function createRunLayout(runDir, { workAgentHome = '' } = {}) {
207
250
  workAgentHome,
208
251
  };
209
252
  layout.skillsDir = path.join(layout.workDir, '.claude', 'skills');
210
- for (const dir of [layout.configDir, layout.workDir, layout.homeDir, layout.tmpDir]) {
253
+ // The Skills' `/tmp/work-agent` (clones, intermediate files): deleted with the run.
254
+ layout.scratchDir = path.join(layout.tmpDir, 'work-agent');
255
+ for (const dir of [layout.configDir, layout.workDir, layout.homeDir, layout.tmpDir, layout.scratchDir]) {
211
256
  fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
212
257
  }
213
258
  if (workAgentHome) {
@@ -22,7 +22,15 @@ export function renameTokenVariable(text) {
22
22
  return String(text).split('GITCODE_TOKEN').join('GITCODE_CRED');
23
23
  }
24
24
 
25
- /** Points the Skills' scratch directory `/tmp/work-agent` at the run's own directory. */
26
- export function rewriteWorkAgentPath(text, replacement) {
27
- return String(text).split('/tmp/work-agent').join(replacement);
25
+ /**
26
+ * Points the Skills' `/tmp/work-agent` at this machine's directories:
27
+ * `/tmp/work-agent/logs` at `logs` (kept after the job, $WORK_AGENT_HOME/logs)
28
+ * and everything else under it — clones, intermediate files — at `scratch`
29
+ * (the job's own directory, deleted with it). A job may write nowhere else:
30
+ * mapping it to $WORK_AGENT_HOME itself put the clones where the sandbox lets
31
+ * no job write. Paths that merely start with the same letters
32
+ * (`/tmp/work-agent-runner`) are left alone.
33
+ */
34
+ export function rewriteWorkAgentPath(text, { logs, scratch }) {
35
+ return String(text).replace(/\/tmp\/work-agent(\/logs)?(?![A-Za-z0-9_.-])/g, (_, inLogs) => (inLogs ? logs : scratch));
28
36
  }
@@ -82,17 +82,20 @@ export function copyTreeDetached(source, target, limits = {}) {
82
82
  return { files, bytes };
83
83
  }
84
84
 
85
- /** Points `/tmp/work-agent` at the run's scratch dir and renames GITCODE_TOKEN (AGENTS.md 5.1). */
86
- export function rewriteSkillPaths(directory, replacement) {
85
+ /**
86
+ * Points `/tmp/work-agent` at `targets` ({ logs, scratch }: skill-text.mjs
87
+ * rewriteWorkAgentPath) and renames GITCODE_TOKEN (AGENTS.md 5.1).
88
+ */
89
+ export function rewriteSkillPaths(directory, targets) {
87
90
  for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
88
91
  const target = path.join(directory, entry.name);
89
92
  if (entry.isDirectory()) {
90
- rewriteSkillPaths(target, replacement);
93
+ rewriteSkillPaths(target, targets);
91
94
  continue;
92
95
  }
93
96
  if (!entry.isFile() || !isSkillTextFile(entry.name)) continue;
94
97
  const text = fs.readFileSync(target, 'utf8');
95
98
  if (!text.includes('/tmp/work-agent') && !text.includes('GITCODE_TOKEN')) continue;
96
- fs.writeFileSync(target, renameTokenVariable(rewriteWorkAgentPath(text, replacement)));
99
+ fs.writeFileSync(target, renameTokenVariable(rewriteWorkAgentPath(text, targets)));
97
100
  }
98
101
  }
@@ -368,16 +368,17 @@ export class WorkAgent {
368
368
  /**
369
369
  * Copies the whole repository into `targetDir` — the job's working directory,
370
370
  * so it runs inside a work-agent repository of its own — and points the
371
- * Skills' `/tmp/work-agent` at `scratchDir` (the persistent WORK_AGENT_HOME).
372
- * `.git` is left out: the copy is for reading and running, not for pushing.
373
- * Returns the commit that was copied.
371
+ * Skills' `/tmp/work-agent` at `targets`: { logs } (the persistent
372
+ * $WORK_AGENT_HOME/logs) and { scratch } (the job's own scratch directory)
373
+ * — lib/skill-text.mjs rewriteWorkAgentPath. `.git` is left out: the copy is
374
+ * for reading and running, not for pushing. Returns the commit that was copied.
374
375
  */
375
- copyRepoInto(targetDir, scratchDir) {
376
+ copyRepoInto(targetDir, targets) {
376
377
  return this.run(() => {
377
378
  const state = this.state();
378
379
  if (!state.ok) throw new Error(state.reason);
379
380
  copyTreeDetached(this.paths.repoDir, targetDir);
380
- rewriteSkillPaths(path.join(targetDir, SKILLS_SUBDIR), scratchDir);
381
+ rewriteSkillPaths(path.join(targetDir, SKILLS_SUBDIR), targets);
381
382
  return { commit: state.commit };
382
383
  });
383
384
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@online-tools/work-agent-runner",
3
- "version": "0.0.3-alpha-1",
3
+ "version": "0.0.4-alpha-1",
4
4
  "description": "GitCode 在线工具的 Agent 执行机器:在自己的机器上运行 work-agent 的 Skill(PR 描述、代码检视)",
5
5
  "license": "MIT",
6
6
  "type": "module",