@namzu/sdk 6.1.0 → 7.0.0

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.
Files changed (185) hide show
  1. package/CHANGELOG.md +361 -0
  2. package/dist/agents/ReactiveAgent.d.ts.map +1 -1
  3. package/dist/agents/ReactiveAgent.js +5 -0
  4. package/dist/agents/ReactiveAgent.js.map +1 -1
  5. package/dist/agents/SupervisorAgent.d.ts.map +1 -1
  6. package/dist/agents/SupervisorAgent.js +9 -0
  7. package/dist/agents/SupervisorAgent.js.map +1 -1
  8. package/dist/agents/__tests__/supervisor-coordinator-registration.test.js +31 -0
  9. package/dist/agents/__tests__/supervisor-coordinator-registration.test.js.map +1 -1
  10. package/dist/agents/runAgent.d.ts +19 -1
  11. package/dist/agents/runAgent.d.ts.map +1 -1
  12. package/dist/agents/runAgent.js +2 -0
  13. package/dist/agents/runAgent.js.map +1 -1
  14. package/dist/connector/mcp/__tests__/positional-arrays.test.d.ts +2 -0
  15. package/dist/connector/mcp/__tests__/positional-arrays.test.d.ts.map +1 -0
  16. package/dist/connector/mcp/__tests__/positional-arrays.test.js +142 -0
  17. package/dist/connector/mcp/__tests__/positional-arrays.test.js.map +1 -0
  18. package/dist/connector/mcp/adapter.d.ts.map +1 -1
  19. package/dist/connector/mcp/adapter.js +123 -8
  20. package/dist/connector/mcp/adapter.js.map +1 -1
  21. package/dist/constants/agent/index.d.ts +5 -0
  22. package/dist/constants/agent/index.d.ts.map +1 -1
  23. package/dist/constants/agent/index.js +5 -0
  24. package/dist/constants/agent/index.js.map +1 -1
  25. package/dist/constants/plugin/index.d.ts +15 -0
  26. package/dist/constants/plugin/index.d.ts.map +1 -1
  27. package/dist/constants/plugin/index.js +15 -0
  28. package/dist/constants/plugin/index.js.map +1 -1
  29. package/dist/gateway/__tests__/completion-inbox.test.js +17 -0
  30. package/dist/gateway/__tests__/completion-inbox.test.js.map +1 -1
  31. package/dist/gateway/completion-inbox.d.ts.map +1 -1
  32. package/dist/gateway/completion-inbox.js +17 -3
  33. package/dist/gateway/completion-inbox.js.map +1 -1
  34. package/dist/gateway/local.d.ts +11 -0
  35. package/dist/gateway/local.d.ts.map +1 -1
  36. package/dist/gateway/local.js +27 -1
  37. package/dist/gateway/local.js.map +1 -1
  38. package/dist/manager/agent/lifecycle.d.ts.map +1 -1
  39. package/dist/manager/agent/lifecycle.js +6 -0
  40. package/dist/manager/agent/lifecycle.js.map +1 -1
  41. package/dist/provider/thinking-support.d.ts +2 -1
  42. package/dist/provider/thinking-support.d.ts.map +1 -1
  43. package/dist/provider/thinking-support.js +14 -0
  44. package/dist/provider/thinking-support.js.map +1 -1
  45. package/dist/public-runtime.d.ts +1 -1
  46. package/dist/public-runtime.d.ts.map +1 -1
  47. package/dist/public-runtime.js +9 -1
  48. package/dist/public-runtime.js.map +1 -1
  49. package/dist/runtime/query/__tests__/context.test.js +18 -0
  50. package/dist/runtime/query/__tests__/context.test.js.map +1 -1
  51. package/dist/runtime/query/__tests__/effort-reaches-the-wire.test.d.ts +2 -0
  52. package/dist/runtime/query/__tests__/effort-reaches-the-wire.test.d.ts.map +1 -0
  53. package/dist/runtime/query/__tests__/effort-reaches-the-wire.test.js +118 -0
  54. package/dist/runtime/query/__tests__/effort-reaches-the-wire.test.js.map +1 -0
  55. package/dist/runtime/query/__tests__/run-survives-its-own-park.proc-test.d.ts +2 -0
  56. package/dist/runtime/query/__tests__/run-survives-its-own-park.proc-test.d.ts.map +1 -0
  57. package/dist/runtime/query/__tests__/run-survives-its-own-park.proc-test.js +122 -0
  58. package/dist/runtime/query/__tests__/run-survives-its-own-park.proc-test.js.map +1 -0
  59. package/dist/runtime/query/__tests__/tool-timeout.test.js +34 -0
  60. package/dist/runtime/query/__tests__/tool-timeout.test.js.map +1 -1
  61. package/dist/runtime/query/context.d.ts.map +1 -1
  62. package/dist/runtime/query/context.js +16 -1
  63. package/dist/runtime/query/context.js.map +1 -1
  64. package/dist/runtime/query/executor.d.ts.map +1 -1
  65. package/dist/runtime/query/executor.js +11 -1
  66. package/dist/runtime/query/executor.js.map +1 -1
  67. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  68. package/dist/runtime/query/iteration/index.js +5 -0
  69. package/dist/runtime/query/iteration/index.js.map +1 -1
  70. package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
  71. package/dist/runtime/query/iteration/phases/context.js +43 -13
  72. package/dist/runtime/query/iteration/phases/context.js.map +1 -1
  73. package/dist/telemetry/__tests__/model-call-span.test.js +22 -4
  74. package/dist/telemetry/__tests__/model-call-span.test.js.map +1 -1
  75. package/dist/telemetry/__tests__/span-closure.test.js +12 -5
  76. package/dist/telemetry/__tests__/span-closure.test.js.map +1 -1
  77. package/dist/tools/builtins/__tests__/bash.proc-test.d.ts +2 -0
  78. package/dist/tools/builtins/__tests__/bash.proc-test.d.ts.map +1 -0
  79. package/dist/tools/builtins/__tests__/bash.proc-test.js +115 -0
  80. package/dist/tools/builtins/__tests__/bash.proc-test.js.map +1 -0
  81. package/dist/tools/builtins/__tests__/bash.test.d.ts +2 -0
  82. package/dist/tools/builtins/__tests__/bash.test.d.ts.map +1 -0
  83. package/dist/tools/builtins/__tests__/bash.test.js +65 -0
  84. package/dist/tools/builtins/__tests__/bash.test.js.map +1 -0
  85. package/dist/tools/builtins/bash.d.ts.map +1 -1
  86. package/dist/tools/builtins/bash.js +89 -17
  87. package/dist/tools/builtins/bash.js.map +1 -1
  88. package/dist/tools/coordinator/__tests__/allow-delegation.test.d.ts +2 -0
  89. package/dist/tools/coordinator/__tests__/allow-delegation.test.d.ts.map +1 -0
  90. package/dist/tools/coordinator/__tests__/allow-delegation.test.js +94 -0
  91. package/dist/tools/coordinator/__tests__/allow-delegation.test.js.map +1 -0
  92. package/dist/tools/coordinator/__tests__/wait-with-idle-bound.test.d.ts +2 -0
  93. package/dist/tools/coordinator/__tests__/wait-with-idle-bound.test.d.ts.map +1 -0
  94. package/dist/tools/coordinator/__tests__/wait-with-idle-bound.test.js +193 -0
  95. package/dist/tools/coordinator/__tests__/wait-with-idle-bound.test.js.map +1 -0
  96. package/dist/tools/coordinator/index.d.ts +28 -0
  97. package/dist/tools/coordinator/index.d.ts.map +1 -1
  98. package/dist/tools/coordinator/index.js +101 -62
  99. package/dist/tools/coordinator/index.js.map +1 -1
  100. package/dist/tools/coordinator/wait-with-idle-bound.d.ts +66 -0
  101. package/dist/tools/coordinator/wait-with-idle-bound.d.ts.map +1 -0
  102. package/dist/tools/coordinator/wait-with-idle-bound.js +78 -0
  103. package/dist/tools/coordinator/wait-with-idle-bound.js.map +1 -0
  104. package/dist/types/agent/base.d.ts +16 -0
  105. package/dist/types/agent/base.d.ts.map +1 -1
  106. package/dist/types/agent/gateway.d.ts +23 -0
  107. package/dist/types/agent/gateway.d.ts.map +1 -1
  108. package/dist/types/agent/lifecycle-event.d.ts +9 -1
  109. package/dist/types/agent/lifecycle-event.d.ts.map +1 -1
  110. package/dist/types/agent/supervisor.d.ts +27 -0
  111. package/dist/types/agent/supervisor.d.ts.map +1 -1
  112. package/dist/types/agent/task.d.ts +5 -0
  113. package/dist/types/agent/task.d.ts.map +1 -1
  114. package/dist/types/hitl/index.d.ts +10 -0
  115. package/dist/types/hitl/index.d.ts.map +1 -1
  116. package/dist/types/hitl/index.js.map +1 -1
  117. package/dist/types/probe/registry.d.ts +6 -0
  118. package/dist/types/probe/registry.d.ts.map +1 -1
  119. package/dist/types/run/config.d.ts +25 -0
  120. package/dist/types/run/config.d.ts.map +1 -1
  121. package/dist/utils/__tests__/abort-reason.test.d.ts +2 -0
  122. package/dist/utils/__tests__/abort-reason.test.d.ts.map +1 -0
  123. package/dist/utils/__tests__/abort-reason.test.js +48 -0
  124. package/dist/utils/__tests__/abort-reason.test.js.map +1 -0
  125. package/dist/utils/abort.d.ts +26 -0
  126. package/dist/utils/abort.d.ts.map +1 -1
  127. package/dist/utils/abort.js +34 -0
  128. package/dist/utils/abort.js.map +1 -1
  129. package/dist/verification/__tests__/rule-order-and-reason.test.d.ts +2 -0
  130. package/dist/verification/__tests__/rule-order-and-reason.test.d.ts.map +1 -0
  131. package/dist/verification/__tests__/rule-order-and-reason.test.js +126 -0
  132. package/dist/verification/__tests__/rule-order-and-reason.test.js.map +1 -0
  133. package/dist/verification/gate.d.ts +17 -1
  134. package/dist/verification/gate.d.ts.map +1 -1
  135. package/dist/verification/gate.js +58 -2
  136. package/dist/verification/gate.js.map +1 -1
  137. package/dist/verification/index.d.ts +1 -1
  138. package/dist/verification/index.d.ts.map +1 -1
  139. package/dist/verification/index.js +1 -1
  140. package/dist/verification/index.js.map +1 -1
  141. package/package.json +3 -2
  142. package/src/agents/ReactiveAgent.ts +5 -0
  143. package/src/agents/SupervisorAgent.ts +9 -0
  144. package/src/agents/__tests__/supervisor-coordinator-registration.test.ts +37 -0
  145. package/src/agents/runAgent.ts +22 -1
  146. package/src/connector/mcp/__tests__/positional-arrays.test.ts +183 -0
  147. package/src/connector/mcp/adapter.ts +131 -7
  148. package/src/constants/agent/index.ts +5 -0
  149. package/src/constants/plugin/index.ts +15 -0
  150. package/src/gateway/__tests__/completion-inbox.test.ts +23 -0
  151. package/src/gateway/completion-inbox.ts +17 -3
  152. package/src/gateway/local.ts +26 -1
  153. package/src/manager/agent/lifecycle.ts +6 -0
  154. package/src/provider/thinking-support.ts +19 -2
  155. package/src/public-runtime.ts +9 -0
  156. package/src/runtime/query/__tests__/context.test.ts +24 -0
  157. package/src/runtime/query/__tests__/effort-reaches-the-wire.test.ts +135 -0
  158. package/src/runtime/query/__tests__/run-survives-its-own-park.proc-test.ts +127 -0
  159. package/src/runtime/query/__tests__/tool-timeout.test.ts +38 -0
  160. package/src/runtime/query/context.ts +16 -1
  161. package/src/runtime/query/executor.ts +11 -1
  162. package/src/runtime/query/iteration/index.ts +5 -0
  163. package/src/runtime/query/iteration/phases/context.ts +41 -15
  164. package/src/telemetry/__tests__/model-call-span.test.ts +22 -5
  165. package/src/telemetry/__tests__/span-closure.test.ts +12 -5
  166. package/src/tools/builtins/__tests__/bash.proc-test.ts +142 -0
  167. package/src/tools/builtins/__tests__/bash.test.ts +79 -0
  168. package/src/tools/builtins/bash.ts +100 -18
  169. package/src/tools/coordinator/__tests__/allow-delegation.test.ts +120 -0
  170. package/src/tools/coordinator/__tests__/wait-with-idle-bound.test.ts +247 -0
  171. package/src/tools/coordinator/index.ts +112 -65
  172. package/src/tools/coordinator/wait-with-idle-bound.ts +142 -0
  173. package/src/types/agent/base.ts +17 -0
  174. package/src/types/agent/gateway.ts +24 -0
  175. package/src/types/agent/lifecycle-event.ts +7 -0
  176. package/src/types/agent/supervisor.ts +28 -0
  177. package/src/types/agent/task.ts +5 -0
  178. package/src/types/hitl/index.ts +10 -0
  179. package/src/types/probe/registry.ts +6 -0
  180. package/src/types/run/config.ts +26 -0
  181. package/src/utils/__tests__/abort-reason.test.ts +56 -0
  182. package/src/utils/abort.ts +34 -0
  183. package/src/verification/__tests__/rule-order-and-reason.test.ts +149 -0
  184. package/src/verification/gate.ts +61 -3
  185. package/src/verification/index.ts +1 -1
@@ -0,0 +1,142 @@
1
+ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
2
+ import { tmpdir } from 'node:os'
3
+ import { join } from 'node:path'
4
+ import { afterEach, describe, expect, it } from 'vitest'
5
+
6
+ import type { ToolContext } from '../../../types/tool/index.js'
7
+ import { BashTool } from '../bash.js'
8
+
9
+ /**
10
+ * What a shell command actually tells the model, measured by running one.
11
+ *
12
+ * The only builtin that runs a shell had no test at all, and what that cost
13
+ * is visible in the code it shipped: the host path called `exec` with no
14
+ * `catch`, and `exec` REJECTS on a non-zero exit. So the two things an agent
15
+ * runs a shell for most — a test run and a build — both threw, and the
16
+ * registry turned the throw into "the tool failed" with none of the stdout,
17
+ * stderr or exit code that explains why. The sandbox path beside it reported
18
+ * all three, so the same command told the model two different amounts
19
+ * depending on where it happened to run.
20
+ *
21
+ * These spawn real processes, so they live in the `proc-test` suite rather
22
+ * than the unit one — measured, running them beside 2594 unit tests flaked
23
+ * four unrelated timing-sensitive tests. The assertions that need no shell
24
+ * stayed behind in `bash.test.ts`.
25
+ *
26
+ * Commands are written to behave identically under `cmd.exe` and `sh`,
27
+ * because `exec` picks the platform shell and a test that only passes on one
28
+ * of them is a test that fails for whoever is on the other.
29
+ */
30
+
31
+ const dirs: string[] = []
32
+ afterEach(() => {
33
+ for (const dir of dirs) {
34
+ // A killed child can still hold its working directory for a moment —
35
+ // on Windows that surfaces as EBUSY, and it failed the timeout test
36
+ // from the cleanup rather than the assertion, which is the most
37
+ // misleading way for a test to go red. Retry, then let it go: a temp
38
+ // directory that outlives the run is the operating system's problem,
39
+ // not a result worth reporting.
40
+ try {
41
+ rmSync(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 })
42
+ } catch {
43
+ // Deliberately swallowed. See above.
44
+ }
45
+ }
46
+ dirs.length = 0
47
+ })
48
+
49
+ function ctx(): ToolContext {
50
+ const workingDirectory = mkdtempSync(join(tmpdir(), 'namzu-bash-'))
51
+ dirs.push(workingDirectory)
52
+ return { workingDirectory } as ToolContext
53
+ }
54
+
55
+ async function run(
56
+ input: Record<string, unknown>,
57
+ context: ToolContext = ctx(),
58
+ ): Promise<{ success: boolean; output: string; error?: string; data?: Record<string, unknown> }> {
59
+ const parsed = BashTool.inputSchema.parse(input)
60
+ return (await BashTool.execute(parsed as never, context)) as never
61
+ }
62
+
63
+ describe('a command that succeeds', () => {
64
+ it('returns its stdout', async () => {
65
+ const result = await run({ command: 'echo hello' })
66
+
67
+ expect(result.success).toBe(true)
68
+ expect(result.output).toContain('hello')
69
+ expect(result.data?.exitCode).toBe(0)
70
+ })
71
+
72
+ it('runs in the working directory it was given', async () => {
73
+ const context = ctx()
74
+ writeFileSync(join(context.workingDirectory, 'marker.txt'), 'x')
75
+
76
+ const result = await run(
77
+ { command: `node -e "console.log(require('fs').readdirSync('.'))"` },
78
+ context,
79
+ )
80
+
81
+ expect(result.output).toContain('marker.txt')
82
+ })
83
+
84
+ it('says so rather than returning an empty string', async () => {
85
+ const result = await run({ command: 'node -e ""' })
86
+
87
+ expect(result.output).toBe('(no output)')
88
+ })
89
+ })
90
+
91
+ describe('a command that fails still says what happened', () => {
92
+ it('reports the exit code instead of throwing', async () => {
93
+ // The whole defect: this used to reject out of `execute`.
94
+ const result = await run({ command: 'node -e "process.exit(3)"' })
95
+
96
+ expect(result.success).toBe(false)
97
+ expect(result.data?.exitCode).toBe(3)
98
+ expect(result.error).toContain('exited with code 3')
99
+ })
100
+
101
+ it('keeps the output a failing command produced', async () => {
102
+ // The reason a model runs a shell at all: a failing test prints WHY it
103
+ // failed, on stdout, before exiting non-zero.
104
+ const result = await run({
105
+ command: `node -e "console.log('3 tests failed'); process.exit(1)"`,
106
+ })
107
+
108
+ expect(result.success).toBe(false)
109
+ expect(result.output, 'the failure output was discarded').toContain('3 tests failed')
110
+ })
111
+
112
+ it('keeps stderr too', async () => {
113
+ const result = await run({
114
+ command: `node -e "console.error('compiler said no'); process.exit(2)"`,
115
+ })
116
+
117
+ expect(result.output).toContain('compiler said no')
118
+ expect(result.data?.exitCode).toBe(2)
119
+ })
120
+
121
+ it('reports a missing command as a failure, not as success', async () => {
122
+ const result = await run({ command: 'definitely-not-a-real-command-xyz' })
123
+
124
+ expect(result.success).toBe(false)
125
+ expect(result.error).toBeTruthy()
126
+ })
127
+ })
128
+
129
+ describe('a command that runs out of time', () => {
130
+ it('says it timed out rather than that it exited', async () => {
131
+ // "Ran out of time" and "exited 1" are different diagnoses and lead to
132
+ // different next moves, so the message has to distinguish them.
133
+ const result = await run({
134
+ command: `node -e "setTimeout(() => {}, 10000)"`,
135
+ timeout: 300,
136
+ })
137
+
138
+ expect(result.success).toBe(false)
139
+ expect(result.data?.timedOut).toBe(true)
140
+ expect(result.error).toContain('timed out')
141
+ }, 20_000)
142
+ })
@@ -0,0 +1,79 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import { BashTool } from '../bash.js'
4
+
5
+ /**
6
+ * What `bash` promises before it runs anything.
7
+ *
8
+ * These need no shell, so they stay in the unit suite. The ones that actually
9
+ * execute a command live in `bash.proc-test.ts` — spawning real processes
10
+ * beside 2594 unit tests flaked four unrelated timing-sensitive ones, so the
11
+ * process suite is separate and has its own CI step.
12
+ */
13
+
14
+ describe('the two clocks agree', () => {
15
+ it('declares a deadline of its own', () => {
16
+ // The executor reads a tool's `timeoutMs` before falling back to its
17
+ // generic default. With none declared, `bash` inherited that default —
18
+ // the same two minutes as its OWN default — so the two agreed by
19
+ // coincidence and diverged the moment a model asked for longer because
20
+ // it knew a build was slow. It got two minutes, from a clock it had not
21
+ // been told about, reported as an abandoned tool rather than as a
22
+ // command that ran out of time.
23
+ expect(BashTool.timeoutMs).toBeDefined()
24
+ })
25
+
26
+ it('puts that deadline above the longest the model may request', () => {
27
+ // So this tool's own clock is the one that fires, and the executor's is
28
+ // a backstop rather than a second clock racing it.
29
+ const accepted = BashTool.inputSchema.safeParse({ command: 'true', timeout: 10 * 60 * 1000 })
30
+
31
+ expect(accepted.success).toBe(true)
32
+ expect(BashTool.timeoutMs as number).toBeGreaterThan(10 * 60 * 1000)
33
+ })
34
+
35
+ it('refuses an over-long request rather than silently shortening it', () => {
36
+ // Refuse, do not degrade. A number the model was not told had changed
37
+ // is how it learns to distrust its own arguments.
38
+ const overCeiling = BashTool.inputSchema.safeParse({
39
+ command: 'true',
40
+ timeout: 60 * 60 * 1000,
41
+ })
42
+
43
+ expect(overCeiling.success, 'the ceiling is not enforced').toBe(false)
44
+ })
45
+
46
+ it('refuses a nonsensical deadline', () => {
47
+ expect(BashTool.inputSchema.safeParse({ command: 'true', timeout: 0 }).success).toBe(false)
48
+ expect(BashTool.inputSchema.safeParse({ command: 'true', timeout: -1 }).success).toBe(false)
49
+ })
50
+
51
+ it('applies its default when none is given', () => {
52
+ const parsed = BashTool.inputSchema.parse({ command: 'true' })
53
+
54
+ expect(parsed.timeout).toBeGreaterThan(0)
55
+ })
56
+ })
57
+
58
+ describe('the input is closed before a shell ever sees it', () => {
59
+ it('refuses an empty command', () => {
60
+ expect(BashTool.inputSchema.safeParse({ command: '' }).success).toBe(false)
61
+ })
62
+
63
+ it('accepts a numeric timeout sent as a string', () => {
64
+ // Providers do this, and the coercion is deliberate.
65
+ const parsed = BashTool.inputSchema.parse({ command: 'true', timeout: '5000' })
66
+
67
+ expect(parsed.timeout).toBe(5000)
68
+ })
69
+ })
70
+
71
+ describe('the danger flag reads the command', () => {
72
+ it('marks a destructive command destructive', () => {
73
+ expect(BashTool.isDestructive?.({ command: 'rm -rf /', timeout: 1000 } as never)).toBe(true)
74
+ })
75
+
76
+ it('leaves an ordinary command alone', () => {
77
+ expect(BashTool.isDestructive?.({ command: 'ls -la', timeout: 1000 } as never)).toBe(false)
78
+ })
79
+ })
@@ -22,6 +22,25 @@ const DEFAULT_BASH_MAX_BUFFER_BYTES = readPositiveIntEnv(
22
22
  100 * 1024 * 1024,
23
23
  )
24
24
 
25
+ /**
26
+ * The longest deadline this tool will accept from the model.
27
+ *
28
+ * There are two clocks on a bash call and until now only one of them was
29
+ * declared. This tool enforces `input.timeout` itself; the EXECUTOR enforces
30
+ * a separate per-tool deadline, and with none declared here it fell back to
31
+ * its own generic default — also two minutes. The two agreed by coincidence,
32
+ * so a model that asked for five minutes because it knew the build was slow
33
+ * got two, from a clock it had not been told about, reported as an abandoned
34
+ * tool rather than as a command that ran out of time.
35
+ *
36
+ * So the tool declares a ceiling and the executor is given a deadline above
37
+ * it (see `timeoutMs` on the definition), which makes this the only clock
38
+ * that can fire in practice. A request past the ceiling is REFUSED rather
39
+ * than quietly clamped: the model asked for something specific, and silently
40
+ * giving it a different number is how it learns to distrust the answer.
41
+ */
42
+ const MAX_BASH_TIMEOUT_MS = readPositiveIntEnv('NAMZU_BASH_MAX_TIMEOUT_MS', 10 * 60 * 1000)
43
+
25
44
  const inputSchema = z.object({
26
45
  command: z
27
46
  .string()
@@ -32,9 +51,11 @@ const inputSchema = z.object({
32
51
  timeout: z
33
52
  .preprocess(
34
53
  (v) => (typeof v === 'string' ? Number(v) : v),
35
- z.number().default(DEFAULT_BASH_TIMEOUT_MS),
54
+ z.number().positive().max(MAX_BASH_TIMEOUT_MS).default(DEFAULT_BASH_TIMEOUT_MS),
36
55
  )
37
- .describe(`Command timeout in milliseconds. Default: ${DEFAULT_BASH_TIMEOUT_MS}`),
56
+ .describe(
57
+ `Command timeout in milliseconds. Default: ${DEFAULT_BASH_TIMEOUT_MS}, maximum: ${MAX_BASH_TIMEOUT_MS}. For work that legitimately runs longer than the maximum, start it in the background and poll, rather than holding the turn open.`,
58
+ ),
38
59
  })
39
60
 
40
61
  type BashInput = z.infer<typeof inputSchema>
@@ -53,6 +74,12 @@ export const BashTool = defineTool({
53
74
  readOnly: false,
54
75
  destructive: (input: BashInput) => isDangerousCommand(input.command),
55
76
  concurrencySafe: false,
77
+ // Above the ceiling the input schema accepts, so the executor's deadline
78
+ // is a backstop rather than a second clock racing this tool's own. It used
79
+ // to be undefined, which meant the executor's generic default applied —
80
+ // the same two minutes as this tool's DEFAULT, so they agreed by accident
81
+ // and diverged the moment a model asked for longer.
82
+ timeoutMs: MAX_BASH_TIMEOUT_MS + 30_000,
56
83
 
57
84
  async execute(input, context) {
58
85
  if (isDangerousCommand(input.command)) {
@@ -133,26 +160,81 @@ export const BashTool = defineTool({
133
160
  // a Stop tore down the model stream and left the command running,
134
161
  // and the executor's deadline could only ever DETACH from the tool
135
162
  // rather than end the work it started.
136
- const { stdout, stderr } = await execAsync(input.command, {
137
- cwd: context.workingDirectory,
138
- timeout: input.timeout,
139
- env: { ...process.env, ...context.env },
140
- maxBuffer: DEFAULT_BASH_MAX_BUFFER_BYTES,
141
- signal: context.abortSignal,
142
- })
143
-
144
- const output = [stdout ? `STDOUT:\n${stdout}` : '', stderr ? `STDERR:\n${stderr}` : '']
145
- .filter(Boolean)
146
- .join('\n\n')
147
-
148
- return {
149
- success: true,
150
- output: output || '(no output)',
151
- data: { exitCode: 0 },
163
+ // `exec` REJECTS on a non-zero exit, on its own timeout, and on a
164
+ // kill — and the rejection carries `stdout`, `stderr`, `code` and
165
+ // `killed`. Letting it propagate threw all of that away: the registry
166
+ // turned the throw into a structured failure, so the model was told a
167
+ // command failed and not one word about how.
168
+ //
169
+ // That is the common case, not an edge one. A failing test run and a
170
+ // failing build are the two things an agent runs bash for most, and
171
+ // both exit non-zero WITH the output that explains why. The sandbox
172
+ // branch above already reports all of it; this branch did not, so the
173
+ // same command told the model two different amounts depending on where
174
+ // it happened to run.
175
+ try {
176
+ const { stdout, stderr } = await execAsync(input.command, {
177
+ cwd: context.workingDirectory,
178
+ timeout: input.timeout,
179
+ env: { ...process.env, ...context.env },
180
+ maxBuffer: DEFAULT_BASH_MAX_BUFFER_BYTES,
181
+ signal: context.abortSignal,
182
+ })
183
+
184
+ return {
185
+ success: true,
186
+ output: formatShellOutput(stdout, stderr) || '(no output)',
187
+ data: { exitCode: 0 },
188
+ }
189
+ } catch (err) {
190
+ const failure = err as NodeJS.ErrnoException & {
191
+ stdout?: string
192
+ stderr?: string
193
+ code?: number | string
194
+ killed?: boolean
195
+ signal?: string
196
+ }
197
+
198
+ // A caller-owned Stop is the caller's, not a command failure.
199
+ if (context.abortSignal?.aborted) throw err
200
+
201
+ // `exec` reports its own timeout as a kill, and the distinction
202
+ // matters to the model: "ran out of time" is a different next move
203
+ // from "exited 1".
204
+ const timedOut = failure.killed === true && failure.signal === 'SIGTERM'
205
+ const exitCode = typeof failure.code === 'number' ? failure.code : undefined
206
+ const output = formatShellOutput(failure.stdout, failure.stderr)
207
+
208
+ return {
209
+ success: false,
210
+ output: output || '(no output)',
211
+ data: {
212
+ ...(exitCode !== undefined ? { exitCode } : {}),
213
+ timedOut,
214
+ ...(failure.signal ? { signal: failure.signal } : {}),
215
+ },
216
+ error: timedOut
217
+ ? `Command timed out after ${input.timeout}ms. Any output it produced before the deadline is above.`
218
+ : exitCode !== undefined
219
+ ? `Command exited with code ${exitCode}`
220
+ : `Command failed: ${failure.message}`,
221
+ }
152
222
  }
153
223
  },
154
224
  })
155
225
 
226
+ /**
227
+ * The two streams, labelled, with empty ones left out.
228
+ *
229
+ * Shared by the success and failure paths so a command tells the model the
230
+ * same shape either way — the failure path used to tell it nothing at all.
231
+ */
232
+ function formatShellOutput(stdout: string | undefined, stderr: string | undefined): string {
233
+ return [stdout ? `STDOUT:\n${stdout}` : '', stderr ? `STDERR:\n${stderr}` : '']
234
+ .filter(Boolean)
235
+ .join('\n\n')
236
+ }
237
+
156
238
  function readPositiveIntEnv(key: string, fallback: number): number {
157
239
  const value = process.env[key]?.trim()
158
240
  if (!value) return fallback
@@ -0,0 +1,120 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import type { TaskGateway } from '../../../types/agent/gateway.js'
4
+ import { buildCoordinatorTools } from '../index.js'
5
+
6
+ /**
7
+ * Whether a run may delegate is not the same question as who it may delegate
8
+ * to, and only the caller can answer the first.
9
+ *
10
+ * The roster answers WHO. It cannot answer WHETHER, because two runs are
11
+ * indistinguishable in it: a supervisor whose roster happens to hold one
12
+ * specialist, where delegating is the point; and a run whose own persona IS
13
+ * that specialist, where delegating is delegating to itself. A host builds the
14
+ * second by putting a specialist's persona into the supervisor shell and its
15
+ * id into the roster — so a predicate comparing the roster against the
16
+ * executing agent sees two different ids and cheerfully says "can delegate".
17
+ *
18
+ * Measured before this existed: such a run carried `create_task`,
19
+ * `wait_for_task`, `cancel_task` and `agent_task_list`, byte-identical to a
20
+ * run that could actually delegate, and the model could only discover the
21
+ * refusal by spending a turn on it.
22
+ */
23
+
24
+ const gateway = {
25
+ listTasks: () => [],
26
+ onTaskCompleted: () => () => {},
27
+ } as unknown as TaskGateway
28
+
29
+ function namesFor(opts: {
30
+ agentIds: string[]
31
+ allowDelegation?: boolean
32
+ withHitl?: boolean
33
+ }): string[] {
34
+ return buildCoordinatorTools({
35
+ gateway,
36
+ workingDirectory: '/tmp/test',
37
+ allowedAgentIds: opts.agentIds,
38
+ ...(opts.allowDelegation !== undefined ? { allowDelegation: opts.allowDelegation } : {}),
39
+ ...(opts.withHitl
40
+ ? { resumeHandler: (async () => ({ action: 'continue' })) as never, runId: 'run_1' as never }
41
+ : {}),
42
+ }).map((t) => t.name)
43
+ }
44
+
45
+ describe('a run can decline to delegate while still naming who it would have called', () => {
46
+ it('withholds the delegation tools when delegation is off', () => {
47
+ const names = namesFor({ agentIds: ['specialist'], allowDelegation: false })
48
+
49
+ expect(names).not.toContain('create_task')
50
+ expect(names).not.toContain('wait_for_task')
51
+ expect(names).not.toContain('cancel_task')
52
+ })
53
+
54
+ it('produces exactly the empty-roster surface', () => {
55
+ // The two reasons differ but the outcome is the same one tool, so a
56
+ // reader does not have to hold two shapes in their head.
57
+ expect(namesFor({ agentIds: ['specialist'], allowDelegation: false })).toEqual(
58
+ namesFor({ agentIds: [] }),
59
+ )
60
+ })
61
+
62
+ it('keeps the listing, because a run may still want to see what is running', () => {
63
+ expect(namesFor({ agentIds: ['specialist'], allowDelegation: false })).toContain(
64
+ 'agent_task_list',
65
+ )
66
+ })
67
+
68
+ it('leaves the human channel alone — that is not delegation', () => {
69
+ // `approve_plan` and `ask_user_question` are the HITL park surface. A
70
+ // run that must not delegate needs them as much as any other.
71
+ const names = namesFor({ agentIds: ['specialist'], allowDelegation: false, withHitl: true })
72
+
73
+ expect(names).toContain('ask_user_question')
74
+ })
75
+ })
76
+
77
+ describe('an absent flag changes nothing', () => {
78
+ it('mounts the full surface, as it always did', () => {
79
+ const names = namesFor({ agentIds: ['specialist'] })
80
+
81
+ expect(names).toContain('create_task')
82
+ expect(names).toContain('wait_for_task')
83
+ expect(names).toContain('cancel_task')
84
+ expect(names).toContain('agent_task_list')
85
+ })
86
+
87
+ it('is identical to opting in explicitly', () => {
88
+ // So adopting the flag cannot change a caller that says yes out loud.
89
+ expect(namesFor({ agentIds: ['a', 'b'], allowDelegation: true })).toEqual(
90
+ namesFor({ agentIds: ['a', 'b'] }),
91
+ )
92
+ })
93
+
94
+ it('still withholds everything on an empty roster, flag or no flag', () => {
95
+ expect(namesFor({ agentIds: [], allowDelegation: true })).toEqual(['agent_task_list'])
96
+ })
97
+ })
98
+
99
+ describe('the flag is absolute', () => {
100
+ it('cannot be overridden back on', () => {
101
+ // Worth a test precisely because the opposite is the intuitive guess:
102
+ // "explicit beats implicit" would say a runtime override should win.
103
+ // It cannot, mechanically — the override pass in SupervisorAgent runs
104
+ // over the array this builder returns, and there is no entry for it to
105
+ // act on. And it should not: both values come from the same caller in
106
+ // the same call, so "this run must not delegate" plus "give it
107
+ // create_task" is a caller contradicting itself, not one who knows
108
+ // something extra.
109
+ //
110
+ // Same rule the empty roster has always had.
111
+ const tools = buildCoordinatorTools({
112
+ gateway,
113
+ workingDirectory: '/tmp/test',
114
+ allowedAgentIds: ['specialist'],
115
+ allowDelegation: false,
116
+ })
117
+
118
+ expect(tools.find((t) => t.name === 'create_task')).toBeUndefined()
119
+ })
120
+ })