@namzu/sdk 59.0.0 → 60.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 (260) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/dist/compaction/context-window.d.ts +53 -0
  3. package/dist/compaction/context-window.d.ts.map +1 -1
  4. package/dist/compaction/context-window.js +141 -2
  5. package/dist/compaction/context-window.js.map +1 -1
  6. package/dist/execution/workflow/index.d.ts +3 -1
  7. package/dist/execution/workflow/index.d.ts.map +1 -1
  8. package/dist/execution/workflow/index.js +2 -1
  9. package/dist/execution/workflow/index.js.map +1 -1
  10. package/dist/execution/workflow/journal.d.ts +24 -4
  11. package/dist/execution/workflow/journal.d.ts.map +1 -1
  12. package/dist/execution/workflow/journal.js +131 -9
  13. package/dist/execution/workflow/journal.js.map +1 -1
  14. package/dist/execution/workflow/prelude.d.ts +2 -0
  15. package/dist/execution/workflow/prelude.d.ts.map +1 -1
  16. package/dist/execution/workflow/prelude.js +49 -8
  17. package/dist/execution/workflow/prelude.js.map +1 -1
  18. package/dist/execution/workflow/resume.d.ts +59 -0
  19. package/dist/execution/workflow/resume.d.ts.map +1 -0
  20. package/dist/execution/workflow/resume.js +273 -0
  21. package/dist/execution/workflow/resume.js.map +1 -0
  22. package/dist/execution/workflow/runtime.d.ts.map +1 -1
  23. package/dist/execution/workflow/runtime.js +112 -8
  24. package/dist/execution/workflow/runtime.js.map +1 -1
  25. package/dist/execution/workflow/types.d.ts +105 -1
  26. package/dist/execution/workflow/types.d.ts.map +1 -1
  27. package/dist/index.d.ts +3 -0
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +4 -0
  30. package/dist/index.js.map +1 -1
  31. package/dist/plugin/compat.d.ts +24 -0
  32. package/dist/plugin/compat.d.ts.map +1 -0
  33. package/dist/plugin/compat.js +149 -0
  34. package/dist/plugin/compat.js.map +1 -0
  35. package/dist/plugin/loader.d.ts +2 -1
  36. package/dist/plugin/loader.d.ts.map +1 -1
  37. package/dist/plugin/loader.js +6 -0
  38. package/dist/plugin/loader.js.map +1 -1
  39. package/dist/pricing/index.d.ts.map +1 -1
  40. package/dist/pricing/index.js +2 -1
  41. package/dist/pricing/index.js.map +1 -1
  42. package/dist/process/kill-tree.d.ts +44 -0
  43. package/dist/process/kill-tree.d.ts.map +1 -1
  44. package/dist/process/kill-tree.js +102 -4
  45. package/dist/process/kill-tree.js.map +1 -1
  46. package/dist/prompt/coding-agent-doctrine.d.ts +1 -1
  47. package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
  48. package/dist/prompt/coding-agent-doctrine.js +9 -1
  49. package/dist/prompt/coding-agent-doctrine.js.map +1 -1
  50. package/dist/provider/model-window-suffix.d.ts +18 -0
  51. package/dist/provider/model-window-suffix.d.ts.map +1 -0
  52. package/dist/provider/model-window-suffix.js +77 -0
  53. package/dist/provider/model-window-suffix.js.map +1 -0
  54. package/dist/public-runtime.d.ts +5 -3
  55. package/dist/public-runtime.d.ts.map +1 -1
  56. package/dist/public-runtime.js +5 -4
  57. package/dist/public-runtime.js.map +1 -1
  58. package/dist/public-types.d.ts +1 -0
  59. package/dist/public-types.d.ts.map +1 -1
  60. package/dist/registry/command/kernel-commands.d.ts +1 -4
  61. package/dist/registry/command/kernel-commands.d.ts.map +1 -1
  62. package/dist/registry/command/kernel-commands.js +0 -28
  63. package/dist/registry/command/kernel-commands.js.map +1 -1
  64. package/dist/runtime/jobs/awaited-jobs.d.ts +6 -0
  65. package/dist/runtime/jobs/awaited-jobs.d.ts.map +1 -1
  66. package/dist/runtime/jobs/awaited-jobs.js +11 -0
  67. package/dist/runtime/jobs/awaited-jobs.js.map +1 -1
  68. package/dist/runtime/jobs/host-exit-finalizer.d.ts +20 -0
  69. package/dist/runtime/jobs/host-exit-finalizer.d.ts.map +1 -0
  70. package/dist/runtime/jobs/host-exit-finalizer.js +57 -0
  71. package/dist/runtime/jobs/host-exit-finalizer.js.map +1 -0
  72. package/dist/runtime/jobs/liveness.d.ts +67 -0
  73. package/dist/runtime/jobs/liveness.d.ts.map +1 -0
  74. package/dist/runtime/jobs/liveness.js +94 -0
  75. package/dist/runtime/jobs/liveness.js.map +1 -0
  76. package/dist/runtime/jobs/registry.d.ts +21 -0
  77. package/dist/runtime/jobs/registry.d.ts.map +1 -1
  78. package/dist/runtime/jobs/registry.js +104 -2
  79. package/dist/runtime/jobs/registry.js.map +1 -1
  80. package/dist/runtime/jobs/wake-budget.d.ts +35 -0
  81. package/dist/runtime/jobs/wake-budget.d.ts.map +1 -0
  82. package/dist/runtime/jobs/wake-budget.js +48 -0
  83. package/dist/runtime/jobs/wake-budget.js.map +1 -0
  84. package/dist/runtime/query/detached-calls.d.ts +65 -0
  85. package/dist/runtime/query/detached-calls.d.ts.map +1 -0
  86. package/dist/runtime/query/detached-calls.js +116 -0
  87. package/dist/runtime/query/detached-calls.js.map +1 -0
  88. package/dist/runtime/query/executor.d.ts +4 -1
  89. package/dist/runtime/query/executor.d.ts.map +1 -1
  90. package/dist/runtime/query/executor.js +90 -6
  91. package/dist/runtime/query/executor.js.map +1 -1
  92. package/dist/runtime/query/index.d.ts +20 -0
  93. package/dist/runtime/query/index.d.ts.map +1 -1
  94. package/dist/runtime/query/index.js +31 -5
  95. package/dist/runtime/query/index.js.map +1 -1
  96. package/dist/runtime/query/iteration/outstanding-work.d.ts.map +1 -1
  97. package/dist/runtime/query/iteration/outstanding-work.js +11 -3
  98. package/dist/runtime/query/iteration/outstanding-work.js.map +1 -1
  99. package/dist/runtime/query/iteration/phases/context.d.ts +7 -0
  100. package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
  101. package/dist/runtime/query/iteration/phases/context.js.map +1 -1
  102. package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
  103. package/dist/runtime/query/iteration/phases/tool-review.js +63 -1
  104. package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
  105. package/dist/runtime/query/session-storage.d.ts.map +1 -1
  106. package/dist/runtime/query/session-storage.js +8 -3
  107. package/dist/runtime/query/session-storage.js.map +1 -1
  108. package/dist/runtime/query/steering.d.ts +29 -3
  109. package/dist/runtime/query/steering.d.ts.map +1 -1
  110. package/dist/runtime/query/steering.js +58 -3
  111. package/dist/runtime/query/steering.js.map +1 -1
  112. package/dist/runtime/query/tooling.d.ts +1 -0
  113. package/dist/runtime/query/tooling.d.ts.map +1 -1
  114. package/dist/runtime/query/tooling.js +1 -0
  115. package/dist/runtime/query/tooling.js.map +1 -1
  116. package/dist/runtime/system-events.d.ts +5 -0
  117. package/dist/runtime/system-events.d.ts.map +1 -1
  118. package/dist/runtime/system-events.js +7 -1
  119. package/dist/runtime/system-events.js.map +1 -1
  120. package/dist/sandbox/denial.d.ts +11 -0
  121. package/dist/sandbox/denial.d.ts.map +1 -0
  122. package/dist/sandbox/denial.js +61 -0
  123. package/dist/sandbox/denial.js.map +1 -0
  124. package/dist/sandbox/file-walk-program.d.ts.map +1 -1
  125. package/dist/sandbox/file-walk-program.js +31 -13
  126. package/dist/sandbox/file-walk-program.js.map +1 -1
  127. package/dist/sandbox/file-walk.d.ts +2 -0
  128. package/dist/sandbox/file-walk.d.ts.map +1 -1
  129. package/dist/sandbox/file-walk.js +64 -13
  130. package/dist/sandbox/file-walk.js.map +1 -1
  131. package/dist/sandbox/ignore-matcher.d.ts +30 -0
  132. package/dist/sandbox/ignore-matcher.d.ts.map +1 -0
  133. package/dist/sandbox/ignore-matcher.js +11 -0
  134. package/dist/sandbox/ignore-matcher.js.map +1 -0
  135. package/dist/sandbox/ignore-source.d.ts +26 -0
  136. package/dist/sandbox/ignore-source.d.ts.map +1 -0
  137. package/dist/sandbox/ignore-source.js +221 -0
  138. package/dist/sandbox/ignore-source.js.map +1 -0
  139. package/dist/sandbox/provider/local.d.ts.map +1 -1
  140. package/dist/sandbox/provider/local.js +11 -1
  141. package/dist/sandbox/provider/local.js.map +1 -1
  142. package/dist/scheduler/completion-inbox.d.ts.map +1 -1
  143. package/dist/scheduler/completion-inbox.js +1 -0
  144. package/dist/scheduler/completion-inbox.js.map +1 -1
  145. package/dist/session/log-hash.d.ts.map +1 -1
  146. package/dist/session/log-hash.js +18 -1
  147. package/dist/session/log-hash.js.map +1 -1
  148. package/dist/store/session-index/sqlite.d.ts.map +1 -1
  149. package/dist/store/session-index/sqlite.js +169 -16
  150. package/dist/store/session-index/sqlite.js.map +1 -1
  151. package/dist/tools/builtins/bash.d.ts.map +1 -1
  152. package/dist/tools/builtins/bash.js +10 -4
  153. package/dist/tools/builtins/bash.js.map +1 -1
  154. package/dist/tools/builtins/computer-use.d.ts +35 -0
  155. package/dist/tools/builtins/computer-use.d.ts.map +1 -1
  156. package/dist/tools/builtins/computer-use.js +512 -67
  157. package/dist/tools/builtins/computer-use.js.map +1 -1
  158. package/dist/tools/builtins/glob.d.ts +1 -0
  159. package/dist/tools/builtins/glob.d.ts.map +1 -1
  160. package/dist/tools/builtins/glob.js +65 -28
  161. package/dist/tools/builtins/glob.js.map +1 -1
  162. package/dist/tools/builtins/grep.d.ts +2 -0
  163. package/dist/tools/builtins/grep.d.ts.map +1 -1
  164. package/dist/tools/builtins/grep.js +94 -40
  165. package/dist/tools/builtins/grep.js.map +1 -1
  166. package/dist/tools/builtins/job.d.ts +1 -1
  167. package/dist/tools/builtins/job.d.ts.map +1 -1
  168. package/dist/tools/builtins/job.js +34 -7
  169. package/dist/tools/builtins/job.js.map +1 -1
  170. package/dist/tools/builtins/search-support.d.ts +44 -0
  171. package/dist/tools/builtins/search-support.d.ts.map +1 -0
  172. package/dist/tools/builtins/search-support.js +89 -0
  173. package/dist/tools/builtins/search-support.js.map +1 -0
  174. package/dist/tools/builtins/wait-for-job-bounds.d.ts +2 -0
  175. package/dist/tools/builtins/wait-for-job-bounds.d.ts.map +1 -1
  176. package/dist/tools/builtins/wait-for-job-bounds.js +3 -1
  177. package/dist/tools/builtins/wait-for-job-bounds.js.map +1 -1
  178. package/dist/tools/builtins/wait-for-job.d.ts +1 -1
  179. package/dist/tools/builtins/wait-for-job.d.ts.map +1 -1
  180. package/dist/tools/builtins/wait-for-job.js +13 -2
  181. package/dist/tools/builtins/wait-for-job.js.map +1 -1
  182. package/dist/tools/command-shell.d.ts.map +1 -1
  183. package/dist/tools/command-shell.js +14 -5
  184. package/dist/tools/command-shell.js.map +1 -1
  185. package/dist/types/computer-use/index.d.ts +129 -13
  186. package/dist/types/computer-use/index.d.ts.map +1 -1
  187. package/dist/types/computer-use/index.js.map +1 -1
  188. package/dist/types/plugin/index.d.ts +24 -0
  189. package/dist/types/plugin/index.d.ts.map +1 -1
  190. package/dist/types/plugin/index.js +4 -0
  191. package/dist/types/plugin/index.js.map +1 -1
  192. package/dist/types/sandbox/index.d.ts +38 -1
  193. package/dist/types/sandbox/index.d.ts.map +1 -1
  194. package/dist/types/sandbox/index.js.map +1 -1
  195. package/dist/types/session/records.d.ts +264 -0
  196. package/dist/types/session/records.d.ts.map +1 -1
  197. package/dist/types/session/records.js +32 -1
  198. package/dist/types/session/records.js.map +1 -1
  199. package/dist/types/tool/index.d.ts +30 -0
  200. package/dist/types/tool/index.d.ts.map +1 -1
  201. package/dist/types/tool/index.js.map +1 -1
  202. package/dist/types/tool/presentation.d.ts +8 -0
  203. package/dist/types/tool/presentation.d.ts.map +1 -1
  204. package/package.json +1 -1
  205. package/src/compaction/context-window.ts +163 -2
  206. package/src/execution/workflow/index.ts +14 -1
  207. package/src/execution/workflow/journal.ts +161 -11
  208. package/src/execution/workflow/prelude.ts +51 -8
  209. package/src/execution/workflow/resume.ts +313 -0
  210. package/src/execution/workflow/runtime.ts +127 -9
  211. package/src/execution/workflow/types.ts +109 -1
  212. package/src/index.ts +17 -0
  213. package/src/plugin/compat.ts +173 -0
  214. package/src/plugin/loader.ts +7 -1
  215. package/src/pricing/index.ts +2 -1
  216. package/src/process/kill-tree.ts +106 -4
  217. package/src/prompt/coding-agent-doctrine.ts +9 -1
  218. package/src/provider/model-window-suffix.ts +84 -0
  219. package/src/public-runtime.ts +21 -2
  220. package/src/public-types.ts +1 -0
  221. package/src/registry/command/kernel-commands.ts +1 -32
  222. package/src/runtime/jobs/awaited-jobs.ts +11 -0
  223. package/src/runtime/jobs/host-exit-finalizer.ts +67 -0
  224. package/src/runtime/jobs/liveness.ts +144 -0
  225. package/src/runtime/jobs/registry.ts +114 -2
  226. package/src/runtime/jobs/wake-budget.ts +55 -0
  227. package/src/runtime/query/detached-calls.ts +135 -0
  228. package/src/runtime/query/executor.ts +92 -10
  229. package/src/runtime/query/index.ts +50 -7
  230. package/src/runtime/query/iteration/outstanding-work.ts +12 -3
  231. package/src/runtime/query/iteration/phases/context.ts +7 -0
  232. package/src/runtime/query/iteration/phases/tool-review.ts +73 -8
  233. package/src/runtime/query/session-storage.ts +8 -3
  234. package/src/runtime/query/steering.ts +77 -6
  235. package/src/runtime/query/tooling.ts +2 -0
  236. package/src/runtime/system-events.ts +12 -1
  237. package/src/sandbox/denial.ts +79 -0
  238. package/src/sandbox/file-walk-program.ts +32 -13
  239. package/src/sandbox/file-walk.ts +75 -12
  240. package/src/sandbox/ignore-matcher.ts +46 -0
  241. package/src/sandbox/ignore-source.ts +220 -0
  242. package/src/sandbox/provider/local.ts +11 -1
  243. package/src/scheduler/completion-inbox.ts +1 -0
  244. package/src/session/log-hash.ts +21 -0
  245. package/src/store/session-index/sqlite.ts +171 -16
  246. package/src/tools/builtins/bash.ts +11 -4
  247. package/src/tools/builtins/computer-use.ts +593 -68
  248. package/src/tools/builtins/glob.ts +79 -32
  249. package/src/tools/builtins/grep.ts +107 -43
  250. package/src/tools/builtins/job.ts +42 -12
  251. package/src/tools/builtins/search-support.ts +107 -0
  252. package/src/tools/builtins/wait-for-job-bounds.ts +5 -1
  253. package/src/tools/builtins/wait-for-job.ts +14 -2
  254. package/src/tools/command-shell.ts +13 -5
  255. package/src/types/computer-use/index.ts +157 -15
  256. package/src/types/plugin/index.ts +8 -0
  257. package/src/types/sandbox/index.ts +40 -1
  258. package/src/types/session/records.ts +36 -2
  259. package/src/types/tool/index.ts +30 -0
  260. package/src/types/tool/presentation.ts +8 -0
@@ -1,8 +1,19 @@
1
1
  import { createHash } from 'node:crypto'
2
- import { appendFile, mkdir, writeFile } from 'node:fs/promises'
3
- import { join } from 'node:path'
2
+ import { mkdir, open, readFile, stat, writeFile } from 'node:fs/promises'
3
+ import { basename, dirname, join, resolve } from 'node:path'
4
4
 
5
- import type { WorkflowJournal, WorkflowJournalRecord } from './types.js'
5
+ import {
6
+ formatWorkflowJournalLine,
7
+ readWorkflowJournalChecked,
8
+ workflowJournalLineHash,
9
+ workflowJournalSeed,
10
+ } from './resume.js'
11
+ import type {
12
+ CheckedWorkflowJournal,
13
+ WorkflowJournal,
14
+ WorkflowJournalReader,
15
+ WorkflowJournalRecord,
16
+ } from './types.js'
6
17
 
7
18
  export function sha256(text: string): string {
8
19
  return createHash('sha256').update(text).digest('hex')
@@ -17,30 +28,169 @@ export function preview(text: string, max = 500): string {
17
28
  return `${text.slice(0, end)}…`
18
29
  }
19
30
 
31
+ /** The chain state of one journal file, shared by every writer in this process. */
32
+ interface ChainState {
33
+ queue: Promise<void>
34
+ loaded: boolean
35
+ seq: number
36
+ prev: string
37
+ needsNewline: boolean
38
+ finished: boolean
39
+ /** Bytes the file had after the last write this process made; a different size means someone else wrote. */
40
+ size: number
41
+ /** Appends queued or running; the state is dropped once a pass's end is written and none are left. */
42
+ pending: number
43
+ }
44
+
45
+ const chains = new Map<string, ChainState>()
46
+
47
+ /** Records after which a crash must not lose the line: a saved result, the end of a pass, the start of one. */
48
+ function mustSurviveCrash(record: WorkflowJournalRecord): boolean {
49
+ return (
50
+ record.type === 'run_finished' ||
51
+ record.type === 'run_started' ||
52
+ (record.type === 'agent_outcome' && Object.hasOwn(record, 'result'))
53
+ )
54
+ }
55
+
20
56
  /**
21
57
  * A journal in a directory: `script.js` (the exact script text, mode 0600)
22
58
  * and `journal.jsonl` (one JSON record per line, append-only). The caller
23
59
  * chooses the directory, normally `<session>/runs/<runId>/workflow/`.
60
+ *
61
+ * Every line carries `seq` and `prev` (the hash of the line before it, the
62
+ * first chaining from a seed bound to the run id), so a line that was edited,
63
+ * removed or cut short is found when the journal is read back. One writer per
64
+ * file serves the whole process: a resumed pass and a late agent of the pass
65
+ * before it append through the same queue and the same chain, and nothing is
66
+ * appended after a pass's `run_finished` until the next pass starts. This
67
+ * assumes one process writes the file at a time, which the session lease
68
+ * already guarantees. A chain proves the file was not damaged or edited by
69
+ * accident; someone who can rewrite the whole file can rewrite the chain too.
24
70
  */
25
- export class FileWorkflowJournal implements WorkflowJournal {
71
+ export class FileWorkflowJournal implements WorkflowJournal, WorkflowJournalReader {
26
72
  private ready: Promise<void> | undefined
73
+ private readonly file: string
27
74
 
28
- constructor(private readonly dir: string) {}
75
+ constructor(
76
+ private readonly dir: string,
77
+ private readonly options: {
78
+ readonly runId?: string
79
+ /** A secret that signs every line; see `readWorkflowJournalChecked`. */
80
+ readonly key?: string | Uint8Array
81
+ } = {},
82
+ ) {
83
+ this.file = resolve(dir, 'journal.jsonl')
84
+ }
29
85
 
30
86
  private ensure(): Promise<void> {
31
87
  this.ready ??= mkdir(this.dir, { recursive: true, mode: 0o700 }).then(() => undefined)
32
88
  return this.ready
33
89
  }
34
90
 
35
- async writeScript(source: string): Promise<void> {
36
- await this.ensure()
37
- await writeFile(join(this.dir, 'script.js'), source, { mode: 0o600 })
91
+ /** The run id of the file's first line, learned from the file or from the first `run_started` written. */
92
+ private learned: string | undefined
93
+
94
+ private runId(record?: WorkflowJournalRecord, text?: string): string {
95
+ if (this.options.runId !== undefined) return this.options.runId
96
+ if (record?.type === 'run_started') this.learned = record.runId
97
+ if (this.learned !== undefined) return this.learned
98
+ const first = text?.slice(0, text.indexOf('\n') < 0 ? undefined : text.indexOf('\n'))
99
+ if (first) {
100
+ try {
101
+ const parsed = JSON.parse(first) as { type?: unknown; runId?: unknown }
102
+ if (parsed.type === 'run_started' && typeof parsed.runId === 'string') {
103
+ this.learned = parsed.runId
104
+ return parsed.runId
105
+ }
106
+ } catch {
107
+ // Not a record: fall back to the folder name below.
108
+ }
109
+ }
110
+ return basename(dirname(resolve(this.dir)))
38
111
  }
39
112
 
40
- async append(record: WorkflowJournalRecord): Promise<void> {
113
+ async writeScript(source: string, _sha256?: string, pass = 1): Promise<void> {
41
114
  await this.ensure()
42
- await appendFile(join(this.dir, 'journal.jsonl'), `${JSON.stringify(record)}\n`, {
43
- mode: 0o600,
115
+ const name = pass <= 1 ? 'script.js' : `script.${pass}.js`
116
+ await writeFile(join(this.dir, name), source, { mode: 0o600 })
117
+ }
118
+
119
+ async read(): Promise<CheckedWorkflowJournal> {
120
+ let text: string
121
+ try {
122
+ text = await readFile(this.file, 'utf8')
123
+ } catch (error) {
124
+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
125
+ text = ''
126
+ }
127
+ return readWorkflowJournalChecked(text, this.runId(undefined, text), {
128
+ ...(this.options.key !== undefined ? { key: this.options.key } : {}),
129
+ })
130
+ }
131
+
132
+ append(record: WorkflowJournalRecord): Promise<void> {
133
+ let state = chains.get(this.file)
134
+ if (state === undefined) {
135
+ state = {
136
+ queue: Promise.resolve(),
137
+ loaded: false,
138
+ seq: 0,
139
+ prev: '',
140
+ needsNewline: false,
141
+ finished: false,
142
+ size: 0,
143
+ pending: 0,
144
+ }
145
+ chains.set(this.file, state)
146
+ }
147
+ const chain = state
148
+ chain.pending++
149
+ const run = chain.queue.then(async () => {
150
+ await this.ensure()
151
+ const onDisk = await stat(this.file).then(
152
+ (info) => info.size,
153
+ () => 0,
154
+ )
155
+ if (!chain.loaded || onDisk !== chain.size) {
156
+ const checked = await this.read()
157
+ chain.seq = checked.lastSeq
158
+ chain.prev = checked.lastHash
159
+ chain.needsNewline = !checked.endsWithNewline
160
+ chain.finished = checked.records.at(-1)?.type === 'run_finished'
161
+ chain.loaded = true
162
+ chain.size = onDisk
163
+ }
164
+ if (record.type === 'run_started') chain.finished = false
165
+ // Nothing belongs after the pass's end; a late agent of that pass is dropped here.
166
+ else if (chain.finished) return
167
+ if (chain.seq === 0) chain.prev = workflowJournalSeed(this.runId(record))
168
+ const seq = chain.seq + 1
169
+ const line = formatWorkflowJournalLine(record, seq, chain.prev, this.options.key)
170
+ const handle = await open(this.file, 'a', 0o600)
171
+ try {
172
+ const text = `${chain.needsNewline ? '\n' : ''}${line}\n`
173
+ await handle.appendFile(text)
174
+ chain.size += Buffer.byteLength(text)
175
+ if (mustSurviveCrash(record)) await handle.datasync()
176
+ } finally {
177
+ await handle.close()
178
+ }
179
+ chain.needsNewline = false
180
+ chain.seq = seq
181
+ chain.prev = workflowJournalLineHash(line)
182
+ if (record.type === 'run_finished') chain.finished = true
44
183
  })
184
+ chain.queue = run
185
+ .catch(() => undefined)
186
+ .then(() => {
187
+ chain.pending--
188
+ // A finished run keeps no state: the next append reads the file again, and
189
+ // what it finds (a last `run_finished`) is the same thing the state held.
190
+ if (chain.pending === 0 && chain.finished && chains.get(this.file) === chain) {
191
+ chains.delete(this.file)
192
+ }
193
+ })
194
+ return run
45
195
  }
46
196
  }
@@ -28,7 +28,7 @@ const PRELUDE_SOURCE = String.raw`(function (config) {
28
28
  }
29
29
  function phase(title) {
30
30
  var t = clean(title, 120);
31
- if (!t) throw new TypeError('phase() needs a non-empty title.');
31
+ if (!t) throw misuse('phase() needs a non-empty title.');
32
32
  cursor = t;
33
33
  emit({ k: 'phase', title: t });
34
34
  }
@@ -53,13 +53,26 @@ const PRELUDE_SOURCE = String.raw`(function (config) {
53
53
  return e;
54
54
  }
55
55
  function isBudgetError(e) { return !!e && e.name === 'WorkflowBudgetExceededError'; }
56
+ function misuse(message) {
57
+ var e = new TypeError(message);
58
+ e.workflowMisuse = true;
59
+ return e;
60
+ }
61
+ function isMisuse(e) { return !!e && e.workflowMisuse === true; }
62
+ var optionNames = ['label', 'phase', 'schema', 'agentType', 'model', 'effort'];
56
63
  async function agent(prompt, opts) {
57
64
  if (typeof prompt !== 'string' || prompt.length === 0) {
58
- throw new TypeError('agent(prompt, options?): the prompt must be a non-empty string, got ' + kind(prompt));
65
+ throw misuse('agent(prompt, options?): the prompt must be a non-empty string, got ' + kind(prompt));
59
66
  }
60
67
  var o = opts == null ? {} : opts;
61
68
  if (typeof o !== 'object' || Array.isArray(o)) {
62
- throw new TypeError('agent(prompt, options?): options must be an object, got ' + kind(o));
69
+ throw misuse('agent(prompt, options?): options must be an object, got ' + kind(o));
70
+ }
71
+ var unknown = Object.keys(o).filter(function (k) { return optionNames.indexOf(k) < 0; });
72
+ if (unknown.length > 0) {
73
+ // Ignored, as it always was, but no longer silently: a saved script that
74
+ // misspells an option keeps running and the line says why it had no effect.
75
+ log('agent(prompt, options?): ignoring unknown option "' + unknown[0] + '"; the options are ' + optionNames.join(', ') + '.');
63
76
  }
64
77
  if (overBudget()) throw budgetError();
65
78
  var request = { prompt: prompt, cursor: cursor };
@@ -69,23 +82,32 @@ const PRELUDE_SOURCE = String.raw`(function (config) {
69
82
  if (o.agentType != null) request.agentType = clean(o.agentType, 120);
70
83
  if (o.model != null) request.model = clean(o.model, 120);
71
84
  if (o.effort != null) request.effort = clean(o.effort, 32);
72
- var reply = await rawCall('agent', request);
85
+ var reply;
86
+ try {
87
+ reply = await rawCall('agent', request);
88
+ } catch (e) {
89
+ // The host rejects a malformed request with an "agent(): ..." message;
90
+ // that is the script's mistake, not a failed child.
91
+ if (e && typeof e.message === 'string' && e.message.indexOf('agent(): ') === 0) throw misuse(e.message);
92
+ throw e;
93
+ }
73
94
  spent.tokensUsed = reply.t;
74
95
  return reply.v;
75
96
  }
76
97
  async function parallel(thunks) {
77
98
  if (!Array.isArray(thunks)) {
78
- throw new TypeError('parallel(thunks): the argument must be an array of functions, got ' + kind(thunks));
99
+ throw misuse('parallel(thunks): the argument must be an array of functions, got ' + kind(thunks));
79
100
  }
80
101
  for (var p = 0; p < thunks.length; p++) {
81
102
  if (typeof thunks[p] !== 'function') {
82
- throw new TypeError('parallel(thunks): item ' + (p + 1) + ' must be a function (wrap the work as () => agent(...)), got ' + kind(thunks[p]));
103
+ throw misuse('parallel(thunks): item ' + (p + 1) + ' must be a function (wrap the work as () => agent(...)), got ' + kind(thunks[p]));
83
104
  }
84
105
  }
85
106
  var dropped = 0;
86
107
  var results = await Promise.all(thunks.map(function (t, n) {
87
108
  return Promise.resolve().then(t).catch(function (e) {
88
109
  if (isBudgetError(e)) { dropped++; return null; }
110
+ if (isMisuse(e)) throw e;
89
111
  log('Step ' + (n + 1) + ' stopped: ' + clean(e && e.message ? e.message : e, 300));
90
112
  return null;
91
113
  });
@@ -95,12 +117,12 @@ const PRELUDE_SOURCE = String.raw`(function (config) {
95
117
  }
96
118
  async function pipeline(items) {
97
119
  if (!Array.isArray(items)) {
98
- throw new TypeError('pipeline(items, ...stages): the first argument must be an array, got ' + kind(items));
120
+ throw misuse('pipeline(items, ...stages): the first argument must be an array, got ' + kind(items));
99
121
  }
100
122
  var stages = Array.prototype.slice.call(arguments, 1);
101
123
  for (var s = 0; s < stages.length; s++) {
102
124
  if (typeof stages[s] !== 'function') {
103
- throw new TypeError('pipeline(items, ...stages): stage ' + (s + 1) + ' must be a function, got ' + kind(stages[s]));
125
+ throw misuse('pipeline(items, ...stages): stage ' + (s + 1) + ' must be a function, got ' + kind(stages[s]));
104
126
  }
105
127
  }
106
128
  if (items.length > 0 && overBudget()) throw budgetError();
@@ -112,6 +134,7 @@ const PRELUDE_SOURCE = String.raw`(function (config) {
112
134
  value = await stages[i](value, item, index);
113
135
  } catch (e) {
114
136
  if (isBudgetError(e)) { dropped++; return null; }
137
+ if (isMisuse(e)) throw e;
115
138
  log('Item ' + (index + 1) + ' stopped at step ' + (i + 1) + ': ' + clean(e && e.message ? e.message : e, 300));
116
139
  return null;
117
140
  }
@@ -141,6 +164,24 @@ const PRELUDE_SOURCE = String.raw`(function (config) {
141
164
  install('log', log);
142
165
  install('budget', budget);
143
166
  install('args', config.args);
167
+ if (config.deterministic) {
168
+ var RealDate = Date;
169
+ var clockMessage = 'Date.now() / new Date() are unavailable in workflow scripts (breaks resume). Stamp results after the workflow returns, or pass timestamps through args.';
170
+ var randomMessage = 'Math.random() is unavailable in workflow scripts (breaks resume). For N independent samples, include the index in the agent label or prompt.';
171
+ var ShimDate = function Date(a, b, c, d, e, f, g) {
172
+ if (!new.target || arguments.length === 0) throw new Error(clockMessage);
173
+ return Reflect.construct(RealDate, Array.prototype.slice.call(arguments), new.target === ShimDate ? RealDate : new.target);
174
+ };
175
+ Object.defineProperty(ShimDate, 'prototype', { value: RealDate.prototype, writable: false, configurable: false });
176
+ Object.defineProperty(ShimDate, 'now', { value: function now() { throw new Error(clockMessage); }, writable: false, configurable: false });
177
+ Object.defineProperty(ShimDate, 'parse', { value: RealDate.parse, writable: false, configurable: false });
178
+ Object.defineProperty(ShimDate, 'UTC', { value: RealDate.UTC, writable: false, configurable: false });
179
+ Object.defineProperty(RealDate.prototype, 'constructor', { value: ShimDate, writable: false, configurable: false });
180
+ Object.defineProperty(globalThis, 'Date', { value: ShimDate, writable: false, configurable: false, enumerable: false });
181
+ Object.defineProperty(Math, 'random', { value: function random() { throw new Error(randomMessage); }, writable: false, configurable: false });
182
+ try { delete globalThis.eval; } catch (ignored) {}
183
+ Object.freeze(RealDate);
184
+ }
144
185
  })`
145
186
 
146
187
  /**
@@ -151,6 +192,7 @@ const PRELUDE_SOURCE = String.raw`(function (config) {
151
192
  export function preludeLineCount(config: {
152
193
  args: unknown
153
194
  tokenLimit: number | null
195
+ deterministic?: boolean
154
196
  }): number {
155
197
  return buildPrelude(config).split('\n').length
156
198
  }
@@ -159,6 +201,7 @@ export function preludeLineCount(config: {
159
201
  export function buildPrelude(config: {
160
202
  args: unknown
161
203
  tokenLimit: number | null
204
+ deterministic?: boolean
162
205
  }): string {
163
206
  const literal = JSON.stringify(config)
164
207
  .split(String.fromCharCode(0x2028))
@@ -0,0 +1,313 @@
1
+ import { createHash, createHmac, timingSafeEqual } from 'node:crypto'
2
+
3
+ import type {
4
+ ChainedWorkflowJournalRecord,
5
+ CheckedWorkflowJournal,
6
+ WorkflowFinishStatus,
7
+ WorkflowJournalRecord,
8
+ WorkflowResumeState,
9
+ } from './types.js'
10
+
11
+ /** The key version. A change to how keys are made bumps it, so old keys simply never match. */
12
+ const KEY_VERSION = 'k1'
13
+
14
+ function sha256Hex(text: string): string {
15
+ return createHash('sha256').update(text).digest('hex')
16
+ }
17
+
18
+ /** JSON with every object's keys sorted and `__proto__` dropped, so equal values give equal text. */
19
+ function canonicalJson(value: unknown): string {
20
+ if (Array.isArray(value)) return `[${value.map((item) => canonicalJson(item)).join(',')}]`
21
+ if (typeof value === 'object' && value !== null) {
22
+ const record = value as Record<string, unknown>
23
+ const keys = Object.keys(record)
24
+ .filter((key) => key !== '__proto__' && record[key] !== undefined)
25
+ .sort()
26
+ return `{${keys.map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`).join(',')}}`
27
+ }
28
+ return JSON.stringify(value) ?? 'null'
29
+ }
30
+
31
+ /** What of an `agent()` call decides its result. `label`, `phase` and progress callbacks do not. */
32
+ export interface WorkflowCallIdentity {
33
+ readonly schema?: unknown
34
+ readonly model?: string
35
+ readonly effort?: string
36
+ readonly agentType?: string
37
+ }
38
+
39
+ /**
40
+ * The key a call is saved and looked up under: the prompt, the options that
41
+ * change what the agent does (schema, model, effort, agent type) and how many
42
+ * identical calls came before it in the same pass. Each part is length-prefixed
43
+ * so one cannot be read as part of another.
44
+ */
45
+ export function workflowCallKey(
46
+ prompt: string,
47
+ call: WorkflowCallIdentity,
48
+ ordinal: number,
49
+ ): string {
50
+ return `${KEY_VERSION}:${sha256Hex(`${workflowCallContent(prompt, call)}${ordinal}`)}`
51
+ }
52
+
53
+ /**
54
+ * What makes two calls the same call: the prompt and the options that change
55
+ * the agent, in a form where key order and `undefined` do not matter. The
56
+ * runtime counts repeats of this text to give identical calls distinct slots,
57
+ * and the key hashes the same text, so the two can never disagree.
58
+ */
59
+ export function workflowCallContent(prompt: string, call: WorkflowCallIdentity): string {
60
+ const options = canonicalJson({
61
+ schema: call.schema,
62
+ model: call.model,
63
+ effort: call.effort,
64
+ agentType: call.agentType,
65
+ })
66
+ return `${prompt.length}:${prompt}${options.length}:${options}`
67
+ }
68
+
69
+ /** The hash the first line of a run's journal chains from. It binds the file to its run id. */
70
+ export function workflowJournalSeed(runId: string): string {
71
+ return sha256Hex(`namzu-workflow-journal\0${runId}`)
72
+ }
73
+
74
+ /** The hash a line contributes to the next one: its exact bytes, newline included. */
75
+ export function workflowJournalLineHash(line: string): string {
76
+ return sha256Hex(`${line}\n`)
77
+ }
78
+
79
+ /** The line `record` is written as. With a key, a `mac` over the rest of the line is added last. */
80
+ export function formatWorkflowJournalLine(
81
+ record: WorkflowJournalRecord,
82
+ seq: number,
83
+ prev: string,
84
+ key?: string | Uint8Array,
85
+ ): string {
86
+ const body = { seq, prev, ...record }
87
+ if (key === undefined) return JSON.stringify(body)
88
+ const mac = createHmac('sha256', key).update(JSON.stringify(body)).digest('hex')
89
+ return JSON.stringify({ ...body, mac })
90
+ }
91
+
92
+ function macMatches(record: Record<string, unknown>, key: string | Uint8Array): boolean {
93
+ const { mac, ...rest } = record
94
+ if (typeof mac !== 'string') return false
95
+ const expected = createHmac('sha256', key).update(JSON.stringify(rest)).digest()
96
+ const actual = Buffer.from(mac, 'hex')
97
+ return actual.length === expected.length && timingSafeEqual(actual, expected)
98
+ }
99
+
100
+ function parseLine(line: string): Record<string, unknown> | undefined {
101
+ try {
102
+ const value: unknown = JSON.parse(line)
103
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
104
+ ? (value as Record<string, unknown>)
105
+ : undefined
106
+ } catch {
107
+ return undefined
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Reads a journal's text and keeps only the records that can be trusted.
113
+ *
114
+ * Every line carries `seq` (counting trusted lines from 1) and `prev` (the hash
115
+ * of the line before; the first chains from a seed bound to the run id). A
116
+ * line that does not chain, a line that does not parse, and anything after
117
+ * one is dropped until a `run_started` of a resumed pass picks the chain up
118
+ * again at the last trusted line. A last line without its newline that is not
119
+ * whole JSON is a torn write and is ignored; one that is whole JSON is read
120
+ * like any other line. The file is never rewritten.
121
+ *
122
+ * The chain alone cannot notice an edit to the final line (nothing chains from
123
+ * it yet). With `key`, every line must also carry a `mac` made with that
124
+ * secret, which closes that gap against anyone who does not hold the key.
125
+ */
126
+ export function readWorkflowJournalChecked(
127
+ text: string,
128
+ runId: string,
129
+ options: { readonly key?: string | Uint8Array } = {},
130
+ ): CheckedWorkflowJournal {
131
+ const endsWithNewline = text === '' || text.endsWith('\n')
132
+ const pieces = text.split('\n')
133
+ const fragment = endsWithNewline ? undefined : pieces.pop()
134
+ if (endsWithNewline) pieces.pop()
135
+ // A last line without its newline that is whole JSON was only missing the newline: it is
136
+ // judged like any other line (the writer adds the newline before the next record, so the
137
+ // hash it contributes is the one of the line with its newline). Anything else is a torn write.
138
+ const wholeFragment =
139
+ fragment !== undefined && fragment !== '' && parseLine(fragment) !== undefined
140
+ if (wholeFragment) pieces.push(fragment as string)
141
+ const tornTail = fragment !== undefined && fragment !== '' && !wholeFragment
142
+
143
+ const parsed = pieces.map((line) => parseLine(line))
144
+ const firstRecord = parsed.find((record) => record !== undefined)
145
+ if (firstRecord !== undefined && typeof firstRecord.seq !== 'number') {
146
+ return {
147
+ records: [],
148
+ legacy: true,
149
+ tornTail,
150
+ lastSeq: 0,
151
+ lastHash: workflowJournalSeed(runId),
152
+ endsWithNewline,
153
+ untrusted: parsed.filter((record) => record !== undefined) as WorkflowJournalRecord[],
154
+ }
155
+ }
156
+
157
+ const records: ChainedWorkflowJournalRecord[] = []
158
+ let lastSeq = 0
159
+ let lastHash = workflowJournalSeed(runId)
160
+ let gapStart: number | undefined
161
+ let gapLines = 0
162
+ let gapParsed = false
163
+ let brokenAt: number | undefined
164
+ const closeGap = (): void => {
165
+ if (gapStart !== undefined && (gapLines > 1 || gapParsed)) brokenAt ??= gapStart
166
+ gapStart = undefined
167
+ gapLines = 0
168
+ gapParsed = false
169
+ }
170
+
171
+ for (let i = 0; i < pieces.length; i++) {
172
+ const line = pieces[i] as string
173
+ const record = parsed[i]
174
+ const chains =
175
+ record !== undefined &&
176
+ record.seq === lastSeq + 1 &&
177
+ record.prev === lastHash &&
178
+ (options.key === undefined || macMatches(record, options.key))
179
+ const first = records.length === 0
180
+ const acceptable =
181
+ chains &&
182
+ (first
183
+ ? record.type === 'run_started' && record.runId === runId
184
+ : gapStart === undefined || record.type === 'run_started')
185
+ if (acceptable) {
186
+ closeGap()
187
+ records.push(record as unknown as ChainedWorkflowJournalRecord)
188
+ lastSeq = record.seq as number
189
+ lastHash = workflowJournalLineHash(line)
190
+ continue
191
+ }
192
+ if (gapStart === undefined) gapStart = i + 1
193
+ gapLines++
194
+ if (record !== undefined) gapParsed = true
195
+ }
196
+ // A gap that runs to the end is a change, except a single line that does not parse
197
+ // (what a torn write leaves once the next pass closed it with a newline).
198
+ closeGap()
199
+
200
+ return {
201
+ records,
202
+ legacy: false,
203
+ tornTail,
204
+ ...(brokenAt !== undefined ? { brokenAt } : {}),
205
+ lastSeq,
206
+ lastHash,
207
+ endsWithNewline,
208
+ untrusted: [],
209
+ }
210
+ }
211
+
212
+ function hasResult(record: { readonly result?: unknown }): boolean {
213
+ return Object.hasOwn(record, 'result') && record.result !== undefined && record.result !== null
214
+ }
215
+
216
+ /**
217
+ * Folds a checked journal into what a resumed pass needs.
218
+ *
219
+ * The last saved result for a key wins. An agent that was asked for again
220
+ * after a result was saved gives that result up until it finishes again. A
221
+ * failed or skipped outcome removes the result and marks the key failed; an
222
+ * outcome the run recorded as interrupted (stopped, not failed) leaves the key
223
+ * as seen but unfinished. Lines marked `cached` repeat an earlier result and
224
+ * are skipped, and so is any agent record from a pass older than the latest
225
+ * `run_started`, which a slow agent of an earlier pass can still write.
226
+ */
227
+ export function loadWorkflowResume(checked: CheckedWorkflowJournal): WorkflowResumeState {
228
+ const results = new Map<
229
+ string,
230
+ { value: unknown; pass: number; index: number; childSessionId?: string }
231
+ >()
232
+ const seen = new Set<string>()
233
+ const failed = new Set<string>()
234
+ const demoted = new Set<string>()
235
+ const problems: string[] = []
236
+ const keysByIndex = new Map<string, string>()
237
+ let currentPass = 0
238
+ let scriptSha256: string | undefined
239
+ let previousFinish: WorkflowFinishStatus | 'unfinished' = 'unfinished'
240
+
241
+ for (const record of checked.records) {
242
+ if (record.type === 'run_started') {
243
+ currentPass = record.resume?.pass ?? 1
244
+ scriptSha256 = record.scriptSha256
245
+ previousFinish = 'unfinished'
246
+ keysByIndex.clear()
247
+ continue
248
+ }
249
+ if (record.type === 'run_finished') {
250
+ previousFinish = record.status
251
+ continue
252
+ }
253
+ if (record.type !== 'agent_request' && record.type !== 'agent_outcome') continue
254
+ const pass = record.pass ?? 1
255
+ if (pass < currentPass || record.cached === true) continue
256
+ if (record.type === 'agent_request') {
257
+ if (record.key === undefined) continue
258
+ keysByIndex.set(`${pass}:${record.index}`, record.key)
259
+ seen.add(record.key)
260
+ if (results.delete(record.key)) demoted.add(record.key)
261
+ continue
262
+ }
263
+ const key = record.key ?? keysByIndex.get(`${pass}:${record.index}`)
264
+ if (key === undefined) continue
265
+ seen.add(key)
266
+ if (record.state === 'done' && hasResult(record)) {
267
+ results.set(key, {
268
+ value: record.result,
269
+ pass,
270
+ index: record.index,
271
+ ...(record.childSessionId !== undefined ? { childSessionId: record.childSessionId } : {}),
272
+ })
273
+ failed.delete(key)
274
+ demoted.delete(key)
275
+ } else if (record.state === 'done' && record.unsaved === true) {
276
+ results.delete(key)
277
+ demoted.add(key)
278
+ } else if (record.interrupted === true) {
279
+ // Stopped, not failed: the agent runs again and the cache stays open.
280
+ results.delete(key)
281
+ } else {
282
+ results.delete(key)
283
+ demoted.delete(key)
284
+ failed.add(key)
285
+ }
286
+ }
287
+
288
+ if (checked.legacy) {
289
+ problems.push(
290
+ 'This run was saved before resuming was possible, so its agents cannot be reused.',
291
+ )
292
+ }
293
+ if (checked.brokenAt !== undefined) {
294
+ problems.push(
295
+ `The journal was changed or cut short at line ${checked.brokenAt}. Results after that point are not trusted and those agents will run again.`,
296
+ )
297
+ }
298
+ const started = checked.records.some((record) => record.type === 'run_started')
299
+ if (!checked.legacy && !started) problems.push('The journal has no usable record of the run.')
300
+
301
+ return {
302
+ pass: currentPass + 1,
303
+ results,
304
+ seen,
305
+ failed,
306
+ demoted,
307
+ ...(scriptSha256 !== undefined ? { scriptSha256 } : {}),
308
+ previousFinish,
309
+ problems,
310
+ resumable: !checked.legacy && started,
311
+ ...(checked.brokenAt !== undefined ? { brokenAt: checked.brokenAt } : {}),
312
+ }
313
+ }