opencode-dux 1.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 (302) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +452 -0
  3. package/dist/agents/descriptions.d.ts +6 -0
  4. package/dist/agents/designer.d.ts +2 -0
  5. package/dist/agents/explorer.d.ts +2 -0
  6. package/dist/agents/fixer.d.ts +2 -0
  7. package/dist/agents/index.d.ts +22 -0
  8. package/dist/agents/interpreter.d.ts +2 -0
  9. package/dist/agents/librarian.d.ts +2 -0
  10. package/dist/agents/oracle.d.ts +2 -0
  11. package/dist/agents/orchestrator.d.ts +27 -0
  12. package/dist/agents/overrides.d.ts +18 -0
  13. package/dist/agents/prompt-blocks.d.ts +97 -0
  14. package/dist/agents/steward.d.ts +3 -0
  15. package/dist/cli/config-io.d.ts +24 -0
  16. package/dist/cli/config-manager.d.ts +4 -0
  17. package/dist/cli/index.d.ts +2 -0
  18. package/dist/cli/index.js +1006 -0
  19. package/dist/cli/install.d.ts +2 -0
  20. package/dist/cli/mcps.d.ts +13 -0
  21. package/dist/cli/model-key-normalization.d.ts +1 -0
  22. package/dist/cli/paths.d.ts +35 -0
  23. package/dist/cli/providers.d.ts +137 -0
  24. package/dist/cli/skills.d.ts +22 -0
  25. package/dist/cli/system.d.ts +5 -0
  26. package/dist/cli/types.d.ts +38 -0
  27. package/dist/config/constants.d.ts +12 -0
  28. package/dist/config/index.d.ts +4 -0
  29. package/dist/config/loader.d.ts +40 -0
  30. package/dist/config/runtime-preset.d.ts +12 -0
  31. package/dist/config/schema.d.ts +281 -0
  32. package/dist/config/utils.d.ts +10 -0
  33. package/dist/discovery/local/types.d.ts +79 -0
  34. package/dist/discovery/local.d.ts +73 -0
  35. package/dist/discovery/mcp-servers.d.ts +88 -0
  36. package/dist/discovery/skills.d.ts +94 -0
  37. package/dist/hooks/apply-patch/codec.d.ts +7 -0
  38. package/dist/hooks/apply-patch/errors.d.ts +25 -0
  39. package/dist/hooks/apply-patch/execution-context.d.ts +27 -0
  40. package/dist/hooks/apply-patch/index.d.ts +15 -0
  41. package/dist/hooks/apply-patch/matching.d.ts +26 -0
  42. package/dist/hooks/apply-patch/operations.d.ts +3 -0
  43. package/dist/hooks/apply-patch/patch.d.ts +2 -0
  44. package/dist/hooks/apply-patch/prepared-changes.d.ts +17 -0
  45. package/dist/hooks/apply-patch/resolution.d.ts +19 -0
  46. package/dist/hooks/apply-patch/rewrite.d.ts +7 -0
  47. package/dist/hooks/apply-patch/test-helpers.d.ts +6 -0
  48. package/dist/hooks/apply-patch/types.d.ts +80 -0
  49. package/dist/hooks/auto-update-checker/cache.d.ts +11 -0
  50. package/dist/hooks/auto-update-checker/checker.d.ts +32 -0
  51. package/dist/hooks/auto-update-checker/constants.d.ts +11 -0
  52. package/dist/hooks/auto-update-checker/index.d.ts +18 -0
  53. package/dist/hooks/auto-update-checker/types.d.ts +22 -0
  54. package/dist/hooks/chat-headers.d.ts +16 -0
  55. package/dist/hooks/context-pressure-reminder/index.d.ts +33 -0
  56. package/dist/hooks/delegate-task-retry/guidance.d.ts +2 -0
  57. package/dist/hooks/delegate-task-retry/hook.d.ts +8 -0
  58. package/dist/hooks/delegate-task-retry/index.d.ts +4 -0
  59. package/dist/hooks/delegate-task-retry/patterns.d.ts +11 -0
  60. package/dist/hooks/filter-available-skills/index.d.ts +32 -0
  61. package/dist/hooks/foreground-fallback/index.d.ts +72 -0
  62. package/dist/hooks/image-hook.d.ts +5 -0
  63. package/dist/hooks/index.d.ts +14 -0
  64. package/dist/hooks/json-error-recovery/hook.d.ts +18 -0
  65. package/dist/hooks/json-error-recovery/index.d.ts +1 -0
  66. package/dist/hooks/phase-reminder/index.d.ts +26 -0
  67. package/dist/hooks/post-file-tool-nudge/index.d.ts +19 -0
  68. package/dist/hooks/task-session-manager/index.d.ts +52 -0
  69. package/dist/hooks/todo-continuation/index.d.ts +53 -0
  70. package/dist/hooks/todo-continuation/todo-hygiene.d.ts +35 -0
  71. package/dist/index.d.ts +5 -0
  72. package/dist/index.js +31782 -0
  73. package/dist/mcp/context7.d.ts +6 -0
  74. package/dist/mcp/grep-app.d.ts +6 -0
  75. package/dist/mcp/index.d.ts +13 -0
  76. package/dist/mcp/types.d.ts +12 -0
  77. package/dist/mcp/websearch.d.ts +9 -0
  78. package/dist/skills/registry.d.ts +29 -0
  79. package/dist/subscriptions/accounts-store.d.ts +57 -0
  80. package/dist/subscriptions/index.d.ts +13 -0
  81. package/dist/subscriptions/neuralwatt-scraper.d.ts +14 -0
  82. package/dist/subscriptions/opencode-go-scraper.d.ts +27 -0
  83. package/dist/subscriptions/types.d.ts +115 -0
  84. package/dist/subscriptions/usage-service.d.ts +74 -0
  85. package/dist/tools/ast-grep/cli.d.ts +15 -0
  86. package/dist/tools/ast-grep/constants.d.ts +25 -0
  87. package/dist/tools/ast-grep/downloader.d.ts +5 -0
  88. package/dist/tools/ast-grep/index.d.ts +10 -0
  89. package/dist/tools/ast-grep/tools.d.ts +3 -0
  90. package/dist/tools/ast-grep/types.d.ts +30 -0
  91. package/dist/tools/ast-grep/utils.d.ts +4 -0
  92. package/dist/tools/delegate.d.ts +14 -0
  93. package/dist/tools/index.d.ts +5 -0
  94. package/dist/tools/preset-manager.d.ts +27 -0
  95. package/dist/tools/smartfetch/binary.d.ts +3 -0
  96. package/dist/tools/smartfetch/cache.d.ts +6 -0
  97. package/dist/tools/smartfetch/constants.d.ts +12 -0
  98. package/dist/tools/smartfetch/index.d.ts +3 -0
  99. package/dist/tools/smartfetch/network.d.ts +38 -0
  100. package/dist/tools/smartfetch/secondary-model.d.ts +28 -0
  101. package/dist/tools/smartfetch/tool.d.ts +3 -0
  102. package/dist/tools/smartfetch/types.d.ts +122 -0
  103. package/dist/tools/smartfetch/utils.d.ts +18 -0
  104. package/dist/tui-state.d.ts +168 -0
  105. package/dist/tui.d.ts +37 -0
  106. package/dist/tui.js +1896 -0
  107. package/dist/utils/agent-variant.d.ts +63 -0
  108. package/dist/utils/compat.d.ts +30 -0
  109. package/dist/utils/env.d.ts +1 -0
  110. package/dist/utils/index.d.ts +9 -0
  111. package/dist/utils/internal-initiator.d.ts +6 -0
  112. package/dist/utils/logger.d.ts +8 -0
  113. package/dist/utils/polling.d.ts +21 -0
  114. package/dist/utils/session-manager.d.ts +55 -0
  115. package/dist/utils/session.d.ts +90 -0
  116. package/dist/utils/subagent-depth.d.ts +35 -0
  117. package/dist/utils/system-collapse.d.ts +6 -0
  118. package/dist/utils/task.d.ts +4 -0
  119. package/dist/utils/zip-extractor.d.ts +1 -0
  120. package/index.ts +1 -0
  121. package/opencode-dux.schema.json +634 -0
  122. package/package.json +103 -0
  123. package/src/agents/descriptions.ts +55 -0
  124. package/src/agents/designer.test.ts +86 -0
  125. package/src/agents/designer.ts +154 -0
  126. package/src/agents/display-name.test.ts +186 -0
  127. package/src/agents/explorer.test.ts +79 -0
  128. package/src/agents/explorer.ts +144 -0
  129. package/src/agents/fixer.test.ts +79 -0
  130. package/src/agents/fixer.ts +145 -0
  131. package/src/agents/index.test.ts +472 -0
  132. package/src/agents/index.ts +248 -0
  133. package/src/agents/interpreter.ts +136 -0
  134. package/src/agents/librarian.test.ts +80 -0
  135. package/src/agents/librarian.ts +145 -0
  136. package/src/agents/oracle.test.ts +89 -0
  137. package/src/agents/oracle.ts +184 -0
  138. package/src/agents/orchestrator.test.ts +116 -0
  139. package/src/agents/orchestrator.ts +574 -0
  140. package/src/agents/overrides.ts +95 -0
  141. package/src/agents/prompt-blocks.test.ts +114 -0
  142. package/src/agents/prompt-blocks.ts +640 -0
  143. package/src/agents/steward.ts +146 -0
  144. package/src/cli/config-io.test.ts +536 -0
  145. package/src/cli/config-io.ts +473 -0
  146. package/src/cli/config-manager.test.ts +141 -0
  147. package/src/cli/config-manager.ts +4 -0
  148. package/src/cli/index.ts +88 -0
  149. package/src/cli/install.ts +282 -0
  150. package/src/cli/mcps.test.ts +62 -0
  151. package/src/cli/mcps.ts +39 -0
  152. package/src/cli/model-key-normalization.test.ts +21 -0
  153. package/src/cli/model-key-normalization.ts +60 -0
  154. package/src/cli/paths.test.ts +167 -0
  155. package/src/cli/paths.ts +144 -0
  156. package/src/cli/providers.test.ts +118 -0
  157. package/src/cli/providers.ts +141 -0
  158. package/src/cli/skills.test.ts +111 -0
  159. package/src/cli/skills.ts +103 -0
  160. package/src/cli/system.test.ts +91 -0
  161. package/src/cli/system.ts +180 -0
  162. package/src/cli/types.ts +43 -0
  163. package/src/config/constants.ts +58 -0
  164. package/src/config/index.ts +4 -0
  165. package/src/config/loader.test.ts +1194 -0
  166. package/src/config/loader.ts +269 -0
  167. package/src/config/model-resolution.test.ts +176 -0
  168. package/src/config/runtime-preset.test.ts +61 -0
  169. package/src/config/runtime-preset.ts +37 -0
  170. package/src/config/schema.ts +248 -0
  171. package/src/config/utils.test.ts +41 -0
  172. package/src/config/utils.ts +23 -0
  173. package/src/discovery/local/types.ts +85 -0
  174. package/src/discovery/local.ts +322 -0
  175. package/src/discovery/mcp-servers.ts +804 -0
  176. package/src/discovery/skills.ts +959 -0
  177. package/src/hooks/apply-patch/codec.test.ts +184 -0
  178. package/src/hooks/apply-patch/codec.ts +352 -0
  179. package/src/hooks/apply-patch/errors.ts +117 -0
  180. package/src/hooks/apply-patch/execution-context.ts +432 -0
  181. package/src/hooks/apply-patch/hook.test.ts +768 -0
  182. package/src/hooks/apply-patch/index.ts +126 -0
  183. package/src/hooks/apply-patch/matching.test.ts +215 -0
  184. package/src/hooks/apply-patch/matching.ts +586 -0
  185. package/src/hooks/apply-patch/operations.test.ts +1535 -0
  186. package/src/hooks/apply-patch/operations.ts +3 -0
  187. package/src/hooks/apply-patch/patch.ts +9 -0
  188. package/src/hooks/apply-patch/prepared-changes.ts +400 -0
  189. package/src/hooks/apply-patch/resolution.test.ts +420 -0
  190. package/src/hooks/apply-patch/resolution.ts +437 -0
  191. package/src/hooks/apply-patch/rewrite.ts +496 -0
  192. package/src/hooks/apply-patch/test-helpers.ts +52 -0
  193. package/src/hooks/apply-patch/types.ts +111 -0
  194. package/src/hooks/auto-update-checker/cache.test.ts +179 -0
  195. package/src/hooks/auto-update-checker/cache.ts +188 -0
  196. package/src/hooks/auto-update-checker/checker.test.ts +159 -0
  197. package/src/hooks/auto-update-checker/checker.ts +308 -0
  198. package/src/hooks/auto-update-checker/constants.ts +33 -0
  199. package/src/hooks/auto-update-checker/index.test.ts +282 -0
  200. package/src/hooks/auto-update-checker/index.ts +225 -0
  201. package/src/hooks/auto-update-checker/types.ts +26 -0
  202. package/src/hooks/chat-headers.test.ts +236 -0
  203. package/src/hooks/chat-headers.ts +97 -0
  204. package/src/hooks/context-pressure-reminder/index.test.ts +179 -0
  205. package/src/hooks/context-pressure-reminder/index.ts +137 -0
  206. package/src/hooks/delegate-task-retry/guidance.ts +41 -0
  207. package/src/hooks/delegate-task-retry/hook.ts +23 -0
  208. package/src/hooks/delegate-task-retry/index.test.ts +38 -0
  209. package/src/hooks/delegate-task-retry/index.ts +7 -0
  210. package/src/hooks/delegate-task-retry/patterns.ts +79 -0
  211. package/src/hooks/filter-available-skills/index.test.ts +297 -0
  212. package/src/hooks/filter-available-skills/index.ts +160 -0
  213. package/src/hooks/foreground-fallback/index.test.ts +624 -0
  214. package/src/hooks/foreground-fallback/index.ts +374 -0
  215. package/src/hooks/image-hook.ts +6 -0
  216. package/src/hooks/index.ts +17 -0
  217. package/src/hooks/json-error-recovery/hook.ts +73 -0
  218. package/src/hooks/json-error-recovery/index.test.ts +111 -0
  219. package/src/hooks/json-error-recovery/index.ts +6 -0
  220. package/src/hooks/phase-reminder/index.test.ts +74 -0
  221. package/src/hooks/phase-reminder/index.ts +85 -0
  222. package/src/hooks/post-file-tool-nudge/index.test.ts +94 -0
  223. package/src/hooks/post-file-tool-nudge/index.ts +63 -0
  224. package/src/hooks/task-session-manager/index.test.ts +833 -0
  225. package/src/hooks/task-session-manager/index.ts +434 -0
  226. package/src/hooks/todo-continuation/index.test.ts +3026 -0
  227. package/src/hooks/todo-continuation/index.ts +878 -0
  228. package/src/hooks/todo-continuation/todo-hygiene.test.ts +204 -0
  229. package/src/hooks/todo-continuation/todo-hygiene.ts +207 -0
  230. package/src/index.ts +1672 -0
  231. package/src/mcp/context7.ts +14 -0
  232. package/src/mcp/grep-app.ts +11 -0
  233. package/src/mcp/index.test.ts +96 -0
  234. package/src/mcp/index.ts +66 -0
  235. package/src/mcp/types.ts +16 -0
  236. package/src/mcp/websearch.ts +47 -0
  237. package/src/skills/codemap/README.md +60 -0
  238. package/src/skills/codemap/SKILL.md +174 -0
  239. package/src/skills/codemap/scripts/codemap.mjs +483 -0
  240. package/src/skills/codemap/scripts/codemap.test.ts +129 -0
  241. package/src/skills/registry.ts +218 -0
  242. package/src/skills/simplify/README.md +19 -0
  243. package/src/skills/simplify/SKILL.md +138 -0
  244. package/src/subscriptions/accounts-store.test.ts +236 -0
  245. package/src/subscriptions/accounts-store.ts +184 -0
  246. package/src/subscriptions/index.ts +30 -0
  247. package/src/subscriptions/neuralwatt-scraper.ts +108 -0
  248. package/src/subscriptions/opencode-go-scraper.ts +301 -0
  249. package/src/subscriptions/types.ts +145 -0
  250. package/src/subscriptions/usage-service.test.ts +202 -0
  251. package/src/subscriptions/usage-service.ts +651 -0
  252. package/src/tools/ast-grep/cli.ts +257 -0
  253. package/src/tools/ast-grep/constants.ts +214 -0
  254. package/src/tools/ast-grep/downloader.ts +131 -0
  255. package/src/tools/ast-grep/index.ts +24 -0
  256. package/src/tools/ast-grep/tools.ts +117 -0
  257. package/src/tools/ast-grep/types.ts +51 -0
  258. package/src/tools/ast-grep/utils.ts +126 -0
  259. package/src/tools/delegate-handoff.test.ts +18 -0
  260. package/src/tools/delegate.ts +508 -0
  261. package/src/tools/index.ts +8 -0
  262. package/src/tools/preset-manager.test.ts +795 -0
  263. package/src/tools/preset-manager.ts +332 -0
  264. package/src/tools/smartfetch/binary.ts +58 -0
  265. package/src/tools/smartfetch/cache.test.ts +34 -0
  266. package/src/tools/smartfetch/cache.ts +112 -0
  267. package/src/tools/smartfetch/constants.ts +29 -0
  268. package/src/tools/smartfetch/index.ts +8 -0
  269. package/src/tools/smartfetch/network.test.ts +178 -0
  270. package/src/tools/smartfetch/network.ts +614 -0
  271. package/src/tools/smartfetch/secondary-model.test.ts +85 -0
  272. package/src/tools/smartfetch/secondary-model.ts +276 -0
  273. package/src/tools/smartfetch/tool.test.ts +60 -0
  274. package/src/tools/smartfetch/tool.ts +832 -0
  275. package/src/tools/smartfetch/types.ts +135 -0
  276. package/src/tools/smartfetch/utils.test.ts +24 -0
  277. package/src/tools/smartfetch/utils.ts +456 -0
  278. package/src/tui-state.test.ts +867 -0
  279. package/src/tui-state.ts +1255 -0
  280. package/src/tui.test.ts +336 -0
  281. package/src/tui.ts +1539 -0
  282. package/src/utils/agent-variant.test.ts +244 -0
  283. package/src/utils/agent-variant.ts +187 -0
  284. package/src/utils/compat.ts +91 -0
  285. package/src/utils/env.ts +12 -0
  286. package/src/utils/index.ts +9 -0
  287. package/src/utils/internal-initiator.ts +28 -0
  288. package/src/utils/logger.test.ts +220 -0
  289. package/src/utils/logger.ts +136 -0
  290. package/src/utils/polling.test.ts +191 -0
  291. package/src/utils/polling.ts +67 -0
  292. package/src/utils/session-manager.test.ts +173 -0
  293. package/src/utils/session-manager.ts +356 -0
  294. package/src/utils/session.test.ts +110 -0
  295. package/src/utils/session.ts +389 -0
  296. package/src/utils/subagent-depth.test.ts +170 -0
  297. package/src/utils/subagent-depth.ts +75 -0
  298. package/src/utils/system-collapse.test.ts +86 -0
  299. package/src/utils/system-collapse.ts +24 -0
  300. package/src/utils/task.test.ts +24 -0
  301. package/src/utils/task.ts +20 -0
  302. package/src/utils/zip-extractor.ts +102 -0
@@ -0,0 +1,173 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { deriveTaskSessionLabel, SessionManager } from './session-manager';
3
+
4
+ describe('SessionManager', () => {
5
+ test('keeps most recently used sessions within limit', () => {
6
+ const manager = new SessionManager(2);
7
+
8
+ manager.remember({
9
+ parentSessionId: 'parent-1',
10
+ taskId: 'task-1',
11
+ agentType: 'explorer',
12
+ label: 'first thread',
13
+ });
14
+ manager.remember({
15
+ parentSessionId: 'parent-1',
16
+ taskId: 'task-2',
17
+ agentType: 'explorer',
18
+ label: 'second thread',
19
+ });
20
+ manager.markUsed('parent-1', 'explorer', 'task-1');
21
+ manager.remember({
22
+ parentSessionId: 'parent-1',
23
+ taskId: 'task-3',
24
+ agentType: 'explorer',
25
+ label: 'third thread',
26
+ });
27
+
28
+ const prompt = manager.formatForPrompt('parent-1');
29
+ expect(prompt).toContain('exp-1 first thread');
30
+ expect(prompt).toContain('exp-3 third thread');
31
+ expect(prompt).not.toContain('exp-2 second thread');
32
+ });
33
+
34
+ test('clears parent-scoped sessions', () => {
35
+ const manager = new SessionManager(2);
36
+
37
+ manager.remember({
38
+ parentSessionId: 'parent-1',
39
+ taskId: 'task-1',
40
+ agentType: 'oracle',
41
+ label: 'architecture',
42
+ });
43
+
44
+ manager.clearParent('parent-1');
45
+
46
+ expect(manager.formatForPrompt('parent-1')).toBeUndefined();
47
+ });
48
+
49
+ test('includes read context for remembered sessions', () => {
50
+ const manager = new SessionManager(2);
51
+
52
+ manager.remember({
53
+ parentSessionId: 'parent-1',
54
+ taskId: 'task-1',
55
+ agentType: 'explorer',
56
+ label: 'session manager',
57
+ });
58
+ manager.addContext('task-1', [
59
+ { path: 'src/index.ts', lineCount: 42, lastReadAt: 1 },
60
+ ]);
61
+
62
+ const prompt = manager.formatForPrompt('parent-1');
63
+ expect(prompt).toContain('exp-1 session manager');
64
+ expect(prompt).toContain(
65
+ 'Context read by exp-1: src/index.ts (42 lines)',
66
+ );
67
+ });
68
+
69
+ test('filters tiny reads and caps read context files', () => {
70
+ const manager = new SessionManager(2);
71
+
72
+ manager.remember({
73
+ parentSessionId: 'parent-1',
74
+ taskId: 'task-1',
75
+ agentType: 'explorer',
76
+ label: 'large context',
77
+ });
78
+ manager.addContext(
79
+ 'task-1',
80
+ Array.from({ length: 10 }, (_, index) => ({
81
+ path: `file-${index}.ts`,
82
+ lineCount: index === 0 ? 9 : 20 + index,
83
+ lastReadAt: index,
84
+ })),
85
+ );
86
+
87
+ const prompt = manager.formatForPrompt('parent-1') ?? '';
88
+ expect(prompt).not.toContain('file-0.ts');
89
+ expect(prompt).toContain('file-9.ts (29 lines)');
90
+ expect(prompt).toContain('(+1 more)');
91
+ });
92
+
93
+ test('uses configurable read context thresholds', () => {
94
+ const manager = new SessionManager(2, {
95
+ readContextMinLines: 5,
96
+ readContextMaxFiles: 1,
97
+ });
98
+
99
+ manager.remember({
100
+ parentSessionId: 'parent-1',
101
+ taskId: 'task-1',
102
+ agentType: 'explorer',
103
+ label: 'custom thresholds',
104
+ });
105
+ manager.addContext('task-1', [
106
+ { path: 'small.ts', lineCount: 4, lastReadAt: 1 },
107
+ { path: 'medium.ts', lineCount: 5, lastReadAt: 2 },
108
+ { path: 'large.ts', lineCount: 12, lastReadAt: 3 },
109
+ ]);
110
+
111
+ const prompt = manager.formatForPrompt('parent-1') ?? '';
112
+ expect(prompt).not.toContain('small.ts');
113
+ expect(prompt).toContain('large.ts (12 lines)');
114
+ expect(prompt).not.toContain('medium.ts');
115
+ expect(prompt).toContain('(+1 more)');
116
+ });
117
+
118
+ test('bounds stored read context files to the render cap plus overflow marker', () => {
119
+ const manager = new SessionManager(2, {
120
+ readContextMinLines: 1,
121
+ readContextMaxFiles: 2,
122
+ });
123
+
124
+ const remembered = manager.remember({
125
+ parentSessionId: 'parent-1',
126
+ taskId: 'task-1',
127
+ agentType: 'explorer',
128
+ label: 'bounded context',
129
+ });
130
+ manager.addContext(
131
+ 'task-1',
132
+ Array.from({ length: 10 }, (_, index) => ({
133
+ path: `file-${index}.ts`,
134
+ lineCount: 10,
135
+ lastReadAt: index,
136
+ })),
137
+ );
138
+
139
+ expect(remembered.contextFiles).toHaveLength(3);
140
+ const prompt = manager.formatForPrompt('parent-1') ?? '';
141
+ expect(prompt).toContain('file-9.ts (10 lines)');
142
+ expect(prompt).toContain('file-8.ts (10 lines)');
143
+ expect(prompt).toContain('(+1 more)');
144
+ expect(prompt).not.toContain('file-0.ts');
145
+ });
146
+ });
147
+
148
+ describe('deriveTaskSessionLabel', () => {
149
+ test('prefers description over prompt', () => {
150
+ expect(
151
+ deriveTaskSessionLabel({
152
+ description: 'config schema lookup',
153
+ prompt: 'ignored prompt line',
154
+ agentType: 'explorer',
155
+ }),
156
+ ).toBe('config schema lookup');
157
+ });
158
+
159
+ test('falls back to prompt then generic label', () => {
160
+ expect(
161
+ deriveTaskSessionLabel({
162
+ prompt: '\n inspect task resumption support \nmore context',
163
+ agentType: 'explorer',
164
+ }),
165
+ ).toBe('inspect task resumption support');
166
+
167
+ expect(
168
+ deriveTaskSessionLabel({
169
+ agentType: 'fixer',
170
+ }),
171
+ ).toBe('recent fixer task');
172
+ });
173
+ });
@@ -0,0 +1,356 @@
1
+ import type { AgentName } from '../config';
2
+
3
+ export interface ContextFile {
4
+ path: string;
5
+ lineCount: number;
6
+ lineNumbers?: number[];
7
+ lastReadAt: number;
8
+ }
9
+
10
+ export interface RememberedTaskSession {
11
+ alias: string;
12
+ taskId: string;
13
+ agentType: AgentName;
14
+ label: string;
15
+ contextFiles: ContextFile[];
16
+ createdAt: number;
17
+ lastUsedAt: number;
18
+ }
19
+
20
+ type SessionGroupMap = Map<AgentName, RememberedTaskSession[]>;
21
+
22
+ const MIN_CONTEXT_FILE_LINES = 10;
23
+ const MAX_CONTEXT_FILES_PER_SESSION = 8;
24
+
25
+ interface SessionManagerOptions {
26
+ readContextMinLines?: number;
27
+ readContextMaxFiles?: number;
28
+ }
29
+
30
+ function aliasPrefix(agentType: AgentName): string {
31
+ switch (agentType) {
32
+ case 'explorer':
33
+ return 'exp';
34
+ case 'librarian':
35
+ return 'lib';
36
+ case 'oracle':
37
+ return 'ora';
38
+ case 'designer':
39
+ return 'des';
40
+ case 'fixer':
41
+ return 'fix';
42
+ case 'steward':
43
+ return 'stw';
44
+ case 'interpreter':
45
+ return 'int';
46
+
47
+ case 'orchestrator':
48
+ return 'orc';
49
+ }
50
+ }
51
+
52
+ function normalizeWhitespace(value: string): string {
53
+ return value.replace(/\s+/g, ' ').trim();
54
+ }
55
+
56
+ export function deriveTaskSessionLabel(input: {
57
+ description?: string;
58
+ prompt?: string;
59
+ agentType: AgentName;
60
+ }): string {
61
+ const preferred = normalizeWhitespace(input.description ?? '');
62
+ if (preferred) {
63
+ return preferred.slice(0, 48);
64
+ }
65
+
66
+ const firstPromptLine = (input.prompt ?? '')
67
+ .split(/\r?\n/)
68
+ .map((line) => normalizeWhitespace(line))
69
+ .find(Boolean);
70
+
71
+ if (firstPromptLine) {
72
+ return firstPromptLine.slice(0, 48);
73
+ }
74
+
75
+ return `recent ${input.agentType} task`;
76
+ }
77
+
78
+ export class SessionManager {
79
+ private readonly maxSessionsPerAgent: number;
80
+ private readonly readContextMinLines: number;
81
+ private readonly readContextMaxFiles: number;
82
+ private readonly sessionsByParent = new Map<string, SessionGroupMap>();
83
+ private readonly nextAliasIndexByParent = new Map<
84
+ string,
85
+ Map<AgentName, number>
86
+ >();
87
+ private orderCounter = 0;
88
+
89
+ constructor(
90
+ maxSessionsPerAgent: number,
91
+ options: SessionManagerOptions = {},
92
+ ) {
93
+ this.maxSessionsPerAgent = maxSessionsPerAgent;
94
+ this.readContextMinLines =
95
+ options.readContextMinLines ?? MIN_CONTEXT_FILE_LINES;
96
+ this.readContextMaxFiles =
97
+ options.readContextMaxFiles ?? MAX_CONTEXT_FILES_PER_SESSION;
98
+ }
99
+
100
+ remember(input: {
101
+ parentSessionId: string;
102
+ taskId: string;
103
+ agentType: AgentName;
104
+ label: string;
105
+ }): RememberedTaskSession {
106
+ const now = this.nextOrder();
107
+ const group = this.getAgentGroup(
108
+ input.parentSessionId,
109
+ input.agentType,
110
+ true,
111
+ );
112
+ if (!group) {
113
+ throw new Error('Failed to initialize session group');
114
+ }
115
+ const existing = group.find((entry) => entry.taskId === input.taskId);
116
+
117
+ if (existing) {
118
+ existing.label = input.label;
119
+ existing.lastUsedAt = this.nextOrder();
120
+ return existing;
121
+ }
122
+
123
+ const remembered: RememberedTaskSession = {
124
+ alias: this.nextAlias(input.parentSessionId, input.agentType),
125
+ taskId: input.taskId,
126
+ agentType: input.agentType,
127
+ label: input.label,
128
+ contextFiles: [],
129
+ createdAt: now,
130
+ lastUsedAt: now,
131
+ };
132
+
133
+ group.push(remembered);
134
+ this.trimGroup(group);
135
+ return remembered;
136
+ }
137
+
138
+ markUsed(parentSessionId: string, agentType: AgentName, key: string): void {
139
+ const group = this.getAgentGroup(parentSessionId, agentType, false);
140
+ const match = group?.find(
141
+ (entry) => entry.alias === key || entry.taskId === key,
142
+ );
143
+
144
+ if (match) {
145
+ match.lastUsedAt = this.nextOrder();
146
+ }
147
+ }
148
+
149
+ resolve(parentSessionId: string, agentType: AgentName, key: string) {
150
+ const group = this.getAgentGroup(parentSessionId, agentType, false);
151
+ return group?.find((entry) => entry.alias === key || entry.taskId === key);
152
+ }
153
+
154
+ drop(parentSessionId: string, agentType: AgentName, key: string): void {
155
+ const group = this.getAgentGroup(parentSessionId, agentType, false);
156
+ if (!group) return;
157
+
158
+ const next = group.filter(
159
+ (entry) => entry.alias !== key && entry.taskId !== key,
160
+ );
161
+ this.setAgentGroup(parentSessionId, agentType, next);
162
+ }
163
+
164
+ dropTask(taskId: string): void {
165
+ for (const [parentSessionId, groups] of this.sessionsByParent.entries()) {
166
+ for (const [agentType, group] of groups.entries()) {
167
+ const next = group.filter((entry) => entry.taskId !== taskId);
168
+ this.setAgentGroup(parentSessionId, agentType, next);
169
+ }
170
+ }
171
+ }
172
+
173
+ taskIds(): Set<string> {
174
+ const ids = new Set<string>();
175
+ for (const groups of this.sessionsByParent.values()) {
176
+ for (const group of groups.values()) {
177
+ for (const entry of group) {
178
+ ids.add(entry.taskId);
179
+ }
180
+ }
181
+ }
182
+ return ids;
183
+ }
184
+
185
+ addContext(taskId: string, files: ContextFile[]): void {
186
+ if (files.length === 0) return;
187
+
188
+ for (const groups of this.sessionsByParent.values()) {
189
+ for (const group of groups.values()) {
190
+ const match = group.find((entry) => entry.taskId === taskId);
191
+ if (!match) continue;
192
+
193
+ const existing = new Map(
194
+ match.contextFiles.map((file) => [file.path, file]),
195
+ );
196
+ for (const file of files) {
197
+ const previous = existing.get(file.path);
198
+ if (previous) {
199
+ previous.lineCount = Math.max(previous.lineCount, file.lineCount);
200
+ previous.lastReadAt = Math.max(
201
+ previous.lastReadAt,
202
+ file.lastReadAt,
203
+ );
204
+ continue;
205
+ }
206
+ match.contextFiles.push({ ...file });
207
+ }
208
+ this.trimContextFiles(match);
209
+ }
210
+ }
211
+ }
212
+
213
+ clearParent(parentSessionId: string): void {
214
+ this.sessionsByParent.delete(parentSessionId);
215
+ this.nextAliasIndexByParent.delete(parentSessionId);
216
+ }
217
+
218
+ formatForPrompt(parentSessionId: string): string | undefined {
219
+ const groups = this.sessionsByParent.get(parentSessionId);
220
+ if (!groups || groups.size === 0) return undefined;
221
+
222
+ const lines = [...groups.entries()]
223
+ .map(
224
+ ([agentType, entries]) =>
225
+ [
226
+ agentType,
227
+ [...entries].sort((a, b) => b.lastUsedAt - a.lastUsedAt),
228
+ ] as const,
229
+ )
230
+ .filter(([, entries]) => entries.length > 0)
231
+ .sort((a, b) => b[1][0].lastUsedAt - a[1][0].lastUsedAt)
232
+ .map(([agentType, entries]) =>
233
+ [
234
+ `- ${agentType}: ${entries
235
+ .map((entry) => `${entry.alias} ${entry.label}`)
236
+ .join('; ')}`,
237
+ ...entries
238
+ .map(
239
+ (entry) =>
240
+ [
241
+ entry,
242
+ formatContextFiles(entry.contextFiles, {
243
+ minLines: this.readContextMinLines,
244
+ maxFiles: this.readContextMaxFiles,
245
+ }),
246
+ ] as const,
247
+ )
248
+ .filter(([, context]) => context.length > 0)
249
+ .map(
250
+ ([entry, context]) =>
251
+ ` Context read by ${entry.alias}: ${context}`,
252
+ ),
253
+ ].join('\n'),
254
+ );
255
+
256
+ if (lines.length === 0) return undefined;
257
+
258
+ return [
259
+ '### Resumable Sessions',
260
+ 'Reuse only for clear continuation of the same thread. Otherwise start fresh.',
261
+ '',
262
+ ...lines,
263
+ ].join('\n');
264
+ }
265
+
266
+ private getAgentGroup(
267
+ parentSessionId: string,
268
+ agentType: AgentName,
269
+ create: boolean,
270
+ ): RememberedTaskSession[] | undefined {
271
+ let groups = this.sessionsByParent.get(parentSessionId);
272
+ if (!groups && create) {
273
+ groups = new Map();
274
+ this.sessionsByParent.set(parentSessionId, groups);
275
+ }
276
+
277
+ let group = groups?.get(agentType);
278
+ if (!group && create && groups) {
279
+ group = [];
280
+ groups.set(agentType, group);
281
+ }
282
+
283
+ return group;
284
+ }
285
+
286
+ private setAgentGroup(
287
+ parentSessionId: string,
288
+ agentType: AgentName,
289
+ entries: RememberedTaskSession[],
290
+ ): void {
291
+ const groups = this.sessionsByParent.get(parentSessionId);
292
+ if (!groups) return;
293
+
294
+ if (entries.length === 0) {
295
+ groups.delete(agentType);
296
+ if (groups.size === 0) {
297
+ this.sessionsByParent.delete(parentSessionId);
298
+ this.nextAliasIndexByParent.delete(parentSessionId);
299
+ }
300
+ return;
301
+ }
302
+
303
+ groups.set(agentType, entries);
304
+ }
305
+
306
+ private nextAlias(parentSessionId: string, agentType: AgentName): string {
307
+ let counters = this.nextAliasIndexByParent.get(parentSessionId);
308
+ if (!counters) {
309
+ counters = new Map();
310
+ this.nextAliasIndexByParent.set(parentSessionId, counters);
311
+ }
312
+
313
+ const next = (counters.get(agentType) ?? 0) + 1;
314
+ counters.set(agentType, next);
315
+ return `${aliasPrefix(agentType)}-${next}`;
316
+ }
317
+
318
+ private trimGroup(group: RememberedTaskSession[]): void {
319
+ group.sort((a, b) => b.lastUsedAt - a.lastUsedAt);
320
+ if (group.length > this.maxSessionsPerAgent) {
321
+ group.length = this.maxSessionsPerAgent;
322
+ }
323
+ }
324
+
325
+ private trimContextFiles(entry: RememberedTaskSession): void {
326
+ if (this.readContextMaxFiles === 0) {
327
+ entry.contextFiles = [];
328
+ return;
329
+ }
330
+
331
+ entry.contextFiles = entry.contextFiles
332
+ .filter((file) => file.lineCount >= this.readContextMinLines)
333
+ .sort((a, b) => b.lastReadAt - a.lastReadAt)
334
+ .slice(0, this.readContextMaxFiles + 1);
335
+ }
336
+
337
+ private nextOrder(): number {
338
+ this.orderCounter += 1;
339
+ return this.orderCounter;
340
+ }
341
+ }
342
+
343
+ function formatContextFiles(
344
+ files: ContextFile[],
345
+ options: { minLines: number; maxFiles: number },
346
+ ): string {
347
+ const eligible = files
348
+ .filter((file) => file.lineCount >= options.minLines)
349
+ .sort((a, b) => b.lastReadAt - a.lastReadAt);
350
+ const shown = eligible.slice(0, options.maxFiles);
351
+ const rest = eligible.length - shown.length;
352
+ const rendered = shown.map(
353
+ (file) => `${file.path} (${file.lineCount} lines)`,
354
+ );
355
+ return `${rendered.join(', ')}${rest > 0 ? ` (+${rest} more)` : ''}`;
356
+ }
@@ -0,0 +1,110 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import {
3
+ isForwardableImagePart,
4
+ normalizeImagePartsForChildPrompt,
5
+ type PromptBodyPart,
6
+ } from './session';
7
+
8
+ describe('isForwardableImagePart', () => {
9
+ test('accepts legacy image part type', () => {
10
+ expect(isForwardableImagePart({ type: 'image' })).toBe(true);
11
+ });
12
+
13
+ test('accepts OpenCode file part with image/* mime', () => {
14
+ expect(
15
+ isForwardableImagePart({
16
+ type: 'file',
17
+ mime: 'image/png',
18
+ url: 'https://example.com/blob',
19
+ }),
20
+ ).toBe(true);
21
+ expect(
22
+ isForwardableImagePart({
23
+ type: 'file',
24
+ mime: 'IMAGE/JPEG',
25
+ url: 'https://example.com/blob',
26
+ }),
27
+ ).toBe(true);
28
+ });
29
+
30
+ test('rejects non-image file mime', () => {
31
+ expect(
32
+ isForwardableImagePart({
33
+ type: 'file',
34
+ mime: 'application/pdf',
35
+ url: 'x',
36
+ }),
37
+ ).toBe(false);
38
+ });
39
+
40
+ test('accepts file part with image-like filename when mime absent', () => {
41
+ expect(
42
+ isForwardableImagePart({
43
+ type: 'file',
44
+ url: 'x',
45
+ filename: 'clipboard.webp',
46
+ }),
47
+ ).toBe(true);
48
+ });
49
+
50
+ test('rejects plain text parts', () => {
51
+ expect(isForwardableImagePart({ type: 'text', text: 'hi' })).toBe(false);
52
+ });
53
+ });
54
+
55
+ describe('normalizeImagePartsForChildPrompt', () => {
56
+ test('drops stored IDs and keeps FilePartInput-shaped drafts', () => {
57
+ const normalized = normalizeImagePartsForChildPrompt([
58
+ {
59
+ type: 'file',
60
+ mime: 'image/png',
61
+ url: 'https://cdn.example.com/a.png',
62
+ id: 'part1',
63
+ sessionID: 'sess',
64
+ messageID: 'msg',
65
+ } as PromptBodyPart,
66
+ ]);
67
+ expect(normalized).toEqual([
68
+ {
69
+ type: 'file',
70
+ mime: 'image/png',
71
+ url: 'https://cdn.example.com/a.png',
72
+ },
73
+ ]);
74
+ });
75
+
76
+ test('resolves file source.path to file URL when url missing', () => {
77
+ const normalized = normalizeImagePartsForChildPrompt(
78
+ [
79
+ {
80
+ type: 'file',
81
+ mime: 'image/png',
82
+ url: '',
83
+ source: { type: 'file', path: 'assets/clip.png' },
84
+ } as PromptBodyPart,
85
+ ],
86
+ '/workspace/proj',
87
+ );
88
+ expect(normalized.length).toBe(1);
89
+ expect(normalized[0]?.type).toBe('file');
90
+ expect(normalized[0]?.mime).toBe('image/png');
91
+ expect(String(normalized[0]?.url)).toMatch(/^file:/);
92
+ expect(String(normalized[0]?.url)).toMatch(/clip\.png/);
93
+ });
94
+
95
+ test('maps legacy data URL image part to file draft', () => {
96
+ const normalized = normalizeImagePartsForChildPrompt([
97
+ {
98
+ type: 'image',
99
+ image: 'data:image/gif;base64,R0lGODlhAQABAAAAACw=',
100
+ } as PromptBodyPart,
101
+ ]);
102
+ expect(normalized).toEqual([
103
+ {
104
+ type: 'file',
105
+ mime: 'image/gif',
106
+ url: 'data:image/gif;base64,R0lGODlhAQABAAAAACw=',
107
+ },
108
+ ]);
109
+ });
110
+ });