|
| 1 | +import * as Sentry from '@sentry/node'; |
| 2 | +import { BACKGROUND_CONTEXT } from '@earendil-works/chord/context'; |
| 3 | +import { Type } from '@earendil-works/pi-ai'; |
| 4 | +import { createModels } from '@earendil-works/pi-ai/models'; |
| 5 | +import { fauxAssistantMessage, fauxProvider, fauxToolCall } from '@earendil-works/pi-ai/providers/faux'; |
| 6 | +import { createRegistry, defineExtension, defineTool, Harness, MemoryStorage } from '@earendil-works/pi-durable'; |
| 7 | + |
| 8 | +const context = BACKGROUND_CONTEXT; |
| 9 | + |
| 10 | +// Six runs that do not end with an answer: |
| 11 | +// 1. the model request fails (`stopReason` error), with retries off; |
| 12 | +// 2. the provider throws, which faults the generation task; |
| 13 | +// 3. the run is aborted while the `slow` tool waits; |
| 14 | +// 4. the run is aborted while the model request is in flight; |
| 15 | +// 5. the Harness closes while the `slow` tool waits; |
| 16 | +// 6. in a second Harness, the model request fails and the Harness closes right after `wait()`. |
| 17 | +const started = new Map(); |
| 18 | +const toolStarted = job => new Promise(resolve => started.set(job, resolve)); |
| 19 | + |
| 20 | +const faux = fauxProvider({ provider: 'faux', models: [{ id: 'faux-model' }] }); |
| 21 | +const realModels = createModels(); |
| 22 | +realModels.setProvider(faux.provider); |
| 23 | +// The faux provider turns a throwing response into an error message, so the throw of a broken |
| 24 | +// provider is staged in front of pi-ai: the request after `breakNextRequest()` throws synchronously. |
| 25 | +let brokenRequests = 0; |
| 26 | +const breakNextRequest = () => brokenRequests++; |
| 27 | +const models = new Proxy(realModels, { |
| 28 | + get(target, property) { |
| 29 | + const value = Reflect.get(target, property, target); |
| 30 | + if (property === 'streamSimple') { |
| 31 | + return (...args) => { |
| 32 | + if (brokenRequests > 0) { |
| 33 | + brokenRequests--; |
| 34 | + throw new Error('provider broke'); |
| 35 | + } |
| 36 | + return value.apply(target, args); |
| 37 | + }; |
| 38 | + } |
| 39 | + return typeof value === 'function' ? value.bind(target) : value; |
| 40 | + }, |
| 41 | +}); |
| 42 | +faux.setResponses([ |
| 43 | + fauxAssistantMessage('', { stopReason: 'error', errorMessage: 'provider exploded' }), |
| 44 | + fauxAssistantMessage(fauxToolCall('slow', { job: 'abort' }, { id: 'call_abort' }), { stopReason: 'toolUse' }), |
| 45 | + async (_transcript, options) => { |
| 46 | + started.get('request')(); |
| 47 | + await new Promise(resolve => options.signal.addEventListener('abort', resolve)); |
| 48 | + return fauxAssistantMessage('partial', { stopReason: 'aborted' }); |
| 49 | + }, |
| 50 | + fauxAssistantMessage(fauxToolCall('slow', { job: 'close' }, { id: 'call_close' }), { stopReason: 'toolUse' }), |
| 51 | + fauxAssistantMessage('', { stopReason: 'error', errorMessage: 'provider exploded again' }), |
| 52 | +]); |
| 53 | + |
| 54 | +const registry = createRegistry(); |
| 55 | +registry.install( |
| 56 | + defineExtension({ |
| 57 | + name: 'app', |
| 58 | + tools: [ |
| 59 | + defineTool({ |
| 60 | + name: 'slow', |
| 61 | + description: 'Waits until it is aborted.', |
| 62 | + parameters: Type.Object({ job: Type.String() }), |
| 63 | + execute: async (args, _api, toolContext) => { |
| 64 | + started.get(args.job)(); |
| 65 | + await new Promise((_, reject) => |
| 66 | + toolContext.abortSignal.addEventListener('abort', () => reject(new Error('tool aborted'))), |
| 67 | + ); |
| 68 | + return { content: [] }; |
| 69 | + }, |
| 70 | + }), |
| 71 | + ], |
| 72 | + }), |
| 73 | +); |
| 74 | + |
| 75 | +const harness = await Harness.open( |
| 76 | + new MemoryStorage(), |
| 77 | + { models, registry, settings: { retry: { enabled: false } } }, |
| 78 | + context, |
| 79 | +); |
| 80 | +const root = await harness.root(context, { agent: { model: { provider: 'faux', modelId: 'faux-model' } } }); |
| 81 | + |
| 82 | +await (await root.submit({ type: 'input', content: 'Fail please.' }, context)).wait(context); |
| 83 | +breakNextRequest(); |
| 84 | +await (await root.submit({ type: 'input', content: 'Break please.' }, context)).wait(context); |
| 85 | + |
| 86 | +const abortRun = toolStarted('abort'); |
| 87 | +const aborted = await root.submit({ type: 'input', content: 'Run slow, then abort.' }, context); |
| 88 | +await abortRun; |
| 89 | +await root.abort(context); |
| 90 | +await aborted.wait(context); |
| 91 | + |
| 92 | +const requestRun = toolStarted('request'); |
| 93 | +const abortedRequest = await root.submit({ type: 'input', content: 'Answer slowly, then abort.' }, context); |
| 94 | +await requestRun; |
| 95 | +await root.abort(context); |
| 96 | +await abortedRequest.wait(context); |
| 97 | + |
| 98 | +const closeRun = toolStarted('close'); |
| 99 | +await root.submit({ type: 'input', content: 'Run slow, then close.' }, context); |
| 100 | +await closeRun; |
| 101 | +await harness.close(context); |
| 102 | + |
| 103 | +const second = await Harness.open( |
| 104 | + new MemoryStorage(), |
| 105 | + { models, registry, settings: { retry: { enabled: false } } }, |
| 106 | + context, |
| 107 | +); |
| 108 | +const secondRoot = await second.root(context, { agent: { model: { provider: 'faux', modelId: 'faux-model' } } }); |
| 109 | +await (await secondRoot.submit({ type: 'input', content: 'Fail, then close.' }, context)).wait(context); |
| 110 | +await second.close(context); |
| 111 | + |
| 112 | +// Everything captured during the runs is sent before this sentinel. |
| 113 | +await Sentry.flush(2000); |
| 114 | +Sentry.captureMessage('pi-durable interrupted done'); |
0 commit comments