@astrale-os/cli 0.6.3-alpha.0 → 0.8.0-alpha.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.
- package/README.md +3 -2
- package/dist/astrale.js +339 -242
- package/package.json +2 -3
- package/src/commands/__tests__/admin-instance.test.ts +49 -2
- package/src/commands/__tests__/domain-install-owned.test.ts +122 -0
- package/src/commands/__tests__/help-contract.test.ts +15 -0
- package/src/commands/domain/install.ts +15 -4
- package/src/commands/instance/active.ts +4 -7
- package/src/commands/instance/list.ts +4 -8
- package/src/commands/instance/status.ts +14 -9
- package/src/commands/instance/use.ts +13 -11
- package/src/commands/studio.ts +13 -0
- package/src/commands/view.ts +40 -13
- package/src/kernel/__tests__/client-owned-lookup.test.ts +51 -0
- package/src/kernel/client.ts +47 -20
- package/src/kernel/expand.ts +3 -22
- package/src/lib/__tests__/instance-candidates.test.ts +94 -19
- package/src/lib/__tests__/instance-target.test.ts +9 -1
- package/src/lib/__tests__/view-assets.test.ts +114 -0
- package/src/lib/__tests__/view-port-allocation.test.ts +82 -0
- package/src/lib/admin-instance.ts +27 -2
- package/src/lib/instance-candidates.ts +3 -3
- package/src/lib/instance-target.ts +1 -0
- package/src/lib/instance.ts +0 -11
- package/src/lib/login-flow.ts +4 -41
- package/src/lib/view/assets.ts +64 -0
- package/src/lib/view/port-allocation.ts +19 -0
- package/src/lib/view/server.ts +4 -37
- package/src/setup/__tests__/instance-step.test.ts +239 -0
- package/src/setup/steps/instance.ts +141 -61
- package/studio/client/dist/assets/index-BaqEuIQJ.js +109 -0
- package/studio/client/dist/assets/index-jRdExahh.css +1 -0
- package/studio/client/dist/index.html +2 -2
- package/studio/server/agent/ask.test.ts +74 -0
- package/studio/server/agent/ask.ts +20 -22
- package/studio/server/agent/bridge/client.test.ts +49 -0
- package/studio/server/agent/bridge/client.ts +37 -0
- package/studio/server/agent/bridge/grant.test.ts +62 -0
- package/studio/server/agent/bridge/grant.ts +84 -0
- package/studio/server/agent/bridge/routes.test.ts +151 -0
- package/studio/server/agent/bridge/routes.ts +148 -0
- package/studio/server/agent/{bridge-mcp.ts → bridge/stdio.ts} +27 -21
- package/studio/server/agent/conversation.test.ts +84 -0
- package/studio/server/agent/conversation.ts +110 -0
- package/studio/server/agent/{types.ts → harness/adapter.ts} +63 -12
- package/studio/server/agent/harness/claude/adapter.test.ts +338 -0
- package/studio/server/agent/harness/claude/adapter.ts +71 -0
- package/studio/server/agent/harness/claude/ask.ts +114 -0
- package/studio/server/agent/harness/claude/capabilities.ts +27 -0
- package/studio/server/agent/harness/claude/command.ts +63 -0
- package/studio/server/agent/harness/claude/events.ts +215 -0
- package/studio/server/agent/harness/claude/loadout.ts +132 -0
- package/studio/server/agent/harness/claude/mcp.test.ts +36 -0
- package/studio/server/agent/harness/claude/mcp.ts +41 -0
- package/studio/server/agent/harness/claude/skills.ts +43 -0
- package/studio/server/agent/harness/codex/adapter.test.ts +269 -0
- package/studio/server/agent/harness/codex/adapter.ts +116 -0
- package/studio/server/agent/harness/codex/ask.test.ts +176 -0
- package/studio/server/agent/harness/codex/ask.ts +181 -0
- package/studio/server/agent/harness/codex/command.test.ts +69 -0
- package/studio/server/agent/harness/codex/command.ts +43 -0
- package/studio/server/agent/harness/codex/events.test.ts +94 -0
- package/studio/server/agent/harness/codex/events.ts +132 -0
- package/studio/server/agent/harness/codex/exec.ts +113 -0
- package/studio/server/agent/harness/codex/loadout.ts +63 -0
- package/studio/server/agent/harness/codex/mcp.test.ts +32 -0
- package/studio/server/agent/harness/codex/mcp.ts +27 -0
- package/studio/server/agent/harness/codex/models.test.ts +167 -0
- package/studio/server/agent/harness/codex/models.ts +211 -0
- package/studio/server/agent/harness/codex/skills.ts +33 -0
- package/studio/server/agent/harness/gateway/config.test.ts +98 -0
- package/studio/server/{state/harness-gateway.ts → agent/harness/gateway/config.ts} +30 -8
- package/studio/server/agent/harness/gateway/token.test.ts +133 -0
- package/studio/server/agent/harness/gateway/token.ts +166 -0
- package/studio/server/agent/harness/mock/adapter.ts +190 -0
- package/studio/server/agent/harness/mock/domain-edit.ts +41 -0
- package/studio/server/agent/harness/process.test.ts +46 -0
- package/studio/server/agent/harness/process.ts +104 -0
- package/studio/server/agent/harness/registry.ts +37 -0
- package/studio/server/agent/harness/selection.test.ts +75 -0
- package/studio/server/agent/harness/selection.ts +77 -0
- package/studio/server/agent/harness/skills.test.ts +95 -0
- package/studio/server/agent/harness/skills.ts +100 -0
- package/studio/server/agent/layout.test.ts +95 -0
- package/studio/server/agent/notify.ts +12 -0
- package/studio/server/agent/{schema-map.ts → prompts/anchors.ts} +2 -2
- package/studio/server/agent/prompts/ask.ts +34 -0
- package/studio/server/agent/{prompt.ts → prompts/system.ts} +3 -107
- package/studio/server/agent/prompts/turn.ts +57 -0
- package/studio/server/agent/routes.test.ts +176 -0
- package/studio/server/agent/routes.ts +214 -0
- package/studio/server/agent/run/completion.ts +193 -0
- package/studio/server/agent/run/coordinator.test.ts +337 -0
- package/studio/server/agent/run/coordinator.ts +104 -0
- package/studio/server/agent/run/live-state.ts +73 -0
- package/studio/server/agent/run/preparation.ts +162 -0
- package/studio/server/agent/run/transcript.test.ts +47 -0
- package/studio/server/agent/run/transcript.ts +30 -0
- package/studio/server/agent/run/usage.test.ts +46 -0
- package/studio/server/{state → agent/run}/usage.ts +4 -4
- package/studio/server/agent/stream.ts +37 -0
- package/studio/server/agent/{session-id.ts → telemetry.ts} +1 -1
- package/studio/server/api-agent-loadout.test.ts +69 -0
- package/studio/server/api.ts +3 -155
- package/studio/server/index.ts +6 -5
- package/studio/server/state/comments.test.ts +29 -0
- package/studio/server/state/comments.ts +3 -3
- package/studio/server/state/settings.test.ts +78 -0
- package/studio/server/state/settings.ts +28 -1
- package/studio/shared/agent-effort.test.ts +12 -0
- package/studio/shared/agent-effort.ts +15 -0
- package/studio/shared/agent-models.test.ts +9 -0
- package/studio/shared/agent-models.ts +14 -0
- package/studio/shared/settings-values.test.ts +18 -0
- package/studio/shared/settings-values.ts +20 -0
- package/studio/shared/types.ts +58 -7
- package/src/connect-core.test.ts +0 -42
- package/src/connect-core.ts +0 -53
- package/studio/client/dist/assets/index-CyN5G8IA.js +0 -109
- package/studio/client/dist/assets/index-DKKMHBBC.css +0 -1
- package/studio/server/agent/bridge.ts +0 -188
- package/studio/server/agent/claude.ts +0 -678
- package/studio/server/agent/mock.ts +0 -186
- package/studio/server/agent/registry.ts +0 -29
- package/studio/server/agent/runner.ts +0 -487
- package/studio/server/state/harness-token.ts +0 -0
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { randomUUID } from 'node:crypto'
|
|
3
|
+
import {
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
mkdtempSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
readdirSync,
|
|
9
|
+
rmSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from 'node:fs'
|
|
12
|
+
import { tmpdir } from 'node:os'
|
|
13
|
+
import { join } from 'node:path'
|
|
14
|
+
|
|
15
|
+
import type { AgentRun } from '../../../shared/types'
|
|
16
|
+
|
|
17
|
+
import { registerDomain, unregisterDomain, type DomainHandle } from '../../domain'
|
|
18
|
+
import { readComments, upsertComment } from '../../state/comments'
|
|
19
|
+
import { updateSettings } from '../../state/settings'
|
|
20
|
+
import { handleBridge } from '../bridge/routes'
|
|
21
|
+
import { readConversation, saveConversation } from '../conversation'
|
|
22
|
+
import { setHarnessGateway } from '../harness/gateway/config'
|
|
23
|
+
import {
|
|
24
|
+
cancelRun,
|
|
25
|
+
getSnapshot,
|
|
26
|
+
isRunning,
|
|
27
|
+
resetConversation,
|
|
28
|
+
setSessionId,
|
|
29
|
+
submitRun,
|
|
30
|
+
} from './coordinator'
|
|
31
|
+
|
|
32
|
+
const roots: string[] = []
|
|
33
|
+
const domainIds: string[] = []
|
|
34
|
+
const envBefore = {
|
|
35
|
+
harness: process.env.DOMAIN_STUDIO_HARNESS,
|
|
36
|
+
mode: process.env.DOMAIN_STUDIO_MOCK_MODE,
|
|
37
|
+
delay: process.env.DOMAIN_STUDIO_MOCK_DELAY_MS,
|
|
38
|
+
model: process.env.DOMAIN_STUDIO_MOCK_EXPECT_MODEL,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function restoreEnv(name: keyof typeof envBefore, variable: string): void {
|
|
42
|
+
const value = envBefore[name]
|
|
43
|
+
if (value === undefined) delete process.env[variable]
|
|
44
|
+
else process.env[variable] = value
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
restoreEnv('harness', 'DOMAIN_STUDIO_HARNESS')
|
|
49
|
+
restoreEnv('mode', 'DOMAIN_STUDIO_MOCK_MODE')
|
|
50
|
+
restoreEnv('delay', 'DOMAIN_STUDIO_MOCK_DELAY_MS')
|
|
51
|
+
restoreEnv('model', 'DOMAIN_STUDIO_MOCK_EXPECT_MODEL')
|
|
52
|
+
while (domainIds.length) unregisterDomain(domainIds.pop()!)
|
|
53
|
+
while (roots.length) rmSync(roots.pop()!, { recursive: true, force: true })
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
function fixture(): DomainHandle {
|
|
57
|
+
const root = mkdtempSync(join(tmpdir(), `studio-runner-${randomUUID()}-`))
|
|
58
|
+
roots.push(root)
|
|
59
|
+
mkdirSync(join(root, 'schema'))
|
|
60
|
+
writeFileSync(join(root, 'astrale.config.ts'), 'export default {}\n')
|
|
61
|
+
writeFileSync(join(root, 'domain.ts'), 'export default {}\n')
|
|
62
|
+
writeFileSync(join(root, 'schema/index.ts'), 'export const Test = {}\n')
|
|
63
|
+
const handle = registerDomain(root)!
|
|
64
|
+
domainIds.push(handle.id)
|
|
65
|
+
return handle
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function bridgeFiles(root: string): string[] {
|
|
69
|
+
const dir = join(root, '.domain-studio', '.cache', 'agent')
|
|
70
|
+
if (!existsSync(dir)) return []
|
|
71
|
+
return readdirSync(dir).filter((file) => file.startsWith('bridge-'))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function waitForTerminal(domainId: string): Promise<AgentRun> {
|
|
75
|
+
for (let i = 0; i < 200; i++) {
|
|
76
|
+
const run = (await getSnapshot(domainId)).run
|
|
77
|
+
if (run && !['queued', 'running'].includes(run.status)) return run
|
|
78
|
+
await Bun.sleep(25)
|
|
79
|
+
}
|
|
80
|
+
throw new Error(`timed out waiting for ${domainId}`)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function useMock(mode = 'normal', delay = '0'): void {
|
|
84
|
+
process.env.DOMAIN_STUDIO_HARNESS = 'mock'
|
|
85
|
+
process.env.DOMAIN_STUDIO_MOCK_MODE = mode
|
|
86
|
+
process.env.DOMAIN_STUDIO_MOCK_DELAY_MS = delay
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
describe.serial('agent runner invariants', () => {
|
|
90
|
+
test('passes the selected harness model into the turn and its persisted prompt snapshot', async () => {
|
|
91
|
+
useMock()
|
|
92
|
+
const handle = fixture()
|
|
93
|
+
process.env.DOMAIN_STUDIO_MOCK_EXPECT_MODEL = 'mock-domain-model'
|
|
94
|
+
updateSettings(handle.root, { agentModels: { mock: 'mock-domain-model' } })
|
|
95
|
+
|
|
96
|
+
await submitRun(handle, () => {}, { message: 'use the selected model' })
|
|
97
|
+
const run = await waitForTerminal(handle.id)
|
|
98
|
+
|
|
99
|
+
expect(run.status).toBe('succeeded')
|
|
100
|
+
expect(run.prompt?.model).toBe('mock-domain-model')
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('reserves setup synchronously and starts exactly one turn', async () => {
|
|
104
|
+
useMock()
|
|
105
|
+
const handle = fixture()
|
|
106
|
+
|
|
107
|
+
const first = submitRun(handle, () => {}, { message: 'first' })
|
|
108
|
+
expect(isRunning(handle.id)).toBe(true)
|
|
109
|
+
expect(setSessionId(handle.id, 'hijack')).toBe(false)
|
|
110
|
+
expect(resetConversation(handle.id)).toBe(false)
|
|
111
|
+
|
|
112
|
+
const second = await submitRun(handle, () => {}, { message: 'second' })
|
|
113
|
+
expect(second).toEqual({ error: 'an agent run is already in progress for this domain' })
|
|
114
|
+
expect((await first).run?.status).toBe('running')
|
|
115
|
+
const completed = await waitForTerminal(handle.id)
|
|
116
|
+
expect(completed.status).toBe('succeeded')
|
|
117
|
+
expect(readConversation(handle.root, 'mock')).toMatchObject({
|
|
118
|
+
sessionId: 'mock-session',
|
|
119
|
+
turns: 1,
|
|
120
|
+
})
|
|
121
|
+
expect(isRunning(handle.id)).toBe(false)
|
|
122
|
+
expect(bridgeFiles(handle.root)).toEqual([])
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test('can cancel a reserved turn before the harness launches', async () => {
|
|
126
|
+
useMock()
|
|
127
|
+
const handle = fixture()
|
|
128
|
+
|
|
129
|
+
const pending = submitRun(handle, () => {}, { message: 'cancel setup' })
|
|
130
|
+
expect(isRunning(handle.id)).toBe(true)
|
|
131
|
+
expect(cancelRun(handle.id)).toBe(true)
|
|
132
|
+
expect(await pending).toEqual({ error: 'agent run canceled during setup' })
|
|
133
|
+
expect(isRunning(handle.id)).toBe(false)
|
|
134
|
+
expect(readConversation(handle.root, 'mock').sessionId).toBeUndefined()
|
|
135
|
+
expect(bridgeFiles(handle.root)).toEqual([])
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('notification failures cannot strand a running domain lock', async () => {
|
|
139
|
+
useMock()
|
|
140
|
+
const handle = fixture()
|
|
141
|
+
const started = await submitRun(
|
|
142
|
+
handle,
|
|
143
|
+
() => {
|
|
144
|
+
throw new Error('listener failed')
|
|
145
|
+
},
|
|
146
|
+
{ message: 'keep running' },
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
expect(started.run?.status).toBe('running')
|
|
150
|
+
expect((await waitForTerminal(handle.id)).status).toBe('succeeded')
|
|
151
|
+
expect(isRunning(handle.id)).toBe(false)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('recovers a stale selected-harness session as one fresh first turn', async () => {
|
|
155
|
+
useMock('resumefail')
|
|
156
|
+
const handle = fixture()
|
|
157
|
+
process.env.DOMAIN_STUDIO_MOCK_EXPECT_MODEL = 'mock-recovery-model'
|
|
158
|
+
updateSettings(handle.root, { agentModels: { mock: 'mock-recovery-model' } })
|
|
159
|
+
setHarnessGateway(handle.root, {
|
|
160
|
+
scope: 'domain',
|
|
161
|
+
config: {
|
|
162
|
+
enabled: true,
|
|
163
|
+
baseUrl: 'https://gateway.invalid/v1/models/test',
|
|
164
|
+
auth: { mode: 'host' },
|
|
165
|
+
},
|
|
166
|
+
})
|
|
167
|
+
saveConversation(handle.root, 'mock', {
|
|
168
|
+
sessionId: 'stale-session',
|
|
169
|
+
turns: 7,
|
|
170
|
+
updatedAt: 'before',
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
const started = await submitRun(handle, () => {}, { message: 'continue safely' })
|
|
174
|
+
expect(started.run?.harness).toBe('mock')
|
|
175
|
+
const run = await waitForTerminal(handle.id)
|
|
176
|
+
|
|
177
|
+
expect(run).toMatchObject({
|
|
178
|
+
status: 'succeeded',
|
|
179
|
+
sessionId: 'mock-session',
|
|
180
|
+
resumed: false,
|
|
181
|
+
})
|
|
182
|
+
expect(run.prompt).toMatchObject({
|
|
183
|
+
firstTurn: true,
|
|
184
|
+
resumed: false,
|
|
185
|
+
sessionId: undefined,
|
|
186
|
+
model: 'mock-recovery-model',
|
|
187
|
+
})
|
|
188
|
+
expect(run.events.map((event) => event.text)).toContain(
|
|
189
|
+
'previous conversation was no longer available — started a new one',
|
|
190
|
+
)
|
|
191
|
+
expect(readConversation(handle.root, 'mock')).toMatchObject({
|
|
192
|
+
sessionId: 'mock-session',
|
|
193
|
+
turns: 1,
|
|
194
|
+
})
|
|
195
|
+
expect(bridgeFiles(handle.root)).toEqual([])
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
test('does not replay a rejected resume after observable activity', async () => {
|
|
199
|
+
useMock('resumefailafterevent')
|
|
200
|
+
const handle = fixture()
|
|
201
|
+
saveConversation(handle.root, 'mock', {
|
|
202
|
+
sessionId: 'stale-session',
|
|
203
|
+
turns: 2,
|
|
204
|
+
updatedAt: 'before',
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
await submitRun(handle, () => {}, { message: 'continue once' })
|
|
208
|
+
const run = await waitForTerminal(handle.id)
|
|
209
|
+
|
|
210
|
+
expect(run).toMatchObject({
|
|
211
|
+
status: 'failed',
|
|
212
|
+
sessionId: undefined,
|
|
213
|
+
resumed: false,
|
|
214
|
+
error:
|
|
215
|
+
'the previous conversation was rejected after observable activity — Studio did not retry automatically to avoid duplicating work',
|
|
216
|
+
})
|
|
217
|
+
expect(run.events.filter((event) => event.kind === 'tool')).toHaveLength(1)
|
|
218
|
+
expect(readConversation(handle.root, 'mock').sessionId).toBeUndefined()
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test('cancellation skips partial replies, preserves the session, cleans up, and permits retry', async () => {
|
|
222
|
+
useMock('normal', '5000')
|
|
223
|
+
const handle = fixture()
|
|
224
|
+
const comment = upsertComment(handle.root, {
|
|
225
|
+
anchors: ['Test'],
|
|
226
|
+
anchorRefs: [{ ref: 'class.Test', kind: 'schema' }],
|
|
227
|
+
text: 'keep this open if canceled',
|
|
228
|
+
})
|
|
229
|
+
saveConversation(handle.root, 'mock', {
|
|
230
|
+
sessionId: 'stable-session',
|
|
231
|
+
turns: 2,
|
|
232
|
+
updatedAt: 'before',
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
const started = await submitRun(handle, () => {})
|
|
236
|
+
expect(started.run?.status).toBe('running')
|
|
237
|
+
const bridgeFile = bridgeFiles(handle.root)[0]!
|
|
238
|
+
const { token } = JSON.parse(
|
|
239
|
+
readFileSync(join(handle.root, '.domain-studio', '.cache', 'agent', bridgeFile), 'utf8'),
|
|
240
|
+
) as { token: string }
|
|
241
|
+
expect(cancelRun(handle.id)).toBe(true)
|
|
242
|
+
expect(
|
|
243
|
+
(await handleBridge(handle, 'threads', new Request('http://studio.test'), { token })).status,
|
|
244
|
+
).toBe(401)
|
|
245
|
+
const canceled = await waitForTerminal(handle.id)
|
|
246
|
+
|
|
247
|
+
expect(canceled.status).toBe('canceled')
|
|
248
|
+
expect(readConversation(handle.root, 'mock')).toMatchObject({
|
|
249
|
+
sessionId: 'stable-session',
|
|
250
|
+
turns: 2,
|
|
251
|
+
})
|
|
252
|
+
expect(readComments(handle.root).comments.find((item) => item.id === comment.id)).toMatchObject(
|
|
253
|
+
{
|
|
254
|
+
status: 'open',
|
|
255
|
+
thread: [{ role: 'user', text: 'keep this open if canceled' }],
|
|
256
|
+
},
|
|
257
|
+
)
|
|
258
|
+
expect(bridgeFiles(handle.root)).toEqual([])
|
|
259
|
+
expect(isRunning(handle.id)).toBe(false)
|
|
260
|
+
|
|
261
|
+
process.env.DOMAIN_STUDIO_MOCK_DELAY_MS = '0'
|
|
262
|
+
const retry = await submitRun(handle, () => {}, { message: 'retry' })
|
|
263
|
+
expect(retry.run?.status).toBe('running')
|
|
264
|
+
expect((await waitForTerminal(handle.id)).status).toBe('succeeded')
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
test('harness throws and malformed final replies fail without changing the prior session', async () => {
|
|
268
|
+
useMock('error')
|
|
269
|
+
const handle = fixture()
|
|
270
|
+
const comment = upsertComment(handle.root, {
|
|
271
|
+
anchors: ['Test'],
|
|
272
|
+
anchorRefs: [{ ref: 'class.Test', kind: 'schema' }],
|
|
273
|
+
text: 'do not merge failed output',
|
|
274
|
+
})
|
|
275
|
+
saveConversation(handle.root, 'mock', {
|
|
276
|
+
sessionId: 'stable-session',
|
|
277
|
+
turns: 4,
|
|
278
|
+
updatedAt: 'before',
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
await submitRun(handle, () => {})
|
|
282
|
+
const thrown = await waitForTerminal(handle.id)
|
|
283
|
+
expect(thrown).toMatchObject({
|
|
284
|
+
status: 'failed',
|
|
285
|
+
error: 'mock harness failure (test)',
|
|
286
|
+
})
|
|
287
|
+
expect(readConversation(handle.root, 'mock')).toMatchObject({
|
|
288
|
+
sessionId: 'stable-session',
|
|
289
|
+
turns: 4,
|
|
290
|
+
})
|
|
291
|
+
expect(
|
|
292
|
+
readComments(handle.root).comments.find((item) => item.id === comment.id)?.thread,
|
|
293
|
+
).toHaveLength(1)
|
|
294
|
+
expect(bridgeFiles(handle.root)).toEqual([])
|
|
295
|
+
|
|
296
|
+
process.env.DOMAIN_STUDIO_MOCK_MODE = 'badblock'
|
|
297
|
+
await submitRun(handle, () => {})
|
|
298
|
+
const malformed = await waitForTerminal(handle.id)
|
|
299
|
+
expect(malformed.status).toBe('failed')
|
|
300
|
+
expect(malformed.error).toContain('malformed JSON')
|
|
301
|
+
expect(readConversation(handle.root, 'mock')).toMatchObject({
|
|
302
|
+
sessionId: 'stable-session',
|
|
303
|
+
turns: 4,
|
|
304
|
+
})
|
|
305
|
+
expect(
|
|
306
|
+
readComments(handle.root).comments.find((item) => item.id === comment.id)?.thread,
|
|
307
|
+
).toHaveLength(1)
|
|
308
|
+
expect(bridgeFiles(handle.root)).toEqual([])
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
test('dedupes an echoed live reply while still merging distinct final detail', async () => {
|
|
312
|
+
useMock('liveandblockdifferent')
|
|
313
|
+
const handle = fixture()
|
|
314
|
+
const comment = upsertComment(handle.root, {
|
|
315
|
+
anchors: ['Test'],
|
|
316
|
+
anchorRefs: [{ ref: 'class.Test', kind: 'schema' }],
|
|
317
|
+
text: 'answer live and in the final block',
|
|
318
|
+
})
|
|
319
|
+
expect(readComments(handle.root).comments.map((item) => item.id)).toEqual([comment.id])
|
|
320
|
+
|
|
321
|
+
await submitRun(handle, () => {})
|
|
322
|
+
const run = await waitForTerminal(handle.id)
|
|
323
|
+
expect(run).toMatchObject({
|
|
324
|
+
status: 'succeeded',
|
|
325
|
+
liveReplies: 1,
|
|
326
|
+
merge: { merged: 1, closed: 0 },
|
|
327
|
+
})
|
|
328
|
+
const saved = readComments(handle.root).comments.find((item) => item.id === comment.id)!
|
|
329
|
+
expect(saved.status).toBe('closed')
|
|
330
|
+
expect(saved.thread.map((entry) => `${entry.role}:${entry.text}`)).toEqual([
|
|
331
|
+
'user:answer live and in the final block',
|
|
332
|
+
'author:Acknowledged. (mock agent)',
|
|
333
|
+
'author:Additional final detail. (mock agent)',
|
|
334
|
+
])
|
|
335
|
+
expect(bridgeFiles(handle.root)).toEqual([])
|
|
336
|
+
})
|
|
337
|
+
})
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentRun,
|
|
3
|
+
AgentRunSnapshot,
|
|
4
|
+
ConversationInfo,
|
|
5
|
+
StudioEvent,
|
|
6
|
+
} from '../../../shared/types'
|
|
7
|
+
import type { DomainHandle } from '../../domain'
|
|
8
|
+
|
|
9
|
+
import { getDomain } from '../../domain'
|
|
10
|
+
import {
|
|
11
|
+
clearConversation,
|
|
12
|
+
conversationInfo,
|
|
13
|
+
sessionInfo,
|
|
14
|
+
setConversationSession,
|
|
15
|
+
} from '../conversation'
|
|
16
|
+
import { getHarness } from '../harness/selection'
|
|
17
|
+
import { emitStudioEvent } from '../notify'
|
|
18
|
+
import { completeRun } from './completion'
|
|
19
|
+
import {
|
|
20
|
+
attachCancellation,
|
|
21
|
+
cancelActiveRun,
|
|
22
|
+
currentRun,
|
|
23
|
+
hydrateRun,
|
|
24
|
+
isRunActive,
|
|
25
|
+
releasePreparation,
|
|
26
|
+
reserveRun,
|
|
27
|
+
setCurrentRun,
|
|
28
|
+
} from './live-state'
|
|
29
|
+
import { prepareRun, type SubmitOpts } from './preparation'
|
|
30
|
+
import { persistRun } from './transcript'
|
|
31
|
+
|
|
32
|
+
function conversationOf(root: string): ConversationInfo {
|
|
33
|
+
const harness = getHarness(root)
|
|
34
|
+
return conversationInfo(root, harness.id)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function isRunning(domainId: string): boolean {
|
|
38
|
+
return isRunActive(domainId)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function getSnapshot(domainId: string): Promise<AgentRunSnapshot> {
|
|
42
|
+
const handle = getDomain(domainId)
|
|
43
|
+
const harness = getHarness(handle?.root ?? process.cwd())
|
|
44
|
+
if (handle) hydrateRun(domainId, handle.root)
|
|
45
|
+
return {
|
|
46
|
+
harness: harness.id,
|
|
47
|
+
available: await harness.isAvailable(),
|
|
48
|
+
run: currentRun(domainId) ?? null,
|
|
49
|
+
conversation: handle ? conversationOf(handle.root) : { active: false, turns: 0 },
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function cancelRun(domainId: string): boolean {
|
|
54
|
+
return cancelActiveRun(domainId)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function resetConversation(domainId: string): boolean {
|
|
58
|
+
const handle = getDomain(domainId)
|
|
59
|
+
if (!handle || isRunActive(domainId)) return false
|
|
60
|
+
clearConversation(handle.root, getHarness(handle.root).id)
|
|
61
|
+
return true
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function getSessionId(domainId: string): {
|
|
65
|
+
sessionId: string | null
|
|
66
|
+
turns: number
|
|
67
|
+
harness?: string
|
|
68
|
+
} {
|
|
69
|
+
const handle = getDomain(domainId)
|
|
70
|
+
if (!handle) return { sessionId: null, turns: 0 }
|
|
71
|
+
return sessionInfo(handle.root, getHarness(handle.root).id)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function setSessionId(domainId: string, sessionId: string): boolean {
|
|
75
|
+
const handle = getDomain(domainId)
|
|
76
|
+
if (!handle || isRunActive(domainId)) return false
|
|
77
|
+
const harness = getHarness(handle.root)
|
|
78
|
+
const trimmed = sessionId.trim()
|
|
79
|
+
if (!trimmed) clearConversation(handle.root, harness.id)
|
|
80
|
+
else setConversationSession(handle.root, harness.id, trimmed)
|
|
81
|
+
return true
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function submitRun(
|
|
85
|
+
handle: DomainHandle,
|
|
86
|
+
notify: (event: StudioEvent) => void,
|
|
87
|
+
options?: SubmitOpts,
|
|
88
|
+
): Promise<{ run?: AgentRun; error?: string }> {
|
|
89
|
+
const controller = reserveRun(handle.id)
|
|
90
|
+
if (!controller) return { error: 'an agent run is already in progress for this domain' }
|
|
91
|
+
try {
|
|
92
|
+
const result = await prepareRun(handle, notify, controller, options)
|
|
93
|
+
if ('error' in result) return result
|
|
94
|
+
const { prepared } = result
|
|
95
|
+
attachCancellation(handle.id, controller, prepared.bridge.dispose)
|
|
96
|
+
setCurrentRun(prepared.run)
|
|
97
|
+
persistRun(prepared.root, prepared.run)
|
|
98
|
+
emitStudioEvent(notify, { type: 'agent-run', domainId: handle.id, run: prepared.run })
|
|
99
|
+
void completeRun(prepared, controller, notify)
|
|
100
|
+
return { run: prepared.run }
|
|
101
|
+
} finally {
|
|
102
|
+
releasePreparation(handle.id, controller)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { AgentRun } from '../../../shared/types'
|
|
2
|
+
|
|
3
|
+
import { readLastRun } from './transcript'
|
|
4
|
+
|
|
5
|
+
const runs = new Map<string, AgentRun>()
|
|
6
|
+
const controllers = new Map<string, AbortController>()
|
|
7
|
+
const cancellation = new Map<string, () => void>()
|
|
8
|
+
const starting = new Set<string>()
|
|
9
|
+
const hydrated = new Set<string>()
|
|
10
|
+
|
|
11
|
+
export function currentRun(domainId: string): AgentRun | undefined {
|
|
12
|
+
return runs.get(domainId)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function setCurrentRun(run: AgentRun): void {
|
|
16
|
+
runs.set(run.domainId, run)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function isRunActive(domainId: string): boolean {
|
|
20
|
+
const status = runs.get(domainId)?.status
|
|
21
|
+
return starting.has(domainId) || status === 'running' || status === 'queued'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Reserve a domain synchronously before asynchronous run preparation begins. */
|
|
25
|
+
export function reserveRun(domainId: string): AbortController | null {
|
|
26
|
+
if (isRunActive(domainId)) return null
|
|
27
|
+
const controller = new AbortController()
|
|
28
|
+
starting.add(domainId)
|
|
29
|
+
controllers.set(domainId, controller)
|
|
30
|
+
return controller
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function releasePreparation(domainId: string, controller: AbortController): void {
|
|
34
|
+
starting.delete(domainId)
|
|
35
|
+
if (controllers.get(domainId) === controller && !isRunActive(domainId)) {
|
|
36
|
+
controllers.delete(domainId)
|
|
37
|
+
cancellation.delete(domainId)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function attachCancellation(
|
|
42
|
+
domainId: string,
|
|
43
|
+
controller: AbortController,
|
|
44
|
+
cancel: () => void,
|
|
45
|
+
): void {
|
|
46
|
+
if (controllers.get(domainId) === controller) cancellation.set(domainId, cancel)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function releaseController(domainId: string, controller: AbortController): void {
|
|
50
|
+
if (controllers.get(domainId) === controller) {
|
|
51
|
+
controllers.delete(domainId)
|
|
52
|
+
cancellation.delete(domainId)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function cancelActiveRun(domainId: string): boolean {
|
|
57
|
+
const controller = controllers.get(domainId)
|
|
58
|
+
if (!controller) return false
|
|
59
|
+
controller.abort()
|
|
60
|
+
try {
|
|
61
|
+
cancellation.get(domainId)?.()
|
|
62
|
+
} catch {
|
|
63
|
+
/* cancellation must still succeed */
|
|
64
|
+
}
|
|
65
|
+
return true
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function hydrateRun(domainId: string, root: string): void {
|
|
69
|
+
if (hydrated.has(domainId) || runs.has(domainId)) return
|
|
70
|
+
hydrated.add(domainId)
|
|
71
|
+
const last = readLastRun(domainId, root)
|
|
72
|
+
if (last) runs.set(domainId, last)
|
|
73
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
AgentPromptSnapshot,
|
|
5
|
+
AgentRun,
|
|
6
|
+
AgentEffort,
|
|
7
|
+
Comment,
|
|
8
|
+
StudioEvent,
|
|
9
|
+
StudioSettings,
|
|
10
|
+
} from '../../../shared/types'
|
|
11
|
+
import type { DomainHandle } from '../../domain'
|
|
12
|
+
import type { Bridge } from '../bridge/grant'
|
|
13
|
+
import type { AgentHarness } from '../harness/adapter'
|
|
14
|
+
|
|
15
|
+
import { getBundle } from '../../cache'
|
|
16
|
+
import { readComments } from '../../state/comments'
|
|
17
|
+
import { readContext } from '../../state/context'
|
|
18
|
+
import { listDocuments } from '../../state/documents'
|
|
19
|
+
import { refreshAuto } from '../../state/handoff'
|
|
20
|
+
import { startBridge } from '../bridge/grant'
|
|
21
|
+
import { readConversation } from '../conversation'
|
|
22
|
+
import { getHarness, resolveHarnessConfiguration } from '../harness/selection'
|
|
23
|
+
import { buildSystemPrompt } from '../prompts/system'
|
|
24
|
+
import { buildResumePrompt, buildTurnPrompt } from '../prompts/turn'
|
|
25
|
+
import { studioSessionId } from '../telemetry'
|
|
26
|
+
|
|
27
|
+
export interface SubmitOpts {
|
|
28
|
+
message?: string
|
|
29
|
+
resume?: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface PreparedRun {
|
|
33
|
+
domainId: string
|
|
34
|
+
root: string
|
|
35
|
+
harness: AgentHarness
|
|
36
|
+
settings: StudioSettings
|
|
37
|
+
session: ReturnType<typeof readConversation>
|
|
38
|
+
resume?: string
|
|
39
|
+
schemaHash: string
|
|
40
|
+
model?: string
|
|
41
|
+
effort?: AgentEffort
|
|
42
|
+
harnessEnv: Record<string, string>
|
|
43
|
+
bridge: Bridge
|
|
44
|
+
run: AgentRun
|
|
45
|
+
promptSnapshot(sessionId: string | undefined, firstTurn: boolean): AgentPromptSnapshot
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type PreparationResult = { prepared: PreparedRun } | { error: string }
|
|
49
|
+
|
|
50
|
+
function awaitingThreads(root: string): Comment[] {
|
|
51
|
+
return readComments(root).comments.filter(
|
|
52
|
+
(comment) => comment.status === 'open' && comment.thread.at(-1)?.role !== 'author',
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Gather and freeze every input required to start one agent run. */
|
|
57
|
+
export async function prepareRun(
|
|
58
|
+
handle: DomainHandle,
|
|
59
|
+
notify: (event: StudioEvent) => void,
|
|
60
|
+
controller: AbortController,
|
|
61
|
+
options?: SubmitOpts,
|
|
62
|
+
): Promise<PreparationResult> {
|
|
63
|
+
const domainId = handle.id
|
|
64
|
+
const root = handle.root
|
|
65
|
+
const harness = getHarness(root)
|
|
66
|
+
const available = await harness.isAvailable(controller.signal)
|
|
67
|
+
if (controller.signal.aborted) return { error: 'agent run canceled during setup' }
|
|
68
|
+
if (!available) return { error: `${harness.label} is not available on this machine` }
|
|
69
|
+
|
|
70
|
+
const session = readConversation(root, harness.id)
|
|
71
|
+
const resume = session.sessionId
|
|
72
|
+
const bareResume = options?.resume === true && !!resume
|
|
73
|
+
const awaiting = awaitingThreads(root)
|
|
74
|
+
const message = (options?.message ?? '').trim()
|
|
75
|
+
if (!bareResume && awaiting.length === 0 && !message)
|
|
76
|
+
return { error: 'nothing to send — type an instruction or open a thread' }
|
|
77
|
+
|
|
78
|
+
await refreshAuto(handle).catch(() => {})
|
|
79
|
+
if (controller.signal.aborted) return { error: 'agent run canceled during setup' }
|
|
80
|
+
const bundle = await getBundle(domainId)
|
|
81
|
+
if (controller.signal.aborted) return { error: 'agent run canceled during setup' }
|
|
82
|
+
|
|
83
|
+
const schemaHash = bundle?.schemaHash ?? ''
|
|
84
|
+
const context = readContext(root)
|
|
85
|
+
const documents = listDocuments(root)
|
|
86
|
+
const configuration = await resolveHarnessConfiguration(root, harness)
|
|
87
|
+
if (!configuration.ok) return { error: `model gateway auth failed — ${configuration.error}` }
|
|
88
|
+
const { settings, model, effort, env } = configuration.configuration
|
|
89
|
+
if (controller.signal.aborted) return { error: 'agent run canceled during setup' }
|
|
90
|
+
|
|
91
|
+
const harnessEnv = { ...env, ASTRALE_SESSION: studioSessionId(domainId) }
|
|
92
|
+
const bridge = startBridge(handle, notify)
|
|
93
|
+
const makeTurn = (firstTurn: boolean) =>
|
|
94
|
+
bareResume && !firstTurn
|
|
95
|
+
? buildResumePrompt()
|
|
96
|
+
: buildTurnPrompt({
|
|
97
|
+
origin: bundle?.overlay.origin ?? domainId,
|
|
98
|
+
root,
|
|
99
|
+
schemaHash,
|
|
100
|
+
awaitingThreads: awaiting,
|
|
101
|
+
userContext: context.user,
|
|
102
|
+
autoContext: context.auto.filter((item) => item.includeInHandoff),
|
|
103
|
+
documents,
|
|
104
|
+
firstTurn,
|
|
105
|
+
message,
|
|
106
|
+
ir: bundle?.ir ?? null,
|
|
107
|
+
overlay: bundle?.overlay,
|
|
108
|
+
})
|
|
109
|
+
const systemPrompt = buildSystemPrompt({ bridge: bridge.enabled })
|
|
110
|
+
const promptSnapshot = (
|
|
111
|
+
sessionId: string | undefined,
|
|
112
|
+
firstTurn: boolean,
|
|
113
|
+
): AgentPromptSnapshot => ({
|
|
114
|
+
createdAt: new Date().toISOString(),
|
|
115
|
+
systemPrompt,
|
|
116
|
+
turnPrompt: makeTurn(firstTurn),
|
|
117
|
+
firstTurn,
|
|
118
|
+
resumed: !!sessionId,
|
|
119
|
+
sessionId,
|
|
120
|
+
model,
|
|
121
|
+
effort,
|
|
122
|
+
access: settings.agentAccess,
|
|
123
|
+
mcpTools: bridge.mcpServers.flatMap((server) => server.enabledTools ?? []),
|
|
124
|
+
})
|
|
125
|
+
const run: AgentRun = {
|
|
126
|
+
id: randomUUID(),
|
|
127
|
+
domainId,
|
|
128
|
+
harness: harness.id,
|
|
129
|
+
status: 'running',
|
|
130
|
+
createdAt: new Date().toISOString(),
|
|
131
|
+
summary: bareResume
|
|
132
|
+
? 'continuing after interruption'
|
|
133
|
+
: message
|
|
134
|
+
? message.slice(0, 60) + (message.length > 60 ? '…' : '')
|
|
135
|
+
: awaiting.length === 1
|
|
136
|
+
? '1 open thread'
|
|
137
|
+
: `${awaiting.length} open threads`,
|
|
138
|
+
targetCommentIds: awaiting.map((comment) => comment.id),
|
|
139
|
+
events: [],
|
|
140
|
+
sessionId: resume,
|
|
141
|
+
resumed: !!resume,
|
|
142
|
+
prompt: promptSnapshot(resume, !resume),
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
prepared: {
|
|
147
|
+
domainId,
|
|
148
|
+
root,
|
|
149
|
+
harness,
|
|
150
|
+
settings,
|
|
151
|
+
session,
|
|
152
|
+
resume,
|
|
153
|
+
schemaHash,
|
|
154
|
+
model,
|
|
155
|
+
effort,
|
|
156
|
+
harnessEnv,
|
|
157
|
+
bridge,
|
|
158
|
+
run,
|
|
159
|
+
promptSnapshot,
|
|
160
|
+
},
|
|
161
|
+
}
|
|
162
|
+
}
|