dsh-progress-union 0.6.3 → 0.7.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/harness.mjs CHANGED
@@ -1,249 +1,249 @@
1
- /**
2
- * 共享 mock 基座:纯内存 services 驱动 apply(),不起真宿主。
3
- * 形状对齐 l3j3y3/dsh-progress-board test/host.test.mjs 的 createHarness。
4
- */
5
- import { apply } from '../index.js'
6
-
7
- export const SESSION = 'session-union-test'
8
-
9
- export function createHarness({ seed = {}, config = {}, withProjection = true, withProvide = true, storageless = false, llm = undefined, settings = undefined, cwd = '' } = {}) {
10
- const records = { ...seed }
11
- const routes = new Map()
12
- const toolMap = new Map()
13
- const commandMap = new Map()
14
- const sections = new Map()
15
- const contexts = new Map()
16
- const listeners = []
17
- const agentHooks = []
18
- const provided = new Map()
19
- const projections = new Map()
20
-
21
- const unit = {
22
- async loadAll() {
23
- return { tables: { sessions: { ...records } } }
24
- },
25
- async putRecord(_table, key, record) {
26
- records[key] = record
27
- },
28
- async close() {},
29
- }
30
-
31
- const storage = storageless
32
- ? { backend: { names: () => [], get: () => ({}) } }
33
- : {
34
- backend: {
35
- names: () => ['json'],
36
- get: () => ({ kv: { open: async () => unit } }),
37
- },
38
- }
39
-
40
- const webServer = {
41
- register(route) {
42
- routes.set(route.path, route)
43
- return () => routes.delete(route.path)
44
- },
45
- }
46
-
47
- const tools = {
48
- register(definition, options) {
49
- toolMap.set(definition.name, { definition, options })
50
- return () => toolMap.delete(definition.name)
51
- },
52
- }
53
-
54
- const commands = {
55
- register(definition) {
56
- commandMap.set(definition.name, definition)
57
- return () => commandMap.delete(definition.name)
58
- },
59
- }
60
-
61
- const systemPrompt = {}
62
-
63
- const appends = []
64
- const followups = []
65
- // 宿主 inbox 形状(durable projection):followup 排进 nextTurn,被取走才算消费。
66
- // 驱动器靠它判断「已有一轮还没跑」,避免补点火与 idle 边沿撞车连排两轮。
67
- const nextTurn = []
68
- const nextStep = []
69
- const agent = {
70
- id: SESSION,
71
- status: 'idle',
72
- inbox: {
73
- nextTurn,
74
- nextStep,
75
- // 宿主 Inbox 契约:撤回一条排队消息(驱动器用它在重启清理孤儿轮次)。
76
- remove(id) {
77
- for (const list of [nextTurn, nextStep]) {
78
- const i = list.findIndex((m) => m && m.id === id)
79
- if (i >= 0) { list.splice(i, 1); return true }
80
- }
81
- return false
82
- },
83
- },
84
- // /progress-goal 续轮驱动器经 agent.followup 排队;这里记录被派发的轮次消息。
85
- followups,
86
- followup(message) { followups.push(message); nextTurn.push(message) },
87
- // v0.3.0:todo 镜像写通道。默认记录调用;测试可覆写 append 注入失败。
88
- // v0.5.0:cwd 放在 session **顶层**(实测 shape,不是 header.cwd)——plan 扫描靠它。
89
- session: {
90
- id: SESSION,
91
- ...(cwd ? { cwd } : {}),
92
- append(type, data) { appends.push({ type, data }); return Promise.resolve() },
93
- },
94
- ctx: {
95
- effect(fn) {
96
- return fn()
97
- },
98
- systemPrompt: {
99
- section({ name, text }) {
100
- sections.set(name, text)
101
- return () => sections.delete(name)
102
- },
103
- context({ name, text }) {
104
- contexts.set(name, text)
105
- return () => contexts.delete(name)
106
- },
107
- },
108
- on(type, handler) {
109
- agentHooks.push({ type, handler })
110
- return () => {
111
- const index = agentHooks.findIndex((h) => h.type === type && h.handler === handler)
112
- if (index >= 0) agentHooks.splice(index, 1)
113
- }
114
- },
115
- },
116
- }
117
-
118
- const agents = { list: () => [agent] }
119
-
120
- const ctx = {
121
- get(name) {
122
- return { storage, webServer, systemPrompt, tools, agents, commands, llm, settings }[name]
123
- },
124
- on(type, handler) {
125
- listeners.push({ type, handler })
126
- return () => {
127
- const index = listeners.findIndex((h) => h.type === type && h.handler === handler)
128
- if (index >= 0) listeners.splice(index, 1)
129
- }
130
- },
131
- effect(fn) {
132
- const dispose = fn()
133
- return () => {
134
- if (typeof dispose === 'function') dispose()
135
- }
136
- },
137
- inject(deps, callback) {
138
- if (withProjection && deps.includes('sessionProjections')) {
139
- callback({
140
- sessionProjections: {
141
- register(definition) {
142
- projections.set(definition.key, definition)
143
- return () => projections.delete(definition.key)
144
- },
145
- },
146
- })
147
- } else {
148
- callback({})
149
- }
150
- },
151
- }
152
- if (withProvide) {
153
- ctx.provide = (name, factory) => {
154
- provided.set(name, factory())
155
- return () => provided.delete(name)
156
- }
157
- }
158
-
159
- return {
160
- ctx,
161
- config,
162
- agent,
163
- appends,
164
- followups,
165
- inbox: agent.inbox,
166
- records,
167
- routes,
168
- toolMap,
169
- commandMap,
170
- sections,
171
- contexts,
172
- listeners,
173
- agentHooks,
174
- provided,
175
- projections,
176
- run: () => apply(ctx, config),
177
- /** 模拟宿主 claim:把一条排队消息从 nextTurn 取走(consume=true 才算消费)。 */
178
- claim(message) {
179
- const i = nextTurn.indexOf(message)
180
- if (i >= 0) nextTurn.splice(i, 1)
181
- return message
182
- },
183
- /** 按 (session, {type, data}) 派发一条 session/event */
184
- emit(type, data, session) {
185
- // 默认 session 带顶层 cwd(实测 shape):plan 扫描靠事件流里的 session。
186
- const target = session ?? (cwd ? { id: SESSION, cwd } : { id: SESSION })
187
- for (const h of listeners.filter((l) => l.type === 'session/event')) h.handler(target, { type, data })
188
- },
189
- }
190
- }
191
-
192
- export function jsonReq(method, url, body) {
193
- const payload = Buffer.from(JSON.stringify(body))
194
- return {
195
- method,
196
- url,
197
- headers: { 'content-type': 'application/json' },
198
- async *[Symbol.asyncIterator]() {
199
- yield payload
200
- },
201
- }
202
- }
203
-
204
- export function getReq(url) {
205
- return { method: 'GET', url, headers: {} }
206
- }
207
-
208
- export function fakeRes() {
209
- return {
210
- status: 0,
211
- body: '',
212
- writeHead(status) {
213
- this.status = status
214
- },
215
- end(text) {
216
- this.body = text
217
- },
218
- json() {
219
- return JSON.parse(this.body || '{}')
220
- },
221
- }
222
- }
223
-
224
- export async function callRoute(harness, path, req) {
225
- const route = harness.routes.get(path)
226
- if (!route) throw new Error(`route not registered: ${path}`)
227
- const res = fakeRes()
228
- await route.handler(req, res)
229
- return res
230
- }
231
-
232
- export function withClock(fn) {
233
- const real = Date.now
234
- let offset = 0
235
- Date.now = () => real() + offset
236
- const advance = (ms) => {
237
- offset += ms
238
- }
239
- const restore = () => {
240
- Date.now = real
241
- }
242
- return { advance, restore, real }
243
- }
244
-
245
- export async function flush() {
246
- // 让 persist 写链落完
247
- await new Promise((resolve) => setImmediate(resolve))
248
- await new Promise((resolve) => setImmediate(resolve))
249
- }
1
+ /**
2
+ * 共享 mock 基座:纯内存 services 驱动 apply(),不起真宿主。
3
+ * 形状对齐 l3j3y3/dsh-progress-board test/host.test.mjs 的 createHarness。
4
+ */
5
+ import { apply } from '../index.js'
6
+
7
+ export const SESSION = 'session-union-test'
8
+
9
+ export function createHarness({ seed = {}, config = {}, withProjection = true, withProvide = true, storageless = false, llm = undefined, settings = undefined, cwd = '' } = {}) {
10
+ const records = { ...seed }
11
+ const routes = new Map()
12
+ const toolMap = new Map()
13
+ const commandMap = new Map()
14
+ const sections = new Map()
15
+ const contexts = new Map()
16
+ const listeners = []
17
+ const agentHooks = []
18
+ const provided = new Map()
19
+ const projections = new Map()
20
+
21
+ const unit = {
22
+ async loadAll() {
23
+ return { tables: { sessions: { ...records } } }
24
+ },
25
+ async putRecord(_table, key, record) {
26
+ records[key] = record
27
+ },
28
+ async close() {},
29
+ }
30
+
31
+ const storage = storageless
32
+ ? { backend: { names: () => [], get: () => ({}) } }
33
+ : {
34
+ backend: {
35
+ names: () => ['json'],
36
+ get: () => ({ kv: { open: async () => unit } }),
37
+ },
38
+ }
39
+
40
+ const webServer = {
41
+ register(route) {
42
+ routes.set(route.path, route)
43
+ return () => routes.delete(route.path)
44
+ },
45
+ }
46
+
47
+ const tools = {
48
+ register(definition, options) {
49
+ toolMap.set(definition.name, { definition, options })
50
+ return () => toolMap.delete(definition.name)
51
+ },
52
+ }
53
+
54
+ const commands = {
55
+ register(definition) {
56
+ commandMap.set(definition.name, definition)
57
+ return () => commandMap.delete(definition.name)
58
+ },
59
+ }
60
+
61
+ const systemPrompt = {}
62
+
63
+ const appends = []
64
+ const followups = []
65
+ // 宿主 inbox 形状(durable projection):followup 排进 nextTurn,被取走才算消费。
66
+ // 驱动器靠它判断「已有一轮还没跑」,避免补点火与 idle 边沿撞车连排两轮。
67
+ const nextTurn = []
68
+ const nextStep = []
69
+ const agent = {
70
+ id: SESSION,
71
+ status: 'idle',
72
+ inbox: {
73
+ nextTurn,
74
+ nextStep,
75
+ // 宿主 Inbox 契约:撤回一条排队消息(驱动器用它在重启清理孤儿轮次)。
76
+ remove(id) {
77
+ for (const list of [nextTurn, nextStep]) {
78
+ const i = list.findIndex((m) => m && m.id === id)
79
+ if (i >= 0) { list.splice(i, 1); return true }
80
+ }
81
+ return false
82
+ },
83
+ },
84
+ // /progress-goal 续轮驱动器经 agent.followup 排队;这里记录被派发的轮次消息。
85
+ followups,
86
+ followup(message) { followups.push(message); nextTurn.push(message) },
87
+ // v0.3.0:todo 镜像写通道。默认记录调用;测试可覆写 append 注入失败。
88
+ // v0.5.0:cwd 放在 session **顶层**(实测 shape,不是 header.cwd)——plan 扫描靠它。
89
+ session: {
90
+ id: SESSION,
91
+ ...(cwd ? { cwd } : {}),
92
+ append(type, data) { appends.push({ type, data }); return Promise.resolve() },
93
+ },
94
+ ctx: {
95
+ effect(fn) {
96
+ return fn()
97
+ },
98
+ systemPrompt: {
99
+ section({ name, text }) {
100
+ sections.set(name, text)
101
+ return () => sections.delete(name)
102
+ },
103
+ context({ name, text }) {
104
+ contexts.set(name, text)
105
+ return () => contexts.delete(name)
106
+ },
107
+ },
108
+ on(type, handler) {
109
+ agentHooks.push({ type, handler })
110
+ return () => {
111
+ const index = agentHooks.findIndex((h) => h.type === type && h.handler === handler)
112
+ if (index >= 0) agentHooks.splice(index, 1)
113
+ }
114
+ },
115
+ },
116
+ }
117
+
118
+ const agents = { list: () => [agent] }
119
+
120
+ const ctx = {
121
+ get(name) {
122
+ return { storage, webServer, systemPrompt, tools, agents, commands, llm, settings }[name]
123
+ },
124
+ on(type, handler) {
125
+ listeners.push({ type, handler })
126
+ return () => {
127
+ const index = listeners.findIndex((h) => h.type === type && h.handler === handler)
128
+ if (index >= 0) listeners.splice(index, 1)
129
+ }
130
+ },
131
+ effect(fn) {
132
+ const dispose = fn()
133
+ return () => {
134
+ if (typeof dispose === 'function') dispose()
135
+ }
136
+ },
137
+ inject(deps, callback) {
138
+ if (withProjection && deps.includes('sessionProjections')) {
139
+ callback({
140
+ sessionProjections: {
141
+ register(definition) {
142
+ projections.set(definition.key, definition)
143
+ return () => projections.delete(definition.key)
144
+ },
145
+ },
146
+ })
147
+ } else {
148
+ callback({})
149
+ }
150
+ },
151
+ }
152
+ if (withProvide) {
153
+ ctx.provide = (name, factory) => {
154
+ provided.set(name, factory())
155
+ return () => provided.delete(name)
156
+ }
157
+ }
158
+
159
+ return {
160
+ ctx,
161
+ config,
162
+ agent,
163
+ appends,
164
+ followups,
165
+ inbox: agent.inbox,
166
+ records,
167
+ routes,
168
+ toolMap,
169
+ commandMap,
170
+ sections,
171
+ contexts,
172
+ listeners,
173
+ agentHooks,
174
+ provided,
175
+ projections,
176
+ run: () => apply(ctx, config),
177
+ /** 模拟宿主 claim:把一条排队消息从 nextTurn 取走(consume=true 才算消费)。 */
178
+ claim(message) {
179
+ const i = nextTurn.indexOf(message)
180
+ if (i >= 0) nextTurn.splice(i, 1)
181
+ return message
182
+ },
183
+ /** 按 (session, {type, data}) 派发一条 session/event */
184
+ emit(type, data, session) {
185
+ // 默认 session 带顶层 cwd(实测 shape):plan 扫描靠事件流里的 session。
186
+ const target = session ?? (cwd ? { id: SESSION, cwd } : { id: SESSION })
187
+ for (const h of listeners.filter((l) => l.type === 'session/event')) h.handler(target, { type, data })
188
+ },
189
+ }
190
+ }
191
+
192
+ export function jsonReq(method, url, body) {
193
+ const payload = Buffer.from(JSON.stringify(body))
194
+ return {
195
+ method,
196
+ url,
197
+ headers: { 'content-type': 'application/json' },
198
+ async *[Symbol.asyncIterator]() {
199
+ yield payload
200
+ },
201
+ }
202
+ }
203
+
204
+ export function getReq(url) {
205
+ return { method: 'GET', url, headers: {} }
206
+ }
207
+
208
+ export function fakeRes() {
209
+ return {
210
+ status: 0,
211
+ body: '',
212
+ writeHead(status) {
213
+ this.status = status
214
+ },
215
+ end(text) {
216
+ this.body = text
217
+ },
218
+ json() {
219
+ return JSON.parse(this.body || '{}')
220
+ },
221
+ }
222
+ }
223
+
224
+ export async function callRoute(harness, path, req) {
225
+ const route = harness.routes.get(path)
226
+ if (!route) throw new Error(`route not registered: ${path}`)
227
+ const res = fakeRes()
228
+ await route.handler(req, res)
229
+ return res
230
+ }
231
+
232
+ export function withClock(fn) {
233
+ const real = Date.now
234
+ let offset = 0
235
+ Date.now = () => real() + offset
236
+ const advance = (ms) => {
237
+ offset += ms
238
+ }
239
+ const restore = () => {
240
+ Date.now = real
241
+ }
242
+ return { advance, restore, real }
243
+ }
244
+
245
+ export async function flush() {
246
+ // 让 persist 写链落完
247
+ await new Promise((resolve) => setImmediate(resolve))
248
+ await new Promise((resolve) => setImmediate(resolve))
249
+ }