Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

test(cli): isolate model-service requests from unrelated loopback probes (#943)

Dee63417c941e12
diff --git a/cli/src/backendSocket.gridReads.spec.ts b/cli/src/backendSocket.gridReads.spec.ts
index a986a12cb..83b84e488 100644
--- a/cli/src/backendSocket.gridReads.spec.ts
+++ b/cli/src/backendSocket.gridReads.spec.ts
@@ -7,6 +7,7 @@
* The relay is the spy: nothing here recomputes what the daemon sends, it reads what arrived.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { randomUUID } from 'node:crypto'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { createServer, type IncomingHttpHeaders, type Server } from 'node:http'
import { tmpdir } from 'node:os'
@@ -23,6 +24,7 @@ import { MODELS_REQUESTS, startModels } from './services/models.js'
import { fakeCore } from './testing/fakeCore.js'
const OWN = 'mine', OWN_ID = 'net-own'
+const externalFetch = globalThis.fetch.bind(globalThis)
// Grid's set-up, which a move onto a grid model asks the models service for first: the fake grid here is
// set up already, as on a machine that has used it before.
@@ -71,9 +73,17 @@ function expectNoCredential(): void {
beforeEach(async () => {
seen = []
+ const clientId = randomUUID()
overview = { status: 200, body: { nodes: [{ name: 'mac', engine: 'llama.cpp', models: ['small-q4'], online: true }], models: [{ id: 'Small-Q4' }] } }
modelsDelayMs = 0
server = createServer((req, res) => {
+ // Quiet-machine QA found local discovery probing this port. Identify the test client, so
+ // unrelated probes cannot pollute its log while every path that it requests remains visible.
+ if (req.headers['x-harness-test-client'] !== clientId) {
+ req.resume()
+ res.writeHead(404).end()
+ return
+ }
seen.push({ path: req.url ?? '', headers: req.headers })
const models = req.url === `/g/${OWN_ID}/relay/v1/models`
const found = req.url === `/g/${OWN_ID}${OVERVIEW}` ? overview : models ? { status: 200, body: { data: [] } } : { status: 404, body: {} }
@@ -85,6 +95,14 @@ beforeEach(async () => {
})
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
base = `http://127.0.0.1:${(server.address() as { port: number }).port}`
+ const relayOrigin = base
+ vi.stubGlobal('fetch', (input: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => {
+ const url = input instanceof Request ? input.url : input.toString()
+ if (new URL(url).origin !== relayOrigin) return externalFetch(input, init)
+ const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined))
+ headers.set('x-harness-test-client', clientId)
+ return externalFetch(input, { ...init, headers })
+ })
root = mkdtempSync(join(tmpdir(), 'grid-reads-rpc-'))
gridHome = join(root, 'grid-home')
mkdirSync(gridHome, { recursive: true })
@@ -127,10 +145,29 @@ afterEach(async () => {
vi.unstubAllEnvs()
server.closeAllConnections()
await new Promise<void>((resolve) => server.close(() => resolve()))
+ vi.unstubAllGlobals()
rmSync(root, { recursive: true, force: true })
})
describe('grid_models_list', () => {
+ it('keeps another client probing the port out of the model service request log', async () => {
+ // Found by QA on a quiet machine: local discovery sent /v1/models to this fake relay.
+ const probe = await externalFetch(`${base}/v1/models`)
+ expect(probe.status).toBe(404)
+ await probe.arrayBuffer()
+ const reply = await ask('grid_models_list')
+ expect(reply).toMatchObject({ gridName: OWN, models: [{ id: 'Small-Q4', node: 'mac' }] })
+ expect(seen.map((request) => request.path)).toEqual([`/g/${OWN_ID}${OVERVIEW}`])
+ expectNoCredential()
+ })
+
+ it('still records an unsupported path sent by this test client', async () => {
+ const reply = await fetch(`${base}/v1/models`)
+ expect(reply.status).toBe(404)
+ await reply.arrayBuffer()
+ expect(seen.map((request) => request.path)).toEqual(['/v1/models'])
+ })
+
it('answers in the old shape with the three new fields, and reads the grid with no credential', async () => {
const reply = await ask('grid_models_list')