Open History to see the last commit
1import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
2import { tmpdir } from 'node:os'
3import { join } from 'node:path'
4import { test, expect } from '../fixtures'
5import type { APIRequestContext } from '@playwright/test'
6
7/**
8 * The workspace of `workspaceTools()` for many threads and many hosts. Each
9 * scenario of `/api/coding-workspace` runs in a temp folder that the test
10 * makes, with a scripted model, and answers with JSON. See the route for
11 * the scenarios.
12 */
13
14/** The workspace of the running test. */
15let root = ''
16const file = (path: string) => join(root, path)
17
18/** The message of a tool result that is an error, `{ "error": "..." }`. */
19const errorOf = (result: string | undefined): unknown =>
20 JSON.parse(result ?? '{}').error
21
22/** Run `scenario` in `root`. Gives the JSON of the route. */
23async function run(request: APIRequestContext, scenario: string) {
24 const response = await request.post('/api/coding-workspace', {
25 data: { scenario, root },
26 })
27 const body = await response.text()
28 expect(response.ok(), body).toBe(true)
29 const json: Record<string, unknown> = JSON.parse(body)
30 expect(json.error, body).toBeUndefined()
31 return json
32}
33
34test.describe('Coding tools workspace', () => {
35 test.beforeEach(async () => {
36 root = await mkdtemp(join(tmpdir(), 'e2e-coding-workspace-'))
37 })
38
39 test.afterEach(async () => {
40 await rm(root, { recursive: true, force: true })
41 })
42
43 test('a command does not see an env var of the server', async ({
44 request,
45 }) => {
46 const result = await run(request, 'env')
47
48 // The server has the var. The command gets only the allowlist.
49 expect(result.serverHasSecret).toBe(true)
50 expect(result.results).toEqual(['exit code: 0\nsecret=unset'])
51 })
52
53 test('a thread cannot read the folder of another thread', async ({
54 request,
55 }) => {
56 await mkdir(file('alpha'))
57 await mkdir(file('beta'))
58 await writeFile(file('alpha/secret.txt'), 'alpha only\n')
59
60 const result = await run(request, 'other-root')
61
62 expect(result.alpha).toMatchObject({ results: ['1\talpha only'] })
63 const beta = result.beta as { results: Array<string> }
64 expect(beta.results.map(errorOf)).toEqual([
65 `Path "${file('alpha/secret.txt')}" is outside the workspace.`,
66 'Path "../alpha/secret.txt" is outside the workspace.',
67 ])
68 })
69
70 test('a sandbox thread moves to a second host with reattach, not bind', async ({
71 request,
72 }) => {
73 await writeFile(file('notes.txt'), 'alpha\nbeta\n')
74
75 const result = await run(request, 'sandbox-move')
76
77 expect(result.first).toMatchObject({
78 results: ['1\talpha\n2\tbeta'],
79 bindCalls: 1,
80 })
81 expect(result.second).toMatchObject({
82 results: ['1\talpha\n2\tbeta'],
83 bindCalls: 0,
84 reattached: [
85 {
86 kind: 'sandbox',
87 provider: 'local-process',
88 sandboxId: root,
89 root: '/workspace',
90 },
91 ],
92 stranded: [],
93 })
94 })
95
96 test('a sandbox thread on a host with no reattach is stranded', async ({
97 request,
98 }) => {
99 await writeFile(file('notes.txt'), 'alpha\nbeta\n')
100
101 const result = await run(request, 'stranded')
102
103 const second = result.second as {
104 results: Array<string>
105 bindCalls: number
106 stranded: Array<unknown>
107 }
108 expect(second.bindCalls).toBe(0)
109 expect(second.results.map(errorOf)).toEqual([
110 `The workspace of this thread, /workspace in the local-process sandbox ${root}, is gone, so the tools cannot work on it.`,
111 ])
112 // The event comes each time the tools try the workspace: before the
113 // turn, and at the tool call.
114 expect(second.stranded.length).toBeGreaterThan(0)
115 for (const binding of second.stranded) {
116 expect(binding).toEqual({
117 kind: 'sandbox',
118 provider: 'local-process',
119 sandboxId: root,
120 root: '/workspace',
121 })
122 }
123 })
124
125 test('a folder thread is pinned to its ownership.hostId, also after a restart', async ({
126 request,
127 }) => {
128 await writeFile(file('notes.txt'), 'alpha\nbeta\n')
129
130 const result = await run(request, 'pin')
131
132 expect(result.bindings).toEqual([{ kind: 'host', hostId: 'pod-a', root }])
133 // pod-b offers the input, and its model never runs.
134 expect(result.offers).toBe(1)
135 expect(result.podBCalls).toBe(0)
136 expect(result.podBCallsAfter).toBe(0)
137 expect(result.podBSendEnded).toBe(true)
138 // pod-a after a restart runs it: a tool call and an answer.
139 expect(result.restartedCalls).toBe(2)
140 expect(result.answers).toEqual([
141 'Read notes.txt on pod-a.',
142 'Read notes.txt on pod-a again.',
143 ])
144 })
145})