@xiangyixie/dsh-superagent 0.12.1 → 0.14.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/lib/workbench.js CHANGED
@@ -49,7 +49,7 @@ const ROUTE_HINT = 'GET / serves the panel; the JSON API is under /api.'
49
49
  const SNAPSHOT_SKIP = new Set(['.git', '.hg', '.svn', 'node_modules'])
50
50
 
51
51
  /** Error shape used by the routing layer to choose a status code. */
52
- class WorkbenchError extends Error {
52
+ export class WorkbenchError extends Error {
53
53
  constructor(status, message, { id = 'bad-request', hint } = {}) {
54
54
  super(message)
55
55
  this.status = status
@@ -62,7 +62,7 @@ function sha256(buffer) {
62
62
  return createHash('sha256').update(buffer).digest('hex')
63
63
  }
64
64
 
65
- function sameToken(given, expected) {
65
+ export function sameToken(given, expected) {
66
66
  const a = Buffer.from(given)
67
67
  const b = Buffer.from(expected)
68
68
  return a.length === b.length && timingSafeEqual(a, b)
@@ -75,7 +75,7 @@ export function looksBinary(buffer) {
75
75
  }
76
76
 
77
77
  /** Send JSON with the headers every response in this module shares. */
78
- function send(res, status, payload, extra = {}) {
78
+ export function send(res, status, payload, extra = {}) {
79
79
  if (res.headersSent) return
80
80
  const body = `${JSON.stringify(payload)}\n`
81
81
  res.writeHead(status, {
@@ -88,7 +88,7 @@ function send(res, status, payload, extra = {}) {
88
88
  res.end(body)
89
89
  }
90
90
 
91
- function sendText(res, status, text, type = 'text/plain; charset=utf-8', extra = {}) {
91
+ export function sendText(res, status, text, type = 'text/plain; charset=utf-8', extra = {}) {
92
92
  if (res.headersSent) return
93
93
  res.writeHead(status, {
94
94
  'content-type': type,
@@ -100,7 +100,7 @@ function sendText(res, status, text, type = 'text/plain; charset=utf-8', extra =
100
100
  res.end(text)
101
101
  }
102
102
 
103
- function errorPayload(error) {
103
+ export function errorPayload(error) {
104
104
  if (error instanceof WorkbenchError) {
105
105
  return {
106
106
  ok: false,
@@ -114,7 +114,7 @@ function errorPayload(error) {
114
114
  }
115
115
  }
116
116
 
117
- function statusFor(error) {
117
+ export function statusFor(error) {
118
118
  return error instanceof WorkbenchError ? error.status : 500
119
119
  }
120
120
 
@@ -152,7 +152,7 @@ function readBody(req, limit = BODY_LIMIT) {
152
152
  })
153
153
  }
154
154
 
155
- async function readJson(req) {
155
+ export async function readJson(req) {
156
156
  const text = (await readBody(req)).trim()
157
157
  if (!text) return {}
158
158
  let parsed
package/lib/workspace.js CHANGED
@@ -9,14 +9,26 @@
9
9
  * feature exists to fix). A worktree is also cheap: it shares the object
10
10
  * database and the ignore rules, so a worker sees the same repository.
11
11
  *
12
- * The one honest limitation: dsh 0.2.2-rc.1 `startActivation` has no `cwd`
13
- * field, so a child process cannot be *given* a directory by the host. Isolation
14
- * is therefore enforced in two parts — a prompt contract that names the absolute
15
- * worktree path and forbids touching anything else, plus a real branch per
16
- * worker so whatever the worker wrote is captured and merged back with git
17
- * rather than trusted. A worker that ignores the contract still cannot damage a
18
- * sibling worker's branch; it would just edit the main checkout, which
19
- * `git status` on the main directory then shows before any merge happens.
12
+ * Isolation has two parts. The host `startActivation` request takes a `cwd`, so
13
+ * each worker starts inside its own worktree and relative writes land there.
14
+ * The prompt names the same absolute path as a second line of defense, and every
15
+ * worker gets a real branch so whatever it wrote is captured and merged back
16
+ * with git rather than trusted. A worker that ignores both still cannot damage a
17
+ * sibling worker's branch; it would edit the main checkout, which the
18
+ * deliverable check reports as `misplaced` instead of silently accepting it.
19
+ *
20
+ * Where the checkouts live is not cosmetic. The host's `workspace-write`
21
+ * sandbox takes its writable boundary from the session's immutable `cwd` — the
22
+ * directory the host was started in — which a delegated child inherits from its
23
+ * parent and a `working-directory` change does not move. A checkout under the
24
+ * state directory, or inside a repository the host was not started in, is
25
+ * outside that boundary: every worker write is denied no matter what `cwd` the
26
+ * activation requests. The pool is therefore anchored to the boundary — the
27
+ * repository when it sits inside the boundary, otherwise the boundary itself —
28
+ * at `<anchor>/.superagent/worktrees/<runId>/w<n>`, mirroring the host's own
29
+ * worktree service (`.agents/worktrees`). The pool carries its own
30
+ * `.gitignore`, so those checkouts never appear as untracked files and never
31
+ * block the merge.
20
32
  *
21
33
  * Nothing here imports the host: git is spawned directly, so the module is
22
34
  * testable against real temporary repositories.
@@ -102,6 +114,13 @@ function samePath(value) {
102
114
  return process.platform === 'win32' ? resolved.replace(/\\/g, '/').toLowerCase() : resolved
103
115
  }
104
116
 
117
+ /** True when `child` is `parent` or lives under it, compared the OS way. */
118
+ function within(parent, child) {
119
+ const base = samePath(parent)
120
+ const target = samePath(child)
121
+ return target === base || target.startsWith(base.endsWith('/') ? base : `${base}/`)
122
+ }
123
+
105
124
  /** A quoted path, so a directory with spaces can be pasted into a shell. */
106
125
  function quote(value) {
107
126
  return `"${String(value).replace(/"/g, '')}"`
@@ -117,16 +136,21 @@ function looksLikeLockRace(result) {
117
136
  * Build a workspace manager bound to one repository.
118
137
  *
119
138
  * @param {object} options - manager settings.
120
- * @param {string} options.root - directory that holds every worker worktree.
139
+ * @param {string} [options.root] - worktree pool, relative to the anchor
140
+ * (default `.superagent/worktrees`) so the host sandbox covers it; an absolute
141
+ * path is used as-is and is only correct inside the sandbox boundary.
121
142
  * @param {string} options.baseDir - the main checkout workers are isolated from.
143
+ * @param {string} [options.boundary] - the host sandbox's writable root, i.e.
144
+ * the session cwd. Defaults to `baseDir`.
122
145
  * @param {string | null} [options.branchPrefix] - branch namespace for workers.
123
146
  * @param {(message: string) => void} [options.log] - diagnostics sink.
124
147
  * @param {(ms: number) => Promise<void>} [options.sleep] - backoff, injectable.
125
148
  * @param {Function} [options.exec] - command runner, injectable for tests.
126
149
  */
127
150
  export function createWorkspaceManager({
128
- root,
151
+ root = path.join('.superagent', 'worktrees'),
129
152
  baseDir,
153
+ boundary = baseDir,
130
154
  branchPrefix = 'superagent',
131
155
  log = () => {},
132
156
  sleep = ms => new Promise(resolve => setTimeout(resolve, ms)),
@@ -138,6 +162,7 @@ export function createWorkspaceManager({
138
162
  // flake that retries alone would only make rarer.
139
163
  let prepareChain = Promise.resolve()
140
164
  let probed = null
165
+ let resolvedRoot = null
141
166
 
142
167
  const run = (cwd, args) => git(cwd, args, { exec })
143
168
 
@@ -148,11 +173,11 @@ export function createWorkspaceManager({
148
173
  * it would keep reporting yesterday's missing repository after the user fixed
149
174
  * it in the same host process.
150
175
  *
151
- * @returns {Promise<{ ok: boolean, base: string, baseDir: string, reason: string | null }>}
176
+ * @returns {Promise<{ ok: boolean, base: string, baseDir: string, root: string | null, reason: string | null }>}
152
177
  */
153
178
  async function probe() {
154
179
  if (probed) return probed
155
- const unavailable = reason => ({ ok: false, base: null, baseDir, reason })
180
+ const unavailable = reason => ({ ok: false, base: null, baseDir, root: null, reason })
156
181
  if (typeof baseDir !== 'string' || !baseDir.trim()) {
157
182
  return unavailable('no workspace directory is configured (set workspaceRoot to the repository the workers should edit)')
158
183
  }
@@ -175,31 +200,51 @@ export function createWorkspaceManager({
175
200
  if (!head.ok) {
176
201
  return unavailable(`the repository at ${base} has no commit yet, so there is no revision to branch a worker from`)
177
202
  }
203
+ // The pool must live inside the host sandbox boundary, or every worker
204
+ // write is denied. Prefer the repository itself when the boundary contains
205
+ // it; otherwise the boundary directory is the only writable choice, and a
206
+ // git worktree is legal outside its repository.
207
+ const anchor = within(boundary, base) ? base : path.resolve(boundary)
208
+ const pool = path.isAbsolute(root) ? path.resolve(root) : path.join(anchor, root)
178
209
  try {
179
- await fsp.mkdir(root, { recursive: true })
180
- // The state directory may itself live inside a repository. An ignore file
181
- // here keeps a worker's checkout out of the user's next `git add -A`,
182
- // exactly like the reports directory does.
183
- const ignore = path.join(root, '.gitignore')
210
+ await fsp.mkdir(pool, { recursive: true })
211
+ // The ignore file here keeps a worker's checkout out of the user's next
212
+ // `git add -A` and out of the `git status` gate that holds the merge.
213
+ const ignore = path.join(pool, '.gitignore')
184
214
  await fsp.writeFile(ignore, '*\n', { flag: 'wx' }).catch(error => {
185
215
  if (error?.code !== 'EEXIST') throw error
186
216
  })
187
217
  } catch (error) {
188
- return unavailable(`cannot create the worktree root ${root}: ${error?.message ?? error}`)
218
+ return unavailable(`cannot create the worktree root ${pool}: ${error?.message ?? error}`)
189
219
  }
190
- probed = { ok: true, base, baseDir: base, reason: null }
220
+ resolvedRoot = pool
221
+ probed = { ok: true, base, baseDir: base, root: pool, reason: null }
191
222
  return probed
192
223
  }
193
224
 
194
- /** Paths git already has checked out, so a resume reuses its worktree. */
225
+ /**
226
+ * Checkouts git already has, so a resume reuses its worktree. The branch is
227
+ * read too: a run recorded before the pool moved inside the repository still
228
+ * owns its branch at the old path, and a resume must land in that checkout
229
+ * instead of failing on a branch git already has checked out.
230
+ *
231
+ * @returns {Promise<Array<{ path: string, key: string, branch: string | null }>>}
232
+ */
195
233
  async function registeredWorktrees() {
196
234
  const listed = await run(baseDir, ['worktree', 'list', '--porcelain'])
197
- if (!listed.ok) return new Set()
198
- const paths = new Set()
235
+ if (!listed.ok) return []
236
+ const trees = []
237
+ let current = null
199
238
  for (const line of listed.stdout.split('\n')) {
200
- if (line.startsWith('worktree ')) paths.add(samePath(line.slice('worktree '.length).trim()))
239
+ if (line.startsWith('worktree ')) {
240
+ const dir = line.slice('worktree '.length).trim()
241
+ current = { path: dir, key: samePath(dir), branch: null }
242
+ trees.push(current)
243
+ } else if (current && line.startsWith('branch ')) {
244
+ current.branch = line.slice('branch '.length).trim().replace(/^refs\/heads\//, '')
245
+ }
201
246
  }
202
- return paths
247
+ return trees
203
248
  }
204
249
 
205
250
  /**
@@ -219,13 +264,18 @@ export function createWorkspaceManager({
219
264
  if (!verdict.ok) {
220
265
  return { ok: false, dir: null, branch: null, base: verdict.baseDir ?? null, reused: false, reason: verdict.reason }
221
266
  }
222
- const dir = path.join(root, runId, `w${index}`)
267
+ const dir = path.join(verdict.root, runId, `w${index}`)
223
268
  const branch = `${branchPrefix}/${runId}-w${index}`
224
269
  const existing = await registeredWorktrees()
225
- if (existing.has(samePath(dir))) {
270
+ if (existing.some(tree => tree.key === samePath(dir))) {
226
271
  // A resumed run must not throw away what an earlier attempt wrote.
227
272
  return { ok: true, dir, branch, base: verdict.base, reused: true, reason: null }
228
273
  }
274
+ const elsewhere = existing.find(tree => tree.branch === branch)
275
+ if (elsewhere) {
276
+ // Same branch, different pool: keep the checkout that owns it.
277
+ return { ok: true, dir: elsewhere.path, branch, base: verdict.base, reused: true, reason: null }
278
+ }
229
279
  let last = null
230
280
  for (let attempt = 1; attempt <= LOCK_ATTEMPTS; attempt++) {
231
281
  const added = await run(verdict.base, ['worktree', 'add', '-b', branch, dir, 'HEAD'])
@@ -383,7 +433,18 @@ export function createWorkspaceManager({
383
433
  return outcomes
384
434
  }
385
435
 
386
- return { probe, prepare, harvest, mergeAll, release, root, baseDir }
436
+ return {
437
+ probe,
438
+ prepare,
439
+ harvest,
440
+ mergeAll,
441
+ release,
442
+ /** The resolved pool; available once `probe()` has found the repository. */
443
+ get root() {
444
+ return resolvedRoot
445
+ },
446
+ baseDir,
447
+ }
387
448
  }
388
449
 
389
450
  /**
package/locale/en.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "meta": {
3
+ "title": "Superagent",
4
+ "description": "Long-horizon autonomous runs: one goal becomes planned parallel subtasks and one merged report.",
5
+ "detail": "docs/DETAIL.md"
6
+ }
7
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "meta": {
3
+ "title": "超级智能体",
4
+ "description": "长周期自主执行:一句话目标拆成可并行的子任务,最后合并成一份报告。",
5
+ "detail": "docs/DETAIL.zh.md"
6
+ }
7
+ }
package/package.json CHANGED
@@ -1,92 +1,110 @@
1
- {
2
- "name": "@xiangyixie/dsh-superagent",
3
- "version": "0.12.1",
4
- "description": "Long-horizon autonomous runs for DeepSeek Harness: one goal, model-planned parallel subtasks, one merged report, durable across restarts.",
5
- "author": "Admire (LittleChenLiya)",
6
- "type": "module",
7
- "repository": {
8
- "type": "git",
9
- "url": "git+https://github.com/LittleChenLiya/dsh-superagent.git"
10
- },
11
- "homepage": "https://github.com/LittleChenLiya/dsh-superagent#readme",
12
- "bugs": {
13
- "url": "https://github.com/LittleChenLiya/dsh-superagent/issues"
14
- },
1
+ {
2
+ "name": "@xiangyixie/dsh-superagent",
3
+ "version": "0.14.1",
4
+ "description": "长周期自主执行插件:一个目标拆成可并行的子任务,交付一份合并报告,重启后仍可继续。",
5
+ "author": "Admire (LittleChenLiya)",
6
+ "type": "module",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/LittleChenLiya/dsh-superagent.git"
10
+ },
11
+ "homepage": "https://github.com/LittleChenLiya/dsh-superagent#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/LittleChenLiya/dsh-superagent/issues"
14
+ },
15
15
  "main": "lib/index.js",
16
- "bin": {
17
- "dsh-superagent-watch": "bin/watch.js",
18
- "dsh-superagent-report": "bin/report.js",
19
- "dsh-superagent-doctor": "bin/doctor.js",
20
- "dsh-superagent-archive": "bin/archive.js",
21
- "dsh-superagent-sync": "bin/sync.js",
22
- "dsh-superagent-workbench": "bin/workbench.js"
23
- },
24
- "exports": {
25
- ".": "./lib/index.js",
26
- "./cordis.patch.yml": "./cordis.patch.yml",
27
- "./package.json": "./package.json"
28
- },
29
- "publishConfig": {
30
- "registry": "https://registry.npmjs.org",
31
- "access": "public"
32
- },
33
- "files": [
34
- "lib/index.js",
35
- "lib/orchestrator.js",
36
- "lib/store.js",
37
- "lib/errors.js",
38
- "lib/usage.js",
39
- "lib/cost.js",
40
- "lib/ledger.js",
41
- "lib/control.js",
42
- "lib/memory.js",
43
- "lib/guard.js",
44
- "lib/notify.js",
45
- "lib/lock.js",
46
- "lib/templates.js",
47
- "lib/policy.js",
48
- "lib/audit.js",
49
- "lib/inject.js",
50
- "lib/sync.js",
51
- "lib/workbench.js",
52
- "lib/watch.js",
53
- "lib/report.js",
54
- "lib/workspace.js",
55
- "lib/doctor.js",
56
- "bin/watch.js",
57
- "bin/report.js",
58
- "bin/doctor.js",
59
- "bin/archive.js",
60
- "bin/sync.js",
61
- "bin/workbench.js",
62
- "public/workbench.html",
63
- "cordis.patch.yml",
64
- "docs/policy.example.json",
65
- "CHANGELOG.md",
66
- "README.md"
67
- ],
68
- "dependencies": {},
69
- "engines": {
70
- "node": ">=24.9.0",
71
- "dsh": ">=0.2.2-rc.1 <0.3.0-0"
72
- },
73
- "scripts": {
74
- "test": "node --test test/orchestrator.test.mjs test/direct-and-queue.test.mjs test/dialog.test.mjs test/workspace.test.mjs test/store.test.mjs test/errors.test.mjs test/plugin.test.mjs test/guard.test.mjs test/audit.test.mjs test/inject.test.mjs test/sync.test.mjs test/workbench.test.mjs test/usage.test.mjs test/cost.test.mjs test/ledger.test.mjs test/control.test.mjs test/watch.test.mjs test/report.test.mjs test/memory.test.mjs test/notify.test.mjs test/templates.test.mjs test/archive.test.mjs test/doctor.test.mjs test/pack.test.mjs",
75
- "check-pack": "node scripts/check-pack.mjs"
76
- },
77
- "keywords": [
78
- "dsh",
79
- "dsh-plugin",
80
- "deepseek-harness",
81
- "cordis",
82
- "agent",
83
- "autonomous",
84
- "subagent"
85
- ],
86
- "license": "MIT",
87
- "dsh": {
88
- "bundle": {
89
- "patch": "./cordis.patch.yml"
90
- }
91
- }
92
- }
16
+ "icon": "./icon.svg",
17
+ "bin": {
18
+ "dsh-superagent-watch": "bin/watch.js",
19
+ "dsh-superagent-report": "bin/report.js",
20
+ "dsh-superagent-doctor": "bin/doctor.js",
21
+ "dsh-superagent-archive": "bin/archive.js",
22
+ "dsh-superagent-sync": "bin/sync.js",
23
+ "dsh-superagent-workbench": "bin/workbench.js"
24
+ },
25
+ "exports": {
26
+ ".": "./lib/index.js",
27
+ "./icon": "./icon.svg",
28
+ "./locale/*.json": "./locale/*.json",
29
+ "./cordis.patch.yml": "./cordis.patch.yml",
30
+ "./package.json": "./package.json"
31
+ },
32
+ "publishConfig": {
33
+ "registry": "https://registry.npmjs.org",
34
+ "access": "public"
35
+ },
36
+ "files": [
37
+ "icon.svg",
38
+ "locale/*.json",
39
+ "lib/index.js",
40
+ "lib/orchestrator.js",
41
+ "lib/store.js",
42
+ "lib/errors.js",
43
+ "lib/usage.js",
44
+ "lib/cost.js",
45
+ "lib/ledger.js",
46
+ "lib/control.js",
47
+ "lib/memory.js",
48
+ "lib/user-memory.js",
49
+ "lib/guard.js",
50
+ "lib/goal.js",
51
+ "lib/cron.js",
52
+ "lib/schedule.js",
53
+ "lib/notify.js",
54
+ "lib/lock.js",
55
+ "lib/templates.js",
56
+ "lib/policy.js",
57
+ "lib/audit.js",
58
+ "lib/inject.js",
59
+ "lib/sync.js",
60
+ "lib/workbench.js",
61
+ "lib/console.js",
62
+ "lib/watch.js",
63
+ "lib/report.js",
64
+ "lib/workspace.js",
65
+ "lib/doctor.js",
66
+ "lib/browser.js",
67
+ "lib/desktop.js",
68
+ "lib/runtime-control.js",
69
+ "bin/watch.js",
70
+ "bin/report.js",
71
+ "bin/doctor.js",
72
+ "bin/archive.js",
73
+ "bin/sync.js",
74
+ "bin/workbench.js",
75
+ "public/workbench.html",
76
+ "public/console.html",
77
+ "cordis.patch.yml",
78
+ "docs/policy.example.json",
79
+ "docs/DETAIL.md",
80
+ "docs/DETAIL.zh.md",
81
+ "CHANGELOG.md",
82
+ "README.md",
83
+ "README.en.md",
84
+ "README.zh.md"
85
+ ],
86
+ "dependencies": {},
87
+ "engines": {
88
+ "node": ">=24.9.0",
89
+ "dsh": ">=0.2.2-rc.1 <0.3.0-0"
90
+ },
91
+ "scripts": {
92
+ "test": "node --test test/orchestrator.test.mjs test/direct-and-queue.test.mjs test/dialog.test.mjs test/workspace.test.mjs test/store.test.mjs test/errors.test.mjs test/plugin.test.mjs test/guard.test.mjs test/goal.test.mjs test/cron.test.mjs test/schedule.test.mjs test/audit.test.mjs test/inject.test.mjs test/sync.test.mjs test/workbench.test.mjs test/usage.test.mjs test/cost.test.mjs test/ledger.test.mjs test/control.test.mjs test/watch.test.mjs test/report.test.mjs test/memory.test.mjs test/user-memory.test.mjs test/notify.test.mjs test/templates.test.mjs test/archive.test.mjs test/doctor.test.mjs test/pack.test.mjs test/runtime-control.test.mjs test/console.test.mjs",
93
+ "check-pack": "node scripts/check-pack.mjs"
94
+ },
95
+ "keywords": [
96
+ "dsh",
97
+ "dsh-plugin",
98
+ "deepseek-harness",
99
+ "cordis",
100
+ "agent",
101
+ "autonomous",
102
+ "subagent"
103
+ ],
104
+ "license": "MIT",
105
+ "dsh": {
106
+ "bundle": {
107
+ "patch": "./cordis.patch.yml"
108
+ }
109
+ }
110
+ }