@tangle-network/create-agent-app 0.63.5 → 0.63.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangle-network/create-agent-app",
3
- "version": "0.63.5",
3
+ "version": "0.63.7",
4
4
  "description": "Create a tested Tangle agent application with either a tool loop or a complete chat interface.",
5
5
  "keywords": [
6
6
  "tangle",
@@ -8,7 +8,7 @@
8
8
  * turn `createChatTurnRoutes` — body validation, turn identity,
9
9
  * the default turn-buffer tap (replay after a drop), user and
10
10
  * assistant rows persisted with typed parts + usage receipt
11
- * producer the hybrid lane from `src/sandbox.ts`: the Router answers
11
+ * producer the shared default lane: the Router answers
12
12
  * first and escalates to the box through `sandbox_run`
13
13
  * uploads `createUploadRoute` — small files inline (`data:` URI),
14
14
  * large files into the sandbox workspace by path
@@ -23,7 +23,6 @@
23
23
  * later: `@tangle-network/agent-app/teams` + `createChatTables({ workspaceTable })`.
24
24
  */
25
25
 
26
- import { createDurableTurnLock } from '@tangle-network/agent-app/turn-stream'
27
26
  import { config } from '../agent.config'
28
27
  import { createAppAuth, type AppAuth } from '@tangle-network/agent-app/app-auth'
29
28
  import {
@@ -48,7 +47,7 @@ import {
48
47
  import { drizzle } from 'drizzle-orm/d1'
49
48
  import { accounts, messages, sessions, threads, users, verifications } from './db/schema'
50
49
  import type { AppEnv } from './env'
51
- import { appSlug, createHybridLane, createPrewarmRoute, resolveSidecarConnection, resolveUploadSink } from './sandbox'
50
+ import { appSlug, createSandboxShell, resolveSidecarConnection, resolveUploadSink } from './sandbox'
52
51
 
53
52
  export interface ChatAppOverrides {
54
53
  /** Test seam: an in-memory drizzle db (the e2e test runs the REAL
@@ -123,12 +122,14 @@ export function buildChatApp(env: AppEnv, overrides: ChatAppOverrides = {}): Cha
123
122
  const session = await requireUser(args.request)
124
123
  if (!session.ok) return session
125
124
  const { user } = session.value
126
- // `turn` and `running` are thread-scoped and MUST verify ownership: both name
125
+ // `turn` and thread-scoped `running` MUST verify ownership: both name
127
126
  // a thread the caller supplies (POST body for turn, `?threadId=` for running,
128
127
  // whose response enumerates that thread's live turn ids). Inaccessible reads
129
128
  // are indistinguishable from missing ones — a cross-workspace probe must not
130
129
  // learn the thread exists.
131
- if (args.intent === 'turn' || args.intent === 'running') {
130
+ // Page-open prewarm carries no thread yet. This single-user template may
131
+ // warm only the authenticated member's own workspace lock in that case.
132
+ if (args.intent === 'turn' || (args.intent === 'running' && args.threadId)) {
132
133
  const threadId = args.intent === 'turn' ? String(args.body?.threadId ?? '') : args.threadId
133
134
  const thread = threadId ? await store.getThread(threadId) : null
134
135
  if (!thread || thread.workspaceId !== user.id) return { ok: false, response: notFound() }
@@ -138,42 +139,13 @@ export function buildChatApp(env: AppEnv, overrides: ChatAppOverrides = {}): Cha
138
139
  return { ok: true, tenantId: user.id, userId: user.id, context: undefined }
139
140
  }
140
141
 
141
- const durableLock = env.TURN_STREAM && env.TURN_STREAM_AUTH_SECRET
142
- ? createDurableTurnLock<void>({ namespace: env.TURN_STREAM, auth: { secret: env.TURN_STREAM_AUTH_SECRET }, locationHint: env.D1_REGION, scopeOf: () => 'workspace',
143
- onRefused: active => Response.json({ error: { code: 'workspace_turn_in_flight', message: `Execution ${active.executionId} in thread ${active.threadId} still owns this workspace. If observation was lost, the deployment operator must verify that exact backend execution stopped and use a fenced reconciliation adapter; elapsed time or another UUID is not recovery.`, executionId: active.executionId, threadId: active.threadId } }, { status: 409 }),
142
+ const markBackendTerminal = async (input: { userMessageId?: string | null; backendTerminal?: boolean }) => {
143
+ if (input.userMessageId && input.backendTerminal) await store.updateMessage(input.userMessageId, {
144
+ extras: { backendTerminal: true },
144
145
  })
145
- : null
146
- interface Admission { handle: unknown; started: boolean; producer?: ChatTurnRouteProducer; userMessageId?: string }
147
- const admissions = new Map<string, Admission>()
148
- const lane = overrides.produce ? null : createHybridLane(env)
149
- // Native Sandbox execution keeps the backend-terminal ownership check.
150
- // Router option data goes directly to the shared route before its lock.
151
- const nativeProduce = async (args: ChatTurnProduceArgs<void>) => {
152
- const admission = admissions.get(args.executionId)
153
- if (!admission) throw new Error('Turn admission is unavailable')
154
- admission.started = true
155
- admission.producer = await (overrides.produce ?? lane!.produce.sandbox)(args)
156
- return admission.producer
157
146
  }
158
147
  const routes = createChatTurnRoutes<void>({
159
- turnLock: {
160
- acquire: async args => {
161
- if (!durableLock) return { acquired: false, response: Response.json({ error: 'Durable turn locking is not configured', code: 'chat.lock_not_configured' }, { status: 503 }) }
162
- const acquired = await durableLock.acquire(args)
163
- if (!acquired.acquired) return acquired
164
- admissions.set(args.executionId, { handle: acquired.handle, started: false })
165
- return { acquired: true, handle: args.executionId }
166
- },
167
- release: async handle => {
168
- const id = String(handle)
169
- const admission = admissions.get(id)
170
- admissions.delete(id)
171
- if (!admission) return
172
- if (admission.started && !admission.producer?.terminalObserved?.()) return
173
- if (admission.userMessageId) await store.updateMessage(admission.userMessageId, { extras: { backendTerminal: true } })
174
- await durableLock?.release(admission.handle)
175
- },
176
- },
148
+ env,
177
149
  // `projectId` also names the Intelligence project every turn's trace goes to (first_text_ms included). The route
178
150
  // reads the Sandbox product key and warns in the Worker log when an export fails; pass `turnTrace: false` only
179
151
  // with an exemption in signoff.config.mjs.
@@ -181,16 +153,17 @@ export function buildChatApp(env: AppEnv, overrides: ChatAppOverrides = {}): Cha
181
153
  authorize,
182
154
  store,
183
155
  turnStore,
184
- // The box starts the moment the caller is admitted when the ask needs a computer.
185
- ...(lane ? { afterAuthorize: lane.afterAuthorize } : {}),
156
+ produce: overrides.produce ?? {
157
+ shell: createSandboxShell(env), model: config.model.default,
158
+ systemPrompt: config.systemPrompt, harness: config.harness,
159
+ effort: config.model.effort, interactions: config.interactions,
160
+ },
186
161
  beforeTurn: async args => {
187
- const admission = admissions.get(args.executionId)
188
- if (args.userMessageId) {
189
- if (admission) admission.userMessageId = args.userMessageId
190
- await store.updateMessage(args.userMessageId, { extras: { turnStreamId: args.turnStreamId, backendTerminal: false } })
191
- }
162
+ if (args.userMessageId) await store.updateMessage(args.userMessageId, {
163
+ extras: { turnStreamId: args.turnStreamId, backendTerminal: false },
164
+ })
192
165
  },
193
- produce: overrides.produce ? nativeProduce : { ...lane!.produce, sandbox: nativeProduce },
166
+ lifecycle: { onTurnComplete: markBackendTerminal, onTurnError: markBackendTerminal },
194
167
  interactions: {
195
168
  resolveConnection: async ({ request, intent, body }) => {
196
169
  const session = await requireUser(request)
@@ -262,17 +235,12 @@ export function buildChatApp(env: AppEnv, overrides: ChatAppOverrides = {}): Cha
262
235
  return Response.json({ thread, messages: await store.listMessages(params.threadId) })
263
236
  }
264
237
 
265
- const prewarmRoute = createPrewarmRoute(env, async request => {
266
- const session = await requireUser(request)
267
- return session.ok ? session.value.user.id : null
268
- })
269
-
270
238
  return {
271
239
  auth,
272
240
  store,
273
241
  turnStore,
274
242
  routes: { ...routes, createThread, listThreads, threadMessages },
275
243
  upload,
276
- prewarm: request => prewarmRoute(request),
244
+ prewarm: routes.prewarm,
277
245
  }
278
246
  }
@@ -1,14 +1,12 @@
1
1
  /**
2
- * src/sandbox.ts — the config-driven sandbox lane. CODE, not data.
2
+ * src/sandbox.ts — product sandbox policy and file/interaction access.
3
3
  *
4
- * Everything agent-shaped runs in a Tangle sandbox (a full agent harness:
5
- * skills, tools, bash, MCP) reached through `@tangle-network/agent-app`'s
6
- * sandbox helpers. This file turns `agent.config.ts` + env into the three
7
- * seams the chat vertical needs:
4
+ * `createChatTurnRoutes` owns Router-first hybrid execution and sandbox fallback.
5
+ * This file supplies the product policy and the sandbox access that uploads
6
+ * and interaction answers need:
8
7
  *
9
- * - `createSandboxProduce` — the turn producer (`createChatTurnRoutes`'s
10
- * `produce` seam): resolve the workspace box, stream the prompt, bridge
11
- * raw sidecar events through `createSandboxChatProducer`.
8
+ * - `createSandboxShell` — product provisioning, profile and credential policy.
9
+ * `createChatTurnRoutes` assembles the lane from it.
12
10
  * - `resolveUploadSink` — where >inline-cap uploads land (`box.fs`).
13
11
  * - `resolveSidecarConnection` — where interaction answers go.
14
12
  *
@@ -18,30 +16,17 @@
18
16
 
19
17
  import { config } from '../agent.config'
20
18
  import {
21
- createSandboxChatProducer,
22
- normalizeChatPromptForSandbox,
23
- startHybridBox,
24
- turnCarriesFiles,
25
- type ChatTurnAfterAuthorizeArgs,
26
- type ChatTurnProduceArgs,
27
- type ChatTurnRouteProducer,
28
- type ChatTurnHybridConfiguration,
29
19
  type SandboxUploadSink,
30
20
  } from '@tangle-network/agent-app/chat-routes'
31
21
  import type { SidecarInteractionsConnection } from '@tangle-network/agent-app/interactions'
32
22
  import {
33
23
  createD1PrewarmClaimStore,
34
- createSandboxPrewarmer,
35
- createWorkspacePrewarmRoute,
36
24
  ensureWorkspaceSandbox,
37
25
  peekWorkspaceSandbox,
38
26
  runForegroundSandboxSingleFlight,
39
- sandboxPromptRunner,
40
- streamSandboxPrompt,
41
27
  type SandboxRuntimeConfig,
42
28
  } from '@tangle-network/agent-app/sandbox'
43
29
  import type { TurnTrace } from '@tangle-network/agent-app/turn-trace'
44
- import type { SandboxInstance } from '@tangle-network/sandbox'
45
30
  import type { AppEnv } from './env'
46
31
 
47
32
  /** Lowercased, non-alphanumerics collapsed: box names + projectId. */
@@ -105,176 +90,6 @@ async function ensureForegroundWorkspaceSandbox(
105
90
  })
106
91
  }
107
92
 
108
- /**
109
- * The `produce` seam for `createChatTurnRoutes`: one call per turn. The
110
- * chat thread id doubles as the agent session id, so follow-up turns land in
111
- * the same sidecar session and keep its context.
112
- */
113
- export function createSandboxProduce(env: AppEnv) {
114
- const shell = createSandboxShell(env)
115
- return async ({
116
- body,
117
- identity,
118
- prompt,
119
- executionId,
120
- executionLimits,
121
- trace,
122
- }: ChatTurnProduceArgs<void>): Promise<ChatTurnRouteProducer> => {
123
- const box = await ensureForegroundWorkspaceSandbox(env, shell, {
124
- workspaceId: identity.tenantId,
125
- userId: identity.userId,
126
- }, trace)
127
- const model = body.model ?? env.MODEL_NAME ?? config.model.default
128
- return createSandboxChatProducer({
129
- model,
130
- errorNotice: true,
131
- // A detached dispatch can keep doing hidden work after transport loss.
132
- // Do not turn observation failure into another model dispatch.
133
- modelFailover: false,
134
- openEvents: ({ model: attemptModel, signal }) => {
135
- return streamSandboxPrompt(shell, box, normalizeChatPromptForSandbox(prompt), {
136
- sessionId: identity.sessionId,
137
- executionId,
138
- turnId: executionId,
139
- model: attemptModel,
140
- signal,
141
- ...(trace ? { trace } : {}),
142
- // A zero reasoning allowance maps to the profile's supported
143
- // no-reasoning control. Positive limits use token ceilings below.
144
- effort: executionLimits?.maxReasoningTokens === 0
145
- ? 'none'
146
- : (body.effort ?? config.model.effort),
147
- ...(executionLimits?.maxOutputTokens
148
- ? { maxOutputTokens: executionLimits.maxOutputTokens }
149
- : {}),
150
- ...(executionLimits?.maxReasoningTokens
151
- ? { maxReasoningTokens: executionLimits.maxReasoningTokens }
152
- : {}),
153
- // Gateway output is inclusive of reasoning. Keep the same total
154
- // ceiling even when a separate reasoning subset was authorized.
155
- ...(executionLimits?.maxOutputTokens !== undefined
156
- ? { maxTotalOutputTokens: executionLimits.maxOutputTokens }
157
- : {}),
158
- harness: config.harness,
159
- systemPrompt: config.systemPrompt,
160
- // Durable by default: the run keeps executing server-side if the operator
161
- // refreshes, closes the tab, or the Worker restarts mid-turn. On reopen
162
- // the client re-attaches via GET /api/chat/running → /api/chat/replay, so
163
- // nothing is lost. Drop this only for a run that should stop when the tab
164
- // closes (e.g. a throwaway preview).
165
- detach: true,
166
- interactions: config.interactions,
167
- })
168
- },
169
- })
170
- }
171
- }
172
-
173
- /** The Router's OpenAI-compatible base URL (`…/v1`), or undefined for the platform default. */
174
- function routerBaseUrl(env: AppEnv): string | undefined {
175
- const url = env.TANGLE_ROUTER_URL?.trim().replace(/\/+$/, '')
176
- if (!url) return undefined
177
- return url.endsWith('/v1') ? url : `${url}/v1`
178
- }
179
-
180
- /**
181
- * The default chat lane: the model answers first through the Tangle Router and
182
- * escalates to the workspace box through one `sandbox_run` call when the work
183
- * needs a computer (`createHybridTurnProducer`). The box starts at admission
184
- * when the ask needs it (`afterAuthorize`), or when the model names
185
- * `sandbox_run`, and that one start serves the call. A turn that carries files,
186
- * or a deployment with no Router key, runs on the sandbox lane, where the
187
- * files are.
188
- *
189
- * Pass `afterAuthorize` and `produce` to `createChatTurnRoutes`.
190
- */
191
- export function createHybridLane(env: AppEnv) {
192
- const shell = createSandboxShell(env)
193
- const sandboxProduce = createSandboxProduce(env)
194
- // The box a turn's admission started, by thread; `produce` takes it in the same request.
195
- const started = new Map<string, Promise<SandboxInstance>>()
196
- const acquire = (scope: { workspaceId: string; userId: string }, trace?: TurnTrace) =>
197
- ensureForegroundWorkspaceSandbox(env, shell, scope, trace)
198
-
199
- return {
200
- afterAuthorize({ body, tenantId, userId, threadId, trace }: ChatTurnAfterAuthorizeArgs<void>) {
201
- if (!env.TANGLE_API_KEY?.trim()) return undefined
202
- const start = startHybridBox(body, () => acquire({ workspaceId: tenantId, userId }, trace))
203
- if (!start) return undefined
204
- started.set(threadId, start)
205
- // A turn that ends before `produce` leaves the box to idle out.
206
- return () => { started.delete(threadId) }
207
- },
208
- produce: {
209
- when: ({ prompt }: ChatTurnProduceArgs<void>) => Boolean(env.TANGLE_API_KEY?.trim()) && !turnCarriesFiles(prompt),
210
- router(args: ChatTurnProduceArgs<void>): ChatTurnHybridConfiguration {
211
- const { body, identity, trace } = args
212
- const box = started.get(identity.sessionId)
213
- started.delete(identity.sessionId)
214
- const apiKey = env.TANGLE_API_KEY?.trim()
215
- if (!apiKey) throw new Error('Router credentials are not configured')
216
- const model = body.model ?? env.MODEL_NAME ?? config.model.default
217
- const baseUrl = routerBaseUrl(env)
218
- const scope = { workspaceId: identity.tenantId, userId: identity.userId }
219
- return {
220
- apiKey,
221
- model,
222
- ...(baseUrl ? { baseUrl } : {}),
223
- systemPrompt: { profile: { prompt: { systemPrompt: config.systemPrompt } } },
224
- box: {
225
- acquire: () => acquire(scope, trace),
226
- ...(box ? { started: box } : {}),
227
- // The box agent keeps its own session per thread, so a sandbox-lane turn on the same thread never collides with it.
228
- runTask: sandboxPromptRunner(shell, { model, harness: config.harness, sessionId: `sandbox-run-${identity.sessionId}` }),
229
- },
230
- }
231
- },
232
- sandbox(args: ChatTurnProduceArgs<void>) {
233
- started.delete(args.identity.sessionId)
234
- return sandboxProduce(args)
235
- },
236
- },
237
- }
238
- }
239
-
240
- /**
241
- * Warm the open workspace's box (`POST /api/chat/prewarm`): the shared route
242
- * behind `AgentWorkspaceLayout`'s `prewarm`. Every configured lane wakes its
243
- * workspace lock. The Router lane leaves the box to its existing speculator;
244
- * it warms only the lock on open/focus, with no paid provisioning.
245
- */
246
- export function createPrewarmRoute(env: AppEnv, requireUserId: (request: Request) => Promise<string | null>) {
247
- return createWorkspacePrewarmRoute<void>({
248
- ...(env.TURN_STREAM && env.TURN_STREAM_AUTH_SECRET ? { turnStore: {
249
- db: env.DB, region: env.D1_REGION,
250
- lock: { namespace: env.TURN_STREAM, auth: { secret: env.TURN_STREAM_AUTH_SECRET } },
251
- } } : {}),
252
- authorize: async ({ request }) => {
253
- const userId = await requireUserId(request)
254
- if (!userId) return { status: 'denied', response: Response.json({ error: 'Unauthorized' }, { status: 401 }) }
255
- if (!env.TURN_STREAM || !env.TURN_STREAM_AUTH_SECRET) return { status: 'declined', reason: 'lock_not_configured' }
256
- if (env.TANGLE_API_KEY?.trim() || !env.SANDBOX_API_KEY?.trim() || !env.SANDBOX_GATEWAY_URL?.trim()) {
257
- return { status: 'allowed', workspaceId: userId, steps: [] }
258
- }
259
- const scope = { workspaceId: userId, userId }
260
- return {
261
- status: 'allowed',
262
- workspaceId: scope.workspaceId,
263
- key: scope.workspaceId,
264
- warm: async () => {
265
- const prewarmer = createSandboxPrewarmer(createSandboxShell(env), {
266
- claim: createD1PrewarmClaimStore(env.DB),
267
- mode: 'create-or-resume',
268
- })
269
- const decision = await prewarmer.prewarm({ ...scope, harness: config.harness })
270
- await decision.completion
271
- return { outcome: decision.outcome }
272
- },
273
- }
274
- },
275
- })
276
- }
277
-
278
93
  /** Where large uploads land: the workspace box's filesystem. Returns null when
279
94
  * no sandbox is configured — the upload route then accepts inline files only
280
95
  * and rejects oversized ones with an explicit 413. */
@@ -1,73 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from 'vitest'
2
- import type { ChatTurnProduceArgs } from '@tangle-network/agent-app/chat-routes'
3
-
4
- const boundary = vi.hoisted(() => ({
5
- claim: vi.fn(),
6
- ensure: vi.fn(),
7
- stream: vi.fn(),
8
- }))
9
- vi.mock('@tangle-network/agent-app/sandbox', async (importOriginal) => ({
10
- ...await importOriginal<typeof import('@tangle-network/agent-app/sandbox')>(),
11
- createD1PrewarmClaimStore: boundary.claim,
12
- ensureWorkspaceSandbox: boundary.ensure,
13
- streamSandboxPrompt: boundary.stream,
14
- }))
15
- import { createSandboxProduce } from '../src/sandbox'
16
- import type { AppEnv } from '../src/env'
17
-
18
- beforeEach(() => {
19
- vi.clearAllMocks()
20
- boundary.claim.mockReturnValue({
21
- acquire: async () => true,
22
- release: async () => undefined,
23
- isHeld: async () => false,
24
- inspect: async () => ({ status: 'absent' as const }),
25
- acquireLease: async () => ({ key: 'workspace-1::opencode', expiresAt: Date.now() + 180000 }),
26
- releaseLease: async () => undefined,
27
- })
28
- boundary.ensure.mockResolvedValue({ id: 'box-1' })
29
- boundary.stream.mockImplementation(async function* () {
30
- yield { type: 'result', data: { finalText: 'Done.' } }
31
- })
32
- })
33
-
34
- const env = {
35
- DB: {}, BETTER_AUTH_URL: 'http://localhost:8787', BETTER_AUTH_SECRET: 'test-only-secret',
36
- SANDBOX_API_KEY: 'not-a-live-key', SANDBOX_GATEWAY_URL: 'https://sandbox.example.test',
37
- } as AppEnv
38
-
39
- async function dispatch(limits: ChatTurnProduceArgs<void>['executionLimits']) {
40
- const producer = await createSandboxProduce(env)({
41
- request: new Request('http://localhost:8787/api/chat', { method: 'POST' }),
42
- body: { threadId: 'thread-1', content: 'Hello' },
43
- identity: { tenantId: 'workspace-1', sessionId: 'thread-1', userId: 'user-1', turnIndex: 0 },
44
- context: undefined, prompt: 'Hello', executionId: 'execution-1', turnStreamId: 'turn-1',
45
- priorMessages: [], userMessageId: 'message-1', executionLimits: limits,
46
- })
47
- for await (const _event of producer.stream) { /* Drain the actual producer. */ }
48
- expect(boundary.stream).toHaveBeenCalledTimes(1)
49
- const call = boundary.stream.mock.calls[0]
50
- if (!call) throw new Error('The actual producer did not call the native stream boundary')
51
- return call[3]
52
- }
53
-
54
- describe('Gateway inclusive output budget at the native sandbox boundary', () => {
55
- it.each([undefined, 100, 0])('does not add the reasoning subset (%s) to total output authority', async reasoning => {
56
- const options = await dispatch({
57
- maxOutputTokens: 321,
58
- ...(reasoning !== undefined ? { maxReasoningTokens: reasoning } : {}),
59
- })
60
- expect(options.maxOutputTokens).toBe(321)
61
- expect(options.maxTotalOutputTokens).toBe(321)
62
- if (reasoning === undefined) expect(options).not.toHaveProperty('maxReasoningTokens')
63
- else if (reasoning === 0) expect(options.effort).toBe('none')
64
- else expect(options.maxReasoningTokens).toBe(reasoning)
65
- })
66
-
67
- it('does not invent token ceilings for an uncapped browser turn', async () => {
68
- const options = await dispatch(undefined)
69
- expect(options).not.toHaveProperty('maxOutputTokens')
70
- expect(options).not.toHaveProperty('maxTotalOutputTokens')
71
- expect(options).not.toHaveProperty('maxReasoningTokens')
72
- })
73
- })