@tangle-network/create-agent-app 0.63.25 → 0.63.26
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 +1 -1
- package/template-chat/AGENTS.md +1 -0
- package/template-chat/CUSTOMIZE.md +1 -0
- package/template-chat/README.md +12 -0
- package/template-chat/_package.json +2 -1
- package/template-chat/scripts/release-canary.mjs +130 -0
- package/template-chat/src/chat.ts +4 -4
- package/template-chat/tests/release-canary.e2e.test.ts +229 -0
package/package.json
CHANGED
package/template-chat/AGENTS.md
CHANGED
|
@@ -50,6 +50,7 @@ The turn test uses real migrations and shell factories with a fake sandbox event
|
|
|
50
50
|
The parent repository's fresh packed-generation gate also runs a local Worker/D1 browser proof.
|
|
51
51
|
That proof checks real auth and thread persistence across a Worker restart, but makes no model request.
|
|
52
52
|
For deployment, complete environment configuration, apply the required database migrations, and exercise the actual page and turn flow.
|
|
53
|
+
`pnpm deploy` ends with the release canary (`scripts/release-canary.mjs`): a plain turn and a computer turn on the deployed origin, judged from the persisted transcript. Keep both turns; a release whose computer step fails must fail.
|
|
53
54
|
Record hosted/model/disconnect proofs separately; do not claim them from compilation or an injected producer.
|
|
54
55
|
Fix failures without weakening the protected behavior.
|
|
55
56
|
|
|
@@ -42,6 +42,7 @@ Discovery: **Does the public-package frontend work on the real Worker origin?**
|
|
|
42
42
|
|
|
43
43
|
- [ ] Run `pnpm build`. Worker and DOM typechecks are separate; Vite emits `dist/client`.
|
|
44
44
|
- [ ] Run `pnpm exec wrangler deploy --dry-run` to check the Worker bundle without deploying.
|
|
45
|
+
- [ ] Before `pnpm deploy`, export `CANARY_URL` (the deployed origin), `CANARY_EMAIL` and `CANARY_PASSWORD` for the release canary. It runs a plain turn and a computer turn on the deployed app and fails the release, naming the cause, when either fails.
|
|
45
46
|
- [ ] Run `pnpm dev`, open the Worker origin, sign up, open History, create a conversation, and retain its `?threadId=...` URL.
|
|
46
47
|
- [ ] Confirm desktop and mobile navigation, sign-out, fresh login, and reopening a saved thread after a local Worker restart.
|
|
47
48
|
|
package/template-chat/README.md
CHANGED
|
@@ -97,6 +97,18 @@ Before production, complete a real model turn, reload its saved text and usage,
|
|
|
97
97
|
and check live disconnect/replay on the deployed origin. Those checks are not
|
|
98
98
|
implied by a local build or no-model browser proof.
|
|
99
99
|
|
|
100
|
+
## Release canary
|
|
101
|
+
|
|
102
|
+
`pnpm deploy` ends with `pnpm canary` (`scripts/release-canary.mjs`), which runs
|
|
103
|
+
two turns on the deployed origin as a canary member: a plain answer, and a
|
|
104
|
+
command only the workspace computer can run. The release fails when either turn
|
|
105
|
+
fails, and the log names the computer step's cause from the transcript's
|
|
106
|
+
recorded notice. Set `CANARY_URL` (the deployed origin), `CANARY_EMAIL` and
|
|
107
|
+
`CANARY_PASSWORD` in the shell that deploys; the first release signs that member
|
|
108
|
+
up. A deploy without them fails rather than passing unchecked.
|
|
109
|
+
`tests/release-canary.e2e.test.ts` runs the same script against this app's
|
|
110
|
+
Worker over local protocol peers.
|
|
111
|
+
|
|
100
112
|
## Artifact index
|
|
101
113
|
|
|
102
114
|
`GET /api/files` lists files under `/home/agent/artifacts` for the signed-in
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"dev": "wrangler dev",
|
|
12
12
|
"build": "pnpm run typecheck && pnpm run build:web",
|
|
13
13
|
"build:web": "vite build",
|
|
14
|
-
"deploy": "pnpm run peer-check && pnpm run signoff && wrangler deploy",
|
|
14
|
+
"deploy": "pnpm run peer-check && pnpm run signoff && wrangler deploy && pnpm run canary",
|
|
15
|
+
"canary": "node scripts/release-canary.mjs",
|
|
15
16
|
"db:migrate": "wrangler d1 migrations apply __PACKAGE_NAME__ --remote",
|
|
16
17
|
"db:migrate:local": "wrangler d1 migrations apply __PACKAGE_NAME__ --local",
|
|
17
18
|
"peer-check": "agent-app-peer-check",
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The release canary. `pnpm deploy` runs it after `wrangler deploy`; `pnpm canary` runs it again.
|
|
3
|
+
//
|
|
4
|
+
// It signs in as the canary member and runs two turns on the deployed app, one after the other, each on a new thread:
|
|
5
|
+
// answer a plain reply from the model;
|
|
6
|
+
// computer a command only the workspace computer can run: it hashes a fresh nonce, so the digest
|
|
7
|
+
// proves the box ran it. The turn passes only when its `sandbox_run` call completed with
|
|
8
|
+
// that digest and the transcript carries no computer failure.
|
|
9
|
+
// It reads each verdict from the persisted transcript, the record a member and an operator read, and
|
|
10
|
+
// exits 1 naming the cause when either turn fails, with the computer step's recorded diagnostic.
|
|
11
|
+
//
|
|
12
|
+
// CANARY_URL the deployed origin, e.g. https://my-agent.example.workers.dev (required)
|
|
13
|
+
// CANARY_EMAIL the canary member's email (required); the first release signs the member up
|
|
14
|
+
// CANARY_PASSWORD its password (required)
|
|
15
|
+
// CANARY_TURN_MS the budget for each turn, including a cold box (default 300000)
|
|
16
|
+
|
|
17
|
+
import { createHash, randomBytes, randomUUID } from 'node:crypto'
|
|
18
|
+
|
|
19
|
+
const base = process.env.CANARY_URL?.trim().replace(/\/+$/, '')
|
|
20
|
+
const email = process.env.CANARY_EMAIL?.trim()
|
|
21
|
+
const password = process.env.CANARY_PASSWORD
|
|
22
|
+
const turnMs = Number(process.env.CANARY_TURN_MS || 300_000)
|
|
23
|
+
|
|
24
|
+
function refuse(message) {
|
|
25
|
+
console.error(`canary: ${message}`)
|
|
26
|
+
process.exit(1)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!base || !email || !password) {
|
|
30
|
+
refuse('set CANARY_URL, CANARY_EMAIL and CANARY_PASSWORD; a release with no canary is unverified')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function signIn() {
|
|
34
|
+
const post = (path, body) => fetch(`${base}${path}`, {
|
|
35
|
+
method: 'POST', headers: { 'content-type': 'application/json', origin: base }, body: JSON.stringify(body),
|
|
36
|
+
})
|
|
37
|
+
let response = await post('/api/auth/sign-in/email', { email, password })
|
|
38
|
+
// The first release creates the canary member; every later one signs it in.
|
|
39
|
+
if (response.status === 401) response = await post('/api/auth/sign-up/email', { email, password, name: 'Release canary' })
|
|
40
|
+
if (!response.ok) refuse(`sign-in answered ${response.status}: ${(await response.text()).slice(0, 300)}`)
|
|
41
|
+
const cookie = response.headers.getSetCookie().map(value => value.split(';')[0]).join('; ')
|
|
42
|
+
if (!cookie) refuse('sign-in returned no session cookie')
|
|
43
|
+
return cookie
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function json(response, what) {
|
|
47
|
+
if (!response.ok) throw new Error(`${what} answered ${response.status}: ${(await response.text()).slice(0, 300)}`)
|
|
48
|
+
return response.json()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** One turn on a new thread, read back from the persisted transcript. */
|
|
52
|
+
async function turn(cookie, title, content) {
|
|
53
|
+
const headers = { cookie, 'content-type': 'application/json' }
|
|
54
|
+
const signal = AbortSignal.timeout(turnMs)
|
|
55
|
+
const { thread } = await json(await fetch(`${base}/api/threads`, {
|
|
56
|
+
method: 'POST', headers, body: JSON.stringify({ title }), signal,
|
|
57
|
+
}), 'thread creation')
|
|
58
|
+
const reply = await fetch(`${base}/api/chat`, {
|
|
59
|
+
method: 'POST', headers, body: JSON.stringify({ threadId: thread.id, turnId: randomUUID(), content }), signal,
|
|
60
|
+
})
|
|
61
|
+
if (!reply.ok) throw new Error(`the turn answered ${reply.status}: ${(await reply.text()).slice(0, 300)}`)
|
|
62
|
+
await reply.text()
|
|
63
|
+
// The final assistant row lands as the turn settles, when the user row is marked backendTerminal (src/chat.ts).
|
|
64
|
+
// A computer step that could not be confirmed leaves it unmarked; its row is read after the window anyway.
|
|
65
|
+
const settleBy = Date.now() + 15_000
|
|
66
|
+
for (;;) {
|
|
67
|
+
const { messages } = await json(await fetch(`${base}/api/threads/${encodeURIComponent(thread.id)}/messages`, {
|
|
68
|
+
headers: { cookie }, signal: AbortSignal.timeout(30_000),
|
|
69
|
+
}), 'transcript read')
|
|
70
|
+
const user = messages.filter(message => message.role === 'user').at(-1)
|
|
71
|
+
const assistant = messages.filter(message => message.role === 'assistant').at(-1)
|
|
72
|
+
const late = Date.now() > settleBy
|
|
73
|
+
if (assistant && (user?.backendTerminal || late)) {
|
|
74
|
+
return { threadId: thread.id, assistant, parts: Array.isArray(assistant.parts) ? assistant.parts : [] }
|
|
75
|
+
}
|
|
76
|
+
if (late) throw new Error('no assistant reply was persisted within 15 s of the turn')
|
|
77
|
+
await new Promise(resolve => setTimeout(resolve, 1_000))
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const failureNotice = parts => parts.find(part => part.type === 'notice'
|
|
82
|
+
&& (part.noticeKind === 'error' || String(part.code ?? '').startsWith('sandbox_')))
|
|
83
|
+
|
|
84
|
+
function judgeAnswer({ assistant, parts }) {
|
|
85
|
+
const notice = failureNotice(parts)
|
|
86
|
+
if (notice) return `${notice.code ?? notice.noticeKind}: ${notice.detail ?? notice.text}`
|
|
87
|
+
if (!String(assistant.content ?? '').trim()) return 'the reply is empty'
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function judgeComputer({ assistant, parts }, digest) {
|
|
92
|
+
const notice = failureNotice(parts)
|
|
93
|
+
const call = parts.find(part => part.type === 'tool' && part.tool === 'sandbox_run')
|
|
94
|
+
if (notice) return `the computer step failed (${notice.code ?? notice.noticeKind}): ${notice.detail ?? notice.text}`
|
|
95
|
+
if (!call) return `the model answered without a computer step: ${String(assistant.content ?? '').slice(0, 200)}`
|
|
96
|
+
if (call.state?.status !== 'completed') return `the computer step ended ${call.state?.status}: ${call.state?.error ?? ''}`
|
|
97
|
+
if (!JSON.stringify(call.state.output ?? '').includes(digest)) return 'the computer step completed without the command\'s digest'
|
|
98
|
+
return null
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const nonce = `canary-${randomBytes(16).toString('hex')}`
|
|
102
|
+
const digest = createHash('sha256').update(nonce).digest('hex')
|
|
103
|
+
const checks = [
|
|
104
|
+
{ name: 'answer', title: 'Release canary: answer', judge: judgeAnswer,
|
|
105
|
+
content: 'Release check: reply with one short sentence confirming you can answer.' },
|
|
106
|
+
{ name: 'computer', title: 'Release canary: computer', judge: result => judgeComputer(result, digest),
|
|
107
|
+
content: 'Release check: run this exact shell command on the workspace computer with sandbox_run, then reply ' +
|
|
108
|
+
`with only the 64-character hex digest it prints:\nprintf %s '${nonce}' | sha256sum` },
|
|
109
|
+
]
|
|
110
|
+
|
|
111
|
+
const cookie = await signIn()
|
|
112
|
+
let failed = false
|
|
113
|
+
// One turn at a time: the member's workspace admits one turn, so a second would be refused while the first runs.
|
|
114
|
+
for (const check of checks) {
|
|
115
|
+
const started = Date.now()
|
|
116
|
+
let failure
|
|
117
|
+
let threadId
|
|
118
|
+
try {
|
|
119
|
+
const result = await turn(cookie, check.title, check.content)
|
|
120
|
+
threadId = result.threadId
|
|
121
|
+
failure = check.judge(result)
|
|
122
|
+
} catch (error) {
|
|
123
|
+
failure = error instanceof Error ? error.message : String(error)
|
|
124
|
+
}
|
|
125
|
+
const ms = Date.now() - started
|
|
126
|
+
const where = threadId ? ` thread=${threadId}` : ''
|
|
127
|
+
console.log(failure ? `canary ${check.name} FAIL ${ms}ms${where}: ${failure}` : `canary ${check.name} PASS ${ms}ms${where}`)
|
|
128
|
+
failed ||= Boolean(failure)
|
|
129
|
+
}
|
|
130
|
+
process.exit(failed ? 1 : 0)
|
|
@@ -29,9 +29,8 @@ import {
|
|
|
29
29
|
createChatTurnRoutes,
|
|
30
30
|
createUploadRoute,
|
|
31
31
|
type ChatTurnAuthorization,
|
|
32
|
-
type ChatTurnProduceArgs,
|
|
33
|
-
type ChatTurnRouteProducer,
|
|
34
32
|
type ChatTurnRoutes,
|
|
33
|
+
type CreateChatTurnRoutesOptions,
|
|
35
34
|
type SandboxUploadSink,
|
|
36
35
|
} from '@tangle-network/agent-app/chat-routes'
|
|
37
36
|
import {
|
|
@@ -55,8 +54,9 @@ export interface ChatAppOverrides {
|
|
|
55
54
|
db?: ChatDatabase
|
|
56
55
|
/** Test seam: `createMemoryTurnEventStore()`. Default: D1-backed buffer. */
|
|
57
56
|
turnStore?: TurnEventStore
|
|
58
|
-
/** Test seam: a fake producer
|
|
59
|
-
|
|
57
|
+
/** Test seam: a fake producer, or the hybrid lane's `{ router }` configuration over local protocol peers.
|
|
58
|
+
* Default: the real hybrid lane. */
|
|
59
|
+
produce?: CreateChatTurnRoutesOptions<void>['produce']
|
|
60
60
|
/** Test seam: where large uploads land. Default: the workspace box's fs. */
|
|
61
61
|
uploadSink?: (scope: { workspaceId: string; userId: string }) => Promise<SandboxUploadSink | null>
|
|
62
62
|
/** Server-trusted identity for a private gateway chat assembly. Never read
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The release canary, run as `pnpm canary` runs it, against this app's real Worker served over HTTP: real
|
|
3
|
+
* better-auth sign-up, the real migration in SQLite, the shared hybrid lane, and the real Sandbox SDK.
|
|
4
|
+
* The Router and the sandbox are local protocol peers; the sandbox peer computes the command's digest as a box
|
|
5
|
+
* would. A pass needs both turns; a failed computer step fails the release, and the log names its recorded cause.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { spawn } from 'node:child_process'
|
|
9
|
+
import { createHash } from 'node:crypto'
|
|
10
|
+
import { readdirSync, readFileSync } from 'node:fs'
|
|
11
|
+
import { createServer, type Server } from 'node:http'
|
|
12
|
+
import type { AddressInfo } from 'node:net'
|
|
13
|
+
import { dirname, join } from 'node:path'
|
|
14
|
+
import { fileURLToPath } from 'node:url'
|
|
15
|
+
import Database from 'better-sqlite3'
|
|
16
|
+
import { drizzle } from 'drizzle-orm/better-sqlite3'
|
|
17
|
+
import { Sandbox } from '@tangle-network/sandbox'
|
|
18
|
+
import { describe, expect, it } from 'vitest'
|
|
19
|
+
import type { ChatDatabase } from '@tangle-network/agent-app/chat-store'
|
|
20
|
+
import { sandboxPromptRunner } from '@tangle-network/agent-app/sandbox'
|
|
21
|
+
import { createMemoryTurnEventStore } from '@tangle-network/agent-app/stream'
|
|
22
|
+
import { createMemoryTurnStreamHarness, MEMORY_TURN_STREAM_AUTH_SECRET } from '@tangle-network/agent-app/turn-stream'
|
|
23
|
+
import { buildChatApp, type ChatApp } from '../src/chat'
|
|
24
|
+
import type { AppEnv } from '../src/env'
|
|
25
|
+
import { createSandboxShell } from '../src/sandbox'
|
|
26
|
+
import { TurnStreamDO } from '../src/turn-lock'
|
|
27
|
+
import { createChatWorker } from '../src/worker'
|
|
28
|
+
|
|
29
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..')
|
|
30
|
+
const CANARY = join(ROOT, 'scripts', 'release-canary.mjs')
|
|
31
|
+
|
|
32
|
+
async function listen(server: Server): Promise<string> {
|
|
33
|
+
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve))
|
|
34
|
+
return `http://127.0.0.1:${(server.address() as AddressInfo).port}`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function close(server: Server): Promise<void> {
|
|
38
|
+
server.closeAllConnections()
|
|
39
|
+
await new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve()))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The Router and the sandbox API over loopback HTTP. A box that `answers` runs the canary's command; one that
|
|
43
|
+
* `fails` reports a failed run; an `unplaced` box is refused by the sandbox API before it can run anything. */
|
|
44
|
+
async function protocolPeers(computer: 'answers' | 'fails' | 'unplaced') {
|
|
45
|
+
const dispatched: string[] = []
|
|
46
|
+
let origin = ''
|
|
47
|
+
const server = createServer(async (request, response) => {
|
|
48
|
+
let body = ''
|
|
49
|
+
for await (const chunk of request) body += String(chunk)
|
|
50
|
+
const path = new URL(request.url ?? '/', 'http://peer.local').pathname
|
|
51
|
+
const json = (value: unknown, status = 200) => response.writeHead(status, { 'content-type': 'application/json' }).end(JSON.stringify(value))
|
|
52
|
+
const frame = (type: string, data: unknown) => `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`
|
|
53
|
+
if (path === '/v1/sandboxes/canary-box') {
|
|
54
|
+
if (computer === 'unplaced') return json({ code: 'COMPUTER_UNAVAILABLE', message: 'No sandbox host has capacity for canary-box' }, 409)
|
|
55
|
+
return json({ id: 'canary-box', status: 'running', filesystemIncarnationId: 'canary-incarnation',
|
|
56
|
+
filesystemIncarnationProvenance: 'fresh', filesystemIncarnationReadiness: 'ready',
|
|
57
|
+
connection: { runtimeUrl: `${origin}/v1/sandboxes/canary-box/runtime` } })
|
|
58
|
+
}
|
|
59
|
+
if (request.method === 'POST' && path === '/v1/sandboxes/canary-box/runtime/agents/run/stream') {
|
|
60
|
+
const input = JSON.parse(body) as { sessionId: string; parts?: Array<{ type: string; text?: string }> }
|
|
61
|
+
// The SDK encodes every text part on the wire; the sidecar decodes it before dispatch.
|
|
62
|
+
const task = (input.parts ?? []).filter(part => part.type === 'text')
|
|
63
|
+
.map(part => Buffer.from(part.text ?? '', 'base64').toString('utf8')).join('\n')
|
|
64
|
+
dispatched.push(task)
|
|
65
|
+
const ids = { executionId: String(request.headers['x-execution-id'] ?? 'canary-execution'), sessionId: input.sessionId }
|
|
66
|
+
response.writeHead(200, { 'content-type': 'text/event-stream' })
|
|
67
|
+
if (computer === 'fails') {
|
|
68
|
+
const refusal = { code: 'PAYLOAD_TOO_LARGE', status: 413, message: 'Request body exceeds the 1048576 byte limit' }
|
|
69
|
+
return response.end(frame('execution.started', ids) + frame('error', refusal)
|
|
70
|
+
+ frame('result', { ...ids, status: 'failed', error: refusal }))
|
|
71
|
+
}
|
|
72
|
+
const nonce = /canary-[0-9a-f]{32}/.exec(task)?.[0] ?? ''
|
|
73
|
+
const printed = `${createHash('sha256').update(nonce).digest('hex')} -`
|
|
74
|
+
return response.end(frame('execution.started', ids) + frame('result', { ...ids, finalText: printed })
|
|
75
|
+
+ frame('done', { ...ids, status: 'completed' }))
|
|
76
|
+
}
|
|
77
|
+
if (request.method === 'POST' && path.endsWith('/chat/completions')) {
|
|
78
|
+
const payload = JSON.parse(body) as { messages: Array<{ role: string; content: unknown }> }
|
|
79
|
+
const member = String(payload.messages.filter(message => message.role === 'user').at(-1)?.content ?? '')
|
|
80
|
+
const result = payload.messages.find(message => message.role === 'tool')
|
|
81
|
+
const escalate = member.includes('sandbox_run') && !result
|
|
82
|
+
const delta = escalate
|
|
83
|
+
? { tool_calls: [{ index: 0, id: 'canary-call', function: { name: 'sandbox_run', arguments: JSON.stringify({ task: member }) } }] }
|
|
84
|
+
: { content: result ? `The computer returned: ${String(result.content)}` : 'I can answer.' }
|
|
85
|
+
response.writeHead(200, { 'content-type': 'text/event-stream' })
|
|
86
|
+
return response.end(`data: ${JSON.stringify({ model: 'served/canary', choices: [{ index: 0, delta,
|
|
87
|
+
finish_reason: escalate ? 'tool_calls' : 'stop' }] })}\n\ndata: [DONE]\n\n`)
|
|
88
|
+
}
|
|
89
|
+
return json({ error: `Unexpected protocol path ${request.method} ${path}` }, 500)
|
|
90
|
+
})
|
|
91
|
+
origin = await listen(server)
|
|
92
|
+
return { origin, dispatched, close: () => close(server) }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** This app's Worker over HTTP, on the real migration, with the hybrid lane bound to the peers. */
|
|
96
|
+
async function servedApp(peer: { origin: string }) {
|
|
97
|
+
const sqlite = new Database(':memory:')
|
|
98
|
+
sqlite.pragma('foreign_keys = ON')
|
|
99
|
+
const migrations = join(ROOT, 'migrations')
|
|
100
|
+
for (const name of readdirSync(migrations).filter(file => /^\d+.*\.sql$/.test(file)).sort()) {
|
|
101
|
+
sqlite.exec(readFileSync(join(migrations, name), 'utf8'))
|
|
102
|
+
}
|
|
103
|
+
let app: ChatApp | undefined
|
|
104
|
+
let env: AppEnv | undefined
|
|
105
|
+
const pending: Promise<unknown>[] = []
|
|
106
|
+
const worker = createChatWorker({
|
|
107
|
+
buildChatApp: () => app!,
|
|
108
|
+
buildGatewayApp: () => { throw new Error('The canary does not use the gateway') },
|
|
109
|
+
gatewayEnabled: false,
|
|
110
|
+
})
|
|
111
|
+
const server = createServer(async (incoming, outgoing) => {
|
|
112
|
+
try {
|
|
113
|
+
const chunks: Buffer[] = []
|
|
114
|
+
for await (const chunk of incoming) chunks.push(Buffer.from(chunk))
|
|
115
|
+
const headers = new Headers()
|
|
116
|
+
for (const [name, value] of Object.entries(incoming.headers)) {
|
|
117
|
+
if (Array.isArray(value)) for (const item of value) headers.append(name, item)
|
|
118
|
+
else if (value !== undefined) headers.set(name, value)
|
|
119
|
+
}
|
|
120
|
+
const request = new Request(`${origin}${incoming.url}`, { method: incoming.method, headers,
|
|
121
|
+
...(['GET', 'HEAD'].includes(incoming.method ?? 'GET') ? {} : { body: Buffer.concat(chunks) }) })
|
|
122
|
+
const response = await worker.fetch!(request as Parameters<NonNullable<typeof worker.fetch>>[0], env!, {
|
|
123
|
+
waitUntil: (work: Promise<unknown>) => { pending.push(work) }, passThroughOnException: () => undefined, props: {},
|
|
124
|
+
} as unknown as ExecutionContext)
|
|
125
|
+
outgoing.statusCode = response.status
|
|
126
|
+
for (const [name, value] of response.headers) if (name !== 'set-cookie') outgoing.setHeader(name, value)
|
|
127
|
+
const cookies = response.headers.getSetCookie()
|
|
128
|
+
if (cookies.length) outgoing.setHeader('set-cookie', cookies)
|
|
129
|
+
outgoing.end(Buffer.from(await response.arrayBuffer()))
|
|
130
|
+
} catch (error) { outgoing.statusCode = 500; outgoing.end(String(error)) }
|
|
131
|
+
})
|
|
132
|
+
const origin = await listen(server)
|
|
133
|
+
env = {
|
|
134
|
+
DB: null as unknown as AppEnv['DB'], BETTER_AUTH_URL: origin, BETTER_AUTH_SECRET: 'canary-test-secret-not-for-production',
|
|
135
|
+
TURN_STREAM: createMemoryTurnStreamHarness((state, secret) => new TurnStreamDO(state, { TURN_STREAM_AUTH_SECRET: secret })).namespace,
|
|
136
|
+
TURN_STREAM_AUTH_SECRET: MEMORY_TURN_STREAM_AUTH_SECRET,
|
|
137
|
+
TANGLE_API_KEY: 'loopback-only-router-key', TANGLE_ROUTER_URL: peer.origin,
|
|
138
|
+
SANDBOX_API_KEY: 'loopback-only-sandbox-key', SANDBOX_GATEWAY_URL: peer.origin,
|
|
139
|
+
}
|
|
140
|
+
const client = new Sandbox({ apiKey: 'loopback-only-sandbox-key', baseUrl: peer.origin })
|
|
141
|
+
const shell = createSandboxShell(env)
|
|
142
|
+
app = buildChatApp(env, {
|
|
143
|
+
db: drizzle(sqlite) as unknown as ChatDatabase,
|
|
144
|
+
turnStore: createMemoryTurnEventStore(),
|
|
145
|
+
uploadSink: async () => null,
|
|
146
|
+
produce: {
|
|
147
|
+
router: args => ({
|
|
148
|
+
apiKey: 'loopback-only-router-key', model: 'requested/canary', baseUrl: peer.origin,
|
|
149
|
+
systemPrompt: 'Use the workspace computer when the member asks for it.',
|
|
150
|
+
box: {
|
|
151
|
+
acquire: async () => {
|
|
152
|
+
const box = await client.get('canary-box')
|
|
153
|
+
if (!box) throw new Error('The loopback sandbox peer has no canary box')
|
|
154
|
+
return box
|
|
155
|
+
},
|
|
156
|
+
runTask: sandboxPromptRunner(shell, { model: 'native/canary', sessionId: `sandbox-run-${args.identity.sessionId}` }),
|
|
157
|
+
timeoutMs: 10_000,
|
|
158
|
+
},
|
|
159
|
+
}),
|
|
160
|
+
},
|
|
161
|
+
})
|
|
162
|
+
return {
|
|
163
|
+
origin,
|
|
164
|
+
close: async () => { await Promise.allSettled(pending); await close(server); sqlite.close() },
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function runCanary(env: Record<string, string>): Promise<{ code: number | null; output: string }> {
|
|
169
|
+
return new Promise((resolve, reject) => {
|
|
170
|
+
const child = spawn(process.execPath, [CANARY], { env: { PATH: process.env.PATH ?? '', ...env } })
|
|
171
|
+
let output = ''
|
|
172
|
+
child.stdout.on('data', chunk => { output += String(chunk) })
|
|
173
|
+
child.stderr.on('data', chunk => { output += String(chunk) })
|
|
174
|
+
child.on('error', reject)
|
|
175
|
+
child.on('close', code => resolve({ code, output }))
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const member = { CANARY_EMAIL: 'release-canary@example.test', CANARY_PASSWORD: 'canary-fixture-password', CANARY_TURN_MS: '20000' }
|
|
180
|
+
|
|
181
|
+
describe('release canary', () => {
|
|
182
|
+
it('passes a release whose plain answer and computer step both succeed', async () => {
|
|
183
|
+
const peer = await protocolPeers('answers')
|
|
184
|
+
const served = await servedApp(peer)
|
|
185
|
+
try {
|
|
186
|
+
const run = await runCanary({ ...member, CANARY_URL: served.origin })
|
|
187
|
+
expect(run.output).toMatch(/canary answer PASS/)
|
|
188
|
+
expect(run.output).toMatch(/canary computer PASS/)
|
|
189
|
+
expect(run.code).toBe(0)
|
|
190
|
+
// The computer step reached the box once, carrying the command with the canary's fresh nonce.
|
|
191
|
+
expect(peer.dispatched).toHaveLength(1)
|
|
192
|
+
expect(peer.dispatched[0]).toMatch(/printf %s 'canary-[0-9a-f]{32}' \| sha256sum/)
|
|
193
|
+
// The second release signs the member in rather than signing up again, and passes again.
|
|
194
|
+
expect((await runCanary({ ...member, CANARY_URL: served.origin })).code).toBe(0)
|
|
195
|
+
} finally { await served.close(); await peer.close() }
|
|
196
|
+
}, 60_000)
|
|
197
|
+
|
|
198
|
+
it('fails the release when the box reports a failed computer step', async () => {
|
|
199
|
+
const peer = await protocolPeers('fails')
|
|
200
|
+
const served = await servedApp(peer)
|
|
201
|
+
try {
|
|
202
|
+
const run = await runCanary({ ...member, CANARY_URL: served.origin })
|
|
203
|
+
expect(run.output).toMatch(/canary answer PASS/)
|
|
204
|
+
expect(run.output).toMatch(/canary computer FAIL .*the computer step failed \(sandbox_\w+\)/)
|
|
205
|
+
expect(peer.dispatched).toHaveLength(1)
|
|
206
|
+
expect(run.code).toBe(1)
|
|
207
|
+
} finally { await served.close(); await peer.close() }
|
|
208
|
+
}, 60_000)
|
|
209
|
+
|
|
210
|
+
it('fails the release when the computer cannot be placed, naming the recorded cause', async () => {
|
|
211
|
+
const peer = await protocolPeers('unplaced')
|
|
212
|
+
const served = await servedApp(peer)
|
|
213
|
+
try {
|
|
214
|
+
const run = await runCanary({ ...member, CANARY_URL: served.origin })
|
|
215
|
+
expect(run.output).toMatch(/canary answer PASS/)
|
|
216
|
+
expect(run.output).toMatch(/canary computer FAIL .*the computer step failed \(sandbox_unavailable\)/)
|
|
217
|
+
// The sandbox API's refusal reaches the release log through the transcript's persisted notice.
|
|
218
|
+
expect(run.output).toContain('No sandbox host has capacity for canary-box')
|
|
219
|
+
expect(peer.dispatched).toEqual([])
|
|
220
|
+
expect(run.code).toBe(1)
|
|
221
|
+
} finally { await served.close(); await peer.close() }
|
|
222
|
+
}, 60_000)
|
|
223
|
+
|
|
224
|
+
it('refuses to pass a release it was not configured to check', async () => {
|
|
225
|
+
const run = await runCanary({})
|
|
226
|
+
expect(run.output).toContain('CANARY_URL')
|
|
227
|
+
expect(run.code).toBe(1)
|
|
228
|
+
})
|
|
229
|
+
})
|