arco-workbench 0.1.6 → 0.1.8

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 (236) hide show
  1. package/CONTRIBUTING.md +52 -12
  2. package/README.md +20 -13
  3. package/client/dist/assets/{PhosphorBackdrop-BR-TgB5x.js → PhosphorBackdrop-D7jdAV_G.js} +1 -1
  4. package/client/dist/assets/{ThemedBackgroundShaderImpl-DgcFj6JN.js → ThemedBackgroundShaderImpl-DrDm60h8.js} +1 -1
  5. package/client/dist/assets/{XtermView-CflVBQgC.js → XtermView-BGFujb1e.js} +1 -1
  6. package/client/dist/assets/{galleryEditor-BB-emfmi.js → galleryEditor-D951A2sE.js} +1 -1
  7. package/client/dist/assets/{index-B_EqHDz3.js → index-CsVy3eZm.js} +248 -248
  8. package/client/dist/index.html +1 -1
  9. package/client/src/buildCheck.ts +3 -1
  10. package/client/src/components/AppUpdateNotice.tsx +2 -1
  11. package/client/src/components/ClusterRightPanel/AutoPickupControl.tsx +24 -12
  12. package/client/src/components/ClusterRightPanel/ReplaceMemberDialog.tsx +65 -15
  13. package/client/src/components/ClusterRightPanel/SkillsControl.tsx +107 -0
  14. package/client/src/components/ClusterRightPanel/index.tsx +6 -1
  15. package/client/src/components/ClusterTaskBoardTab.tsx +9 -22
  16. package/client/src/components/HeaderVersionBox.tsx +7 -4
  17. package/client/src/components/NeedsYouBadge.tsx +3 -3
  18. package/client/src/components/ProviderReleasesModal.tsx +8 -4
  19. package/client/src/components/ProvidersPopover.tsx +28 -23
  20. package/client/src/components/SettingsPanel.tsx +2 -2
  21. package/client/src/components/SkillsSettings.tsx +5 -0
  22. package/client/src/components/TabBar.tsx +91 -9
  23. package/client/src/components/TerminalInput.tsx +12 -10
  24. package/client/src/components/TranscriptSendDialog.tsx +12 -8
  25. package/client/src/components/TranscriptWorkerDialog.tsx +119 -0
  26. package/client/src/components/UpdateAction.tsx +18 -4
  27. package/client/src/components/UpdateResultNotice.tsx +3 -1
  28. package/client/src/components/clusterNeedsYou.ts +11 -18
  29. package/client/src/help/content/cluster.tsx +54 -22
  30. package/client/src/help/content/composer.tsx +22 -0
  31. package/client/src/help/content/header.tsx +19 -1
  32. package/client/src/help/content/misc.tsx +8 -0
  33. package/client/src/help/content/rightPanel.tsx +1 -1
  34. package/client/src/help/content/settings.tsx +19 -5
  35. package/client/src/help/content/tabbar.tsx +5 -0
  36. package/client/src/help/manualSections/actions.tsx +31 -0
  37. package/client/src/help/manualSections/clusters.tsx +58 -21
  38. package/client/src/help/manualSections/configuration.tsx +38 -4
  39. package/client/src/help/manualSections/maintenance.tsx +30 -0
  40. package/client/src/help/manualSections/plans.tsx +5 -1
  41. package/client/src/help/manualSections/providers.tsx +32 -8
  42. package/client/src/help/manualSections/troubleshooting.tsx +2 -1
  43. package/client/src/help/manualSections/workflows.tsx +19 -0
  44. package/client/src/help/manualSections/workspace.tsx +6 -0
  45. package/client/src/stores/appUpdateStore.ts +29 -8
  46. package/client/src/utils/transcriptWorkerRequest.ts +24 -0
  47. package/client/src/utils/wsSession.ts +2 -1
  48. package/client/src/ws/handleServerMessage.ts +8 -2
  49. package/docs/ARCHITECTURE.md +1 -1
  50. package/docs/CLIENT.md +9 -8
  51. package/docs/FEATURES.md +8 -5
  52. package/docs/GETTING_STARTED.md +9 -4
  53. package/docs/GOTCHAS.md +2 -1
  54. package/docs/OPERATIONS.md +37 -4
  55. package/docs/PERSISTENCE.md +25 -3
  56. package/docs/PRIMITIVES.md +42 -0
  57. package/docs/PROTOCOL_CLUSTER.md +51 -5
  58. package/docs/PROTOCOL_PRIMITIVES.md +39 -0
  59. package/docs/PROVIDERS.md +50 -13
  60. package/docs/SETTINGS.md +27 -3
  61. package/docs/TASK_LEDGER.md +7 -15
  62. package/node_modules/@arco/shared/dist/clusterCoordination.d.ts +2 -0
  63. package/node_modules/@arco/shared/dist/clusterCoordination.js +5 -1
  64. package/node_modules/@arco/shared/dist/protocol/autoPickup.d.ts +3 -0
  65. package/node_modules/@arco/shared/dist/protocol/clientMessages.d.ts +3 -2
  66. package/node_modules/@arco/shared/dist/protocol/clusters.d.ts +13 -1
  67. package/node_modules/@arco/shared/dist/protocol/index.d.ts +1 -0
  68. package/node_modules/@arco/shared/dist/protocol/index.js +1 -0
  69. package/node_modules/@arco/shared/dist/protocol/models.d.ts +4 -3
  70. package/node_modules/@arco/shared/dist/protocol/models.js +26 -2
  71. package/node_modules/@arco/shared/dist/protocol/serverMessages.d.ts +2 -1
  72. package/node_modules/@arco/shared/dist/protocol/tasks.d.ts +4 -0
  73. package/node_modules/@arco/shared/dist/protocol/transcriptWorker.d.ts +47 -0
  74. package/node_modules/@arco/shared/dist/protocol/transcriptWorker.js +2 -0
  75. package/node_modules/@arco/shared/dist/runtimeVersions.d.ts +1 -0
  76. package/node_modules/@arco/shared/dist/runtimeVersions.js +14 -0
  77. package/node_modules/@arco/shared/package.json +1 -1
  78. package/package.json +2 -2
  79. package/scripts/packaging/arco.mjs +1 -1
  80. package/scripts/packaging/native-repair.mjs +78 -12
  81. package/scripts/packaging/update.mjs +5 -1
  82. package/server/dist/atomicFile.js +1 -2
  83. package/server/dist/capabilities.d.ts +5 -2
  84. package/server/dist/capabilities.js +59 -9
  85. package/server/dist/cliUpdateDecision.d.ts +1 -1
  86. package/server/dist/cliUpdateDecision.js +5 -3
  87. package/server/dist/clusterAutoTurn.d.ts +4 -0
  88. package/server/dist/clusterAutoTurn.js +29 -25
  89. package/server/dist/clusterBusyReminder.js +1 -1
  90. package/server/dist/clusterDirector.js +1 -1
  91. package/server/dist/clusterPeerOps.js +10 -7
  92. package/server/dist/clusterProgress.js +1 -1
  93. package/server/dist/clusterSkills.d.ts +13 -0
  94. package/server/dist/clusterSkills.js +81 -0
  95. package/server/dist/clusterSnapshots.js +2 -0
  96. package/server/dist/clusterStore.d.ts +10 -2
  97. package/server/dist/clusterStore.js +64 -1
  98. package/server/dist/diagnostics/codeSkew.js +1 -0
  99. package/server/dist/eventLog.d.ts +7 -0
  100. package/server/dist/eventLog.js +70 -13
  101. package/server/dist/historyLimits.d.ts +1 -0
  102. package/server/dist/historyLimits.js +3 -0
  103. package/server/dist/http/router.js +1 -1
  104. package/server/dist/index.js +24 -1
  105. package/server/dist/jsonlLines.d.ts +4 -0
  106. package/server/dist/jsonlLines.js +53 -0
  107. package/server/dist/management/identity.js +1 -0
  108. package/server/dist/mcp/clusterMcpTools.js +4 -4
  109. package/server/dist/project/taskLedger.d.ts +4 -0
  110. package/server/dist/project/taskLedger.js +7 -4
  111. package/server/dist/providerConversation.d.ts +1 -0
  112. package/server/dist/providerConversation.js +17 -0
  113. package/server/dist/providers/claude-native/resumeSummary.d.ts +9 -0
  114. package/server/dist/providers/claude-native/resumeSummary.js +102 -49
  115. package/server/dist/providers/claude-native/worker.js +8 -4
  116. package/server/dist/providers/claude.js +2 -1
  117. package/server/dist/providers/clusterTaskToolDefinitions.js +4 -4
  118. package/server/dist/providers/codex-appserver/catalog.js +16 -1
  119. package/server/dist/providers/codex.js +2 -1
  120. package/server/dist/providers/codexCodeMode.d.ts +1 -1
  121. package/server/dist/providers/codexCodeMode.js +4 -3
  122. package/server/dist/providers/cursor/print.js +2 -1
  123. package/server/dist/providers/cursor/worker.js +2 -1
  124. package/server/dist/providers/llamacpp.js +17 -8
  125. package/server/dist/providers/ollamaRecovery.js +6 -6
  126. package/server/dist/providers/ollamaResilience.js +3 -3
  127. package/server/dist/providers/ollamaTokens.js +1 -1
  128. package/server/dist/quota.d.ts +0 -18
  129. package/server/dist/quota.js +0 -44
  130. package/server/dist/replacementHistory.d.ts +19 -0
  131. package/server/dist/replacementHistory.js +49 -0
  132. package/server/dist/server/broadcast.d.ts +2 -1
  133. package/server/dist/server/broadcast.js +1 -0
  134. package/server/dist/server/prompts.d.ts +1 -0
  135. package/server/dist/server/prompts.js +6 -1
  136. package/server/dist/sessionJsonl.d.ts +1 -1
  137. package/server/dist/sessionJsonl.js +115 -40
  138. package/server/dist/smartPickup.d.ts +2 -1
  139. package/server/dist/smartPickup.js +28 -1
  140. package/server/dist/smartPickupAdapters.js +15 -2
  141. package/server/dist/smartPickupLedger.js +6 -4
  142. package/server/dist/smartPickupObserver.d.ts +1 -0
  143. package/server/dist/smartPickupObserver.js +24 -15
  144. package/server/dist/transcriptWorker.d.ts +34 -0
  145. package/server/dist/transcriptWorker.js +201 -0
  146. package/server/dist/workflowStore.d.ts +1 -0
  147. package/server/dist/workflowStore.js +31 -0
  148. package/server/dist/ws/clusterHandlers.d.ts +2 -0
  149. package/server/dist/ws/clusterHandlers.js +64 -27
  150. package/server/dist/ws/handoffHandlers.js +5 -2
  151. package/server/dist/ws/transcriptWorkerHandlers.d.ts +25 -0
  152. package/server/dist/ws/transcriptWorkerHandlers.js +139 -0
  153. package/server/dist/ws/turnEngine.js +23 -9
  154. package/server/dist/ws/updatePrimaryConfig.js +27 -8
  155. package/server/dist/ws/workflowHandlers.d.ts +4 -1
  156. package/server/dist/ws/workflowHandlers.js +3 -1
  157. package/server/package.json +1 -1
  158. package/server/src/atomicFile.ts +1 -2
  159. package/server/src/capabilities.ts +58 -17
  160. package/server/src/cliUpdateDecision.ts +5 -4
  161. package/server/src/clusterAutoTurn.ts +26 -21
  162. package/server/src/clusterBusyReminder.ts +1 -1
  163. package/server/src/clusterDirector.ts +1 -1
  164. package/server/src/clusterPeerOps.ts +11 -8
  165. package/server/src/clusterProgress.ts +1 -1
  166. package/server/src/clusterSkills.ts +79 -0
  167. package/server/src/clusterSnapshots.ts +1 -0
  168. package/server/src/clusterStore.ts +64 -1
  169. package/server/src/diagnostics/codeSkew.ts +1 -0
  170. package/server/src/eventLog.ts +73 -13
  171. package/server/src/historyLimits.ts +2 -0
  172. package/server/src/http/router.ts +1 -1
  173. package/server/src/index.ts +23 -0
  174. package/server/src/jsonlLines.ts +52 -0
  175. package/server/src/management/identity.ts +1 -0
  176. package/server/src/mcp/clusterMcpTools.ts +4 -4
  177. package/server/src/project/taskLedger.ts +7 -5
  178. package/server/src/providerConversation.ts +15 -0
  179. package/server/src/providers/claude-native/resumeSummary.ts +98 -50
  180. package/server/src/providers/claude-native/worker.ts +8 -4
  181. package/server/src/providers/claude.ts +2 -1
  182. package/server/src/providers/clusterTaskToolDefinitions.ts +4 -4
  183. package/server/src/providers/codex-appserver/catalog.ts +14 -1
  184. package/server/src/providers/codex.ts +2 -1
  185. package/server/src/providers/codexCodeMode.ts +4 -4
  186. package/server/src/providers/cursor/print.ts +2 -1
  187. package/server/src/providers/cursor/worker.ts +2 -1
  188. package/server/src/providers/llamacpp.ts +18 -7
  189. package/server/src/providers/llamacppRecovery.ts +2 -0
  190. package/server/src/providers/ollamaRecovery.ts +6 -6
  191. package/server/src/providers/ollamaResilience.ts +3 -3
  192. package/server/src/providers/ollamaTokens.ts +1 -1
  193. package/server/src/providers/ollamaToolSubset.ts +3 -3
  194. package/server/src/quota.ts +0 -50
  195. package/server/src/replacementHistory.ts +44 -0
  196. package/server/src/server/broadcast.ts +2 -0
  197. package/server/src/server/prompts.ts +8 -1
  198. package/server/src/sessionJsonl.ts +92 -32
  199. package/server/src/smartPickup.ts +27 -3
  200. package/server/src/smartPickupAdapters.ts +17 -3
  201. package/server/src/smartPickupLedger.ts +6 -4
  202. package/server/src/smartPickupObserver.ts +20 -11
  203. package/server/src/transcriptWorker.ts +182 -0
  204. package/server/src/workflowStore.ts +21 -0
  205. package/server/src/ws/clusterHandlers.ts +46 -27
  206. package/server/src/ws/handoffHandlers.ts +5 -1
  207. package/server/src/ws/transcriptWorkerHandlers.ts +126 -0
  208. package/server/src/ws/turnEngine.ts +20 -8
  209. package/server/src/ws/updatePrimaryConfig.ts +25 -6
  210. package/server/src/ws/workflowHandlers.ts +3 -1
  211. package/shared/dist/clusterCoordination.d.ts +2 -0
  212. package/shared/dist/clusterCoordination.js +5 -1
  213. package/shared/dist/protocol/autoPickup.d.ts +3 -0
  214. package/shared/dist/protocol/clientMessages.d.ts +3 -2
  215. package/shared/dist/protocol/clusters.d.ts +13 -1
  216. package/shared/dist/protocol/index.d.ts +1 -0
  217. package/shared/dist/protocol/index.js +1 -0
  218. package/shared/dist/protocol/models.d.ts +4 -3
  219. package/shared/dist/protocol/models.js +26 -2
  220. package/shared/dist/protocol/serverMessages.d.ts +2 -1
  221. package/shared/dist/protocol/tasks.d.ts +4 -0
  222. package/shared/dist/protocol/transcriptWorker.d.ts +47 -0
  223. package/shared/dist/protocol/transcriptWorker.js +2 -0
  224. package/shared/dist/runtimeVersions.d.ts +1 -0
  225. package/shared/dist/runtimeVersions.js +14 -0
  226. package/shared/src/clusterCoordination.ts +7 -1
  227. package/shared/src/protocol/autoPickup.ts +5 -0
  228. package/shared/src/protocol/clientMessages.ts +5 -0
  229. package/shared/src/protocol/clusters.ts +16 -1
  230. package/shared/src/protocol/index.ts +1 -0
  231. package/shared/src/protocol/models.ts +31 -5
  232. package/shared/src/protocol/serverMessages.ts +2 -0
  233. package/shared/src/protocol/settings.ts +2 -3
  234. package/shared/src/protocol/tasks.ts +4 -7
  235. package/shared/src/protocol/transcriptWorker.ts +49 -0
  236. package/shared/src/runtimeVersions.ts +14 -0
@@ -1,7 +1,8 @@
1
- import { watch } from 'fs';
2
- import { readFile } from 'fs/promises';
3
- import { join, resolve } from 'path';
1
+ import { watch, statSync } from 'fs';
2
+ import { open } from 'fs/promises';
3
+ import { basename, dirname, join, resolve } from 'path';
4
4
  import { homedir } from 'os';
5
+ import { jsonlLines } from './jsonlLines.js';
5
6
  export function resolveSessionJsonlPath(cwd, sessionId) {
6
7
  // Claude's CLI encodes cwd by replacing any non-[A-Za-z0-9-] char with '-',
7
8
  // not just '/'. So `/home/operator/Projects/Sample_Project` becomes
@@ -21,30 +22,64 @@ export function resolveSubagentsWorkflowsDir(cwd, sessionId) {
21
22
  return join(homedir(), '.claude', 'projects', encoded, sessionId, 'subagents', 'workflows');
22
23
  }
23
24
  export async function loadSessionJsonl(path) {
24
- let buf;
25
+ let fh;
25
26
  try {
26
- buf = await readFile(path);
27
+ fh = await open(path, 'r');
27
28
  }
28
- catch {
29
- return { entries: [], byteLength: 0 };
29
+ catch (err) {
30
+ if (err.code === 'ENOENT')
31
+ return { entries: [], byteLength: 0 };
32
+ throw err;
30
33
  }
31
34
  const entries = [];
32
- const complete = buf.lastIndexOf(10) + 1;
33
- for (const line of buf.subarray(0, complete).toString('utf-8').split('\n')) {
34
- if (!line.trim())
35
- continue;
36
- try {
37
- const e = JSON.parse(line);
38
- if (e && typeof e.type === 'string')
39
- entries.push(e);
35
+ let complete = 0;
36
+ try {
37
+ const size = (await fh.stat()).size;
38
+ async function* chunks() {
39
+ for (let position = 0; position < size;) {
40
+ const buffer = Buffer.alloc(Math.min(64 * 1024, size - position));
41
+ const { bytesRead } = await fh.read(buffer, 0, buffer.length, position);
42
+ if (!bytesRead)
43
+ break;
44
+ const chunk = buffer.subarray(0, bytesRead);
45
+ const newline = chunk.lastIndexOf(0x0a);
46
+ if (newline >= 0)
47
+ complete = position + newline + 1;
48
+ position += bytesRead;
49
+ yield chunk;
50
+ }
40
51
  }
41
- catch { /* skip malformed lines */ }
52
+ for await (const line of jsonlLines(chunks(), path, { finalLine: false, carriageReturns: false })) {
53
+ if (!line.trim())
54
+ continue;
55
+ try {
56
+ const e = JSON.parse(line);
57
+ if (e && typeof e.type === 'string')
58
+ entries.push(e);
59
+ }
60
+ catch { /* skip malformed lines */ }
61
+ }
62
+ return { entries, byteLength: complete };
63
+ }
64
+ finally {
65
+ await fh.close();
42
66
  }
43
- return { entries, byteLength: complete };
67
+ }
68
+ async function lastCompleteOffset(fh, size) {
69
+ for (let end = size; end > 0;) {
70
+ const start = Math.max(0, end - 64 * 1024), buffer = Buffer.alloc(end - start);
71
+ const { bytesRead } = await fh.read(buffer, 0, buffer.length, start);
72
+ const newline = buffer.subarray(0, bytesRead).lastIndexOf(0x0a);
73
+ if (newline >= 0)
74
+ return start + newline + 1;
75
+ end = start;
76
+ }
77
+ return 0;
44
78
  }
45
79
  // Tail the session JSONL for new entries, emitting each one via `onEntry`.
46
80
  // `initialOffset` should be the byteLength from loadSessionJsonl so we
47
- // don't re-emit history. Returns a dispose function.
81
+ // don't re-emit history. Null anchors at the first readable complete boundary
82
+ // after initial loading failed. Returns a dispose function.
48
83
  export function watchSessionJsonl(path, initialOffset, onEntry) {
49
84
  let lastOffset = initialOffset;
50
85
  let active = true;
@@ -52,6 +87,12 @@ export function watchSessionJsonl(path, initialOffset, onEntry) {
52
87
  let restartTimer = null;
53
88
  let reading = false;
54
89
  let requested = false;
90
+ let identity;
91
+ try {
92
+ const st = statSync(path);
93
+ identity = `${st.dev}:${st.ino}`;
94
+ }
95
+ catch { /* not created yet */ }
55
96
  const readNew = async () => {
56
97
  if (!active)
57
98
  return;
@@ -63,30 +104,59 @@ export function watchSessionJsonl(path, initialOffset, onEntry) {
63
104
  while (active && requested) {
64
105
  requested = false;
65
106
  try {
66
- const buf = await readFile(path);
67
- if (!active)
68
- return;
69
- if (buf.length < lastOffset)
70
- lastOffset = 0;
71
- const end = buf.lastIndexOf(10) + 1;
72
- if (end <= lastOffset)
73
- continue;
74
- const slice = buf.subarray(lastOffset, end).toString('utf-8');
75
- lastOffset = end;
76
- for (const line of slice.split('\n')) {
107
+ const fh = await open(path, 'r');
108
+ try {
77
109
  if (!active)
78
110
  return;
79
- if (!line.trim())
80
- continue;
81
- let e;
82
- try {
83
- e = JSON.parse(line);
111
+ const st = await fh.stat();
112
+ const nextIdentity = `${st.dev}:${st.ino}`;
113
+ // After an initial-load failure, anchor to the first readable
114
+ // complete boundary. Old terminal rows must not look like new
115
+ // concurrent-session writes; an unfinished last row stays pending.
116
+ if (lastOffset === null) {
117
+ lastOffset = await lastCompleteOffset(fh, st.size);
118
+ identity = nextIdentity;
84
119
  }
85
- catch {
86
- continue;
120
+ if ((identity !== undefined && identity !== nextIdentity) || st.size < lastOffset)
121
+ lastOffset = 0;
122
+ identity = nextIdentity;
123
+ const startOffset = lastOffset;
124
+ let completeOffset = startOffset;
125
+ // Read only the appended range. An unfinished record is left at
126
+ // lastOffset and completed by the next notification, as before.
127
+ async function* chunks() {
128
+ for (let position = startOffset; active && position < st.size;) {
129
+ const buffer = Buffer.alloc(Math.min(64 * 1024, st.size - position));
130
+ const { bytesRead } = await fh.read(buffer, 0, buffer.length, position);
131
+ if (!bytesRead)
132
+ break;
133
+ const chunk = buffer.subarray(0, bytesRead);
134
+ const newline = chunk.lastIndexOf(0x0a);
135
+ if (newline >= 0)
136
+ completeOffset = position + newline + 1;
137
+ position += bytesRead;
138
+ yield chunk;
139
+ }
87
140
  }
88
- if (e && typeof e.type === 'string')
89
- onEntry(e);
141
+ for await (const line of jsonlLines(chunks(), path, { finalLine: false, carriageReturns: false })) {
142
+ if (!active)
143
+ return;
144
+ if (!line.trim())
145
+ continue;
146
+ let e;
147
+ try {
148
+ e = JSON.parse(line);
149
+ }
150
+ catch {
151
+ continue;
152
+ }
153
+ if (e && typeof e.type === 'string')
154
+ onEntry(e);
155
+ }
156
+ lastOffset = completeOffset;
157
+ }
158
+ finally {
159
+ await fh.close();
90
160
  }
91
161
  }
92
162
  catch { /* file unavailable; retry at the next notification */ }
@@ -112,7 +182,12 @@ export function watchSessionJsonl(path, initialOffset, onEntry) {
112
182
  if (!active || watcher)
113
183
  return;
114
184
  try {
115
- watcher = watch(path, { persistent: false }, () => { void readNew(); });
185
+ // Watching the directory survives replacement of the session file;
186
+ // an inode-bound watcher would keep watching the removed file.
187
+ watcher = watch(dirname(path), { persistent: false }, (_event, name) => {
188
+ if (name === null || name === basename(path))
189
+ void readNew();
190
+ });
116
191
  void readNew();
117
192
  watcher.on('error', () => {
118
193
  watcher = null;
@@ -120,7 +195,7 @@ export function watchSessionJsonl(path, initialOffset, onEntry) {
120
195
  });
121
196
  }
122
197
  catch {
123
- // File not yet created — retry after 2s (happens for brand-new sessions)
198
+ // Session directory not yet created — retry after 2s (happens for brand-new sessions)
124
199
  scheduleRestart();
125
200
  }
126
201
  };
@@ -2,9 +2,10 @@ import { type AutoPickupMode, type ClusterAutoPickupState, type SmartPickupEvalu
2
2
  import * as clusterStore from './clusterStore.js';
3
3
  type Changed = (clusterId: string) => void;
4
4
  export declare const SMART_PICKUP_SWEEP_MS: number;
5
- export type ArmProblem = 'unknown_cluster' | 'invalid_mode' | 'evaluator_required' | 'evaluator_provider_disabled' | 'evaluator_unavailable' | 'ledger_unreadable';
5
+ export type ArmProblem = 'unknown_cluster' | 'invalid_mode' | 'invalid_pause' | 'evaluator_required' | 'evaluator_provider_disabled' | 'evaluator_unavailable' | 'ledger_unreadable';
6
6
  export declare function applyAutoPickup(clusterId: string, request: {
7
7
  mode: unknown;
8
+ paused?: unknown;
8
9
  evaluator?: unknown;
9
10
  limits?: unknown;
10
11
  ruleLimits?: unknown;
@@ -4,6 +4,7 @@
4
4
  import { isSmartPickupMode, sanitizeSmartPickupEvaluator, sanitizeSmartPickupLimits, sanitizeRulePickupLimits, RULE_PICKUP_LIMIT_KEYS, } from '@arco/shared';
5
5
  import * as ruleAutoTurn from './clusterAutoTurn.js';
6
6
  import * as clusterStore from './clusterStore.js';
7
+ import { pickupSmartWaitingMail } from './smartPickupObserver.js';
7
8
  import { evaluatorAdapter } from './smartPickupAdapters.js';
8
9
  import { recordSmartPickupDecision, recoverSmartPickupLedger, smartPickupSnapshot, SmartPickupLedgerUnreadable, } from './smartPickupLedger.js';
9
10
  let onChanged = () => { };
@@ -22,6 +23,30 @@ export async function applyAutoPickup(clusterId, request, isProviderEnabled, now
22
23
  const state = clusterStore.getClusterCached(clusterId) ?? await clusterStore.readCluster(clusterId).catch(() => null);
23
24
  if (!state)
24
25
  return { ok: false, problem: 'unknown_cluster' };
26
+ if (request.paused !== undefined) {
27
+ if (typeof request.paused !== 'boolean' || mode === 'off' || mode !== clusterStore.autoPickupModeOf(state)
28
+ || request.resumeRule || request.evaluator !== undefined || request.limits !== undefined || request.ruleLimits !== undefined || request.remindBusy !== undefined)
29
+ return { ok: false, problem: 'invalid_pause' };
30
+ const session = state.smartPickup?.session;
31
+ if (isSmartPickupMode(mode) && (!session || (state.autoPickupPausedAt ?? now) >= session.expiresAt)) {
32
+ await disarm(clusterId, session ? 'session_expired' : 'session_missing', now);
33
+ return { ok: true, mode: 'off' };
34
+ }
35
+ const updated = await clusterStore.setAutoPickup(clusterId, { mode, paused: request.paused, now });
36
+ if (!updated)
37
+ return { ok: false, problem: 'invalid_pause' };
38
+ await recordSmartPickupDecision(clusterId, { at: now, action: null, reason: request.paused ? 'operator_paused' : 'operator_resumed', outcome: 'none' }).catch(() => undefined);
39
+ if (state.autoPickupPausedAt !== undefined && !request.paused) {
40
+ // Reply with the new control state promptly; evaluator requests may take
41
+ // time. Both scans use the same admission path as ordinary waiting mail.
42
+ setImmediate(() => {
43
+ void (mode === 'rule' ? ruleAutoTurn.pickupWaitingMail(clusterId) : pickupSmartWaitingMail(clusterId)).catch((error) => console.error('[arco] auto-pickup resume failed:', error));
44
+ });
45
+ }
46
+ return { ok: true, mode };
47
+ }
48
+ if (request.resumeRule && state.autoPickupPausedAt !== undefined)
49
+ return { ok: false, problem: 'invalid_pause' };
25
50
  let evaluator;
26
51
  if (isSmartPickupMode(mode)) {
27
52
  evaluator = sanitizeSmartPickupEvaluator(request.evaluator) ?? undefined;
@@ -89,6 +114,8 @@ export async function autoPickupStateFor(state, now = Date.now()) {
89
114
  const smart = state.smartPickup ?? {};
90
115
  const out = {
91
116
  mode,
117
+ paused: state.autoPickupPausedAt !== undefined,
118
+ ...(state.autoPickupPausedAt !== undefined ? { pausedAt: state.autoPickupPausedAt } : {}),
92
119
  remindBusy: state.remindBusy === true,
93
120
  limits: sanitizeSmartPickupLimits(smart.limits),
94
121
  ruleLimits: sanitizeRulePickupLimits(state.rulePickupLimits),
@@ -130,7 +157,7 @@ export async function sweepSmartPickup(now = Date.now()) {
130
157
  await disarm(clusterId, 'session_missing', now);
131
158
  continue;
132
159
  }
133
- if (now >= session.expiresAt) {
160
+ if (state.autoPickupPausedAt === undefined && now >= session.expiresAt) {
134
161
  await disarm(clusterId, 'session_expired', now);
135
162
  continue;
136
163
  }
@@ -215,6 +215,19 @@ export function claudeAdapter(opts = {}) {
215
215
  },
216
216
  };
217
217
  }
218
+ // A terminating child can still hold its cwd open on Windows. Allow up to
219
+ // 250 ms for exit, then at most 600 ms of removal retries. Cleanup must never
220
+ // change the evaluator's answer or refusal.
221
+ async function removeEvaluatorFolder(proc, cwd) {
222
+ if (proc.pid && proc.exitCode === null && proc.signalCode === null) {
223
+ await new Promise((resolve) => {
224
+ const done = () => { clearTimeout(timer); proc.off('close', done); resolve(); };
225
+ const timer = setTimeout(done, 250);
226
+ proc.once('close', done);
227
+ });
228
+ }
229
+ await rm(cwd, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 }).catch(() => undefined);
230
+ }
218
231
  export function cursorAdapter(opts = {}) {
219
232
  // `cursor-agent acp` ignores `--model`; the model is selected over ACP after
220
233
  // session/new and confirmed, and the evaluator refuses rather than answer
@@ -357,7 +370,7 @@ export function cursorAdapter(opts = {}) {
357
370
  clearTimeout(timer);
358
371
  req.signal?.removeEventListener('abort', onAbort);
359
372
  stop();
360
- await rm(cwd, { recursive: true, force: true }).catch(() => undefined);
373
+ await removeEvaluatorFolder(proc, cwd);
361
374
  }
362
375
  },
363
376
  };
@@ -504,7 +517,7 @@ export function codexAdapter(opts = {}) {
504
517
  clearTimeout(timer);
505
518
  req.signal?.removeEventListener('abort', onAbort);
506
519
  stop();
507
- await rm(cwd, { recursive: true, force: true }).catch(() => undefined);
520
+ await removeEvaluatorFolder(proc, cwd);
508
521
  }
509
522
  },
510
523
  };
@@ -10,10 +10,11 @@ import { promises as fs } from 'node:fs';
10
10
  import { join } from 'node:path';
11
11
  import { randomUUID } from 'node:crypto';
12
12
  import { atomicWriteFile } from './atomicFile.js';
13
- import { clusterDir } from './clusterStore.js';
13
+ import { clusterDir, getClusterCached } from './clusterStore.js';
14
14
  const HOUR_MS = 60 * 60 * 1000;
15
- // Entries older than this cannot affect any limit (hour windows and sessions
16
- // of at most 8 hours), so they are dropped on the next write.
15
+ // Older entries normally cannot affect a limit (hour windows and sessions
16
+ // of at most 8 hours), so they are dropped on the next write. Pauses can
17
+ // extend a session's wall-clock life: retain its entries until it ends.
17
18
  const RETAIN_MS = 9 * HOUR_MS;
18
19
  const DECISIONS_KEPT = 50;
19
20
  export class SmartPickupLedgerUnreadable extends Error {
@@ -50,7 +51,8 @@ async function read(clusterId) {
50
51
  }
51
52
  }
52
53
  async function write(clusterId, ledger, now) {
53
- ledger.entries = ledger.entries.filter((e) => e.status === 'reserved' || now - e.at < RETAIN_MS);
54
+ const activeSession = getClusterCached(clusterId)?.smartPickup?.session?.id;
55
+ ledger.entries = ledger.entries.filter((e) => e.status === 'reserved' || e.sessionId === activeSession || now - e.at < RETAIN_MS);
54
56
  ledger.decisions = ledger.decisions.slice(-DECISIONS_KEPT);
55
57
  await fs.mkdir(clusterDir(clusterId), { recursive: true });
56
58
  await atomicWriteFile(ledgerPath(clusterId), JSON.stringify(ledger, null, 2));
@@ -26,5 +26,6 @@ export declare function onSmartPickupMail(clusterId: string, workflowId: string,
26
26
  export declare function evaluateOnce(clusterId: string, workflowId: string): Promise<ObservedDecision | null>;
27
27
  export declare function noteSmartPickupStop(workflowId: string): Promise<void>;
28
28
  export declare function sweepSmartPickupMail(): Promise<void>;
29
+ export declare function pickupSmartWaitingMail(clusterId: string): Promise<void>;
29
30
  export declare function startSmartPickupObserver(next: Partial<Deps>): void;
30
31
  export {};
@@ -91,7 +91,7 @@ function armed(clusterId) {
91
91
  return null;
92
92
  const mode = clusterStore.autoPickupModeOf(state);
93
93
  const session = state.smartPickup?.session;
94
- if (!isSmartPickupMode(mode) || !session || !state.smartPickup?.evaluator)
94
+ if (state.autoPickupPausedAt !== undefined || !isSmartPickupMode(mode) || !session || !state.smartPickup?.evaluator)
95
95
  return null;
96
96
  if (deps.now() >= session.expiresAt)
97
97
  return null;
@@ -101,11 +101,11 @@ function armed(clusterId) {
101
101
  // evaluation per recipient at a time; a trigger during a run schedules one
102
102
  // more pass afterwards.
103
103
  export function onSmartPickupMail(clusterId, workflowId, chainDepth = 0) {
104
- if (!armed(clusterId))
105
- return Promise.resolve();
106
104
  const k = key(clusterId, workflowId);
107
105
  if (chainDepth > (depthSeen.get(k) ?? 0))
108
106
  depthSeen.set(k, chainDepth);
107
+ if (!armed(clusterId))
108
+ return Promise.resolve();
109
109
  const current = running.get(k);
110
110
  if (current) {
111
111
  rerun.add(k);
@@ -177,6 +177,8 @@ export async function evaluateOnce(clusterId, workflowId) {
177
177
  return d;
178
178
  }
179
179
  const evaluator = state.smartPickup.evaluator;
180
+ if (armed(clusterId)?.sessionId !== arm.sessionId)
181
+ return null;
180
182
  const slot = await reserveSmartPickup(clusterId, { kind: 'evaluator', memberId: workflowId, sessionId: arm.sessionId, limits, now: deps.now() }).catch(() => ({ ok: false, reason: 'ledger_unreadable' }));
181
183
  if (!slot.ok) {
182
184
  const d = { ...base, action: null, reason: slot.reason, outcome: 'skipped' };
@@ -191,6 +193,10 @@ export async function evaluateOnce(clusterId, workflowId) {
191
193
  }
192
194
  let raw = null;
193
195
  let failure = null;
196
+ if (armed(clusterId)?.sessionId !== arm.sessionId) {
197
+ await settleSmartPickup(clusterId, slot.id, 'session_changed', deps.now()).catch(() => undefined);
198
+ return null;
199
+ }
194
200
  try {
195
201
  raw = await evaluatorAdapter(evaluator.provider).evaluate({
196
202
  model: evaluator.model,
@@ -379,24 +385,27 @@ export async function sweepSmartPickupMail() {
379
385
  sweeping = true;
380
386
  try {
381
387
  for (const clusterId of clusterStore.listKnownClusterIds().sort()) {
382
- if (!armed(clusterId))
383
- continue;
384
- const members = [...(clusterStore.getClusterCached(clusterId)?.members ?? [])].sort();
385
- // One member at a time per cluster, in a stable order, so the
386
- // in-flight limit is respected and no mailbox starves another.
387
- for (const wfid of members) {
388
- if (deps.isBusy(wfid))
389
- continue;
390
- const depth = await clusterMailbox.depthFor(wfid).catch(() => 0);
391
- if (depth > 0)
392
- await onSmartPickupMail(clusterId, wfid).catch(() => undefined);
393
- }
388
+ await pickupSmartWaitingMail(clusterId);
394
389
  }
395
390
  }
396
391
  finally {
397
392
  sweeping = false;
398
393
  }
399
394
  }
395
+ export async function pickupSmartWaitingMail(clusterId) {
396
+ if (!armed(clusterId))
397
+ return;
398
+ const members = [...(clusterStore.getClusterCached(clusterId)?.members ?? [])].sort();
399
+ // One member at a time per cluster, in a stable order, so the
400
+ // in-flight limit is respected and no mailbox starves another.
401
+ for (const wfid of members) {
402
+ if (deps.isBusy(wfid))
403
+ continue;
404
+ const depth = await clusterMailbox.depthFor(wfid).catch(() => 0);
405
+ if (depth > 0)
406
+ await onSmartPickupMail(clusterId, wfid).catch(() => undefined);
407
+ }
408
+ }
400
409
  let mailTimer = null;
401
410
  const MAIL_SWEEP_MS = (() => {
402
411
  const raw = Number(process.env.ARCO_SMART_PICKUP_MAIL_SWEEP_MS);
@@ -0,0 +1,34 @@
1
+ export declare const TRANSCRIPT_WORKER_MAX_BYTES: number;
2
+ export declare const TRANSCRIPT_WORKER_PREFACE_BYTES: number;
3
+ export interface TranscriptSliceOptions {
4
+ turns: number;
5
+ includePartial: boolean;
6
+ preface: string;
7
+ cutoffSeq?: number;
8
+ wasBusy: boolean;
9
+ }
10
+ export declare function buildTranscriptWorkerSlice(cwd: string, workflowId: string, options: TranscriptSliceOptions): Promise<{
11
+ seed: string;
12
+ bytes: number;
13
+ availableTurns: number;
14
+ selectedTurns: number;
15
+ partialIncluded: boolean;
16
+ cutoffSeq: number;
17
+ digest: string;
18
+ branchKey: string;
19
+ }>;
20
+ export interface TranscriptSelectionPolicy {
21
+ maxBytes: number;
22
+ fitWholeTurns: boolean;
23
+ allTurns?: boolean;
24
+ }
25
+ export declare function buildTranscriptSlice(cwd: string, workflowId: string, options: TranscriptSliceOptions, policy?: TranscriptSelectionPolicy): Promise<{
26
+ seed: string;
27
+ bytes: number;
28
+ availableTurns: number;
29
+ selectedTurns: number;
30
+ partialIncluded: boolean;
31
+ cutoffSeq: number;
32
+ digest: string;
33
+ branchKey: string;
34
+ }>;