diff --git a/apps/host/server/agent/plugins/strata-search.mjs b/apps/host/server/agent/plugins/strata-search.mjs index cf5bda68..1eb612cd 100644 --- a/apps/host/server/agent/plugins/strata-search.mjs +++ b/apps/host/server/agent/plugins/strata-search.mjs @@ -16,7 +16,7 @@ export const inject = ['web']; export function apply(ctx, config = {}) { const keyOf = () => (config.keyEnv ? process.env[config.keyEnv] ?? '' : ''); // Tests only (the bridge option `searchMock`): the same search code on a pretend web, so nothing leaves the machine. - const pretend = config.mock === 'limited' ? { fetch: async url => new Response('please verify you are human: captcha', { status: /duckduckgo/.test(String(url)) ? 202 : 429 }) } : null; + const pretend = config.mock === 'offline' ? { fetch: async () => { throw new TypeError('fetch failed'); } } : config.mock === 'limited' ? { fetch: async url => new Response('please verify you are human: captcha', { status: /duckduckgo/.test(String(url)) ? 202 : 429 }) } : null; const provider = pretend ? createDuckDuckGoSearch(pretend) : config.provider === 'exa' ? createExaSearch({ key: keyOf }) : config.provider === 'perplexity' ? createPerplexitySearch({ key: keyOf }) diff --git a/apps/host/tests/web-offline.test.mjs b/apps/host/tests/web-offline.test.mjs new file mode 100644 index 00000000..5bd12108 --- /dev/null +++ b/apps/host/tests/web-offline.test.mjs @@ -0,0 +1,48 @@ +/** With no internet the assistant gets plain words from the web tools, not "fetch failed", and can go on without them. */ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtempSync, mkdirSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { createDuckDuckGoSearch, UNREACHABLE_MESSAGE } from '../server/web-search-ddg.mjs'; +import { plainToolError, withPlain } from '../server/agent/events.mjs'; +import { startScriptedWorker } from './helpers/scripted-worker.mjs'; +import { createAgentBridge } from '../server/agent/agent-bridge.mjs'; +import { resolveHarness } from '../server/agent/harness-runtime.mjs'; + +const HARNESS = process.env.STRATA_HARNESS_DIR; +const skip = !HARNESS || !resolveHarness({ env: process.env }).ok ? 'STRATA_HARNESS_DIR is not set to a built Harness' : false; + +test('search with no internet: one readable sentence that tells the model to go on without it', async () => { + let t = 5_000_000; // a pretend clock that moves when the search waits, as in web-search.test.mjs + const offline = createDuckDuckGoSearch({ fetch: async () => { throw new TypeError('fetch failed'); }, now: () => t, sleep: async ms => { t += ms; }, random: () => 0.5 }); + const error = await offline.search({ query: 'heat pumps' }).catch(e => e); + assert.equal(error.message, UNREACHABLE_MESSAGE); + assert.match(error.message, /could not reach the internet/); + assert.match(error.message, /go on with what you know/); + assert.doesNotMatch(error.message, /fetch failed|ENOTFOUND|TypeError/); +}); + +test('opening a page with no internet: the card says so in plain words and keeps the technical text behind Details', () => { + for (const raw of ['Error: web fetch failed: fetch failed', 'Error: getaddrinfo ENOTFOUND example.com', 'Error: connect ECONNREFUSED 93.184.216.34:443']) { + const text = withPlain(raw); + assert.match(text, /^That web page could not be opened\. Check the address and your internet connection\./, raw); + assert.match(text, /Details: /); + } + assert.equal(plainToolError(UNREACHABLE_MESSAGE), null, 'the search sentence is already plain and is left as it is'); +}); + +test('real Harness: the model gets the plain sentence when the search cannot reach the internet', { skip }, async t => { + const worker = await startScriptedWorker({ chunkDelayMs: 5 }); + const dir = mkdtempSync(join(tmpdir(), 'strata-weboff-')); + const ws = join(dir, 'ws'); mkdirSync(ws, { recursive: true }); + const bridge = createAgentBridge({ dataDir: dir, localInstances: () => [{ id: 'i', label: 'S', baseUrl: worker.url, modelId: 'scripted-model', context: 32768 }], connections: () => [], env: process.env, startTimeoutMs: 90_000, searchMock: 'offline' }); + t.after(async () => { await bridge.close(); await worker.close(); try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } }); + const frames = []; + await bridge.runTurn({ chatId: 'off1', message: 'SCRIPT:call web_search {"queries":["heat pumps"]}', workspacePath: ws, mode: 'bypass', onFrame: f => frames.push(f) }); + const card = frames.filter(f => f.type === 'agent-tool' && f.tool.name === 'web_search').at(-1).tool; + assert.equal(card.state, 'error'); + assert.match(card.result, /could not reach the internet/); + assert.doesNotMatch(card.result, /fetch failed/); + assert.equal(frames.at(-1).type, 'done', 'the turn ends normally'); +});