diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 7bc4ac74449..deb4e526c78 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -7,25 +7,26 @@ /apps/sim/app/api/mothership/execute/ @simstudioai/mothership # Server-side stream orchestration, persistence, and protocol. -/apps/sim/lib/copilot/chat/ @simstudioai/mothership -/apps/sim/lib/copilot/async-runs/ @simstudioai/mothership -/apps/sim/lib/copilot/request/ @simstudioai/mothership -/apps/sim/lib/copilot/generated/ @simstudioai/mothership -/apps/sim/lib/copilot/constants.ts @simstudioai/mothership +/apps/sim/lib/mothership/chat/ @simstudioai/mothership +/apps/sim/lib/mothership/async-runs/ @simstudioai/mothership +/apps/sim/lib/mothership/request/ @simstudioai/mothership +/apps/sim/lib/mothership/generated/ @simstudioai/mothership +/apps/sim/lib/mothership/observability/ @simstudioai/mothership +/apps/sim/lib/mothership/constants.ts @simstudioai/mothership /apps/sim/lib/core/utils/sse.ts @simstudioai/mothership # Stream-time tool execution, confirmations, resource persistence, and handlers. -/apps/sim/lib/copilot/tool-executor/ @simstudioai/mothership -/apps/sim/lib/copilot/tools/ @simstudioai/mothership -/apps/sim/lib/copilot/persistence/ @simstudioai/mothership -/apps/sim/lib/copilot/resources/ @simstudioai/mothership +/apps/sim/lib/mothership/tool-executor/ @simstudioai/mothership +/apps/sim/lib/mothership/tools/ @simstudioai/mothership +/apps/sim/lib/mothership/persistence/ @simstudioai/mothership +/apps/sim/lib/mothership/resources/ @simstudioai/mothership # Client-side stream consumption, hydration, and reconnect. /apps/sim/app/workspace/*/home/hooks/index.ts @simstudioai/mothership /apps/sim/app/workspace/*/home/hooks/use-chat.ts @simstudioai/mothership /apps/sim/app/workspace/*/home/hooks/preview/ @simstudioai/mothership /apps/sim/app/workspace/*/home/hooks/stream/ @simstudioai/mothership -/apps/sim/hooks/queries/tasks.ts @simstudioai/mothership +/apps/sim/hooks/queries/mothership-tasks.ts @simstudioai/mothership # Dependency manifests and package-manager config. Any change here — adding, # removing, or bumping a dependency, or altering install/security settings — diff --git a/apps/sim/app/api/billing/update-cost/replay.live.test.ts b/apps/sim/app/api/billing/update-cost/replay.live.test.ts deleted file mode 100644 index 2d7be627d8f..00000000000 --- a/apps/sim/app/api/billing/update-cost/replay.live.test.ts +++ /dev/null @@ -1,221 +0,0 @@ -import { type ExecFileException, execFile } from 'node:child_process' -import { createServer } from 'node:http' -import { promisify } from 'node:util' -import { resetEnvFlagsMock, resetEnvMock, setEnv, setEnvFlags } from '@sim/testing' -import { auditMock } from '@sim/testing/mocks/audit.mock' -import { billingAccessMock, billingAccessMockFns } from '@sim/testing/mocks/billing-access.mock' -import { billingCoreMock, billingCoreMockFns } from '@sim/testing/mocks/billing-core.mock' -import { billingOutboxHandlersMock } from '@sim/testing/mocks/billing-outbox-handlers.mock' -import { billingPlanMock } from '@sim/testing/mocks/billing-plan.mock' -import { - billingPlanHelpersMock, - billingPlanHelpersMockFns, -} from '@sim/testing/mocks/billing-plan-helpers.mock' -import { - billingSubscriptionMock, - billingSubscriptionMockFns, -} from '@sim/testing/mocks/billing-subscription.mock' -import { - billingSubscriptionUtilsMock, - billingSubscriptionUtilsMockFns, -} from '@sim/testing/mocks/billing-subscription-utils.mock' -import { billingUsageMonitorMock } from '@sim/testing/mocks/billing-usage-monitor.mock' -import { mothershipOtelMock } from '@sim/testing/mocks/mothership-otel.mock' -import { outboxServiceMock } from '@sim/testing/mocks/outbox-service.mock' -import { posthogServerMock } from '@sim/testing/mocks/posthog-server.mock' -import { NextRequest } from 'next/server' -import type { Sql } from 'postgres' -import { afterAll, describe, expect, it, vi } from 'vitest' - -const state = vi.hoisted(() => ({ - databaseUrl: process.env.BILLING_REPLAY_IT_DATABASE_URL, - copilotDirectory: process.env.BILLING_REPLAY_COPILOT_DIR, - client: null as Sql | null, - schema: `billing_callback_${process.pid}`, - temporaryFailures: 1, -})) - -vi.unmock('drizzle-orm') -vi.unmock('@sim/db/schema') -vi.mock('@sim/db', async () => { - const { drizzle } = await import('drizzle-orm/postgres-js') - const { default: postgres } = await import('postgres') - const client = postgres(state.databaseUrl ?? 'postgres://127.0.0.1:1/unused', { - max: 2, - connection: { search_path: state.schema }, - onnotice: () => {}, - }) - state.client = client - const db = drizzle(client) - return { db, dbReplica: db, dbFor: () => db } -}) - -/** Subscription and payment-provider fixtures; callback, ledger and replay code are real. */ -vi.mock('@/lib/billing/core/subscription', () => billingSubscriptionMock) -vi.mock('@/lib/billing/core/plan', () => billingPlanMock) -vi.mock('@/lib/billing/core/access', () => billingAccessMock) -vi.mock('@/lib/billing/core/billing', () => billingCoreMock) -vi.mock('@/lib/billing/cycle-close', () => ({ isSubscriptionCycleCloseCurrent: async () => true })) -vi.mock('@/lib/billing/plan-helpers', () => billingPlanHelpersMock) -vi.mock('@/lib/billing/subscriptions/utils', () => billingSubscriptionUtilsMock) -vi.mock('@/lib/billing/calculations/usage-monitor', () => billingUsageMonitorMock) -vi.mock('@/lib/billing/webhooks/outbox-handlers', () => billingOutboxHandlersMock) -vi.mock('@/lib/core/outbox/service', () => outboxServiceMock) -vi.mock('@sim/audit', () => auditMock) -vi.mock('@/lib/posthog/server', () => posthogServerMock) -vi.mock('@/lib/mothership/request/otel', () => mothershipOtelMock) - -import { POST } from '@/app/api/billing/update-cost/route' - -const run = promisify(execFile) - -const subscription = async (userId: string) => { - if (userId === 'billing-replay-transient' && state.temporaryFailures-- > 0) { - throw new Error('Temporary subscription lookup failure') - } - return { - id: 'billing-replay-subscription', - referenceId: userId, - plan: 'pro', - status: 'active', - periodStart: new Date('2025-02-01T00:00:00.000Z'), - periodEnd: new Date('2025-03-01T00:00:00.000Z'), - } -} -billingSubscriptionMockFns.mockGetHighestPrioritySubscription.mockImplementation(subscription) -billingSubscriptionMockFns.mockGetHighestPriorityPersonalSubscription.mockImplementation( - subscription -) -billingAccessMockFns.mockGetEffectiveBillingStatus.mockResolvedValue({ billingBlocked: false }) -billingAccessMockFns.mockIsOrganizationBillingBlocked.mockResolvedValue(false) -billingCoreMockFns.mockCalculateSubscriptionOverage.mockResolvedValue(0) -billingPlanHelpersMockFns.mockIsEnterprise.mockReturnValue(false) -billingPlanHelpersMockFns.mockIsFree.mockReturnValue(false) -billingSubscriptionUtilsMockFns.mockHasUsableSubscriptionAccess.mockReturnValue(true) -billingSubscriptionUtilsMockFns.mockIsOrgScopedSubscription.mockReturnValue(false) - -afterAll(async () => { - await state.client?.end() - resetEnvMock() - resetEnvFlagsMock() -}) - -/** - * Requires an isolated localhost PostgreSQL database and the Copilot checkout. - * Runs the actual Go client/reconciler/repository through HTTP into this route, - * with real header validation, cumulative ledger SQL and period classification. - */ -describe.skipIf(!state.databaseUrl || !state.copilotDirectory)( - 'cross-service billing replay', - () => { - it('quarantines expired charges durably and recovers temporary failures without double billing', async () => { - const databaseUrl = new URL(state.databaseUrl as string) - expect(['127.0.0.1', 'localhost']).toContain(databaseUrl.hostname) - const client = state.client - if (!client) throw new Error('Test database client was not initialized') - setEnv({ INTERNAL_API_SECRET: 'billing-replay-local-secret' }) - setEnvFlags({ isBillingEnabled: true, isHosted: true }) - await client.unsafe(`CREATE SCHEMA "${state.schema}"`) - const statuses: number[] = [] - const server = createServer(async (request, response) => { - try { - const chunks: Buffer[] = [] - let bytes = 0 - for await (const chunk of request) { - const buffer = Buffer.from(chunk) - bytes += buffer.length - if (bytes > 16384) throw new Error('Test request exceeds the callback fixture limit') - chunks.push(buffer) - } - const headers = new Headers() - for (const [key, value] of Object.entries(request.headers)) { - if (typeof value === 'string') headers.set(key, value) - } - const result = await POST( - new NextRequest(`http://127.0.0.1${request.url}`, { - method: 'POST', - headers, - body: Buffer.concat(chunks).toString(), - }) - ) - statuses.push(result.status) - response.writeHead(result.status, Object.fromEntries(result.headers)) - response.end(await result.text()) - } catch (error) { - response.writeHead(500) - response.end(String(error)) - } - }) - try { - await client.unsafe(`CREATE TABLE "user" (id text PRIMARY KEY); - INSERT INTO "user" (id) VALUES ('billing-replay-actor'), ('billing-replay-transient'); - CREATE TABLE usage_log ( - id text PRIMARY KEY, user_id text NOT NULL, category text NOT NULL, source text NOT NULL, - description text NOT NULL, metadata jsonb, cost numeric NOT NULL, event_key text, - billing_entity_type text, billing_entity_id text, billing_period_start timestamp, - billing_period_end timestamp, workspace_id text, workflow_id text, execution_id text, - created_at timestamp NOT NULL DEFAULT now(), - CONSTRAINT usage_log_user_id_user_id_fk FOREIGN KEY (user_id) REFERENCES "user"(id) - ); CREATE UNIQUE INDEX usage_log_event_key_unique ON usage_log(event_key) WHERE event_key IS NOT NULL`) - await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) - const address = server.address() - if (!address || typeof address === 'string') throw new Error('Expected HTTP server port') - const result = await run( - 'go', - [ - 'test', - './internal/analytics', - '-run', - '^TestBillingReplayCrossService$', - '-count=1', - '-v', - ], - { - cwd: state.copilotDirectory, - env: { - ...process.env, - INTERNAL_API_SECRET: 'billing-replay-local-secret', - BILLING_REPLAY_SIM_URL: `http://127.0.0.1:${address.port}`, - BILLING_REPLAY_IT_DATABASE_URL: state.databaseUrl, - }, - timeout: 60_000, - maxBuffer: 1024 * 1024, - } - ).catch((error: ExecFileException & { stdout?: string; stderr?: string }) => { - throw new Error([error.message, error.stdout, error.stderr].filter(Boolean).join('\n'), { - cause: error, - }) - }) - expect(result.stdout).toContain('--- PASS: TestBillingReplayCrossService') - expect(statuses.filter((status) => status === 503)).toHaveLength(1) - expect(statuses.filter((status) => status === 409)).toHaveLength(10) - const rows = - await client`SELECT user_id, cost, billing_period_start::text AS period_start, billing_period_end::text AS period_end FROM usage_log ORDER BY user_id` - expect(rows).toHaveLength(4) - for (const row of rows) { - expect(Number(row.cost)).toBe(1.25) - expect(row.period_start).toBe( - row.user_id === 'billing-replay-transient' - ? '2025-02-01 00:00:00' - : '2025-01-01 00:00:00' - ) - expect(row.period_end).toBe( - row.user_id === 'billing-replay-transient' - ? '2025-03-01 00:00:00' - : '2025-02-01 00:00:00' - ) - } - } finally { - try { - if (server.listening) { - await new Promise((resolve, reject) => - server.close((error) => (error ? reject(error) : resolve())) - ) - } - } finally { - await client.unsafe(`DROP SCHEMA "${state.schema}" CASCADE`) - } - } - }, 90_000) - } -) diff --git a/apps/sim/app/api/billing/update-cost/route.test.ts b/apps/sim/app/api/billing/update-cost/route.test.ts index 4ffd160166d..2a2ee681ae4 100644 --- a/apps/sim/app/api/billing/update-cost/route.test.ts +++ b/apps/sim/app/api/billing/update-cost/route.test.ts @@ -169,7 +169,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => { }) }) - it('keeps the worker callback protocol aligned with the Go-produced wire constants', () => { + it('keeps the callback protocol aligned with the worker wire constants', () => { expect(Object.values(BillingProtocol).sort()).toEqual( Object.values(COPILOT_BILLING_PROTOCOL).sort() ) @@ -230,7 +230,7 @@ describe('POST /api/billing/update-cost — workspaceId attribution', () => { expect(mockRecordCumulativeUsage).not.toHaveBeenCalled() }) - it('returns no-op success for markerless local self-hosted Go when billing is disabled', async () => { + it('returns no-op success for a markerless self-hosted caller when billing is disabled', async () => { setEnvFlags({ isBillingEnabled: false }) const res = await POST( diff --git a/apps/sim/app/api/billing/update-cost/route.ts b/apps/sim/app/api/billing/update-cost/route.ts index 76ca4f278b6..08c7f941e1c 100644 --- a/apps/sim/app/api/billing/update-cost/route.ts +++ b/apps/sim/app/api/billing/update-cost/route.ts @@ -47,12 +47,12 @@ import { generateRequestId } from '@/lib/core/utils/request' import { withRouteHandler } from '@/lib/core/utils/with-route-handler' import { BillingInFlightCode } from '@/lib/mothership/generated/billing' import { BILLING_CALLBACK_OUTCOME } from '@/lib/mothership/generated/billing-protocol-v1' -import { BillingRouteOutcome } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' import { recordJobRunSpend } from '@/lib/mothership/jobs/budget' +import { BillingRouteOutcome } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { checkInternalApiKey } from '@/lib/mothership/request/http' -import { withIncomingGoSpan } from '@/lib/mothership/request/otel' +import { withIncomingWorkerSpan } from '@/lib/mothership/request/otel' const logger = createLogger('BillingUpdateCostAPI') /** @@ -174,13 +174,13 @@ function getBillingResolution( * POST /api/billing/update-cost * Update user cost with a pre-calculated cost value (internal API key auth required) * - * Parented under the Go-side `sim.update_cost` span via W3C traceparent - * propagation. Every mothership request that bills should therefore show - * the Go client span AND this Sim server span sharing one trace, with + * Parented under the worker's span via W3C traceparent propagation. Every + * mothership request that bills should therefore show the worker's span AND + * this Sim server span sharing one trace, with * the actual usage/overage work nested below. */ export const POST = withRouteHandler((req: NextRequest) => - withIncomingGoSpan( + withIncomingWorkerSpan( req.headers, TraceSpan.CopilotBillingUpdateCost, { @@ -417,7 +417,7 @@ async function updateCostInner(req: NextRequest, span: Span): Promise - withIncomingGoSpan( + withIncomingWorkerSpan( req.headers, TraceSpan.CopilotAuthValidateApiKey, { diff --git a/apps/sim/app/api/copilot/byok/validate/route.ts b/apps/sim/app/api/copilot/byok/validate/route.ts index 7d88ea56a46..72399e940ba 100644 --- a/apps/sim/app/api/copilot/byok/validate/route.ts +++ b/apps/sim/app/api/copilot/byok/validate/route.ts @@ -12,7 +12,7 @@ const logger = createLogger('CopilotByokValidate') /** * Authoritative entitlement gate for enterprise BYOK, called server-to-server by - * the mothership (Go) before it uses a workspace's own provider key. Gated by + * the mothership worker before it uses a workspace's own provider key. Gated by * INTERNAL_API_SECRET — never exposed to the browser. * * Returns 200 when EITHER: @@ -22,7 +22,7 @@ const logger = createLogger('CopilotByokValidate') * another org's stored key to be used) AND the workspace is on an * enterprise plan. * - * Any other case returns 403 (not entitled) or 401 (bad internal auth). The Go + * Any other case returns 403 (not entitled) or 401 (bad internal auth). The * caller fails closed to hosted keys on anything but a 200. */ export const POST = withRouteHandler(async (req: NextRequest) => { diff --git a/apps/sim/app/api/copilot/chat/steer/route.test.ts b/apps/sim/app/api/copilot/chat/steer/route.test.ts index f63075e7322..9c6fa8008d9 100644 --- a/apps/sim/app/api/copilot/chat/steer/route.test.ts +++ b/apps/sim/app/api/copilot/chat/steer/route.test.ts @@ -66,7 +66,7 @@ describe('POST /api/copilot/chat/steer', () => { mockAppend.mockResolvedValue(undefined) }) - it('returns 409 when Go rejects the steer so the client falls back to a normal send', async () => { + it('returns 409 when the worker rejects the steer so the client falls back to a normal send', async () => { mockRequestStreamSteering.mockResolvedValue({ queued: false, status: 429 }) const response = await POST(steerRequest()) diff --git a/apps/sim/app/api/copilot/chat/steer/route.ts b/apps/sim/app/api/copilot/chat/steer/route.ts index 7d20e65273c..43dce70974f 100644 --- a/apps/sim/app/api/copilot/chat/steer/route.ts +++ b/apps/sim/app/api/copilot/chat/steer/route.ts @@ -7,13 +7,13 @@ import { internalRateLimits, internalSessionAuth, } from '@/lib/api/server/routes' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { runControlOperations, SteeringNotQueuedError, steerRun, } from '@/lib/mothership/request/application/controls' -import { withIncomingGoSpan } from '@/lib/mothership/request/otel' +import { withIncomingWorkerSpan } from '@/lib/mothership/request/otel' const handler = defineInternalJsonRoute({ contract: copilotChatSteerContract, @@ -26,7 +26,7 @@ const handler = defineInternalJsonRoute({ errorPolicy: { project: (error) => error instanceof SteeringNotQueuedError - ? internalErrorResponse(409, { ok: false, queued: false, goStatus: error.goStatus }) + ? internalErrorResponse(409, { ok: false, queued: false, workerStatus: error.workerStatus }) : internalOrchestrationErrorPolicy.project(error), }, mapInput: ({ body }) => body, @@ -34,6 +34,6 @@ const handler = defineInternalJsonRoute({ }) export const POST = (request: NextRequest, context?: Parameters[1]) => - withIncomingGoSpan(request.headers, TraceSpan.CopilotChatSteerStream, undefined, () => + withIncomingWorkerSpan(request.headers, TraceSpan.CopilotChatSteerStream, undefined, () => handler(request, context) ) diff --git a/apps/sim/app/api/copilot/confirm/route.ts b/apps/sim/app/api/copilot/confirm/route.ts index 02d96580eeb..05d56b77e77 100644 --- a/apps/sim/app/api/copilot/confirm/route.ts +++ b/apps/sim/app/api/copilot/confirm/route.ts @@ -23,9 +23,9 @@ import { getClaimedWorkflowExecutionId, getRunSegment, } from '@/lib/mothership/async-runs/repository' -import { CopilotConfirmOutcome } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { CopilotConfirmOutcome } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { authenticateCopilotRequestSessionOnly, createInternalServerErrorResponse, @@ -33,7 +33,7 @@ import { createRequestTracker, createUnauthorizedResponse, } from '@/lib/mothership/request/http' -import { withIncomingGoSpan } from '@/lib/mothership/request/otel' +import { withIncomingWorkerSpan } from '@/lib/mothership/request/otel' import { sealClientToolSettlement } from '@/lib/mothership/request/tools/client-completion-seal.server' import { type ClientToolSettlementGuard, @@ -165,7 +165,7 @@ async function updateToolCallStatus( export const POST = withRouteHandler((req: NextRequest) => { const tracker = createRequestTracker() - return withIncomingGoSpan( + return withIncomingWorkerSpan( req.headers, TraceSpan.CopilotConfirmToolResult, { [TraceAttr.RequestId]: tracker.requestId }, diff --git a/apps/sim/app/api/copilot/tool-permission/route.ts b/apps/sim/app/api/copilot/tool-permission/route.ts index a9121dbc06a..d347f02acb8 100644 --- a/apps/sim/app/api/copilot/tool-permission/route.ts +++ b/apps/sim/app/api/copilot/tool-permission/route.ts @@ -11,8 +11,8 @@ import { getRunSegment, recordToolPermissionDecision, } from '@/lib/mothership/async-runs/repository' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { publishToolPermissionDecision, TOOL_PERMISSION_DECISION, @@ -30,7 +30,7 @@ import { createRequestTracker, createUnauthorizedResponse, } from '@/lib/mothership/request/http' -import { withIncomingGoSpan } from '@/lib/mothership/request/otel' +import { withIncomingWorkerSpan } from '@/lib/mothership/request/otel' import { isWorkspaceCapabilityWithheld } from '@/lib/permission-groups/capability-assertions' const logger = createLogger('CopilotToolPermissionAPI') @@ -150,7 +150,7 @@ async function applyDecision( export const POST = withRouteHandler((req: NextRequest) => { const tracker = createRequestTracker() - return withIncomingGoSpan( + return withIncomingWorkerSpan( req.headers, TraceSpan.CopilotToolPermissionDecide, { [TraceAttr.RequestId]: tracker.requestId }, diff --git a/apps/sim/app/api/copilot/tools/execute/route.test.ts b/apps/sim/app/api/copilot/tools/execute/route.test.ts index 01c4d001e12..564e91831de 100644 --- a/apps/sim/app/api/copilot/tools/execute/route.test.ts +++ b/apps/sim/app/api/copilot/tools/execute/route.test.ts @@ -102,7 +102,7 @@ describe('POST /api/copilot/tools/execute (in-band)', () => { }) /** - * The generated key is a browser-only artifact. This lane's response is what Go feeds the + * The generated key is a browser-only artifact. This lane's response is what the worker feeds the * model, so it must carry the same projection the resume lane produces — the status message * alone. A `key` field here would put the plaintext credential in model context. */ @@ -163,7 +163,7 @@ describe('POST /api/copilot/tools/execute (in-band)', () => { }) }) /** - * Go's body cannot carry the turn's secret authority — the turn payload never did — so a + * The worker's body cannot carry the turn's secret authority — the turn payload never did — so a * context rebuilt from it alone read as "every secret, as the body's user" for a turn that * was admitted with less. */ diff --git a/apps/sim/app/api/copilot/tools/execute/route.ts b/apps/sim/app/api/copilot/tools/execute/route.ts index 703708a1a09..45a660b834b 100644 --- a/apps/sim/app/api/copilot/tools/execute/route.ts +++ b/apps/sim/app/api/copilot/tools/execute/route.ts @@ -9,10 +9,10 @@ import { getRunSecretAuthority } from '@/lib/mothership/async-runs/repository' import { toolResultForModel } from '@/lib/mothership/chat/sim-key-redaction' import { prepareCopilotEnvironmentContext } from '@/lib/mothership/environment-context' import { MothershipStreamV1ToolOutcome } from '@/lib/mothership/generated/mothership-stream-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { checkInternalApiKey } from '@/lib/mothership/request/http' -import { withIncomingGoSpan } from '@/lib/mothership/request/otel' +import { withIncomingWorkerSpan } from '@/lib/mothership/request/otel' import { describeWithholdingCause, inspectToolResultForCopilot, @@ -36,7 +36,7 @@ const logger = createLogger('CopilotToolExecuteInternalAPI') * across a turn. Keyed by the turn (messageId) so one background lane shares * one registry; idle-evicted since nothing signals turn end on this route. * - * The turn's secret authority, which Go's body cannot carry, is cached beside it + * The turn's secret authority, which the worker's body cannot carry, is cached beside it * (see {@link getRunSecretAuthority}). */ const turnContextCache = new LRUCache< @@ -68,23 +68,23 @@ async function getTurnToolContext( return entry } -// POST /api/copilot/tools/execute — internal (Go → Sim) in-band execution of +// POST /api/copilot/tools/execute — internal (worker → Sim) in-band execution of // one sim-server tool announced on a LIVE mothership turn. This is what lets // background (async) subagents — and the main lane while background agents are -// running — use sim-executed tools without a checkpoint pause: Go calls here +// running — use sim-executed tools without a checkpoint pause: the worker calls here // synchronously instead of parking the turn, and the tool runs through the // same server tool router the resume driver uses. Trusted server-to-server -// only: Go supplies the acting user, proven by the internal API secret. +// only: the worker supplies the acting user, proven by the internal API secret. // // Results cross a model boundary here just as they do in the resume driver, so // this route mirrors its provenance discipline: a per-call registry fork feeds -// the handler, the settled result is projected before it returns to Go, and +// the handler, the settled result is projected before it returns to the worker, and // the fork is merged back only when the projection was safe and the fork // stayed complete. Without this, every result crossed unprojected and every // thrown error was replaced by the opaque "could not be returned safely" // sentinel (the projection fails closed on a missing registry). export const POST = withRouteHandler((request: NextRequest) => - withIncomingGoSpan( + withIncomingWorkerSpan( request.headers, TraceSpan.CopilotToolsExecuteInband, undefined, @@ -275,7 +275,7 @@ export const POST = withRouteHandler((request: NextRequest) => }) } /** - * The response IS the model-facing channel on this lane — Go relays it straight into + * The response IS the model-facing channel on this lane — the worker relays it straight into * the turn — so it carries the same projection the resume lane's * `getToolCallTerminalData` produces, not the raw handler output. Without this, * `generate_api_key`'s freshly minted plaintext key crossed to the model here while diff --git a/apps/sim/app/api/mothership/chat/abort/route.ts b/apps/sim/app/api/mothership/chat/abort/route.ts index b10d43b2550..a828d4a575b 100644 --- a/apps/sim/app/api/mothership/chat/abort/route.ts +++ b/apps/sim/app/api/mothership/chat/abort/route.ts @@ -6,9 +6,9 @@ import { internalRateLimits, internalSessionAuth, } from '@/lib/api/server/routes' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { abortRun, runControlOperations } from '@/lib/mothership/request/application/controls' -import { withIncomingGoSpan } from '@/lib/mothership/request/otel' +import { withIncomingWorkerSpan } from '@/lib/mothership/request/otel' const handler = defineInternalJsonRoute({ contract: mothershipChatAbortContract, @@ -24,6 +24,6 @@ const handler = defineInternalJsonRoute({ }) export const POST = (request: NextRequest, context?: Parameters[1]) => - withIncomingGoSpan(request.headers, TraceSpan.CopilotChatAbortStream, undefined, () => + withIncomingWorkerSpan(request.headers, TraceSpan.CopilotChatAbortStream, undefined, () => handler(request, context) ) diff --git a/apps/sim/app/api/mothership/chat/stop/route.ts b/apps/sim/app/api/mothership/chat/stop/route.ts index 8cfe51b65d1..3bd5ce1ae0a 100644 --- a/apps/sim/app/api/mothership/chat/stop/route.ts +++ b/apps/sim/app/api/mothership/chat/stop/route.ts @@ -19,10 +19,10 @@ import { publishChatStatusChanged } from '@/lib/mothership/chat-status' import { CopilotChatFinalizeOutcome, CopilotStopOutcome, -} from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' -import { withIncomingGoSpan } from '@/lib/mothership/request/otel' +} from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' +import { withIncomingWorkerSpan } from '@/lib/mothership/request/otel' const logger = createLogger('CopilotChatStopAPI') @@ -31,7 +31,7 @@ const logger = createLogger('CopilotChatStopAPI') * the aborted server stream unwinding, not this handler. */ export const POST = withRouteHandler((req: NextRequest) => - withIncomingGoSpan(req.headers, TraceSpan.CopilotChatStopStream, undefined, async (span) => { + withIncomingWorkerSpan(req.headers, TraceSpan.CopilotChatStopStream, undefined, async (span) => { try { const session = await getSession() if (!session?.user?.id) { diff --git a/apps/sim/app/api/mothership/chat/stream/route.test.ts b/apps/sim/app/api/mothership/chat/stream/route.test.ts index 969b82368da..fe03c951c5c 100644 --- a/apps/sim/app/api/mothership/chat/stream/route.test.ts +++ b/apps/sim/app/api/mothership/chat/stream/route.test.ts @@ -8,9 +8,9 @@ import { MothershipStreamV1CompletionStatus, MothershipStreamV1EventType, } from '@/lib/mothership/generated/mothership-stream-v1' -import { CopilotResumeOutcome } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { CopilotResumeOutcome } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' const { getLatestRunForStream, readEvents, readFilePreviewSessions, findReplayGap } = vi.hoisted( () => ({ diff --git a/apps/sim/app/api/mothership/chat/stream/route.ts b/apps/sim/app/api/mothership/chat/stream/route.ts index 15b35846352..56161c4ce27 100644 --- a/apps/sim/app/api/mothership/chat/stream/route.ts +++ b/apps/sim/app/api/mothership/chat/stream/route.ts @@ -25,11 +25,10 @@ import { import { CopilotResumeOutcome, CopilotTransport, -} from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +} from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { readChatStream } from '@/lib/mothership/request/application/recover-stream' -import { contextFromRequestHeaders } from '@/lib/mothership/request/go/propagation' import { getCopilotTracer, markSpanForError } from '@/lib/mothership/request/otel' import { createEvent, @@ -48,6 +47,7 @@ import { SSE_RESPONSE_HEADERS, } from '@/lib/mothership/request/session' import { toReplayEnvelope, toStreamBatchEvent } from '@/lib/mothership/request/session/types' +import { contextFromRequestHeaders } from '@/lib/mothership/request/worker/propagation' export const maxDuration = 3600 diff --git a/apps/sim/app/api/mothership/chats/[chatId]/fork/route.test.ts b/apps/sim/app/api/mothership/chats/[chatId]/fork/route.test.ts index 58abf60d17c..a3cdd83c746 100644 --- a/apps/sim/app/api/mothership/chats/[chatId]/fork/route.test.ts +++ b/apps/sim/app/api/mothership/chats/[chatId]/fork/route.test.ts @@ -22,9 +22,9 @@ import { mothershipChatStatusMockFns, } from '@sim/testing/mocks/mothership-chat-status.mock' import { - mothershipGoFetchMock, - mothershipGoFetchMockFns, -} from '@sim/testing/mocks/mothership-go-fetch.mock' + mothershipWorkerFetchMock, + mothershipWorkerFetchMockFns, +} from '@sim/testing/mocks/mothership-worker-fetch.mock' import { permissionGroupsResolveMock, permissionGroupsResolveMockFns, @@ -76,7 +76,7 @@ vi.mock('@/lib/mothership/chat/messages-store', () => mothershipChatMessagesMock vi.mock('@/lib/mothership/chat-status', () => mothershipChatStatusMock) -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) @@ -87,7 +87,7 @@ vi.mock('@/lib/workspaces/permissions/utils', () => permissionsMock) import { POST } from '@/app/api/mothership/chats/[chatId]/fork/route' mothershipAgentUrlMockFns.mockGetMothershipBaseURL.mockResolvedValue('http://mothership.test') -const { mockFetchGo } = mothershipGoFetchMockFns +const { mockFetchWorker } = mothershipWorkerFetchMockFns const { mockAppendCopilotChatMessages } = mothershipChatMessagesMockFns const { mockLoadCopilotChatMessages } = mothershipChatLifecycleMockFns const { mockPublishChatStatusChanged: mockPublishStatusChanged } = mothershipChatStatusMockFns @@ -177,7 +177,7 @@ describe('POST /api/mothership/chats/[chatId]/fork', () => { mockPersistChatFileCopies.mockResolvedValue(undefined) mockAssertActiveWorkspaceAccess.mockResolvedValue(undefined) mockPermissionConfig.mockResolvedValue(null) - mockFetchGo.mockImplementation(async (_url: string, options: { body: string }) => { + mockFetchWorker.mockImplementation(async (_url: string, options: { body: string }) => { const request = JSON.parse(options.body) return Response.json({ chatId: request.newChatId, sourceThroughSeq: 3 }) }) @@ -211,7 +211,7 @@ describe('POST /api/mothership/chats/[chatId]/fork', () => { ]) const res = await POST(createRequest('chat-1'), createRouteContext({ chatId: 'chat-1' })) expect(res.status).toBe(200) - const request = JSON.parse(mockFetchGo.mock.calls[0][1].body) + const request = JSON.parse(mockFetchWorker.mock.calls[0][1].body) expect(request).toMatchObject({ organizationId: 'org-1', userId: 'user-1', @@ -240,7 +240,7 @@ describe('POST /api/mothership/chats/[chatId]/fork', () => { expect(res.status).toBe(404) expect(mockAppendCopilotChatMessages).not.toHaveBeenCalled() expect(mockPublishStatusChanged).not.toHaveBeenCalled() - expect(mockFetchGo.mock.calls.map(([url]) => url)).toEqual([ + expect(mockFetchWorker.mock.calls.map(([url]) => url)).toEqual([ 'http://mothership.test/api/chats/fork', 'http://mothership.test/api/tasks/cleanup', ]) @@ -257,7 +257,7 @@ describe('POST /api/mothership/chats/[chatId]/fork', () => { if (revocation === 'capability') mockPermissionConfig.mockResolvedValue({ hideCopilot: true }) const res = await POST(createRequest('chat-1'), createRouteContext({ chatId: 'chat-1' })) expect(res.status).toBe(404) - expect(mockFetchGo).not.toHaveBeenCalled() + expect(mockFetchWorker).not.toHaveBeenCalled() expect(mockLoadCopilotChatMessages).not.toHaveBeenCalled() expect(dbChainMockFns.transaction).not.toHaveBeenCalled() } @@ -303,12 +303,12 @@ describe('POST /api/mothership/chats/[chatId]/fork', () => { expect(mockExecuteChatFileBlobCopies).toHaveBeenCalledWith(blobTasks) - const goCall = mockFetchGo.mock.calls[0] - expect(goCall[0]).toBe('http://mothership.test/api/chats/fork') - const goBody = JSON.parse(goCall[1].body) + const workerCall = mockFetchWorker.mock.calls[0] + expect(workerCall[0]).toBe('http://mothership.test/api/chats/fork') + const workerBody = JSON.parse(workerCall[1].body) // The copilot service only knows USER message ids, so the clone cut is the // kept slice's last user message (msg-1), not the clicked assistant (msg-2). - expect(goBody).toEqual({ + expect(workerBody).toEqual({ sourceChatId: 'chat-1', newChatId: body.id, upToMessageId: 'msg-1', @@ -340,7 +340,7 @@ describe('POST /api/mothership/chats/[chatId]/fork', () => { }) it('does not publish a chat when the worker copy fails', async () => { - mockFetchGo.mockRejectedValue(new Error('mothership unreachable')) + mockFetchWorker.mockRejectedValue(new Error('mothership unreachable')) const res = await POST(createRequest('chat-1'), createRouteContext({ chatId: 'chat-1' })) expect(res.status).toBe(500) expect(dbChainMockFns.transaction).not.toHaveBeenCalled() @@ -348,7 +348,7 @@ describe('POST /api/mothership/chats/[chatId]/fork', () => { }) it.each([404, 409, 413])('passes a worker %i refusal through', async (status) => { - mockFetchGo.mockResolvedValue(Response.json({ error: 'refused' }, { status })) + mockFetchWorker.mockResolvedValue(Response.json({ error: 'refused' }, { status })) const res = await POST(createRequest('chat-1'), createRouteContext({ chatId: 'chat-1' })) expect(res.status).toBe(status) }) @@ -377,7 +377,7 @@ describe('POST /api/mothership/chats/[chatId]/fork', () => { dbChainMockFns.transaction.mockRejectedValueOnce(new Error('connection lost')) const res = await POST(createRequest('chat-1'), createRouteContext({ chatId: 'chat-1' })) expect(res.status).toBe(500) - const [fork, discard] = mockFetchGo.mock.calls + const [fork, discard] = mockFetchWorker.mock.calls expect(fork[0]).toBe('http://mothership.test/api/chats/fork') expect(discard[0]).toBe('http://mothership.test/api/tasks/cleanup') expect(JSON.parse(discard[1].body)).toEqual({ @@ -416,7 +416,7 @@ describe('POST /api/mothership/chats/[chatId]/fork', () => { expect(mockAppendCopilotChatMessages.mock.calls[0][1][0].content).toBe( `See ![cat](/api/files/view/${OLD_FILE_ID})` ) - const forkRequest = JSON.parse(mockFetchGo.mock.calls[0][1].body) + const forkRequest = JSON.parse(mockFetchWorker.mock.calls[0][1].body) expect(forkRequest.fileIds).toEqual({}) expect(forkRequest.fileKeys).toEqual({}) }) diff --git a/apps/sim/app/api/mothership/execute/route.ts b/apps/sim/app/api/mothership/execute/route.ts index a48442f1941..16a16eff010 100644 --- a/apps/sim/app/api/mothership/execute/route.ts +++ b/apps/sim/app/api/mothership/execute/route.ts @@ -369,7 +369,7 @@ export const POST = withRouteHandler(async (req: NextRequest) => { workflowId, executionId, simRequestId: requestId, - goRoute: '/api/mothership/execute', + workerRoute: '/api/mothership/execute', autoExecuteTools: true, mcpBlockId: delegation.mcpBlockId, executorDelegationOrigin: delegation, diff --git a/apps/sim/app/api/telemetry/route.ts b/apps/sim/app/api/telemetry/route.ts index 3a974f128a4..2077117bc93 100644 --- a/apps/sim/app/api/telemetry/route.ts +++ b/apps/sim/app/api/telemetry/route.ts @@ -8,6 +8,7 @@ import { isProd } from '@/lib/core/config/env-flags' import { enforceIpRateLimit } from '@/lib/core/rate-limiter' import { withRouteHandler } from '@/lib/core/utils/with-route-handler' import { isTelemetryDisabled, normalizeOtlpTracesUrl } from '@/lib/monitoring/otlp' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' import telemetryConfig from '@/telemetry.config' const logger = createLogger('TelemetryAPI') @@ -76,13 +77,13 @@ async function forwardToCollector(data: Record): Promise { userId: 'user-1', workspaceId: 'workspace-1', chatId: SERVER_ISSUED_CHAT_ID, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', autoExecuteTools: true, interactive: false, // Hosted execution refuses to run without attribution, so the resolved diff --git a/apps/sim/app/api/v2/chat/route.ts b/apps/sim/app/api/v2/chat/route.ts index 5c950b4a2fa..a3036694426 100644 --- a/apps/sim/app/api/v2/chat/route.ts +++ b/apps/sim/app/api/v2/chat/route.ts @@ -389,7 +389,7 @@ export const POST = withRouteHandler( workspaceId, chatId, simRequestId: requestId, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', autoExecuteTools: true, interactive: false, abortSignal: lifecycleAbortController.signal, diff --git a/apps/sim/app/api/workflows/[id]/execute/route.ts b/apps/sim/app/api/workflows/[id]/execute/route.ts index 23e541c29df..31ca931b575 100644 --- a/apps/sim/app/api/workflows/[id]/execute/route.ts +++ b/apps/sim/app/api/workflows/[id]/execute/route.ts @@ -98,7 +98,7 @@ import { settleClientWorkflowToolExecution, } from '@/lib/mothership/async-runs/repository' import { COPILOT_WORKFLOW_EXECUTION_CONFLICT_CODE } from '@/lib/mothership/constants' -import { CopilotDegradedReason } from '@/lib/mothership/generated/trace-attribute-values-v1' +import { CopilotDegradedReason } from '@/lib/mothership/observability/trace-attribute-values' import { recordDegraded } from '@/lib/mothership/request/metrics' import { reportQueuedClientWorkflowTool, diff --git a/apps/sim/app/workspace/[workspaceId]/home/components/message-content/components/agent-group/tool-call-item.tsx b/apps/sim/app/workspace/[workspaceId]/home/components/message-content/components/agent-group/tool-call-item.tsx index 9278ef7ff1b..4f50d34aca3 100644 --- a/apps/sim/app/workspace/[workspaceId]/home/components/message-content/components/agent-group/tool-call-item.tsx +++ b/apps/sim/app/workspace/[workspaceId]/home/components/message-content/components/agent-group/tool-call-item.tsx @@ -101,7 +101,7 @@ export function ToolCallItem({ // Like read's VFS-target resolution above, the gateway uses its exact // discovered toolId only as a deterministic registry lookup. This renders - // the real integration brand while Go validates/resolves the operation. + // the real integration brand while the worker validates/resolves the operation. const gatewayBlock = useMemo(() => { if (toolName !== CallIntegrationTool.id) return undefined const toolId = params?.toolId ?? extractStreamingStringArgument(streamingArgs, 'toolId') diff --git a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/handle-span-event.ts b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/handle-span-event.ts index 5e48afd5f69..f8d2a0645a0 100644 --- a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/handle-span-event.ts +++ b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/handle-span-event.ts @@ -8,10 +8,7 @@ import type { StreamEventScope, StreamLoopContext, } from '@/app/workspace/[workspaceId]/home/hooks/stream/stream-context' -import { - asPayloadRecord, - FILE_SUBAGENT_ID, -} from '@/app/workspace/[workspaceId]/home/hooks/stream/stream-helpers' +import { FILE_SUBAGENT_ID } from '@/app/workspace/[workspaceId]/home/hooks/stream/stream-helpers' type SpanEvent = Extract @@ -29,20 +26,11 @@ export function handleSpanEvent( scope: StreamEventScope ): void { const { state, ops, deps } = ctx - const { scopedParentToolCallId, scopedAgentId, scopedSpanId } = scope + const { scopedParentToolCallId: parentToolCallId, scopedAgentId, scopedSpanId } = scope const payload = parsed.payload if (payload.kind !== MothershipStreamV1SpanPayloadKind.subagent) { return } - const spanData = asPayloadRecord(payload.data) - const parentToolCallIdFromData = - typeof spanData?.tool_call_id === 'string' - ? spanData.tool_call_id - : typeof spanData?.toolCallId === 'string' - ? spanData.toolCallId - : undefined - const parentToolCallId = scopedParentToolCallId ?? parentToolCallIdFromData - const isPendingPause = spanData?.pending === true const name = typeof payload.agent === 'string' ? payload.agent : scopedAgentId const runId = scopedSpanId ?? parentToolCallId @@ -79,9 +67,6 @@ export function handleSpanEvent( } if (payload.event === MothershipStreamV1SpanLifecycleEvent.end) { - if (isPendingPause) { - return - } if ( deps.previewSessionRef.current && (!deps.activePreviewSessionIdRef.current || diff --git a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/stream-helpers.ts b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/stream-helpers.ts index 6d1911b6453..30a527edba7 100644 --- a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/stream-helpers.ts +++ b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/stream-helpers.ts @@ -59,12 +59,6 @@ export const WORKFLOW_MUTATION_TOOL_NAMES: Set = new Set([ 'rename_workflow', ]) -export type StreamPayload = Record - -export function asPayloadRecord(value: unknown): StreamPayload | undefined { - return isRecordLike(value) ? value : undefined -} - /** * Settles every unfinished tool row (running, pending, or awaiting approval) at * a turn terminal by propagating the turn's outcome: a clean `complete` settles @@ -176,7 +170,7 @@ function functionExecuteTitle(title: string | undefined): string { * streaming VFS targets. The trusted integration branding is the ICON, which * the row component derives deterministically from the streamed `toolId` (or * the rebound operation name) via Sim's block registry — the text carries no - * integration name. After Go's authoritative frame rebinds the row to the + * integration name. After the worker's authoritative frame rebinds the row to the * exact operation (e.g. `gmail_read_v2`), the preserved * `integrationDescription` keeps the same text. Returns undefined until a * description is readable (callers fall back to the neutral gateway label). diff --git a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model-serialize.test.ts b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model-serialize.test.ts index db8669f8e1c..ad94cba996d 100644 --- a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model-serialize.test.ts +++ b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model-serialize.test.ts @@ -183,15 +183,7 @@ describe('modelToContentBlocks', () => { expect(mainText[0].content).toBe('Spawning research.') // The real subagent_start lands afterward and no-ops: still one research lane. - reduceEvent( - m, - env( - 4, - 'span', - { kind: 'subagent', event: 'start', agent: 'research', data: { tool_call_id: 'tc-r1' } }, - sub - ) - ) + reduceEvent(m, env(4, 'span', { kind: 'subagent', event: 'start', agent: 'research' }, sub)) const after = modelToContentBlocks(m) expect(after.filter((b) => b.type === 'subagent')).toHaveLength(1) expect(after.find((b) => b.type === 'subagent')?.content).toBe('research') @@ -208,12 +200,7 @@ describe('modelToContentBlocks', () => { build([ env(1, 'text', { channel: 'assistant', text: 'before' }), env(2, 'tool', { phase: 'call', toolCallId: 'tc-file', toolName: 'file' }), - env( - 3, - 'span', - { kind: 'subagent', event: 'start', agent: 'file', data: { tool_call_id: 'tc-file' } }, - sub - ), + env(3, 'span', { kind: 'subagent', event: 'start', agent: 'file' }, sub), env( 4, 'tool', @@ -247,12 +234,7 @@ describe('modelToContentBlocks', () => { } const m = build([ env(1, 'tool', { phase: 'call', toolCallId: 'tc-file', toolName: 'file' }), - env( - 2, - 'span', - { kind: 'subagent', event: 'start', agent: 'file', data: { tool_call_id: 'tc-file' } }, - sub - ), + env(2, 'span', { kind: 'subagent', event: 'start', agent: 'file' }, sub), env( 3, 'tool', diff --git a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model-serialize.ts b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model-serialize.ts index 0f5ac910ad1..a89841ce5f8 100644 --- a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model-serialize.ts +++ b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model-serialize.ts @@ -349,10 +349,7 @@ export function contentBlocksToModel(blocks: ContentBlock[]): TurnModel { kind: 'subagent', event: 'start', agent: block.content, - data: { - ...(block.parentToolCallId ? { tool_call_id: block.parentToolCallId } : {}), - ...(block.subagentName ? { name: block.subagentName } : {}), - }, + data: block.subagentName ? { name: block.subagentName } : {}, }, scopeFor(block), block.timestamp diff --git a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model.test.ts b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model.test.ts index eb8bdc88674..2b753b3b927 100644 --- a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model.test.ts +++ b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model.test.ts @@ -62,7 +62,6 @@ function spanStart( kind: 'subagent', event: 'start', agent, - data: parentToolCallId ? { tool_call_id: parentToolCallId } : {}, }, { lane: 'subagent', @@ -74,12 +73,7 @@ function spanStart( ) } -function spanEnd( - seq: number, - spanId: string, - agent: string, - opts?: { error?: string; pending?: boolean } -) { +function spanEnd(seq: number, spanId: string, agent: string, opts?: { error?: string }) { return envelope( seq, 'span', @@ -87,10 +81,7 @@ function spanEnd( kind: 'subagent', event: 'end', agent, - data: { - ...(opts?.error ? { error: opts.error } : {}), - ...(opts?.pending ? { pending: true } : {}), - }, + data: opts?.error ? { error: opts.error } : {}, }, { lane: 'subagent', spanId, agentId: agent } ) @@ -300,7 +291,7 @@ describe('reduceEvent — span-start owner reconciliation', () => { envelope( 2, 'span', - { kind: 'subagent', event: 'start', agent: 'workflow', data: { tool_call_id: 'd1' } }, + { kind: 'subagent', event: 'start', agent: 'workflow' }, { lane: 'subagent', spanId: 'S1', parentToolCallId: 'd1' } ) ) @@ -316,12 +307,7 @@ describe('reduceEvent — span end settles stale lane tools', () => { it('marks still-running tools success when their lane ends cleanly', () => { const model = apply([ - envelope( - 1, - 'span', - { kind: 'subagent', event: 'start', agent: 'browser', data: { tool_call_id: 'd1' } }, - laneScope - ), + envelope(1, 'span', { kind: 'subagent', event: 'start', agent: 'browser' }, laneScope), toolCall(2, 'click-1', 'browser_click', laneScope), // No result for click-1 — dropped/reordered past the lane end. envelope( @@ -344,12 +330,7 @@ describe('reduceEvent — span end settles stale lane tools', () => { it('leaves settled tools alone and lets a late result overwrite the settle', () => { const model = apply([ - envelope( - 1, - 'span', - { kind: 'subagent', event: 'start', agent: 'browser', data: { tool_call_id: 'd1' } }, - laneScope - ), + envelope(1, 'span', { kind: 'subagent', event: 'start', agent: 'browser' }, laneScope), toolCall(2, 'click-1', 'browser_click', laneScope), envelope( 3, diff --git a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model.ts b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model.ts index 48a0edc56bd..bc1a72bc588 100644 --- a/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model.ts +++ b/apps/sim/app/workspace/[workspaceId]/home/hooks/stream/turn-model.ts @@ -12,7 +12,10 @@ import { } from '@/lib/mothership/generated/mothership-stream-v1' import { ToolActivity } from '@/lib/mothership/generated/protocol' import { CallIntegrationTool } from '@/lib/mothership/generated/tool-catalog-v1' -import type { PersistedStreamEventEnvelope } from '@/lib/mothership/request/session/contract' +import { + type PersistedStreamEventEnvelope, + subagentSpanData, +} from '@/lib/mothership/request/session/contract' import type { TaskBlockInfo } from '@/lib/mothership/request/types' import { readFrameActivity } from '@/lib/mothership/tools/activity' import { @@ -24,13 +27,13 @@ import { extractStreamingStringArgument } from '@/lib/mothership/tools/streaming /** * The single deterministic model of one assistant turn, derived purely from the - * Go wire stream. Every tool and subagent is a {@link LifecycleNode} with one + * worker wire stream. Every tool and subagent is a {@link LifecycleNode} with one * explicit terminal source, so rendering reads node status instead of inferring * it from transport, preview sessions, or a turn-complete sweep. Parallel * subagents are independent span lanes; nested subagents nest by `parentSpanId`. */ -/** The root span lane id Go stamps on main-agent (non-subagent) events. */ +/** The root span lane id the worker stamps on main-agent (non-subagent) events. */ export const MAIN_SPAN = 'main' /** @@ -227,7 +230,7 @@ function asString(value: unknown): string | undefined { /** * The integration gateway intentionally emits a second authoritative call frame - * under the SAME provider call id once Go resolves the exact server-owned + * under the SAME provider call id once the worker resolves the exact server-owned * operation (call_integration_tool -> e.g. gmail_read_v2). Rebind the node to * that operation so the row brands from the real integration (name -> block * registry) and keep only the model-authored `description` for presentation — @@ -427,7 +430,7 @@ function applyToolResult( * parallel bursts (many subagents streaming into one ordered channel); without * the owning `AgentNode` the serializer can't attribute the content, so it leaks * into the main lane and the subagent's thinking is dropped until the start - * lands. The wire scope already carries the lane identity (Go tags every + * lands. The wire scope already carries the lane identity (the worker tags every * forwarded subagent event with its agent id/span), so the lane is rebuilt * deterministically from the content event itself — the symmetric counterpart to * buffering a result before its call. The later `subagent_start` finds this node @@ -591,11 +594,10 @@ export function reduceEvent(model: TurnModel, envelope: PersistedStreamEventEnve case MothershipStreamV1EventType.span: { const payload = envelope.payload if (payload.kind !== MothershipStreamV1SpanPayloadKind.subagent) break - const data = isRecordLike(payload.data) ? payload.data : undefined - const triggerToolCallId = - scope?.parentToolCallId ?? asString(data?.tool_call_id) ?? asString(data?.toolCallId) + const data = subagentSpanData(payload.data) + const triggerToolCallId = scope?.parentToolCallId const agentId = asString(payload.agent) ?? scope?.agentId ?? '' - const displayName = asString(data?.name) + const displayName = data.name const resolvedSpanId = scope?.spanId ?? (triggerToolCallId ? `span:${triggerToolCallId}` : `span:${seq}`) const parentSpanId = scope?.parentSpanId ?? MAIN_SPAN @@ -642,11 +644,9 @@ export function reduceEvent(model: TurnModel, envelope: PersistedStreamEventEnve model.order.push(node.id) model.agentBySpanId.set(resolvedSpanId, node.id) } else if (payload.event === MothershipStreamV1SpanLifecycleEvent.end) { - // A pending pause is not a terminal — the run resumes later. - if (data?.pending === true) break breakLane(model, resolvedSpanId, tsMs) const node = model.nodes.get(resolvedSpanId) - const error = data && asString(data.error) + const error = data.error const spanErrored = Boolean(error) if (node && node.kind === 'agent' && !isNodeTerminal(node.status)) { node.status = spanErrored ? 'error' : 'success' diff --git a/apps/sim/instrumentation-node.ts b/apps/sim/instrumentation-node.ts index 276871980dd..fe23ba06acb 100644 --- a/apps/sim/instrumentation-node.ts +++ b/apps/sim/instrumentation-node.ts @@ -1,6 +1,6 @@ -// Sim OTel bootstrap. Filter by `mothership.origin` or span-name -// prefix (`sim-mothership:` / `go-mothership:`) to separate the two -// halves of a mothership trace in the OTLP backend. +// Sim OTel bootstrap. Sim's half of a mothership trace carries +// `mothership.origin=sim-mothership` and the `sim-mothership:` span-name +// prefix; the worker's half is identified by service.name `mothership-worker`. import { hostname } from 'node:os' import type { Attributes, Context, Link, SpanKind } from '@opentelemetry/api' @@ -13,7 +13,8 @@ import type { SpanProcessor, } from '@opentelemetry/sdk-trace' import { createLogger } from '@sim/logger' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { env } from './lib/core/config/env' import { ANONYMOUS_TELEMETRY_ENDPOINT, @@ -48,7 +49,7 @@ const DEFAULT_TELEMETRY_CONFIG = { // Non-mothership code (workflow executor, block runtime, framework // noise) is dropped. Broaden carefully — `http.` etc. would reopen // the firehose. -const ALLOWED_SPAN_PREFIXES = ['gen_ai.', 'copilot.', 'sim →', 'sim.', 'tool.execute'] +const ALLOWED_SPAN_PREFIXES = ['gen_ai.', 'copilot.', 'sim →', 'sim.', TraceSpan.ToolExecute] function isBusinessSpan(spanName: string): boolean { return ALLOWED_SPAN_PREFIXES.some((prefix) => spanName.startsWith(prefix)) @@ -90,18 +91,18 @@ function normalizeOtlpLogsUrl(url: string): string { } } -// deployment.environment in the GO value space (dev | staging | prod) without +// deployment.environment in the worker's value space (dev | staging | prod) without // any new infra env var. Every deployed Sim tier already gets // APPCONFIG_ENVIRONMENT = the infra env name (dev | staging | production), so we -// reuse it and map production -> prod to match Go's `OTEL_DEPLOYMENT_ENVIRONMENT` -// (and thus a single Grafana $env filter spans Sim + Go). Returns undefined when +// reuse it and map production -> prod to match the worker's `OTEL_DEPLOYMENT_ENVIRONMENT` +// (and thus a single Grafana $env filter spans Sim + worker). Returns undefined when // unset (local dev) so the OTEL_/NODE_ENV fallbacks still apply. function deploymentEnvFromAppConfig(v: string | undefined): string | undefined { if (!v) return undefined return v === 'production' ? 'prod' : v } -// Sampling ratio from env (mirrors Go's `samplerFromEnv`); fallback +// Sampling ratio from env; fallback // is 100% everywhere. Retention caps cost, not sampling. function resolveSamplingRatio(_isLocalEndpoint: boolean): number { const raw = process.env.TELEMETRY_SAMPLING_RATIO || process.env.OTEL_TRACES_SAMPLER_ARG || '' @@ -300,7 +301,7 @@ async function initializeOpenTelemetry() { // Must be unique per process: replicas sharing one instance id collapse // into a single Prometheus series, so their independent cumulative // counters interleave and corrupt rate()/increase(). The slug keeps Sim - // distinct from Go for Jaeger's clock-skew grouping; the hostname (the + // distinct from the worker for Jaeger's clock-skew grouping; the hostname (the // container id under ECS) makes each replica its own series. const serviceInstanceId = `${telemetryConfig.serviceName}-${SERVICE_INSTANCE_SLUG}-${hostname()}` const resource = defaultResource().merge( @@ -308,8 +309,7 @@ async function initializeOpenTelemetry() { [ATTR_SERVICE_NAME]: telemetryConfig.serviceName, [ATTR_SERVICE_VERSION]: telemetryConfig.serviceVersion, // OTEL_ → DEPLOYMENT_ENVIRONMENT → APPCONFIG_ENVIRONMENT (mapped to the - // Go value space) → NODE_ENV. Matches Go's `resourceEnvFromEnv()` so a - // single $env spans Sim + Go. APPCONFIG_ENVIRONMENT (already set on every + // worker's value space) → NODE_ENV, so a single $env spans Sim + worker. APPCONFIG_ENVIRONMENT (already set on every // deployed tier) is the fix that stops deployed Sim tagging everything // "production" via the NODE_ENV fallback — no new infra env var needed. [ATTR_DEPLOYMENT_ENVIRONMENT]: @@ -318,12 +318,12 @@ async function initializeOpenTelemetry() { deploymentEnvFromAppConfig(process.env.APPCONFIG_ENVIRONMENT) || env.NODE_ENV || 'development', - 'service.namespace': 'mothership', - 'service.instance.id': serviceInstanceId, - 'mothership.origin': MOTHERSHIP_ORIGIN, - 'telemetry.sdk.name': 'opentelemetry', - 'telemetry.sdk.language': 'nodejs', - 'telemetry.sdk.version': '1.0.0', + [TraceAttr.ServiceNamespace]: 'mothership', + [TraceAttr.ServiceInstanceId]: serviceInstanceId, + [TraceAttr.MothershipOrigin]: MOTHERSHIP_ORIGIN, + [TraceAttr.TelemetrySdkName]: 'opentelemetry', + [TraceAttr.TelemetrySdkLanguage]: 'nodejs', + [TraceAttr.TelemetrySdkVersion]: '1.0.0', }) ) diff --git a/apps/sim/lib/api/contracts/copilot.ts b/apps/sim/lib/api/contracts/copilot.ts index 4b8446c034c..c5f41ac7fef 100644 --- a/apps/sim/lib/api/contracts/copilot.ts +++ b/apps/sim/lib/api/contracts/copilot.ts @@ -158,7 +158,7 @@ export type ValidateCopilotApiKeyError = z.output /** - * Server-to-server entitlement gate called by the mothership (Go) before it + * Server-to-server entitlement gate called by the mothership worker before it * uses a workspace's own provider key. Empty 200/401/403 responses signal the - * outcome; the Go caller fails closed to hosted keys on anything but a 200. + * outcome; the caller fails closed to hosted keys on anything but a 200. */ export const validateCopilotByokContract = defineRouteContract({ method: 'POST', @@ -400,6 +400,6 @@ export const copilotChatSteerContract = defineRouteContract({ body: copilotChatSteerBodySchema, response: { mode: 'json', - schema: z.object({ ok: z.boolean(), queued: z.boolean(), goStatus: z.number().optional() }), + schema: z.object({ ok: z.boolean(), queued: z.boolean(), workerStatus: z.number().optional() }), }, }) diff --git a/apps/sim/lib/assistant-actions/tool-permissions.integration.ts b/apps/sim/lib/assistant-actions/tool-permissions.integration.ts index 6d1871a0649..f1f342bad01 100644 --- a/apps/sim/lib/assistant-actions/tool-permissions.integration.ts +++ b/apps/sim/lib/assistant-actions/tool-permissions.integration.ts @@ -79,7 +79,6 @@ import { } from '@/lib/mothership/async-runs/repository' import { buildIntegrationToolSchemas } from '@/lib/mothership/chat/payload' import { createStreamingContext } from '@/lib/mothership/request/context/request-context' -import { runStreamLoop } from '@/lib/mothership/request/go/stream' import { createEvent } from '@/lib/mothership/request/session/event' import { executeToolAndReport } from '@/lib/mothership/request/tools/executor' import type { @@ -88,6 +87,7 @@ import type { StreamEvent, ToolCallState, } from '@/lib/mothership/request/types' +import { runStreamLoop } from '@/lib/mothership/request/worker/stream' import { defaultLiveSearchPolicy } from '@/lib/sim-search/live/policy-schema' import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry' import { executeTool } from '@/tools' diff --git a/apps/sim/lib/benchmarks/worker.ts b/apps/sim/lib/benchmarks/worker.ts index 86a86e18a94..293cedfbb0f 100644 --- a/apps/sim/lib/benchmarks/worker.ts +++ b/apps/sim/lib/benchmarks/worker.ts @@ -119,7 +119,7 @@ export async function executeBenchmarkJson(input: { { ...payload }, { benchmark: input.profile?.stage ?? 'tool-free', - goRoute: '/api/mothership/execute', + workerRoute: '/api/mothership/execute', userId: executionUserId, ...scope, chatId: payload.chatId, @@ -187,7 +187,7 @@ export async function executeBenchmarkPlan(input: { try { result = await runHeadlessCopilotLifecycle(payload, { benchmark: 'plan', - goRoute: '/api/mothership', + workerRoute: '/api/mothership', userId: executionUserId, organizationId: input.benchmark.organizationId, chatId: target.chatId, diff --git a/apps/sim/lib/billing/core/attribution.ts b/apps/sim/lib/billing/core/attribution.ts index 67681fccd49..e25dbcbb0f8 100644 --- a/apps/sim/lib/billing/core/attribution.ts +++ b/apps/sim/lib/billing/core/attribution.ts @@ -475,7 +475,7 @@ function parseBillingAttributionHeader( return attribution } -/** Settles exactly the admitted scope, including organization charges replayed from Go's ledger. */ +/** Settles exactly the admitted scope, including organization charges replayed from the worker's ledger. */ export function requireBillingCallbackAttribution( headers: Pick, expected: { actorUserId: string; workspaceId?: string; organizationId?: string } @@ -592,7 +592,7 @@ function assertAccountBillingDecision(value: unknown): AccountBillingDecision { /** * Encodes the immutable direct-account payer selected by trusted Sim - * admission. Go stores and returns this value opaquely. + * admission. The worker stores and returns this value opaquely. */ export function serializeAccountBillingDecisionHeader(decision: AccountBillingDecision): string { return encodeURIComponent(JSON.stringify(assertAccountBillingDecision(decision))) @@ -870,7 +870,7 @@ export function assertBillingAttributionOwner( * requests when the workspace is absent from this Sim deployment. * * Unlike modern attributed-v1/direct-v1 envelopes, this decision is mutable: - * historical markerless Go allocated its callback billing ID after admission + * the retired markerless Go copilot allocated its callback billing ID after admission * and returned no payer material, so admission and callback independently * resolve current state. Hosted traffic may use this only for explicit * legacy-v0 replay; markerless use is confined to local self-hosting. diff --git a/apps/sim/lib/core/config/env-flags.ts b/apps/sim/lib/core/config/env-flags.ts index 6c612890e94..eec1554e5fe 100644 --- a/apps/sim/lib/core/config/env-flags.ts +++ b/apps/sim/lib/core/config/env-flags.ts @@ -105,7 +105,7 @@ export const isStatusNoticePreviewEnabled = isTruthy(getEnv('NEXT_PUBLIC_STATUS_ * Off by default: turning it on makes the copilot prompt on its most frequently * used tools, so it is an opt-in change in how the product feels, not just a * safety toggle. With it off nothing is stamped, gated, or persisted, and an - * approval stamp arriving from Go is cleared on the way to the client. + * approval stamp arriving from the worker is cleared on the way to the client. * * The gate is a property of the lane, not only of the tool. Sim can hold a call * for a decision on the dispatch lane, where a streaming context and a decision diff --git a/apps/sim/lib/core/telemetry.ts b/apps/sim/lib/core/telemetry.ts index 28560c7b665..8164a8f8102 100644 --- a/apps/sim/lib/core/telemetry.ts +++ b/apps/sim/lib/core/telemetry.ts @@ -20,40 +20,40 @@ import { context, type Span, SpanStatusCode, trace } from '@opentelemetry/api' import { createLogger } from '@sim/logger' import type { TraceSpan } from '@/lib/logs/types' import { hostedKeyMetrics } from '@/lib/monitoring/metrics' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' /** * GenAI Semantic Convention Attributes */ const GenAIAttributes = { SYSTEM: 'gen_ai.system', - REQUEST_MODEL: 'gen_ai.request.model', + REQUEST_MODEL: TraceAttr.GenAiRequestModel, RESPONSE_MODEL: 'gen_ai.response.model', - USAGE_INPUT_TOKENS: 'gen_ai.usage.input_tokens', - USAGE_OUTPUT_TOKENS: 'gen_ai.usage.output_tokens', - USAGE_TOTAL_TOKENS: 'gen_ai.usage.total_tokens', + USAGE_INPUT_TOKENS: TraceAttr.GenAiUsageInputTokens, + USAGE_OUTPUT_TOKENS: TraceAttr.GenAiUsageOutputTokens, + USAGE_TOTAL_TOKENS: TraceAttr.GenAiUsageTotalTokens, REQUEST_TEMPERATURE: 'gen_ai.request.temperature', REQUEST_TOP_P: 'gen_ai.request.top_p', REQUEST_MAX_TOKENS: 'gen_ai.request.max_tokens', RESPONSE_FINISH_REASON: 'gen_ai.response.finish_reason', - AGENT_ID: 'gen_ai.agent.id', - AGENT_NAME: 'gen_ai.agent.name', + AGENT_ID: TraceAttr.GenAiAgentId, + AGENT_NAME: TraceAttr.GenAiAgentName, AGENT_TASK: 'gen_ai.agent.task', - WORKFLOW_ID: 'gen_ai.workflow.id', - WORKFLOW_NAME: 'gen_ai.workflow.name', + WORKFLOW_ID: TraceAttr.GenAiWorkflowId, + WORKFLOW_NAME: TraceAttr.GenAiWorkflowName, WORKFLOW_VERSION: 'gen_ai.workflow.version', WORKFLOW_EXECUTION_ID: 'gen_ai.workflow.execution_id', - TOOL_NAME: 'gen_ai.tool.name', + TOOL_NAME: TraceAttr.GenAiToolName, TOOL_DESCRIPTION: 'gen_ai.tool.description', - COST_TOTAL: 'gen_ai.cost.total', - COST_INPUT: 'gen_ai.cost.input', - COST_OUTPUT: 'gen_ai.cost.output', + COST_TOTAL: TraceAttr.GenAiCostTotal, + COST_INPUT: TraceAttr.GenAiCostInput, + COST_OUTPUT: TraceAttr.GenAiCostOutput, } const logger = createLogger('OTelIntegration') @@ -127,17 +127,17 @@ const BLOCK_TYPE_MAPPING: Record< spanKind: 'gen_ai.tool', getAttributes: (span) => ({ [GenAIAttributes.TOOL_NAME]: span.name, - 'tool.id': span.id, - 'tool.duration_ms': span.duration, + [TraceAttr.ToolId]: span.id, + [TraceAttr.ToolDurationMs]: span.duration, }), }, model: { spanName: 'gen_ai.model.request', spanKind: 'gen_ai.model', getAttributes: (span) => ({ - 'model.name': span.name, - 'model.id': span.id, - 'model.duration_ms': span.duration, + [TraceAttr.ModelName]: span.name, + [TraceAttr.ModelId]: span.id, + [TraceAttr.ModelDurationMs]: span.duration, }), }, api: { @@ -150,8 +150,8 @@ const BLOCK_TYPE_MAPPING: Record< 'http.request.method': input?.method || 'GET', 'http.request.url': input?.url || '', 'http.response.status_code': output?.status || 0, - 'block.id': span.blockId, - 'block.name': span.name, + [TraceAttr.BlockId]: span.blockId, + [TraceAttr.BlockName]: span.name, } }, }, @@ -159,9 +159,9 @@ const BLOCK_TYPE_MAPPING: Record< spanName: 'function.execute', spanKind: 'function', getAttributes: (span) => ({ - 'function.name': span.name, - 'function.id': span.blockId, - 'function.execution_time_ms': span.duration, + [TraceAttr.FunctionName]: span.name, + [TraceAttr.FunctionId]: span.blockId, + [TraceAttr.FunctionExecutionTimeMs]: span.duration, }), }, router: { @@ -170,9 +170,9 @@ const BLOCK_TYPE_MAPPING: Record< getAttributes: (span) => { const output = span.output as { selectedPath?: { blockId?: string } } | undefined return { - 'router.name': span.name, - 'router.id': span.blockId, - 'router.selected_path': output?.selectedPath?.blockId, + [TraceAttr.RouterName]: span.name, + [TraceAttr.RouterId]: span.blockId, + [TraceAttr.RouterSelectedPath]: output?.selectedPath?.blockId, } }, }, @@ -182,9 +182,9 @@ const BLOCK_TYPE_MAPPING: Record< getAttributes: (span) => { const output = span.output as { conditionResult?: boolean | string } | undefined return { - 'condition.name': span.name, - 'condition.id': span.blockId, - 'condition.result': output?.conditionResult, + [TraceAttr.ConditionName]: span.name, + [TraceAttr.ConditionId]: span.blockId, + [TraceAttr.ConditionResult]: output?.conditionResult, } }, }, @@ -192,18 +192,18 @@ const BLOCK_TYPE_MAPPING: Record< spanName: 'loop.execute', spanKind: 'loop', getAttributes: (span) => ({ - 'loop.name': span.name, - 'loop.id': span.blockId, - 'loop.iterations': span.children?.length || 0, + [TraceAttr.LoopName]: span.name, + [TraceAttr.LoopId]: span.blockId, + [TraceAttr.LoopIterations]: span.children?.length || 0, }), }, parallel: { spanName: 'parallel.execute', spanKind: 'parallel', getAttributes: (span) => ({ - 'parallel.name': span.name, - 'parallel.id': span.blockId, - 'parallel.branches': span.children?.length || 0, + [TraceAttr.ParallelName]: span.name, + [TraceAttr.ParallelId]: span.blockId, + [TraceAttr.ParallelBranches]: span.children?.length || 0, }), }, } @@ -220,17 +220,17 @@ function createOTelSpanFromTraceSpan(traceSpan: TraceSpan, parentSpan?: Span): S spanName: `block.${traceSpan.type}`, spanKind: 'internal', getAttributes: (span: TraceSpan) => ({ - 'block.type': span.type, - 'block.id': span.blockId, - 'block.name': span.name, + [TraceAttr.BlockType]: span.type, + [TraceAttr.BlockId]: span.blockId, + [TraceAttr.BlockName]: span.name, }), } const attributes = { ...blockMapping.getAttributes(traceSpan), - 'span.type': traceSpan.type, - 'span.duration_ms': traceSpan.duration, - 'span.status': traceSpan.status, + [TraceAttr.SpanType]: traceSpan.type, + [TraceAttr.SpanDurationMs]: traceSpan.duration, + [TraceAttr.SpanStatus]: traceSpan.status, } const ctx = parentSpan ? trace.setSpan(context.active(), parentSpan) : context.active() @@ -414,9 +414,9 @@ export const PlatformEvents = { provider?: string }) => { trackPlatformEvent('platform.user.signed_up', { - 'user.id': attrs.userId, - 'user.auth_method': attrs.authMethod, - ...(attrs.provider && { 'user.auth_provider': attrs.provider }), + [TraceAttr.UserId]: attrs.userId, + [TraceAttr.UserAuthMethod]: attrs.authMethod, + ...(attrs.provider && { [TraceAttr.UserAuthProvider]: attrs.provider }), }) }, @@ -429,9 +429,9 @@ export const PlatformEvents = { provider?: string }) => { trackPlatformEvent('platform.user.signed_in', { - 'user.id': attrs.userId, - 'user.auth_method': attrs.authMethod, - ...(attrs.provider && { 'user.auth_provider': attrs.provider }), + [TraceAttr.UserId]: attrs.userId, + [TraceAttr.UserAuthMethod]: attrs.authMethod, + ...(attrs.provider && { [TraceAttr.UserAuthProvider]: attrs.provider }), }) }, @@ -440,7 +440,7 @@ export const PlatformEvents = { */ passwordResetRequested: (attrs: { userId: string }) => { trackPlatformEvent('platform.user.password_reset_requested', { - 'user.id': attrs.userId, + [TraceAttr.UserId]: attrs.userId, }) }, @@ -449,9 +449,9 @@ export const PlatformEvents = { */ workspaceCreated: (attrs: { workspaceId: string; userId: string; name: string }) => { trackPlatformEvent('platform.workspace.created', { - 'workspace.id': attrs.workspaceId, - 'workspace.name': attrs.name, - 'user.id': attrs.userId, + [TraceAttr.WorkspaceId]: attrs.workspaceId, + [TraceAttr.WorkspaceName]: attrs.name, + [TraceAttr.UserId]: attrs.userId, }) }, @@ -466,9 +466,9 @@ export const PlatformEvents = { membershipIntent?: string }) => { trackPlatformEvent('platform.workspace.member_invited', { - 'workspace.id': attrs.workspaceId, - 'user.id': attrs.invitedBy, - 'invitation.role': attrs.role, + [TraceAttr.WorkspaceId]: attrs.workspaceId, + [TraceAttr.UserId]: attrs.invitedBy, + [TraceAttr.InvitationRole]: attrs.role, ...(attrs.membershipIntent ? { 'invitation.membership_intent': attrs.membershipIntent } : {}), }) }, @@ -484,10 +484,10 @@ export const PlatformEvents = { role: string }) => { trackPlatformEvent('platform.workspace.member_added', { - 'workspace.id': attrs.workspaceId, - 'user.id': attrs.addedBy, + [TraceAttr.WorkspaceId]: attrs.workspaceId, + [TraceAttr.UserId]: attrs.addedBy, 'member.id': attrs.addedUserId, - 'member.role': attrs.role, + [TraceAttr.MemberRole]: attrs.role, }) }, @@ -496,9 +496,9 @@ export const PlatformEvents = { */ workspaceMemberJoined: (attrs: { workspaceId: string; userId: string; role: string }) => { trackPlatformEvent('platform.workspace.member_joined', { - 'workspace.id': attrs.workspaceId, - 'user.id': attrs.userId, - 'member.role': attrs.role, + [TraceAttr.WorkspaceId]: attrs.workspaceId, + [TraceAttr.UserId]: attrs.userId, + [TraceAttr.MemberRole]: attrs.role, }) }, @@ -512,10 +512,10 @@ export const PlatformEvents = { folderId?: string }) => { trackPlatformEvent('platform.workflow.created', { - 'workflow.id': attrs.workflowId, - 'workflow.name': attrs.name, - 'workflow.has_workspace': !!attrs.workspaceId, - 'workflow.has_folder': !!attrs.folderId, + [TraceAttr.WorkflowId]: attrs.workflowId, + [TraceAttr.WorkflowName]: attrs.name, + [TraceAttr.WorkflowHasWorkspace]: !!attrs.workspaceId, + [TraceAttr.WorkflowHasFolder]: !!attrs.folderId, }) }, @@ -524,8 +524,8 @@ export const PlatformEvents = { */ workflowDeleted: (attrs: { workflowId: string; workspaceId?: string }) => { trackPlatformEvent('platform.workflow.deleted', { - 'workflow.id': attrs.workflowId, - ...(attrs.workspaceId && { 'workspace.id': attrs.workspaceId }), + [TraceAttr.WorkflowId]: attrs.workflowId, + ...(attrs.workspaceId && { [TraceAttr.WorkspaceId]: attrs.workspaceId }), }) }, @@ -538,9 +538,9 @@ export const PlatformEvents = { workspaceId?: string }) => { trackPlatformEvent('platform.workflow.duplicated', { - 'workflow.source_id': attrs.sourceWorkflowId, - 'workflow.new_id': attrs.newWorkflowId, - ...(attrs.workspaceId && { 'workspace.id': attrs.workspaceId }), + [TraceAttr.WorkflowSourceId]: attrs.sourceWorkflowId, + [TraceAttr.WorkflowNewId]: attrs.newWorkflowId, + ...(attrs.workspaceId && { [TraceAttr.WorkspaceId]: attrs.workspaceId }), }) }, @@ -558,16 +558,16 @@ export const PlatformEvents = { blockTypes?: string }) => { trackPlatformEvent('platform.workflow.deployed', { - 'workflow.id': attrs.workflowId, - 'workflow.name': attrs.workflowName, - 'workflow.blocks_count': attrs.blocksCount, - 'workflow.edges_count': attrs.edgesCount, - 'deployment.version': attrs.version, - ...(attrs.loopsCount !== undefined && { 'workflow.loops_count': attrs.loopsCount }), + [TraceAttr.WorkflowId]: attrs.workflowId, + [TraceAttr.WorkflowName]: attrs.workflowName, + [TraceAttr.WorkflowBlocksCount]: attrs.blocksCount, + [TraceAttr.WorkflowEdgesCount]: attrs.edgesCount, + [TraceAttr.DeploymentVersion]: attrs.version, + ...(attrs.loopsCount !== undefined && { [TraceAttr.WorkflowLoopsCount]: attrs.loopsCount }), ...(attrs.parallelsCount !== undefined && { - 'workflow.parallels_count': attrs.parallelsCount, + [TraceAttr.WorkflowParallelsCount]: attrs.parallelsCount, }), - ...(attrs.blockTypes && { 'workflow.block_types': attrs.blockTypes }), + ...(attrs.blockTypes && { [TraceAttr.WorkflowBlockTypes]: attrs.blockTypes }), }) }, @@ -576,7 +576,7 @@ export const PlatformEvents = { */ workflowUndeployed: (attrs: { workflowId: string }) => { trackPlatformEvent('platform.workflow.undeployed', { - 'workflow.id': attrs.workflowId, + [TraceAttr.WorkflowId]: attrs.workflowId, }) }, @@ -593,13 +593,13 @@ export const PlatformEvents = { totalCost?: number }) => { trackPlatformEvent('platform.workflow.executed', { - 'workflow.id': attrs.workflowId, - 'execution.duration_ms': attrs.durationMs, - 'execution.status': attrs.status, - 'execution.trigger': attrs.trigger, - 'execution.blocks_executed': attrs.blocksExecuted, - 'execution.has_errors': attrs.hasErrors, - ...(attrs.totalCost !== undefined && { 'execution.total_cost': attrs.totalCost }), + [TraceAttr.WorkflowId]: attrs.workflowId, + [TraceAttr.ExecutionDurationMs]: attrs.durationMs, + [TraceAttr.ExecutionStatus]: attrs.status, + [TraceAttr.ExecutionTrigger]: attrs.trigger, + [TraceAttr.ExecutionBlocksExecuted]: attrs.blocksExecuted, + [TraceAttr.ExecutionHasErrors]: attrs.hasErrors, + ...(attrs.totalCost !== undefined && { [TraceAttr.ExecutionTotalCost]: attrs.totalCost }), }) }, @@ -614,7 +614,7 @@ export const PlatformEvents = { trackPlatformEvent('platform.knowledge_base.created', { 'knowledge_base.id': attrs.knowledgeBaseId, 'knowledge_base.name': attrs.name, - ...(attrs.workspaceId && { 'workspace.id': attrs.workspaceId }), + ...(attrs.workspaceId && { [TraceAttr.WorkspaceId]: attrs.workspaceId }), }) }, @@ -641,12 +641,12 @@ export const PlatformEvents = { }) => { trackPlatformEvent('platform.knowledge_base.documents_uploaded', { 'knowledge_base.id': attrs.knowledgeBaseId, - 'documents.count': attrs.documentsCount, - 'documents.upload_type': attrs.uploadType, - ...(attrs.chunkSize !== undefined && { 'processing.chunk_size': attrs.chunkSize }), - ...(attrs.recipe && { 'processing.recipe': attrs.recipe }), - ...(attrs.mimeType && { 'document.mime_type': attrs.mimeType }), - ...(attrs.fileSize !== undefined && { 'document.file_size': attrs.fileSize }), + [TraceAttr.DocumentsCount]: attrs.documentsCount, + [TraceAttr.DocumentsUploadType]: attrs.uploadType, + ...(attrs.chunkSize !== undefined && { [TraceAttr.ProcessingChunkSize]: attrs.chunkSize }), + ...(attrs.recipe && { [TraceAttr.ProcessingRecipe]: attrs.recipe }), + ...(attrs.mimeType && { [TraceAttr.DocumentMimeType]: attrs.mimeType }), + ...(attrs.fileSize !== undefined && { [TraceAttr.DocumentFileSize]: attrs.fileSize }), }) }, @@ -673,10 +673,10 @@ export const PlatformEvents = { 'search.document_ids': attrs.documentIds, 'search.documents_count': attrs.documentIds.length, 'search.connector_types': attrs.connectorTypes, - 'search.results_count': attrs.resultsCount, + [TraceAttr.SearchResultsCount]: attrs.resultsCount, 'search.principal_kind': attrs.principalKind, - ...(attrs.workspaceId && { 'workspace.id': attrs.workspaceId }), - ...(attrs.actorUserId && { 'user.id': attrs.actorUserId }), + ...(attrs.workspaceId && { [TraceAttr.WorkspaceId]: attrs.workspaceId }), + ...(attrs.actorUserId && { [TraceAttr.UserId]: attrs.actorUserId }), ...(attrs.delegatedServiceId && { 'search.delegated_service': attrs.delegatedServiceId }), ...(attrs.accessScopeKind && { 'search.access_scope_kind': attrs.accessScopeKind }), ...(attrs.surface && { 'search.surface': attrs.surface }), @@ -688,8 +688,8 @@ export const PlatformEvents = { */ apiKeyGenerated: (attrs: { userId: string; keyName?: string }) => { trackPlatformEvent('platform.api_key.generated', { - 'user.id': attrs.userId, - ...(attrs.keyName && { 'api_key.name': attrs.keyName }), + [TraceAttr.UserId]: attrs.userId, + ...(attrs.keyName && { [TraceAttr.ApiKeyName]: attrs.keyName }), }) }, @@ -698,8 +698,8 @@ export const PlatformEvents = { */ apiKeyRevoked: (attrs: { userId: string; keyId: string }) => { trackPlatformEvent('platform.api_key.revoked', { - 'user.id': attrs.userId, - 'api_key.id': attrs.keyId, + [TraceAttr.UserId]: attrs.userId, + [TraceAttr.ApiKeyId]: attrs.keyId, }) }, @@ -708,9 +708,9 @@ export const PlatformEvents = { */ oauthConnected: (attrs: { userId: string; provider: string; workspaceId?: string }) => { trackPlatformEvent('platform.oauth.connected', { - 'user.id': attrs.userId, - 'oauth.provider': attrs.provider, - ...(attrs.workspaceId && { 'workspace.id': attrs.workspaceId }), + [TraceAttr.UserId]: attrs.userId, + [TraceAttr.OauthProvider]: attrs.provider, + ...(attrs.workspaceId && { [TraceAttr.WorkspaceId]: attrs.workspaceId }), }) }, @@ -719,8 +719,8 @@ export const PlatformEvents = { */ oauthDisconnected: (attrs: { userId: string; provider: string }) => { trackPlatformEvent('platform.oauth.disconnected', { - 'user.id': attrs.userId, - 'oauth.provider': attrs.provider, + [TraceAttr.UserId]: attrs.userId, + [TraceAttr.OauthProvider]: attrs.provider, }) }, @@ -734,10 +734,10 @@ export const PlatformEvents = { workspaceId?: string }) => { trackPlatformEvent('platform.webhook.created', { - 'webhook.id': attrs.webhookId, - 'workflow.id': attrs.workflowId, - 'webhook.provider': attrs.provider, - ...(attrs.workspaceId && { 'workspace.id': attrs.workspaceId }), + [TraceAttr.WebhookId]: attrs.webhookId, + [TraceAttr.WorkflowId]: attrs.workflowId, + [TraceAttr.WebhookProvider]: attrs.provider, + ...(attrs.workspaceId && { [TraceAttr.WorkspaceId]: attrs.workspaceId }), }) }, @@ -746,8 +746,8 @@ export const PlatformEvents = { */ webhookDeleted: (attrs: { webhookId: string; workflowId: string }) => { trackPlatformEvent('platform.webhook.deleted', { - 'webhook.id': attrs.webhookId, - 'workflow.id': attrs.workflowId, + [TraceAttr.WebhookId]: attrs.webhookId, + [TraceAttr.WorkflowId]: attrs.workflowId, }) }, @@ -761,10 +761,10 @@ export const PlatformEvents = { success: boolean }) => { trackPlatformEvent('platform.webhook.triggered', { - 'webhook.id': attrs.webhookId, - 'workflow.id': attrs.workflowId, - 'webhook.provider': attrs.provider, - 'webhook.trigger_success': attrs.success, + [TraceAttr.WebhookId]: attrs.webhookId, + [TraceAttr.WorkflowId]: attrs.workflowId, + [TraceAttr.WebhookProvider]: attrs.provider, + [TraceAttr.WebhookTriggerSuccess]: attrs.success, }) }, @@ -778,10 +778,10 @@ export const PlatformEvents = { workspaceId: string }) => { trackPlatformEvent('platform.mcp.server_added', { - 'mcp.server_id': attrs.serverId, - 'mcp.server_name': attrs.serverName, - 'mcp.transport': attrs.transport, - 'workspace.id': attrs.workspaceId, + [TraceAttr.McpServerId]: attrs.serverId, + [TraceAttr.McpServerName]: attrs.serverName, + [TraceAttr.McpTransport]: attrs.transport, + [TraceAttr.WorkspaceId]: attrs.workspaceId, }) }, @@ -795,10 +795,10 @@ export const PlatformEvents = { workspaceId: string }) => { trackPlatformEvent('platform.mcp.tool_executed', { - 'mcp.server_id': attrs.serverId, - 'mcp.tool_name': attrs.toolName, - 'mcp.execution_status': attrs.status, - 'workspace.id': attrs.workspaceId, + [TraceAttr.McpServerId]: attrs.serverId, + [TraceAttr.McpToolName]: attrs.toolName, + [TraceAttr.McpExecutionStatus]: attrs.status, + [TraceAttr.WorkspaceId]: attrs.workspaceId, }) }, @@ -811,9 +811,9 @@ export const PlatformEvents = { interval: 'monthly' | 'yearly' }) => { trackPlatformEvent('platform.billing.subscription_created', { - 'user.id': attrs.userId, - 'billing.plan': attrs.plan, - 'billing.interval': attrs.interval, + [TraceAttr.UserId]: attrs.userId, + [TraceAttr.BillingPlan]: attrs.plan, + [TraceAttr.BillingInterval]: attrs.interval, }) }, @@ -827,10 +827,10 @@ export const PlatformEvents = { changeType: 'upgrade' | 'downgrade' }) => { trackPlatformEvent('platform.billing.subscription_changed', { - 'user.id': attrs.userId, - 'billing.previous_plan': attrs.previousPlan, - 'billing.new_plan': attrs.newPlan, - 'billing.change_type': attrs.changeType, + [TraceAttr.UserId]: attrs.userId, + [TraceAttr.BillingPreviousPlan]: attrs.previousPlan, + [TraceAttr.BillingNewPlan]: attrs.newPlan, + [TraceAttr.BillingChangeType]: attrs.changeType, }) }, @@ -839,8 +839,8 @@ export const PlatformEvents = { */ subscriptionCancelled: (attrs: { userId: string; plan: string }) => { trackPlatformEvent('platform.billing.subscription_cancelled', { - 'user.id': attrs.userId, - 'billing.plan': attrs.plan, + [TraceAttr.UserId]: attrs.userId, + [TraceAttr.BillingPlan]: attrs.plan, }) }, @@ -851,7 +851,7 @@ export const PlatformEvents = { trackPlatformEvent('platform.folder.created', { 'folder.id': attrs.folderId, 'folder.name': attrs.name, - 'workspace.id': attrs.workspaceId, + [TraceAttr.WorkspaceId]: attrs.workspaceId, }) }, @@ -861,7 +861,7 @@ export const PlatformEvents = { folderDeleted: (attrs: { folderId: string; workspaceId: string }) => { trackPlatformEvent('platform.folder.deleted', { 'folder.id': attrs.folderId, - 'workspace.id': attrs.workspaceId, + [TraceAttr.WorkspaceId]: attrs.workspaceId, }) }, @@ -955,10 +955,10 @@ export const PlatformEvents = { hasOutputConfigs: boolean }) => { trackPlatformEvent('platform.chat.deployed', { - 'chat.id': attrs.chatId, - 'workflow.id': attrs.workflowId, - 'chat.auth_type': attrs.authType, - 'chat.has_output_configs': attrs.hasOutputConfigs, + [TraceAttr.ChatId]: attrs.chatId, + [TraceAttr.WorkflowId]: attrs.workflowId, + [TraceAttr.ChatAuthType]: attrs.authType, + [TraceAttr.ChatHasOutputConfigs]: attrs.hasOutputConfigs, }) }, } diff --git a/apps/sim/lib/core/utils/with-route-handler.ts b/apps/sim/lib/core/utils/with-route-handler.ts index 7de2af3d220..e0576bd6310 100644 --- a/apps/sim/lib/core/utils/with-route-handler.ts +++ b/apps/sim/lib/core/utils/with-route-handler.ts @@ -126,7 +126,7 @@ function applyResponseHeaders( /** * Extracts the 32-hex trace id from a W3C `traceparent` header * (`00---`) so cross-service log lines can be - * grepped by the same trace id the Go copilot logs. Returns undefined for + * grepped by the trace id their spans share. Returns undefined for * absent or malformed headers and the all-zero (invalid) trace id. */ function traceIdFromTraceparent(header: string | null | undefined): string | undefined { diff --git a/apps/sim/lib/desktop/executor/bound-turn.integration.ts b/apps/sim/lib/desktop/executor/bound-turn.integration.ts index 9366ee9fae8..377f2409b31 100644 --- a/apps/sim/lib/desktop/executor/bound-turn.integration.ts +++ b/apps/sim/lib/desktop/executor/bound-turn.integration.ts @@ -82,7 +82,6 @@ import { import { abortRun } from '@/lib/mothership/request/application/controls' import { prePersistClientExecutableToolCall, sseHandlers } from '@/lib/mothership/request/handlers' import { waitForClientToolCompletion } from '@/lib/mothership/request/tools/client' -import { TraceCollector } from '@/lib/mothership/request/trace' import type { StreamEvent, StreamingContext } from '@/lib/mothership/request/types' import { chatSandboxSessionKey } from '@/lib/mothership/tools/sandbox-session-key' import { POST as toolPermissionPOST } from '@/app/api/copilot/tool-permission/route' @@ -257,7 +256,6 @@ describe.runIf(Boolean(redisUrl))("a turn bound to a desktop's background execut accumulatedContent: '', finalAssistantContent: '', sawMainToolCall: false, - trace: new TraceCollector(), contentBlocks: [], toolCalls: new Map(), pendingToolPromises: new Map(), diff --git a/apps/sim/lib/knowledge/__integration__/upload-read-provenance.integration.ts b/apps/sim/lib/knowledge/__integration__/upload-read-provenance.integration.ts index 5f386ac758e..0c88818d51d 100644 --- a/apps/sim/lib/knowledge/__integration__/upload-read-provenance.integration.ts +++ b/apps/sim/lib/knowledge/__integration__/upload-read-provenance.integration.ts @@ -123,7 +123,7 @@ async function readUpload(ids: Awaited>) { } } -/** Models promotion by an already-running Go tool after the new reader captured its revision. */ +/** Models promotion by an already-running worker tool after the new reader captured its revision. */ async function promoteUpload(ids: Awaited>) { await db .update(workspaceFiles) diff --git a/apps/sim/lib/knowledge/application/chat.ts b/apps/sim/lib/knowledge/application/chat.ts index b3ea7ab995e..78d74b16260 100644 --- a/apps/sim/lib/knowledge/application/chat.ts +++ b/apps/sim/lib/knowledge/application/chat.ts @@ -170,7 +170,7 @@ export const organizationSearchChat: OperationUseCase< organizationId, chatId, simRequestId: messageId, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', interactive: false, autoExecuteTools: true, secretActorUserId: null, diff --git a/apps/sim/lib/knowledge/application/slack-search/assistant.test.ts b/apps/sim/lib/knowledge/application/slack-search/assistant.test.ts index 304c3c7e4b2..bd0ce7e9e78 100644 --- a/apps/sim/lib/knowledge/application/slack-search/assistant.test.ts +++ b/apps/sim/lib/knowledge/application/slack-search/assistant.test.ts @@ -224,7 +224,7 @@ describe('organization Assistant from Slack', () => { userId: 'member1', organizationId: 'org1', chatId: 'chat1', - goRoute: '/api/mothership', + workerRoute: '/api/mothership', autoExecuteTools: true, searchSurface: 'slack', billingAttribution: { organizationId: 'org1', actorUserId: 'member1' }, diff --git a/apps/sim/lib/knowledge/application/slack-search/assistant.ts b/apps/sim/lib/knowledge/application/slack-search/assistant.ts index dad4f2e0c7f..8a4ee86b145 100644 --- a/apps/sim/lib/knowledge/application/slack-search/assistant.ts +++ b/apps/sim/lib/knowledge/application/slack-search/assistant.ts @@ -266,7 +266,7 @@ export async function runSlackSearchAssistant( chatId: chat.id, executionId, runId, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', billingAttribution, environmentContext, resolvedSecretTraceRegistry: environmentContext.resolvedSecretTraceRegistry, diff --git a/apps/sim/lib/logs/execution/logger.ts b/apps/sim/lib/logs/execution/logger.ts index dce29e84919..9bb4422fd31 100644 --- a/apps/sim/lib/logs/execution/logger.ts +++ b/apps/sim/lib/logs/execution/logger.ts @@ -1826,7 +1826,7 @@ export class ExecutionLogger { // construction: under the lock no delta collides, so the new sum is // the prior workflow-source sum plus the deltas just inserted. This // supersedes the main-transaction GREATEST baseline except when the - // display total contains Mothership cost owned by Go update-cost. + // display total contains Mothership cost owned by the worker's update-cost callback. // // Gated on billable deltas: a boundary that only wrote zero-cost // unbilled rows has changed no cost, and must not restate cost_total. diff --git a/apps/sim/lib/logs/execution/logging-factory.ts b/apps/sim/lib/logs/execution/logging-factory.ts index 8813fe275ea..23f73591c7c 100644 --- a/apps/sim/lib/logs/execution/logging-factory.ts +++ b/apps/sim/lib/logs/execution/logging-factory.ts @@ -120,7 +120,7 @@ export interface CostSummary { models: Record /** * Model costs owned by workflow finalization. Mothership spans remain in - * `models` and the display totals, but Go's cumulative update-cost path owns + * `models` and the display totals, but the worker's cumulative update-cost path owns * their usage ledger rows. */ workflowLedgerModels: Record @@ -138,7 +138,7 @@ function isModelBreakdownSpan(span: CostTraceSpan): boolean { return span.type === 'model' } -/** Mothership model spend is ledgered cumulatively by Go update-cost. */ +/** Mothership model spend is ledgered cumulatively by the worker's update-cost callback. */ function isMothershipUpdateCostOwned(span: CostTraceSpan): boolean { return span.type === 'mothership' } diff --git a/apps/sim/lib/media/ffmpeg-limits.ts b/apps/sim/lib/media/ffmpeg-limits.ts index 06fc100377d..147e1d54c31 100644 --- a/apps/sim/lib/media/ffmpeg-limits.ts +++ b/apps/sim/lib/media/ffmpeg-limits.ts @@ -1,17 +1,15 @@ /** * Execution bounds the ffmpeg tool enforces. * - * These are mirrored into the Go tool catalog - * (`copilot/internal/tools/catalog/other/ffmpeg.go`), which is what lets the - * router reject an out-of-range argument structurally, before any storage read - * or child process. The model itself learns the limits from the parameter - * descriptions — copilot's `NormalizeToolParameters` drops every JSON Schema - * keyword outside its allowlist on the way to a provider, so the numbers are - * stated in prose there too. `ffmpeg-schema-parity.test.ts` fails when the two - * copies drift. + * These are mirrored into the worker's tool catalog (the mothership repo's + * `worker/packages/contracts/tool-catalog-v1.json`), which is what lets the + * worker reject an out-of-range argument structurally, before any storage read + * or child process. The model also learns the limits from the parameter + * descriptions, so the numbers are stated in prose there too. + * `ffmpeg-schema-parity.test.ts` fails when the two copies drift. * * `maxScalePixels` has no JSON Schema equivalent, so it lives in the parameter - * description on the Go side and is enforced here only. + * description in the catalog and is enforced here only. */ export const FFMPEG_LIMITS = { /** Every input costs a full re-encode pass in `concat`, the only multi-input operation. */ diff --git a/apps/sim/lib/media/ffmpeg-schema-parity.test.ts b/apps/sim/lib/media/ffmpeg-schema-parity.test.ts index 0de1229c16e..b8359e13f9a 100644 --- a/apps/sim/lib/media/ffmpeg-schema-parity.test.ts +++ b/apps/sim/lib/media/ffmpeg-schema-parity.test.ts @@ -4,13 +4,14 @@ import { TOOL_RUNTIME_SCHEMAS } from '@/lib/mothership/generated/tool-schemas-v1 /** * The ffmpeg bounds live twice: here, where the executor enforces them, and in - * the Go tool catalog, where they become the JSON Schema the model reads and - * Ajv checks at the router. Drift between the two is silent and user-visible — + * the worker's tool catalog, where they become the JSON Schema the model reads + * and the worker checks arguments against. Drift between the two is silent and user-visible — * the model is told one ceiling and the transcode refuses at another — so pin * the generated schema against the executor's own numbers. * - * When this fails, change `ffmpeg.go` in the copilot repo and regenerate; do - * not edit the generated schema. + * When this fails, change the ffmpeg entry in the mothership repo's + * `worker/packages/contracts/tool-catalog-v1.json` and run + * `bun run mship-tools:generate`; do not edit the generated schema. */ interface SchemaNode { properties?: Record diff --git a/apps/sim/lib/model-router/resolve.test.ts b/apps/sim/lib/model-router/resolve.test.ts index dc566dac3f4..98542cb5202 100644 --- a/apps/sim/lib/model-router/resolve.test.ts +++ b/apps/sim/lib/model-router/resolve.test.ts @@ -5,9 +5,9 @@ import { mothershipAgentUrlMockFns, } from '@sim/testing/mocks/mothership-agent-url.mock' import { - mothershipGoFetchMock, - mothershipGoFetchMockFns, -} from '@sim/testing/mocks/mothership-go-fetch.mock' + mothershipWorkerFetchMock, + mothershipWorkerFetchMockFns, +} from '@sim/testing/mocks/mothership-worker-fetch.mock' import { permissionCheckMock, permissionCheckMockFns, @@ -15,7 +15,7 @@ import { import { providersUtilsMock, providersUtilsMockFns } from '@sim/testing/mocks/providers-utils.mock' import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) vi.mock('@/ee/access-control/utils/permission-check', () => permissionCheckMock) @@ -30,7 +30,7 @@ import { import type { ExecutionContext } from '@/executor/types' import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry' -const mockFetchGo = mothershipGoFetchMockFns.mockFetchGo +const mockFetchWorker = mothershipWorkerFetchMockFns.mockFetchWorker const mockGetMothershipBaseURL = mothershipAgentUrlMockFns.mockGetMothershipBaseURL const mockGetProviderFromModel = providersUtilsMockFns.mockGetProviderFromModel @@ -112,7 +112,7 @@ describe('resolveAutoModel', () => { byTier.forEach((expected, index) => { const tier = String(index + 1) it(`routes ${mediaKind} tier ${tier} to ${expected}`, async () => { - mockFetchGo.mockResolvedValue(routerResponse({ choice: tier })) + mockFetchWorker.mockResolvedValue(routerResponse({ choice: tier })) const result = await resolveAutoModel({ ctx, @@ -128,7 +128,7 @@ describe('resolveAutoModel', () => { } it('never crosses media kinds when walking down from a denied tier', async () => { - mockFetchGo.mockResolvedValue(routerResponse({ choice: '3' })) + mockFetchWorker.mockResolvedValue(routerResponse({ choice: '3' })) // gpt-5.6-sol denied; the file column must drop to sonnet, never to a // Fireworks model that cannot accept the attachment at all. mockValidateModelProvider.mockImplementation(async (_u, _w, model: string) => { @@ -147,7 +147,7 @@ describe('resolveAutoModel', () => { }) it('uses the router choice and applies the cost multiplier when billable', async () => { - mockFetchGo.mockResolvedValue( + mockFetchWorker.mockResolvedValue( routerResponse({ choice: '2', decidedBy: 'llm', @@ -174,7 +174,7 @@ describe('resolveAutoModel', () => { }) it('does not bill when the response omits billable (fail-safe)', async () => { - mockFetchGo.mockResolvedValue( + mockFetchWorker.mockResolvedValue( routerResponse({ choice: '1', usage: { @@ -197,7 +197,7 @@ describe('resolveAutoModel', () => { }) it('falls back when the router call fails', async () => { - mockFetchGo.mockRejectedValue(new Error('timeout')) + mockFetchWorker.mockRejectedValue(new Error('timeout')) const result = await resolveAutoModel({ ctx, blockId: 'b1', @@ -209,7 +209,7 @@ describe('resolveAutoModel', () => { }) it('falls back when every pool model is denied by workspace permissions', async () => { - mockFetchGo.mockResolvedValue(routerResponse({ choice: '1' })) + mockFetchWorker.mockResolvedValue(routerResponse({ choice: '1' })) mockValidateModelProvider.mockRejectedValue(new Error('provider blocked')) const result = await resolveAutoModel({ ctx, diff --git a/apps/sim/lib/model-router/resolve.ts b/apps/sim/lib/model-router/resolve.ts index 44f032bfa81..327c4624ad0 100644 --- a/apps/sim/lib/model-router/resolve.ts +++ b/apps/sim/lib/model-router/resolve.ts @@ -8,7 +8,7 @@ import type { ModelRouterResponse, ModelRouterUsage, } from '@/lib/mothership/generated/model-router' -import { fetchGo } from '@/lib/mothership/request/go/fetch' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url' import { validateModelProvider } from '@/ee/access-control/utils/permission-check' import type { ExecutionContext } from '@/executor/types' @@ -211,7 +211,7 @@ async function callModelRouter( ): Promise | null> { const baseURL = await getMothershipBaseURL({ userId: ctx.userId ?? '' }) - const res = await fetchGo(`${baseURL}/api/model-router`, { + const res = await fetchWorker(`${baseURL}/api/model-router`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -235,7 +235,7 @@ async function callModelRouter( executionId: ctx.executionId, } satisfies ModelRouterRequest), signal: AbortSignal.timeout(MODEL_ROUTER_TIMEOUT_MS), - spanName: 'sim → go /api/model-router', + spanName: 'sim → worker /api/model-router', operation: 'model_router', }) diff --git a/apps/sim/lib/mothership/agent-cli/execute.ts b/apps/sim/lib/mothership/agent-cli/execute.ts index 428b981fa06..1d7c1d4308d 100644 --- a/apps/sim/lib/mothership/agent-cli/execute.ts +++ b/apps/sim/lib/mothership/agent-cli/execute.ts @@ -10,7 +10,7 @@ import type { AgentCliRequest, } from '@/lib/mothership/generated/agent-cli' import type { ResourceChange } from '@/lib/mothership/generated/resources' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { withCopilotSpan } from '@/lib/mothership/request/otel' import { chatSandboxSessionKey } from '@/lib/mothership/tools/sandbox-session-key' diff --git a/apps/sim/lib/mothership/agent-cli/saved-run-read.live.test.ts b/apps/sim/lib/mothership/agent-cli/saved-run-read.live.test.ts index 71801fffbca..329e5dc6e5f 100644 --- a/apps/sim/lib/mothership/agent-cli/saved-run-read.live.test.ts +++ b/apps/sim/lib/mothership/agent-cli/saved-run-read.live.test.ts @@ -140,6 +140,7 @@ import { } from '@/lib/mothership/generated/protocol' import { prepareInboxAttachments } from '@/lib/mothership/inbox/attachments' import { claimRunController } from '@/lib/mothership/request/lifecycle/controller-ownership' +import { persistedStreamRecoveryConfig } from '@/lib/mothership/request/lifecycle/recovery-config' import { runCopilotLifecycle } from '@/lib/mothership/request/lifecycle/run' import { isToolCallStreamEvent, @@ -1374,7 +1375,7 @@ describe.skipIf(!process.env.MSHIP_TEST_DATABASE_URL)( message: { id: streamId, content: 'Preserve the original instruction and attachments' }, recovery: { kind: 'interactive_stream' as const, - goRoute: '/api/mothership' as const, + workerRoute: '/api/mothership' as const, clientToolPickupExpected: false, request: { messageId: streamId, @@ -1396,7 +1397,7 @@ describe.skipIf(!process.env.MSHIP_TEST_DATABASE_URL)( const run = await admitChatTurn.execute({ principal: admissionPrincipal, input }) expect(run.streamId).toBe(input.message.id) expect(run.requestContext).toMatchObject({ - recovery: input.recovery, + recovery: persistedStreamRecoveryConfig(input.recovery), controllerToken: input.lease.value, }) const messages = await loadCopilotChatMessages(input.chatId) @@ -1518,7 +1519,7 @@ describe.skipIf(!process.env.MSHIP_TEST_DATABASE_URL)( executionId: generateId(), runId: generateId(), interactive: true, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', onComplete, recovery: { streamId, @@ -3261,12 +3262,13 @@ describe.skipIf(!process.env.MSHIP_TEST_DATABASE_URL)( workflowId, chatId, ...(resume ? { runId: resume.id, executionId: resume.executionId } : {}), - goRoute: initialRoute, + workerRoute: initialRoute, interactive: false, clientToolPickupExpected: false, flushAfterEvent: false, onEvent: (event) => { - if (event.type === 'tool' && event.payload.replay) replayedToolFrames++ + if (event.type === 'tool' && 'replay' in event.payload && event.payload.replay) + replayedToolFrames++ if (resume && isToolCallStreamEvent(event) && !event.payload.replay) secondSawCall.resolve() }, @@ -3845,7 +3847,7 @@ describe.skipIf(!process.env.MSHIP_TEST_DATABASE_URL)( workspaceId, workflowId: draftId, chatId, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', interactive: false, clientToolPickupExpected: false, flushAfterEvent: false, diff --git a/apps/sim/lib/mothership/agent-cli/traced-transport.ts b/apps/sim/lib/mothership/agent-cli/traced-transport.ts index a203c4f3255..e4babb26cea 100644 --- a/apps/sim/lib/mothership/agent-cli/traced-transport.ts +++ b/apps/sim/lib/mothership/agent-cli/traced-transport.ts @@ -1,8 +1,8 @@ import { SpanKind, SpanStatusCode } from '@opentelemetry/api' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' -import { traceHeaders } from '@/lib/mothership/request/go/propagation' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { getCopilotTracer } from '@/lib/mothership/request/otel' +import { traceHeaders } from '@/lib/mothership/request/worker/propagation' /** Links internal CLI HTTP calls to their tool; the span ends at response headers. */ export function createTracedCliTransport(endpoint: string, transport: typeof fetch): typeof fetch { diff --git a/apps/sim/lib/mothership/agent-cli/workspace.ts b/apps/sim/lib/mothership/agent-cli/workspace.ts index e69b7f58f9e..c1fa2f546f2 100644 --- a/apps/sim/lib/mothership/agent-cli/workspace.ts +++ b/apps/sim/lib/mothership/agent-cli/workspace.ts @@ -15,7 +15,7 @@ import { } from '@/lib/mothership/auth/application-delegation' import type { AgentCliRawResult, AgentCliRequest } from '@/lib/mothership/generated/agent-cli' import type { ResourceChange } from '@/lib/mothership/generated/resources' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { withCopilotSpan } from '@/lib/mothership/request/otel' import { isFileResourceType } from '@/lib/mothership/resources/types' import { WORKSPACE_FILES_DELEGATION_AUDIENCE } from '@/lib/workspace-files/application/authorization' diff --git a/apps/sim/lib/mothership/async-runs/lifecycle.ts b/apps/sim/lib/mothership/async-runs/lifecycle.ts index b24928becf2..5c4d27e78f6 100644 --- a/apps/sim/lib/mothership/async-runs/lifecycle.ts +++ b/apps/sim/lib/mothership/async-runs/lifecycle.ts @@ -1,8 +1,5 @@ import type { CopilotAsyncToolStatus, CopilotToolPermissionDecision } from '@sim/db/schema' -import { - MothershipStreamV1AsyncToolRecordStatus, - MothershipStreamV1ToolOutcome, -} from '@/lib/mothership/generated/mothership-stream-v1' +import { MothershipStreamV1ToolOutcome } from '@/lib/mothership/generated/mothership-stream-v1' /** Version 3 binds execution to the persisted invocation and supports reviewed argument edits. */ export const SIM_TOOL_EXECUTION_VERSION = 3 @@ -14,7 +11,15 @@ export function supportsSimToolExecution(version: number | undefined): boolean { return SUPPORTED_SIM_TOOL_EXECUTION_VERSIONS.some((supported) => supported === version) } -export const ASYNC_TOOL_STATUS = MothershipStreamV1AsyncToolRecordStatus +/** The async tool record lifecycle, Sim's own `copilot_async_tool_status` column; not a wire field. */ +export const ASYNC_TOOL_STATUS = { + pending: 'pending', + running: 'running', + completed: 'completed', + failed: 'failed', + cancelled: 'cancelled', + delivered: 'delivered', +} as const satisfies { [K in CopilotAsyncToolStatus]: K } export const EXECUTABLE_TOOL_PERMISSION_DECISIONS = [ 'allow', diff --git a/apps/sim/lib/mothership/async-runs/repository.ts b/apps/sim/lib/mothership/async-runs/repository.ts index 0b6a8c50894..4ac74202b8d 100644 --- a/apps/sim/lib/mothership/async-runs/repository.ts +++ b/apps/sim/lib/mothership/async-runs/repository.ts @@ -62,12 +62,12 @@ import { supportsSimToolExecution, } from '@/lib/mothership/async-runs/lifecycle' import { TOOL_WATCHDOG_RESUME_GRACE_MS } from '@/lib/mothership/constants' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' import { traceMothershipQuery, traceMothershipTransaction, } from '@/lib/mothership/observability/database' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { isDurableStreamRun, parseRecordedSecretAuthority, @@ -604,7 +604,7 @@ export async function recordRunBillingAdmission( /** * The secret authority a turn was admitted with, for a lane that rebuilds its tool context from - * Go's request instead of inheriting the turn's. Headless runs record it at admission; an + * the worker's request instead of inheriting the turn's. Headless runs record it at admission; an * interactive chat narrows it only in assistant mode, so its saved request decides. Anything * else — no run, another user's or chat's run, an unrecorded headless run — mounts nothing. */ diff --git a/apps/sim/lib/mothership/async-runs/tool-identity.integration.ts b/apps/sim/lib/mothership/async-runs/tool-identity.integration.ts index cab21f5bf1c..82a48e6a323 100644 --- a/apps/sim/lib/mothership/async-runs/tool-identity.integration.ts +++ b/apps/sim/lib/mothership/async-runs/tool-identity.integration.ts @@ -80,7 +80,7 @@ import { import { createProviderToolCallIdentity, scopeProviderToolCallId, -} from '@/lib/mothership/request/go/tool-call-identity' +} from '@/lib/mothership/request/worker/tool-call-identity' const connection = postgres(databaseUrl, { max: 1 }) const providerId = 'call_reused_fixture' diff --git a/apps/sim/lib/mothership/block-visibility.ts b/apps/sim/lib/mothership/block-visibility.ts index 430fe099fe9..830da013bac 100644 --- a/apps/sim/lib/mothership/block-visibility.ts +++ b/apps/sim/lib/mothership/block-visibility.ts @@ -5,7 +5,7 @@ import { getWorkspaceWithOwner } from '@/lib/workspaces/permissions/utils' /** * Copilot-side resolver for the viewer's block-visibility projection. * - * A single mothership turn fans out into many Go→Sim tool callbacks; resolving + * A single mothership turn fans out into many worker → Sim tool callbacks; resolving * visibility per callback would repeat the workspace→org lookup (and, for * admin-gated rules, a replica read) N times. This memoizes the resolved state * per (userId, workspaceId) for a short TTL matching the AppConfig cache diff --git a/apps/sim/lib/mothership/chat/application/admit-turn.test.ts b/apps/sim/lib/mothership/chat/application/admit-turn.test.ts index c1190f5d4e5..29cec3d597a 100644 --- a/apps/sim/lib/mothership/chat/application/admit-turn.test.ts +++ b/apps/sim/lib/mothership/chat/application/admit-turn.test.ts @@ -68,7 +68,7 @@ function input(mode: 'assistant' | 'agent' | 'plan' = 'assistant') { message: { id: streamId, content: 'Find the policy', requestMode: mode }, recovery: { kind: 'interactive_stream' as const, - goRoute: '/api/mothership' as const, + workerRoute: '/api/mothership' as const, clientToolPickupExpected: false, request: { userId: 'actor', diff --git a/apps/sim/lib/mothership/chat/application/admit-turn.ts b/apps/sim/lib/mothership/chat/application/admit-turn.ts index 97da3da6fb5..439efef10c0 100644 --- a/apps/sim/lib/mothership/chat/application/admit-turn.ts +++ b/apps/sim/lib/mothership/chat/application/admit-turn.ts @@ -3,7 +3,6 @@ import { copilotChats, idempotencyKey } from '@sim/db/schema' import { createLogger } from '@sim/logger' import { getErrorMessage } from '@sim/utils/errors' import { and, eq, isNull, sql } from 'drizzle-orm' -import type { z } from 'zod' import { defineWorkspaceOperation } from '@/lib/core/application' import { defineOrganizationOperation } from '@/lib/core/application/organization-operation' import { OrchestrationError } from '@/lib/core/orchestration/types' @@ -23,7 +22,10 @@ import { } from '@/lib/mothership/chat/persisted-message' import { publishChatStatusChanged } from '@/lib/mothership/chat-status' import type { MothershipEffort } from '@/lib/mothership/model-options' -import { StreamRecoveryConfigSchema } from '@/lib/mothership/request/lifecycle/recovery-config' +import { + persistedStreamRecoveryConfig, + type StreamRecoveryConfigInput, +} from '@/lib/mothership/request/lifecycle/recovery-config' import { assertChatStreamLease, type ChatStreamLease, @@ -35,7 +37,7 @@ interface AdmitTurnInput { executionId: string requestId: string message: UserMessageParams - recovery: z.input + recovery: StreamRecoveryConfigInput lease: ChatStreamLease sendClaim: { normalizedKey: string; claimToken: string } notifyWorkspaceStatus: boolean @@ -124,7 +126,7 @@ export const admitChatTurn = defineAuthorizedChatUseCase({ authorizationOptions: {}, async execute({ principal, context, input }) { const { userId, workspaceId, organizationId, chatId } = context - const recovery = StreamRecoveryConfigSchema.parse(input.recovery) + const recovery = persistedStreamRecoveryConfig(input.recovery) const request = recovery.request if ( request.userId !== userId || diff --git a/apps/sim/lib/mothership/chat/application/admit-unattended-turn.ts b/apps/sim/lib/mothership/chat/application/admit-unattended-turn.ts index cb431c47960..c02e8d442c9 100644 --- a/apps/sim/lib/mothership/chat/application/admit-unattended-turn.ts +++ b/apps/sim/lib/mothership/chat/application/admit-unattended-turn.ts @@ -1,7 +1,6 @@ import type { DelegatedPrincipal, OrganizationDelegatedPrincipal } from '@sim/auth/principal' import { copilotRuns } from '@sim/db/schema' import { eq } from 'drizzle-orm' -import type { z } from 'zod' import { defineWorkspaceOperation } from '@/lib/core/application' import { defineOrganizationOperation } from '@/lib/core/application/organization-operation' import { OrchestrationError } from '@/lib/core/orchestration/types' @@ -11,7 +10,10 @@ import { defineAuthorizedChatUseCase } from '@/lib/mothership/chat/application/a import { resolveOwnedWorkspaceChatContext } from '@/lib/mothership/chat/application/context' import { appendCopilotChatMessages } from '@/lib/mothership/chat/messages-store' import { buildPersistedUserMessage } from '@/lib/mothership/chat/persisted-message' -import { StreamRecoveryConfigSchema } from '@/lib/mothership/request/lifecycle/recovery-config' +import { + persistedStreamRecoveryConfig, + type StreamRecoveryConfigInput, +} from '@/lib/mothership/request/lifecycle/recovery-config' import { assertChatStreamLease, type ChatStreamLease, @@ -23,7 +25,7 @@ export const UNATTENDED_TURN_AUDIENCE = 'sim:copilot-jobs' interface AdmitUnattendedTurnInput { /** The turn's request is its identity: its message id names the run, its stream and its user message. */ - recovery: z.input + recovery: StreamRecoveryConfigInput lease: ChatStreamLease secretAuthority: TurnSecretAuthority } @@ -76,7 +78,7 @@ export const admitUnattendedTurn = defineAuthorizedChatUseCase({ }, async execute({ context, input }): Promise { const { userId, workspaceId, chatId } = context - const recovery = StreamRecoveryConfigSchema.parse(input.recovery) + const recovery = persistedStreamRecoveryConfig(input.recovery) const request = recovery.request if ( recovery.kind !== 'unattended_stream' || diff --git a/apps/sim/lib/mothership/chat/child-failure-transcript.test.ts b/apps/sim/lib/mothership/chat/child-failure-transcript.test.ts index 933ef76e6ec..697368dfd4f 100644 --- a/apps/sim/lib/mothership/chat/child-failure-transcript.test.ts +++ b/apps/sim/lib/mothership/chat/child-failure-transcript.test.ts @@ -179,9 +179,8 @@ describe('child failure transcript boundaries', () => { verify(modelToContentBlocks(recovered), nested, partial) }) - it('persists the handler outcome on its own span and reports failure in the trace', async () => { + it('persists the handler outcome on its own span', async () => { const context = createStreamingContext() - const endSpan = vi.spyOn(context.trace, 'endSpan') for (const frame of childEvents(false, false)) { if (frame.type === 'span') await handleSpanEvent( @@ -201,10 +200,6 @@ describe('child failure transcript boundaries', () => { const restored = contentBlocksToModel(blocks) expect(restored.nodes.get('failed')).toMatchObject({ status: 'error', error: failure }) expect(restored.nodes.get('sibling')).toMatchObject({ status: 'success' }) - expect(endSpan.mock.calls.map(([span, status]) => [span.attributes?.spanId, status])).toEqual([ - ['failed', 'error'], - ['sibling', 'ok'], - ]) expect(context.errors).toEqual([]) expect(context.streamComplete).toBe(false) }) diff --git a/apps/sim/lib/mothership/chat/completion.ts b/apps/sim/lib/mothership/chat/completion.ts index 2f1c943d7d2..f62e72afe89 100644 --- a/apps/sim/lib/mothership/chat/completion.ts +++ b/apps/sim/lib/mothership/chat/completion.ts @@ -6,7 +6,7 @@ import { } from '@/lib/mothership/chat/persisted-message' import { finalizeAssistantTurn } from '@/lib/mothership/chat/terminal-state' import { publishChatStatusChanged } from '@/lib/mothership/chat-status' -import { CopilotChatFinalizeOutcome } from '@/lib/mothership/generated/trace-attribute-values-v1' +import { CopilotChatFinalizeOutcome } from '@/lib/mothership/observability/trace-attribute-values' import { StreamControllerSupersededError } from '@/lib/mothership/request/session/controller-lease' import type { OrchestratorResult } from '@/lib/mothership/request/types' @@ -28,7 +28,7 @@ export function buildOnComplete(params: { * assistant message + invoked tool calls are recorded as * `gen_ai.output.messages` on it before persistence runs. Keeps * the Honeycomb Gen AI view complete across both the Sim root - * span and the Go-side `llm.stream` spans. + * span and the worker's model-call spans. */ otelRoot?: { setOutputMessages: (output: { diff --git a/apps/sim/lib/mothership/chat/effective-transcript.test.ts b/apps/sim/lib/mothership/chat/effective-transcript.test.ts index b121b47481b..be40df30145 100644 --- a/apps/sim/lib/mothership/chat/effective-transcript.test.ts +++ b/apps/sim/lib/mothership/chat/effective-transcript.test.ts @@ -225,8 +225,7 @@ describe('buildEffectiveChatTranscript', () => { stream: { streamId: 'stream-1' }, trace: { requestId: 'req-1' }, payload: { - kind: MothershipStreamV1SessionKind.trace, - requestId: 'req-1', + kind: MothershipStreamV1SessionKind.start, }, }), toBatchEvent(2, { @@ -398,7 +397,6 @@ describe('buildEffectiveChatTranscript', () => { kind: MothershipStreamV1SpanPayloadKind.subagent, event: MothershipStreamV1SpanLifecycleEvent.start, agent: 'workflow', - data: { tool_call_id: 'tc-workflow' }, }, }), toBatchEvent(2, { @@ -417,10 +415,7 @@ describe('buildEffectiveChatTranscript', () => { type: MothershipStreamV1EventType.run, stream, scope, - payload: { - kind: MothershipStreamV1RunKind.compaction_done, - data: { summary_chars: 42 }, - }, + payload: { kind: MothershipStreamV1RunKind.compaction_done }, }), ], previewSessions: [], diff --git a/apps/sim/lib/mothership/chat/effective-transcript.ts b/apps/sim/lib/mothership/chat/effective-transcript.ts index 5e664ccffe7..f7a8b774964 100644 --- a/apps/sim/lib/mothership/chat/effective-transcript.ts +++ b/apps/sim/lib/mothership/chat/effective-transcript.ts @@ -9,7 +9,6 @@ import { type MothershipStreamV1ErrorPayload, MothershipStreamV1EventType, MothershipStreamV1RunKind, - MothershipStreamV1SessionKind, MothershipStreamV1SpanLifecycleEvent, MothershipStreamV1SpanPayloadKind, MothershipStreamV1TextChannel, @@ -18,7 +17,7 @@ import { } from '@/lib/mothership/generated/mothership-stream-v1' import type { ToolActivity } from '@/lib/mothership/generated/protocol' import { CallIntegrationTool } from '@/lib/mothership/generated/tool-catalog-v1' -import { isTerminalStreamStatus } from '@/lib/mothership/request/session/contract' +import { isTerminalStreamStatus, subagentSpanData } from '@/lib/mothership/request/session/contract' import type { FilePreviewSession } from '@/lib/mothership/request/session/file-preview-session-contract' import type { StreamBatchEvent } from '@/lib/mothership/request/session/types' import type { TaskBlockInfo } from '@/lib/mothership/request/types' @@ -281,18 +280,8 @@ export function buildLiveAssistantMessage(params: { : 'main' switch (parsed.type) { - case MothershipStreamV1EventType.session: { - if (parsed.payload.kind === MothershipStreamV1SessionKind.chat) { - continue - } - if (parsed.payload.kind === MothershipStreamV1SessionKind.start) { - continue - } - if (parsed.payload.kind === MothershipStreamV1SessionKind.trace) { - requestId = parsed.payload.requestId - } + case MothershipStreamV1EventType.session: continue - } case MothershipStreamV1EventType.text: { const chunk = parsed.payload.text if (!chunk) { @@ -389,14 +378,8 @@ export function buildLiveAssistantMessage(params: { continue } - const spanData = asPayloadRecord(parsed.payload.data) - const parentToolCallIdFromData = - typeof spanData?.tool_call_id === 'string' - ? spanData.tool_call_id - : typeof spanData?.toolCallId === 'string' - ? spanData.toolCallId - : undefined - const parentToolCallId = scopedParentToolCallId ?? parentToolCallIdFromData + const data = subagentSpanData(parsed.payload.data) + const parentToolCallId = scopedParentToolCallId const name = typeof parsed.payload.agent === 'string' ? parsed.payload.agent : scopedAgentId if (parsed.payload.event === MothershipStreamV1SpanLifecycleEvent.start && name) { if (scopedSpanId) { @@ -411,7 +394,7 @@ export function buildLiveAssistantMessage(params: { kind: MothershipStreamV1SpanPayloadKind.subagent, lifecycle: MothershipStreamV1SpanLifecycleEvent.start, content: name, - ...(typeof spanData?.name === 'string' && spanData.name ? { name: spanData.name } : {}), + ...(data.name ? { name: data.name } : {}), ...(parentToolCallId ? { parentToolCallId } : {}), ...spanIdentity, }) @@ -419,9 +402,6 @@ export function buildLiveAssistantMessage(params: { } if (parsed.payload.event === MothershipStreamV1SpanLifecycleEvent.end) { - if (spanData?.pending === true) { - continue - } if (scopedSpanId) { subagentBySpanId.delete(scopedSpanId) } @@ -438,9 +418,7 @@ export function buildLiveAssistantMessage(params: { type: MothershipStreamV1EventType.span, kind: MothershipStreamV1SpanPayloadKind.subagent, lifecycle: MothershipStreamV1SpanLifecycleEvent.end, - ...(typeof spanData?.error === 'string' && spanData.error - ? { error: spanData.error } - : {}), + ...(data.error ? { error: data.error } : {}), ...(parentToolCallId ? { parentToolCallId } : {}), ...spanIdentity, }) diff --git a/apps/sim/lib/mothership/chat/fork-worker.test.ts b/apps/sim/lib/mothership/chat/fork-worker.test.ts index 63bf3de8351..e30e344da23 100644 --- a/apps/sim/lib/mothership/chat/fork-worker.test.ts +++ b/apps/sim/lib/mothership/chat/fork-worker.test.ts @@ -3,16 +3,16 @@ import { mothershipAgentUrlMockFns, } from '@sim/testing/mocks/mothership-agent-url.mock' import { - mothershipGoFetchMock, - mothershipGoFetchMockFns, -} from '@sim/testing/mocks/mothership-go-fetch.mock' + mothershipWorkerFetchMock, + mothershipWorkerFetchMockFns, +} from '@sim/testing/mocks/mothership-worker-fetch.mock' import { generateId } from '@sim/utils/id' import { beforeEach, expect, it, vi } from 'vitest' import { asOrchestrationError } from '@/lib/core/orchestration/types' import { copyWorkerConversation } from '@/lib/mothership/chat/fork-worker' import type { ForkChatRequest } from '@/lib/mothership/generated/protocol' -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) const request: ForkChatRequest = { @@ -52,8 +52,8 @@ beforeEach(() => { mothershipAgentUrlMockFns.mockGetMothershipBaseURL.mockResolvedValue('http://worker.test') received = [] script = [] - mothershipGoFetchMockFns.mockFetchGo.mockReset() - mothershipGoFetchMockFns.mockFetchGo.mockImplementation( + mothershipWorkerFetchMockFns.mockFetchWorker.mockReset() + mothershipWorkerFetchMockFns.mockFetchWorker.mockImplementation( async (_url: string, init: { body: string }) => { received.push(JSON.parse(init.body)) return (script.shift() ?? receipt)() diff --git a/apps/sim/lib/mothership/chat/fork-worker.ts b/apps/sim/lib/mothership/chat/fork-worker.ts index d5570db0c2b..93b7b5b2051 100644 --- a/apps/sim/lib/mothership/chat/fork-worker.ts +++ b/apps/sim/lib/mothership/chat/fork-worker.ts @@ -3,8 +3,8 @@ import { getErrorMessage } from '@sim/utils/errors' import { isRetryableNetworkError } from '@/lib/core/errors/retryable-infrastructure' import { OrchestrationError } from '@/lib/core/orchestration/types' import { type ForkChatRequest, ForkChatResponse } from '@/lib/mothership/generated/protocol' -import { fetchGo } from '@/lib/mothership/request/go/fetch' import { mothershipRequestHeaders } from '@/lib/mothership/request/headers' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url' const logger = createLogger('ForkWorker') @@ -56,7 +56,7 @@ export async function copyWorkerConversation(request: ForkChatRequest): Promise< const canRetry = attempt === 0 let outcome: Attempt try { - const response = await fetchGo(`${baseURL}/api/chats/fork`, { + const response = await fetchWorker(`${baseURL}/api/chats/fork`, { method: 'POST', headers: mothershipRequestHeaders(), body, @@ -100,7 +100,7 @@ export async function discardWorkerConversation( ): Promise { try { const baseURL = await getMothershipBaseURL({ userId: request.userId }) - const response = await fetchGo(`${baseURL}/api/tasks/cleanup`, { + const response = await fetchWorker(`${baseURL}/api/tasks/cleanup`, { method: 'POST', headers: mothershipRequestHeaders(), body: JSON.stringify({ chatIds: [request.newChatId] }), diff --git a/apps/sim/lib/mothership/chat/persisted-message.ts b/apps/sim/lib/mothership/chat/persisted-message.ts index a079bcfe284..b0c50e4150e 100644 --- a/apps/sim/lib/mothership/chat/persisted-message.ts +++ b/apps/sim/lib/mothership/chat/persisted-message.ts @@ -617,7 +617,7 @@ interface RawBlock { name?: string subagentName?: string content?: string - /** Go persists text blocks with key "text" instead of "content" */ + /** Messages the retired Go copilot persisted carry text blocks under "text", not "content". */ text?: string channel?: string phase?: string diff --git a/apps/sim/lib/mothership/chat/post.test.ts b/apps/sim/lib/mothership/chat/post.test.ts index 2b32e7fed05..d26c4fca429 100644 --- a/apps/sim/lib/mothership/chat/post.test.ts +++ b/apps/sim/lib/mothership/chat/post.test.ts @@ -938,7 +938,7 @@ describe('handleUnifiedChatPost', () => { workspaceId: 'ws-1', orchestrateOptions: expect.objectContaining({ workflowId: 'wf-1', - goRoute: '/api/copilot', + workerRoute: '/api/copilot', executionContext: expect.objectContaining({ userId: 'user-1', workflowId: 'wf-1', @@ -1007,7 +1007,7 @@ describe('handleUnifiedChatPost', () => { workspaceId: 'ws-1', orchestrateOptions: expect.objectContaining({ workspaceId: 'ws-1', - goRoute: '/api/mothership', + workerRoute: '/api/mothership', executionContext: expect.objectContaining({ userId: 'user-1', workflowId: '', diff --git a/apps/sim/lib/mothership/chat/post.ts b/apps/sim/lib/mothership/chat/post.ts index 3d6018bf194..eca44792c33 100644 --- a/apps/sim/lib/mothership/chat/post.ts +++ b/apps/sim/lib/mothership/chat/post.ts @@ -66,10 +66,10 @@ import { ModelSelectionSchema, PROTOCOL_VERSION, } from '@/lib/mothership/generated/protocol' -import { CopilotTransport } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' import { resolveMothershipModelSettings } from '@/lib/mothership/model-options' +import { CopilotTransport } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { createBadRequestResponse, createUnauthorizedResponse } from '@/lib/mothership/request/http' import { durableResourceAuthoringVersion } from '@/lib/mothership/request/lifecycle/recovery-config' import { createSSEStream, SSE_RESPONSE_HEADERS } from '@/lib/mothership/request/lifecycle/start' @@ -255,7 +255,7 @@ type UnifiedChatBranch = selectedModel: string mode: UnifiedChatRequest['mode'] provider?: string - goRoute: '/api/copilot' + workerRoute: '/api/copilot' titleModel: string titleProvider?: string notifyChatStatus: false @@ -305,7 +305,7 @@ type UnifiedChatBranch = ) & { workspacePermission: PermissionType | null effectiveModel: string - goRoute: '/api/mothership' + workerRoute: '/api/mothership' titleModel: string titleProvider?: undefined notifyChatStatus: boolean @@ -548,7 +548,7 @@ async function resolveBranch(params: { organizationId, workspacePermission: null, effectiveModel: DEFAULT_MODEL, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', titleModel: DEFAULT_MODEL, notifyChatStatus: true, buildPayload: async (payloadParams) => @@ -595,7 +595,7 @@ async function resolveBranch(params: { selectedModel, mode: mode ?? 'agent', provider, - goRoute: '/api/copilot', + workerRoute: '/api/copilot', titleModel: selectedModel, titleProvider: provider, notifyChatStatus: false, @@ -666,7 +666,7 @@ async function resolveBranch(params: { workspaceId: requestedWorkspaceId, workspacePermission, effectiveModel: DEFAULT_MODEL, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', titleModel: DEFAULT_MODEL, notifyChatStatus: true, buildPayload: async (payloadParams) => @@ -825,9 +825,8 @@ export async function handleUnifiedChatPost(req: NextRequest) { if (otelRoot.requestId) { requestId = otelRoot.requestId } - // Identity stamp — Go already stamps `user.id` on spans from the - // validated API-key path, but Sim is the only side of the wire - // that knows the human-facing email. Stamping both on the Sim + // Identity stamp — Sim is the only side of the wire that knows the + // human-facing email. Stamping `user.id` and the email on the Sim // root (so they show up on `rootAttrs` in Tempo search) saves // the "turn user.id into a real person" round-trip to the DB // for every ad-hoc investigation. @@ -1297,7 +1296,7 @@ export async function handleUnifiedChatPost(req: NextRequest) { kind: 'interactive_stream', resourceAuthoringVersion, request: { ...requestPayload, messageId: userMessageId, chatId: actualChatId }, - goRoute: branch.goRoute, + workerRoute: branch.workerRoute, clientToolPickupExpected, userTimezone: executionContext.userTimezone, requestMode: body.mode, @@ -1335,7 +1334,7 @@ export async function handleUnifiedChatPost(req: NextRequest) { chatId: actualChatId, executionId, runId, - goRoute: branch.goRoute, + workerRoute: branch.workerRoute, autoExecuteTools: true, interactive: true, // Executor routing is decided HERE, once per turn, from the caller's declared diff --git a/apps/sim/lib/mothership/chat/terminal-state.ts b/apps/sim/lib/mothership/chat/terminal-state.ts index 891ca013608..e07e9741a7e 100644 --- a/apps/sim/lib/mothership/chat/terminal-state.ts +++ b/apps/sim/lib/mothership/chat/terminal-state.ts @@ -12,9 +12,9 @@ import { mergeAndRedactPersistedBlocks, redactSensitiveContent, } from '@/lib/mothership/chat/sim-key-redaction' -import { CopilotChatFinalizeOutcome } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { CopilotChatFinalizeOutcome } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { withCopilotSpan } from '@/lib/mothership/request/otel' import { readEvents } from '@/lib/mothership/request/session/buffer' import { isTerminalStreamStatus } from '@/lib/mothership/request/session/contract' diff --git a/apps/sim/lib/mothership/constants.ts b/apps/sim/lib/mothership/constants.ts index 0af77ea2793..a13f23acfda 100644 --- a/apps/sim/lib/mothership/constants.ts +++ b/apps/sim/lib/mothership/constants.ts @@ -23,7 +23,7 @@ export const WORKER_STREAM_IDLE_TIMEOUT_MS = 120_000 /** * Watchdog cap for a single sim-executed copilot tool. A tool that neither * resolves nor rejects within its cap is failed with a timeout error so the - * checkpoint loop can resume Go with an error result instead of wedging the + * checkpoint loop can resume the worker with an error result instead of wedging the * chat (and its pending-stream lock) behind a hung await forever. */ export const TOOL_WATCHDOG_DEFAULT_MS = 60_000 diff --git a/apps/sim/lib/mothership/generated/metrics-v1.ts b/apps/sim/lib/mothership/generated/metrics-v1.ts deleted file mode 100644 index edf31d9b3cc..00000000000 --- a/apps/sim/lib/mothership/generated/metrics-v1.ts +++ /dev/null @@ -1,67 +0,0 @@ -// GENERATED — do not edit. Source of truth: mothership worker packages/contracts/src/metrics-v1.ts -// Regenerate with `bun run contracts:sync` in the worker. - -// Canonical mothership OTel metric names Sim emits. Names only: label keys and histogram -// buckets are the emitter's. - -export const Metric = { - CopilotBillingLongContextCount: "copilot.billing.long_context.count", - CopilotCacheAttempted: "copilot.cache.attempted", - CopilotCacheHit: "copilot.cache.hit", - CopilotCacheWrite: "copilot.cache.write", - CopilotChatBlobBytes: "copilot.chat.blob.bytes", - CopilotChatBlobCount: "copilot.chat.blob.count", - CopilotDegradedCount: "copilot.degraded.count", - CopilotFileReadDuration: "copilot.file.read.duration", - CopilotFileReadSize: "copilot.file.read.size", - CopilotMessagesSerializeDuration: "copilot.messages.serialize.duration", - CopilotOptionsEmittedCount: "copilot.options.emitted.count", - CopilotPromptComponentChars: "copilot.prompt.component.chars", - CopilotRequestCount: "copilot.request.count", - CopilotRequestDuration: "copilot.request.duration", - CopilotToolCalls: "copilot.tool.calls", - CopilotToolDuration: "copilot.tool.duration", - CopilotVfsDelta: "copilot.vfs.delta", - CopilotVfsMaterializeDuration: "copilot.vfs.materialize.duration", - GenAiClientCacheTokenUsage: "gen_ai.client.cache.token.usage", - GenAiClientTokenUsage: "gen_ai.client.token.usage", - LlmClientCompactions: "llm.client.compactions", - LlmClientContextTokens: "llm.client.context_tokens", - LlmClientErrors: "llm.client.errors", - LlmClientOutputCutoff: "llm.client.output_cutoff", - LlmClientStreamDuration: "llm.client.stream.duration", - LlmClientTimeToFirstToken: "llm.client.time_to_first_token", -} as const; - -export type MetricKey = keyof typeof Metric; -export type MetricValue = (typeof Metric)[MetricKey]; - -/** Readonly sorted list of every canonical mothership metric name. */ -export const MetricValues: readonly MetricValue[] = [ - "copilot.billing.long_context.count", - "copilot.cache.attempted", - "copilot.cache.hit", - "copilot.cache.write", - "copilot.chat.blob.bytes", - "copilot.chat.blob.count", - "copilot.degraded.count", - "copilot.file.read.duration", - "copilot.file.read.size", - "copilot.messages.serialize.duration", - "copilot.options.emitted.count", - "copilot.prompt.component.chars", - "copilot.request.count", - "copilot.request.duration", - "copilot.tool.calls", - "copilot.tool.duration", - "copilot.vfs.delta", - "copilot.vfs.materialize.duration", - "gen_ai.client.cache.token.usage", - "gen_ai.client.token.usage", - "llm.client.compactions", - "llm.client.context_tokens", - "llm.client.errors", - "llm.client.output_cutoff", - "llm.client.stream.duration", - "llm.client.time_to_first_token", -] as const; diff --git a/apps/sim/lib/mothership/generated/mothership-stream-v1.ts b/apps/sim/lib/mothership/generated/mothership-stream-v1.ts index 2ce8f5b596b..9ed51b1338f 100644 --- a/apps/sim/lib/mothership/generated/mothership-stream-v1.ts +++ b/apps/sim/lib/mothership/generated/mothership-stream-v1.ts @@ -1,38 +1,65 @@ // GENERATED — do not edit. Source of truth: mothership worker packages/contracts/src/mothership-stream-v1.ts // Regenerate with `bun run contracts:sync` in the worker. -// The mothership-stream-v1 SSE envelope taxonomy Sim parses. The worker writes the shared -// StreamEnvelope shell in protocol.ts; this file is the full v1 event vocabulary Sim's -// stream readers are typed against. +// The mothership-stream-v1 SSE wire: every `data:` frame the worker emits to Sim (each leg +// ends with a literal `data: [DONE]` line), and the vocabulary Sim re-emits to the browser +// (Sim adds `session` chat/title frames of its own). +// The worker's emitter (apps/server/src/protocol/envelope.ts) builds these envelopes, so a +// frame that drifts from what Sim parses fails typecheck. Each discriminant const below is +// checked against its payload union, because Sim's reader accepts exactly the const's values. -/** - * Shared execution-oriented mothership stream contract from the worker to Sim. - */ +import type { + StreamActivityCheckpoint, + StreamTextCompletion, + StreamTextPosition, + StreamToolDeadline, + StreamToolReplay, +} from "./protocol"; +import type { ResourcePayload } from "./resources"; + +/** The shell every frame shares. */ +export interface MothershipStreamV1Envelope { + payload: TPayload; + scope?: MothershipStreamV1StreamScope; + seq: number; + stream: MothershipStreamV1StreamRef; + trace?: MothershipStreamV1Trace; + ts: string; + type: TType; + v: 1; +} + +/** Every frame on the wire, one member per payload shape. */ export type MothershipStreamV1EventEnvelope = - | MothershipStreamV1SessionStartEventEnvelope - | MothershipStreamV1SessionChatEventEnvelope - | MothershipStreamV1SessionTitleEventEnvelope - | MothershipStreamV1SessionTraceEventEnvelope - | MothershipStreamV1TextEventEnvelope - | MothershipStreamV1ToolCallEventEnvelope - | MothershipStreamV1ToolArgsDeltaEventEnvelope - | MothershipStreamV1ToolResultEventEnvelope - | MothershipStreamV1SubagentSpanStartEventEnvelope - | MothershipStreamV1SubagentSpanEndEventEnvelope - | MothershipStreamV1StructuredResultSpanEventEnvelope - | MothershipStreamV1SubagentResultSpanEventEnvelope - | MothershipStreamV1ResourceUpsertEventEnvelope - | MothershipStreamV1ResourceRemoveEventEnvelope - | MothershipStreamV1CheckpointPauseEventEnvelope - | MothershipStreamV1RunResumedEventEnvelope - | MothershipStreamV1CompactionStartEventEnvelope - | MothershipStreamV1CompactionDoneEventEnvelope - | MothershipStreamV1SteeringAppliedEventEnvelope - | MothershipStreamV1TaskArmedEventEnvelope - | MothershipStreamV1TaskDeliveredEventEnvelope - | MothershipStreamV1ErrorEventEnvelope - | MothershipStreamV1CompleteEventEnvelope; + | MothershipStreamV1Envelope<"session", MothershipStreamV1SessionStartPayload> + | MothershipStreamV1Envelope<"session", MothershipStreamV1SessionChatPayload> + | MothershipStreamV1Envelope<"session", MothershipStreamV1SessionTitlePayload> + | MothershipStreamV1Envelope<"text", MothershipStreamV1TextPayload> + | MothershipStreamV1Envelope<"tool", MothershipStreamV1ToolCallDescriptor> + | MothershipStreamV1Envelope<"tool", MothershipStreamV1ToolArgsDeltaPayload> + | MothershipStreamV1Envelope<"tool", MothershipStreamV1ToolResultPayload> + | MothershipStreamV1Envelope<"span", MothershipStreamV1SubagentSpanPayload> + | MothershipStreamV1Envelope<"resource", ResourcePayload> + | MothershipStreamV1Envelope<"run", MothershipStreamV1CheckpointPausePayload> + | MothershipStreamV1Envelope<"run", MothershipStreamV1CompactionStartPayload> + | MothershipStreamV1Envelope<"run", MothershipStreamV1CompactionDonePayload> + | MothershipStreamV1Envelope<"run", MothershipStreamV1SteeringAppliedPayload> + | MothershipStreamV1Envelope<"run", MothershipStreamV1TaskArmedPayload> + | MothershipStreamV1Envelope<"run", MothershipStreamV1TaskDeliveredPayload> + | MothershipStreamV1Envelope<"error", MothershipStreamV1ErrorPayload> + | MothershipStreamV1Envelope<"complete", MothershipStreamV1CompletePayload>; + +/** The payload a frame of `type` carries. */ +type PayloadOf = Extract< + MothershipStreamV1EventEnvelope, + { type: TType } +>["payload"]; + export type MothershipStreamV1TextChannel = "assistant" | "thinking"; +/** + * Where a tool call executes. `go` means the worker executes it: the name is a persisted + * historical value (stored chat blocks and self-hosted Sims read it), so it stays on the wire. + */ export type MothershipStreamV1ToolExecutor = "go" | "sim" | "client"; export type MothershipStreamV1ToolMode = "sync" | "async"; export type MothershipStreamV1ToolStatus = @@ -46,23 +73,6 @@ export type MothershipStreamV1ToolStatus = | "rejected"; export type MothershipStreamV1CompletionStatus = "complete" | "error" | "cancelled"; -export interface MothershipStreamV1SessionStartEventEnvelope { - payload: MothershipStreamV1SessionStartPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "session"; - v: 1; -} -export interface MothershipStreamV1SessionStartPayload { - data?: MothershipStreamV1SessionStartData; - kind: "start"; -} -export interface MothershipStreamV1SessionStartData { - responseId?: string; -} export interface MothershipStreamV1StreamScope { agentId?: string; lane: "subagent"; @@ -76,83 +86,36 @@ export interface MothershipStreamV1StreamRef { streamId: string; } export interface MothershipStreamV1Trace { - /** - * OTel trace ID from the first worker ingress. May differ from requestId when Sim assigns the canonical request identity. - */ - goTraceId?: string; requestId: string; spanId?: string; } -export interface MothershipStreamV1SessionChatEventEnvelope { - payload: MothershipStreamV1SessionChatPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "session"; - v: 1; + +export interface MothershipStreamV1SessionStartPayload { + data?: MothershipStreamV1SessionStartData; + kind: "start"; } +export interface MothershipStreamV1SessionStartData { + responseId?: string; +} +/** Emitted by Sim, not the worker. */ export interface MothershipStreamV1SessionChatPayload { chatId: string; kind: "chat"; } -export interface MothershipStreamV1SessionTitleEventEnvelope { - payload: MothershipStreamV1SessionTitlePayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "session"; - v: 1; -} +/** Emitted by Sim, not the worker. */ export interface MothershipStreamV1SessionTitlePayload { kind: "title"; title: string; } -export interface MothershipStreamV1SessionTraceEventEnvelope { - payload: MothershipStreamV1SessionTracePayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "session"; - v: 1; -} -export interface MothershipStreamV1SessionTracePayload { - kind: "trace"; - requestId: string; - spanId?: string; -} -export interface MothershipStreamV1TextEventEnvelope { - payload: MothershipStreamV1TextPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "text"; - v: 1; -} -export interface MothershipStreamV1TextPayload { + +export interface MothershipStreamV1TextPayload extends StreamTextPosition { channel: MothershipStreamV1TextChannel; text: string; } -export interface MothershipStreamV1ToolCallEventEnvelope { - payload: MothershipStreamV1ToolCallDescriptor; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "tool"; - v: 1; -} -export interface MothershipStreamV1ToolCallDescriptor { + +export interface MothershipStreamV1ToolCallDescriptor extends StreamToolReplay, StreamToolDeadline { activityDescription?: string; - arguments?: MothershipStreamV1AdditionalPropertiesMap; + arguments?: Record; execName?: string; executor: MothershipStreamV1ToolExecutor; mode: MothershipStreamV1ToolMode; @@ -163,9 +126,6 @@ export interface MothershipStreamV1ToolCallDescriptor { toolName: string; ui?: MothershipStreamV1ToolUI; } -export interface MothershipStreamV1AdditionalPropertiesMap { - [k: string]: unknown; -} export interface MothershipStreamV1ToolUI { clientExecutable?: boolean; hidden?: boolean; @@ -173,35 +133,14 @@ export interface MothershipStreamV1ToolUI { internal?: boolean; simExecutable?: boolean; } -export interface MothershipStreamV1ToolArgsDeltaEventEnvelope { - payload: MothershipStreamV1ToolArgsDeltaPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "tool"; - v: 1; -} +/** Draw-only argument text as it generates; never replayed, never dispatched on. */ export interface MothershipStreamV1ToolArgsDeltaPayload { argumentsDelta: string; - executor: MothershipStreamV1ToolExecutor; - mode: MothershipStreamV1ToolMode; phase: "args_delta"; toolCallId: string; toolName: string; } -export interface MothershipStreamV1ToolResultEventEnvelope { - payload: MothershipStreamV1ToolResultPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "tool"; - v: 1; -} -export interface MothershipStreamV1ToolResultPayload { +export interface MothershipStreamV1ToolResultPayload extends StreamToolReplay { error?: string; executor: MothershipStreamV1ToolExecutor; mode: MothershipStreamV1ToolMode; @@ -212,199 +151,41 @@ export interface MothershipStreamV1ToolResultPayload { toolCallId: string; toolName: string; } -export interface MothershipStreamV1SubagentSpanStartEventEnvelope { - payload: MothershipStreamV1SubagentSpanStartPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "span"; - v: 1; -} -export interface MothershipStreamV1SubagentSpanStartPayload { - agent?: string; - data?: unknown; - event: "start"; - kind: "subagent"; -} -export interface MothershipStreamV1SubagentSpanEndEventEnvelope { - payload: MothershipStreamV1SubagentSpanEndPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "span"; - v: 1; + +/** A subagent lane's display title (on start) or failure (on end). */ +export interface MothershipStreamV1SubagentSpanData { + name?: string; + error?: string; } -export interface MothershipStreamV1SubagentSpanEndPayload { + +export interface MothershipStreamV1SubagentSpanPayload { agent?: string; - data?: unknown; - event: "end"; + data?: MothershipStreamV1SubagentSpanData; + event: "start" | "end"; kind: "subagent"; } -export interface MothershipStreamV1StructuredResultSpanEventEnvelope { - payload: MothershipStreamV1StructuredResultSpanPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "span"; - v: 1; -} -export interface MothershipStreamV1StructuredResultSpanPayload { - agent?: string; - data?: unknown; - kind: "structured_result"; -} -export interface MothershipStreamV1SubagentResultSpanEventEnvelope { - payload: MothershipStreamV1SubagentResultSpanPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "span"; - v: 1; -} -export interface MothershipStreamV1SubagentResultSpanPayload { - agent?: string; - data?: unknown; - kind: "subagent_result"; -} -export interface MothershipStreamV1ResourceUpsertEventEnvelope { - payload: MothershipStreamV1ResourceUpsertPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "resource"; - v: 1; -} -export interface MothershipStreamV1ResourceUpsertPayload { - op: "upsert"; - resource: MothershipStreamV1ResourceDescriptor; -} -export interface MothershipStreamV1ResourceDescriptor { - clearViewId?: boolean; - id: string; - title?: string; - type: string; - viewId?: string; -} -export interface MothershipStreamV1ResourceRemoveEventEnvelope { - payload: MothershipStreamV1ResourceRemovePayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "resource"; - v: 1; -} -export interface MothershipStreamV1ResourceRemovePayload { - op: "remove"; - resource: MothershipStreamV1ResourceDescriptor; -} -export interface MothershipStreamV1CheckpointPauseEventEnvelope { - payload: MothershipStreamV1CheckpointPausePayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "run"; - v: 1; -} -export interface MothershipStreamV1CheckpointPausePayload { + +export interface MothershipStreamV1CheckpointPausePayload extends StreamActivityCheckpoint { checkpointId: string; executionId: string; - frames?: MothershipStreamV1CheckpointPauseFrame[]; kind: "checkpoint_pause"; pendingToolCallIds: string[]; runId: string; } -export interface MothershipStreamV1CheckpointPauseFrame { - checkpointId?: string; - parentToolCallId: string; - parentToolName: string; - pendingToolIds: string[]; -} -export interface MothershipStreamV1RunResumedEventEnvelope { - payload: MothershipStreamV1RunResumedPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "run"; - v: 1; -} -export interface MothershipStreamV1RunResumedPayload { - kind: "resumed"; -} -export interface MothershipStreamV1CompactionStartEventEnvelope { - payload: MothershipStreamV1CompactionStartPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "run"; - v: 1; -} export interface MothershipStreamV1CompactionStartPayload { kind: "compaction_start"; } -export interface MothershipStreamV1CompactionDoneEventEnvelope { - payload: MothershipStreamV1CompactionDonePayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "run"; - v: 1; -} export interface MothershipStreamV1CompactionDonePayload { - data?: MothershipStreamV1CompactionDoneData; kind: "compaction_done"; } -export interface MothershipStreamV1CompactionDoneData { - summary_chars: number; -} -export interface MothershipStreamV1SteeringAppliedEventEnvelope { - payload: MothershipStreamV1SteeringAppliedPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "run"; - v: 1; -} +/** A steer reached the run's model input (the loss-free ack for POST /api/streams/steer). */ export interface MothershipStreamV1SteeringAppliedPayload { - content?: string; + /** As in SteerV2Response's `applied`; absent only on frames from workers without steer v2. */ eventSeq?: number; kind: "steering_applied"; - messageId?: string; - mode?: "deferred" | "interrupt"; steeringId: string; textOffset?: number; } -export interface MothershipStreamV1TaskArmedEventEnvelope { - payload: MothershipStreamV1TaskArmedPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "run"; - v: 1; -} export interface MothershipStreamV1TaskArmedPayload { kind: "task_armed"; note: string; @@ -412,51 +193,25 @@ export interface MothershipStreamV1TaskArmedPayload { taskId: string; taskKind: "timer" | "workflow_run"; } -export interface MothershipStreamV1TaskDeliveredEventEnvelope { - payload: MothershipStreamV1TaskDeliveredPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "run"; - v: 1; -} export interface MothershipStreamV1TaskDeliveredPayload { kind: "task_delivered"; status: "completed" | "failed" | "stopped" | "expired"; summary: string; taskId: string; } -export interface MothershipStreamV1ErrorEventEnvelope { - payload: MothershipStreamV1ErrorPayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "error"; - v: 1; -} + export interface MothershipStreamV1ErrorPayload { code?: string; + /** Sim-emitted detail (e.g. the replay-gap bounds); the worker sends none. */ data?: unknown; displayMessage?: string; + /** Sim-emitted alias of `message`; the worker sends none. */ error?: string; message: string; provider?: string; } -export interface MothershipStreamV1CompleteEventEnvelope { - payload: MothershipStreamV1CompletePayload; - scope?: MothershipStreamV1StreamScope; - seq: number; - stream: MothershipStreamV1StreamRef; - trace?: MothershipStreamV1Trace; - ts: string; - type: "complete"; - v: 1; -} -export interface MothershipStreamV1CompletePayload { + +export interface MothershipStreamV1CompletePayload extends StreamTextCompletion, StreamActivityCheckpoint { cost?: MothershipStreamV1CostData; reason?: string; response?: unknown; @@ -477,38 +232,7 @@ export interface MothershipStreamV1UsageData { total_tokens?: number; } -export type MothershipStreamV1AsyncToolRecordStatus = - | "pending" - | "running" - | "completed" - | "failed" - | "cancelled" - | "delivered"; - -export const MothershipStreamV1AsyncToolRecordStatus = { - pending: "pending", - running: "running", - completed: "completed", - failed: "failed", - cancelled: "cancelled", - delivered: "delivered", -} as const; - -export const MothershipStreamV1CompletionStatus = { - complete: "complete", - error: "error", - cancelled: "cancelled", -} as const; - -export type MothershipStreamV1EventType = - | "session" - | "text" - | "tool" - | "span" - | "resource" - | "run" - | "error" - | "complete"; +export type MothershipStreamV1EventType = MothershipStreamV1EventEnvelope["type"]; export const MothershipStreamV1EventType = { session: "session", @@ -519,105 +243,100 @@ export const MothershipStreamV1EventType = { run: "run", error: "error", complete: "complete", -} as const; - -export type MothershipStreamV1ResourceOp = "upsert" | "remove"; - -export const MothershipStreamV1ResourceOp = { - upsert: "upsert", - remove: "remove", -} as const; +} as const satisfies { [K in MothershipStreamV1EventType]: K }; -export type MothershipStreamV1RunKind = - | "checkpoint_pause" - | "resumed" - | "compaction_start" - | "compaction_done" - | "steering_applied" - | "task_armed" - | "task_delivered"; - -export const MothershipStreamV1RunKind = { - checkpoint_pause: "checkpoint_pause", - resumed: "resumed", - compaction_start: "compaction_start", - compaction_done: "compaction_done", - steering_applied: "steering_applied", - task_armed: "task_armed", - task_delivered: "task_delivered", -} as const; - -export type MothershipStreamV1SessionKind = "trace" | "chat" | "title" | "start"; +export type MothershipStreamV1SessionKind = PayloadOf<"session">["kind"]; export const MothershipStreamV1SessionKind = { - trace: "trace", chat: "chat", title: "title", start: "start", -} as const; - -export type MothershipStreamV1SpanKind = "subagent"; - -export const MothershipStreamV1SpanKind = { - subagent: "subagent", -} as const; - -export type MothershipStreamV1SpanLifecycleEvent = "start" | "end"; - -export const MothershipStreamV1SpanLifecycleEvent = { - start: "start", - end: "end", -} as const; - -export type MothershipStreamV1SpanPayloadKind = "subagent" | "structured_result" | "subagent_result"; - -export const MothershipStreamV1SpanPayloadKind = { - subagent: "subagent", - structured_result: "structured_result", - subagent_result: "subagent_result", -} as const; +} as const satisfies { [K in MothershipStreamV1SessionKind]: K }; export const MothershipStreamV1TextChannel = { assistant: "assistant", thinking: "thinking", -} as const; +} as const satisfies { [K in MothershipStreamV1TextChannel]: K }; + +export type MothershipStreamV1ToolPhase = PayloadOf<"tool">["phase"]; + +export const MothershipStreamV1ToolPhase = { + call: "call", + args_delta: "args_delta", + result: "result", +} as const satisfies { [K in MothershipStreamV1ToolPhase]: K }; export const MothershipStreamV1ToolExecutor = { go: "go", sim: "sim", client: "client", -} as const; +} as const satisfies { [K in MothershipStreamV1ToolExecutor]: K }; export const MothershipStreamV1ToolMode = { sync: "sync", async: "async", -} as const; - -export type MothershipStreamV1ToolOutcome = "success" | "error" | "cancelled" | "skipped" | "rejected"; +} as const satisfies { [K in MothershipStreamV1ToolMode]: K }; -export const MothershipStreamV1ToolOutcome = { +export const MothershipStreamV1ToolStatus = { + generating: "generating", + awaiting_approval: "awaiting_approval", + executing: "executing", success: "success", error: "error", cancelled: "cancelled", skipped: "skipped", rejected: "rejected", -} as const; +} as const satisfies { [K in MothershipStreamV1ToolStatus]: K }; -export type MothershipStreamV1ToolPhase = "call" | "args_delta" | "result"; +export type MothershipStreamV1SpanPayloadKind = PayloadOf<"span">["kind"]; -export const MothershipStreamV1ToolPhase = { - call: "call", - args_delta: "args_delta", - result: "result", -} as const; +export const MothershipStreamV1SpanPayloadKind = { + subagent: "subagent", +} as const satisfies { [K in MothershipStreamV1SpanPayloadKind]: K }; -export const MothershipStreamV1ToolStatus = { - generating: "generating", - awaiting_approval: "awaiting_approval", - executing: "executing", +export type MothershipStreamV1SpanLifecycleEvent = PayloadOf<"span">["event"]; + +export const MothershipStreamV1SpanLifecycleEvent = { + start: "start", + end: "end", +} as const satisfies { [K in MothershipStreamV1SpanLifecycleEvent]: K }; + +export type MothershipStreamV1ResourceOp = ResourcePayload["op"]; + +export const MothershipStreamV1ResourceOp = { + upsert: "upsert", + remove: "remove", + clear_view: "clear_view", + refresh: "refresh", +} as const satisfies { [K in MothershipStreamV1ResourceOp]: K }; + +export type MothershipStreamV1RunKind = PayloadOf<"run">["kind"]; + +export const MothershipStreamV1RunKind = { + checkpoint_pause: "checkpoint_pause", + compaction_start: "compaction_start", + compaction_done: "compaction_done", + steering_applied: "steering_applied", + task_armed: "task_armed", + task_delivered: "task_delivered", +} as const satisfies { [K in MothershipStreamV1RunKind]: K }; + +export const MothershipStreamV1CompletionStatus = { + complete: "complete", + error: "error", + cancelled: "cancelled", +} as const satisfies { [K in MothershipStreamV1CompletionStatus]: K }; + +/** The terminal outcomes Sim reports for a tool call it executed (a subset of the tool status). */ +export type MothershipStreamV1ToolOutcome = Extract< + MothershipStreamV1ToolStatus, + "success" | "error" | "cancelled" | "skipped" | "rejected" +>; + +export const MothershipStreamV1ToolOutcome = { success: "success", error: "error", cancelled: "cancelled", skipped: "skipped", rejected: "rejected", -} as const; +} as const satisfies { [K in MothershipStreamV1ToolOutcome]: K }; diff --git a/apps/sim/lib/mothership/generated/protocol.ts b/apps/sim/lib/mothership/generated/protocol.ts index bdb63bbcc86..9c4d1f00e1b 100644 --- a/apps/sim/lib/mothership/generated/protocol.ts +++ b/apps/sim/lib/mothership/generated/protocol.ts @@ -596,15 +596,6 @@ export const SteerDispositionResponse = z.discriminatedUnion("status", [ ]); export type SteerDispositionResponse = z.infer; -/** The `run`/`steering_applied` frame: a steer reached the run's model input. */ -export interface StreamSteeringApplied { - kind: "steering_applied"; - steeringId: string; - /** As in SteerV2Response's `applied`; absent only on frames from workers without steer v2. */ - eventSeq?: number | undefined; - textOffset?: number | undefined; -} - /** POST /api/chats/fork — copy a selected conversation snapshot, never live execution. */ export const ForkChatRequest = z .strictObject({ @@ -696,31 +687,3 @@ export interface ExecuteMessage { role: "system" | "user" | "assistant"; content: string; } - -/** - * The response half of the wire: every SSE `data:` line is one StreamEnvelope (the - * mothership-stream-v1 shape), terminated by a literal `data: [DONE]` line per leg. The - * worker's emitter is compile-locked to this; sim's parser adopts it at the client rework. - */ -export interface StreamEnvelope { - v: 1; - type: "session" | "text" | "tool" | "span" | "run" | "resource" | "error" | "complete"; - seq: number; - /** ISO timestamp. */ - ts: string; - stream: { streamId: string; chatId?: string | undefined; cursor?: string | undefined }; - trace?: { requestId?: string | undefined } | undefined; - /** Subagent-lane attribution (mothership-stream-v1 scope): frames carrying it render - * inside the named root-level lane instead of the main transcript. */ - scope?: StreamScope | undefined; - payload: Record; -} - -/** One subagent lane: keyed by the delegating tool call; agentId/spanId identify the lane. */ -export interface StreamScope { - lane: "subagent"; - agentId?: string | undefined; - parentToolCallId?: string | undefined; - spanId?: string | undefined; - parentSpanId?: string | undefined; -} diff --git a/apps/sim/lib/mothership/generated/request-trace-v1.ts b/apps/sim/lib/mothership/generated/request-trace-v1.ts deleted file mode 100644 index 80ebb12dadc..00000000000 --- a/apps/sim/lib/mothership/generated/request-trace-v1.ts +++ /dev/null @@ -1,139 +0,0 @@ -// AUTO-GENERATED FILE. DO NOT EDIT. -// - -/** - * This interface was referenced by `RequestTraceV1SimReport`'s JSON-Schema - * via the `definition` "RequestTraceV1Outcome". - */ -export type RequestTraceV1Outcome = 'success' | 'error' | 'cancelled' -/** - * This interface was referenced by `RequestTraceV1SimReport`'s JSON-Schema - * via the `definition` "RequestTraceV1SpanSource". - */ -export type RequestTraceV1SpanSource = 'sim' | 'go' -/** - * This interface was referenced by `RequestTraceV1SimReport`'s JSON-Schema - * via the `definition` "RequestTraceV1SpanStatus". - */ -export type RequestTraceV1SpanStatus = 'ok' | 'error' | 'cancelled' | 'pending' - -/** - * Trace report sent from Sim to Go after a request completes. - */ -export interface RequestTraceV1SimReport { - chatId?: string - cost?: RequestTraceV1CostSummary - durationMs: number - endMs: number - executionId?: string - goTraceId?: string - outcome: RequestTraceV1Outcome - runId?: string - simRequestId: string - spans: RequestTraceV1Span[] - startMs: number - streamId?: string - usage?: RequestTraceV1UsageSummary - userMessage?: string -} -/** - * This interface was referenced by `RequestTraceV1SimReport`'s JSON-Schema - * via the `definition` "RequestTraceV1CostSummary". - */ -export interface RequestTraceV1CostSummary { - billedTotalCost?: number - rawTotalCost?: number -} -/** - * This interface was referenced by `RequestTraceV1SimReport`'s JSON-Schema - * via the `definition` "RequestTraceV1Span". - */ -export interface RequestTraceV1Span { - attributes?: MothershipStreamV1AdditionalPropertiesMap - durationMs: number - endMs: number - kind?: string - name: string - parentName?: string - source?: RequestTraceV1SpanSource - startMs: number - status: RequestTraceV1SpanStatus -} -/** - * This interface was referenced by `RequestTraceV1SimReport`'s JSON-Schema - * via the `definition` "MothershipStreamV1AdditionalPropertiesMap". - */ -export interface MothershipStreamV1AdditionalPropertiesMap { - [k: string]: unknown -} -/** - * This interface was referenced by `RequestTraceV1SimReport`'s JSON-Schema - * via the `definition` "RequestTraceV1UsageSummary". - */ -export interface RequestTraceV1UsageSummary { - cacheAttemptedRequests?: number - cacheHitRequests?: number - cacheReadTokens?: number - cacheSavingsRate?: number - cacheWriteRequests?: number - cacheWriteTokens?: number - inputTokens?: number - outputTokens?: number -} -/** - * This interface was referenced by `RequestTraceV1SimReport`'s JSON-Schema - * via the `definition` "RequestTraceV1MergedTrace". - */ -export interface RequestTraceV1MergedTrace { - chatId?: string - cost?: RequestTraceV1CostSummary - durationMs: number - endMs: number - goTraceId: string - outcome: RequestTraceV1Outcome - serviceCharges?: MothershipStreamV1AdditionalPropertiesMap - simRequestId?: string - spans: RequestTraceV1Span[] - startMs: number - streamId?: string - usage?: RequestTraceV1UsageSummary - userId?: string -} -/** - * This interface was referenced by `RequestTraceV1SimReport`'s JSON-Schema - * via the `definition` "RequestTraceV1SimReport". - */ -export interface RequestTraceV1SimReport1 { - chatId?: string - cost?: RequestTraceV1CostSummary - durationMs: number - endMs: number - executionId?: string - goTraceId?: string - outcome: RequestTraceV1Outcome - runId?: string - simRequestId: string - spans: RequestTraceV1Span[] - startMs: number - streamId?: string - usage?: RequestTraceV1UsageSummary - userMessage?: string -} - -export const RequestTraceV1Outcome = { - success: 'success', - error: 'error', - cancelled: 'cancelled', -} as const - -export const RequestTraceV1SpanSource = { - sim: 'sim', - go: 'go', -} as const - -export const RequestTraceV1SpanStatus = { - ok: 'ok', - error: 'error', - cancelled: 'cancelled', - pending: 'pending', -} as const diff --git a/apps/sim/lib/mothership/generated/trace-attribute-values-v1.ts b/apps/sim/lib/mothership/generated/trace-attribute-values-v1.ts deleted file mode 100644 index e873df7d6e7..00000000000 --- a/apps/sim/lib/mothership/generated/trace-attribute-values-v1.ts +++ /dev/null @@ -1,447 +0,0 @@ -// GENERATED — do not edit. Source of truth: mothership worker packages/contracts/src/trace-attribute-values-v1.ts -// Regenerate with `bun run contracts:sync` in the worker. - -// Closed-set value vocabularies for the attributes in trace-attributes-v1.ts. Call sites -// reference e.g. `CopilotRequestCancelReason.ExplicitStop` rather than raw strings. - -export const AbortBackend = { - InProcess: "in_process", - Redis: "redis", -} as const; - -export type AbortBackendKey = keyof typeof AbortBackend; -export type AbortBackendValue = (typeof AbortBackend)[AbortBackendKey]; - -export const AbortRedisResult = { - Error: "error", - Miss: "miss", - Ok: "ok", - Slow: "slow", -} as const; - -export type AbortRedisResultKey = keyof typeof AbortRedisResult; -export type AbortRedisResultValue = (typeof AbortRedisResult)[AbortRedisResultKey]; - -export const AuthKeyMatch = { - None: "none", - User: "user", -} as const; - -export type AuthKeyMatchKey = keyof typeof AuthKeyMatch; -export type AuthKeyMatchValue = (typeof AuthKeyMatch)[AuthKeyMatchKey]; - -export const BillingFlushOutcome = { - CheckpointAlreadyClaimed: "checkpoint_already_claimed", - CheckpointLoadFailed: "checkpoint_load_failed", - Flushed: "flushed", - NoCheckpoint: "no_checkpoint", - NoSnapshot: "no_snapshot", - SkippedUnconfigured: "skipped_unconfigured", -} as const; - -export type BillingFlushOutcomeKey = keyof typeof BillingFlushOutcome; -export type BillingFlushOutcomeValue = (typeof BillingFlushOutcome)[BillingFlushOutcomeKey]; - -export const BillingRouteOutcome = { - AuthFailed: "auth_failed", - Billed: "billed", - BillingDisabled: "billing_disabled", - DuplicateIdempotencyKey: "duplicate_idempotency_key", - InternalError: "internal_error", - InvalidBody: "invalid_body", - ReconciliationRequired: "reconciliation_required", -} as const; - -export type BillingRouteOutcomeKey = keyof typeof BillingRouteOutcome; -export type BillingRouteOutcomeValue = (typeof BillingRouteOutcome)[BillingRouteOutcomeKey]; - -export const ContextBudgetSource = { - Model: "model", - PricingBoundary: "pricing_boundary", -} as const; - -export type ContextBudgetSourceKey = keyof typeof ContextBudgetSource; -export type ContextBudgetSourceValue = (typeof ContextBudgetSource)[ContextBudgetSourceKey]; - -export const CopilotAbortOutcome = { - BadRequest: "bad_request", - FallbackPersistFailed: "fallback_persist_failed", - ForceReleased: "force_released", - MissingMessageId: "missing_message_id", - MissingStreamId: "missing_stream_id", - NoChatId: "no_chat_id", - Ok: "ok", - SettleTimeout: "settle_timeout", - Settled: "settled", - Unauthorized: "unauthorized", -} as const; - -export type CopilotAbortOutcomeKey = keyof typeof CopilotAbortOutcome; -export type CopilotAbortOutcomeValue = (typeof CopilotAbortOutcome)[CopilotAbortOutcomeKey]; - -export const CopilotBranchKind = { - Workflow: "workflow", - Workspace: "workspace", -} as const; - -export type CopilotBranchKindKey = keyof typeof CopilotBranchKind; -export type CopilotBranchKindValue = (typeof CopilotBranchKind)[CopilotBranchKindKey]; - -export const CopilotChatFinalizeOutcome = { - AppendedAssistant: "appended_assistant", - AssistantAlreadyPersisted: "assistant_already_persisted", - ClearedStreamMarkerOnly: "cleared_stream_marker_only", - StaleUserMessage: "stale_user_message", -} as const; - -export type CopilotChatFinalizeOutcomeKey = keyof typeof CopilotChatFinalizeOutcome; -export type CopilotChatFinalizeOutcomeValue = - (typeof CopilotChatFinalizeOutcome)[CopilotChatFinalizeOutcomeKey]; - -export const CopilotChatPersistOutcome = { - Appended: "appended", - ChatNotFound: "chat_not_found", -} as const; - -export type CopilotChatPersistOutcomeKey = keyof typeof CopilotChatPersistOutcome; -export type CopilotChatPersistOutcomeValue = (typeof CopilotChatPersistOutcome)[CopilotChatPersistOutcomeKey]; - -export const CopilotConfirmOutcome = { - Delivered: "delivered", - Forbidden: "forbidden", - InternalError: "internal_error", - RunNotFound: "run_not_found", - ToolCallNotFound: "tool_call_not_found", - Unauthorized: "unauthorized", - UpdateFailed: "update_failed", - ValidationError: "validation_error", -} as const; - -export type CopilotConfirmOutcomeKey = keyof typeof CopilotConfirmOutcome; -export type CopilotConfirmOutcomeValue = (typeof CopilotConfirmOutcome)[CopilotConfirmOutcomeKey]; - -export const CopilotDegradedReason = { - BindingRejected: "binding_rejected", - ClientPickupTimeout: "client_pickup_timeout", - MissingToolResult: "missing_tool_result", - StreamDeadBeforeDispatch: "stream_dead_before_dispatch", -} as const; - -export type CopilotDegradedReasonKey = keyof typeof CopilotDegradedReason; -export type CopilotDegradedReasonValue = (typeof CopilotDegradedReason)[CopilotDegradedReasonKey]; - -export const CopilotFinalizeOutcome = { - Aborted: "aborted", - Error: "error", - Success: "success", -} as const; - -export type CopilotFinalizeOutcomeKey = keyof typeof CopilotFinalizeOutcome; -export type CopilotFinalizeOutcomeValue = (typeof CopilotFinalizeOutcome)[CopilotFinalizeOutcomeKey]; - -export const CopilotLeg = { - SimToGo: "sim_to_go", -} as const; - -export type CopilotLegKey = keyof typeof CopilotLeg; -export type CopilotLegValue = (typeof CopilotLeg)[CopilotLegKey]; - -export const CopilotOutputFileOutcome = { - Failed: "failed", - Uploaded: "uploaded", -} as const; - -export type CopilotOutputFileOutcomeKey = keyof typeof CopilotOutputFileOutcome; -export type CopilotOutputFileOutcomeValue = (typeof CopilotOutputFileOutcome)[CopilotOutputFileOutcomeKey]; - -export const CopilotRecoveryOutcome = { - GapDetected: "gap_detected", - InRange: "in_range", -} as const; - -export type CopilotRecoveryOutcomeKey = keyof typeof CopilotRecoveryOutcome; -export type CopilotRecoveryOutcomeValue = (typeof CopilotRecoveryOutcome)[CopilotRecoveryOutcomeKey]; - -export const CopilotRequestCancelReason = { - ClientDisconnect: "client_disconnect", - ExplicitStop: "explicit_stop", - Timeout: "timeout", - Unknown: "unknown", -} as const; - -export type CopilotRequestCancelReasonKey = keyof typeof CopilotRequestCancelReason; -export type CopilotRequestCancelReasonValue = - (typeof CopilotRequestCancelReason)[CopilotRequestCancelReasonKey]; - -export const CopilotResourcesOp = { - Delete: "delete", - None: "none", - Upsert: "upsert", -} as const; - -export type CopilotResourcesOpKey = keyof typeof CopilotResourcesOp; -export type CopilotResourcesOpValue = (typeof CopilotResourcesOp)[CopilotResourcesOpKey]; - -export const CopilotResumeOutcome = { - BatchDelivered: "batch_delivered", - ClientDisconnected: "client_disconnected", - EndedWithoutTerminal: "ended_without_terminal", - StreamNotFound: "stream_not_found", - TerminalDelivered: "terminal_delivered", -} as const; - -export type CopilotResumeOutcomeKey = keyof typeof CopilotResumeOutcome; -export type CopilotResumeOutcomeValue = (typeof CopilotResumeOutcome)[CopilotResumeOutcomeKey]; - -export const CopilotSseCloseReason = { - Aborted: "aborted", - BackendError: "backend_error", - BillingLimit: "billing_limit", - ClosedNoTerminal: "closed_no_terminal", - Error: "error", - Terminal: "terminal", - Timeout: "timeout", -} as const; - -export type CopilotSseCloseReasonKey = keyof typeof CopilotSseCloseReason; -export type CopilotSseCloseReasonValue = (typeof CopilotSseCloseReason)[CopilotSseCloseReasonKey]; - -export const CopilotSteerOutcome = { - BadRequest: "bad_request", - MissingContent: "missing_content", - MissingMessageId: "missing_message_id", - NoActiveTurn: "no_active_turn", - QueueFull: "queue_full", - Queued: "queued", -} as const; - -export type CopilotSteerOutcomeKey = keyof typeof CopilotSteerOutcome; -export type CopilotSteerOutcomeValue = (typeof CopilotSteerOutcome)[CopilotSteerOutcomeKey]; - -export const CopilotStopOutcome = { - ChatNotFound: "chat_not_found", - InternalError: "internal_error", - NoMatchingRow: "no_matching_row", - Persisted: "persisted", - Unauthorized: "unauthorized", - ValidationError: "validation_error", -} as const; - -export type CopilotStopOutcomeKey = keyof typeof CopilotStopOutcome; -export type CopilotStopOutcomeValue = (typeof CopilotStopOutcome)[CopilotStopOutcomeKey]; - -export const CopilotSurface = { - Copilot: "copilot", - Mothership: "mothership", -} as const; - -export type CopilotSurfaceKey = keyof typeof CopilotSurface; -export type CopilotSurfaceValue = (typeof CopilotSurface)[CopilotSurfaceKey]; - -export const CopilotTableOutcome = { - EmptyContent: "empty_content", - EmptyRows: "empty_rows", - Failed: "failed", - Imported: "imported", - InvalidJsonShape: "invalid_json_shape", - InvalidShape: "invalid_shape", - RowLimitExceeded: "row_limit_exceeded", - TableNotFound: "table_not_found", - Wrote: "wrote", -} as const; - -export type CopilotTableOutcomeKey = keyof typeof CopilotTableOutcome; -export type CopilotTableOutcomeValue = (typeof CopilotTableOutcome)[CopilotTableOutcomeKey]; - -export const CopilotTableSourceFormat = { - Csv: "csv", - Json: "json", -} as const; - -export type CopilotTableSourceFormatKey = keyof typeof CopilotTableSourceFormat; -export type CopilotTableSourceFormatValue = (typeof CopilotTableSourceFormat)[CopilotTableSourceFormatKey]; - -export const CopilotTransport = { - Batch: "batch", - Headless: "headless", - Stream: "stream", -} as const; - -export type CopilotTransportKey = keyof typeof CopilotTransport; -export type CopilotTransportValue = (typeof CopilotTransport)[CopilotTransportKey]; - -export const CopilotValidateOutcome = { - InternalAuthFailed: "internal_auth_failed", - InternalError: "internal_error", - InvalidBody: "invalid_body", - Ok: "ok", - UsageExceeded: "usage_exceeded", - UserNotFound: "user_not_found", -} as const; - -export type CopilotValidateOutcomeKey = keyof typeof CopilotValidateOutcome; -export type CopilotValidateOutcomeValue = (typeof CopilotValidateOutcome)[CopilotValidateOutcomeKey]; - -export const CopilotVfsOutcome = { - PassthroughFitsBudget: "passthrough_fits_budget", - PassthroughNoMetadata: "passthrough_no_metadata", - PassthroughNoSharp: "passthrough_no_sharp", - RejectedNoMetadata: "rejected_no_metadata", - RejectedNoSharp: "rejected_no_sharp", - RejectedTooLargeAfterResize: "rejected_too_large_after_resize", - Resized: "resized", -} as const; - -export type CopilotVfsOutcomeKey = keyof typeof CopilotVfsOutcome; -export type CopilotVfsOutcomeValue = (typeof CopilotVfsOutcome)[CopilotVfsOutcomeKey]; - -export const CopilotVfsReadOutcome = { - BinaryPlaceholder: "binary_placeholder", - DocumentParsed: "document_parsed", - DocumentTooLarge: "document_too_large", - ImagePrepared: "image_prepared", - ImageTooLarge: "image_too_large", - ParseFailed: "parse_failed", - ReadFailed: "read_failed", - TextRead: "text_read", - TextTooLarge: "text_too_large", -} as const; - -export type CopilotVfsReadOutcomeKey = keyof typeof CopilotVfsReadOutcome; -export type CopilotVfsReadOutcomeValue = (typeof CopilotVfsReadOutcome)[CopilotVfsReadOutcomeKey]; - -export const CopilotVfsReadPath = { - Binary: "binary", - Image: "image", - ParseableDocument: "parseable_document", - Text: "text", -} as const; - -export type CopilotVfsReadPathKey = keyof typeof CopilotVfsReadPath; -export type CopilotVfsReadPathValue = (typeof CopilotVfsReadPath)[CopilotVfsReadPathKey]; - -export const LlmErrorStage = { - BuildRequest: "build_request", - Decode: "decode", - HttpBuild: "http_build", - HttpStatus: "http_status", - Invoke: "invoke", - MarshalRequest: "marshal_request", - StreamClose: "stream_close", -} as const; - -export type LlmErrorStageKey = keyof typeof LlmErrorStage; -export type LlmErrorStageValue = (typeof LlmErrorStage)[LlmErrorStageKey]; - -export const PromptComponent = { - ActiveSkills: "active_skills", - AgentGuidance: "agent_guidance", - Capabilities: "capabilities", - Credentials: "credentials", - DesktopContext: "desktop_context", - ExternalTools: "external_tools", - Files: "files", - History: "history", - Override: "override", - PermissionsContext: "permissions_context", - Persona: "persona", - Policies: "policies", - RuntimeTail: "runtime_tail", - SessionContext: "session_context", - SkillsIndex: "skills_index", - SpawnContext: "spawn_context", - Steering: "steering", - SubagentDocs: "subagent_docs", - SubagentRegistry: "subagent_registry", - TaggedResources: "tagged_resources", - TimeContext: "time_context", - ToolDocs: "tool_docs", - ToolResults: "tool_results", - ToolsWire: "tools_wire", - Vfs: "vfs", - WorkflowContext: "workflow_context", - WorkspaceGuide: "workspace_guide", - WorkspaceInventory: "workspace_inventory", -} as const; - -export type PromptComponentKey = keyof typeof PromptComponent; -export type PromptComponentValue = (typeof PromptComponent)[PromptComponentKey]; - -export const RateLimitOutcome = { - Allowed: "allowed", - IncrError: "incr_error", - Limited: "limited", -} as const; - -export type RateLimitOutcomeKey = keyof typeof RateLimitOutcome; -export type RateLimitOutcomeValue = (typeof RateLimitOutcome)[RateLimitOutcomeKey]; - -export const SubagentExecutionPhase = { - Continuation: "continuation", - Initial: "initial", -} as const; - -export type SubagentExecutionPhaseKey = keyof typeof SubagentExecutionPhase; -export type SubagentExecutionPhaseValue = (typeof SubagentExecutionPhase)[SubagentExecutionPhaseKey]; - -export const SubagentSessionOperation = { - Ephemeral: "ephemeral", - New: "new", - Resume: "resume", -} as const; - -export type SubagentSessionOperationKey = keyof typeof SubagentSessionOperation; -export type SubagentSessionOperationValue = (typeof SubagentSessionOperation)[SubagentSessionOperationKey]; - -export const SubagentSessionPersistenceOutcome = { - Deferred: "deferred", - Failed: "failed", - NotApplicable: "not_applicable", - NotAttempted: "not_attempted", - Succeeded: "succeeded", -} as const; - -export type SubagentSessionPersistenceOutcomeKey = keyof typeof SubagentSessionPersistenceOutcome; -export type SubagentSessionPersistenceOutcomeValue = - (typeof SubagentSessionPersistenceOutcome)[SubagentSessionPersistenceOutcomeKey]; - -export const ToolAsyncWaiterResolution = { - ContextCancelled: "context_cancelled", - Poll: "poll", - Pubsub: "pubsub", - StoredAfterClose: "stored_after_close", - StoredBeforeSubscribe: "stored_before_subscribe", - StoredPostSubscribe: "stored_post_subscribe", - SubscriptionClosed: "subscription_closed", - Unknown: "unknown", -} as const; - -export type ToolAsyncWaiterResolutionKey = keyof typeof ToolAsyncWaiterResolution; -export type ToolAsyncWaiterResolutionValue = (typeof ToolAsyncWaiterResolution)[ToolAsyncWaiterResolutionKey]; - -export const ToolErrorKind = { - Dispatch: "dispatch", - NotFound: "not_found", -} as const; - -export type ToolErrorKindKey = keyof typeof ToolErrorKind; -export type ToolErrorKindValue = (typeof ToolErrorKind)[ToolErrorKindKey]; - -export const ToolExecutor = { - Client: "client", - Go: "go", - Sim: "sim", -} as const; - -export type ToolExecutorKey = keyof typeof ToolExecutor; -export type ToolExecutorValue = (typeof ToolExecutor)[ToolExecutorKey]; - -export const ToolStoreStatus = { - Cancelled: "cancelled", - Completed: "completed", - Failed: "failed", - Pending: "pending", -} as const; - -export type ToolStoreStatusKey = keyof typeof ToolStoreStatus; -export type ToolStoreStatusValue = (typeof ToolStoreStatus)[ToolStoreStatusKey]; diff --git a/apps/sim/lib/mothership/generated/trace-attributes-v1.ts b/apps/sim/lib/mothership/generated/trace-attributes-v1.ts deleted file mode 100644 index 15c3331dacf..00000000000 --- a/apps/sim/lib/mothership/generated/trace-attributes-v1.ts +++ /dev/null @@ -1,1373 +0,0 @@ -// GENERATED — do not edit. Source of truth: mothership worker packages/contracts/src/trace-attributes-v1.ts -// Regenerate with `bun run contracts:sync` in the worker. - -// Canonical custom mothership OTel span attribute keys Sim emits. Call sites reference -// `TraceAttr.` rather than raw strings so typos are compile errors. OTel -// semantic-convention keys (`http.*`, `db.*`, `gen_ai.*`, ...) come from -// `@opentelemetry/semantic-conventions`, not here. - -export const TraceAttr = { - AbortBackend: "abort.backend", - AbortFound: "abort.found", - AbortRedisResult: "abort.redis_result", - AnalyticsAborted: "analytics.aborted", - AnalyticsBilledCacheReadCost: "analytics.billed_cache_read_cost", - AnalyticsBilledCacheWriteCost: "analytics.billed_cache_write_cost", - AnalyticsBilledInputCost: "analytics.billed_input_cost", - AnalyticsBilledOutputCost: "analytics.billed_output_cost", - AnalyticsBilledTotalCost: "analytics.billed_total_cost", - AnalyticsCacheAttemptedRequests: "analytics.cache_attempted_requests", - AnalyticsCacheHitRequests: "analytics.cache_hit_requests", - AnalyticsCacheReadTokens: "analytics.cache_read_tokens", - AnalyticsCacheWriteRequests: "analytics.cache_write_requests", - AnalyticsCacheWriteTokens: "analytics.cache_write_tokens", - AnalyticsCustomerType: "analytics.customer_type", - AnalyticsDurationMs: "analytics.duration_ms", - AnalyticsError: "analytics.error", - AnalyticsInputTokens: "analytics.input_tokens", - AnalyticsModel: "analytics.model", - AnalyticsOutputTokens: "analytics.output_tokens", - AnalyticsProvider: "analytics.provider", - AnalyticsRawCacheReadCost: "analytics.raw_cache_read_cost", - AnalyticsRawCacheWriteCost: "analytics.raw_cache_write_cost", - AnalyticsRawInputCost: "analytics.raw_input_cost", - AnalyticsRawOutputCost: "analytics.raw_output_cost", - AnalyticsRawTotalCost: "analytics.raw_total_cost", - AnalyticsSource: "analytics.source", - AnalyticsToolCallCount: "analytics.tool_call_count", - ApiKeyId: "api_key.id", - ApiKeyName: "api_key.name", - AuthIncomingInternal: "auth.incoming_internal", - AuthKeyMatch: "auth.key.match", - AuthKeyPreview: "auth.key.preview", - AuthKeySource: "auth.key.source", - AuthKeyType: "auth.key.type", - AuthProvider: "auth.provider", - AuthValidateStatusCode: "auth.validate.status_code", - AwsRegion: "aws.region", - BillingAttempts: "billing.attempts", - BillingByok: "billing.byok", - BillingChangeType: "billing.change_type", - BillingCostInputUsd: "billing.cost.input_usd", - BillingCostOutputUsd: "billing.cost.output_usd", - BillingCostTotalUsd: "billing.cost.total_usd", - BillingCostUsd: "billing.cost_usd", - BillingCustomerType: "billing.customer_type", - BillingDuplicate: "billing.duplicate", - BillingDurationMs: "billing.duration_ms", - BillingHasIdempotencyKey: "billing.has_idempotency_key", - BillingIdempotencyKey: "billing.idempotency_key", - BillingInterval: "billing.interval", - BillingIsMcp: "billing.is_mcp", - BillingLlmCost: "billing.llm_cost", - BillingLongContext: "billing.long_context", - BillingNewPlan: "billing.new_plan", - BillingOutcome: "billing.outcome", - BillingPlan: "billing.plan", - BillingPreviousPlan: "billing.previous_plan", - BillingServiceCharges: "billing.service_charges", - BillingSource: "billing.source", - BillingTotalCost: "billing.total_cost", - BillingUsageCurrent: "billing.usage.current", - BillingUsageExceeded: "billing.usage.exceeded", - BillingUsageLimit: "billing.usage.limit", - BlockId: "block.id", - BlockName: "block.name", - BlockType: "block.type", - ChatActiveMessagesBytes: "chat.active_messages_bytes", - ChatActiveMessagesCount: "chat.active_messages_count", - ChatAppendBytes: "chat.append_bytes", - ChatAppendCount: "chat.append_count", - ChatArtifactKeys: "chat.artifact_keys", - ChatArtifactsBytes: "chat.artifacts_bytes", - ChatAuthType: "chat.auth_type", - ChatBlobOp: "chat.blob.op", - ChatBlobSite: "chat.blob.site", - ChatBlobBytesOffloaded: "chat.blob_bytes_offloaded", - ChatBlobBytesResolved: "chat.blob_bytes_resolved", - ChatBlobsOffloaded: "chat.blobs_offloaded", - ChatBlobsResolved: "chat.blobs_resolved", - ChatContextCount: "chat.context_count", - ChatContextUsage: "chat.context_usage", - ChatContinuationMessagesBefore: "chat.continuation.messages_before", - ChatContinuationToolResultBytes: "chat.continuation.tool_result_bytes", - ChatContinuationToolResultFailure: "chat.continuation.tool_result_failure", - ChatContinuationToolResultSuccess: "chat.continuation.tool_result_success", - ChatContinuationToolResults: "chat.continuation.tool_results", - ChatContinuationTotalToolCalls: "chat.continuation.total_tool_calls", - ChatExistingMessageCount: "chat.existing_message_count", - ChatFileAttachmentCount: "chat.file_attachment_count", - ChatFinalizeOutcome: "chat.finalize.outcome", - ChatFound: "chat.found", - ChatHasAssistantMessage: "chat.has_assistant_message", - ChatHasOutputConfigs: "chat.has_output_configs", - ChatId: "chat.id", - ChatMessageBytes: "chat.message_bytes", - ChatMessagesAfter: "chat.messages_after", - ChatMessagesBytes: "chat.messages_bytes", - ChatMessagesCount: "chat.messages_count", - ChatPersistOutcome: "chat.persist.outcome", - ChatPreexisting: "chat.preexisting", - ChatRollbackIndex: "chat.rollback_index", - ChatTokensUsed: "chat.tokens_used", - ChatType: "chat.type", - ChatUserMessageId: "chat.user_message_id", - CheckpointAge: "checkpoint.age", - CheckpointAttemptsBytes: "checkpoint.attempts_bytes", - CheckpointBytesAssistantToolUse: "checkpoint.bytes.assistant_tool_use", - CheckpointBytesCurrentMessages: "checkpoint.bytes.current_messages", - CheckpointBytesImmediateResults: "checkpoint.bytes.immediate_results", - CheckpointBytesPendingToolCalls: "checkpoint.bytes.pending_tool_calls", - CheckpointBytesProviderRequest: "checkpoint.bytes.provider_request", - CheckpointBytesRequestContext: "checkpoint.bytes.request_context", - CheckpointBytesToolUsage: "checkpoint.bytes.tool_usage", - CheckpointCachedCredentialsBytes: "checkpoint.cached_credentials_bytes", - CheckpointClaimed: "checkpoint.claimed", - CheckpointClaimedNow: "checkpoint.claimed_now", - CheckpointCompletedBytes: "checkpoint.completed_bytes", - CheckpointCompletedSteps: "checkpoint.completed_steps", - CheckpointCurrentMessages: "checkpoint.current_messages", - CheckpointDecisionsBytes: "checkpoint.decisions_bytes", - CheckpointFound: "checkpoint.found", - CheckpointFrames: "checkpoint.frames", - CheckpointId: "checkpoint.id", - CheckpointImmediateResults: "checkpoint.immediate_results", - CheckpointMessageId: "checkpoint.message_id", - CheckpointPendingBytes: "checkpoint.pending_bytes", - CheckpointPendingSteps: "checkpoint.pending_steps", - CheckpointPendingToolCount: "checkpoint.pending_tool_count", - CheckpointRows: "checkpoint.rows", - CheckpointTaskId: "checkpoint.task_id", - CheckpointTotalToolCalls: "checkpoint.total_tool_calls", - CheckpointWorkflowSnapshotBytes: "checkpoint.workflow_snapshot_bytes", - ClientVersion: "client.version", - ConditionId: "condition.id", - ConditionName: "condition.name", - ConditionResult: "condition.result", - ContextReduceBudgetChars: "context.reduce.budget_chars", - ContextReduceBudgetSource: "context.reduce.budget_source", - ContextReduceCaller: "context.reduce.caller", - ContextReduceDidReduce: "context.reduce.did_reduce", - ContextReduceInputChars: "context.reduce.input_chars", - ContextReduceInputMessages: "context.reduce.input_messages", - ContextReduceOutcome: "context.reduce.outcome", - ContextReduceOutputChars: "context.reduce.output_chars", - ContextReduceOutputMessages: "context.reduce.output_messages", - ContextReduced: "context.reduced", - ContextSummarizeInputChars: "context.summarize.input_chars", - ContextSummarizeOutputChars: "context.summarize.output_chars", - ContextTransformCaller: "context.transform.caller", - ContextTransformCharsIn: "context.transform.chars_in", - ContextTransformCharsOut: "context.transform.chars_out", - ContextTransformDropCount: "context.transform.drop_count", - ContextTransformDrops: "context.transform.drops", - ContextTransformMessagesIn: "context.transform.messages_in", - ContextTransformMessagesOut: "context.transform.messages_out", - ContextTransformStage: "context.transform.stage", - CopilotAbortControllerFired: "copilot.abort.controller_fired", - CopilotAbortGoMarkerOk: "copilot.abort.go_marker_ok", - CopilotAbortLocalAborted: "copilot.abort.local_aborted", - CopilotAbortMarkerWritten: "copilot.abort.marker_written", - CopilotAbortOutcome: "copilot.abort.outcome", - CopilotAbortUnknownReason: "copilot.abort.unknown_reason", - CopilotAsyncSubagentAgent: "copilot.async_subagent.agent", - CopilotAsyncSubagentCount: "copilot.async_subagent.count", - CopilotAsyncSubagentDeltaChars: "copilot.async_subagent.delta_chars", - CopilotAsyncSubagentId: "copilot.async_subagent.id", - CopilotAsyncSubagentReminders: "copilot.async_subagent.reminders", - CopilotAsyncSubagentSleptMs: "copilot.async_subagent.slept_ms", - CopilotAsyncSubagentStatus: "copilot.async_subagent.status", - CopilotAsyncSubagentWaitMode: "copilot.async_subagent.wait_mode", - CopilotAsyncSubagentWakeReason: "copilot.async_subagent.wake_reason", - CopilotAsyncToolClaimedBy: "copilot.async_tool.claimed_by", - CopilotAsyncToolHasError: "copilot.async_tool.has_error", - CopilotAsyncToolIdsCount: "copilot.async_tool.ids_count", - CopilotAsyncToolPermissionDecision: "copilot.async_tool.permission_decision", - CopilotAsyncToolStatus: "copilot.async_tool.status", - CopilotAsyncToolWorkerId: "copilot.async_tool.worker_id", - CopilotBranchKind: "copilot.branch.kind", - CopilotChatIsNew: "copilot.chat.is_new", - CopilotCheckpointPendingToolCallId: "copilot.checkpoint.pending_tool_call_id", - CopilotCommandsCount: "copilot.commands.count", - CopilotConfirmOutcome: "copilot.confirm.outcome", - CopilotContextsCount: "copilot.contexts.count", - CopilotDegradedReason: "copilot.degraded.reason", - CopilotExecutionId: "copilot.execution.id", - CopilotFileAttachmentsCount: "copilot.file_attachments.count", - CopilotFinalizeOutcome: "copilot.finalize.outcome", - CopilotInterruptedPriorStream: "copilot.interrupted_prior_stream", - CopilotLeg: "copilot.leg", - CopilotMode: "copilot.mode", - CopilotOperation: "copilot.operation", - CopilotOutputFileBytes: "copilot.output_file.bytes", - CopilotOutputFileFormat: "copilot.output_file.format", - CopilotOutputFileId: "copilot.output_file.id", - CopilotOutputFileMimeType: "copilot.output_file.mime_type", - CopilotOutputFileMode: "copilot.output_file.mode", - CopilotOutputFileName: "copilot.output_file.name", - CopilotOutputFileOutcome: "copilot.output_file.outcome", - CopilotOutputFilePath: "copilot.output_file.path", - CopilotOutputFileSandboxPath: "copilot.output_file.sandbox_path", - CopilotPendingStreamWaitMs: "copilot.pending_stream.wait_ms", - CopilotPrefetch: "copilot.prefetch", - CopilotPublisherClientDisconnected: "copilot.publisher.client_disconnected", - CopilotPublisherSawComplete: "copilot.publisher.saw_complete", - CopilotRecoveryLatestSeq: "copilot.recovery.latest_seq", - CopilotRecoveryOldestSeq: "copilot.recovery.oldest_seq", - CopilotRecoveryOutcome: "copilot.recovery.outcome", - CopilotRecoveryRequestedAfterSeq: "copilot.recovery.requested_after_seq", - CopilotRequestCancelReason: "copilot.request.cancel_reason", - CopilotRequestOutcome: "copilot.request.outcome", - CopilotRequestWillRetryOnStreamError: "copilot.request.will_retry_on_stream_error", - CopilotResourceAttachmentsCount: "copilot.resource_attachments.count", - CopilotResourcesAborted: "copilot.resources.aborted", - CopilotResourcesOp: "copilot.resources.op", - CopilotResourcesRemovedCount: "copilot.resources.removed_count", - CopilotResourcesUpsertedCount: "copilot.resources.upserted_count", - CopilotResultContentBlocks: "copilot.result.content_blocks", - CopilotResultContentLength: "copilot.result.content_length", - CopilotResultToolCalls: "copilot.result.tool_calls", - CopilotResumeAfterCursor: "copilot.resume.after_cursor", - CopilotResumeDurationMs: "copilot.resume.duration_ms", - CopilotResumeEventCount: "copilot.resume.event_count", - CopilotResumeOutcome: "copilot.resume.outcome", - CopilotResumePollIterations: "copilot.resume.poll_iterations", - CopilotResumePreviewSessionCount: "copilot.resume.preview_session_count", - CopilotRoute: "copilot.route", - CopilotRunAgent: "copilot.run.agent", - CopilotRunHasCompletedAt: "copilot.run.has_completed_at", - CopilotRunHasError: "copilot.run.has_error", - CopilotRunModel: "copilot.run.model", - CopilotRunParentId: "copilot.run.parent_id", - CopilotRunProvider: "copilot.run.provider", - CopilotRunStatus: "copilot.run.status", - CopilotSseBytesReceived: "copilot.sse.bytes_received", - CopilotSseChunksReceived: "copilot.sse.chunks_received", - CopilotSseCloseReason: "copilot.sse.close_reason", - CopilotSseEventsComplete: "copilot.sse.events.complete", - CopilotSseEventsError: "copilot.sse.events.error", - CopilotSseEventsReceived: "copilot.sse.events.received", - CopilotSseEventsResource: "copilot.sse.events.resource", - CopilotSseEventsRun: "copilot.sse.events.run", - CopilotSseEventsSession: "copilot.sse.events.session", - CopilotSseEventsSpan: "copilot.sse.events.span", - CopilotSseEventsText: "copilot.sse.events.text", - CopilotSseEventsTool: "copilot.sse.events.tool", - CopilotSseExpectedTerminal: "copilot.sse.expected_terminal", - CopilotSseFirstEventMs: "copilot.sse.first_event_ms", - CopilotSseLongestDispatchMs: "copilot.sse.longest_dispatch_ms", - CopilotSseLongestInboundGapMs: "copilot.sse.longest_inbound_gap_ms", - CopilotSseTerminalEventMissing: "copilot.sse.terminal_event_missing", - CopilotSseTerminalEventSeen: "copilot.sse.terminal_event_seen", - CopilotSseTotalDispatchMs: "copilot.sse.total_dispatch_ms", - CopilotSteerOutcome: "copilot.steer.outcome", - CopilotSteeringContentChars: "copilot.steering.content_chars", - CopilotSteeringEntries: "copilot.steering.entries", - CopilotSteeringMode: "copilot.steering.mode", - CopilotSteeringSalvagedChars: "copilot.steering.salvaged_chars", - CopilotStopAppendedAssistant: "copilot.stop.appended_assistant", - CopilotStopBlocksCount: "copilot.stop.blocks_count", - CopilotStopContentLength: "copilot.stop.content_length", - CopilotStopOutcome: "copilot.stop.outcome", - CopilotStream: "copilot.stream", - CopilotSurface: "copilot.surface", - CopilotTableId: "copilot.table.id", - CopilotTableOutcome: "copilot.table.outcome", - CopilotTableRowCount: "copilot.table.row_count", - CopilotTableSourceContentBytes: "copilot.table.source.content_bytes", - CopilotTableSourceFormat: "copilot.table.source.format", - CopilotTableSourcePath: "copilot.table.source.path", - CopilotTraceSpanCount: "copilot.trace.span_count", - CopilotTransport: "copilot.transport", - CopilotUserMessagePreview: "copilot.user.message_preview", - CopilotValidateOutcome: "copilot.validate.outcome", - CopilotVfsDeltaOutcome: "copilot.vfs.delta.outcome", - CopilotVfsFileExtension: "copilot.vfs.file.extension", - CopilotVfsFileMediaType: "copilot.vfs.file.media_type", - CopilotVfsFileName: "copilot.vfs.file.name", - CopilotVfsFileSizeBytes: "copilot.vfs.file.size_bytes", - CopilotVfsHasAlpha: "copilot.vfs.has_alpha", - CopilotVfsInputBytes: "copilot.vfs.input.bytes", - CopilotVfsInputHeight: "copilot.vfs.input.height", - CopilotVfsInputMediaTypeClaimed: "copilot.vfs.input.media_type_claimed", - CopilotVfsInputMediaTypeDetected: "copilot.vfs.input.media_type_detected", - CopilotVfsInputWidth: "copilot.vfs.input.width", - CopilotVfsMaterializeFileCount: "copilot.vfs.materialize.file_count", - CopilotVfsMaterializePhaseMs: "copilot.vfs.materialize.phase_ms", - CopilotVfsMetadataFailed: "copilot.vfs.metadata.failed", - CopilotVfsOutcome: "copilot.vfs.outcome", - CopilotVfsOutputBytes: "copilot.vfs.output.bytes", - CopilotVfsOutputMediaType: "copilot.vfs.output.media_type", - CopilotVfsPhase: "copilot.vfs.phase", - CopilotVfsReadImageResized: "copilot.vfs.read.image.resized", - CopilotVfsReadOutcome: "copilot.vfs.read.outcome", - CopilotVfsReadOutputBytes: "copilot.vfs.read.output.bytes", - CopilotVfsReadOutputLines: "copilot.vfs.read.output.lines", - CopilotVfsReadOutputMediaType: "copilot.vfs.read.output.media_type", - CopilotVfsReadPath: "copilot.vfs.read.path", - CopilotVfsResizeAttempts: "copilot.vfs.resize.attempts", - CopilotVfsResizeChosenDimension: "copilot.vfs.resize.chosen_dimension", - CopilotVfsResizeChosenQuality: "copilot.vfs.resize.chosen_quality", - CopilotVfsResizeDimension: "copilot.vfs.resize.dimension", - CopilotVfsResizeFitsBudget: "copilot.vfs.resize.fits_budget", - CopilotVfsResizeOutputBytes: "copilot.vfs.resize.output_bytes", - CopilotVfsResizeQuality: "copilot.vfs.resize.quality", - CopilotVfsResized: "copilot.vfs.resized", - CopilotVfsSharpLoadFailed: "copilot.vfs.sharp.load_failed", - CostDefaultCost: "cost.default_cost", - CredentialSetId: "credential_set.id", - CredentialSetName: "credential_set.name", - DbOperation: "db.operation", - DbSqlTable: "db.sql.table", - DbSystem: "db.system", - DeploymentEnvironment: "deployment.environment", - DeploymentVersion: "deployment.version", - DocumentFileSize: "document.file_size", - DocumentMimeType: "document.mime_type", - DocumentsCount: "documents.count", - DocumentsUploadType: "documents.upload_type", - Error: "error", - ErrorCode: "error.code", - ErrorInternal: "error.internal", - ErrorMessage: "error.message", - ErrorType: "error.type", - EventName: "event.name", - EventTimestamp: "event.timestamp", - ExecutionBlocksExecuted: "execution.blocks_executed", - ExecutionDurationMs: "execution.duration_ms", - ExecutionErrorMessage: "execution.error_message", - ExecutionHasErrors: "execution.has_errors", - ExecutionStatus: "execution.status", - ExecutionTotalCost: "execution.total_cost", - ExecutionTrigger: "execution.trigger", - FunctionExecutionTimeMs: "function.execution_time_ms", - FunctionId: "function.id", - FunctionName: "function.name", - GenAiAgentId: "gen_ai.agent.id", - GenAiAgentName: "gen_ai.agent.name", - GenAiCacheOutcome: "gen_ai.cache.outcome", - GenAiCacheTokenType: "gen_ai.cache.token.type", - GenAiCostInput: "gen_ai.cost.input", - GenAiCostOutput: "gen_ai.cost.output", - GenAiCostTotal: "gen_ai.cost.total", - GenAiInputMessages: "gen_ai.input.messages", - GenAiOperationName: "gen_ai.operation.name", - GenAiOutputMessages: "gen_ai.output.messages", - GenAiProviderName: "gen_ai.provider.name", - GenAiRequestAssistantMessages: "gen_ai.request.assistant_messages", - GenAiRequestCacheableSystemBlocks: "gen_ai.request.cacheable_system_blocks", - GenAiRequestContentBlocks: "gen_ai.request.content_blocks", - GenAiRequestHasCacheControl: "gen_ai.request.has_cache_control", - GenAiRequestImageBlocks: "gen_ai.request.image_blocks", - GenAiRequestImageDataBytes: "gen_ai.request.image_data_bytes", - GenAiRequestMaxMessageBlocks: "gen_ai.request.max_message_blocks", - GenAiRequestMessagesCount: "gen_ai.request.messages.count", - GenAiRequestModel: "gen_ai.request.model", - GenAiRequestPromptCacheBreakpointCreated: "gen_ai.request.prompt_cache.breakpoint.created", - GenAiRequestPromptCacheBreakpointDynamicWriteSuppressReason: - "gen_ai.request.prompt_cache.breakpoint.dynamic_write_suppress_reason", - GenAiRequestPromptCacheBreakpointDynamicWriteSuppressed: - "gen_ai.request.prompt_cache.breakpoint.dynamic_write_suppressed", - GenAiRequestPromptCacheBreakpointEligible: "gen_ai.request.prompt_cache.breakpoint.eligible", - GenAiRequestPromptCacheBreakpointKind: "gen_ai.request.prompt_cache.breakpoint.kind", - GenAiRequestPromptCacheBreakpointMinimumTokens: "gen_ai.request.prompt_cache.breakpoint.minimum_tokens", - GenAiRequestPromptCacheBreakpointPrefixTokensEstimated: - "gen_ai.request.prompt_cache.breakpoint.prefix_tokens_estimated", - GenAiRequestPromptCacheBreakpointPreviousBoundaryTokens: - "gen_ai.request.prompt_cache.breakpoint.previous_boundary_tokens", - GenAiRequestPromptCacheBreakpointSection: "gen_ai.request.prompt_cache.breakpoint.section", - GenAiRequestPromptCacheBreakpointSkipReason: "gen_ai.request.prompt_cache.breakpoint.skip_reason", - GenAiRequestPromptCacheBreakpointTargetIndex: "gen_ai.request.prompt_cache.breakpoint.target_index", - GenAiRequestPromptCacheBreakpointTargetRole: "gen_ai.request.prompt_cache.breakpoint.target_role", - GenAiRequestPromptCacheBreakpointTargetType: "gen_ai.request.prompt_cache.breakpoint.target_type", - GenAiRequestPromptCacheBreakpointThresholdTokens: "gen_ai.request.prompt_cache.breakpoint.threshold_tokens", - GenAiRequestPromptCacheBreakpointTtl: "gen_ai.request.prompt_cache.breakpoint.ttl", - GenAiRequestPromptCacheBreakpointsCount: "gen_ai.request.prompt_cache.breakpoints.count", - GenAiRequestPromptCacheBreakpointsCreated: "gen_ai.request.prompt_cache.breakpoints.created", - GenAiRequestPromptCacheDynamicMinimumTokens: "gen_ai.request.prompt_cache.dynamic.minimum_tokens", - GenAiRequestPromptCacheDynamicPrefixTokensEstimated: - "gen_ai.request.prompt_cache.dynamic.prefix_tokens_estimated", - GenAiRequestPromptCacheDynamicPreviousBoundaryTokens: - "gen_ai.request.prompt_cache.dynamic.previous_boundary_tokens", - GenAiRequestPromptCacheDynamicSkipReason: "gen_ai.request.prompt_cache.dynamic.skip_reason", - GenAiRequestPromptCacheDynamicTargetType: "gen_ai.request.prompt_cache.dynamic.target_type", - GenAiRequestPromptCacheDynamicThresholdTokens: "gen_ai.request.prompt_cache.dynamic.threshold_tokens", - GenAiRequestPromptCacheDynamicTtl: "gen_ai.request.prompt_cache.dynamic.ttl", - GenAiRequestPromptCacheDynamicUserEligible: "gen_ai.request.prompt_cache.dynamic_user_eligible", - GenAiRequestPromptCacheDynamicUserSkipReason: "gen_ai.request.prompt_cache.dynamic_user_skip_reason", - GenAiRequestPromptCacheStaticTtl: "gen_ai.request.prompt_cache.static.ttl", - GenAiRequestPromptCacheStaticSystem: "gen_ai.request.prompt_cache.static_system", - GenAiRequestPromptCacheStaticTools: "gen_ai.request.prompt_cache.static_tools", - GenAiRequestRuntimeContextChars: "gen_ai.request.runtime_context_chars", - GenAiRequestRuntimeContextMessages: "gen_ai.request.runtime_context_messages", - GenAiRequestSystemBlocks: "gen_ai.request.system_blocks", - GenAiRequestSystemChars: "gen_ai.request.system_chars", - GenAiRequestTextBlocks: "gen_ai.request.text_blocks", - GenAiRequestToolResultBlocks: "gen_ai.request.tool_result_blocks", - GenAiRequestToolUseBlocks: "gen_ai.request.tool_use_blocks", - GenAiRequestToolsCount: "gen_ai.request.tools.count", - GenAiRequestUserMessages: "gen_ai.request.user_messages", - GenAiResponseFinishReasons: "gen_ai.response.finish_reasons", - GenAiResponseModel: "gen_ai.response.model", - GenAiStreamPhaseTextBytes: "gen_ai.stream.phase.text.bytes", - GenAiStreamPhaseTextChunks: "gen_ai.stream.phase.text.chunks", - GenAiStreamPhaseTextFirstMs: "gen_ai.stream.phase.text.first_ms", - GenAiStreamPhaseTextMs: "gen_ai.stream.phase.text.ms", - GenAiStreamPhaseThinkingBytes: "gen_ai.stream.phase.thinking.bytes", - GenAiStreamPhaseThinkingChunks: "gen_ai.stream.phase.thinking.chunks", - GenAiStreamPhaseThinkingFirstMs: "gen_ai.stream.phase.thinking.first_ms", - GenAiStreamPhaseThinkingMs: "gen_ai.stream.phase.thinking.ms", - GenAiStreamPhaseToolArgsBytes: "gen_ai.stream.phase.tool_args.bytes", - GenAiStreamPhaseToolArgsChunks: "gen_ai.stream.phase.tool_args.chunks", - GenAiStreamPhaseToolArgsFirstMs: "gen_ai.stream.phase.tool_args.first_ms", - GenAiStreamPhaseToolArgsMs: "gen_ai.stream.phase.tool_args.ms", - GenAiSystem: "gen_ai.system", - GenAiThinkingBlocksCaptured: "gen_ai.thinking.blocks_captured", - GenAiThinkingRedactedBlocks: "gen_ai.thinking.redacted_blocks", - GenAiTokenType: "gen_ai.token.type", - GenAiToolName: "gen_ai.tool.name", - GenAiUsageCacheCreationInputTokens: "gen_ai.usage.cache_creation.input_tokens", - GenAiUsageCacheCreationTokens: "gen_ai.usage.cache_creation_tokens", - GenAiUsageCacheReadInputTokens: "gen_ai.usage.cache_read.input_tokens", - GenAiUsageCacheReadTokens: "gen_ai.usage.cache_read_tokens", - GenAiUsageContextTokens: "gen_ai.usage.context_tokens", - GenAiUsageInputTokens: "gen_ai.usage.input_tokens", - GenAiUsageOutputTokens: "gen_ai.usage.output_tokens", - GenAiUsageThinkingTokens: "gen_ai.usage.thinking_tokens", - GenAiUsageTotalTokens: "gen_ai.usage.total_tokens", - GenAiWorkflowId: "gen_ai.workflow.id", - GenAiWorkflowName: "gen_ai.workflow.name", - HostedKeyEnvVar: "hosted_key.env_var", - HttpHost: "http.host", - HttpMethod: "http.method", - HttpPath: "http.path", - HttpRemoteAddr: "http.remote_addr", - HttpRequestContentLength: "http.request.content_length", - HttpResponseBodyBytes: "http.response.body_bytes", - HttpResponseContentLength: "http.response.content_length", - HttpResponseHeadersMs: "http.response.headers_ms", - HttpResponseTotalMs: "http.response.total_ms", - HttpRoute: "http.route", - HttpServerDurationMs: "http.server.duration_ms", - HttpStatusCode: "http.status_code", - HttpTarget: "http.target", - HttpUrl: "http.url", - HttpUserAgent: "http.user_agent", - InvitationRole: "invitation.role", - LlmBackend: "llm.backend", - LlmCompactionPause: "llm.compaction.pause", - LlmErrorStage: "llm.error_stage", - LlmProtocol: "llm.protocol", - LlmRequestBodyBytes: "llm.request.body_bytes", - LlmRequestCompactionTrigger: "llm.request.compaction_trigger", - LlmStreamBytes: "llm.stream.bytes", - LlmStreamChunks: "llm.stream.chunks", - LlmStreamFirstChunkBytes: "llm.stream.first_chunk_bytes", - LlmStreamFirstChunkMs: "llm.stream.first_chunk_ms", - LlmStreamFirstTokenMs: "llm.stream.first_token_ms", - LlmStreamOpenMs: "llm.stream.open_ms", - LlmStreamTotalMs: "llm.stream.total_ms", - LockAcquired: "lock.acquired", - LockBackend: "lock.backend", - LockTimedOut: "lock.timed_out", - LockTimeoutMs: "lock.timeout_ms", - LoopId: "loop.id", - LoopIterations: "loop.iterations", - LoopName: "loop.name", - ManageKnowledgeBaseId: "manage_knowledge_base.id", - ManageKnowledgeBaseName: "manage_knowledge_base.name", - McpExecutionStatus: "mcp.execution_status", - McpServerId: "mcp.server_id", - McpServerName: "mcp.server_name", - McpToolName: "mcp.tool_name", - McpTransport: "mcp.transport", - MemberRole: "member.role", - MemoryContentBytes: "memory.content_bytes", - MemoryFound: "memory.found", - MemoryPath: "memory.path", - MemoryRowCount: "memory.row_count", - MessageId: "message.id", - MessagesDeserializeMs: "messages.deserialize_ms", - MessagesSerializeOp: "messages.serialize.op", - MessagesSerializeSite: "messages.serialize.site", - MessagesSerializeMs: "messages.serialize_ms", - MessagingDestinationName: "messaging.destination.name", - MessagingSystem: "messaging.system", - ModelDurationMs: "model.duration_ms", - ModelId: "model.id", - ModelName: "model.name", - MothershipOrigin: "mothership.origin", - NetPeerName: "net.peer.name", - OauthProvider: "oauth.provider", - ParallelBranches: "parallel.branches", - ParallelId: "parallel.id", - ParallelName: "parallel.name", - PrefsToolCount: "prefs.tool_count", - ProcessingChunkSize: "processing.chunk_size", - ProcessingRecipe: "processing.recipe", - PromptCacheableBlocks: "prompt.cacheable_blocks", - PromptComponent: "prompt.component", - PromptRegionFilesChars: "prompt.region.files_chars", - PromptRegionFilesCount: "prompt.region.files_count", - PromptRegionHistoryChars: "prompt.region.history_chars", - PromptRegionHistoryMessages: "prompt.region.history_messages", - PromptRegionRuntimeTailChars: "prompt.region.runtime_tail_chars", - PromptRegionSteeringChars: "prompt.region.steering_chars", - PromptRegionToolResultsChars: "prompt.region.tool_results_chars", - PromptRegionVfsChars: "prompt.region.vfs_chars", - PromptRegionVfsMessages: "prompt.region.vfs_messages", - PromptRuntimeActiveSkillsChars: "prompt.runtime.active_skills_chars", - PromptRuntimeTaggedResourcesChars: "prompt.runtime.tagged_resources_chars", - PromptSectionAgentGuidanceChars: "prompt.section.agent_guidance_chars", - PromptSectionCapabilitiesChars: "prompt.section.capabilities_chars", - PromptSectionCredentialsChars: "prompt.section.credentials_chars", - PromptSectionOverrideChars: "prompt.section.override_chars", - PromptSectionPersonaChars: "prompt.section.persona_chars", - PromptSectionPoliciesChars: "prompt.section.policies_chars", - PromptSectionSkillsIndexChars: "prompt.section.skills_index_chars", - PromptSectionSpawnContextChars: "prompt.section.spawn_context_chars", - PromptSectionSubagentDocsChars: "prompt.section.subagent_docs_chars", - PromptSectionToolDocsChars: "prompt.section.tool_docs_chars", - PromptSectionWorkspaceGuideChars: "prompt.section.workspace_guide_chars", - PromptSectionWorkspaceInventoryChars: "prompt.section.workspace_inventory_chars", - PromptSet: "prompt.set", - PromptSite: "prompt.site", - PromptSystemBlocks: "prompt.system_blocks", - PromptSystemChars: "prompt.system_chars", - PromptToolsWireChars: "prompt.tools.wire_chars", - PromptToolsWireCount: "prompt.tools.wire_count", - ProviderId: "provider.id", - RateLimitAttempt: "rate_limit.attempt", - RateLimitCount: "rate_limit.count", - RateLimitDelayMs: "rate_limit.delay_ms", - RateLimitLimit: "rate_limit.limit", - RateLimitMaxRetries: "rate_limit.max_retries", - RateLimitOutcome: "rate_limit.outcome", - RateLimitRetryAfterMs: "rate_limit.retry_after_ms", - RequestGoTraceId: "request.go_trace_id", - RequestId: "request.id", - RequiredVersion: "required.version", - ResumeRequestBodyBytes: "resume.request.body_bytes", - ResumeResultsCount: "resume.results.count", - ResumeResultsDataBytes: "resume.results.data_bytes", - ResumeResultsFailureCount: "resume.results.failure_count", - ResumeResultsSuccessCount: "resume.results.success_count", - RouterBackendName: "router.backend_name", - RouterConfigVersion: "router.config_version", - RouterId: "router.id", - RouterName: "router.name", - RouterRouteReason: "router.route_reason", - RouterSelectedBackend: "router.selected_backend", - RouterSelectedPath: "router.selected_path", - RunId: "run.id", - SearchResultsCount: "search.results_count", - ServerAddress: "server.address", - ServiceInstanceId: "service.instance.id", - ServiceName: "service.name", - ServiceNamespace: "service.namespace", - ServiceVersion: "service.version", - SettleCompleted: "settle.completed", - SettleTimeoutMs: "settle.timeout_ms", - SettleWaitMs: "settle.wait_ms", - SimOperation: "sim.operation", - SimRequestId: "sim.request_id", - SpanDurationMs: "span.duration_ms", - SpanStatus: "span.status", - SpanType: "span.type", - StreamId: "stream.id", - SubagentDepth: "subagent.depth", - SubagentEscalationApplied: "subagent.escalation.applied", - SubagentEscalationPriorCalls: "subagent.escalation.prior_calls", - SubagentEscalationRequested: "subagent.escalation.requested", - SubagentExecutionPhase: "subagent.execution.phase", - SubagentId: "subagent.id", - SubagentNested: "subagent.nested", - SubagentOutcomeContentBytes: "subagent.outcome.content_bytes", - SubagentOutcomeError: "subagent.outcome.error", - SubagentOutcomeFirstChunkMs: "subagent.outcome.first_chunk_ms", - SubagentOutcomeLlmRounds: "subagent.outcome.llm_rounds", - SubagentOutcomeStructuredType: "subagent.outcome.structured_type", - SubagentOutcomeSuccess: "subagent.outcome.success", - SubagentOutcomeToolCallCount: "subagent.outcome.tool_call_count", - SubagentParentAgentId: "subagent.parent_agent_id", - SubagentRouteManifestMissing: "subagent.route.manifest_missing", - SubagentSessionId: "subagent.session.id", - SubagentSessionOperation: "subagent.session.operation", - SubagentSessionPersistenceOutcome: "subagent.session.persistence_outcome", - SubagentSessionRevision: "subagent.session.revision", - SubagentSessionRunId: "subagent.session.run_id", - SubagentSessionTitle: "subagent.session.title", - SubagentToolCallId: "subagent.tool_call_id", - TaskAge: "task.age", - TaskDecisionCount: "task.decision_count", - TaskErrorCount: "task.error_count", - TaskFound: "task.found", - TaskId: "task.id", - TaskListLimit: "task.list_limit", - TaskRows: "task.rows", - TaskStatus: "task.status", - TaskStepCount: "task.step_count", - TelemetrySdkLanguage: "telemetry.sdk.language", - TelemetrySdkName: "telemetry.sdk.name", - TelemetrySdkVersion: "telemetry.sdk.version", - TemplateId: "template.id", - TemplateName: "template.name", - ThrottleReason: "throttle.reason", - ToolArgsBytes: "tool.args.bytes", - ToolArgsCount: "tool.args.count", - ToolArgsPreview: "tool.args.preview", - ToolAsyncWaiterPollCount: "tool.async_waiter.poll_count", - ToolAsyncWaiterPubsubDeliveries: "tool.async_waiter.pubsub_deliveries", - ToolAsyncWaiterResolution: "tool.async_waiter.resolution", - ToolCallId: "tool.call_id", - ToolClientCount: "tool.client.count", - ToolClientExecutable: "tool.client_executable", - ToolCompletionReceived: "tool.completion.received", - ToolConfirmationStatus: "tool.confirmation.status", - ToolDeferredCount: "tool.deferred.count", - ToolDurationMs: "tool.duration_ms", - ToolErrorKind: "tool.error_kind", - ToolExecutor: "tool.executor", - ToolExternalService: "tool.external.service", - ToolId: "tool.id", - ToolLoadedCount: "tool.loaded.count", - ToolName: "tool.name", - ToolOutcome: "tool.outcome", - ToolOutcomeMessage: "tool.outcome.message", - ToolParentSpan: "tool.parent_span", - ToolPayloadBytes: "tool.payload.bytes", - ToolResultArtifact: "tool.result.artifact", - ToolResultBytes: "tool.result.bytes", - ToolResultSuccess: "tool.result.success", - ToolScheduled: "tool.scheduled", - ToolStatus: "tool.status", - ToolStatusCode: "tool.status_code", - ToolStoreStatus: "tool.store_status", - ToolSync: "tool.sync", - ToolTimeoutMs: "tool.timeout_ms", - ToolVisibleCount: "tool.visible.count", - ToolVisibleNames: "tool.visible.names", - TraceAborted: "trace.aborted", - TraceBilledTotalCost: "trace.billed_total_cost", - TraceCacheAttemptedRequests: "trace.cache_attempted_requests", - TraceCacheHitRequests: "trace.cache_hit_requests", - TraceCacheReadTokens: "trace.cache_read_tokens", - TraceCacheWriteRequests: "trace.cache_write_requests", - TraceCacheWriteTokens: "trace.cache_write_tokens", - TraceDurationMs: "trace.duration_ms", - TraceError: "trace.error", - TraceGoId: "trace.go_id", - TraceInputTokens: "trace.input_tokens", - TraceModel: "trace.model", - TraceOutcome: "trace.outcome", - TraceOutputTokens: "trace.output_tokens", - TraceProvider: "trace.provider", - TraceRawTotalCost: "trace.raw_total_cost", - TraceSpanCount: "trace.span_count", - TraceToolCallCount: "trace.tool_call_count", - UserAuthMethod: "user.auth_method", - UserAuthProvider: "user.auth_provider", - UserEmail: "user.email", - UserId: "user.id", - WebhookId: "webhook.id", - WebhookProvider: "webhook.provider", - WebhookTriggerSuccess: "webhook.trigger_success", - WorkflowBlockTypes: "workflow.block_types", - WorkflowBlocksCount: "workflow.blocks_count", - WorkflowCreatedId: "workflow.created_id", - WorkflowDurationMs: "workflow.duration_ms", - WorkflowEdgesCount: "workflow.edges_count", - WorkflowHasFolder: "workflow.has_folder", - WorkflowHasWorkspace: "workflow.has_workspace", - WorkflowId: "workflow.id", - WorkflowLoopsCount: "workflow.loops_count", - WorkflowName: "workflow.name", - WorkflowNewId: "workflow.new_id", - WorkflowParallelsCount: "workflow.parallels_count", - WorkflowSourceId: "workflow.source_id", - WorkflowTrigger: "workflow.trigger", - WorkspaceId: "workspace.id", - WorkspaceName: "workspace.name", -} as const; - -export type TraceAttrKey = keyof typeof TraceAttr; -export type TraceAttrValue = (typeof TraceAttr)[TraceAttrKey]; - -/** Readonly sorted list of every canonical custom attribute key. */ -export const TraceAttrValues: readonly TraceAttrValue[] = [ - "abort.backend", - "abort.found", - "abort.redis_result", - "analytics.aborted", - "analytics.billed_cache_read_cost", - "analytics.billed_cache_write_cost", - "analytics.billed_input_cost", - "analytics.billed_output_cost", - "analytics.billed_total_cost", - "analytics.cache_attempted_requests", - "analytics.cache_hit_requests", - "analytics.cache_read_tokens", - "analytics.cache_write_requests", - "analytics.cache_write_tokens", - "analytics.customer_type", - "analytics.duration_ms", - "analytics.error", - "analytics.input_tokens", - "analytics.model", - "analytics.output_tokens", - "analytics.provider", - "analytics.raw_cache_read_cost", - "analytics.raw_cache_write_cost", - "analytics.raw_input_cost", - "analytics.raw_output_cost", - "analytics.raw_total_cost", - "analytics.source", - "analytics.tool_call_count", - "api_key.id", - "api_key.name", - "auth.incoming_internal", - "auth.key.match", - "auth.key.preview", - "auth.key.source", - "auth.key.type", - "auth.provider", - "auth.validate.status_code", - "aws.region", - "billing.attempts", - "billing.byok", - "billing.change_type", - "billing.cost.input_usd", - "billing.cost.output_usd", - "billing.cost.total_usd", - "billing.cost_usd", - "billing.customer_type", - "billing.duplicate", - "billing.duration_ms", - "billing.has_idempotency_key", - "billing.idempotency_key", - "billing.interval", - "billing.is_mcp", - "billing.llm_cost", - "billing.long_context", - "billing.new_plan", - "billing.outcome", - "billing.plan", - "billing.previous_plan", - "billing.service_charges", - "billing.source", - "billing.total_cost", - "billing.usage.current", - "billing.usage.exceeded", - "billing.usage.limit", - "block.id", - "block.name", - "block.type", - "chat.active_messages_bytes", - "chat.active_messages_count", - "chat.append_bytes", - "chat.append_count", - "chat.artifact_keys", - "chat.artifacts_bytes", - "chat.auth_type", - "chat.blob.op", - "chat.blob.site", - "chat.blob_bytes_offloaded", - "chat.blob_bytes_resolved", - "chat.blobs_offloaded", - "chat.blobs_resolved", - "chat.context_count", - "chat.context_usage", - "chat.continuation.messages_before", - "chat.continuation.tool_result_bytes", - "chat.continuation.tool_result_failure", - "chat.continuation.tool_result_success", - "chat.continuation.tool_results", - "chat.continuation.total_tool_calls", - "chat.existing_message_count", - "chat.file_attachment_count", - "chat.finalize.outcome", - "chat.found", - "chat.has_assistant_message", - "chat.has_output_configs", - "chat.id", - "chat.message_bytes", - "chat.messages_after", - "chat.messages_bytes", - "chat.messages_count", - "chat.persist.outcome", - "chat.preexisting", - "chat.rollback_index", - "chat.tokens_used", - "chat.type", - "chat.user_message_id", - "checkpoint.age", - "checkpoint.attempts_bytes", - "checkpoint.bytes.assistant_tool_use", - "checkpoint.bytes.current_messages", - "checkpoint.bytes.immediate_results", - "checkpoint.bytes.pending_tool_calls", - "checkpoint.bytes.provider_request", - "checkpoint.bytes.request_context", - "checkpoint.bytes.tool_usage", - "checkpoint.cached_credentials_bytes", - "checkpoint.claimed", - "checkpoint.claimed_now", - "checkpoint.completed_bytes", - "checkpoint.completed_steps", - "checkpoint.current_messages", - "checkpoint.decisions_bytes", - "checkpoint.found", - "checkpoint.frames", - "checkpoint.id", - "checkpoint.immediate_results", - "checkpoint.message_id", - "checkpoint.pending_bytes", - "checkpoint.pending_steps", - "checkpoint.pending_tool_count", - "checkpoint.rows", - "checkpoint.task_id", - "checkpoint.total_tool_calls", - "checkpoint.workflow_snapshot_bytes", - "client.version", - "condition.id", - "condition.name", - "condition.result", - "context.reduce.budget_chars", - "context.reduce.budget_source", - "context.reduce.caller", - "context.reduce.did_reduce", - "context.reduce.input_chars", - "context.reduce.input_messages", - "context.reduce.outcome", - "context.reduce.output_chars", - "context.reduce.output_messages", - "context.reduced", - "context.summarize.input_chars", - "context.summarize.output_chars", - "context.transform.caller", - "context.transform.chars_in", - "context.transform.chars_out", - "context.transform.drop_count", - "context.transform.drops", - "context.transform.messages_in", - "context.transform.messages_out", - "context.transform.stage", - "copilot.abort.controller_fired", - "copilot.abort.go_marker_ok", - "copilot.abort.local_aborted", - "copilot.abort.marker_written", - "copilot.abort.outcome", - "copilot.abort.unknown_reason", - "copilot.async_subagent.agent", - "copilot.async_subagent.count", - "copilot.async_subagent.delta_chars", - "copilot.async_subagent.id", - "copilot.async_subagent.reminders", - "copilot.async_subagent.slept_ms", - "copilot.async_subagent.status", - "copilot.async_subagent.wait_mode", - "copilot.async_subagent.wake_reason", - "copilot.async_tool.claimed_by", - "copilot.async_tool.has_error", - "copilot.async_tool.ids_count", - "copilot.async_tool.permission_decision", - "copilot.async_tool.status", - "copilot.async_tool.worker_id", - "copilot.branch.kind", - "copilot.chat.is_new", - "copilot.checkpoint.pending_tool_call_id", - "copilot.commands.count", - "copilot.confirm.outcome", - "copilot.contexts.count", - "copilot.degraded.reason", - "copilot.execution.id", - "copilot.file_attachments.count", - "copilot.finalize.outcome", - "copilot.interrupted_prior_stream", - "copilot.leg", - "copilot.mode", - "copilot.operation", - "copilot.output_file.bytes", - "copilot.output_file.format", - "copilot.output_file.id", - "copilot.output_file.mime_type", - "copilot.output_file.mode", - "copilot.output_file.name", - "copilot.output_file.outcome", - "copilot.output_file.path", - "copilot.output_file.sandbox_path", - "copilot.pending_stream.wait_ms", - "copilot.prefetch", - "copilot.publisher.client_disconnected", - "copilot.publisher.saw_complete", - "copilot.recovery.latest_seq", - "copilot.recovery.oldest_seq", - "copilot.recovery.outcome", - "copilot.recovery.requested_after_seq", - "copilot.request.cancel_reason", - "copilot.request.outcome", - "copilot.request.will_retry_on_stream_error", - "copilot.resource_attachments.count", - "copilot.resources.aborted", - "copilot.resources.op", - "copilot.resources.removed_count", - "copilot.resources.upserted_count", - "copilot.result.content_blocks", - "copilot.result.content_length", - "copilot.result.tool_calls", - "copilot.resume.after_cursor", - "copilot.resume.duration_ms", - "copilot.resume.event_count", - "copilot.resume.outcome", - "copilot.resume.poll_iterations", - "copilot.resume.preview_session_count", - "copilot.route", - "copilot.run.agent", - "copilot.run.has_completed_at", - "copilot.run.has_error", - "copilot.run.model", - "copilot.run.parent_id", - "copilot.run.provider", - "copilot.run.status", - "copilot.sse.bytes_received", - "copilot.sse.chunks_received", - "copilot.sse.close_reason", - "copilot.sse.events.complete", - "copilot.sse.events.error", - "copilot.sse.events.received", - "copilot.sse.events.resource", - "copilot.sse.events.run", - "copilot.sse.events.session", - "copilot.sse.events.span", - "copilot.sse.events.text", - "copilot.sse.events.tool", - "copilot.sse.expected_terminal", - "copilot.sse.first_event_ms", - "copilot.sse.longest_dispatch_ms", - "copilot.sse.longest_inbound_gap_ms", - "copilot.sse.terminal_event_missing", - "copilot.sse.terminal_event_seen", - "copilot.sse.total_dispatch_ms", - "copilot.steer.outcome", - "copilot.steering.content_chars", - "copilot.steering.entries", - "copilot.steering.mode", - "copilot.steering.salvaged_chars", - "copilot.stop.appended_assistant", - "copilot.stop.blocks_count", - "copilot.stop.content_length", - "copilot.stop.outcome", - "copilot.stream", - "copilot.surface", - "copilot.table.id", - "copilot.table.outcome", - "copilot.table.row_count", - "copilot.table.source.content_bytes", - "copilot.table.source.format", - "copilot.table.source.path", - "copilot.trace.span_count", - "copilot.transport", - "copilot.user.message_preview", - "copilot.validate.outcome", - "copilot.vfs.delta.outcome", - "copilot.vfs.file.extension", - "copilot.vfs.file.media_type", - "copilot.vfs.file.name", - "copilot.vfs.file.size_bytes", - "copilot.vfs.has_alpha", - "copilot.vfs.input.bytes", - "copilot.vfs.input.height", - "copilot.vfs.input.media_type_claimed", - "copilot.vfs.input.media_type_detected", - "copilot.vfs.input.width", - "copilot.vfs.materialize.file_count", - "copilot.vfs.materialize.phase_ms", - "copilot.vfs.metadata.failed", - "copilot.vfs.outcome", - "copilot.vfs.output.bytes", - "copilot.vfs.output.media_type", - "copilot.vfs.phase", - "copilot.vfs.read.image.resized", - "copilot.vfs.read.outcome", - "copilot.vfs.read.output.bytes", - "copilot.vfs.read.output.lines", - "copilot.vfs.read.output.media_type", - "copilot.vfs.read.path", - "copilot.vfs.resize.attempts", - "copilot.vfs.resize.chosen_dimension", - "copilot.vfs.resize.chosen_quality", - "copilot.vfs.resize.dimension", - "copilot.vfs.resize.fits_budget", - "copilot.vfs.resize.output_bytes", - "copilot.vfs.resize.quality", - "copilot.vfs.resized", - "copilot.vfs.sharp.load_failed", - "cost.default_cost", - "credential_set.id", - "credential_set.name", - "db.operation", - "db.sql.table", - "db.system", - "deployment.environment", - "deployment.version", - "document.file_size", - "document.mime_type", - "documents.count", - "documents.upload_type", - "error", - "error.code", - "error.internal", - "error.message", - "error.type", - "event.name", - "event.timestamp", - "execution.blocks_executed", - "execution.duration_ms", - "execution.error_message", - "execution.has_errors", - "execution.status", - "execution.total_cost", - "execution.trigger", - "function.execution_time_ms", - "function.id", - "function.name", - "gen_ai.agent.id", - "gen_ai.agent.name", - "gen_ai.cache.outcome", - "gen_ai.cache.token.type", - "gen_ai.cost.input", - "gen_ai.cost.output", - "gen_ai.cost.total", - "gen_ai.input.messages", - "gen_ai.operation.name", - "gen_ai.output.messages", - "gen_ai.provider.name", - "gen_ai.request.assistant_messages", - "gen_ai.request.cacheable_system_blocks", - "gen_ai.request.content_blocks", - "gen_ai.request.has_cache_control", - "gen_ai.request.image_blocks", - "gen_ai.request.image_data_bytes", - "gen_ai.request.max_message_blocks", - "gen_ai.request.messages.count", - "gen_ai.request.model", - "gen_ai.request.prompt_cache.breakpoint.created", - "gen_ai.request.prompt_cache.breakpoint.dynamic_write_suppress_reason", - "gen_ai.request.prompt_cache.breakpoint.dynamic_write_suppressed", - "gen_ai.request.prompt_cache.breakpoint.eligible", - "gen_ai.request.prompt_cache.breakpoint.kind", - "gen_ai.request.prompt_cache.breakpoint.minimum_tokens", - "gen_ai.request.prompt_cache.breakpoint.prefix_tokens_estimated", - "gen_ai.request.prompt_cache.breakpoint.previous_boundary_tokens", - "gen_ai.request.prompt_cache.breakpoint.section", - "gen_ai.request.prompt_cache.breakpoint.skip_reason", - "gen_ai.request.prompt_cache.breakpoint.target_index", - "gen_ai.request.prompt_cache.breakpoint.target_role", - "gen_ai.request.prompt_cache.breakpoint.target_type", - "gen_ai.request.prompt_cache.breakpoint.threshold_tokens", - "gen_ai.request.prompt_cache.breakpoint.ttl", - "gen_ai.request.prompt_cache.breakpoints.count", - "gen_ai.request.prompt_cache.breakpoints.created", - "gen_ai.request.prompt_cache.dynamic.minimum_tokens", - "gen_ai.request.prompt_cache.dynamic.prefix_tokens_estimated", - "gen_ai.request.prompt_cache.dynamic.previous_boundary_tokens", - "gen_ai.request.prompt_cache.dynamic.skip_reason", - "gen_ai.request.prompt_cache.dynamic.target_type", - "gen_ai.request.prompt_cache.dynamic.threshold_tokens", - "gen_ai.request.prompt_cache.dynamic.ttl", - "gen_ai.request.prompt_cache.dynamic_user_eligible", - "gen_ai.request.prompt_cache.dynamic_user_skip_reason", - "gen_ai.request.prompt_cache.static.ttl", - "gen_ai.request.prompt_cache.static_system", - "gen_ai.request.prompt_cache.static_tools", - "gen_ai.request.runtime_context_chars", - "gen_ai.request.runtime_context_messages", - "gen_ai.request.system_blocks", - "gen_ai.request.system_chars", - "gen_ai.request.text_blocks", - "gen_ai.request.tool_result_blocks", - "gen_ai.request.tool_use_blocks", - "gen_ai.request.tools.count", - "gen_ai.request.user_messages", - "gen_ai.response.finish_reasons", - "gen_ai.response.model", - "gen_ai.stream.phase.text.bytes", - "gen_ai.stream.phase.text.chunks", - "gen_ai.stream.phase.text.first_ms", - "gen_ai.stream.phase.text.ms", - "gen_ai.stream.phase.thinking.bytes", - "gen_ai.stream.phase.thinking.chunks", - "gen_ai.stream.phase.thinking.first_ms", - "gen_ai.stream.phase.thinking.ms", - "gen_ai.stream.phase.tool_args.bytes", - "gen_ai.stream.phase.tool_args.chunks", - "gen_ai.stream.phase.tool_args.first_ms", - "gen_ai.stream.phase.tool_args.ms", - "gen_ai.system", - "gen_ai.thinking.blocks_captured", - "gen_ai.thinking.redacted_blocks", - "gen_ai.token.type", - "gen_ai.tool.name", - "gen_ai.usage.cache_creation.input_tokens", - "gen_ai.usage.cache_creation_tokens", - "gen_ai.usage.cache_read.input_tokens", - "gen_ai.usage.cache_read_tokens", - "gen_ai.usage.context_tokens", - "gen_ai.usage.input_tokens", - "gen_ai.usage.output_tokens", - "gen_ai.usage.thinking_tokens", - "gen_ai.usage.total_tokens", - "gen_ai.workflow.id", - "gen_ai.workflow.name", - "hosted_key.env_var", - "http.host", - "http.method", - "http.path", - "http.remote_addr", - "http.request.content_length", - "http.response.body_bytes", - "http.response.content_length", - "http.response.headers_ms", - "http.response.total_ms", - "http.route", - "http.server.duration_ms", - "http.status_code", - "http.target", - "http.url", - "http.user_agent", - "invitation.role", - "llm.backend", - "llm.compaction.pause", - "llm.error_stage", - "llm.protocol", - "llm.request.body_bytes", - "llm.request.compaction_trigger", - "llm.stream.bytes", - "llm.stream.chunks", - "llm.stream.first_chunk_bytes", - "llm.stream.first_chunk_ms", - "llm.stream.first_token_ms", - "llm.stream.open_ms", - "llm.stream.total_ms", - "lock.acquired", - "lock.backend", - "lock.timed_out", - "lock.timeout_ms", - "loop.id", - "loop.iterations", - "loop.name", - "manage_knowledge_base.id", - "manage_knowledge_base.name", - "mcp.execution_status", - "mcp.server_id", - "mcp.server_name", - "mcp.tool_name", - "mcp.transport", - "member.role", - "memory.content_bytes", - "memory.found", - "memory.path", - "memory.row_count", - "message.id", - "messages.deserialize_ms", - "messages.serialize.op", - "messages.serialize.site", - "messages.serialize_ms", - "messaging.destination.name", - "messaging.system", - "model.duration_ms", - "model.id", - "model.name", - "mothership.origin", - "net.peer.name", - "oauth.provider", - "parallel.branches", - "parallel.id", - "parallel.name", - "prefs.tool_count", - "processing.chunk_size", - "processing.recipe", - "prompt.cacheable_blocks", - "prompt.component", - "prompt.region.files_chars", - "prompt.region.files_count", - "prompt.region.history_chars", - "prompt.region.history_messages", - "prompt.region.runtime_tail_chars", - "prompt.region.steering_chars", - "prompt.region.tool_results_chars", - "prompt.region.vfs_chars", - "prompt.region.vfs_messages", - "prompt.runtime.active_skills_chars", - "prompt.runtime.tagged_resources_chars", - "prompt.section.agent_guidance_chars", - "prompt.section.capabilities_chars", - "prompt.section.credentials_chars", - "prompt.section.override_chars", - "prompt.section.persona_chars", - "prompt.section.policies_chars", - "prompt.section.skills_index_chars", - "prompt.section.spawn_context_chars", - "prompt.section.subagent_docs_chars", - "prompt.section.tool_docs_chars", - "prompt.section.workspace_guide_chars", - "prompt.section.workspace_inventory_chars", - "prompt.set", - "prompt.site", - "prompt.system_blocks", - "prompt.system_chars", - "prompt.tools.wire_chars", - "prompt.tools.wire_count", - "provider.id", - "rate_limit.attempt", - "rate_limit.count", - "rate_limit.delay_ms", - "rate_limit.limit", - "rate_limit.max_retries", - "rate_limit.outcome", - "rate_limit.retry_after_ms", - "request.go_trace_id", - "request.id", - "required.version", - "resume.request.body_bytes", - "resume.results.count", - "resume.results.data_bytes", - "resume.results.failure_count", - "resume.results.success_count", - "router.backend_name", - "router.config_version", - "router.id", - "router.name", - "router.route_reason", - "router.selected_backend", - "router.selected_path", - "run.id", - "search.results_count", - "server.address", - "service.instance.id", - "service.name", - "service.namespace", - "service.version", - "settle.completed", - "settle.timeout_ms", - "settle.wait_ms", - "sim.operation", - "sim.request_id", - "span.duration_ms", - "span.status", - "span.type", - "stream.id", - "subagent.depth", - "subagent.escalation.applied", - "subagent.escalation.prior_calls", - "subagent.escalation.requested", - "subagent.execution.phase", - "subagent.id", - "subagent.nested", - "subagent.outcome.content_bytes", - "subagent.outcome.error", - "subagent.outcome.first_chunk_ms", - "subagent.outcome.llm_rounds", - "subagent.outcome.structured_type", - "subagent.outcome.success", - "subagent.outcome.tool_call_count", - "subagent.parent_agent_id", - "subagent.route.manifest_missing", - "subagent.session.id", - "subagent.session.operation", - "subagent.session.persistence_outcome", - "subagent.session.revision", - "subagent.session.run_id", - "subagent.session.title", - "subagent.tool_call_id", - "task.age", - "task.decision_count", - "task.error_count", - "task.found", - "task.id", - "task.list_limit", - "task.rows", - "task.status", - "task.step_count", - "telemetry.sdk.language", - "telemetry.sdk.name", - "telemetry.sdk.version", - "template.id", - "template.name", - "throttle.reason", - "tool.args.bytes", - "tool.args.count", - "tool.args.preview", - "tool.async_waiter.poll_count", - "tool.async_waiter.pubsub_deliveries", - "tool.async_waiter.resolution", - "tool.call_id", - "tool.client.count", - "tool.client_executable", - "tool.completion.received", - "tool.confirmation.status", - "tool.deferred.count", - "tool.duration_ms", - "tool.error_kind", - "tool.executor", - "tool.external.service", - "tool.id", - "tool.loaded.count", - "tool.name", - "tool.outcome", - "tool.outcome.message", - "tool.parent_span", - "tool.payload.bytes", - "tool.result.artifact", - "tool.result.bytes", - "tool.result.success", - "tool.scheduled", - "tool.status", - "tool.status_code", - "tool.store_status", - "tool.sync", - "tool.timeout_ms", - "tool.visible.count", - "tool.visible.names", - "trace.aborted", - "trace.billed_total_cost", - "trace.cache_attempted_requests", - "trace.cache_hit_requests", - "trace.cache_read_tokens", - "trace.cache_write_requests", - "trace.cache_write_tokens", - "trace.duration_ms", - "trace.error", - "trace.go_id", - "trace.input_tokens", - "trace.model", - "trace.outcome", - "trace.output_tokens", - "trace.provider", - "trace.raw_total_cost", - "trace.span_count", - "trace.tool_call_count", - "user.auth_method", - "user.auth_provider", - "user.email", - "user.id", - "webhook.id", - "webhook.provider", - "webhook.trigger_success", - "workflow.block_types", - "workflow.blocks_count", - "workflow.created_id", - "workflow.duration_ms", - "workflow.edges_count", - "workflow.has_folder", - "workflow.has_workspace", - "workflow.id", - "workflow.loops_count", - "workflow.name", - "workflow.new_id", - "workflow.parallels_count", - "workflow.source_id", - "workflow.trigger", - "workspace.id", - "workspace.name", -] as const; diff --git a/apps/sim/lib/mothership/generated/trace-events-v1.ts b/apps/sim/lib/mothership/generated/trace-events-v1.ts deleted file mode 100644 index c94f4994824..00000000000 --- a/apps/sim/lib/mothership/generated/trace-events-v1.ts +++ /dev/null @@ -1,51 +0,0 @@ -// GENERATED — do not edit. Source of truth: mothership worker packages/contracts/src/trace-events-v1.ts -// Regenerate with `bun run contracts:sync` in the worker. - -// Canonical mothership OTel span event names Sim emits. Call sites reference -// `TraceEvent.` rather than raw strings so typos are compile errors. - -export const TraceEvent = { - ContextTransform: "context.transform", - CopilotDbTransactionBodyComplete: "copilot.db.transaction.body_complete", - CopilotDbTransactionReady: "copilot.db.transaction.ready", - CopilotOutputFileError: "copilot.output_file.error", - CopilotSseFirstEvent: "copilot.sse.first_event", - CopilotSseIdleGapExceeded: "copilot.sse.idle_gap_exceeded", - CopilotSseTerminalEventReceived: "copilot.sse.terminal_event_received", - CopilotTableError: "copilot.table.error", - CopilotVfsParseFailed: "copilot.vfs.parse_failed", - CopilotVfsResizeAttempt: "copilot.vfs.resize_attempt", - CopilotVfsResizeAttemptFailed: "copilot.vfs.resize_attempt_failed", - GenAiPromptCacheBreakpoint: "gen_ai.prompt_cache.breakpoint", - LlmInvokeSent: "llm.invoke.sent", - LlmStreamFirstChunk: "llm.stream.first_chunk", - LlmStreamOpened: "llm.stream.opened", - PgNotifyFailed: "pg_notify_failed", - RedisSubscribed: "redis.subscribed", - RequestCancelled: "request.cancelled", -} as const; - -export type TraceEventKey = keyof typeof TraceEvent; -export type TraceEventValue = (typeof TraceEvent)[TraceEventKey]; - -/** Readonly sorted list of every canonical event name. */ -export const TraceEventValues: readonly TraceEventValue[] = [ - "context.transform", - "copilot.db.transaction.body_complete", - "copilot.db.transaction.ready", - "copilot.output_file.error", - "copilot.sse.first_event", - "copilot.sse.idle_gap_exceeded", - "copilot.sse.terminal_event_received", - "copilot.table.error", - "copilot.vfs.parse_failed", - "copilot.vfs.resize_attempt", - "copilot.vfs.resize_attempt_failed", - "gen_ai.prompt_cache.breakpoint", - "llm.invoke.sent", - "llm.stream.first_chunk", - "llm.stream.opened", - "pg_notify_failed", - "redis.subscribed", - "request.cancelled", -] as const; diff --git a/apps/sim/lib/mothership/generated/trace-spans-v1.ts b/apps/sim/lib/mothership/generated/trace-spans-v1.ts deleted file mode 100644 index 642b3a30a52..00000000000 --- a/apps/sim/lib/mothership/generated/trace-spans-v1.ts +++ /dev/null @@ -1,190 +0,0 @@ -// GENERATED — do not edit. Source of truth: mothership worker packages/contracts/src/trace-spans-v1.ts -// Regenerate with `bun run contracts:sync` in the worker. - -// Canonical mothership OTel span names Sim emits. Call sites reference -// `TraceSpan.` rather than raw strings so typos are compile errors. The -// worker's own span names live in telemetry.ts. - -export const TraceSpan = { - AsyncToolStoreSet: "async_tool_store.set", - AuthRateLimitRecord: "auth.rate_limit.record", - AuthValidateKey: "auth.validate_key", - ChatAsyncSubagentRun: "chat.async_subagent.run", - ChatAsyncSubagentShutdown: "chat.async_subagent.shutdown", - ChatAsyncSubagentSpawn: "chat.async_subagent.spawn", - ChatContinueWithToolResults: "chat.continue_with_tool_results", - ChatExplicitAbortConsume: "chat.explicit_abort.consume", - ChatExplicitAbortFlushPausedBilling: "chat.explicit_abort.flush_paused_billing", - ChatExplicitAbortHandle: "chat.explicit_abort.handle", - ChatExplicitAbortMark: "chat.explicit_abort.mark", - ChatExplicitAbortPeek: "chat.explicit_abort.peek", - ChatGateAcquire: "chat.gate.acquire", - ChatOrchestrateInterrupt: "chat.orchestrate.interrupt", - ChatOrchestrateSteer: "chat.orchestrate.steer", - ChatOrchestrateTail: "chat.orchestrate.tail", - ChatOrchestrateWait: "chat.orchestrate.wait", - ChatPersistAfterDone: "chat.persist_after_done", - ChatSetup: "chat.setup", - ContextReduce: "context.reduce", - ContextSummarizeChunk: "context.summarize_chunk", - CopilotAnalyticsFlush: "copilot.analytics.flush", - CopilotAnalyticsSaveRequest: "copilot.analytics.save_request", - CopilotAnalyticsUpdateBilling: "copilot.analytics.update_billing", - CopilotAsyncRunsClaimCompleted: "copilot.async_runs.claim_completed", - CopilotAsyncRunsCreateRunCheckpoint: "copilot.async_runs.create_run_checkpoint", - CopilotAsyncRunsCreateRunSegment: "copilot.async_runs.create_run_segment", - CopilotAsyncRunsGetAsyncToolCall: "copilot.async_runs.get_async_tool_call", - CopilotAsyncRunsGetLatestForExecution: "copilot.async_runs.get_latest_for_execution", - CopilotAsyncRunsGetLatestForStream: "copilot.async_runs.get_latest_for_stream", - CopilotAsyncRunsGetMany: "copilot.async_runs.get_many", - CopilotAsyncRunsGetRunSegment: "copilot.async_runs.get_run_segment", - CopilotAsyncRunsListForRun: "copilot.async_runs.list_for_run", - CopilotAsyncRunsMarkAsyncToolStatus: "copilot.async_runs.mark_async_tool_status", - CopilotAsyncRunsReleaseClaim: "copilot.async_runs.release_claim", - CopilotAsyncRunsUpdateRunStatus: "copilot.async_runs.update_run_status", - CopilotAsyncRunsUpsertAsyncToolCall: "copilot.async_runs.upsert_async_tool_call", - CopilotAuthValidateApiKey: "copilot.auth.validate_api_key", - CopilotBillingUpdateCost: "copilot.billing.update_cost", - CopilotChatAbortActiveStream: "copilot.chat.abort_active_stream", - CopilotChatAbortStream: "copilot.chat.abort_stream", - CopilotChatAbortWaitSettle: "copilot.chat.abort_wait_settle", - CopilotChatAcquirePendingStreamLock: "copilot.chat.acquire_pending_stream_lock", - CopilotChatBuildExecutionContext: "copilot.chat.build_execution_context", - CopilotChatBuildPayload: "copilot.chat.build_payload", - CopilotChatBuildWorkspaceContext: "copilot.chat.build_workspace_context", - CopilotChatFinalizeAssistantTurn: "copilot.chat.finalize_assistant_turn", - CopilotChatPersistUserMessage: "copilot.chat.persist_user_message", - CopilotChatResolveAgentContexts: "copilot.chat.resolve_agent_contexts", - CopilotChatResolveBranch: "copilot.chat.resolve_branch", - CopilotChatResolveOrCreateChat: "copilot.chat.resolve_or_create_chat", - CopilotChatSteerStream: "copilot.chat.steer_stream", - CopilotChatStopStream: "copilot.chat.stop_stream", - CopilotCliCurate: "copilot.cli.curate", - CopilotCliHttpHeaders: "copilot.cli.http_headers", - CopilotCliIdentity: "copilot.cli.identity", - CopilotCliInvoke: "copilot.cli.invoke", - CopilotCliSink: "copilot.cli.sink", - CopilotConfirmToolResult: "copilot.confirm.tool_result", - CopilotDbQuery: "copilot.db.query", - CopilotDbTransaction: "copilot.db.transaction", - CopilotFinalizeStream: "copilot.finalize_stream", - CopilotRecoveryCheckReplayGap: "copilot.recovery.check_replay_gap", - CopilotResumeRequest: "copilot.resume.request", - CopilotSseReadLoop: "copilot.sse.read_loop", - CopilotSubagentExecute: "copilot.subagent.execute", - CopilotToolResultCommit: "copilot.tool.result_commit", - CopilotToolRuntime: "copilot.tool.runtime", - CopilotToolWaitForClientResult: "copilot.tool.wait_for_client_result", - CopilotToolWaitForPermission: "copilot.tool.wait_for_permission", - CopilotToolPermissionDecide: "copilot.tool_permission.decide", - CopilotToolsExecuteInband: "copilot.tools.execute_inband", - CopilotToolsHandleResourceSideEffects: "copilot.tools.handle_resource_side_effects", - CopilotToolsWriteCsvToTable: "copilot.tools.write_csv_to_table", - CopilotToolsWriteOutputFile: "copilot.tools.write_output_file", - CopilotToolsWriteOutputTable: "copilot.tools.write_output_table", - CopilotVfsMaterialize: "copilot.vfs.materialize", - CopilotVfsPrepareImage: "copilot.vfs.prepare_image", - CopilotVfsReadFile: "copilot.vfs.read_file", - GenAiAgentExecute: "gen_ai.agent.execute", - LlmStream: "llm.stream", - ProviderRouterRoute: "provider.router.route", - SimExecuteTool: "sim.execute_tool", - SimUpdateCost: "sim.update_cost", - SimValidateApiKey: "sim.validate_api_key", - ToolAsyncWaiterWait: "tool.async_waiter.wait", - ToolExecute: "tool.execute", -} as const; - -export type TraceSpanKey = keyof typeof TraceSpan; -export type TraceSpanValue = (typeof TraceSpan)[TraceSpanKey]; - -/** Readonly sorted list of every canonical span name. */ -export const TraceSpanValues: readonly TraceSpanValue[] = [ - "async_tool_store.set", - "auth.rate_limit.record", - "auth.validate_key", - "chat.async_subagent.run", - "chat.async_subagent.shutdown", - "chat.async_subagent.spawn", - "chat.continue_with_tool_results", - "chat.explicit_abort.consume", - "chat.explicit_abort.flush_paused_billing", - "chat.explicit_abort.handle", - "chat.explicit_abort.mark", - "chat.explicit_abort.peek", - "chat.gate.acquire", - "chat.orchestrate.interrupt", - "chat.orchestrate.steer", - "chat.orchestrate.tail", - "chat.orchestrate.wait", - "chat.persist_after_done", - "chat.setup", - "context.reduce", - "context.summarize_chunk", - "copilot.analytics.flush", - "copilot.analytics.save_request", - "copilot.analytics.update_billing", - "copilot.async_runs.claim_completed", - "copilot.async_runs.create_run_checkpoint", - "copilot.async_runs.create_run_segment", - "copilot.async_runs.get_async_tool_call", - "copilot.async_runs.get_latest_for_execution", - "copilot.async_runs.get_latest_for_stream", - "copilot.async_runs.get_many", - "copilot.async_runs.get_run_segment", - "copilot.async_runs.list_for_run", - "copilot.async_runs.mark_async_tool_status", - "copilot.async_runs.release_claim", - "copilot.async_runs.update_run_status", - "copilot.async_runs.upsert_async_tool_call", - "copilot.auth.validate_api_key", - "copilot.billing.update_cost", - "copilot.chat.abort_active_stream", - "copilot.chat.abort_stream", - "copilot.chat.abort_wait_settle", - "copilot.chat.acquire_pending_stream_lock", - "copilot.chat.build_execution_context", - "copilot.chat.build_payload", - "copilot.chat.build_workspace_context", - "copilot.chat.finalize_assistant_turn", - "copilot.chat.persist_user_message", - "copilot.chat.resolve_agent_contexts", - "copilot.chat.resolve_branch", - "copilot.chat.resolve_or_create_chat", - "copilot.chat.steer_stream", - "copilot.chat.stop_stream", - "copilot.cli.curate", - "copilot.cli.http_headers", - "copilot.cli.identity", - "copilot.cli.invoke", - "copilot.cli.sink", - "copilot.confirm.tool_result", - "copilot.db.query", - "copilot.db.transaction", - "copilot.finalize_stream", - "copilot.recovery.check_replay_gap", - "copilot.resume.request", - "copilot.sse.read_loop", - "copilot.subagent.execute", - "copilot.tool.result_commit", - "copilot.tool.runtime", - "copilot.tool.wait_for_client_result", - "copilot.tool.wait_for_permission", - "copilot.tool_permission.decide", - "copilot.tools.execute_inband", - "copilot.tools.handle_resource_side_effects", - "copilot.tools.write_csv_to_table", - "copilot.tools.write_output_file", - "copilot.tools.write_output_table", - "copilot.vfs.materialize", - "copilot.vfs.prepare_image", - "copilot.vfs.read_file", - "gen_ai.agent.execute", - "llm.stream", - "provider.router.route", - "sim.execute_tool", - "sim.update_cost", - "sim.validate_api_key", - "tool.async_waiter.wait", - "tool.execute", -] as const; diff --git a/apps/sim/lib/mothership/inbox/executor.ts b/apps/sim/lib/mothership/inbox/executor.ts index 65f960a88d5..fa120085412 100644 --- a/apps/sim/lib/mothership/inbox/executor.ts +++ b/apps/sim/lib/mothership/inbox/executor.ts @@ -267,7 +267,7 @@ export async function executeInboxTask(taskId: string): Promise { userId, workspaceId: ws.id, chatId: chatId ?? undefined, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', autoExecuteTools: true, interactive: false, billingAttribution, diff --git a/apps/sim/lib/mothership/memories/cleanup-outbox.test.ts b/apps/sim/lib/mothership/memories/cleanup-outbox.test.ts index 25c1da8673b..1d5ac3c504c 100644 --- a/apps/sim/lib/mothership/memories/cleanup-outbox.test.ts +++ b/apps/sim/lib/mothership/memories/cleanup-outbox.test.ts @@ -3,13 +3,13 @@ */ import { resetEnvMock, setEnv } from '@sim/testing/mocks/env.mock' import { mothershipAgentUrlMock } from '@sim/testing/mocks/mothership-agent-url.mock' -import { mothershipGoFetchMock } from '@sim/testing/mocks/mothership-go-fetch.mock' +import { mothershipWorkerFetchMock } from '@sim/testing/mocks/mothership-worker-fetch.mock' import { outboxServiceMock, outboxServiceMockFns } from '@sim/testing/mocks/outbox-service.mock' import { beforeEach, describe, expect, it, vi } from 'vitest' vi.mock('@/lib/core/outbox/service', () => outboxServiceMock) vi.mock('@/lib/mothership/constants', () => ({ SIM_AGENT_API_URL: 'https://agent.example.test' })) -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/request/headers', () => ({ mothershipRequestHeaders: () => ({}) })) vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) diff --git a/apps/sim/lib/mothership/memories/cleanup-outbox.ts b/apps/sim/lib/mothership/memories/cleanup-outbox.ts index cd7fa5e3037..c4ffecb23ec 100644 --- a/apps/sim/lib/mothership/memories/cleanup-outbox.ts +++ b/apps/sim/lib/mothership/memories/cleanup-outbox.ts @@ -16,8 +16,8 @@ import { MemoryCleanupResult, } from '@/lib/mothership/generated/memory-notes-access' import { MEMORY_CLEANUP_EVENT } from '@/lib/mothership/memories/cleanup-event' -import { fetchGo } from '@/lib/mothership/request/go/fetch' import { mothershipRequestHeaders } from '@/lib/mothership/request/headers' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' import { getMothershipSourceEnvHeaders, MOTHERSHIP_SOURCE_ENV_HEADER, @@ -106,7 +106,7 @@ export const memoryCleanupOutboxHandlers = { baseUrl: payload.baseUrl, }) const sourceEnv = payload.sourceHeaders[MOTHERSHIP_SOURCE_ENV_HEADER] - const response = await fetchGo(`${payload.baseUrl}/api/memories/cleanup`, { + const response = await fetchWorker(`${payload.baseUrl}/api/memories/cleanup`, { method: 'POST', headers: { ...omit(mothershipRequestHeaders(), [MOTHERSHIP_SOURCE_ENV_HEADER]), diff --git a/apps/sim/lib/mothership/observability/database.ts b/apps/sim/lib/mothership/observability/database.ts index a9d84d49550..1dba550068c 100644 --- a/apps/sim/lib/mothership/observability/database.ts +++ b/apps/sim/lib/mothership/observability/database.ts @@ -1,8 +1,8 @@ import { SpanKind, SpanStatusCode, trace } from '@opentelemetry/api' import { db } from '@sim/db' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceEvent } from '@/lib/mothership/generated/trace-events-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceEvent } from '@/lib/mothership/observability/trace-events' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' type Transaction = Parameters[0]>[0] diff --git a/apps/sim/lib/mothership/observability/metric-names.ts b/apps/sim/lib/mothership/observability/metric-names.ts new file mode 100644 index 00000000000..d26fe095e38 --- /dev/null +++ b/apps/sim/lib/mothership/observability/metric-names.ts @@ -0,0 +1,10 @@ +// Sim-owned: edit here; the mothership worker does not sync this file. + +// Canonical mothership OTel metric names Sim emits. Names only: label keys and histogram +// buckets are the emitter's. + +export const Metric = { + CopilotDegradedCount: 'copilot.degraded.count', + CopilotToolCalls: 'copilot.tool.calls', + CopilotToolDuration: 'copilot.tool.duration', +} as const diff --git a/apps/sim/lib/mothership/observability/trace-attribute-values.ts b/apps/sim/lib/mothership/observability/trace-attribute-values.ts new file mode 100644 index 00000000000..830bcffa42f --- /dev/null +++ b/apps/sim/lib/mothership/observability/trace-attribute-values.ts @@ -0,0 +1,154 @@ +// Sim-owned: edit here; the mothership worker does not sync this file. + +// Closed-set value vocabularies for the attributes in trace-attributes.ts. Call sites +// reference e.g. `CopilotRequestCancelReason.ExplicitStop` rather than raw strings. + +export const AbortBackend = { + InProcess: 'in_process', + Redis: 'redis', +} as const + +export const BillingRouteOutcome = { + AuthFailed: 'auth_failed', + Billed: 'billed', + BillingDisabled: 'billing_disabled', + DuplicateIdempotencyKey: 'duplicate_idempotency_key', + InternalError: 'internal_error', + InvalidBody: 'invalid_body', + ReconciliationRequired: 'reconciliation_required', +} as const + +export const CopilotBranchKind = { + Organization: 'organization', + Workflow: 'workflow', + Workspace: 'workspace', +} as const + +export const CopilotChatFinalizeOutcome = { + AppendedAssistant: 'appended_assistant', + AssistantAlreadyPersisted: 'assistant_already_persisted', + ClearedStreamMarkerOnly: 'cleared_stream_marker_only', + StaleUserMessage: 'stale_user_message', +} as const + +export const CopilotConfirmOutcome = { + Delivered: 'delivered', + Forbidden: 'forbidden', + InternalError: 'internal_error', + RunNotFound: 'run_not_found', + ToolCallNotFound: 'tool_call_not_found', + Unauthorized: 'unauthorized', + UpdateFailed: 'update_failed', + ValidationError: 'validation_error', +} as const + +export const CopilotDegradedReason = { + BindingRejected: 'binding_rejected', + ClientPickupTimeout: 'client_pickup_timeout', + MissingToolResult: 'missing_tool_result', + StreamDeadBeforeDispatch: 'stream_dead_before_dispatch', +} as const + +export type CopilotDegradedReasonValue = + (typeof CopilotDegradedReason)[keyof typeof CopilotDegradedReason] + +export const CopilotFinalizeOutcome = { + Aborted: 'aborted', + Error: 'error', + Success: 'success', +} as const + +export const CopilotLeg = { + SimToWorker: 'sim_to_worker', +} as const + +export const CopilotOutputFileOutcome = { + Failed: 'failed', + Uploaded: 'uploaded', +} as const + +export const CopilotRecoveryOutcome = { + GapDetected: 'gap_detected', + InRange: 'in_range', +} as const + +export const CopilotRequestCancelReason = { + ExplicitStop: 'explicit_stop', + Unknown: 'unknown', +} as const + +export type CopilotRequestCancelReasonValue = + (typeof CopilotRequestCancelReason)[keyof typeof CopilotRequestCancelReason] + +export const CopilotResourcesOp = { + Delete: 'delete', + None: 'none', + Upsert: 'upsert', +} as const + +export const CopilotResumeOutcome = { + BatchDelivered: 'batch_delivered', + ClientDisconnected: 'client_disconnected', + EndedWithoutTerminal: 'ended_without_terminal', + StreamNotFound: 'stream_not_found', + TerminalDelivered: 'terminal_delivered', +} as const + +export const CopilotSseCloseReason = { + Aborted: 'aborted', + BackendError: 'backend_error', + BillingLimit: 'billing_limit', + ClosedNoTerminal: 'closed_no_terminal', + Error: 'error', + Terminal: 'terminal', + Timeout: 'timeout', +} as const + +export const CopilotStopOutcome = { + ChatNotFound: 'chat_not_found', + InternalError: 'internal_error', + NoMatchingRow: 'no_matching_row', + Persisted: 'persisted', + Unauthorized: 'unauthorized', + ValidationError: 'validation_error', +} as const + +export const CopilotSurface = { + Copilot: 'copilot', + Mothership: 'mothership', +} as const + +export const CopilotTableOutcome = { + EmptyContent: 'empty_content', + EmptyRows: 'empty_rows', + Failed: 'failed', + Imported: 'imported', + InvalidJsonShape: 'invalid_json_shape', + InvalidShape: 'invalid_shape', + RowLimitExceeded: 'row_limit_exceeded', + Wrote: 'wrote', +} as const + +export const CopilotTableSourceFormat = { + Csv: 'csv', + Json: 'json', +} as const + +export const CopilotTransport = { + Batch: 'batch', + Headless: 'headless', + Stream: 'stream', +} as const + +export const CopilotValidateOutcome = { + InternalAuthFailed: 'internal_auth_failed', + InternalError: 'internal_error', + InvalidBody: 'invalid_body', + Ok: 'ok', + UsageExceeded: 'usage_exceeded', + UserNotFound: 'user_not_found', +} as const + +export const ToolExecutor = { + Sim: 'sim', +} as const diff --git a/apps/sim/lib/mothership/observability/trace-attributes.ts b/apps/sim/lib/mothership/observability/trace-attributes.ts new file mode 100644 index 00000000000..def75de8727 --- /dev/null +++ b/apps/sim/lib/mothership/observability/trace-attributes.ts @@ -0,0 +1,260 @@ +// Sim-owned: edit here; the mothership worker does not sync this file. + +// OTel span, metric-label and resource attribute keys Sim emits, the semantic-convention +// keys it uses (`http.*`, `db.*`, `gen_ai.*`, ...) included. Call sites reference +// `TraceAttr.` rather than raw strings so typos are compile errors. + +export const TraceAttr = { + ApiKeyId: 'api_key.id', + ApiKeyName: 'api_key.name', + BillingChangeType: 'billing.change_type', + BillingCostInputUsd: 'billing.cost.input_usd', + BillingCostOutputUsd: 'billing.cost.output_usd', + BillingCostTotalUsd: 'billing.cost.total_usd', + BillingCostUsd: 'billing.cost_usd', + BillingDurationMs: 'billing.duration_ms', + BillingIdempotencyKey: 'billing.idempotency_key', + BillingInterval: 'billing.interval', + BillingIsMcp: 'billing.is_mcp', + BillingNewPlan: 'billing.new_plan', + BillingOutcome: 'billing.outcome', + BillingPlan: 'billing.plan', + BillingPreviousPlan: 'billing.previous_plan', + BillingSource: 'billing.source', + BillingUsageCurrent: 'billing.usage.current', + BillingUsageExceeded: 'billing.usage.exceeded', + BillingUsageLimit: 'billing.usage.limit', + BlockId: 'block.id', + BlockName: 'block.name', + BlockType: 'block.type', + ChatAuthType: 'chat.auth_type', + ChatFinalizeOutcome: 'chat.finalize.outcome', + ChatHasAssistantMessage: 'chat.has_assistant_message', + ChatHasOutputConfigs: 'chat.has_output_configs', + ChatId: 'chat.id', + ChatPreexisting: 'chat.preexisting', + ChatUserMessageId: 'chat.user_message_id', + ConditionId: 'condition.id', + ConditionName: 'condition.name', + ConditionResult: 'condition.result', + CopilotAbortControllerFired: 'copilot.abort.controller_fired', + CopilotAbortMarkerWritten: 'copilot.abort.marker_written', + CopilotAbortUnknownReason: 'copilot.abort.unknown_reason', + CopilotAsyncToolClaimedBy: 'copilot.async_tool.claimed_by', + CopilotAsyncToolHasError: 'copilot.async_tool.has_error', + CopilotAsyncToolIdsCount: 'copilot.async_tool.ids_count', + CopilotAsyncToolPermissionDecision: 'copilot.async_tool.permission_decision', + CopilotAsyncToolStatus: 'copilot.async_tool.status', + CopilotAsyncToolWorkerId: 'copilot.async_tool.worker_id', + CopilotBranchKind: 'copilot.branch.kind', + CopilotChatIsNew: 'copilot.chat.is_new', + CopilotCommandsCount: 'copilot.commands.count', + CopilotConfirmOutcome: 'copilot.confirm.outcome', + CopilotContextsCount: 'copilot.contexts.count', + CopilotDegradedReason: 'copilot.degraded.reason', + CopilotExecutionId: 'copilot.execution.id', + CopilotFileAttachmentsCount: 'copilot.file_attachments.count', + CopilotFinalizeOutcome: 'copilot.finalize.outcome', + CopilotInterruptedPriorStream: 'copilot.interrupted_prior_stream', + CopilotLeg: 'copilot.leg', + CopilotMode: 'copilot.mode', + CopilotOperation: 'copilot.operation', + CopilotOutputFileBytes: 'copilot.output_file.bytes', + CopilotOutputFileFormat: 'copilot.output_file.format', + CopilotOutputFileId: 'copilot.output_file.id', + CopilotOutputFileMode: 'copilot.output_file.mode', + CopilotOutputFileName: 'copilot.output_file.name', + CopilotOutputFileOutcome: 'copilot.output_file.outcome', + CopilotOutputFilePath: 'copilot.output_file.path', + CopilotPendingStreamWaitMs: 'copilot.pending_stream.wait_ms', + CopilotPrefetch: 'copilot.prefetch', + CopilotPublisherClientDisconnected: 'copilot.publisher.client_disconnected', + CopilotPublisherSawComplete: 'copilot.publisher.saw_complete', + CopilotRecoveryLatestSeq: 'copilot.recovery.latest_seq', + CopilotRecoveryOldestSeq: 'copilot.recovery.oldest_seq', + CopilotRecoveryOutcome: 'copilot.recovery.outcome', + CopilotRecoveryRequestedAfterSeq: 'copilot.recovery.requested_after_seq', + CopilotRequestCancelReason: 'copilot.request.cancel_reason', + CopilotRequestOutcome: 'copilot.request.outcome', + CopilotResourceAttachmentsCount: 'copilot.resource_attachments.count', + CopilotResourcesAborted: 'copilot.resources.aborted', + CopilotResourcesOp: 'copilot.resources.op', + CopilotResourcesRemovedCount: 'copilot.resources.removed_count', + CopilotResourcesUpsertedCount: 'copilot.resources.upserted_count', + CopilotResultContentBlocks: 'copilot.result.content_blocks', + CopilotResultContentLength: 'copilot.result.content_length', + CopilotResultToolCalls: 'copilot.result.tool_calls', + CopilotResumeAfterCursor: 'copilot.resume.after_cursor', + CopilotResumeDurationMs: 'copilot.resume.duration_ms', + CopilotResumeEventCount: 'copilot.resume.event_count', + CopilotResumeOutcome: 'copilot.resume.outcome', + CopilotResumePollIterations: 'copilot.resume.poll_iterations', + CopilotResumePreviewSessionCount: 'copilot.resume.preview_session_count', + CopilotRoute: 'copilot.route', + CopilotRunAgent: 'copilot.run.agent', + CopilotRunHasCompletedAt: 'copilot.run.has_completed_at', + CopilotRunHasError: 'copilot.run.has_error', + CopilotRunModel: 'copilot.run.model', + CopilotRunParentId: 'copilot.run.parent_id', + CopilotRunProvider: 'copilot.run.provider', + CopilotRunStatus: 'copilot.run.status', + CopilotSseBytesReceived: 'copilot.sse.bytes_received', + CopilotSseChunksReceived: 'copilot.sse.chunks_received', + CopilotSseCloseReason: 'copilot.sse.close_reason', + CopilotSseEventsComplete: 'copilot.sse.events.complete', + CopilotSseEventsError: 'copilot.sse.events.error', + CopilotSseEventsReceived: 'copilot.sse.events.received', + CopilotSseEventsResource: 'copilot.sse.events.resource', + CopilotSseEventsRun: 'copilot.sse.events.run', + CopilotSseEventsSession: 'copilot.sse.events.session', + CopilotSseEventsSpan: 'copilot.sse.events.span', + CopilotSseEventsText: 'copilot.sse.events.text', + CopilotSseEventsTool: 'copilot.sse.events.tool', + CopilotSseExpectedTerminal: 'copilot.sse.expected_terminal', + CopilotSseFirstEventMs: 'copilot.sse.first_event_ms', + CopilotSseLongestDispatchMs: 'copilot.sse.longest_dispatch_ms', + CopilotSseLongestInboundGapMs: 'copilot.sse.longest_inbound_gap_ms', + CopilotSseTerminalEventMissing: 'copilot.sse.terminal_event_missing', + CopilotSseTerminalEventSeen: 'copilot.sse.terminal_event_seen', + CopilotSseTotalDispatchMs: 'copilot.sse.total_dispatch_ms', + CopilotStopAppendedAssistant: 'copilot.stop.appended_assistant', + CopilotStopBlocksCount: 'copilot.stop.blocks_count', + CopilotStopContentLength: 'copilot.stop.content_length', + CopilotStopOutcome: 'copilot.stop.outcome', + CopilotStream: 'copilot.stream', + CopilotSurface: 'copilot.surface', + CopilotTableId: 'copilot.table.id', + CopilotTableOutcome: 'copilot.table.outcome', + CopilotTableRowCount: 'copilot.table.row_count', + CopilotTableSourceContentBytes: 'copilot.table.source.content_bytes', + CopilotTableSourceFormat: 'copilot.table.source.format', + CopilotTableSourcePath: 'copilot.table.source.path', + CopilotTransport: 'copilot.transport', + CopilotUserMessagePreview: 'copilot.user.message_preview', + CopilotValidateOutcome: 'copilot.validate.outcome', + DbOperation: 'db.operation', + DbSqlTable: 'db.sql.table', + DbSystem: 'db.system', + DeploymentEnvironment: 'deployment.environment', + DeploymentVersion: 'deployment.version', + DocumentFileSize: 'document.file_size', + DocumentMimeType: 'document.mime_type', + DocumentsCount: 'documents.count', + DocumentsUploadType: 'documents.upload_type', + ErrorCode: 'error.code', + ErrorMessage: 'error.message', + EventName: 'event.name', + EventTimestamp: 'event.timestamp', + ExecutionBlocksExecuted: 'execution.blocks_executed', + ExecutionDurationMs: 'execution.duration_ms', + ExecutionHasErrors: 'execution.has_errors', + ExecutionStatus: 'execution.status', + ExecutionTotalCost: 'execution.total_cost', + ExecutionTrigger: 'execution.trigger', + FunctionExecutionTimeMs: 'function.execution_time_ms', + FunctionId: 'function.id', + FunctionName: 'function.name', + GenAiAgentId: 'gen_ai.agent.id', + GenAiAgentName: 'gen_ai.agent.name', + GenAiCostInput: 'gen_ai.cost.input', + GenAiCostOutput: 'gen_ai.cost.output', + GenAiCostTotal: 'gen_ai.cost.total', + GenAiInputMessages: 'gen_ai.input.messages', + GenAiOperationName: 'gen_ai.operation.name', + GenAiOutputMessages: 'gen_ai.output.messages', + GenAiRequestModel: 'gen_ai.request.model', + GenAiSystem: 'gen_ai.system', + GenAiToolName: 'gen_ai.tool.name', + GenAiUsageInputTokens: 'gen_ai.usage.input_tokens', + GenAiUsageOutputTokens: 'gen_ai.usage.output_tokens', + GenAiUsageTotalTokens: 'gen_ai.usage.total_tokens', + GenAiWorkflowId: 'gen_ai.workflow.id', + GenAiWorkflowName: 'gen_ai.workflow.name', + HttpMethod: 'http.method', + HttpPath: 'http.path', + HttpRequestContentLength: 'http.request.content_length', + HttpResponseContentLength: 'http.response.content_length', + HttpResponseHeadersMs: 'http.response.headers_ms', + HttpRoute: 'http.route', + HttpStatusCode: 'http.status_code', + HttpTarget: 'http.target', + HttpUrl: 'http.url', + InvitationRole: 'invitation.role', + LockAcquired: 'lock.acquired', + LockBackend: 'lock.backend', + LockTimedOut: 'lock.timed_out', + LockTimeoutMs: 'lock.timeout_ms', + LoopId: 'loop.id', + LoopIterations: 'loop.iterations', + LoopName: 'loop.name', + McpExecutionStatus: 'mcp.execution_status', + McpServerId: 'mcp.server_id', + McpServerName: 'mcp.server_name', + McpToolName: 'mcp.tool_name', + McpTransport: 'mcp.transport', + MemberRole: 'member.role', + ModelDurationMs: 'model.duration_ms', + ModelId: 'model.id', + ModelName: 'model.name', + MothershipOrigin: 'mothership.origin', + NetPeerName: 'net.peer.name', + OauthProvider: 'oauth.provider', + ParallelBranches: 'parallel.branches', + ParallelId: 'parallel.id', + ParallelName: 'parallel.name', + ProcessingChunkSize: 'processing.chunk_size', + ProcessingRecipe: 'processing.recipe', + RequestId: 'request.id', + RouterId: 'router.id', + RouterName: 'router.name', + RouterSelectedPath: 'router.selected_path', + RunId: 'run.id', + SearchResultsCount: 'search.results_count', + ServiceInstanceId: 'service.instance.id', + ServiceName: 'service.name', + ServiceNamespace: 'service.namespace', + ServiceVersion: 'service.version', + SimRequestId: 'sim.request_id', + SpanDurationMs: 'span.duration_ms', + SpanStatus: 'span.status', + SpanType: 'span.type', + StreamId: 'stream.id', + TelemetrySdkLanguage: 'telemetry.sdk.language', + TelemetrySdkName: 'telemetry.sdk.name', + TelemetrySdkVersion: 'telemetry.sdk.version', + ToolArgsBytes: 'tool.args.bytes', + ToolArgsPreview: 'tool.args.preview', + ToolCallId: 'tool.call_id', + ToolCompletionReceived: 'tool.completion.received', + ToolConfirmationStatus: 'tool.confirmation.status', + ToolDurationMs: 'tool.duration_ms', + ToolExecutor: 'tool.executor', + ToolId: 'tool.id', + ToolName: 'tool.name', + ToolOutcome: 'tool.outcome', + ToolOutcomeMessage: 'tool.outcome.message', + ToolStatus: 'tool.status', + ToolTimeoutMs: 'tool.timeout_ms', + UserAuthMethod: 'user.auth_method', + UserAuthProvider: 'user.auth_provider', + UserEmail: 'user.email', + UserId: 'user.id', + WebhookId: 'webhook.id', + WebhookProvider: 'webhook.provider', + WebhookTriggerSuccess: 'webhook.trigger_success', + WorkflowBlockTypes: 'workflow.block_types', + WorkflowBlocksCount: 'workflow.blocks_count', + WorkflowDurationMs: 'workflow.duration_ms', + WorkflowEdgesCount: 'workflow.edges_count', + WorkflowHasFolder: 'workflow.has_folder', + WorkflowHasWorkspace: 'workflow.has_workspace', + WorkflowId: 'workflow.id', + WorkflowLoopsCount: 'workflow.loops_count', + WorkflowName: 'workflow.name', + WorkflowNewId: 'workflow.new_id', + WorkflowParallelsCount: 'workflow.parallels_count', + WorkflowSourceId: 'workflow.source_id', + WorkflowTrigger: 'workflow.trigger', + WorkspaceId: 'workspace.id', + WorkspaceName: 'workspace.name', +} as const diff --git a/apps/sim/lib/mothership/observability/trace-events.ts b/apps/sim/lib/mothership/observability/trace-events.ts new file mode 100644 index 00000000000..e5930b9a289 --- /dev/null +++ b/apps/sim/lib/mothership/observability/trace-events.ts @@ -0,0 +1,15 @@ +// Sim-owned: edit here; the mothership worker does not sync this file. + +// Canonical mothership OTel span event names Sim emits. Call sites reference +// `TraceEvent.` rather than raw strings so typos are compile errors. + +export const TraceEvent = { + CopilotDbTransactionBodyComplete: 'copilot.db.transaction.body_complete', + CopilotDbTransactionReady: 'copilot.db.transaction.ready', + CopilotOutputFileError: 'copilot.output_file.error', + CopilotSseFirstEvent: 'copilot.sse.first_event', + CopilotSseIdleGapExceeded: 'copilot.sse.idle_gap_exceeded', + CopilotSseTerminalEventReceived: 'copilot.sse.terminal_event_received', + CopilotTableError: 'copilot.table.error', + RequestCancelled: 'request.cancelled', +} as const diff --git a/apps/sim/lib/mothership/observability/trace-spans.ts b/apps/sim/lib/mothership/observability/trace-spans.ts new file mode 100644 index 00000000000..c663208c693 --- /dev/null +++ b/apps/sim/lib/mothership/observability/trace-spans.ts @@ -0,0 +1,52 @@ +// Sim-owned: edit here; the mothership worker does not sync this file. + +// Canonical mothership OTel span names Sim emits. Call sites reference +// `TraceSpan.` rather than raw strings so typos are compile errors. + +export const TraceSpan = { + CopilotAsyncRunsClaimCompleted: 'copilot.async_runs.claim_completed', + CopilotAsyncRunsCreateRunSegment: 'copilot.async_runs.create_run_segment', + CopilotAsyncRunsGetAsyncToolCall: 'copilot.async_runs.get_async_tool_call', + CopilotAsyncRunsGetMany: 'copilot.async_runs.get_many', + CopilotAsyncRunsGetRunSegment: 'copilot.async_runs.get_run_segment', + CopilotAsyncRunsMarkAsyncToolStatus: 'copilot.async_runs.mark_async_tool_status', + CopilotAsyncRunsReleaseClaim: 'copilot.async_runs.release_claim', + CopilotAsyncRunsUpdateRunStatus: 'copilot.async_runs.update_run_status', + CopilotAsyncRunsUpsertAsyncToolCall: 'copilot.async_runs.upsert_async_tool_call', + CopilotAuthValidateApiKey: 'copilot.auth.validate_api_key', + CopilotBillingUpdateCost: 'copilot.billing.update_cost', + CopilotChatAbortActiveStream: 'copilot.chat.abort_active_stream', + CopilotChatAbortStream: 'copilot.chat.abort_stream', + CopilotChatAcquirePendingStreamLock: 'copilot.chat.acquire_pending_stream_lock', + CopilotChatBuildExecutionContext: 'copilot.chat.build_execution_context', + CopilotChatBuildPayload: 'copilot.chat.build_payload', + CopilotChatFinalizeAssistantTurn: 'copilot.chat.finalize_assistant_turn', + CopilotChatResolveAgentContexts: 'copilot.chat.resolve_agent_contexts', + CopilotChatResolveBranch: 'copilot.chat.resolve_branch', + CopilotChatResolveOrCreateChat: 'copilot.chat.resolve_or_create_chat', + CopilotChatSteerStream: 'copilot.chat.steer_stream', + CopilotChatStopStream: 'copilot.chat.stop_stream', + CopilotCliCurate: 'copilot.cli.curate', + CopilotCliHttpHeaders: 'copilot.cli.http_headers', + CopilotCliInvoke: 'copilot.cli.invoke', + CopilotCliSink: 'copilot.cli.sink', + CopilotConfirmToolResult: 'copilot.confirm.tool_result', + CopilotDbQuery: 'copilot.db.query', + CopilotDbTransaction: 'copilot.db.transaction', + CopilotFinalizeStream: 'copilot.finalize_stream', + CopilotRecoveryCheckReplayGap: 'copilot.recovery.check_replay_gap', + CopilotResumeRequest: 'copilot.resume.request', + CopilotSseReadLoop: 'copilot.sse.read_loop', + CopilotToolResultCommit: 'copilot.tool.result_commit', + CopilotToolRuntime: 'copilot.tool.runtime', + CopilotToolWaitForClientResult: 'copilot.tool.wait_for_client_result', + CopilotToolWaitForPermission: 'copilot.tool.wait_for_permission', + CopilotToolPermissionDecide: 'copilot.tool_permission.decide', + CopilotToolsExecuteInband: 'copilot.tools.execute_inband', + CopilotToolsHandleResourceSideEffects: 'copilot.tools.handle_resource_side_effects', + CopilotToolsWriteCsvToTable: 'copilot.tools.write_csv_to_table', + CopilotToolsWriteOutputFile: 'copilot.tools.write_output_file', + CopilotToolsWriteOutputTable: 'copilot.tools.write_output_table', + GenAiAgentExecute: 'gen_ai.agent.execute', + ToolExecute: 'tool.execute', +} as const diff --git a/apps/sim/lib/mothership/request/application/abandoned-runs.integration.ts b/apps/sim/lib/mothership/request/application/abandoned-runs.integration.ts index cd59f1c6831..ca96f0f73fb 100644 --- a/apps/sim/lib/mothership/request/application/abandoned-runs.integration.ts +++ b/apps/sim/lib/mothership/request/application/abandoned-runs.integration.ts @@ -125,11 +125,11 @@ import { RUN_TAKEOVER_AUDIENCE, takeOverStreamRun, } from '@/lib/mothership/request/application/recover-stream' +import { chatStreamLockKey } from '@/lib/mothership/request/session/controller-lease' import { createProviderToolCallIdentity, scopeProviderToolCallId, -} from '@/lib/mothership/request/go/tool-call-identity' -import { chatStreamLockKey } from '@/lib/mothership/request/session/controller-lease' +} from '@/lib/mothership/request/worker/tool-call-identity' import { chatSandboxSessionKey } from '@/lib/mothership/tools/sandbox-session-key' import { GET as streamGET } from '@/app/api/mothership/chat/stream/route' diff --git a/apps/sim/lib/mothership/request/application/controls.ts b/apps/sim/lib/mothership/request/application/controls.ts index 1f41f7c046d..afe05dde41e 100644 --- a/apps/sim/lib/mothership/request/application/controls.ts +++ b/apps/sim/lib/mothership/request/application/controls.ts @@ -179,7 +179,7 @@ export const abortRun = defineAuthorizedChatUseCase({ }) export class SteeringNotQueuedError extends Error { - constructor(readonly goStatus: number) { + constructor(readonly workerStatus: number) { super('Steering was not queued') } } @@ -199,15 +199,15 @@ export const steerRun = defineAuthorizedChatUseCase({ const { streamId, steeringId, content } = input const { userId, chatId } = context let queued = false - let goStatus = 0 + let workerStatus = 0 try { const result = await requestStreamSteering({ streamId, userId, chatId, steeringId, content }) queued = result.queued - goStatus = result.status + workerStatus = result.status } catch (error) { logger.warn('Steering could not reach worker', { streamId, error: getErrorMessage(error) }) } - if (!queued) throw new SteeringNotQueuedError(goStatus) + if (!queued) throw new SteeringNotQueuedError(workerStatus) try { await appendCopilotChatMessages( chatId, diff --git a/apps/sim/lib/mothership/request/application/recover-stream.test.ts b/apps/sim/lib/mothership/request/application/recover-stream.test.ts index f375c9b5ca6..2f4457defe9 100644 --- a/apps/sim/lib/mothership/request/application/recover-stream.test.ts +++ b/apps/sim/lib/mothership/request/application/recover-stream.test.ts @@ -194,7 +194,7 @@ describe('authorized chat stream recovery', () => { const params = mocks.start.mock.calls[0][0] expect(params.requestPayload).toMatchObject(run.requestContext.recovery.request) expect(params.orchestrateOptions).toMatchObject({ - goRoute: '/api/mothership', + workerRoute: '/api/mothership', userPermission: 'read', interactive: true, clientToolPickupExpected: false, diff --git a/apps/sim/lib/mothership/request/application/recover-stream.ts b/apps/sim/lib/mothership/request/application/recover-stream.ts index 03083dc2689..fe36e40d810 100644 --- a/apps/sim/lib/mothership/request/application/recover-stream.ts +++ b/apps/sim/lib/mothership/request/application/recover-stream.ts @@ -287,7 +287,7 @@ async function recoverStreamRun(context: StreamRunContext, admission?: AbortSign runId: run.id, executionId: run.executionId, workflowId: run.workflowId ?? undefined, - goRoute: config.data.goRoute, + workerRoute: config.data.workerRoute, copilotResourceAdmission: config.data.resourceAuthoringVersion === RESOURCE_AUTHORING_VERSION ? createCopilotResourceAdmission({ userId, invocation: { kind: 'chat', chatId } }) diff --git a/apps/sim/lib/mothership/request/application/removal-stops-runs.integration.ts b/apps/sim/lib/mothership/request/application/removal-stops-runs.integration.ts index 63cce9648e7..ac0a83dc9e8 100644 --- a/apps/sim/lib/mothership/request/application/removal-stops-runs.integration.ts +++ b/apps/sim/lib/mothership/request/application/removal-stops-runs.integration.ts @@ -851,7 +851,7 @@ describe.runIf(Boolean(redisUrl))('removing a run’s chat, owner or access stop message: { id: streamId, content }, recovery: { kind: 'interactive_stream', - goRoute: '/api/mothership', + workerRoute: '/api/mothership', clientToolPickupExpected: false, request: { userId: suspended.userId, @@ -1237,7 +1237,7 @@ describe.runIf(Boolean(redisUrl))('removing a run’s chat, owner or access stop message: { id: sendStreamId, content: 'Start over' }, recovery: { kind: 'interactive_stream', - goRoute: '/api/mothership', + workerRoute: '/api/mothership', clientToolPickupExpected: false, request: { userId: ownerId, @@ -1302,7 +1302,7 @@ describe.runIf(Boolean(redisUrl))('removing a run’s chat, owner or access stop message: { id: streamId, content }, recovery: { kind: 'interactive_stream', - goRoute: '/api/mothership', + workerRoute: '/api/mothership', clientToolPickupExpected: false, request: { userId: ownerId, diff --git a/apps/sim/lib/mothership/request/context/request-context.ts b/apps/sim/lib/mothership/request/context/request-context.ts index 1e8b5b6b262..d42c202e3b2 100644 --- a/apps/sim/lib/mothership/request/context/request-context.ts +++ b/apps/sim/lib/mothership/request/context/request-context.ts @@ -1,5 +1,4 @@ import { generateId } from '@sim/utils/id' -import { TraceCollector } from '@/lib/mothership/request/trace' import type { StreamingContext } from '@/lib/mothership/request/types' /** @@ -30,7 +29,6 @@ export function createStreamingContext(overrides?: Partial): S errors: [], activeFileIntents: new Map(), filePreviewBudget: { contentBytes: 0 }, - trace: new TraceCollector(), toolPermissions: { enabled: false, autoAllowed: new Set(), autoAllowPermitted: true }, ...overrides, } diff --git a/apps/sim/lib/mothership/request/context/restore.ts b/apps/sim/lib/mothership/request/context/restore.ts index 36bc8630b78..a6b0bc26bc6 100644 --- a/apps/sim/lib/mothership/request/context/restore.ts +++ b/apps/sim/lib/mothership/request/context/restore.ts @@ -1,10 +1,10 @@ -import { reconcileTextEvent } from '@/lib/mothership/request/go/text-receipt' import { applyStreamEvent } from '@/lib/mothership/request/handlers' import type { ExecutionContext, StreamEvent, StreamingContext, } from '@/lib/mothership/request/types' +import { reconcileTextEvent } from '@/lib/mothership/request/worker/text-receipt' /** Saved delivery is evidence; only a fresh worker handoff can request tool execution. */ export async function restoreStreamingContext( diff --git a/apps/sim/lib/mothership/request/context/result.test.ts b/apps/sim/lib/mothership/request/context/result.test.ts index 711c512c540..eb09ccac7ee 100644 --- a/apps/sim/lib/mothership/request/context/result.test.ts +++ b/apps/sim/lib/mothership/request/context/result.test.ts @@ -2,7 +2,6 @@ import { describe, expect, it } from 'vitest' import { MothershipStreamV1ToolOutcome } from '@/lib/mothership/generated/mothership-stream-v1' import { RunFunction } from '@/lib/mothership/generated/tool-catalog-v1' import { buildToolCallSummaries } from '@/lib/mothership/request/context/result' -import { TraceCollector } from '@/lib/mothership/request/trace' import type { StreamingContext } from '@/lib/mothership/request/types' function makeContext(): StreamingContext { @@ -28,7 +27,6 @@ function makeContext(): StreamingContext { streamComplete: false, wasAborted: false, errors: [], - trace: new TraceCollector(), toolPermissions: { enabled: false, autoAllowed: new Set(), diff --git a/apps/sim/lib/mothership/request/handlers/handlers.test.ts b/apps/sim/lib/mothership/request/handlers/handlers.test.ts index 044a6a26895..94fcae27108 100644 --- a/apps/sim/lib/mothership/request/handlers/handlers.test.ts +++ b/apps/sim/lib/mothership/request/handlers/handlers.test.ts @@ -12,8 +12,8 @@ import { sleep } from '@sim/utils/helpers' import { toRecord } from '@sim/utils/object' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { AsyncToolCallOwnershipError } from '@/lib/mothership/async-runs/errors' +import { ASYNC_TOOL_STATUS } from '@/lib/mothership/async-runs/lifecycle' import { restoreStreamingContext } from '@/lib/mothership/request/context/restore' -import { TraceCollector } from '@/lib/mothership/request/trace' const { isSimExecuted, executeTool, ensureHandlersRegistered, toolRequiresApproval } = vi.hoisted( () => ({ @@ -57,7 +57,6 @@ vi.mock('@/lib/mothership/request/tools/client-completion-seal.server', () => ({ })) import { - MothershipStreamV1AsyncToolRecordStatus, MothershipStreamV1CompletionStatus, MothershipStreamV1EventType, MothershipStreamV1ResourceOp, @@ -123,7 +122,6 @@ describe('sse-handlers tool lifecycle', () => { accumulatedContent: '', finalAssistantContent: '', sawMainToolCall: false, - trace: new TraceCollector(), contentBlocks: [], toolCalls: new Map(), pendingToolPromises: new Map(), @@ -173,10 +171,12 @@ describe('sse-handlers tool lifecycle', () => { ).rejects.toBeInstanceOf(AsyncToolCallOwnershipError) }) - it('replays completed child lanes once without reopening traces or losing errors', async () => { - const start = vi.spyOn(context.trace, 'startSpan') - const end = vi.spyOn(context.trace, 'endSpan') - const emit = (lane: string, event: 'start' | 'end', data?: Record) => { + it('replays completed child lanes once without losing errors', async () => { + const emit = ( + lane: string, + event: 'start' | 'end', + data?: { name?: string; error?: string } + ) => { const scope = { lane: 'subagent' as const, agentId: 'task', @@ -200,11 +200,6 @@ describe('sse-handlers tool lifecycle', () => { for (let attempt = 0; attempt < 2; attempt++) { await emit('A', 'start', { name: 'Inspect workflow' }) await emit('B', 'start', { name: 'Check constraints' }) - await emit('B', 'end', { pending: true }) - if (attempt === 0) - expect( - context.contentBlocks.find((block) => block.parentToolCallId === 'B')?.endedAt - ).toBeUndefined() await emit('B', 'end', { error: 'Check failed' }) await emit('A', 'end') } @@ -218,9 +213,6 @@ describe('sse-handlers tool lifecycle', () => { subagentName: 'Inspect workflow', endedAt: expect.any(Number), }) - expect(start).toHaveBeenCalledTimes(2) - expect(end).toHaveBeenCalledTimes(2) - expect(context.subAgentTraceSpans?.size).toBe(0) }) it('counts the preview content already in the replay toward the restored turn budget', async () => { @@ -372,7 +364,7 @@ describe('sse-handlers tool lifecycle', () => { executableName: toolName, args, sealedContext: { __sealedClientToolContextV1: 'sealed-context' }, - status: MothershipStreamV1AsyncToolRecordStatus.pending, + status: ASYNC_TOOL_STATUS.pending, permissionRequested: gated, }) expect(sealClientToolContext).toHaveBeenCalledWith({ @@ -427,7 +419,7 @@ describe('sse-handlers tool lifecycle', () => { toolName: 'deploy_as_api', executableName: 'deploy_as_api', args: { versionName: 'v2' }, - status: MothershipStreamV1AsyncToolRecordStatus.pending, + status: ASYNC_TOOL_STATUS.pending, permissionRequested: true, }) expect(event.payload.status).toBe('awaiting_approval') @@ -510,8 +502,8 @@ describe('sse-handlers tool lifecycle', () => { }) }) - it('clears a Go-stamped approval frame when the gate is off', async () => { - // Go stamps integration calls regardless of Sim's feature flag. Forwarding + it('clears a worker-stamped approval frame when the gate is off', async () => { + // The worker stamps integration calls regardless of Sim's feature flag. Forwarding // that stamp with nothing gating behind it would draw a card whose buttons // answer into a disabled endpoint. toolRequiresApproval.mockReturnValue(false) @@ -541,7 +533,7 @@ describe('sse-handlers tool lifecycle', () => { expect(upsertAsyncToolCall).not.toHaveBeenCalled() }) - it('clears a Go-stamped approval frame on a partial call, which is never held', async () => { + it('clears a worker-stamped approval frame on a partial call, which is never held', async () => { toolRequiresApproval.mockReturnValue(true) context.runId = 'run-1' context.toolPermissions = { @@ -614,7 +606,7 @@ describe('sse-handlers tool lifecycle', () => { } ) - it('clears a Go-stamped approval frame on an internal tool', async () => { + it('clears a worker-stamped approval frame on an internal tool', async () => { toolRequiresApproval.mockReturnValue(true) context.runId = 'run-1' context.toolPermissions = { @@ -696,7 +688,7 @@ describe('sse-handlers tool lifecycle', () => { toolName: 'run_workflow', executableName: 'run_workflow', args: { workflowId: 'workflow-1' }, - status: MothershipStreamV1AsyncToolRecordStatus.running, + status: ASYNC_TOOL_STATUS.running, permissionRequested: false, }) }) @@ -1069,7 +1061,7 @@ describe('sse-handlers tool lifecycle', () => { ) it('registers but never dispatches an inband-owned sim tool call', async () => { - // Go executes inband-owned calls itself via /api/copilot/tools/execute; + // The worker executes inband-owned calls itself via /api/copilot/tools/execute; // dispatching here too ran the tool twice, racing on mutations. await sseHandlers.tool( { @@ -1121,7 +1113,7 @@ describe('sse-handlers tool lifecycle', () => { expect(completeAsyncToolCall).toHaveBeenCalledWith( expect.objectContaining({ toolCallId: 'tool-primitive', - status: MothershipStreamV1AsyncToolRecordStatus.completed, + status: ASYNC_TOOL_STATUS.completed, result: 'done', error: null, }) @@ -2334,8 +2326,6 @@ describe('sse-handlers tool lifecycle', () => { toolCallId: 'gateway-gmail', toolName: 'call_integration_tool', argumentsDelta, - executor: MothershipStreamV1ToolExecutor.go, - mode: MothershipStreamV1ToolMode.sync, phase: MothershipStreamV1ToolPhase.args_delta, }, } satisfies StreamEvent, @@ -2585,31 +2575,6 @@ describe('sse-handlers tool lifecycle', () => { } ) - it('clears pending continuation state when a run resumes', async () => { - context.awaitingAsyncContinuation = { - checkpointId: 'cp-1', - executionId: 'exec-1', - runId: 'run-1', - pendingToolCallIds: ['tool-1'], - } - context.streamComplete = true - - await sseHandlers.run( - { - type: MothershipStreamV1EventType.run, - payload: { - kind: MothershipStreamV1RunKind.resumed, - }, - } satisfies StreamEvent, - context, - execContext, - { interactive: false, timeout: 1000 } - ) - - expect(context.awaitingAsyncContinuation).toBeUndefined() - expect(context.streamComplete).toBe(false) - }) - it('records the terminal completion status so a finished turn can outrank an in-band failure', async () => { context.errors.push('subagent build failed') diff --git a/apps/sim/lib/mothership/request/handlers/index.ts b/apps/sim/lib/mothership/request/handlers/index.ts index 6463bb0d125..db36e4f840b 100644 --- a/apps/sim/lib/mothership/request/handlers/index.ts +++ b/apps/sim/lib/mothership/request/handlers/index.ts @@ -1,5 +1,6 @@ import { createLogger } from '@sim/logger' import { MothershipStreamV1EventType } from '@/lib/mothership/generated/mothership-stream-v1' +import type { StreamActivityReceipt } from '@/lib/mothership/generated/protocol' import { isSubagentSpanStreamEvent } from '@/lib/mothership/request/session/contract' import type { ExecutionContext, @@ -59,13 +60,14 @@ export async function applyStreamEvent( return } await sseHandlers[event.type]?.(event, context, execContext, options) - if ( - event.type === 'complete' || - (event.type === 'run' && event.payload.kind === 'checkpoint_pause') - ) { - /** Only a handled leg boundary acknowledges its preceding activity. */ - if (event.payload.activityReceipt) context.receivedActivity = event.payload.activityReceipt + /** Only a handled leg boundary acknowledges its preceding activity. */ + let receipt: StreamActivityReceipt | undefined + if (event.type === 'complete') { + receipt = event.payload.activityReceipt + } else if (event.type === 'run' && event.payload.kind === 'checkpoint_pause') { + receipt = event.payload.activityReceipt } + if (receipt) context.receivedActivity = receipt } export function handleSubagentRouting(event: StreamEvent, _context: StreamingContext): boolean { @@ -75,7 +77,7 @@ export function handleSubagentRouting(event: StreamEvent, _context: StreamingCon // With concurrent subagents there is no single "current" lane to fall back to — // routing by a global pointer would mis-attribute interleaved events to the // last-started subagent. A missing parentToolCallId is a contract violation - // (Go always stamps it), so warn and route to the main lane rather than guess. + // (the worker always stamps it), so warn and route to the main lane rather than guess. if (!event.scope?.parentToolCallId) { logger.warn('Subagent event missing parent tool call id; routing to main lane', { type: event.type, diff --git a/apps/sim/lib/mothership/request/handlers/run.ts b/apps/sim/lib/mothership/request/handlers/run.ts index c15cc964126..2f1947ad439 100644 --- a/apps/sim/lib/mothership/request/handlers/run.ts +++ b/apps/sim/lib/mothership/request/handlers/run.ts @@ -73,28 +73,17 @@ export const handleRunEvent: StreamHandler = (event, context) => { } if (event.payload.kind === MothershipStreamV1RunKind.checkpoint_pause) { - const frames = (event.payload.frames ?? []).map((frame) => ({ - parentToolCallId: frame.parentToolCallId, - parentToolName: frame.parentToolName, - pendingToolIds: frame.pendingToolIds, - // Carried through for the per-subagent resume fan-out; undefined under the - // legacy bundled-frame model (all frames share the top-level checkpointId). - ...(frame.checkpointId ? { checkpointId: frame.checkpointId } : {}), - })) - context.awaitingAsyncContinuation = { checkpointId: event.payload.checkpointId, executionId: event.payload.executionId || context.executionId, runId: event.payload.runId || context.runId, pendingToolCallIds: event.payload.pendingToolCallIds, - frames: frames.length > 0 ? frames : undefined, } logger.info('Received checkpoint pause', { checkpointId: context.awaitingAsyncContinuation.checkpointId, executionId: context.awaitingAsyncContinuation.executionId, runId: context.awaitingAsyncContinuation.runId, pendingToolCallIds: context.awaitingAsyncContinuation.pendingToolCallIds, - frameCount: frames.length, }) context.streamComplete = true return @@ -107,13 +96,6 @@ export const handleRunEvent: StreamHandler = (event, context) => { return } - if (event.payload.kind === MothershipStreamV1RunKind.resumed) { - context.awaitingAsyncContinuation = undefined - context.streamComplete = false - logger.info('Received run resumed event') - return - } - const payload = event.payload if (payload.kind === 'task_armed' || payload.kind === 'task_delivered') { const block = context.contentBlocks.find( diff --git a/apps/sim/lib/mothership/request/handlers/span.ts b/apps/sim/lib/mothership/request/handlers/span.ts index 7a160f40ebe..4ccd003d579 100644 --- a/apps/sim/lib/mothership/request/handlers/span.ts +++ b/apps/sim/lib/mothership/request/handlers/span.ts @@ -1,5 +1,4 @@ import { createLogger } from '@sim/logger' -import { isRecordLike } from '@sim/utils/object' import { MothershipStreamV1SpanLifecycleEvent, MothershipStreamV1SpanPayloadKind, @@ -10,6 +9,7 @@ import { flushSubagentThinkingBlock, flushThinkingBlock, } from '@/lib/mothership/request/handlers/types' +import { subagentSpanData } from '@/lib/mothership/request/session/contract' const logger = createLogger('MothershipSpan') @@ -19,29 +19,17 @@ export const handleSpanEvent: StreamHandler = (event, context) => { return } - const payload = event.payload as { - kind?: string - event?: string - agent?: string - data?: unknown - } - const kind = payload?.kind ?? '' - const evt = payload?.event ?? '' + const { kind, event: evt, agent: payloadAgent } = event.payload if (kind === MothershipStreamV1SpanPayloadKind.subagent) { - const data = isRecordLike(payload.data) ? payload.data : undefined - const parentToolCallId = - event.scope?.parentToolCallId ?? - (typeof data?.tool_call_id === 'string' ? data.tool_call_id : undefined) + const parentToolCallId = event.scope?.parentToolCallId const spanId = event.scope?.spanId - const traceKey = spanId ?? parentToolCallId - if (!traceKey) { + if (!spanId && !parentToolCallId) { logger.warn('Subagent lifecycle frame has no lane identity') return } - const agent = typeof payload.agent === 'string' && payload.agent ? payload.agent : 'task' - const name = typeof data?.name === 'string' && data.name ? data.name : undefined - const error = typeof data?.error === 'string' ? data.error : undefined + const agent = payloadAgent || 'task' + const { name, error } = subagentSpanData(event.payload.data) flushSubagentThinkingBlock(context) flushThinkingBlock(context) const block = context.contentBlocks.find( @@ -64,41 +52,10 @@ export const handleSpanEvent: StreamHandler = (event, context) => { ...(event.scope?.parentSpanId ? { parentSpanId: event.scope.parentSpanId } : {}), }) } else if (name) block.subagentName = name - if (block?.endedAt !== undefined) return - context.subAgentTraceSpans ??= new Map() - if (!context.subAgentTraceSpans.has(traceKey)) { - context.subAgentTraceSpans.set( - traceKey, - context.trace.startSpan(`subagent:${agent}`, 'go.subagent', { - agent, - parentToolCallId, - spanId, - }) - ) - } - } else if (evt === MothershipStreamV1SpanLifecycleEvent.end && data?.pending !== true) { - if (block) { - block.endedAt ??= Date.now() - if (error) block.error = error - } - const span = context.subAgentTraceSpans?.get(traceKey) - if (span) { - context.trace.endSpan(span, error ? 'error' : 'ok') - context.subAgentTraceSpans?.delete(traceKey) - } + } else if (evt === MothershipStreamV1SpanLifecycleEvent.end && block) { + block.endedAt ??= Date.now() + if (error) block.error = error } return } - - if ( - kind === MothershipStreamV1SpanPayloadKind.structured_result || - kind === MothershipStreamV1SpanPayloadKind.subagent_result - ) { - const span = context.trace.startSpan(`${kind}:${payload.agent ?? 'main'}`, `go.${kind}`, { - agent: payload.agent, - hasData: payload.data !== undefined, - }) - context.trace.endSpan(span, 'ok') - return - } } diff --git a/apps/sim/lib/mothership/request/handlers/tool.ts b/apps/sim/lib/mothership/request/handlers/tool.ts index 97ede02d5d0..448d86a2f96 100644 --- a/apps/sim/lib/mothership/request/handlers/tool.ts +++ b/apps/sim/lib/mothership/request/handlers/tool.ts @@ -10,9 +10,10 @@ import { getRunDesktopDeviceId } from '@/lib/desktop/executor/repository' import { executeCopilotAssistantAction } from '@/lib/mothership/application/assistant-actions' import { isAssistantIntegrationTool } from '@/lib/mothership/assistant/tool-policy' import { AsyncToolCallOwnershipError } from '@/lib/mothership/async-runs/errors' -import type { - AsyncCompletionSignal, - AsyncTerminalCompletionSnapshot, +import { + ASYNC_TOOL_STATUS, + type AsyncCompletionSignal, + type AsyncTerminalCompletionSnapshot, } from '@/lib/mothership/async-runs/lifecycle' import { getAsyncToolCall, upsertAsyncToolCall } from '@/lib/mothership/async-runs/repository' import { @@ -22,13 +23,12 @@ import { DESKTOP_TOOL_PICKUP_GRACE_MS, } from '@/lib/mothership/constants' import { - MothershipStreamV1AsyncToolRecordStatus, MothershipStreamV1ToolExecutor, MothershipStreamV1ToolOutcome, type MothershipStreamV1ToolResultPayload, } from '@/lib/mothership/generated/mothership-stream-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { withCopilotSpan } from '@/lib/mothership/request/otel' import { isToolArgsDeltaStreamEvent, @@ -190,7 +190,7 @@ function handleToolArgsDelta( } /** - * The model first streams the stable gateway call. Once Go resolves its exact + * The model first streams the stable gateway call. Once the worker resolves its exact * server-owned operation, a second authoritative frame with the same call id * carries the real executable tool name and arguments. Rebind atomically so * execution, persistence, branding, and results all use that operation while @@ -337,7 +337,7 @@ export async function prePersistClientExecutableToolCall( } } - // Go stamps this for resolved integration operations; Sim stamps it below for + // The worker stamps this for resolved integration operations; Sim stamps it below for // catalog-declared tools. Normalizing here means the dispatch path only ever // has to read the frame. // @@ -361,7 +361,7 @@ export async function prePersistClientExecutableToolCall( if (gated) { data.status = TOOL_AWAITING_APPROVAL_STATUS } else if (frameRequestsApproval) { - // Go asked for a prompt this surface will not hold — the feature is off, + // The worker asked for a prompt this surface will not hold — the feature is off, // the tool is internal, or the user already allowed it for good. Clear the // stamp so the row renders as an ordinary call. data.status = undefined @@ -448,8 +448,8 @@ export async function prePersistClientExecutableToolCall( data.arguments, desktopClaimsLocalReads(options, desktopDeviceId) ) - ? MothershipStreamV1AsyncToolRecordStatus.pending - : MothershipStreamV1AsyncToolRecordStatus.running, + ? ASYNC_TOOL_STATUS.pending + : ASYNC_TOOL_STATUS.running, permissionRequested: gated, executionDeadlineAt: toDeadlineDate(frameToolDeadlineAt(data.deadlineMs, Date.now())), }).catch((err) => { @@ -783,7 +783,7 @@ async function handleCallPhase( // sim-routed — the worker runs some legacy-named tools in-process, and a stale-catalog // dispatch raced them with a second, handlerless execution ("No handler for tool"). const staticSimExecuted = isSimExecuted(toolName) && data.executor !== 'go' - // Go executes inband-owned calls itself via /api/copilot/tools/execute + // The worker executes inband-owned calls itself via /api/copilot/tools/execute // (background lanes, and the main lane while background agents run); the // event exists only to draw the row. Dispatching it here would run the // tool a second time, racing the in-band execution on mutations. diff --git a/apps/sim/lib/mothership/request/handlers/types.ts b/apps/sim/lib/mothership/request/handlers/types.ts index 02c3397e35e..dc59f2c31e3 100644 --- a/apps/sim/lib/mothership/request/handlers/types.ts +++ b/apps/sim/lib/mothership/request/handlers/types.ts @@ -16,7 +16,7 @@ import { MothershipStreamV1ToolPhase, type MothershipStreamV1ToolResultPayload, } from '@/lib/mothership/generated/mothership-stream-v1' -import { CopilotDegradedReason } from '@/lib/mothership/generated/trace-attribute-values-v1' +import { CopilotDegradedReason } from '@/lib/mothership/observability/trace-attribute-values' import { recordDegraded } from '@/lib/mothership/request/metrics' import { markToolResultSeen } from '@/lib/mothership/request/sse-utils' import { setTerminalToolCallState } from '@/lib/mothership/request/tool-call-state' @@ -99,7 +99,7 @@ export function flushSubagentThinkingBlock( * scope. The legacy fallback to a single "current subagent" pointer was removed: * with concurrent subagents that pointer reflects whichever subagent started * most recently and would mis-attribute interleaved events. Every subagent-lane - * event is guaranteed to carry parentToolCallId (Go stamps it), so a missing one + * event is guaranteed to carry parentToolCallId (the worker stamps it), so a missing one * is a real contract violation — callers warn and drop rather than guess. */ export function getScopedParentToolCallId(event: StreamEvent): string | undefined { @@ -145,8 +145,8 @@ export function registerPendingToolPromise( } /** - * When the Sim->Go stream is aborted, avoid starting server-side tool work and - * unblock the Go async waiter with a terminal 499 completion. + * When the Sim → worker stream is aborted, avoid starting server-side tool work and + * unblock the worker's async waiter with a terminal 499 completion. */ export function abortPendingToolIfStreamDead( toolCall: ToolCallState, @@ -170,8 +170,7 @@ export function abortPendingToolIfStreamDead( error: 'Tool was not dispatched because its stream had already been aborted', }) markToolResultSeen(context, toolCallId) - // Sim's logs do not reach Loki and the trace span below is collected - // in-process but never exported, so the counter is the only signal that + // Sim's logs do not reach Loki, so the counter is the only signal that // survives to somewhere queryable. recordDegraded(CopilotDegradedReason.StreamDeadBeforeDispatch) logger.warn('Cancelled tool call before dispatch: stream already aborted', { @@ -182,21 +181,12 @@ export function abortPendingToolIfStreamDead( ...(abortReason ? { abortReason } : {}), wasAborted: context.wasAborted ?? false, }) - const toolSpan = context.trace.startSpan(toolCall.name || 'unknown_tool', 'tool.execute', { - toolCallId, - toolName: toolCall.name, - cancelReason: 'stream_dead_before_dispatch', - abortSignalAborted: options.abortSignal?.aborted ?? false, - abortReason, - wasAborted: context.wasAborted ?? false, - }) - context.trace.endSpan(toolSpan, 'cancelled') return true } /** - * Extract the behavioral `ui` flags from a typed tool_call payload. The Go - * backend enriches tool_call events with `ui: { clientExecutable, internal, + * Extract the behavioral `ui` flags from a typed tool_call payload. The worker + * enriches tool_call events with `ui: { clientExecutable, internal, * hidden }`; presentation (title/icon) is derived client-side from the tool name. */ export function getToolCallUI(data: MothershipStreamV1ToolCallDescriptor): { @@ -258,7 +248,7 @@ export function handleClientCompletion( /** * Emit a synthetic tool_result SSE event to the client after a client-executable - * tool completes. The Go backend's actual tool_result is skipped (markToolResultSeen), + * tool completes. The worker's actual tool_result is skipped (markToolResultSeen), * so the client would never learn the outcome without this. */ export async function emitSyntheticToolResult( diff --git a/apps/sim/lib/mothership/request/lifecycle/admission.test.ts b/apps/sim/lib/mothership/request/lifecycle/admission.test.ts index 2f0df88200b..26d5fc1df1d 100644 --- a/apps/sim/lib/mothership/request/lifecycle/admission.test.ts +++ b/apps/sim/lib/mothership/request/lifecycle/admission.test.ts @@ -8,7 +8,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { createAttributedBillingRequestEnvelope } from '@/lib/billing/core/attribution' import { resetMidRunUsageCaches } from '@/lib/billing/core/mid-run-usage' import { OrchestrationError } from '@/lib/core/orchestration/types' -import { BillingLimitError } from '@/lib/mothership/request/go/stream' +import { BillingLimitError } from '@/lib/mothership/request/worker/stream' import { authorizeLifecycleContinuation, restoreBillingAdmission } from './admission' const mocks = vi.hoisted(() => ({ authorize: vi.fn(), standing: vi.fn() })) diff --git a/apps/sim/lib/mothership/request/lifecycle/admission.ts b/apps/sim/lib/mothership/request/lifecycle/admission.ts index 69ea8839441..9ab54475201 100644 --- a/apps/sim/lib/mothership/request/lifecycle/admission.ts +++ b/apps/sim/lib/mothership/request/lifecycle/admission.ts @@ -13,9 +13,9 @@ import { authorizeCopilotChatCallback, checkCopilotContinuationBilling, } from '@/lib/mothership/application/authorize-chat-callback' -import { BillingLimitError } from '@/lib/mothership/request/go/stream' import { BillingAdmissionSchema } from '@/lib/mothership/request/lifecycle/recovery-config' import type { ExecutionContext } from '@/lib/mothership/request/types' +import { BillingLimitError } from '@/lib/mothership/request/worker/stream' /** Restores only the original server-owned admission, never a newly selected payer. */ export function restoreBillingAdmission( diff --git a/apps/sim/lib/mothership/request/lifecycle/execute-events.ts b/apps/sim/lib/mothership/request/lifecycle/execute-events.ts index 76a2c3dd619..94adfa2825e 100644 --- a/apps/sim/lib/mothership/request/lifecycle/execute-events.ts +++ b/apps/sim/lib/mothership/request/lifecycle/execute-events.ts @@ -1,5 +1,5 @@ -import { reconcileTextEvent } from '@/lib/mothership/request/go/text-receipt' import type { StreamEvent } from '@/lib/mothership/request/types' +import { reconcileTextEvent } from '@/lib/mothership/request/worker/text-receipt' import type { AgentStreamEvent, ToolCallEndStatus } from '@/providers/stream-events' /** Projects only the main agent's public lifecycle; payloads never cross this delivery boundary. */ diff --git a/apps/sim/lib/mothership/request/lifecycle/finalize.ts b/apps/sim/lib/mothership/request/lifecycle/finalize.ts index 67dff02c99a..18ed6530533 100644 --- a/apps/sim/lib/mothership/request/lifecycle/finalize.ts +++ b/apps/sim/lib/mothership/request/lifecycle/finalize.ts @@ -6,15 +6,12 @@ import { MothershipStreamV1CompletionStatus, MothershipStreamV1EventType, } from '@/lib/mothership/generated/mothership-stream-v1' -import { - type RequestTraceV1Outcome, - RequestTraceV1Outcome as RequestTraceV1OutcomeConst, -} from '@/lib/mothership/generated/request-trace-v1' -import { CopilotFinalizeOutcome } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { CopilotFinalizeOutcome } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import type { StreamWriter } from '@/lib/mothership/request/session' import { StreamControllerSupersededError } from '@/lib/mothership/request/session/controller-lease' +import { RequestTraceOutcome } from '@/lib/mothership/request/trace' import type { OrchestratorResult } from '@/lib/mothership/request/types' const logger = createLogger('CopilotStreamFinalize') @@ -26,13 +23,13 @@ export async function finalizeStream( result: OrchestratorResult, publisher: StreamWriter, runId: string, - outcome: RequestTraceV1Outcome, + outcome: RequestTraceOutcome, requestId: string ): Promise { const spanOutcome = - outcome === RequestTraceV1OutcomeConst.cancelled + outcome === RequestTraceOutcome.cancelled ? CopilotFinalizeOutcome.Aborted - : outcome === RequestTraceV1OutcomeConst.success + : outcome === RequestTraceOutcome.success ? CopilotFinalizeOutcome.Success : CopilotFinalizeOutcome.Error const span = getTracer().startSpan(TraceSpan.CopilotFinalizeStream, { @@ -48,9 +45,9 @@ export async function finalizeStream( }, }) try { - if (outcome === RequestTraceV1OutcomeConst.cancelled) { + if (outcome === RequestTraceOutcome.cancelled) { await handleAborted(result, publisher, runId, requestId) - } else if (outcome === RequestTraceV1OutcomeConst.error) { + } else if (outcome === RequestTraceOutcome.error) { span.setStatus({ code: SpanStatusCode.ERROR, message: result.error || 'orchestration failed', @@ -61,7 +58,7 @@ export async function finalizeStream( } // Successful + cancelled paths fall through as status-unset → set // OK so dashboards don't show "incomplete" for normal terminals. - if (outcome !== RequestTraceV1OutcomeConst.error) { + if (outcome !== RequestTraceOutcome.error) { span.setStatus({ code: SpanStatusCode.OK }) } } catch (error) { @@ -142,7 +139,7 @@ async function handleError( toolCallCount, }) - // Surface the real error (Go already classifies provider errors like + // Surface the real error (the worker already classifies provider errors like // "overloaded" into a friendly displayMessage). Don't clobber it with a // generic string. await publishThenSettle( diff --git a/apps/sim/lib/mothership/request/lifecycle/headless.test.ts b/apps/sim/lib/mothership/request/lifecycle/headless.test.ts index 43f5f801369..ba360e22a3d 100644 --- a/apps/sim/lib/mothership/request/lifecycle/headless.test.ts +++ b/apps/sim/lib/mothership/request/lifecycle/headless.test.ts @@ -62,7 +62,7 @@ describe('runHeadlessCopilotLifecycle', () => { chatId: 'chat-1', workflowId: 'workflow-1', simRequestId: 'workflow-request-id', - goRoute: '/api/mothership/execute', + workerRoute: '/api/mothership/execute', interactive: false, } ) @@ -78,7 +78,7 @@ describe('runHeadlessCopilotLifecycle', () => { it('threads a valid OTel context into the lifecycle', async () => { let lifecycleTraceparent = '' runCopilotLifecycle.mockImplementationOnce(async (_payload, options) => { - const { traceHeaders } = await import('@/lib/mothership/request/go/propagation') + const { traceHeaders } = await import('@/lib/mothership/request/worker/propagation') lifecycleTraceparent = traceHeaders({}, options.otelContext).traceparent ?? '' return createLifecycleResult() }) @@ -92,7 +92,7 @@ describe('runHeadlessCopilotLifecycle', () => { userId: 'user-1', chatId: 'chat-1', workflowId: 'workflow-1', - goRoute: '/api/mothership/execute', + workerRoute: '/api/mothership/execute', interactive: false, } ) diff --git a/apps/sim/lib/mothership/request/lifecycle/headless.ts b/apps/sim/lib/mothership/request/lifecycle/headless.ts index 02e83fc7232..15ac98ad172 100644 --- a/apps/sim/lib/mothership/request/lifecycle/headless.ts +++ b/apps/sim/lib/mothership/request/lifecycle/headless.ts @@ -1,15 +1,9 @@ import { generateId } from '@sim/utils/id' import type { ChatRequest } from '@/lib/mothership/generated/protocol' -import type { RequestTraceV1Outcome as RequestTraceOutcome } from '@/lib/mothership/generated/request-trace-v1' -import { - RequestTraceV1Outcome, - RequestTraceV1SpanStatus, -} from '@/lib/mothership/generated/request-trace-v1' -import { CopilotTransport } from '@/lib/mothership/generated/trace-attribute-values-v1' +import { CopilotTransport } from '@/lib/mothership/observability/trace-attribute-values' import type { CopilotLifecycleOptions } from '@/lib/mothership/request/lifecycle/run' import { runCopilotLifecycle } from '@/lib/mothership/request/lifecycle/run' import { withCopilotOtelContext } from '@/lib/mothership/request/otel' -import { TraceCollector } from '@/lib/mothership/request/trace' import type { OrchestratorResult } from '@/lib/mothership/request/types' export async function runHeadlessCopilotLifecycle( @@ -22,60 +16,20 @@ export async function runHeadlessCopilotLifecycle( : typeof requestPayload.messageId === 'string' && requestPayload.messageId.length > 0 ? requestPayload.messageId : generateId() - const trace = new TraceCollector() - const requestSpan = trace.startSpan('Headless Sim Agent Request', 'request', { - route: options.goRoute, - workflowId: options.workflowId, - workspaceId: options.workspaceId, - chatId: options.chatId, - }) - - let result: OrchestratorResult | undefined - let outcome: RequestTraceOutcome = RequestTraceV1Outcome.error - return withCopilotOtelContext( { requestId: simRequestId, - route: options.goRoute, + route: options.workerRoute, chatId: options.chatId, workflowId: options.workflowId, executionId: options.executionId, runId: options.runId, transport: CopilotTransport.Headless, }, - async (otelContext) => { - try { - result = await runCopilotLifecycle( - { ...requestPayload }, - { - ...options, - interactive: false, - trace, - simRequestId, - otelContext, - } - ) - outcome = result.success - ? RequestTraceV1Outcome.success - : options.abortSignal?.aborted || result.cancelled - ? RequestTraceV1Outcome.cancelled - : RequestTraceV1Outcome.error - return result - } catch (error) { - outcome = options.abortSignal?.aborted - ? RequestTraceV1Outcome.cancelled - : RequestTraceV1Outcome.error - throw error - } finally { - trace.endSpan( - requestSpan, - outcome === RequestTraceV1Outcome.success - ? RequestTraceV1SpanStatus.ok - : outcome === RequestTraceV1Outcome.cancelled - ? RequestTraceV1SpanStatus.cancelled - : RequestTraceV1SpanStatus.error - ) - } - } + (otelContext) => + runCopilotLifecycle( + { ...requestPayload }, + { ...options, interactive: false, simRequestId, otelContext } + ) ) } diff --git a/apps/sim/lib/mothership/request/lifecycle/recovery-config.test.ts b/apps/sim/lib/mothership/request/lifecycle/recovery-config.test.ts index bad76508c79..344f666ce73 100644 --- a/apps/sim/lib/mothership/request/lifecycle/recovery-config.test.ts +++ b/apps/sim/lib/mothership/request/lifecycle/recovery-config.test.ts @@ -2,6 +2,7 @@ import { featureFlagsMock, featureFlagsMockFns } from '@sim/testing/mocks/featur import { describe, expect, it, vi } from 'vitest' import { createCopilotResourceAdmission } from '@/lib/mothership/auth/application-delegation' import { + persistedStreamRecoveryConfig, StreamRecoveryConfigSchema, streamRecoveryConfig, } from '@/lib/mothership/request/lifecycle/recovery-config' @@ -85,6 +86,15 @@ describe('durable recovery admission contract', () => { }) }) +it('reads the stored goRoute as workerRoute and writes workerRoute back as goRoute', () => { + const read = StreamRecoveryConfigSchema.parse(config(workspaceRequest)) + expect(read.workerRoute).toBe('/api/mothership') + expect(read).not.toHaveProperty('goRoute') + const stored = persistedStreamRecoveryConfig(read) + expect(stored.goRoute).toBe('/api/mothership') + expect(stored).not.toHaveProperty('workerRoute') +}) + it('preserves the explicit Plan wire mode on recovery', () => { expect( StreamRecoveryConfigSchema.parse(config({ ...workspaceRequest, mode: 'plan' }, 'plan')).request @@ -97,7 +107,7 @@ describe('recovery marker written by an admitted turn', () => { interactive: true, userId: identity.userId, chatId: identity.chatId, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', copilotResourceAdmission: createCopilotResourceAdmission({ userId: identity.userId, invocation: { kind: 'chat', chatId: identity.chatId }, @@ -115,6 +125,12 @@ describe('recovery marker written by an admitted turn', () => { } ) + it('stores the route under the goRoute key every deployed Sim parses', async () => { + const recovery = await streamRecoveryConfig(options, workspaceRequest) + expect(recovery?.goRoute).toBe('/api/mothership') + expect(recovery).not.toHaveProperty('workerRoute') + }) + it('stays off a turn that was never admitted', async () => { const recovery = await streamRecoveryConfig( { ...options, copilotResourceAdmission: undefined }, diff --git a/apps/sim/lib/mothership/request/lifecycle/recovery-config.ts b/apps/sim/lib/mothership/request/lifecycle/recovery-config.ts index f48605eb53a..1d975c19d36 100644 --- a/apps/sim/lib/mothership/request/lifecycle/recovery-config.ts +++ b/apps/sim/lib/mothership/request/lifecycle/recovery-config.ts @@ -38,13 +38,20 @@ export function durableResourceAuthoringVersion( return admitted ? RESOURCE_AUTHORING_VERSION : undefined } -export const StreamRecoveryConfigSchema = z +const WORKER_ROUTES = ['/api/mothership', '/api/copilot'] as const + +/** + * The recovery intent as stored on the run. The worker route is persisted as `goRoute`, a name + * every deployed and self-hosted Sim parses under `.strict()`, so it is never renamed; this module + * is the only place that names it. + */ +const PersistedStreamRecoveryConfigSchema = z .object({ kind: z.enum(DURABLE_STREAM_KINDS), resourceAuthoringVersion: z.literal(RESOURCE_AUTHORING_VERSION).optional(), billingAdmission: BillingAdmissionSchema.optional(), request: DurableChatRequestSchema, - goRoute: z.enum(['/api/mothership', '/api/copilot']), + goRoute: z.enum(WORKER_ROUTES), clientToolPickupExpected: z.boolean(), userTimezone: z.string().optional(), requestMode: z.enum(COPILOT_REQUEST_MODES).optional(), @@ -61,6 +68,27 @@ export const StreamRecoveryConfigSchema = z } ) +type PersistedStreamRecoveryConfig = z.output + +/** Reads a stored recovery intent, exposing its route as `workerRoute`. */ +export const StreamRecoveryConfigSchema = PersistedStreamRecoveryConfigSchema.transform( + ({ goRoute, ...config }) => ({ ...config, workerRoute: goRoute }) +) + +/** A recovery intent as code builds it, before it is validated for storage. */ +export type StreamRecoveryConfigInput = Omit< + z.input, + 'goRoute' +> & { workerRoute: (typeof WORKER_ROUTES)[number] } + +/** Validates a recovery intent and returns the shape stored on the run. */ +export function persistedStreamRecoveryConfig({ + workerRoute, + ...config +}: StreamRecoveryConfigInput): PersistedStreamRecoveryConfig { + return PersistedStreamRecoveryConfigSchema.parse({ ...config, goRoute: workerRoute }) +} + export async function streamRecoveryConfig( options: CopilotLifecycleOptions, request: Record @@ -81,10 +109,10 @@ export async function streamRecoveryConfig( options.executionContext?.copilotResourceAdmission ?? options.copilotResourceAdmission, }) ) - return StreamRecoveryConfigSchema.parse({ + return PersistedStreamRecoveryConfigSchema.parse({ kind, request, - goRoute: options.goRoute ?? '/api/copilot', + goRoute: options.workerRoute ?? '/api/copilot', clientToolPickupExpected: options.clientToolPickupExpected ?? true, userTimezone: options.executionContext?.userTimezone, requestMode: options.executionContext?.requestMode, diff --git a/apps/sim/lib/mothership/request/lifecycle/resume-leg-context.test.ts b/apps/sim/lib/mothership/request/lifecycle/resume-leg-context.test.ts deleted file mode 100644 index 22f4bee7393..00000000000 --- a/apps/sim/lib/mothership/request/lifecycle/resume-leg-context.test.ts +++ /dev/null @@ -1,162 +0,0 @@ -import { describe, expect, it, vi } from 'vitest' -import { MothershipStreamV1CompletionStatus } from '@/lib/mothership/generated/mothership-stream-v1' -import { createStreamingContext } from '@/lib/mothership/request/context/request-context' -import { - createProviderToolCallIdentity, - restoreProviderToolCallId, - scopeProviderToolCallId, -} from '@/lib/mothership/request/go/tool-call-identity' - -/** Table side effects are not exercised here, and the real module loads the table application layer. */ -vi.mock('@/lib/mothership/request/tools/tables', () => ({ - maybeWriteOutputToTable: vi.fn(async (_toolName, _params, result) => result), - maybeWriteReadCsvToTable: vi.fn(async (_toolName, _params, result) => result), -})) - -import { makeResumeLegContext, mergeResumeLegOutputs } from '@/lib/mothership/request/lifecycle/run' - -// Guards the makeResumeLegContext / mergeResumeLegOutputs contract: the two MUST -// stay in lockstep (every per-leg-isolated scalar is reset on leg creation and -// folded back on merge), and the heavy accumulators stay shared by reference so -// all concurrent legs build one chat. This is the regression the inline comment -// warns about — without per-leg isolation the orchestrator's pre-fanout content -// gets multiplied by the leg count on merge. -// -// `wasAborted` is the one deliberate exception to "reset AND folded back": it is -// reset per leg but folded back only for a turn-level abort, because a fanout -// cancelling its own lanes must not mark the shared turn aborted. -describe('resume leg context isolate/merge contract', () => { - it('shares provider identity mappings across sibling legs and retries', () => { - const identity = createProviderToolCallIdentity('run-1') - const base = createStreamingContext({ providerToolCallIdentity: identity }) - const first = makeResumeLegContext(base) - const second = makeResumeLegContext(base) - const canonicalId = scopeProviderToolCallId('child-call', identity) - - expect(first.providerToolCallIdentity).toBe(identity) - expect(second.providerToolCallIdentity).toBe(identity) - expect(restoreProviderToolCallId(canonicalId, second.providerToolCallIdentity)).toBe( - 'child-call' - ) - expect(scopeProviderToolCallId('child-call', identity)).toBe(canonicalId) - expect(makeResumeLegContext(createStreamingContext()).providerToolCallIdentity).toBeUndefined() - }) - - it('isolates the per-leg scalars while sharing the heavy accumulators by reference', () => { - const base = createStreamingContext({ - accumulatedContent: 'PRE', - finalAssistantContent: 'PRE-FINAL', - usage: { prompt: 10, completion: 5 }, - cost: { input: 1, output: 2, total: 3 }, - errors: ['pre-existing'], - completionStatus: MothershipStreamV1CompletionStatus.complete, - }) - - const leg = makeResumeLegContext(base) - - // Per-leg scalars reset so a leg accumulates only its OWN output. - expect(leg.accumulatedContent).toBe('') - expect(leg.finalAssistantContent).toBe('') - expect(leg.usage).toBeUndefined() - expect(leg.cost).toBeUndefined() - expect(leg.errors).toEqual([]) - expect(leg.streamComplete).toBe(false) - expect(leg.awaitingAsyncContinuation).toBeUndefined() - expect(leg.completionStatus).toBeUndefined() - // A leg must never be born aborted — that is what let one cancelled lane - // cancel every tool dispatched on every lane created after it. - expect(leg.wasAborted).toBe(false) - - // A leg's own errors array is a fresh array (not the shared one) so a leg's - // retry rollback can't truncate a sibling's errors. - expect(leg.errors).not.toBe(base.errors) - - // Heavy accumulators stay shared by reference (one merged chat). - expect(leg.contentBlocks).toBe(base.contentBlocks) - expect(leg.toolCalls).toBe(base.toolCalls) - expect(leg.pendingToolPromises).toBe(base.pendingToolPromises) - expect(leg.subAgentContent).toBe(base.subAgentContent) - }) - - it('folds a leg back exactly once (no double-count of the orchestrator content)', () => { - const base = createStreamingContext({ accumulatedContent: 'PRE', errors: ['pre'] }) - - const leg = makeResumeLegContext(base) - leg.accumulatedContent = 'JOIN' - leg.finalAssistantContent = 'JOIN-FINAL' - leg.usage = { prompt: 100, completion: 50 } - leg.cost = { input: 4, output: 5, total: 9 } - leg.errors.push('leg-err') - leg.completionStatus = MothershipStreamV1CompletionStatus.complete - - mergeResumeLegOutputs(base, leg) - - // PRE seeded once + the leg's own output appended once — not PRE+PRE+JOIN. - expect(base.accumulatedContent).toBe('PREJOIN') - expect(base.finalAssistantContent).toBe('JOIN-FINAL') - expect(base.usage).toEqual({ prompt: 100, completion: 50 }) - expect(base.cost).toEqual({ input: 4, output: 5, total: 9 }) - expect(base.errors).toEqual(['pre', 'leg-err']) - expect(base.completionStatus).toBe(MothershipStreamV1CompletionStatus.complete) - }) - - it('does not fold a fanout-induced abort onto the shared turn', () => { - // A lane that fails cancels its siblings by design, and each cancelled - // sibling returns normally with wasAborted set. Folding that marked the - // SHARED context aborted, so every leg created afterwards was born aborted - // and every tool it dispatched was cancelled before dispatch — which the - // subagent join then reported as a fatal "missing result". - const base = createStreamingContext({}) - const leg = makeResumeLegContext(base) - leg.wasAborted = true - - mergeResumeLegOutputs(base, leg, false) - - expect(base.wasAborted).toBe(false) - }) - - it('folds a turn-level abort onto the shared turn', () => { - // The other half: a real Stop (or an observed abort marker) must reach the - // shared context, because that is what classifies the request as cancelled - // rather than successful. - const base = createStreamingContext({}) - const leg = makeResumeLegContext(base) - leg.wasAborted = true - - mergeResumeLegOutputs(base, leg, true) - - expect(base.wasAborted).toBe(true) - }) - - it('leaves the turn unfinished when only child legs fold back', () => { - const base = createStreamingContext() - - // A child leg that folds with a terminal pause never carries the turn's - // terminal event, so it must not report the turn as finished. - const childLeg = makeResumeLegContext(base) - childLeg.errors.push('subagent failed') - mergeResumeLegOutputs(base, childLeg) - - expect(base.completionStatus).toBeUndefined() - }) - - it('does not multiply pre-fanout content across many legs (N children + one join leg)', () => { - const base = createStreamingContext({ accumulatedContent: 'PRE' }) - - // Seven child legs that stream subagent content (not main accumulatedContent) - // contribute nothing to the join scalars; only the join-carrying leg does. - for (let i = 0; i < 7; i++) { - const childLeg = makeResumeLegContext(base) - mergeResumeLegOutputs(base, childLeg) - } - const joinLeg = makeResumeLegContext(base) - joinLeg.accumulatedContent = 'SUMMARY' - joinLeg.usage = { prompt: 1, completion: 1 } - mergeResumeLegOutputs(base, joinLeg) - - // Exactly the pre-fanout content + the one join leg's summary — the 7 child - // legs must not each re-append 'PRE'. - expect(base.accumulatedContent).toBe('PRESUMMARY') - expect(base.usage).toEqual({ prompt: 1, completion: 1 }) - }) -}) diff --git a/apps/sim/lib/mothership/request/lifecycle/run.test.ts b/apps/sim/lib/mothership/request/lifecycle/run.test.ts index 62d2ad4fe1c..f5c80594764 100644 --- a/apps/sim/lib/mothership/request/lifecycle/run.test.ts +++ b/apps/sim/lib/mothership/request/lifecycle/run.test.ts @@ -23,12 +23,12 @@ import { } from '@sim/testing/mocks/workspace-file-secret-provenance.mock' import { generateId } from '@sim/utils/id' import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { scopeProviderToolCallId } from '@/lib/mothership/request/go/tool-call-identity' import { handleBillingLimitResponse, handleChatPlanRequiredResponse, } from '@/lib/mothership/request/tools/billing' import type { ExecutionContext, StreamingContext } from '@/lib/mothership/request/types' +import { scopeProviderToolCallId } from '@/lib/mothership/request/worker/tool-call-identity' import { executeFunctionExecute } from '@/lib/mothership/tools/handlers/function-execute' import { executeRunCode } from '@/lib/mothership/tools/handlers/run-code' import { openResourceServerTool } from '@/lib/mothership/tools/server/open-resource' @@ -92,7 +92,7 @@ vi.mock( vi.mock('@/lib/mothership/async-runs/repository', () => mothershipAsyncRunsMock) -vi.mock('@/lib/mothership/request/go/stream', () => { +vi.mock('@/lib/mothership/request/worker/stream', () => { class CopilotBackendError extends Error { status?: number @@ -237,19 +237,19 @@ import { MothershipStreamV1CompletionStatus, MothershipStreamV1ToolOutcome, } from '@/lib/mothership/generated/mothership-stream-v1' -import { - BillingLimitError, - CopilotBackendError, - STREAM_ENDED_WITHOUT_TERMINAL_MESSAGE, - StreamEndedWithoutTerminalError, - WorkerUnreachableError, -} from '@/lib/mothership/request/go/stream' import { runCopilotLifecycle } from '@/lib/mothership/request/lifecycle/run' import { REPLAY_BUDGET_EXHAUSTED_CODE, StreamReplayBudgetExhaustedError, } from '@/lib/mothership/request/session/replay-budget' import { executeToolAndReport } from '@/lib/mothership/request/tools/executor' +import { + BillingLimitError, + CopilotBackendError, + STREAM_ENDED_WITHOUT_TERMINAL_MESSAGE, + StreamEndedWithoutTerminalError, + WorkerUnreachableError, +} from '@/lib/mothership/request/worker/stream' const mockExecuteAppTool = toolsMockFns.mockExecuteTool const { mockCreateRunSegment, mockUpdateRunStatus } = mothershipAsyncRunsMockFns @@ -305,12 +305,12 @@ describe('runCopilotLifecycle', () => { }) it.each([ - { surface: 'web Search', goRoute: '/api/mothership', interactive: true }, - { surface: 'Slack Search', goRoute: '/api/mothership', interactive: false }, - { surface: 'MCP Search', goRoute: '/api/mothership/execute', interactive: false }, + { surface: 'web Search', workerRoute: '/api/mothership', interactive: true }, + { surface: 'Slack Search', workerRoute: '/api/mothership', interactive: false }, + { surface: 'MCP Search', workerRoute: '/api/mothership/execute', interactive: false }, ])( 'preserves the admitted canonical inventory for $surface without loading it again', - async ({ goRoute, interactive }) => { + async ({ workerRoute, interactive }) => { mockRunStreamLoop.mockResolvedValue(undefined) const inventory = [ { type: 'search_integrations', content: '{"connections":[],"available":[]}' }, @@ -323,7 +323,7 @@ describe('runCopilotLifecycle', () => { chatId: 'private-chat-1', executionId: 'execution-1', runId: 'run-1', - goRoute, + workerRoute, interactive, } ) @@ -347,7 +347,7 @@ describe('runCopilotLifecycle', () => { userId: 'user-1', workspaceId: 'ws-1', chatId: 'chat-1', - goRoute: '/api/mothership', + workerRoute: '/api/mothership', interactive: true, } ) @@ -386,7 +386,7 @@ describe('runCopilotLifecycle', () => { : { mode, message: 'Inspect Grafana with the organization key' }, { ...owner, - goRoute: phase === 'resume' ? '/api/tools/resume' : '/api/mothership', + workerRoute: phase === 'resume' ? '/api/tools/resume' : '/api/mothership', ...(phase === 'resume' ? { executionContext: { @@ -470,7 +470,7 @@ describe('runCopilotLifecycle', () => { userId: 'user-1', ...owner, chatId: 'chat-1', - goRoute: '/api/tools/resume', + workerRoute: '/api/tools/resume', executionContext: { userId: 'user-1', ...owner, @@ -873,7 +873,7 @@ describe('runCopilotLifecycle', () => { }) }) - it('preserves ordinary initial Go payload fields that collide with a configured secret', async () => { + it('preserves ordinary initial worker payload fields that collide with a configured secret', async () => { const secret = 'mothership-secret' const registry = new ResolvedSecretTraceRegistry([ { name: 'TOKEN', plaintext: secret, encryptedValue: 'ciphertext' }, @@ -927,7 +927,7 @@ describe('runCopilotLifecycle', () => { it.each(['/api/mothership', '/api/copilot', '/api/mothership/execute'])( 'forces benchmark checkpoint transport over caller claims on %s', - async (goRoute) => { + async (workerRoute) => { setEnvFlags({ isMothershipBenchmarkEnabled: true }) let captured = '' mockRunStreamLoop.mockImplementationOnce(async (_url: string, request: RequestInit) => { @@ -939,7 +939,7 @@ describe('runCopilotLifecycle', () => { userId: 'user-1', workspaceId: 'ws-1', benchmark: 'distill', - goRoute, + workerRoute, } ) expect(JSON.parse(captured).simConnection).toEqual({ @@ -1676,7 +1676,7 @@ describe('runCopilotLifecycle', () => { expect(JSON.parse(capturedRequestBody).responseFormat.schema).toEqual(schema) }) - it('does not block ordinary initial Go payloads on unrelated incomplete provenance', async () => { + it('does not block ordinary initial worker payloads on unrelated incomplete provenance', async () => { const registry = new ResolvedSecretTraceRegistry() registry.markIncomplete('unspecified') @@ -1733,7 +1733,7 @@ describe('runCopilotLifecycle', () => { expect(canonicalIds[0]).not.toBe(canonicalIds[1]) }) - it('returns the original provider call ID at the shared Go resume boundary', async () => { + it('returns the original provider call ID at the shared worker resume boundary', async () => { let canonicalId = '' mockRunStreamLoop.mockImplementationOnce( async (_url: string, _options: RequestInit, context: StreamingContext) => { @@ -1774,7 +1774,7 @@ describe('runCopilotLifecycle', () => { ]) }) - it('does not resume Go with a canonical ID after its reverse mapping is lost', async () => { + it('does not resume the worker with a canonical ID after its reverse mapping is lost', async () => { mockRunStreamLoop.mockImplementationOnce( async (_url: string, _options: RequestInit, context: StreamingContext) => { if (!context.providerToolCallIdentity) throw new Error('Identity map not initialized') @@ -1814,7 +1814,7 @@ describe('runCopilotLifecycle', () => { chatId: 'chat-1', executionId: 'exec-1', runId: 'run-1', - goRoute: '/api/mothership', + workerRoute: '/api/mothership', executionContext: { userId: 'user-1', workflowId: '', @@ -1912,7 +1912,7 @@ describe('runCopilotLifecycle', () => { userId: 'user-1', workspaceId: 'ws-1', chatId: 'chat-1', - goRoute: '/api/copilot', + workerRoute: '/api/copilot', executionContext: { userId: 'user-1', workflowId: 'wf-1', @@ -2789,7 +2789,7 @@ describe('runCopilotLifecycle', () => { ) // 2) First resume leg is refused before the backend takes it: it records an - // error AND throws a retryable 5xx, which releases the claim in Go. + // error AND throws a retryable 5xx, which releases the claim in the worker. mockRunStreamLoop.mockImplementationOnce( async ( _fetchUrl: string, @@ -4196,154 +4196,40 @@ describe('runCopilotLifecycle', () => { } }) - it.each(['main', 'subagent'])( - 'does not invent a result for an unconfirmed %s tool', - async (lane) => { - const bodies: Array> = [] - mockRunStreamLoop.mockImplementation( - async ( - fetchUrl: string, - fetchOptions: RequestInit, - context: StreamingContext - ): Promise => { - if (!fetchUrl.includes('/api/tools/resume')) { - context.toolCalls.set('tool-unconfirmed', { - id: 'tool-unconfirmed', - name: 'sim_cli', - status: 'executing', - }) - context.awaitingAsyncContinuation = { - checkpointId: 'cp-root', - pendingToolCallIds: lane === 'main' ? ['tool-unconfirmed'] : [], - frames: - lane === 'subagent' - ? [ - { - parentToolCallId: 'subagent-file', - parentToolName: 'file', - pendingToolIds: ['tool-unconfirmed'], - checkpointId: 'cp-file', - }, - ] - : [], - } - return - } - bodies.push(JSON.parse(String(fetchOptions.body))) - context.streamComplete = true - context.completionStatus = MothershipStreamV1CompletionStatus.complete - } - ) - - const result = await runCopilotLifecycle( - { message: 'hello', messageId: 'stream-missing-subagent-result' }, - { userId: 'user-1', workspaceId: 'ws-1' } - ) - - expect(bodies).toHaveLength(0) - expect(result.success).toBe(false) - expect(result.error).toContain('No confirmed result is available') - expect(executeToolAndReport).not.toHaveBeenCalled() - } - ) - - it('cancels promptly while a per-subagent tool promise remains unsettled', async () => { - const controller = new AbortController() - const addAbortListener = vi.spyOn(controller.signal, 'addEventListener') - const fetchUrls: string[] = [] - const captured: { context?: StreamingContext } = {} - mockRunStreamLoop.mockImplementationOnce( - async (fetchUrl: string, _fetchOptions: RequestInit, context: StreamingContext) => { - fetchUrls.push(fetchUrl) - captured.context = context - context.toolCalls.set('tool-hung', { - id: 'tool-hung', - name: 'read', - status: 'executing', - }) - context.pendingToolPromises.set('tool-hung', new Promise(() => {})) - context.awaitingAsyncContinuation = { - checkpointId: 'cp-root', - pendingToolCallIds: ['tool-hung'], - frames: [ - { - parentToolCallId: 'subagent-file', - parentToolName: 'file', - pendingToolIds: ['tool-hung'], - checkpointId: 'cp-file', - }, - ], - } - } - ) - - const lifecycle = runCopilotLifecycle( - { message: 'hello', messageId: 'stream-aborted-subagent-wait' }, - { userId: 'user-1', workspaceId: 'ws-1', abortSignal: controller.signal } - ) - - await vi.waitFor(() => { - expect(addAbortListener).toHaveBeenCalledWith('abort', expect.any(Function), { once: true }) - }) - controller.abort('user_stop') - const result = await lifecycle - - expect(result.success).toBe(false) - expect(result.cancelled).toBe(true) - expect(fetchUrls).toEqual(['http://mothership.test/api/copilot']) - expect(mockForceFailHungToolCall).not.toHaveBeenCalled() - expect(captured.context?.toolCalls.get('tool-hung')).toMatchObject({ - status: MothershipStreamV1ToolOutcome.cancelled, - error: 'Stopped by user', - }) - }) - - it('classifies a Stop landing during a subagent fanout as cancelled', async () => { - // Guards the trap in the fanout fix: `wasAborted` is now isolated per leg, so - // a user Stop must still reach the turn — via the abort signal or the folded - // turn-level abort — or a cancelled turn would be reported as a success. - const controller = new AbortController() + it('does not invent a result for an unconfirmed tool', async () => { + const bodies: Array> = [] mockRunStreamLoop.mockImplementation( async ( fetchUrl: string, - _fetchOptions: RequestInit, + fetchOptions: RequestInit, context: StreamingContext ): Promise => { if (!fetchUrl.includes('/api/tools/resume')) { - context.toolCalls.set('tool-done', { - id: 'tool-done', - name: 'read', - status: MothershipStreamV1ToolOutcome.success, - result: { success: true }, - endTime: Date.now(), + context.toolCalls.set('tool-unconfirmed', { + id: 'tool-unconfirmed', + name: 'sim_cli', + status: 'executing', }) context.awaitingAsyncContinuation = { checkpointId: 'cp-root', - pendingToolCallIds: [], - frames: [ - { - parentToolCallId: 'subagent-file', - parentToolName: 'file', - pendingToolIds: ['tool-done'], - checkpointId: 'cp-file', - }, - ], + pendingToolCallIds: ['tool-unconfirmed'], } return } - // The user hits Stop mid-fanout. `wasAborted` is isolated per leg now, so - // the turn's own signal is what has to carry the cancellation into the - // classification — reading only `context.wasAborted` reports success. - controller.abort('user_stop') + bodies.push(JSON.parse(String(fetchOptions.body))) + context.streamComplete = true + context.completionStatus = MothershipStreamV1CompletionStatus.complete } ) const result = await runCopilotLifecycle( - { message: 'hello', messageId: 'stream-stop-during-fanout' }, - { userId: 'user-1', workspaceId: 'ws-1', abortSignal: controller.signal } + { message: 'hello', messageId: 'stream-missing-result' }, + { userId: 'user-1', workspaceId: 'ws-1' } ) - expect(result.cancelled).toBe(true) + expect(bodies).toHaveLength(0) expect(result.success).toBe(false) + expect(result.error).toContain('No confirmed result is available') + expect(executeToolAndReport).not.toHaveBeenCalled() }) }) diff --git a/apps/sim/lib/mothership/request/lifecycle/run.ts b/apps/sim/lib/mothership/request/lifecycle/run.ts index 135b1bc3f0c..c1c89e280a8 100644 --- a/apps/sim/lib/mothership/request/lifecycle/run.ts +++ b/apps/sim/lib/mothership/request/lifecycle/run.ts @@ -42,20 +42,11 @@ import { MothershipStreamV1ToolOutcome, } from '@/lib/mothership/generated/mothership-stream-v1' import type { ResumeRequest, StreamResponseReceipt } from '@/lib/mothership/generated/protocol' -import { CopilotDegradedReason } from '@/lib/mothership/generated/trace-attribute-values-v1' +import { CopilotDegradedReason } from '@/lib/mothership/observability/trace-attribute-values' import { getAutoAllowedTools } from '@/lib/mothership/persistence/tool-permission/auto-allow' import { createStreamingContext } from '@/lib/mothership/request/context/request-context' import { buildToolCallSummaries } from '@/lib/mothership/request/context/result' import { resolveEnterpriseByokKey } from '@/lib/mothership/request/enterprise-byok' -import { - BillingLimitError, - CopilotBackendError, - runStreamLoop, -} from '@/lib/mothership/request/go/stream' -import { - createProviderToolCallIdentity, - restoreProviderToolCallId, -} from '@/lib/mothership/request/go/tool-call-identity' import { mothershipRequestHeaders } from '@/lib/mothership/request/headers' import { authorizeLifecycleContinuation, @@ -64,7 +55,6 @@ import { import type { DurableStreamKind } from '@/lib/mothership/request/lifecycle/durable-run' import { StreamRetryWindow } from '@/lib/mothership/request/lifecycle/stream-retry' import { recordDegraded } from '@/lib/mothership/request/metrics' -import { AbortReason } from '@/lib/mothership/request/session/abort-reason' import { StreamControllerSupersededError } from '@/lib/mothership/request/session/controller-lease' import { turnFailure } from '@/lib/mothership/request/session/turn-failure' import { @@ -81,16 +71,22 @@ import { failPendingToolCall, pendingToolWaitBudgetMs, } from '@/lib/mothership/request/tools/executor' -import { RequestTraceV1SpanStatus, type TraceCollector } from '@/lib/mothership/request/trace' import type { ExecutionContext, OrchestratorOptions, OrchestratorResult, - ResumeContinuation, - ResumeFrame, StreamEvent, StreamingContext, } from '@/lib/mothership/request/types' +import { + BillingLimitError, + CopilotBackendError, + runStreamLoop, +} from '@/lib/mothership/request/worker/stream' +import { + createProviderToolCallIdentity, + restoreProviderToolCallId, +} from '@/lib/mothership/request/worker/tool-call-identity' import { NO_SECRET_AUTHORITY, normalizeSecretMountPolicy, @@ -302,8 +298,8 @@ export interface CopilotLifecycleOptions extends OrchestratorOptions { chatId?: string executionId?: string runId?: string - goRoute?: string - /** Reattach this existing run; goRoute still identifies the original interaction surface. */ + workerRoute?: string + /** Reattach this existing run; workerRoute still identifies the original interaction surface. */ recovery?: { kind?: DurableStreamKind billingAdmission?: { billingRequestId: string; serializedAttribution: string } @@ -312,10 +308,8 @@ export interface CopilotLifecycleOptions extends OrchestratorOptions { userTimezone?: string requestMode?: string } - trace?: TraceCollector simRequestId?: string otelContext?: Context - onGoTraceId?: (goTraceId: string) => void executionContext?: ExecutionContext billingAttribution?: BillingAttributionSnapshot onBillingAdmission?: (admission: AttributedBillingRequestEnvelope) => Promise @@ -344,7 +338,7 @@ async function resolveToolPermissions( const enabled = isCopilotToolPermissionsEnabled && options.interactive !== false && - (options.goRoute ?? '').startsWith('/api/mothership') + (options.workerRoute ?? '').startsWith('/api/mothership') if (!enabled) return { enabled: false, autoAllowed: new Set(), autoAllowPermitted: true } /** @@ -398,13 +392,13 @@ export async function runCopilotLifecycle( chatId, executionId, runId, - goRoute = '/api/copilot', + workerRoute = '/api/copilot', } = options - const isContinuation = Boolean(options.recovery) || goRoute === '/api/tools/resume' + const isContinuation = Boolean(options.recovery) || workerRoute === '/api/tools/resume' const payloadMode = nonBlankString(requestPayload.mode) const requestMode = options.recovery ? payloadMode - : goRoute === '/api/tools/resume' + : workerRoute === '/api/tools/resume' ? (options.executionContext?.requestMode ?? payloadMode) : payloadMode if ( @@ -563,11 +557,10 @@ export async function runCopilotLifecycle( runId: resolvedRunId, messageId: payloadMsgId, providerToolCallIdentity: - goRoute === '/api/tools/resume' + workerRoute === '/api/tools/resume' ? undefined : createProviderToolCallIdentity(resolvedRunId ?? generateId()), toolPermissions: await resolveToolPermissions(lifecycleOptions), - ...(lifecycleOptions.trace ? { trace: lifecycleOptions.trace } : {}), }) if (options.recovery) { const { restoreStreamingContext } = await import('@/lib/mothership/request/context/restore') @@ -577,9 +570,8 @@ export async function runCopilotLifecycle( try { // Hosted admission (plan and usage limits) belongs HERE, at dispatch, with the attribution - // already in hand. The old path outsourced it to the Go backend's api-keys/validate - // callback — a call the TS worker rightly never makes, so without this gate the - // limit check simply never runs on the new path. On exceeded, the same synthetic + // already in hand: the worker does not call api-keys/validate for a turn Sim + // dispatches, so without this gate the limit check never runs. On exceeded, the same synthetic // 402 UX as the mid-stream path renders the upgrade prompt, the backend is never // dispatched, and the shared verdict assembly below runs exactly as after a // mid-stream billing break. @@ -622,7 +614,7 @@ export async function runCopilotLifecycle( context, execContext, lifecycleOptions, - goRoute, + workerRoute, hostedBillingRequest ) } catch (error) { @@ -651,11 +643,8 @@ export async function runCopilotLifecycle( // A turn failure (such as a refused replay write) aborts the turn to stop it, but // the turn failed; it was not stopped by the user. const failure = turnFailure(lifecycleOptions.abortSignal?.reason) - // Consult the lifecycle signal as well as the flag. `context.wasAborted` is - // only reached from a fanout leg through the (deliberately asymmetric) merge - // in `mergeResumeLegOutputs`, so a Stop landing mid-fanout could otherwise - // classify the turn as a success. Mirrors the check already used below on - // the throw path. + // Consult the lifecycle signal as well as the flag, as the throw path below does: + // a Stop can land without the stream ever reporting it. const turnWasAborted = !failure && (context.completionStatus === MothershipStreamV1CompletionStatus.cancelled || @@ -723,7 +712,7 @@ export async function runCopilotLifecycle( }) // If the abort signal fired, this throw is a consequence of the // cancel (publisher.publish fails once the client disconnects, a - // downstream Go read throws on ctx cancel, etc.) — NOT a real + // downstream worker read throws on cancel, etc.) — NOT a real // backend error. Don't invoke `onError`, because on the cancel // path `onComplete(cancelled)` persists partial content with an // idempotent row-locked finalizer. `onError` would race with it via @@ -788,127 +777,6 @@ export async function runCopilotLifecycle( } } -// Per-subagent checkpoint resume (concurrent fan-out) -// -// Under the per-subagent checkpoint model each paused subagent is its OWN -// checkpoint chain (frame.checkpointId) joined at the orchestrator. Instead of -// one bundled /resume, Sim drives one resume chain per child CONCURRENTLY so a -// fast child never waits on a slow sibling, and the Go join wakes the -// orchestrator on whichever child finishes last. Gated by the Go -// `parallel-subagents` flag, surfaced here purely by frames carrying their own -// checkpointId. -// -// IMPORTANT (concurrency): JS is single-threaded, so the legs interleave at await -// points rather than running truly in parallel; shared accumulators -// (contentBlocks, toolCalls maps, errors) are appended via atomic synchronous -// ops and stay shared by reference. Only the per-leg STREAM CONTROL flags -// (streamComplete, awaitingAsyncContinuation) and the join-leg scalars -// (accumulatedContent/usage/cost) are isolated per leg and merged back. - -type AsyncContinuation = ResumeContinuation - -function isPerSubagentContinuation(c: AsyncContinuation): boolean { - return !!c.frames && c.frames.length > 0 && c.frames.every((f) => !!f.checkpointId) -} - -// Shared header set for every Sim -> Go mothership request (initial stream and -// every resume leg), so the auth/source/version headers can't drift between the -// sequential path and the concurrent per-subagent resume legs. -// makeResumeLegContext / mergeResumeLegOutputs are a PAIR and must stay in -// lockstep: every field reset here is folded back there, and nothing else on -// StreamingContext is per-leg. Everything not listed is shared BY REFERENCE -// across all concurrent legs (the one merged chat: contentBlocks, toolCalls, -// pendingToolPromises, subagent maps, etc.). The per-leg ISOLATED set: -// - streamComplete / awaitingAsyncContinuation: stream-control flags, so a -// finished leg can't stop a sibling's read loop (reset only; not merged). -// - accumulatedContent / finalAssistantContent / usage / cost: join-leg -// scalars — only the join-carrying leg sets them; zeroing per leg keeps the -// `+=` merge from multiplying the orchestrator's pre-fanout content by the -// leg count, and keeps a child leg's stale usage/cost from clobbering the -// join leg's real totals on merge. -// - errors: a leg's transient retryable error (rolled back inside -// runResumeLegWithRetry) must not truncate a concurrent sibling's shared -// error array by index; each leg collects its own and merges the survivors. -// - completionStatus: the backend's terminal verdict, set only on the leg that -// carries the turn to its end; a stale one from a sibling would speak for a -// turn that leg never finished. -// - wasAborted: the ONE field with an asymmetric fold. Cancelling a fanout -// cancels its siblings by design, and each cancelled sibling returns -// normally with wasAborted set — folding that unconditionally marked the -// SHARED context aborted, so every later leg was born aborted and every tool -// it dispatched was cancelled before dispatch. Reset per leg, and fold back -// only for a turn-level abort (see mergeResumeLegOutputs). -// When adding a per-leg field, update BOTH functions (and the contract test in -// resume-leg-context.test.ts). Exported only for that test. -export function makeResumeLegContext(base: StreamingContext): StreamingContext { - return { - ...base, - streamComplete: false, - awaitingAsyncContinuation: undefined, - accumulatedContent: '', - finalAssistantContent: '', - usage: undefined, - cost: undefined, - errors: [], - completionStatus: undefined, - wasAborted: false, - } -} - -// mergeResumeLegOutputs folds a finished leg's isolated scalars back into the -// shared context. Child (subagent-lane) legs leave the join scalars empty; only -// the join-carrying leg (which streams the orchestrator continuation) sets them. -// -// `turnWasAborted` is the caller's answer to "was this abort the turn's, or just -// this fanout cancelling its own lanes?". Only a turn-level abort belongs on the -// shared context: it is what `runCopilotLifecycle` reads to classify the request -// as cancelled, and on the headless path (which never wires `onAbortObserved`) -// it is the only record that the abort marker was ever observed. -export function mergeResumeLegOutputs( - context: StreamingContext, - leg: StreamingContext, - turnWasAborted = true -): void { - if (leg.accumulatedContent) context.accumulatedContent += leg.accumulatedContent - if (leg.finalAssistantContent) context.finalAssistantContent += leg.finalAssistantContent - if (leg.usage) context.usage = leg.usage - if (leg.cost) context.cost = leg.cost - if (leg.sawMainToolCall) context.sawMainToolCall = true - if (leg.wasAborted && turnWasAborted) context.wasAborted = true - if (leg.errors.length > 0) context.errors.push(...leg.errors) - if (leg.completionStatus) context.completionStatus = leg.completionStatus -} - -async function waitForToolIds( - context: StreamingContext, - toolIds: string[], - abortSignal?: AbortSignal -): Promise { - const promises: Promise[] = [] - for (const id of toolIds) { - const p = context.pendingToolPromises.get(id) - if (p) promises.push(p) - } - if (promises.length === 0) return true - if (!abortSignal) { - await Promise.allSettled(promises) - return true - } - if (abortSignal.aborted) return false - - let onAbort = () => {} - const aborted = new Promise((resolve) => { - onAbort = () => resolve(false) - abortSignal.addEventListener('abort', onAbort, { once: true }) - if (abortSignal.aborted) onAbort() - }) - try { - return await Promise.race([Promise.allSettled(promises).then(() => true as const), aborted]) - } finally { - abortSignal.removeEventListener('abort', onAbort) - } -} - interface ResumeToolResult { callId: string name: string @@ -946,236 +814,6 @@ function buildResumeToolResult( } } -function collectResultsForToolIds( - context: StreamingContext, - toolIds: string[], - checkpointId: string -): ResumeToolResult[] { - return toolIds.map((toolCallId) => buildResumeToolResult(context, toolCallId, checkpointId)) -} - -/** - * A child connection has the same recovery budget as a main connection. Failed - * attempts roll back only this lane's errors, preserving concurrent sibling work. - * Stop and sibling cancellation interrupt recovery before another request starts. - */ -async function runResumeLegWithRetry( - url: string, - body: Record, - leg: StreamingContext, - execContext: ExecutionContext, - options: CopilotLifecycleOptions, - hostedBillingRequest?: AttributedBillingRequestEnvelope -): Promise { - const retry = new StreamRetryWindow(options.timeout) - /** A leg that streams again has recovered; a later outage gets its own budget. */ - const legOptions: CopilotLifecycleOptions = { - ...options, - onEvent: async (event) => { - retry.recovered() - await options.onEvent?.(event) - }, - } - for (;;) { - options.abortSignal?.throwIfAborted() - const errorsBeforeAttempt = leg.errors.length - await authorizeLifecycleContinuation(execContext) - try { - await runStreamLoop( - url, - { - method: 'POST', - headers: mothershipRequestHeaders(hostedBillingRequest, options.simRequestId), - body: JSON.stringify(body), - }, - leg, - execContext, - { ...legOptions, timeout: retry.remainingMs() } - ) - return - } catch (error) { - const backoff = retry.nextDelay(error, options.abortSignal) - if (backoff !== null) { - leg.errors.length = errorsBeforeAttempt - logger.warn('Child resume leg failed, retrying', { - attempt: retry.attempts + 1, - backoffMs: backoff, - error: toError(error).message, - ...causeForLog(error), - }) - await interruptibleSleep(backoff, options.abortSignal) - continue - } - throw error - } - } -} - -// driveOneChildChain resumes a single subagent's checkpoint chain to its end: -// resume -> (re-pause -> resume)* -> fold into join. Returns the orchestrator's -// follow-on continuation when THIS leg is the one the Go join woke (the last -// finisher whose /resume response carried the orchestrator continuation), else -// null. Re-pause vs follow-on is disambiguated by checkpoint id: a re-pause keeps -// the same child id; the join continuation is a different (orchestrator) id. -async function driveOneChildChain( - frame: ResumeFrame, - context: StreamingContext, - execContext: ExecutionContext, - options: CopilotLifecycleOptions, - baseURL: string, - /** - * The turn's own abort signal, NOT the fanout controller in `options`. Used to - * tell "the user stopped the turn" from "a lane failed and cancelled its - * siblings" when deciding whether a leg's abort belongs on the shared context. - */ - turnAbortSignal: AbortSignal | undefined, - workspaceId?: string, - hostedBillingRequest?: AttributedBillingRequestEnvelope -): Promise { - // ParentToolCallID is the SAME subagent's stable identity across re-pauses; - // the checkpoint id rotates each re-pause (the prior one is already claimed). - const parentToolCallId = frame.parentToolCallId - // Guarded (not cast): a per-subagent frame always carries its own checkpointId - // (isPerSubagentContinuation requires it), but a local guard keeps this driver - // correct on its own terms rather than trusting a caller-side invariant. - if (!frame.checkpointId) return null - let checkpointId = frame.checkpointId - let toolIds = frame.pendingToolIds - - for (;;) { - if (isAborted(options, context)) return null - - const toolsSettled = await waitForToolIds(context, toolIds, options.abortSignal) - if (!toolsSettled || isAborted(options, context)) return null - const results = collectResultsForToolIds(context, toolIds, checkpointId) - - const leg = makeResumeLegContext(context) - // The abort marker is turn-scoped (keyed on the shared messageId), so a leg - // that observes it at body close IS a turn-level abort — and on the headless - // path, where `onAbortObserved` is never wired to the turn controller, this - // is the only record of it. - let markerObserved = false - const legOptions: CopilotLifecycleOptions = { - ...options, - onAbortObserved: (reason) => { - if (reason === AbortReason.MarkerObservedAtBodyClose) markerObserved = true - options.onAbortObserved?.(reason) - }, - } - // Same per-leg BYOK rule as the main loop: this child-chain resume can also land on - // a dead run and become a hosted-key continuation without it. - const byokApiKey = await resolveEnterpriseByokKey(workspaceId) - await runResumeLegWithRetry( - `${baseURL}/api/tools/resume`, - { - streamId: context.messageId, - results, - ...(byokApiKey ? { byokApiKey } : {}), - } satisfies ResumeRequest, - leg, - execContext, - legOptions, - hostedBillingRequest - ) - mergeResumeLegOutputs(context, leg, markerObserved || (turnAbortSignal?.aborted ?? false)) - - const cont = leg.awaitingAsyncContinuation - if (!cont) { - // The last finisher's leg, whose join continuation streamed the - // orchestrator to completion (done): nothing more to drive on this leg. - return null - } - // A NON-last finisher folds with a TERMINAL pause carrying the join id but - // NO pending tools and NO frames — the child's work is done and the join - // wakes on whichever sibling finishes last. End this leg cleanly; do NOT - // mistake the join id for an orchestrator follow-on and try to resume it. - const hasPending = (cont.pendingToolCallIds?.length ?? 0) > 0 - const hasFrames = (cont.frames?.length ?? 0) > 0 - if (!hasPending && !hasFrames) { - return null - } - // Re-pause is identified by THIS subagent's stable parentToolCallId (the - // checkpoint id rotates each re-pause). If present, keep driving this child - // with its new id + leaves. - const repaused = cont.frames?.find( - (f) => f.parentToolCallId === parentToolCallId && f.checkpointId - ) - if (repaused?.checkpointId) { - checkpointId = repaused.checkpointId - toolIds = repaused.pendingToolIds - continue - } - // No frame for this subagent => the join fired and the orchestrator re-paused - // on this leg. Hand it back to the main loop to continue the turn. - return cont - } -} - -// driveSubagentChains fans out one resume chain per child frame concurrently and -// returns the single orchestrator follow-on continuation (if the orchestrator -// re-paused after the join), or null when the turn completed. -// -// Failure isolation: the legs share a per-fanout AbortController so the FIRST leg -// to fail cancels its siblings' in-flight resumes (otherwise a `Promise.all` -// reject leaves the siblings running detached — still mutating shared context and -// POSTing /resume after the turn has errored). The controller also chains off the -// caller's abort signal so a user stop cancels every leg. Each leg's failure is -// caught (so Promise.all can't reject before its siblings unwind); we then -// rethrow the first REAL error, not the AbortErrors it triggered in the siblings. -async function driveSubagentChains( - continuation: AsyncContinuation, - context: StreamingContext, - execContext: ExecutionContext, - options: CopilotLifecycleOptions, - baseURL: string, - workspaceId?: string, - hostedBillingRequest?: AttributedBillingRequestEnvelope -): Promise { - const frames = continuation.frames ?? [] - logger.info('Driving subagent checkpoint chains concurrently', { - childCount: frames.length, - checkpointIds: frames.map((f) => f.checkpointId), - }) - - const fanoutController = new AbortController() - const parentSignal = options.abortSignal - const onParentAbort = () => fanoutController.abort() - if (parentSignal) { - if (parentSignal.aborted) fanoutController.abort() - else parentSignal.addEventListener('abort', onParentAbort, { once: true }) - } - const legOptions: CopilotLifecycleOptions = { ...options, abortSignal: fanoutController.signal } - - let firstError: unknown - try { - const followOns = await Promise.all( - frames.map((frame) => - driveOneChildChain( - frame, - context, - execContext, - legOptions, - baseURL, - parentSignal, - workspaceId, - hostedBillingRequest - ).catch((error) => { - // First real failure wins and cancels the siblings; their resulting - // AbortErrors arrive later and don't overwrite it. Swallow here so - // Promise.all doesn't reject before every leg has unwound. - if (firstError === undefined) firstError = error - fanoutController.abort() - return null - }) - ) - ) - if (firstError !== undefined) throw firstError - return followOns.find((c): c is AsyncContinuation => !!c) ?? null - } finally { - parentSignal?.removeEventListener('abort', onParentAbort) - } -} - // Checkpoint loop – the core state machine async function runCheckpointLoop( @@ -1298,17 +936,6 @@ async function runCheckpointLoop( } retry ??= new StreamRetryWindow(options.timeout) - const streamSpan = context.trace.startSpan( - isResume ? 'Sim → Go (Resume)' : 'Sim → Go Stream', - isResume ? 'lifecycle.resume' : 'sim.stream', - { - route, - isResume, - ...(isResume ? { attempt: retry.attempts } : {}), - } - ) - context.trace.setActiveSpan(streamSpan) - logger.info('Starting stream loop', { route, isResume, @@ -1348,17 +975,8 @@ async function runCheckpointLoop( execContext, { ...loopOptions, timeout: retry.remainingMs() } ) - const streamStatus = isAborted(options, context) - ? RequestTraceV1SpanStatus.cancelled - : context.errors.length > 0 - ? RequestTraceV1SpanStatus.error - : RequestTraceV1SpanStatus.ok - context.trace.endSpan(streamSpan, streamStatus) - context.trace.setActiveSpan(undefined) retry = undefined } catch (streamError) { - context.trace.endSpan(streamSpan, RequestTraceV1SpanStatus.error) - context.trace.setActiveSpan(undefined) const backoff = retry?.nextDelay(streamError, options.abortSignal) ?? null if (backoff !== null) { /** A recovered connection must not finalize with an earlier transport failure. */ @@ -1395,57 +1013,11 @@ async function runCheckpointLoop( break } - let continuation = context.awaitingAsyncContinuation + const continuation = context.awaitingAsyncContinuation if (!continuation) break - // Per-subagent checkpoint model: fan out one concurrent resume chain per - // child instead of a single bundled resume. The driver returns null when the - // turn completed, or the orchestrator's follow-on continuation when it - // re-paused after the join. A per-subagent follow-on (orchestrator spawned - // more subagents) loops back through the driver; a normal follow-on falls - // through to the sequential resume path below. - if (isPerSubagentContinuation(continuation)) { - context.awaitingAsyncContinuation = undefined - let next: AsyncContinuation | null = continuation - while (next && isPerSubagentContinuation(next)) { - if (isAborted(options, context)) { - cancelPendingTools(context) - next = null - break - } - const toolsSettled = await waitForToolIds( - context, - next.pendingToolCallIds, - options.abortSignal - ) - if (!toolsSettled || isAborted(options, context)) { - next = null - break - } - next = await driveSubagentChains( - next, - context, - execContext, - options, - mothershipBaseURL, - lifecycleWorkspaceId, - hostedBillingRequest - ) - } - if (!next) { - if (isAborted(options, context)) cancelPendingTools(context) - break - } - continuation = next - } - if (context.pendingToolPromises.size > 0) { - const waitSpan = context.trace.startSpan('Wait for Tools', 'lifecycle.wait_tools', { - checkpointId: continuation.checkpointId, - pendingCount: context.pendingToolPromises.size, - }) let maximumWaitBudgetMs = 0 - let timedOutCount = 0 const pendingWatchdogs = new Map() /** @@ -1557,7 +1129,6 @@ async function runCheckpointLoop( pendingWatchdogs.delete(toolCallId) }) ) - timedOutCount += expiredTools.length continue } @@ -1594,18 +1165,6 @@ async function runCheckpointLoop( watchdogController.abort() } } - const waitWasAborted = isAborted(options, context) - waitSpan.attributes = { - ...waitSpan.attributes, - waitBudgetMs: maximumWaitBudgetMs, - timedOutCount, - aborted: waitWasAborted, - settledInTime: timedOutCount === 0 && !waitWasAborted, - } - context.trace.endSpan( - waitSpan, - waitWasAborted ? RequestTraceV1SpanStatus.cancelled : RequestTraceV1SpanStatus.ok - ) } if (isAborted(options, context)) { @@ -1664,7 +1223,6 @@ async function runCheckpointLoop( runId: continuation.runId, toolCount: results.length, pendingToolCallIds: continuation.pendingToolCallIds, - frameCount: continuation.frames?.length ?? 0, }) context.awaitingAsyncContinuation = undefined @@ -1773,7 +1331,7 @@ async function buildExecutionContext( /** * The one source of a turn's secret authority: its execution context runs under it, and a - * headless run records it so a lane that rebuilds its tool context from Go's request — the + * headless run records it so a lane that rebuilds its tool context from the worker's request — the * in-band tool route — re-derives it instead of defaulting. Assistant mode mounts nothing. */ function turnSecretAuthority( diff --git a/apps/sim/lib/mothership/request/lifecycle/start.test.ts b/apps/sim/lib/mothership/request/lifecycle/start.test.ts index ea3f19216e5..e3a41c071f8 100644 --- a/apps/sim/lib/mothership/request/lifecycle/start.test.ts +++ b/apps/sim/lib/mothership/request/lifecycle/start.test.ts @@ -17,9 +17,9 @@ import { } from '@sim/testing/mocks/mothership-async-runs.mock' import { mothershipChatStatusMock } from '@sim/testing/mocks/mothership-chat-status.mock' import { - mothershipGoFetchMock, - mothershipGoFetchMockFns, -} from '@sim/testing/mocks/mothership-go-fetch.mock' + mothershipWorkerFetchMock, + mothershipWorkerFetchMockFns, +} from '@sim/testing/mocks/mothership-worker-fetch.mock' import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' import { publishChatStatusChanged } from '@/lib/mothership/chat-status' import { @@ -149,7 +149,7 @@ vi.mock('@/lib/mothership/request/session/sse', () => ({ vi.mock('@/lib/mothership/chat-status', () => mothershipChatStatusMock) -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) @@ -158,7 +158,7 @@ import { createSSEStream, requestChatTitle } from './start' const { mockCreateRunSegment: createRunSegment, mockUpdateRunStatus: updateRunStatus } = mothershipAsyncRunsMockFns const recordRunBillingAdmission = mothershipAsyncRunsMockFns.mockRecordRunBillingAdmission -const fetchGo = mothershipGoFetchMockFns.mockFetchGo +const fetchWorker = mothershipWorkerFetchMockFns.mockFetchWorker mothershipAgentUrlMockFns.mockGetMothershipBaseURL.mockResolvedValue('https://copilot.test') const checkTitleUsage = billingAttributionMockFns.mockCheckAttributedUsageLimits @@ -183,7 +183,7 @@ describe('createSSEStream terminal error handling', () => { mockDisconnected = false resetDbChainMock() setEnvFlags({ isHosted: false }) - fetchGo.mockResolvedValue( + fetchWorker.mockResolvedValue( new Response(JSON.stringify({ title: 'Test title' }), { status: 200, headers: { @@ -353,7 +353,7 @@ describe('createSSEStream terminal error handling', () => { const chatId = '11111111-1111-4111-8111-111111111111' const workspaceId = '22222222-2222-4222-8222-222222222222' dbChainMockFns.returning.mockResolvedValue(stamped ? [{ id: chatId }] : []) - fetchGo.mockImplementation(async (_url, request) => { + fetchWorker.mockImplementation(async (_url, request) => { TitleRequest.parse(JSON.parse(request.body)) return Response.json({ title: 'Workflow planning' }) }) @@ -453,7 +453,7 @@ describe('createSSEStream terminal error handling', () => { resolveContext = resolve }) ) - fetchGo.mockResolvedValueOnce(Response.json({ title: 'Finance planning' })) + fetchWorker.mockResolvedValueOnce(Response.json({ title: 'Finance planning' })) const recoveredInventory = { workflows: [{ name: 'Invoice approval' }] } const stream = createSSEStream({ requestPayload: { message: 'Plan', inventory: recoveredInventory }, @@ -471,13 +471,15 @@ describe('createSSEStream terminal error handling', () => { }) await vi.waitFor(() => expect(buildChatTitleContext).toHaveBeenCalled()) expect(runCopilotLifecycle).toHaveBeenCalled() - expect(fetchGo).not.toHaveBeenCalled() + expect(fetchWorker).not.toHaveBeenCalled() expect(buildChatTitleContext).toHaveBeenCalledWith( expect.objectContaining({ inventory: recoveredInventory }) ) resolveContext('{"workspaceName":"Finance"}') - await vi.waitFor(() => expect(fetchGo).toHaveBeenCalled()) - expect(JSON.parse(fetchGo.mock.calls[0][1].body).context).toBe('{"workspaceName":"Finance"}') + await vi.waitFor(() => expect(fetchWorker).toHaveBeenCalled()) + expect(JSON.parse(fetchWorker.mock.calls[0][1].body).context).toBe( + '{"workspaceName":"Finance"}' + ) await drainStream(stream) }) @@ -500,7 +502,7 @@ describe('createSSEStream terminal error handling', () => { }) ) expect(runCopilotLifecycle).not.toHaveBeenCalled() - expect(fetchGo).not.toHaveBeenCalled() + expect(fetchWorker).not.toHaveBeenCalled() expect(appendEvent).toHaveBeenCalledWith( expect.objectContaining({ type: 'complete', @@ -537,11 +539,11 @@ describe('createSSEStream terminal error handling', () => { } createRunSegment.mockResolvedValueOnce({ status: 'cancelled' }) await drainStream(createSSEStream(params)) - expect(fetchGo).not.toHaveBeenCalled() + expect(fetchWorker).not.toHaveBeenCalled() expect(runCopilotLifecycle).not.toHaveBeenCalled() dbChainMockFns.returning.mockResolvedValue([{ id: chatId }]) - fetchGo.mockImplementation(async (_url, request) => { + fetchWorker.mockImplementation(async (_url, request) => { TitleRequest.parse(JSON.parse(request.body)) return Response.json({ title: 'Workflow planning' }) }) @@ -567,7 +569,7 @@ describe('createSSEStream terminal error handling', () => { ) ) expect(dbChainMockFns.set).toHaveBeenCalledWith({ title: 'Workflow planning' }) - expect(fetchGo).toHaveBeenCalledTimes(1) + expect(fetchWorker).toHaveBeenCalledTimes(1) await drainStream( createSSEStream({ @@ -578,7 +580,7 @@ describe('createSSEStream terminal error handling', () => { currentChat: { title: 'Workflow planning' }, }) ) - expect(fetchGo).toHaveBeenCalledTimes(1) + expect(fetchWorker).toHaveBeenCalledTimes(1) }) it('writes the thrown terminal error event before close for replay durability', async () => { @@ -709,8 +711,8 @@ describe('createSSEStream terminal error handling', () => { }) await drainStream(stream) - await vi.waitFor(() => expect(fetchGo).toHaveBeenCalled()) - const [, request] = fetchGo.mock.calls.at(-1) ?? [] + await vi.waitFor(() => expect(fetchWorker).toHaveBeenCalled()) + const [, request] = fetchWorker.mock.calls.at(-1) ?? [] expect(JSON.parse(request.body)).toEqual( expect.objectContaining({ message: 'hello secret-value' }) ) @@ -726,7 +728,7 @@ describe('requestChatTitle billing protocol', () => { mockDisconnected = false resetDbChainMock() setEnvFlags({ isHosted: true }) - fetchGo.mockImplementation(async (_url, request) => { + fetchWorker.mockImplementation(async (_url, request) => { const parsed = TitleRequest.safeParse(JSON.parse(request.body)) return new Response( JSON.stringify( @@ -751,7 +753,7 @@ describe('requestChatTitle billing protocol', () => { }) expect(checkTitleUsage).toHaveBeenCalledWith(BILLING_ATTRIBUTION) expect(title).toBeNull() - expect(fetchGo).not.toHaveBeenCalled() + expect(fetchWorker).not.toHaveBeenCalled() }) it('freezes and forwards a dedicated attributed identity before title work', async () => { @@ -765,10 +767,10 @@ describe('requestChatTitle billing protocol', () => { signal, }) - expect(fetchGo.mock.calls[0]?.[1]?.signal).toBe(signal) + expect(fetchWorker.mock.calls[0]?.[1]?.signal).toBe(signal) expect(title).toBe('Billing Protocol') - expect(fetchGo.mock.calls[0]?.[1]?.attributes).toBeUndefined() - const headers = fetchGo.mock.calls[0]?.[1]?.headers as Record + expect(fetchWorker.mock.calls[0]?.[1]?.attributes).toBeUndefined() + const headers = fetchWorker.mock.calls[0]?.[1]?.headers as Record const billingRequestId = headers['x-sim-billing-request-id'] expect(billingRequestId).toMatch( /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i @@ -794,7 +796,7 @@ describe('requestChatTitle billing protocol', () => { billingAttribution: attribution, }) ).resolves.toBe('Billing Protocol') - const options = fetchGo.mock.calls[0]?.[1] + const options = fetchWorker.mock.calls[0]?.[1] expect(JSON.parse(options.body)).toEqual( expect.objectContaining({ organizationId: 'org-1', @@ -817,7 +819,7 @@ describe('requestChatTitle billing protocol', () => { billingAttribution: { ...BILLING_ATTRIBUTION, workspaceId: null }, }) ).resolves.toBeNull() - expect(fetchGo).not.toHaveBeenCalled() + expect(fetchWorker).not.toHaveBeenCalled() }) it('fails before hosted title egress without a billing workspace', async () => { @@ -828,6 +830,6 @@ describe('requestChatTitle billing protocol', () => { userId: 'user-1', }) ).resolves.toBeNull() - expect(fetchGo).not.toHaveBeenCalled() + expect(fetchWorker).not.toHaveBeenCalled() }) }) diff --git a/apps/sim/lib/mothership/request/lifecycle/start.ts b/apps/sim/lib/mothership/request/lifecycle/start.ts index 4af8f6fdaf3..23e86ebbbc3 100644 --- a/apps/sim/lib/mothership/request/lifecycle/start.ts +++ b/apps/sim/lib/mothership/request/lifecycle/start.ts @@ -23,17 +23,13 @@ import { MothershipStreamV1SessionKind, } from '@/lib/mothership/generated/mothership-stream-v1' import { TitleRequest } from '@/lib/mothership/generated/protocol' -import { - RequestTraceV1Outcome, - RequestTraceV1SpanStatus, -} from '@/lib/mothership/generated/request-trace-v1' import { CopilotRequestCancelReason, type CopilotRequestCancelReasonValue, CopilotTransport, -} from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceEvent } from '@/lib/mothership/generated/trace-events-v1' +} from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceEvent } from '@/lib/mothership/observability/trace-events' import { resolveEnterpriseByokKey } from '@/lib/mothership/request/enterprise-byok' import { mothershipRequestHeaders } from '@/lib/mothership/request/headers' import { finalizeStream } from '@/lib/mothership/request/lifecycle/finalize' @@ -42,7 +38,7 @@ import { type CopilotLifecycleOptions, runCopilotLifecycle, } from '@/lib/mothership/request/lifecycle/run' -import { type CopilotLifecycleOutcome, startCopilotOtelRoot } from '@/lib/mothership/request/otel' +import { startCopilotOtelRoot } from '@/lib/mothership/request/otel' import { cleanupAbortMarker, clearFilePreviewSessions, @@ -66,7 +62,7 @@ import { import { StreamReplayBudgetExhaustedError } from '@/lib/mothership/request/session/replay-budget' import { SSE_RESPONSE_HEADERS } from '@/lib/mothership/request/session/sse' import { type StreamTurnFailure, turnFailure } from '@/lib/mothership/request/session/turn-failure' -import { TraceCollector } from '@/lib/mothership/request/trace' +import { RequestTraceOutcome } from '@/lib/mothership/request/trace' import type { OrchestratorResult } from '@/lib/mothership/request/types' import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url' @@ -126,7 +122,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS otelRoot ?? startCopilotOtelRoot({ requestId, - route: orchestrateOptions.goRoute, + route: orchestrateOptions.workerRoute, chatId, workflowId: orchestrateOptions.workflowId, executionId, @@ -228,8 +224,6 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS return cancelReason } - const collector = new TraceCollector() - /** * The failure that ends this turn. Once the controller is aborted its reason is * authoritative, so a failure that follows a Stop stays a Stop. @@ -289,7 +283,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS }, publisher, runId, - RequestTraceV1Outcome.error, + RequestTraceOutcome.error, requestId ) try { @@ -314,7 +308,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS // Next handler → ReadableStream.start boundary. await otelContextApi.with(activeOtelRoot.context, async () => { const otelContext = activeOtelRoot.context - let rootOutcome: CopilotLifecycleOutcome = RequestTraceV1Outcome.error + let rootOutcome: RequestTraceOutcome = RequestTraceOutcome.error let rootError: unknown // `cancelReason` must be declared OUTSIDE the outer `try` so // it remains in scope for the outer `finally` that calls @@ -326,12 +320,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS // orphan under a phantom parent. (Regression landed 2026-04-21.) let cancelReason: CopilotRequestCancelReasonValue | undefined try { - const requestSpan = collector.startSpan('Sim Agent Request', 'request', { - streamId, - chatId, - runId, - }) - let outcome: CopilotLifecycleOutcome = RequestTraceV1Outcome.error + let outcome: RequestTraceOutcome = RequestTraceOutcome.error let lifecycleResult: | { usage?: { prompt: number; completion: number } @@ -373,7 +362,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS requestContext: savedRequestContext, })) if (run.status === 'cancelled') { - outcome = RequestTraceV1Outcome.cancelled + outcome = RequestTraceOutcome.cancelled abortController.abort(AbortReason.UserStop) cancelReason = recordCancelled() await endTurn( @@ -432,7 +421,6 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS !publisher.clientDisconnected || (await hasStreamViewer(streamId)), executionId, runId, - trace: collector, simRequestId: requestId, otelContext, abortSignal: abortController.signal, @@ -483,13 +471,13 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS const failure = failureOf() // A completed result wins a late Stop; passive disconnection never cancels. outcome = result.success - ? RequestTraceV1Outcome.success + ? RequestTraceOutcome.success : failure - ? RequestTraceV1Outcome.error + ? RequestTraceOutcome.error : result.cancelled || abortController.signal.aborted - ? RequestTraceV1Outcome.cancelled - : RequestTraceV1Outcome.error - if (outcome === RequestTraceV1Outcome.cancelled) { + ? RequestTraceOutcome.cancelled + : RequestTraceOutcome.error + if (outcome === RequestTraceOutcome.cancelled) { cancelReason = recordCancelled() } await assertControllerOwnership() @@ -509,13 +497,13 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS await assertControllerOwnership() const failure = failureOf(error) if (failure) { - outcome = RequestTraceV1Outcome.error + outcome = RequestTraceOutcome.error await finalizeAfterTurnFailure(failure) return } const wasCancelled = abortController.signal.aborted - outcome = wasCancelled ? RequestTraceV1Outcome.cancelled : RequestTraceV1Outcome.error - if (outcome === RequestTraceV1Outcome.cancelled) { + outcome = wasCancelled ? RequestTraceOutcome.cancelled : RequestTraceOutcome.error + if (outcome === RequestTraceOutcome.cancelled) { cancelReason = recordCancelled(getErrorMessage(error)) } if (publisher.clientDisconnected) { @@ -527,7 +515,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS // Demote to warn when the throw came from a user-initiated // cancel — it isn't an "unexpected" failure then, and the // error-level log pollutes alerting on normal Stop presses. - const logFn = outcome === RequestTraceV1Outcome.cancelled ? logger.warn : logger.error + const logFn = outcome === RequestTraceOutcome.cancelled ? logger.warn : logger.error logFn.call(logger, `Orchestration ended with ${outcome}:`, error, { requestId }) const syntheticResult = { @@ -539,15 +527,6 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS } await endTurn(syntheticResult, publisher, runId, outcome, requestId) } finally { - collector.endSpan( - requestSpan, - outcome === RequestTraceV1Outcome.success - ? RequestTraceV1SpanStatus.ok - : outcome === RequestTraceV1Outcome.cancelled - ? RequestTraceV1SpanStatus.cancelled - : RequestTraceV1SpanStatus.error - ) - clearInterval(abortPoller) try { await publisher.close() @@ -590,7 +569,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS } } } catch (error) { - rootOutcome = RequestTraceV1Outcome.error + rootOutcome = RequestTraceOutcome.error rootError = error throw error } finally { @@ -791,12 +770,12 @@ export async function requestChatTitle(params: { Object.assign(headers, billingRequest.headers) } - const { fetchGo } = await import('@/lib/mothership/request/go/fetch') + const { fetchWorker } = await import('@/lib/mothership/request/worker/fetch') const mothershipBaseURL = await getMothershipBaseURL({ userId }) // Title reads the user's message content, so an enterprise chat pins its key here too. const byokApiKey = await resolveEnterpriseByokKey(workspaceId) const context = await buildChatTitleContext(params) - const response = await fetchGo(`${mothershipBaseURL}/api/generate-chat-title`, { + const response = await fetchWorker(`${mothershipBaseURL}/api/generate-chat-title`, { method: 'POST', signal, headers, @@ -812,7 +791,7 @@ export async function requestChatTitle(params: { }) ), otelContext, - spanName: 'sim → go /api/generate-chat-title', + spanName: 'sim → worker /api/generate-chat-title', operation: 'generate_chat_title', }) diff --git a/apps/sim/lib/mothership/request/lifecycle/stream-retry.test.ts b/apps/sim/lib/mothership/request/lifecycle/stream-retry.test.ts index 6874b5ab796..b05e9b4583a 100644 --- a/apps/sim/lib/mothership/request/lifecycle/stream-retry.test.ts +++ b/apps/sim/lib/mothership/request/lifecycle/stream-retry.test.ts @@ -1,12 +1,12 @@ import { afterEach, describe, expect, it, vi } from 'vitest' +import { StreamRetryWindow } from '@/lib/mothership/request/lifecycle/stream-retry' import { BillingLimitError, CopilotBackendError, StreamEndedWithoutTerminalError, WorkerStreamInterruptedError, WorkerUnreachableError, -} from '@/lib/mothership/request/go/stream' -import { StreamRetryWindow } from '@/lib/mothership/request/lifecycle/stream-retry' +} from '@/lib/mothership/request/worker/stream' afterEach(() => vi.useRealTimers()) diff --git a/apps/sim/lib/mothership/request/lifecycle/stream-retry.ts b/apps/sim/lib/mothership/request/lifecycle/stream-retry.ts index 0bc4cfa1248..fe13ed50fa9 100644 --- a/apps/sim/lib/mothership/request/lifecycle/stream-retry.ts +++ b/apps/sim/lib/mothership/request/lifecycle/stream-retry.ts @@ -1,11 +1,11 @@ import { backoffWithJitter } from '@sim/utils/retry' -import { StreamContinuityError } from '@/lib/mothership/request/go/parser' +import { StreamContinuityError } from '@/lib/mothership/request/worker/parser' import { CopilotBackendError, StreamEndedWithoutTerminalError, WorkerStreamInterruptedError, WorkerUnreachableError, -} from '@/lib/mothership/request/go/stream' +} from '@/lib/mothership/request/worker/stream' const MAX_STREAM_RETRIES = 3 const STREAM_RECOVERY_WINDOW_MS = 30_000 diff --git a/apps/sim/lib/mothership/request/lifecycle/unattended.ts b/apps/sim/lib/mothership/request/lifecycle/unattended.ts index 587665a0eda..1d0d4582805 100644 --- a/apps/sim/lib/mothership/request/lifecycle/unattended.ts +++ b/apps/sim/lib/mothership/request/lifecycle/unattended.ts @@ -22,7 +22,7 @@ import { buildOnComplete, buildOnError } from '@/lib/mothership/chat/completion' import { PROTOCOL_VERSION } from '@/lib/mothership/generated/protocol' import { durableResourceAuthoringVersion, - StreamRecoveryConfigSchema, + type StreamRecoveryConfigInput, } from '@/lib/mothership/request/lifecycle/recovery-config' import { createSSEStream } from '@/lib/mothership/request/lifecycle/start' import { @@ -96,14 +96,14 @@ export async function startUnattendedTurn( userId, invocation: { kind: 'chat', chatId }, }) - const recovery = StreamRecoveryConfigSchema.parse({ + const recovery: StreamRecoveryConfigInput = { kind: 'unattended_stream', request, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', clientToolPickupExpected: false, requestMode: 'agent', resourceAuthoringVersion: durableResourceAuthoringVersion(true), - }) + } const executionId = messageId const requestId = messageId const admission = await admitUnattendedTurn.execute({ @@ -143,7 +143,7 @@ export async function startUnattendedTurn( chatId, executionId, runId: admission.run.id, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', copilotResourceAdmission, billingAttribution: input.billingAttribution, ...(userPermission ? { userPermission } : {}), diff --git a/apps/sim/lib/mothership/request/metrics.ts b/apps/sim/lib/mothership/request/metrics.ts index e7d7547fe51..e66ddeb42ba 100644 --- a/apps/sim/lib/mothership/request/metrics.ts +++ b/apps/sim/lib/mothership/request/metrics.ts @@ -1,36 +1,30 @@ -// Sim server-side copilot metrics (U17). Sim's MeterProvider is wired in -// instrumentation-node.ts (OTLP → Mimir, 60s) but had no copilot instruments; -// this module is its first consumer. We emit the SAME metric names + label keys -// + histogram bucket boundaries as the Go side (copilot internal/telemetry + -// contracts/metrics_v1.go) so the Go∪Sim union is queryable as one series set -// — e.g. `copilot.tool.duration` split by `tool.executor` (go|client|sim). +// Sim server-side copilot metrics. Sim's MeterProvider is wired in +// instrumentation-node.ts (OTLP → Mimir, 60s). Metric names come from +// observability/metric-names.ts; label keys and histogram buckets are Sim's, +// e.g. `copilot.tool.duration` split by `tool.executor`. // // Bounded cardinality only: tool.name and gen_ai.agent.name are capped to the -// shared catalogs (else "other"); vfs phase / file-read outcome are bounded -// sets. NEVER a user/chat/request id (those explode Prometheus series). +// shared catalogs (else "other"). NEVER a user/chat/request id (those explode +// Prometheus series). import { type Counter, type Histogram, metrics } from '@opentelemetry/api' -import { Metric } from '@/lib/mothership/generated/metrics-v1' import { TOOL_CATALOG } from '@/lib/mothership/generated/tool-catalog-v1' -import type { CopilotDegradedReasonValue } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' +import { Metric } from '@/lib/mothership/observability/metric-names' +import { + type CopilotDegradedReasonValue, + ToolExecutor, +} from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' -// MUST match Go's copilot/internal/telemetry/metrics.go LatencyBucketsMs -// exactly — a histogram_quantile(sum by (le) …) over the Go∪Sim union is only -// valid with identical boundaries. If you change one side, change the other. +// Changing these boundaries splits `histogram_quantile(sum by (le) …)` queries +// across the old and new series until the old ones age out. const LATENCY_BUCKETS_MS = [ 50, 100, 250, 500, 1000, 2000, 5000, 10000, 20000, 30000, 60000, 120000, 300000, ] -// File sizes span KB→tens of MB; a bytes-appropriate bucket set (not latency). -const BYTE_BUCKETS = [1024, 8192, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456] - interface CopilotMeterInstruments { toolDuration: Histogram toolCalls: Counter degradedCount: Counter - vfsMaterializeDuration: Histogram - fileReadDuration: Histogram - fileReadBytes: Histogram } let cached: CopilotMeterInstruments | undefined @@ -48,25 +42,13 @@ function instruments(): CopilotMeterInstruments { }), toolCalls: meter.createCounter(Metric.CopilotToolCalls), degradedCount: meter.createCounter(Metric.CopilotDegradedCount), - vfsMaterializeDuration: meter.createHistogram(Metric.CopilotVfsMaterializeDuration, { - unit: 'ms', - advice: { explicitBucketBoundaries: LATENCY_BUCKETS_MS }, - }), - fileReadDuration: meter.createHistogram(Metric.CopilotFileReadDuration, { - unit: 'ms', - advice: { explicitBucketBoundaries: LATENCY_BUCKETS_MS }, - }), - fileReadBytes: meter.createHistogram(Metric.CopilotFileReadSize, { - unit: 'By', - advice: { explicitBucketBoundaries: BYTE_BUCKETS }, - }), } return cached } -// Caps tool.name to the shared catalog (matches Go's cappedToolName): a -// catalog tool keeps its name, everything else (user MCP/custom/unknown) -// collapses to "other" so series count stays finite. +// Caps tool.name to the shared catalog: a catalog tool keeps its name, +// everything else (user MCP/custom/unknown) collapses to "other" so series +// count stays finite. function cappedToolName(name: string): string { return TOOL_CATALOG[name] ? name : 'other' } @@ -92,7 +74,7 @@ export function recordSimToolMetric( const { toolDuration, toolCalls } = instruments() const attrs = { [TraceAttr.ToolName]: cappedToolName(name), - [TraceAttr.ToolExecutor]: 'sim', + [TraceAttr.ToolExecutor]: ToolExecutor.Sim, [TraceAttr.ToolOutcome]: outcome, [TraceAttr.GenAiAgentName]: normalizeToolAgentId(agentId), } @@ -103,29 +85,10 @@ export function recordSimToolMetric( // recordDegraded counts one non-fatal fallback, labelled by the bounded reason // it took. Every degradation path reports here so "are we degrading, and why" is // one query instead of a per-incident investigation — and so a path that should -// be impossible can be alerted on at > 0. Sim's own logs do not reach Loki and -// the in-process TraceCollector is not exported, so without this a fallback is -// invisible. +// be impossible can be alerted on at > 0. Sim's own logs do not reach Loki, so +// without this a fallback is invisible. export function recordDegraded(reason: CopilotDegradedReasonValue): void { instruments().degradedCount.add(1, { [TraceAttr.CopilotDegradedReason]: reason, }) } - -// recordVfsMaterialize records VFS materialization time. Call once per phase -// with that phase's duration and once with phase="total" for the whole op, so -// the dashboard can show total + per-phase. phase must be a bounded value. -export function recordVfsMaterialize(phase: string, durationMs: number): void { - if (durationMs < 0) return - instruments().vfsMaterializeDuration.record(durationMs, { - [TraceAttr.CopilotVfsPhase]: phase, - }) -} - -// recordFileRead records server-side file-read duration + size by outcome. -export function recordFileRead(outcome: string, durationMs: number, bytes: number): void { - const { fileReadDuration, fileReadBytes } = instruments() - const attrs = { [TraceAttr.CopilotVfsReadOutcome]: outcome } - if (durationMs >= 0) fileReadDuration.record(durationMs, attrs) - if (bytes >= 0) fileReadBytes.record(bytes, attrs) -} diff --git a/apps/sim/lib/mothership/request/otel.ts b/apps/sim/lib/mothership/request/otel.ts index f2a94f03fb2..711de10f36d 100644 --- a/apps/sim/lib/mothership/request/otel.ts +++ b/apps/sim/lib/mothership/request/otel.ts @@ -12,23 +12,23 @@ import { } from '@opentelemetry/api' import { setRequestTraceId } from '@sim/logger' import { describeError, toError } from '@sim/utils/errors' -import { RequestTraceV1Outcome } from '@/lib/mothership/generated/request-trace-v1' import { CopilotBranchKind, CopilotRequestCancelReason, type CopilotRequestCancelReasonValue, CopilotSurface, CopilotTransport, -} from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' -import { contextFromRequestHeaders } from '@/lib/mothership/request/go/propagation' + ToolExecutor, +} from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { normalizeToolAgentId } from '@/lib/mothership/request/metrics' import { isExplicitStopReason } from '@/lib/mothership/request/session/abort-reason' +import { RequestTraceOutcome } from '@/lib/mothership/request/trace' +import { contextFromRequestHeaders } from '@/lib/mothership/request/worker/propagation' // OTel GenAI content-capture env var (spec: -// https://opentelemetry.io/docs/specs/semconv/gen-ai/). Mirrored on -// the Go side so a single var controls both halves. +// https://opentelemetry.io/docs/specs/semconv/gen-ai/). const GENAI_CAPTURE_ENV = 'OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT' // OTLP backends commonly reject attrs over 64 KiB; cap proactively. @@ -45,9 +45,7 @@ function isGenAIMessageCaptureEnabled(): boolean { // Policy across the codebase: an explicit user stop leaves span // status UNSET; every other cancellation (client tab close, // network drop, internal timeout, uncategorized abort) escalates -// to `status=error` so it shows up on error dashboards. This is -// the Sim mirror of `requestctx.IsExplicitUserStop` on the Go -// side; keep the two semantically aligned. +// to `status=error` so it shows up on error dashboards. // // Detection modes: // @@ -80,10 +78,6 @@ export function isExplicitUserStopError(err: unknown): boolean { * Everything else — in particular the 4xx flood from bot probes and * expected auth/validation rejections — stays UNSET on the span so * dashboards don't treat normal rejections as errors. - * - * Mirrored on the Go side in - * `copilot/internal/http/middleware/telemetry.go`. Keep the two in - * sync if you change the actionable set. */ export function isActionableErrorStatus(code: number): boolean { if (code >= 500) return true @@ -109,7 +103,7 @@ export function markSpanForError(span: Span, error: unknown): void { } } -// OTel GenAI message shape (kept minimal). Mirror changes on the Go side. +// OTel GenAI message shape (kept minimal). interface GenAIAgentPart { type: 'text' | 'tool_call' | 'tool_call_response' content?: string @@ -128,9 +122,8 @@ function marshalAgentMessages(messages: GenAIAgentMessage[]): string | undefined if (messages.length === 0) return undefined const json = JSON.stringify(messages) if (json.length <= GENAI_MESSAGE_ATTR_MAX_BYTES) return json - // Simple tail-preserving truncation: drop from the front until we - // fit. Matches the Go side's behavior. The last message is - // usually the most diagnostic for span-level outcome. + // Simple tail-preserving truncation: drop from the front until we fit. + // The last message is usually the most diagnostic for span-level outcome. let remaining = messages.slice() while (remaining.length > 1) { remaining = remaining.slice(1) @@ -208,9 +201,6 @@ function setAgentOutputMessages(span: Span, output: CopilotAgentOutputMessages): } } -export type CopilotLifecycleOutcome = - (typeof RequestTraceV1Outcome)[keyof typeof RequestTraceV1Outcome] - // Lazy tracer — Next 16/Turbopack can evaluate modules before NodeSDK // installs the real TracerProvider; resolving per call avoids a // cached NoOpTracer silently disabling OTel. @@ -222,9 +212,9 @@ function getTracer() { return getCopilotTracer() } -// Wrap an inbound handler that Go called into so its span parents -// under the Go-side trace (via `traceparent`). -export async function withIncomingGoSpan( +// Wrap an inbound handler the worker called so its span parents +// under the worker's trace (via `traceparent`). +export async function withIncomingWorkerSpan( headers: Headers, spanName: string, attributes: Record | undefined, @@ -280,8 +270,7 @@ export async function withCopilotSpan( return tracer.startActiveSpan(spanName, { attributes }, runBody) } -// External OTel `tool.execute` span for Sim-side tool work (the Go -// side's `tool.execute` is just the enqueue, stays ~0ms). +// External OTel `tool.execute` span for Sim-side tool work. export async function withCopilotToolSpan( input: { toolName: string @@ -296,12 +285,12 @@ export async function withCopilotToolSpan( ): Promise { const tracer = getTracer() return tracer.startActiveSpan( - `tool.execute ${input.toolName}`, + `${TraceSpan.ToolExecute} ${input.toolName}`, { attributes: { [TraceAttr.ToolName]: input.toolName, [TraceAttr.ToolCallId]: input.toolCallId, - [TraceAttr.ToolExecutor]: 'sim', + [TraceAttr.ToolExecutor]: ToolExecutor.Sim, [TraceAttr.GenAiAgentName]: normalizeToolAgentId(input.agentName), ...(input.runId ? { [TraceAttr.RunId]: input.runId } : {}), ...(input.chatId ? { [TraceAttr.ChatId]: input.chatId } : {}), @@ -421,7 +410,7 @@ interface CopilotOtelRoot { * for non-cancellation outcomes. */ finish: ( - outcome?: CopilotLifecycleOutcome, + outcome?: RequestTraceOutcome, error?: unknown, cancelReason?: CopilotRequestCancelReasonValue ) => void @@ -463,7 +452,7 @@ export function startCopilotOtelRoot( span.setAttribute(TraceAttr.RequestId, requestId) span.setAttribute(TraceAttr.SimRequestId, requestId) // Stamp the trace id onto the route's log context so every sim log line in - // this request greps by the same id the Go copilot logs as trace_id. + // this request greps by the trace id its spans share. if (spanContext.traceId && spanContext.traceId.length === 32) { setRequestTraceId(spanContext.traceId) } @@ -473,7 +462,7 @@ export function startCopilotOtelRoot( const finish: CopilotOtelRoot['finish'] = (outcome, error, cancelReason) => { if (finished) return finished = true - const resolvedOutcome = outcome ?? RequestTraceV1Outcome.success + const resolvedOutcome = outcome ?? RequestTraceOutcome.success span.setAttribute(TraceAttr.CopilotRequestOutcome, resolvedOutcome) // Policy: `explicit_stop` is the ONLY cancellation we treat as // expected (status unset → dashboards see it as OK). Everything @@ -485,9 +474,9 @@ export function startCopilotOtelRoot( if (isExplicitStop || isExplicitUserStopError(error)) { span.setStatus({ code: SpanStatusCode.OK }) } - } else if (resolvedOutcome === RequestTraceV1Outcome.success) { + } else if (resolvedOutcome === RequestTraceOutcome.success) { span.setStatus({ code: SpanStatusCode.OK }) - } else if (resolvedOutcome === RequestTraceV1Outcome.cancelled) { + } else if (resolvedOutcome === RequestTraceOutcome.cancelled) { if (isExplicitStop) { span.setStatus({ code: SpanStatusCode.OK }) } else { diff --git a/apps/sim/lib/mothership/request/session/abort-reason.ts b/apps/sim/lib/mothership/request/session/abort-reason.ts index 8a6b281e2c0..25e6a952af6 100644 --- a/apps/sim/lib/mothership/request/session/abort-reason.ts +++ b/apps/sim/lib/mothership/request/session/abort-reason.ts @@ -24,12 +24,12 @@ export const AbortReason = { RedisPoller: 'redis_abort_marker:poller', /** * Cross-process stop: same root cause as `RedisPoller`, but observed - * by `runStreamLoop` at body close (the Go body ended before the + * by `runStreamLoop` at body close (the worker's body ended before the * 250ms poller's next tick) rather than by the polling timer. */ MarkerObservedAtBodyClose: 'redis_abort_marker:body_close', - /** Internal timeout on the outbound explicit-abort fetch to Go. */ - ExplicitAbortFetchTimeout: 'timeout:go_explicit_abort_fetch', + /** Internal timeout on the outbound explicit-abort fetch to the worker. */ + ExplicitAbortFetchTimeout: 'timeout:explicit_abort_fetch', } as const export type AbortReasonValue = (typeof AbortReason)[keyof typeof AbortReason] @@ -41,7 +41,7 @@ export type AbortReasonValue = (typeof AbortReason)[keyof typeof AbortReason] * `AbortReason` is presumed non-explicit. This is the canonical * "should I treat this cancellation as expected?" predicate: span * status-setters consult it to suppress ERROR only for user-initiated - * stops, mirroring `requestctx.IsExplicitUserStop` on the Go side. + * stops. */ export function isExplicitStopReason(reason: unknown): boolean { return ( diff --git a/apps/sim/lib/mothership/request/session/abort.ts b/apps/sim/lib/mothership/request/session/abort.ts index 840b41198d8..e25fdf0d37b 100644 --- a/apps/sim/lib/mothership/request/session/abort.ts +++ b/apps/sim/lib/mothership/request/session/abort.ts @@ -3,9 +3,9 @@ import { toError } from '@sim/utils/errors' import { sleep } from '@sim/utils/helpers' import { generateId } from '@sim/utils/id' import { acquireLock, extendLock, getRedisClient, releaseLock } from '@/lib/core/config/redis' -import { AbortBackend } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { AbortBackend } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { withCopilotSpan } from '@/lib/mothership/request/otel' import { AbortReason } from './abort-reason' import { clearAbortMarker, hasAbortMarker, refreshBufferTtl, writeAbortMarker } from './buffer' diff --git a/apps/sim/lib/mothership/request/session/contract.test.ts b/apps/sim/lib/mothership/request/session/contract.test.ts index aebf82fdb21..74701e1f595 100644 --- a/apps/sim/lib/mothership/request/session/contract.test.ts +++ b/apps/sim/lib/mothership/request/session/contract.test.ts @@ -24,10 +24,6 @@ describe('stream session contract parser', () => { it('accepts contract text events', () => { const event = { ...BASE_ENVELOPE, - trace: { - ...BASE_ENVELOPE.trace, - goTraceId: 'go-trace-1', - }, type: 'text' as const, payload: { channel: 'assistant' as const, diff --git a/apps/sim/lib/mothership/request/session/contract.ts b/apps/sim/lib/mothership/request/session/contract.ts index e73a63a1c97..17fc7e16ad0 100644 --- a/apps/sim/lib/mothership/request/session/contract.ts +++ b/apps/sim/lib/mothership/request/session/contract.ts @@ -1,9 +1,11 @@ import { getErrorMessage } from '@sim/utils/errors' import { isRecordLike } from '@sim/utils/object' import type { + MothershipStreamV1Envelope, MothershipStreamV1EventEnvelope, MothershipStreamV1StreamRef, MothershipStreamV1StreamScope, + MothershipStreamV1SubagentSpanData, MothershipStreamV1Trace, } from '@/lib/mothership/generated/mothership-stream-v1' import { @@ -15,13 +17,6 @@ import { MothershipStreamV1TextChannel, MothershipStreamV1ToolPhase, } from '@/lib/mothership/generated/mothership-stream-v1' -import type { - StreamActivityCheckpoint, - StreamTextCompletion, - StreamTextPosition, - StreamToolDeadline, - StreamToolReplay, -} from '@/lib/mothership/generated/protocol' import { ResourcePayload } from '@/lib/mothership/generated/resources' import type { FilePreviewTargetKind } from './file-preview-session-contract' @@ -35,25 +30,11 @@ const FILE_PREVIEW_PHASE = { complete: 'file_preview_complete', } as const -type EnvelopeToStreamEvent = T extends { - type: infer TType - payload: infer TPayload - scope?: infer TScope -} +type EnvelopeToStreamEvent = T extends MothershipStreamV1Envelope ? { type: TType - payload: TType extends 'text' - ? TPayload & StreamTextPosition - : TType extends 'complete' - ? TPayload & StreamTextCompletion & StreamActivityCheckpoint - : TType extends 'tool' - ? TPayload & StreamToolReplay & StreamToolDeadline - : TType extends 'resource' - ? ResourcePayload - : TType extends 'run' - ? TPayload & StreamActivityCheckpoint - : TPayload - scope?: Exclude + payload: TPayload + scope?: MothershipStreamV1StreamScope /** Wire ordering key, carried off the envelope; absent on synthetic events. */ seq?: number } @@ -119,33 +100,16 @@ export type SyntheticFilePreviewPayload = | SyntheticFilePreviewContentPayload | SyntheticFilePreviewCompletePayload -export interface SyntheticFilePreviewEventEnvelope { - payload: SyntheticFilePreviewPayload - scope?: MothershipStreamV1StreamScope - seq: number - stream: MothershipStreamV1StreamRef - trace?: MothershipStreamV1Trace - ts: string - type: 'tool' - v: 1 -} - -type ResourceEnvelope = Extract -type ToolEnvelope = Extract -type ToolReplayEnvelope = T extends ToolEnvelope - ? Omit & { payload: T['payload'] & StreamToolReplay & StreamToolDeadline } - : never - -type ContractEventEnvelope = - | Exclude - | ToolReplayEnvelope - | (Omit & { - payload: ResourcePayload - }) +export type SyntheticFilePreviewEventEnvelope = MothershipStreamV1Envelope< + 'tool', + SyntheticFilePreviewPayload +> -export type PersistedStreamEventEnvelope = ContractEventEnvelope | SyntheticFilePreviewEventEnvelope +export type PersistedStreamEventEnvelope = + | MothershipStreamV1EventEnvelope + | SyntheticFilePreviewEventEnvelope -export type ContractStreamEvent = EnvelopeToStreamEvent +export type ContractStreamEvent = EnvelopeToStreamEvent export type SyntheticStreamEvent = EnvelopeToStreamEvent export type SessionStreamEvent = ContractStreamEvent | SyntheticStreamEvent export type StreamEvent = SessionStreamEvent @@ -204,10 +168,7 @@ function isStreamRef(value: unknown): value is MothershipStreamV1StreamRef { function isTrace(value: unknown): value is MothershipStreamV1Trace { return ( - isRecordLike(value) && - typeof value.requestId === 'string' && - isOptionalString(value.goTraceId) && - isOptionalString(value.spanId) + isRecordLike(value) && typeof value.requestId === 'string' && isOptionalString(value.spanId) ) } @@ -260,8 +221,6 @@ function isValidSessionPayload(payload: JsonRecord): boolean { return typeof payload.chatId === 'string' case MothershipStreamV1SessionKind.title: return typeof payload.title === 'string' - case MothershipStreamV1SessionKind.trace: - return typeof payload.requestId === 'string' default: return false } @@ -292,12 +251,7 @@ function isValidToolPayload(payload: JsonRecord): boolean { } function isValidSpanPayload(payload: JsonRecord): boolean { - const kind = payload.kind - return ( - kind === MothershipStreamV1SpanPayloadKind.subagent || - kind === MothershipStreamV1SpanPayloadKind.structured_result || - kind === MothershipStreamV1SpanPayloadKind.subagent_result - ) + return payload.kind === MothershipStreamV1SpanPayloadKind.subagent } function isValidResourcePayload(payload: JsonRecord): boolean { @@ -340,7 +294,7 @@ function isValidCompletePayload(payload: JsonRecord): boolean { return typeof payload.status === 'string' && isValidActivityCheckpoint(payload) } -function isContractEnvelope(value: unknown): value is ContractEventEnvelope { +function isContractEnvelope(value: unknown): value is MothershipStreamV1EventEnvelope { if (!isValidEnvelopeShell(value)) return false const payload = value.payload as JsonRecord switch (value.type) { @@ -475,9 +429,20 @@ export function isSubagentSpanStreamEvent( return event.type === 'span' && isRecordLike(event.payload) && event.payload.kind === 'subagent' } +/** Reads a subagent span's untrusted `data`, keeping only non-empty string fields. */ +export function subagentSpanData(data: unknown): MothershipStreamV1SubagentSpanData { + if (!isRecordLike(data)) return {} + return { + ...(typeof data.name === 'string' && data.name ? { name: data.name } : {}), + ...(typeof data.error === 'string' && data.error ? { error: data.error } : {}), + } +} + // Public contract validators & parsers -export function isContractStreamEventEnvelope(value: unknown): value is ContractEventEnvelope { +export function isContractStreamEventEnvelope( + value: unknown +): value is MothershipStreamV1EventEnvelope { return isContractEnvelope(value) } diff --git a/apps/sim/lib/mothership/request/session/explicit-abort.test.ts b/apps/sim/lib/mothership/request/session/explicit-abort.test.ts index a9038d3a2b8..3c857ac2237 100644 --- a/apps/sim/lib/mothership/request/session/explicit-abort.test.ts +++ b/apps/sim/lib/mothership/request/session/explicit-abort.test.ts @@ -4,9 +4,9 @@ import { mothershipAgentUrlMockFns, } from '@sim/testing/mocks/mothership-agent-url.mock' import { - mothershipGoFetchMock, - mothershipGoFetchMockFns, -} from '@sim/testing/mocks/mothership-go-fetch.mock' + mothershipWorkerFetchMock, + mothershipWorkerFetchMockFns, +} from '@sim/testing/mocks/mothership-worker-fetch.mock' import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' beforeAll(() => { @@ -15,7 +15,7 @@ beforeAll(() => { afterAll(resetEnvMock) -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) @@ -23,7 +23,7 @@ import { BillingCallbackHeaders } from '@/lib/mothership/generated/billing' import { AbortRequest } from '@/lib/mothership/generated/protocol' import { requestExplicitStreamAbort } from '@/lib/mothership/request/session/explicit-abort' -const { mockFetchGo } = mothershipGoFetchMockFns +const { mockFetchWorker } = mothershipWorkerFetchMockFns mothershipAgentUrlMockFns.mockGetMothershipBaseURL.mockResolvedValue('https://copilot.test') mothershipAgentUrlMockFns.mockGetMothershipSourceEnvHeaders.mockReturnValue({ 'X-Sim-Source-Env': 'test', @@ -32,7 +32,7 @@ mothershipAgentUrlMockFns.mockGetMothershipSourceEnvHeaders.mockReturnValue({ describe('requestExplicitStreamAbort', () => { afterEach(() => vi.useRealTimers()) beforeEach(() => { - mockFetchGo.mockImplementation(async (_url, request) => { + mockFetchWorker.mockImplementation(async (_url, request) => { AbortRequest.parse(JSON.parse(request.body)) return new Response(null, { status: 200 }) }) @@ -45,11 +45,11 @@ describe('requestExplicitStreamAbort', () => { chatId: 'chat-1', }) expect(result).toEqual({ settled: false }) - const headers = mockFetchGo.mock.calls[0][1].headers + const headers = mockFetchWorker.mock.calls[0][1].headers expect(BillingCallbackHeaders.safeParse(headers).success).toBe(true) expect(headers).not.toHaveProperty('x-sim-billing-protocol') - expect(mockFetchGo).toHaveBeenCalledWith( + expect(mockFetchWorker).toHaveBeenCalledWith( 'https://copilot.test/api/streams/explicit-abort', expect.objectContaining({ headers: expect.objectContaining({ @@ -65,7 +65,7 @@ describe('requestExplicitStreamAbort', () => { userId: 'actor', chatId: 'chat', }) - const body = JSON.parse(mockFetchGo.mock.calls[0][1].body) + const body = JSON.parse(mockFetchWorker.mock.calls[0][1].body) expect(AbortRequest.parse(body)).toEqual({ messageId: '11111111-1111-4111-8111-111111111111' }) expect(Object.keys(body)).toEqual(['messageId']) }) @@ -73,7 +73,7 @@ describe('requestExplicitStreamAbort', () => { it('waits for a worker that acknowledges Stop before it has finished settling', async () => { vi.useFakeTimers() let attempts = 0 - mockFetchGo.mockImplementation(async () => + mockFetchWorker.mockImplementation(async () => Response.json({ stopped: true, settled: ++attempts >= 3 }) ) const stopping = requestExplicitStreamAbort({ @@ -83,13 +83,13 @@ describe('requestExplicitStreamAbort', () => { }) await vi.runAllTimersAsync() await expect(stopping).resolves.toEqual({ settled: true }) - expect(mockFetchGo).toHaveBeenCalledTimes(3) - expect(new Set(mockFetchGo.mock.calls.map(([, request]) => request.body)).size).toBe(1) + expect(mockFetchWorker).toHaveBeenCalledTimes(3) + expect(new Set(mockFetchWorker.mock.calls.map(([, request]) => request.body)).size).toBe(1) }) it('returns unsettled at its deadline instead of allowing the next turn to overlap', async () => { vi.useFakeTimers() - mockFetchGo.mockImplementation(async () => Response.json({ stopped: true, settled: false })) + mockFetchWorker.mockImplementation(async () => Response.json({ stopped: true, settled: false })) const stopping = requestExplicitStreamAbort({ streamId: '11111111-1111-4111-8111-111111111111', userId: 'user-1', @@ -97,35 +97,35 @@ describe('requestExplicitStreamAbort', () => { }) await vi.runAllTimersAsync() await expect(stopping).resolves.toEqual({ settled: false }) - expect(mockFetchGo.mock.calls.length).toBeGreaterThan(1) - expect(mockFetchGo.mock.lastCall?.[1].signal.aborted).toBe(true) + expect(mockFetchWorker.mock.calls.length).toBeGreaterThan(1) + expect(mockFetchWorker.mock.lastCall?.[1].signal.aborted).toBe(true) }) it('returns immediately once the worker reports settlement', async () => { - mockFetchGo.mockResolvedValue(Response.json({ stopped: true, settled: true })) + mockFetchWorker.mockResolvedValue(Response.json({ stopped: true, settled: true })) await expect( requestExplicitStreamAbort({ streamId: '11111111-1111-4111-8111-111111111111', userId: 'user-1', }) ).resolves.toEqual({ settled: true }) - expect(mockFetchGo).toHaveBeenCalledTimes(1) + expect(mockFetchWorker).toHaveBeenCalledTimes(1) }) it('keeps a real worker error visible instead of retrying it as a pending stop', async () => { - mockFetchGo.mockResolvedValue(new Response(null, { status: 503 })) + mockFetchWorker.mockResolvedValue(new Response(null, { status: 503 })) await expect( requestExplicitStreamAbort({ streamId: '11111111-1111-4111-8111-111111111111', userId: 'user-1', }) ).rejects.toThrow('Explicit abort marker request failed: 503') - expect(mockFetchGo).toHaveBeenCalledTimes(1) + expect(mockFetchWorker).toHaveBeenCalledTimes(1) }) it('bounds a hanging settlement recheck with the same deadline', async () => { vi.useFakeTimers() - mockFetchGo + mockFetchWorker .mockImplementationOnce(async () => Response.json({ settled: false })) .mockImplementation( (_url, request) => @@ -142,20 +142,20 @@ describe('requestExplicitStreamAbort', () => { }) await vi.runAllTimersAsync() await expect(stopping).resolves.toEqual({ settled: false }) - expect(mockFetchGo).toHaveBeenCalledTimes(2) + expect(mockFetchWorker).toHaveBeenCalledTimes(2) }) it.each([{ stopped: true }, { settled: 'true' }, null])( 'does not invent settlement from %j', async (body) => { - mockFetchGo.mockResolvedValue(Response.json(body)) + mockFetchWorker.mockResolvedValue(Response.json(body)) await expect( requestExplicitStreamAbort({ streamId: '11111111-1111-4111-8111-111111111111', userId: 'user-1', }) ).resolves.toEqual({ settled: false }) - expect(mockFetchGo).toHaveBeenCalledTimes(1) + expect(mockFetchWorker).toHaveBeenCalledTimes(1) } ) }) diff --git a/apps/sim/lib/mothership/request/session/explicit-abort.ts b/apps/sim/lib/mothership/request/session/explicit-abort.ts index 2131d8810eb..919cf444de1 100644 --- a/apps/sim/lib/mothership/request/session/explicit-abort.ts +++ b/apps/sim/lib/mothership/request/session/explicit-abort.ts @@ -2,10 +2,10 @@ import type { Context } from '@opentelemetry/api' import { sleep } from '@sim/utils/helpers' import { toRecordOrNull } from '@sim/utils/object' import { AbortRequest, type AbortResponse } from '@/lib/mothership/generated/protocol' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { fetchGo } from '@/lib/mothership/request/go/fetch' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' import { mothershipRequestHeaders } from '@/lib/mothership/request/headers' import { AbortReason } from '@/lib/mothership/request/session/abort' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url' export const DEFAULT_EXPLICIT_ABORT_TIMEOUT_MS = 3000 @@ -39,14 +39,14 @@ export async function requestExplicitStreamAbort(params: { try { const mothershipBaseURL = params.mothershipBaseURL ?? (await getMothershipBaseURL({ userId })) while (!controller.signal.aborted) { - const response = await fetchGo(`${mothershipBaseURL}/api/streams/explicit-abort`, { + const response = await fetchWorker(`${mothershipBaseURL}/api/streams/explicit-abort`, { method: 'POST', headers, signal: controller.signal, /** Sim authorizes the actor and canonical run before this service-authenticated signal. */ body: JSON.stringify(AbortRequest.parse({ messageId: streamId })), otelContext, - spanName: 'sim → go /api/streams/explicit-abort', + spanName: 'sim → worker /api/streams/explicit-abort', operation: 'explicit_abort', attributes: { [TraceAttr.StreamId]: streamId, diff --git a/apps/sim/lib/mothership/request/session/recovery.ts b/apps/sim/lib/mothership/request/session/recovery.ts index 83854f906cf..7dba09db6d1 100644 --- a/apps/sim/lib/mothership/request/session/recovery.ts +++ b/apps/sim/lib/mothership/request/session/recovery.ts @@ -4,9 +4,9 @@ import { MothershipStreamV1CompletionStatus, MothershipStreamV1EventType, } from '@/lib/mothership/generated/mothership-stream-v1' -import { CopilotRecoveryOutcome } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { CopilotRecoveryOutcome } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { withCopilotSpan } from '@/lib/mothership/request/otel' import { getLatestSeq, getOldestSeq, readEvents } from './buffer' import { createEvent } from './event' diff --git a/apps/sim/lib/mothership/request/session/replay-budget.integration.ts b/apps/sim/lib/mothership/request/session/replay-budget.integration.ts index c3e5f49e109..23f01b942b8 100644 --- a/apps/sim/lib/mothership/request/session/replay-budget.integration.ts +++ b/apps/sim/lib/mothership/request/session/replay-budget.integration.ts @@ -140,15 +140,11 @@ import { eq } from 'drizzle-orm' import { NextRequest } from 'next/server' import { closeRedisConnection, getRedisClient } from '@/lib/core/config/redis' import { getRedisBudgetKeys, getRedisBudgetLimits } from '@/lib/core/redis/byte-budget.server' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { readChatStream } from '@/lib/mothership/request/application/recover-stream' import { createStreamingContext } from '@/lib/mothership/request/context/request-context' import { restoreStreamingContext } from '@/lib/mothership/request/context/restore' -import { - createFilePreviewAdapterState, - processFilePreviewStreamEvent, -} from '@/lib/mothership/request/go/file-preview-adapter' import { finalizeStream } from '@/lib/mothership/request/lifecycle/finalize' import { createSSEStream } from '@/lib/mothership/request/lifecycle/start' import { acquirePendingChatStream } from '@/lib/mothership/request/session/abort' @@ -167,6 +163,10 @@ import { STREAM_STRING_PREVIEW_UNITS } from '@/lib/mothership/request/session/re import type { StreamEvent } from '@/lib/mothership/request/session/types' import { StreamWriter } from '@/lib/mothership/request/session/writer' import type { StreamingContext } from '@/lib/mothership/request/types' +import { + createFilePreviewAdapterState, + processFilePreviewStreamEvent, +} from '@/lib/mothership/request/worker/file-preview-adapter' import { readCallActivity } from '@/lib/mothership/tools/activity' import { type PendingFileIntent, diff --git a/apps/sim/lib/mothership/request/session/run-replay.ts b/apps/sim/lib/mothership/request/session/run-replay.ts index 992894b76d6..5271833e3da 100644 --- a/apps/sim/lib/mothership/request/session/run-replay.ts +++ b/apps/sim/lib/mothership/request/session/run-replay.ts @@ -6,9 +6,7 @@ import { z } from 'zod' import { WORKER_STREAM_IDLE_TIMEOUT_MS } from '@/lib/mothership/constants' import { MothershipStreamV1EventType } from '@/lib/mothership/generated/mothership-stream-v1' import { type StreamReplayEnd, StreamReplayRequest } from '@/lib/mothership/generated/protocol' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { fetchGo } from '@/lib/mothership/request/go/fetch' -import { FatalSseEventError, processSSEStream } from '@/lib/mothership/request/go/parser' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' import { mothershipRequestHeaders } from '@/lib/mothership/request/headers' import { isTerminalStreamStatus, @@ -16,6 +14,8 @@ import { parsePersistedStreamEventEnvelope, } from '@/lib/mothership/request/session/contract' import { toReplayEnvelope } from '@/lib/mothership/request/session/types' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' +import { FatalSseEventError, processSSEStream } from '@/lib/mothership/request/worker/parser' import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url' const logger = createLogger('RunReplay') @@ -60,12 +60,12 @@ export async function openRunReplay(params: { ) let response: Response try { - response = await fetchGo(`${baseUrl}${REPLAY_PATH}`, { + response = await fetchWorker(`${baseUrl}${REPLAY_PATH}`, { method: 'POST', headers: mothershipRequestHeaders(), body: JSON.stringify(StreamReplayRequest.parse({ streamId, chatId, userId })), signal: AbortSignal.any([signal, unanswered.signal]), - spanName: `sim → go ${REPLAY_PATH}`, + spanName: `sim → worker ${REPLAY_PATH}`, operation: 'stream_replay', attributes: { [TraceAttr.StreamId]: streamId, [TraceAttr.ChatId]: chatId }, }) diff --git a/apps/sim/lib/mothership/request/session/steer.ts b/apps/sim/lib/mothership/request/session/steer.ts index 9eec17f8520..0d3197989e3 100644 --- a/apps/sim/lib/mothership/request/session/steer.ts +++ b/apps/sim/lib/mothership/request/session/steer.ts @@ -1,15 +1,15 @@ import type { Context } from '@opentelemetry/api' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { fetchGo } from '@/lib/mothership/request/go/fetch' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' import { mothershipRequestHeaders } from '@/lib/mothership/request/headers' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url' export const DEFAULT_STEER_TIMEOUT_MS = 3000 /** - * Queues a mid-turn steering message with the Go side (`/api/streams/steer`). + * Queues a mid-turn steering message with the worker (`/api/streams/steer`). * - * Acceptance means "queued", not "applied": Go acknowledges application with a + * Acceptance means "queued", not "applied": the worker acknowledges application with a * `run`/`steering_applied` stream event carrying the steeringId. A caller that * never sees that ack before the stream ends must re-send the content as an * ordinary message — that contract is what makes delivery loss-free without @@ -41,7 +41,7 @@ export async function requestStreamSteering(params: { try { const mothershipBaseURL = await getMothershipBaseURL({ userId }) - const response = await fetchGo(`${mothershipBaseURL}/api/streams/steer`, { + const response = await fetchWorker(`${mothershipBaseURL}/api/streams/steer`, { method: 'POST', headers, signal: controller.signal, @@ -51,7 +51,7 @@ export async function requestStreamSteering(params: { content, }), otelContext, - spanName: 'sim → go /api/streams/steer', + spanName: 'sim → worker /api/streams/steer', operation: 'steer', attributes: { [TraceAttr.StreamId]: streamId, diff --git a/apps/sim/lib/mothership/request/session/stream-recovery.integration.ts b/apps/sim/lib/mothership/request/session/stream-recovery.integration.ts index 96983f03d13..cfc18b8d518 100644 --- a/apps/sim/lib/mothership/request/session/stream-recovery.integration.ts +++ b/apps/sim/lib/mothership/request/session/stream-recovery.integration.ts @@ -66,13 +66,13 @@ import { completeOwnedSimToolCall, upsertAsyncToolCall, } from '@/lib/mothership/async-runs/repository' -import { - createProviderToolCallIdentity, - scopeProviderToolCallId, -} from '@/lib/mothership/request/go/tool-call-identity' import { appendEvents, readEvents } from '@/lib/mothership/request/session/buffer' import { chatStreamLockKey } from '@/lib/mothership/request/session/controller-lease' import { createEvent } from '@/lib/mothership/request/session/event' +import { + createProviderToolCallIdentity, + scopeProviderToolCallId, +} from '@/lib/mothership/request/worker/tool-call-identity' import { GET as streamGET } from '@/app/api/mothership/chat/stream/route' const userId = generateId() diff --git a/apps/sim/lib/mothership/request/tools/desktop-wait.ts b/apps/sim/lib/mothership/request/tools/desktop-wait.ts index cc2d275cf34..adb7957f85b 100644 --- a/apps/sim/lib/mothership/request/tools/desktop-wait.ts +++ b/apps/sim/lib/mothership/request/tools/desktop-wait.ts @@ -15,7 +15,7 @@ import { type AsyncTerminalCompletionSnapshot, } from '@/lib/mothership/async-runs/lifecycle' import { MothershipStreamV1ToolOutcome } from '@/lib/mothership/generated/mothership-stream-v1' -import { CopilotDegradedReason } from '@/lib/mothership/generated/trace-attribute-values-v1' +import { CopilotDegradedReason } from '@/lib/mothership/observability/trace-attribute-values' import { recordDegraded } from '@/lib/mothership/request/metrics' import { settleToolCallFailure } from '@/lib/mothership/request/tools/call-failure' import { waitForClientToolCompletion } from '@/lib/mothership/request/tools/client' diff --git a/apps/sim/lib/mothership/request/tools/execution-lifetime.ts b/apps/sim/lib/mothership/request/tools/execution-lifetime.ts index 6b4a60bbdeb..ec94320b691 100644 --- a/apps/sim/lib/mothership/request/tools/execution-lifetime.ts +++ b/apps/sim/lib/mothership/request/tools/execution-lifetime.ts @@ -20,8 +20,8 @@ import { settleSimSandboxProcess, settleSimToolExecution, } from '@/lib/mothership/async-runs/repository' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { withCopilotSpan } from '@/lib/mothership/request/otel' import { recoverSandboxProcesses } from '@/lib/mothership/request/tools/sandbox-recovery' diff --git a/apps/sim/lib/mothership/request/tools/executor.test.ts b/apps/sim/lib/mothership/request/tools/executor.test.ts index 1251f922207..18b3422e145 100644 --- a/apps/sim/lib/mothership/request/tools/executor.test.ts +++ b/apps/sim/lib/mothership/request/tools/executor.test.ts @@ -259,7 +259,7 @@ describe('pendingToolWaitBudgetMs', () => { it('waits on a person for as long as the whole turn allows', () => { // The 60s default would force-fail a permission prompt while the user was - // still reading it, resuming Go before they ever answered. + // still reading it, resuming the worker before they ever answered. expect(pendingToolWaitBudgetMs({ name: 'terminal_run', status: 'awaiting_approval' })).toBe( TOOL_WATCHDOG_LONG_RUNNING_MS ) diff --git a/apps/sim/lib/mothership/request/tools/executor.ts b/apps/sim/lib/mothership/request/tools/executor.ts index 4ea8030b5ee..87820fb51f0 100644 --- a/apps/sim/lib/mothership/request/tools/executor.ts +++ b/apps/sim/lib/mothership/request/tools/executor.ts @@ -12,9 +12,10 @@ import { asOrchestrationError, type OrchestrationError } from '@/lib/core/orches import { executeCopilotAssistantAction } from '@/lib/mothership/application/assistant-actions' import { isAssistantIntegrationTool } from '@/lib/mothership/assistant/tool-policy' import { AsyncToolCallOwnershipError } from '@/lib/mothership/async-runs/errors' -import type { - AsyncCompletionEnvelope, - AsyncCompletionSignal, +import { + ASYNC_TOOL_STATUS, + type AsyncCompletionEnvelope, + type AsyncCompletionSignal, } from '@/lib/mothership/async-runs/lifecycle' import { type CompleteAsyncToolCallInput, @@ -33,7 +34,6 @@ import { TOOL_WATCHDOG_LONG_RUNNING_MS, } from '@/lib/mothership/constants' import { - MothershipStreamV1AsyncToolRecordStatus, MothershipStreamV1EventType, MothershipStreamV1ToolExecutor, MothershipStreamV1ToolMode, @@ -57,8 +57,8 @@ import { RunWorkflow, RunWorkflowUntilBlock, } from '@/lib/mothership/generated/tool-catalog-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { publishToolConfirmation, waitForToolConfirmation, @@ -129,81 +129,6 @@ function hasOutputValue(result: { output?: unknown } | undefined): result is { o return result !== undefined && Object.hasOwn(result, 'output') } -interface ToolResultSpanSummary { - resultSuccess: boolean - outputBytes: number - outputKind: string - errorMessage?: string - imageCount?: number - imageBytes?: number - attachmentMediaType?: string -} - -function summarizeToolResultForSpan(result: { - success: boolean - output?: unknown - error?: string -}): ToolResultSpanSummary { - const summary: ToolResultSpanSummary = { - resultSuccess: Boolean(result.success), - outputBytes: 0, - outputKind: 'none', - } - if (!result.success && result.error) { - summary.errorMessage = String(result.error).slice(0, 500) - } - if (!hasOutputValue(result)) { - return summary - } - const output = result.output - if (typeof output === 'string') { - summary.outputKind = 'string' - summary.outputBytes = Buffer.byteLength(output) - } else if (output && typeof output === 'object') { - summary.outputKind = Array.isArray(output) ? 'array' : 'object' - try { - summary.outputBytes = Buffer.byteLength(JSON.stringify(output)) - } catch { - summary.outputBytes = 0 - } - const attachment = extractAttachmentShape(output) - if (attachment) { - summary.imageCount = attachment.imageCount - summary.imageBytes = attachment.imageBytes - if (attachment.mediaType) { - summary.attachmentMediaType = attachment.mediaType - } - } - } else if (output !== undefined && output !== null) { - summary.outputKind = typeof output - summary.outputBytes = Buffer.byteLength(String(output)) - } - return summary -} - -function extractAttachmentShape( - output: unknown -): { imageCount: number; imageBytes: number; mediaType?: string } | null { - if (!isRecordLike(output)) return null - const candidate = (output as Record).attachment - if (!isRecordLike(candidate)) return null - const source = (candidate as Record).source - if (!isRecordLike(source)) return null - const type = - typeof (candidate as Record).type === 'string' - ? ((candidate as Record).type as string) - : '' - if (type !== 'image') return null - const mediaType = - typeof source.media_type === 'string' ? (source.media_type as string) : undefined - const data = typeof source.data === 'string' ? (source.data as string) : '' - return { - imageCount: 1, - imageBytes: data.length, - mediaType, - } -} - function buildCompletionSignal(input: { status: AsyncCompletionSignal['status'] message?: string @@ -713,8 +638,7 @@ export async function executeToolAndReport( String(completion.message).slice(0, 500) ) } - // Durable Grafana signal for "which Sim tool is slowest" (executor=sim); - // pairs with the Go executor-boundary metric (U15) as one series set. + // Durable Grafana signal for "which Sim tool is slowest" (executor=sim). recordSimToolMetric( toolCall.name, toolCall.agentId ?? 'main', @@ -844,8 +768,6 @@ async function observeExistingExecution( return terminalCompletionFromToolCall(toolCall) } -type CancelledSpanDetail = { cancelReason: string; error?: string | undefined } - async function executeToolAndReportInner( toolCall: ToolCallState, context: StreamingContext, @@ -855,7 +777,7 @@ async function executeToolAndReportInner( ): Promise { if (toolCall.status === 'executing') { return buildCompletionSignal({ - status: MothershipStreamV1AsyncToolRecordStatus.running, + status: ASYNC_TOOL_STATUS.running, message: 'Tool already executing', }) } @@ -1007,27 +929,8 @@ async function executeToolAndReportInner( return terminalCompletionFromToolCall(toolCall) } - const argsPreview = toolCall.params ? JSON.stringify(toolCall.params).slice(0, 200) : undefined - const toolSpan = context.trace.startSpan(toolCall.name, 'tool.execute', { - toolCallId: toolCall.id, - ...(toolCall.targetWorkspaceId ? { targetWorkspaceId: toolCall.targetWorkspaceId } : {}), - toolName: toolCall.name, - argsPreview, - abortSignalAborted: execContext.abortSignal?.aborted ?? false, - }) - - /** - * The one cancel-settlement path: mark, ack the async record, publish the terminal - * confirmation, optionally close the span. This block was copy-pasted six times with - * only the message/cancelReason varying — and a seventh copy would inevitably drift. - */ - // Hoisted declaration: the pre-execution abort check calls this before endToolSpan's - // const is assigned — safe because that path passes no span, so the reference is - // never evaluated (and the span does not exist yet there anyway). - async function settleCancelled( - message: string, - span?: CancelledSpanDetail - ): Promise { + /** The one cancel-settlement path: mark, ack the async record, publish the terminal confirmation. */ + async function settleCancelled(message: string): Promise { markToolCallCancelled(message) const cancellationResult = toolCall.result markToolResultSeen(context, toolCall.id) @@ -1035,7 +938,7 @@ async function executeToolAndReportInner( if (toolCall.result !== cancellationResult) { return terminalCompletionFromToolCall(toolCall) } - return publishCancellation(message, span) + return publishCancellation(message) } /** Cancels the call only while nothing has started it; otherwise reports nothing settled. */ @@ -1052,13 +955,13 @@ async function executeToolAndReportInner( return { toolCallId: toolCall.id, ...(toolCall.targetWorkspaceId ? { targetWorkspaceId: toolCall.targetWorkspaceId } : {}), - status: MothershipStreamV1AsyncToolRecordStatus.cancelled, + status: ASYNC_TOOL_STATUS.cancelled, result: { cancelled: true }, error: message, } } - function publishCancellation(message: string, span?: CancelledSpanDetail): AsyncCompletionSignal { + function publishCancellation(message: string): AsyncCompletionSignal { publishTerminalToolConfirmation({ toolCallId: toolCall.id, ...(toolCall.targetWorkspaceId ? { targetWorkspaceId: toolCall.targetWorkspaceId } : {}), @@ -1066,42 +969,9 @@ async function executeToolAndReportInner( message, data: { cancelled: true }, }) - if (span) endToolSpan('cancelled', span) return cancelledCompletion(message) } - const endToolSpan = ( - status: string, - detail?: { error?: string; cancelReason?: string; resultSuccess?: boolean } - ) => { - const abortDetail: Record = {} - if (execContext.abortSignal?.aborted) { - abortDetail.abortSignalAborted = true - abortDetail.abortReason = String(execContext.abortSignal.reason ?? 'unknown') - } - if (options?.abortSignal?.aborted) { - abortDetail.optionsAbortReason = String(options.abortSignal.reason ?? 'unknown') - } - if (context.wasAborted) { - abortDetail.wasAborted = true - } - toolSpan.attributes = { ...toolSpan.attributes, ...abortDetail, ...detail } - context.trace.endSpan(toolSpan, status) - } - const endToolSpanFromTerminalState = () => { - const terminalStatus = - toolCall.status === MothershipStreamV1ToolOutcome.cancelled - ? 'cancelled' - : toolCall.status === MothershipStreamV1ToolOutcome.success || - toolCall.status === MothershipStreamV1ToolOutcome.skipped - ? 'ok' - : 'error' - endToolSpan(terminalStatus, { - resultSuccess: toolCall.status === MothershipStreamV1ToolOutcome.success, - ...(toolCall.error ? { error: toolCall.error } : {}), - }) - } - logger.info('Tool execution started', { toolCallId: toolCall.id, ...(toolCall.targetWorkspaceId ? { targetWorkspaceId: toolCall.targetWorkspaceId } : {}), @@ -1151,24 +1021,13 @@ async function executeToolAndReportInner( admittedAssistantAction ) if (toolCallWasCancelled()) { - return settleCancelled(toolCall.error || 'Stopped by user', { - cancelReason: 'abort_during_execution', - }) + return settleCancelled(toolCall.error || 'Stopped by user') } if (toolCall.endTime || isTerminalToolCallStatus(toolCall.status)) { - endToolSpanFromTerminalState() return terminalCompletionFromToolCall(toolCall) } if (abortRequested(context, execContext, options)) { - const copilotResult = inspectToolResultForCopilot( - result, - toolExecutionContext.resolvedSecretTraceRegistry, - toolCall.name - ).result - return settleCancelled('Request aborted during tool execution', { - cancelReason: 'abort_during_execution', - error: copilotResult.success === false ? copilotResult.error : undefined, - }) + return settleCancelled('Request aborted during tool execution') } result = await maybeWriteOutputToFile( executionCall.name, @@ -1177,13 +1036,10 @@ async function executeToolAndReportInner( toolExecutionContext ) if (toolCall.endTime || isTerminalToolCallStatus(toolCall.status)) { - endToolSpanFromTerminalState() return terminalCompletionFromToolCall(toolCall) } if (abortRequested(context, execContext, options)) { - return settleCancelled('Request aborted during tool post-processing', { - cancelReason: 'abort_during_post_processing_file', - }) + return settleCancelled('Request aborted during tool post-processing') } result = await maybeWriteOutputToTable( executionCall.name, @@ -1192,13 +1048,10 @@ async function executeToolAndReportInner( toolExecutionContext ) if (toolCall.endTime || isTerminalToolCallStatus(toolCall.status)) { - endToolSpanFromTerminalState() return terminalCompletionFromToolCall(toolCall) } if (abortRequested(context, execContext, options)) { - return settleCancelled('Request aborted during tool post-processing', { - cancelReason: 'abort_during_post_processing_table', - }) + return settleCancelled('Request aborted during tool post-processing') } result = await maybeWriteReadCsvToTable( executionCall.name, @@ -1207,13 +1060,10 @@ async function executeToolAndReportInner( toolExecutionContext ) if (toolCall.endTime || isTerminalToolCallStatus(toolCall.status)) { - endToolSpanFromTerminalState() return terminalCompletionFromToolCall(toolCall) } if (abortRequested(context, execContext, options)) { - return settleCancelled('Request aborted during tool post-processing', { - cancelReason: 'abort_during_post_processing_csv', - }) + return settleCancelled('Request aborted during tool post-processing') } const projection = inspectToolResultForCopilot( result, @@ -1224,16 +1074,8 @@ async function executeToolAndReportInner( mergeToolRegistry(projection.safe) const modelSucceeded = copilotResult.success - toolSpan.attributes = { - ...toolSpan.attributes, - ...summarizeToolResultForSpan(copilotResult), - ...(projection.safe - ? {} - : { resultWithheld: true, ...describeWithholdingCause(projection.cause) }), - } if (!projection.safe) { - // A withheld SUCCESS otherwise leaves no trace anywhere: the span reads - // ok and the model just sees a bare `{success: true}` with no output. + // A withheld SUCCESS otherwise leaves no trace anywhere: the model just sees a bare `{success: true}` with no output. // The cause is what says whether a guard latched, no catalog was built, // or the payload itself was unprojectable — three different fixes. logger.warn('Tool result withheld by egress projection', { @@ -1297,14 +1139,11 @@ async function executeToolAndReportInner( await commitToolResult({ toolCallId: toolCall.id, ...(toolCall.targetWorkspaceId ? { targetWorkspaceId: toolCall.targetWorkspaceId } : {}), - status: modelSucceeded - ? MothershipStreamV1AsyncToolRecordStatus.completed - : MothershipStreamV1AsyncToolRecordStatus.failed, + status: modelSucceeded ? ASYNC_TOOL_STATUS.completed : ASYNC_TOOL_STATUS.failed, ...(terminalData !== undefined ? { result: terminalData } : {}), error: modelSucceeded ? null : terminalMessage, }) if (toolCall.result !== terminalResult) { - endToolSpanFromTerminalState() return terminalCompletionFromToolCall(toolCall) } committedCompletion = buildCompletionSignal({ @@ -1372,7 +1211,6 @@ async function executeToolAndReportInner( } } catch (error) { if (toolCall.endTime || isTerminalToolCallStatus(toolCall.status)) { - endToolSpanFromTerminalState() return terminalCompletionFromToolCall(toolCall) } const thrownMessage = enrichOpaqueToolError( @@ -1389,10 +1227,7 @@ async function executeToolAndReportInner( mergeToolRegistry(projection.safe) const safeThrownMessage = copilotError.error || 'Tool failed' if (abortRequested(context, execContext, options)) { - return settleCancelled('Request aborted during tool execution', { - cancelReason: 'abort_during_execution_catch', - error: safeThrownMessage, - }) + return settleCancelled('Request aborted during tool execution') } setTerminalToolCallState(toolCall, { status: MothershipStreamV1ToolOutcome.error, @@ -1412,12 +1247,11 @@ async function executeToolAndReportInner( await commitToolResult({ toolCallId: toolCall.id, ...(toolCall.targetWorkspaceId ? { targetWorkspaceId: toolCall.targetWorkspaceId } : {}), - status: MothershipStreamV1AsyncToolRecordStatus.failed, + status: ASYNC_TOOL_STATUS.failed, result: { error: toolCall.error }, error: toolCall.error, }) if (toolCall.result !== terminalErrorResult) { - endToolSpanFromTerminalState() return terminalCompletionFromToolCall(toolCall) } committedCompletion = buildCompletionSignal({ @@ -1463,9 +1297,5 @@ async function executeToolAndReportInner( error: toError(error).message, }) } - endToolSpan(committedCompletion.status === 'success' ? 'ok' : 'error', { - resultSuccess: committedCompletion.status === 'success', - ...(committedCompletion.status === 'error' ? { error: committedCompletion.message } : {}), - }) return committedCompletion } diff --git a/apps/sim/lib/mothership/request/tools/files.ts b/apps/sim/lib/mothership/request/tools/files.ts index e5ac3643a80..76ced40b888 100644 --- a/apps/sim/lib/mothership/request/tools/files.ts +++ b/apps/sim/lib/mothership/request/tools/files.ts @@ -3,10 +3,10 @@ import { toError } from '@sim/utils/errors' import { isRecordLike } from '@sim/utils/object' import { formatCsvValue, toCsvRow } from '@/lib/core/utils/csv' import { RunFunction, UserTable } from '@/lib/mothership/generated/tool-catalog-v1' -import { CopilotOutputFileOutcome } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceEvent } from '@/lib/mothership/generated/trace-events-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { CopilotOutputFileOutcome } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceEvent } from '@/lib/mothership/observability/trace-events' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { withCopilotSpan } from '@/lib/mothership/request/otel' import { denyOutputWriteWithoutWritePermission } from '@/lib/mothership/request/tools/permissions' import { projectToolErrorMessageForCopilot } from '@/lib/mothership/request/tools/resolved-secret-result' diff --git a/apps/sim/lib/mothership/request/tools/permission.test.ts b/apps/sim/lib/mothership/request/tools/permission.test.ts index a47bc10a90d..677b32b9bfa 100644 --- a/apps/sim/lib/mothership/request/tools/permission.test.ts +++ b/apps/sim/lib/mothership/request/tools/permission.test.ts @@ -1,5 +1,4 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' -import { TraceCollector } from '@/lib/mothership/request/trace' const { toolRequiresApproval, waitForToolPermissionDecision } = vi.hoisted(() => ({ toolRequiresApproval: vi.fn().mockReturnValue(true), @@ -36,7 +35,6 @@ function makeContext() { autoAllowed: new Set(), autoAllowPermitted: true, } - context.trace = new TraceCollector() return context } @@ -150,7 +148,7 @@ describe('toolCallNeedsApproval', () => { ).toBe(false) }) - it('gates a resolved integration operation off the frame Go stamped', () => { + it('gates a resolved integration operation off the frame the worker stamped', () => { // gmail_read_v2 is request-local: it is not in the catalog at all, so the // only thing marking it is the awaiting_approval status on the frame. toolRequiresApproval.mockReturnValue(false) @@ -186,12 +184,12 @@ describe('gated tools are askable', () => { }) it('never marks a go-routed tool as requiring approval', async () => { - // Go executes these in-process and streams back a result; the call never + // The worker executes these in-process and streams back a result; the call never // reaches Sim's dispatch, so this gate has nothing to hold. Marking one // would draw a permission card for work that already ran. // // call_integration_tool is the one exception, and only nominally: the - // catalog describes the gateway as go/sync, but at runtime Go resolves the + // catalog describes the gateway as go/sync, but at runtime the worker resolves the // operation and hands it to Sim on a checkpoint, stamping the frame with // awaiting_approval. It is gated on that stamp, under the resolved // operation's name, never under this one. @@ -370,7 +368,7 @@ describe('runGatedToolExecution', () => { expect(execute).not.toHaveBeenCalled() expect(toolCall.status).toBe('skipped') - // Success keeps Go's consecutive-tool-failure breaker from counting a + // Success keeps the worker's consecutive-tool-failure breaker from counting a // deliberate user decision as a malfunction. expect(signal.status).toBe('success') expect(toolCall.result).toMatchObject({ success: true }) diff --git a/apps/sim/lib/mothership/request/tools/permission.ts b/apps/sim/lib/mothership/request/tools/permission.ts index 2c179366551..811d7ed86c8 100644 --- a/apps/sim/lib/mothership/request/tools/permission.ts +++ b/apps/sim/lib/mothership/request/tools/permission.ts @@ -12,8 +12,8 @@ import { MothershipStreamV1ToolPhase, MothershipStreamV1ToolStatus, } from '@/lib/mothership/generated/mothership-stream-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { decisionAllowsExecution, decisionSuppressesFuturePrompts, @@ -87,7 +87,7 @@ export function toolCallNeedsApproval( options: OrchestratorOptions, /** * True when the incoming call frame already carries `awaiting_approval`. - * Go stamps this for resolved integration operations, whose definitions are + * The worker stamps this for resolved integration operations, whose definitions are * request-local and so never appear in the generated catalog. */ frameRequestsApproval = false, @@ -109,11 +109,14 @@ export function toolCallNeedsApproval( // A go-routed tool executes inside mothership and never reaches Sim's // dispatch, so there is nothing here to hold. Stamping the frame anyway // would draw a card for work that already happened, with no waiter behind - // it. Go asks for those gates explicitly via the frame instead. + // it. The worker asks for those gates explicitly via the frame instead. if (getToolEntry(toolName)?.route === 'go') { - logger.error('Tool declares requiresApproval but runs in Go, where Sim cannot gate it', { - toolName, - }) + logger.error( + 'Tool declares requiresApproval but runs in the worker, where Sim cannot gate it', + { + toolName, + } + ) return false } } @@ -148,7 +151,7 @@ function noPromptOutput(toolName: string) { /** * Tell the client how a gated call ended without executing. * - * Go's own tool_result for this call is suppressed (`markToolResultSeen`), so + * The worker's own tool_result for this call is suppressed (`markToolResultSeen`), so * this synthetic frame is the only thing that moves the row out of its * awaiting-approval state in the UI. */ @@ -189,7 +192,7 @@ async function emitGateResult( * Re-emit the call frame as `executing` once the user allows the tool. * * Without this the card would keep offering Allow/Skip until the tool finished, - * because no further call frame arrives from Go after a decision. + * because no further call frame arrives from the worker after a decision. */ async function emitApprovedCall( toolCallId: string, @@ -227,7 +230,7 @@ async function emitApprovedCall( * and it deliberately stays unsettled across both the wait AND the subsequent * execution. That is what keeps the mothership resume blocked: the checkpoint * loop waits on the pending promises before it POSTs `/api/tools/resume`, so - * as long as this has not settled, no tool result reaches Go. + * as long as this has not settled, no tool result reaches the worker. */ export function runGatedToolExecution( toolCall: ToolCallState, @@ -360,7 +363,7 @@ export function runGatedToolExecution( options ) // Reported as success so a declined tool is a decision the model reads - // and adapts to, not a failure that counts toward Go's tool-failure + // and adapts to, not a failure that counts toward the worker's tool-failure // breaker. return { status: MothershipStreamV1ToolOutcome.success, message: output.message } } diff --git a/apps/sim/lib/mothership/request/tools/resources.ts b/apps/sim/lib/mothership/request/tools/resources.ts index 63ffa35f094..48a2e8669ca 100644 --- a/apps/sim/lib/mothership/request/tools/resources.ts +++ b/apps/sim/lib/mothership/request/tools/resources.ts @@ -5,8 +5,9 @@ import { MothershipStreamV1EventType, MothershipStreamV1ResourceOp, } from '@/lib/mothership/generated/mothership-stream-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { CopilotResourcesOp } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { withCopilotSpan } from '@/lib/mothership/request/otel' import type { StreamEvent, ToolCallResult } from '@/lib/mothership/request/types' import { findNonTabFileIds } from '@/lib/mothership/resources/file-tabs' @@ -216,10 +217,10 @@ export async function handleResourceSideEffects( span.setAttributes({ [TraceAttr.CopilotResourcesOp]: isDeleteOp - ? 'delete' + ? CopilotResourcesOp.Delete : upsertedCount > 0 - ? 'upsert' - : 'none', + ? CopilotResourcesOp.Upsert + : CopilotResourcesOp.None, [TraceAttr.CopilotResourcesRemovedCount]: removedCount, [TraceAttr.CopilotResourcesUpsertedCount]: upsertedCount, [TraceAttr.CopilotResourcesAborted]: isAborted(), diff --git a/apps/sim/lib/mothership/request/tools/tables.ts b/apps/sim/lib/mothership/request/tools/tables.ts index 30206587bac..2debd6c7ec0 100644 --- a/apps/sim/lib/mothership/request/tools/tables.ts +++ b/apps/sim/lib/mothership/request/tools/tables.ts @@ -4,10 +4,13 @@ import { parse as csvParse } from 'csv-parse/sync' import { executeCopilotReplaceProjectedWireRows } from '@/lib/mothership/application/table-commands' import { messageForCopilotTableError } from '@/lib/mothership/auth/table-delegation' import { Read as ReadTool, RunFunction } from '@/lib/mothership/generated/tool-catalog-v1' -import { CopilotTableOutcome } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceEvent } from '@/lib/mothership/generated/trace-events-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' +import { + CopilotTableOutcome, + CopilotTableSourceFormat, +} from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceEvent } from '@/lib/mothership/observability/trace-events' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { withCopilotSpan } from '@/lib/mothership/request/otel' import { getOutputFileReceipts, outputWriteFailure } from '@/lib/mothership/request/tools/files' import { denyOutputWriteWithoutWritePermission } from '@/lib/mothership/request/tools/permissions' @@ -245,7 +248,8 @@ export async function maybeWriteReadCsvToTable( const ext = filePath.split('.').pop()?.toLowerCase() span.setAttributes({ [TraceAttr.CopilotTableSourcePath]: filePath, - [TraceAttr.CopilotTableSourceFormat]: ext === 'json' ? 'json' : 'csv', + [TraceAttr.CopilotTableSourceFormat]: + ext === 'json' ? CopilotTableSourceFormat.Json : CopilotTableSourceFormat.Csv, [TraceAttr.CopilotTableSourceContentBytes]: content.length, }) diff --git a/apps/sim/lib/mothership/request/tools/workflow-client-fallback.ts b/apps/sim/lib/mothership/request/tools/workflow-client-fallback.ts index 6311c488771..86886f9a715 100644 --- a/apps/sim/lib/mothership/request/tools/workflow-client-fallback.ts +++ b/apps/sim/lib/mothership/request/tools/workflow-client-fallback.ts @@ -15,7 +15,7 @@ import { getAsyncToolCall, getClaimedWorkflowExecutionId, } from '@/lib/mothership/async-runs/repository' -import { CopilotDegradedReason } from '@/lib/mothership/generated/trace-attribute-values-v1' +import { CopilotDegradedReason } from '@/lib/mothership/observability/trace-attribute-values' import { recordDegraded } from '@/lib/mothership/request/metrics' import { waitForWorkflowToolCompletion } from '@/lib/mothership/request/tools/client' import { settleClientToolCall } from '@/lib/mothership/request/tools/client-settlement.server' diff --git a/apps/sim/lib/mothership/request/trace.ts b/apps/sim/lib/mothership/request/trace.ts index 3c00d77f4e6..14af0e31fc3 100644 --- a/apps/sim/lib/mothership/request/trace.ts +++ b/apps/sim/lib/mothership/request/trace.ts @@ -1,128 +1,8 @@ -import { - type RequestTraceV1CostSummary, - RequestTraceV1Outcome, - type RequestTraceV1SimReport, - type RequestTraceV1Span, - RequestTraceV1SpanSource, - RequestTraceV1SpanStatus, - type RequestTraceV1UsageSummary, -} from '@/lib/mothership/generated/request-trace-v1' - -export class TraceCollector { - private readonly spans: RequestTraceV1Span[] = [] - private readonly startMs = Date.now() - private goTraceId?: string - private activeSpan?: RequestTraceV1Span - - startSpan( - name: string, - kind: string, - attributes?: Record, - parent?: RequestTraceV1Span - ): RequestTraceV1Span { - const startMs = Date.now() - const span: RequestTraceV1Span = { - name, - kind, - startMs, - endMs: startMs, - durationMs: 0, - status: RequestTraceV1SpanStatus.ok, - source: RequestTraceV1SpanSource.sim, - ...(parent - ? { parentName: parent.name } - : this.activeSpan - ? { parentName: this.activeSpan.name } - : {}), - ...(attributes && Object.keys(attributes).length > 0 ? { attributes } : {}), - } - this.spans.push(span) - return span - } - - endSpan( - span: RequestTraceV1Span, - status: RequestTraceV1SpanStatus | string = RequestTraceV1SpanStatus.ok - ): void { - span.endMs = Date.now() - span.durationMs = span.endMs - span.startMs - span.status = status as RequestTraceV1SpanStatus - } - - setActiveSpan(span: RequestTraceV1Span | undefined): void { - this.activeSpan = span - } - - setGoTraceId(id: string): void { - if (!this.goTraceId && id) { - this.goTraceId = id - } - } - - build(params: { - outcome: RequestTraceV1Outcome - simRequestId: string - streamId?: string - chatId?: string - runId?: string - executionId?: string - // Original user prompt, surfaced on the `request_traces.message` - // column at row-insert time so it's queryable from the DB without - // going through Tempo. Sim already has this at chat-POST time; it's - // threaded through here to the trace report so the row is complete - // the moment it's first written instead of waiting on the late - // analytics UPDATE. - userMessage?: string - usage?: { - prompt: number - completion: number - cacheAttemptedRequests?: number - cacheHitRequests?: number - cacheWriteRequests?: number - cacheReadTokens?: number - cacheWriteTokens?: number - cacheSavingsRate?: number - } - cost?: { input: number; output: number; total: number } - }): RequestTraceV1SimReport { - const endMs = Date.now() - const usage: RequestTraceV1UsageSummary | undefined = params.usage - ? { - inputTokens: params.usage.prompt, - outputTokens: params.usage.completion, - cacheAttemptedRequests: params.usage.cacheAttemptedRequests ?? 0, - cacheHitRequests: params.usage.cacheHitRequests ?? 0, - cacheWriteRequests: params.usage.cacheWriteRequests ?? 0, - cacheReadTokens: params.usage.cacheReadTokens ?? 0, - cacheWriteTokens: params.usage.cacheWriteTokens ?? 0, - cacheSavingsRate: params.usage.cacheSavingsRate ?? 0, - } - : undefined - - const cost: RequestTraceV1CostSummary | undefined = params.cost - ? { - rawTotalCost: params.cost.total, - billedTotalCost: params.cost.total, - } - : undefined - - return { - simRequestId: params.simRequestId, - goTraceId: this.goTraceId, - streamId: params.streamId, - chatId: params.chatId, - runId: params.runId, - executionId: params.executionId, - ...(params.userMessage ? { userMessage: params.userMessage } : {}), - startMs: this.startMs, - endMs, - durationMs: endMs - this.startMs, - outcome: params.outcome, - usage, - cost, - spans: this.spans, - } - } -} - -export { RequestTraceV1Outcome, RequestTraceV1SpanStatus } +/** How a copilot request ended, shared by the lifecycle, finalization and OTel root spans. */ +export const RequestTraceOutcome = { + success: 'success', + error: 'error', + cancelled: 'cancelled', +} as const + +export type RequestTraceOutcome = (typeof RequestTraceOutcome)[keyof typeof RequestTraceOutcome] diff --git a/apps/sim/lib/mothership/request/types.ts b/apps/sim/lib/mothership/request/types.ts index f37597a1942..9d0d3c7db09 100644 --- a/apps/sim/lib/mothership/request/types.ts +++ b/apps/sim/lib/mothership/request/types.ts @@ -4,10 +4,8 @@ import { MothershipStreamV1ToolOutcome, } from '@/lib/mothership/generated/mothership-stream-v1' import type { StreamActivityReceipt, ToolActivity } from '@/lib/mothership/generated/protocol' -import type { RequestTraceV1Span } from '@/lib/mothership/generated/request-trace-v1' -import type { ProviderToolCallIdentity } from '@/lib/mothership/request/go/tool-call-identity' import type { StreamEvent } from '@/lib/mothership/request/session' -import type { TraceCollector } from '@/lib/mothership/request/trace' +import type { ProviderToolCallIdentity } from '@/lib/mothership/request/worker/tool-call-identity' import type { ToolExecutionContext, ToolExecutionResult, @@ -152,30 +150,11 @@ export interface ActiveFileIntent { edit?: Record } -// One paused subagent frame in an async continuation. Mirrors the wire -// MothershipStreamV1CheckpointPauseFrame the run handler maps from, but is the -// internal shape the resume driver consumes (named once here so the lifecycle -// driver and handlers reference the same type instead of re-declaring it inline). -export interface ResumeFrame { - parentToolCallId: string - parentToolName: string - pendingToolIds: string[] - // Per-subagent checkpoint model: this frame's OWN checkpoint chain. When set, - // the resume loop must POST /api/tools/resume with THIS id (not the top-level - // checkpointId) carrying only this frame's leaf results, and may drive the N - // frames concurrently. Empty under the bundled-frame model. - checkpointId?: string -} - -// The async-continuation state captured from a checkpoint_pause: what the resume -// loop needs to drive the next /resume (the bundled top-level id + pending tools, -// or per-subagent frames each carrying their own checkpointId). -export interface ResumeContinuation { +interface ResumeContinuation { checkpointId: string executionId?: string runId?: string pendingToolCallIds: string[] - frames?: ResumeFrame[] } export interface StreamingContext { @@ -190,7 +169,7 @@ export interface StreamingContext { desktopDeviceId?: string | null messageId: string /** - * Shared by all live resume legs. Reconnects replay events without resuming Go; any future + * Shared by all live resume legs. Reconnects replay events without resuming the worker; any future * durable lifecycle takeover must persist and restore this map alongside its checkpoints. * Absent on legacy contexts, whose tool IDs retain their original meaning. */ @@ -244,8 +223,6 @@ export interface StreamingContext { activeFileIntents: Map /** File preview content this turn has streamed; one object shared by every leg. */ filePreviewBudget: { contentBytes: number } - trace: TraceCollector - subAgentTraceSpans?: Map /** * Per-request state for the tool permission gate. `autoAllowed` starts from * the user's saved always-allow list and is added to in place when they pick diff --git a/apps/sim/lib/mothership/request/go/fetch.ts b/apps/sim/lib/mothership/request/worker/fetch.ts similarity index 81% rename from apps/sim/lib/mothership/request/go/fetch.ts rename to apps/sim/lib/mothership/request/worker/fetch.ts index 64aaacec800..b2c397c6219 100644 --- a/apps/sim/lib/mothership/request/go/fetch.ts +++ b/apps/sim/lib/mothership/request/worker/fetch.ts @@ -1,13 +1,13 @@ import { type Context, context, SpanStatusCode, trace } from '@opentelemetry/api' -import { CopilotLeg } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { traceHeaders } from '@/lib/mothership/request/go/propagation' +import { CopilotLeg } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' import { isActionableErrorStatus, markSpanForError } from '@/lib/mothership/request/otel' +import { traceHeaders } from '@/lib/mothership/request/worker/propagation' // Lazy tracer resolution: module-level `trace.getTracer()` can be evaluated // before `instrumentation-node.ts` installs the TracerProvider under // Next.js 16 + Turbopack dev, freezing a NoOp tracer and silently dropping -// every outbound Sim → Go span. Resolving per-call avoids the race. +// every outbound Sim → worker span. Resolving per-call avoids the race. const getTracer = () => trace.getTracer('sim-copilot-http', '1.0.0') interface OutboundFetchOptions extends RequestInit { @@ -18,15 +18,18 @@ interface OutboundFetchOptions extends RequestInit { } /** - * Perform an outbound Sim → Go fetch wrapped in an OTel child span so each + * Perform an outbound Sim → worker fetch wrapped in an OTel child span so each * call shows up as a distinct segment in Jaeger, and propagates the W3C - * traceparent so the Go-side span joins the same trace. + * traceparent so the worker's span joins the same trace. * * The span captures generic attributes (method, status, duration, response * size, error code) so any future latency investigation — not just images or * Bedrock — has uniform metadata to work with. */ -export async function fetchGo(url: string, options: OutboundFetchOptions = {}): Promise { +export async function fetchWorker( + url: string, + options: OutboundFetchOptions = {} +): Promise { const { otelContext, spanName, @@ -42,14 +45,14 @@ export async function fetchGo(url: string, options: OutboundFetchOptions = {}): const parentContext = otelContext ?? context.active() const span = getTracer().startSpan( - spanName ?? `sim → go ${pathname}`, + spanName ?? `sim → worker ${pathname}`, { attributes: { [TraceAttr.HttpMethod]: method, [TraceAttr.HttpUrl]: url, [TraceAttr.HttpTarget]: pathname, [TraceAttr.NetPeerName]: parsed?.host ?? '', - [TraceAttr.CopilotLeg]: CopilotLeg.SimToGo, + [TraceAttr.CopilotLeg]: CopilotLeg.SimToWorker, ...(operation ? { [TraceAttr.CopilotOperation]: operation } : {}), ...(attributes ?? {}), }, @@ -83,8 +86,7 @@ export async function fetchGo(url: string, options: OutboundFetchOptions = {}): // Only mark ERROR for actionable status codes. 4xx that represent // normal auth/validation rejections (400/401/403/404/405/422/etc.) // stay UNSET so error dashboards don't drown in expected rejection - // paths. See `isActionableErrorStatus` in Go's telemetry middleware - // for the mirror rule (5xx + 402/409/429). + // paths (`isActionableErrorStatus`: 5xx + 402/409/429). if (isActionableErrorStatus(response.status)) { span.setStatus({ code: SpanStatusCode.ERROR, diff --git a/apps/sim/lib/mothership/request/go/file-preview-adapter.test.ts b/apps/sim/lib/mothership/request/worker/file-preview-adapter.test.ts similarity index 96% rename from apps/sim/lib/mothership/request/go/file-preview-adapter.test.ts rename to apps/sim/lib/mothership/request/worker/file-preview-adapter.test.ts index 090ad17dcf3..ab1057cf77b 100644 --- a/apps/sim/lib/mothership/request/go/file-preview-adapter.test.ts +++ b/apps/sim/lib/mothership/request/worker/file-preview-adapter.test.ts @@ -22,12 +22,6 @@ vi.mock('@/lib/mothership/application/execute-file-use-case', () => ({ })) import { createStreamingContext } from '@/lib/mothership/request/context/request-context' -import { - createFilePreviewAdapterState, - type FilePreviewAdapterState, - PREVIEW_TURN_CONTENT_BYTES, - processFilePreviewStreamEvent, -} from '@/lib/mothership/request/go/file-preview-adapter' import { createEvent, eventToStreamEvent } from '@/lib/mothership/request/session' import { STREAM_EVENT_MAX_PAYLOAD_BYTES } from '@/lib/mothership/request/session/replay-compaction' import type { @@ -35,6 +29,12 @@ import type { ExecutionContext, StreamEvent, } from '@/lib/mothership/request/types' +import { + createFilePreviewAdapterState, + type FilePreviewAdapterState, + PREVIEW_TURN_CONTENT_BYTES, + processFilePreviewStreamEvent, +} from '@/lib/mothership/request/worker/file-preview-adapter' const STREAM_ID = 'stream-1' const EDIT_TOOL_CALL_ID = 'edit-content-1' @@ -50,8 +50,13 @@ function toolEvent(payload: Record): StreamEvent { requestId: 'req-1', type: MothershipStreamV1EventType.tool, payload: { - executor: MothershipStreamV1ToolExecutor.sim, - mode: MothershipStreamV1ToolMode.async, + // Only call and result frames name an executor; argument deltas are draw-only. + ...(payload.phase === MothershipStreamV1ToolPhase.args_delta + ? {} + : { + executor: MothershipStreamV1ToolExecutor.sim, + mode: MothershipStreamV1ToolMode.async, + }), ...payload, }, }) diff --git a/apps/sim/lib/mothership/request/go/file-preview-adapter.ts b/apps/sim/lib/mothership/request/worker/file-preview-adapter.ts similarity index 100% rename from apps/sim/lib/mothership/request/go/file-preview-adapter.ts rename to apps/sim/lib/mothership/request/worker/file-preview-adapter.ts diff --git a/apps/sim/lib/mothership/request/go/inline-images.test.ts b/apps/sim/lib/mothership/request/worker/inline-images.test.ts similarity index 98% rename from apps/sim/lib/mothership/request/go/inline-images.test.ts rename to apps/sim/lib/mothership/request/worker/inline-images.test.ts index 977db56d2d2..580a2bc4333 100644 --- a/apps/sim/lib/mothership/request/go/inline-images.test.ts +++ b/apps/sim/lib/mothership/request/worker/inline-images.test.ts @@ -7,7 +7,7 @@ vi.mock('@/lib/mothership/chat/application/inline-images', () => ({ import { createStreamingContext } from '@/lib/mothership/request/context/request-context' import type { ExecutionContext, StreamEvent } from '@/lib/mothership/request/types' -import { prepareStreamImages } from './inline-images' +import { prepareStreamImages } from '@/lib/mothership/request/worker/inline-images' const chatId = '0518a4c3-f530-4520-8713-9bb3535ef605' const requestId = 'dbd59c19-ea27-43da-992d-71601358e667' diff --git a/apps/sim/lib/mothership/request/go/inline-images.ts b/apps/sim/lib/mothership/request/worker/inline-images.ts similarity index 83% rename from apps/sim/lib/mothership/request/go/inline-images.ts rename to apps/sim/lib/mothership/request/worker/inline-images.ts index 73507709aeb..9454b33ebb5 100644 --- a/apps/sim/lib/mothership/request/go/inline-images.ts +++ b/apps/sim/lib/mothership/request/worker/inline-images.ts @@ -24,13 +24,12 @@ export async function prepareStreamImages( return if (event.type === 'text' && !/[)\]\n]/.test(event.payload.text)) return const { chatId, requestId } = context - if ( - !chatId || - !requestId || - (!execution.workspaceId && !execution.organizationId) || - execution.copilotToolExecution !== true - ) - return + const owner = execution.organizationId + ? { organizationId: execution.organizationId } + : execution.workspaceId + ? { workspaceId: execution.workspaceId } + : undefined + if (!chatId || !requestId || !owner || execution.copilotToolExecution !== true) return if (attempted.size >= MAX_INLINE_IMAGES_PER_TURN) return const content = context.accumulatedContent + (event.type === 'text' ? event.payload.text : '') if (!content.includes('![')) return @@ -42,13 +41,7 @@ export async function prepareStreamImages( signal?.throwIfAborted() try { await materializeStreamImage( - { - userId: execution.userId, - ...(execution.organizationId - ? { organizationId: execution.organizationId } - : { workspaceId: execution.workspaceId! }), - chatId, - }, + { userId: execution.userId, ...owner, chatId }, { requestId, reference, signal } ) } catch (error) { diff --git a/apps/sim/lib/mothership/request/go/parser.ts b/apps/sim/lib/mothership/request/worker/parser.ts similarity index 100% rename from apps/sim/lib/mothership/request/go/parser.ts rename to apps/sim/lib/mothership/request/worker/parser.ts diff --git a/apps/sim/lib/mothership/request/go/propagation.ts b/apps/sim/lib/mothership/request/worker/propagation.ts similarity index 88% rename from apps/sim/lib/mothership/request/go/propagation.ts rename to apps/sim/lib/mothership/request/worker/propagation.ts index 51ec28566cc..8e8872800b2 100644 --- a/apps/sim/lib/mothership/request/go/propagation.ts +++ b/apps/sim/lib/mothership/request/worker/propagation.ts @@ -23,7 +23,7 @@ const headerGetter = { /** * Injects W3C trace context (traceparent, tracestate) into outbound HTTP - * headers so Go-side spans join the same OTel trace tree as the calling + * headers so the worker's spans join the same OTel trace tree as the calling * Sim span. * * Usage: spread the result into your fetch headers: @@ -42,9 +42,9 @@ export function traceHeaders( * Extracts W3C trace context from incoming request headers (traceparent / * tracestate) and returns an OTel Context seeded with the upstream span. * - * Use this at the top of inbound Sim route handlers that Go calls into + * Use this at the top of inbound Sim route handlers that the worker calls * (e.g. /api/billing/update-cost, /api/copilot/api-keys/validate) so the - * Sim-side span becomes a proper child of the Go-side client span in the + * Sim-side span becomes a proper child of the worker's client span in the * same trace — closing the round trip in Jaeger. * * When no traceparent is present (e.g. calls from a browser or a client diff --git a/apps/sim/lib/mothership/request/go/stream.test.ts b/apps/sim/lib/mothership/request/worker/stream.test.ts similarity index 99% rename from apps/sim/lib/mothership/request/go/stream.test.ts rename to apps/sim/lib/mothership/request/worker/stream.test.ts index 48431df2ca3..d0e6f91f3e6 100644 --- a/apps/sim/lib/mothership/request/go/stream.test.ts +++ b/apps/sim/lib/mothership/request/worker/stream.test.ts @@ -78,6 +78,8 @@ vi.mock('@/lib/mothership/tools/server/files/preview', async () => { } }) +import { createEvent, hasAbortMarker } from '@/lib/mothership/request/session' +import type { ExecutionContext, StreamingContext } from '@/lib/mothership/request/types' import { buildPreviewContentUpdate, CopilotBackendError, @@ -88,15 +90,12 @@ import { StreamEndedWithoutTerminalError, WorkerStreamInterruptedError, WorkerUnreachableError, -} from '@/lib/mothership/request/go/stream' +} from '@/lib/mothership/request/worker/stream' import { createProviderToolCallIdentity, PROVIDER_TOOL_CALL_IDENTITY_LIMITS, scopeProviderToolCallId, -} from '@/lib/mothership/request/go/tool-call-identity' -import { createEvent, hasAbortMarker } from '@/lib/mothership/request/session' -import { TraceCollector } from '@/lib/mothership/request/trace' -import type { ExecutionContext, StreamingContext } from '@/lib/mothership/request/types' +} from '@/lib/mothership/request/worker/tool-call-identity' const mockFindFilesByReference = workspaceFilesListMockFns.mockFindActiveWorkspaceFilesByReference @@ -144,7 +143,6 @@ function createStreamingContext(): StreamingContext { errors: [], activeFileIntents: new Map(), filePreviewBudget: { contentBytes: 0 }, - trace: new TraceCollector(), toolPermissions: { enabled: false, autoAllowed: new Set(), @@ -168,7 +166,7 @@ function turnScopedExecContext(): ExecutionContext { } } -describe('copilot go stream helpers', () => { +describe('worker stream helpers', () => { beforeEach(() => { vi.stubGlobal('fetch', vi.fn()) resolveWorkspaceFileReferenceMock.mockReset() @@ -606,8 +604,6 @@ describe('copilot go stream helpers', () => { payload: { toolCallId: 'edit-content-alias-1', toolName: 'apply_file_edit', - executor: MothershipStreamV1ToolExecutor.sim, - mode: MothershipStreamV1ToolMode.async, phase: MothershipStreamV1ToolPhase.args_delta, argumentsDelta: '{"content":"\\n- Added a workflow step', }, diff --git a/apps/sim/lib/mothership/request/go/stream.ts b/apps/sim/lib/mothership/request/worker/stream.ts similarity index 89% rename from apps/sim/lib/mothership/request/go/stream.ts rename to apps/sim/lib/mothership/request/worker/stream.ts index 818a77c83cd..8d4ea9bddca 100644 --- a/apps/sim/lib/mothership/request/go/stream.ts +++ b/apps/sim/lib/mothership/request/worker/stream.ts @@ -4,26 +4,10 @@ import { getErrorMessage } from '@sim/utils/errors' import { toRecordOrNull } from '@sim/utils/object' import { WORKER_STREAM_IDLE_TIMEOUT_MS } from '@/lib/mothership/constants' import { MothershipStreamV1EventType } from '@/lib/mothership/generated/mothership-stream-v1' -import { CopilotSseCloseReason } from '@/lib/mothership/generated/trace-attribute-values-v1' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { TraceEvent } from '@/lib/mothership/generated/trace-events-v1' -import { TraceSpan } from '@/lib/mothership/generated/trace-spans-v1' -import { fetchGo } from '@/lib/mothership/request/go/fetch' -import { - buildPreviewContentUpdate, - createFilePreviewAdapterState, - decodeJsonStringPrefix, - extractEditContent, - processFilePreviewStreamEvent, -} from '@/lib/mothership/request/go/file-preview-adapter' -import { prepareStreamImages } from '@/lib/mothership/request/go/inline-images' -import { - FatalSseEventError, - processSSEStream, - StreamContinuityError, -} from '@/lib/mothership/request/go/parser' -import { reconcileTextEvent } from '@/lib/mothership/request/go/text-receipt' -import { scopeProviderToolCallEvent } from '@/lib/mothership/request/go/tool-call-identity' +import { CopilotSseCloseReason } from '@/lib/mothership/observability/trace-attribute-values' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { TraceEvent } from '@/lib/mothership/observability/trace-events' +import { TraceSpan } from '@/lib/mothership/observability/trace-spans' import { applyStreamEvent, prePersistClientExecutableToolCall, @@ -50,8 +34,24 @@ import type { StreamEvent, StreamingContext, } from '@/lib/mothership/request/types' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' +import { + buildPreviewContentUpdate, + createFilePreviewAdapterState, + decodeJsonStringPrefix, + extractEditContent, + processFilePreviewStreamEvent, +} from '@/lib/mothership/request/worker/file-preview-adapter' +import { prepareStreamImages } from '@/lib/mothership/request/worker/inline-images' +import { + FatalSseEventError, + processSSEStream, + StreamContinuityError, +} from '@/lib/mothership/request/worker/parser' +import { reconcileTextEvent } from '@/lib/mothership/request/worker/text-receipt' +import { scopeProviderToolCallEvent } from '@/lib/mothership/request/worker/tool-call-identity' -const logger = createLogger('CopilotGoStream') +const logger = createLogger('MothershipWorkerStream') export { buildPreviewContentUpdate, decodeJsonStringPrefix, extractEditContent } @@ -174,15 +174,11 @@ export interface StreamLoopOptions extends OrchestratorOptions { * Return true to skip the default handler for this event. */ onBeforeDispatch?: (event: StreamEvent, context: StreamingContext) => boolean | undefined - /** - * Called when the Go backend's trace ID (go_trace_id) is first received via SSE. - */ - onGoTraceId?: (goTraceId: string) => void otelContext?: Context } /** - * Run the SSE stream processing loop against the Go backend. + * Run the SSE stream processing loop against the worker. * * Handles: fetch -> parse -> normalize -> dedupe -> subagent routing -> handler dispatch. * Callers provide the fetch URL/options and can intercept events via onBeforeDispatch. @@ -220,20 +216,14 @@ export async function runStreamLoop( const pathname = new URL(fetchUrl).pathname const requestBodyBytes = estimateBodyBytes(fetchOptions.body) - const fetchSpan = context.trace.startSpan(`HTTP Request → ${pathname}`, 'sim.http.fetch', { - url: fetchUrl, - method: fetchOptions.method ?? 'GET', - requestBodyBytes, - }) - const fetchStart = performance.now() let response: Response armIdleTimeout() try { - response = await fetchGo(fetchUrl, { + response = await fetchWorker(fetchUrl, { ...fetchOptions, signal: requestSignal, otelContext: options.otelContext, - spanName: `sim → go ${pathname}`, + spanName: `sim → worker ${pathname}`, operation: 'stream', attributes: { [TraceAttr.CopilotStream]: true, @@ -241,26 +231,13 @@ export async function runStreamLoop( }, }) } catch (error) { - fetchSpan.attributes = { - ...(fetchSpan.attributes ?? {}), - headersMs: Math.round(performance.now() - fetchStart), - } - context.trace.endSpan(fetchSpan, abortSignal?.aborted ? 'cancelled' : 'error') if (idle.signal.aborted) throw new WorkerUnreachableError(idle.signal.reason) if (requestSignal.aborted) throw error throw new WorkerUnreachableError(error) } finally { clearTimeout(idleTimer) } - const headersElapsedMs = Math.round(performance.now() - fetchStart) - fetchSpan.attributes = { - ...(fetchSpan.attributes ?? {}), - status: response.status, - headersMs: headersElapsedMs, - } - if (!response.ok) { - context.trace.endSpan(fetchSpan, 'error') // An error body is bounded by the same silence as the leg; a stalled one reads as empty. armIdleTimeout() const errorText = await new Promise((resolve) => { @@ -279,29 +256,20 @@ export async function runStreamLoop( } if (!response.body) { - context.trace.endSpan(fetchSpan, 'error') throw new CopilotBackendError('Copilot backend response missing body') } - context.trace.endSpan(fetchSpan) - - const bodySpan = context.trace.startSpan(`SSE Body → ${pathname}`, 'sim.http.stream_body', { - url: fetchUrl, - method: fetchOptions.method ?? 'GET', - }) - // Aggregate counters populated inline by the reader wrapper + onEvent - // dispatcher below and flushed to both the legacy TraceCollector span - // and the OTel read-loop span when the loop terminates. Kept as plain - // JS variables (not span attrs) so incrementing them is free — we - // only pay OTel cost once at span End(). + // dispatcher below and flushed to the OTel read-loop span when the loop + // terminates. Kept as plain JS variables (not span attrs) so incrementing + // them is free — we only pay OTel cost once at span End(). // // Idle-gap tracking is split two ways so we can tell apart // upstream-silent from we-were-busy: // // - `longestInboundGapMs`: biggest time between consecutive // `reader.read()` calls returning bytes. Upper bound on - // "Go silent". Actually also includes Node waiting for main + // "worker silent". Actually also includes Node waiting for main // thread free, so see dispatchMs below. // - `longestDispatchMs`: biggest time any single event handler // took between "event received" and "returned control". Upper @@ -389,7 +357,7 @@ export async function runStreamLoop( try { await processSSEStream(reader, abortSignal, async (raw) => { // Track how long THIS handler invocation takes so we can tell - // apart "Go was silent" from "we were CPU-bound on a handler". + // apart "the worker was silent" from "we were CPU-bound on a handler". // `longestInboundGapMs` includes handler time (the next reader.read // doesn't run until the previous handler returns), so dispatch // time is the correction needed to isolate upstream silence. @@ -432,11 +400,6 @@ export async function runStreamLoop( } const streamEvent = reconcileTextEvent(scopedEvent, context.accumulatedContent) if (!streamEvent) return - if (envelope.trace?.requestId) { - const goTraceId = envelope.trace.goTraceId || envelope.trace.requestId - context.trace.setGoTraceId(goTraceId) - options.onGoTraceId?.(goTraceId) - } // Per-type counters for the copilot.sse.read_loop span. Bound set // (8 types) so this can never blow up into high cardinality. @@ -452,7 +415,7 @@ export async function runStreamLoop( // upstream provider error) here to explain a client-side "Stream error". if (streamEvent.type === MothershipStreamV1EventType.error) { const errorPayload = streamEvent.payload - logger.error('Received error event from Go copilot stream', { + logger.error('Received error event from worker stream', { path: pathname, lane: streamEvent.scope?.lane ?? 'main', parentToolCallId: streamEvent.scope?.parentToolCallId, @@ -612,26 +575,6 @@ export async function runStreamLoop( clearTimeout(timeoutId) clearTimeout(idleTimer) - // Legacy TraceCollector span (consumed by the in-memory trace - // collector, kept for backwards compatibility with existing - // tooling). The real OTel span is stamped below. - const bodyDurationMs = Math.round(performance.now() - bodyStart) - bodySpan.attributes = { - ...(bodySpan.attributes ?? {}), - eventsReceived: counters.events, - firstEventMs: counters.firstEventMs, - endedOn, - durationMs: bodyDurationMs, - } - context.trace.endSpan( - bodySpan, - endedOn === CopilotSseCloseReason.Terminal - ? 'ok' - : endedOn === CopilotSseCloseReason.Aborted - ? 'cancelled' - : 'error' - ) - // Real OTel span for Tempo/Grafana. Stamped aggregate-only so // there is no per-chunk OTel cost — one span per read loop with // integer counters, plus a bounded set of events. diff --git a/apps/sim/lib/mothership/request/go/text-receipt.test.ts b/apps/sim/lib/mothership/request/worker/text-receipt.test.ts similarity index 96% rename from apps/sim/lib/mothership/request/go/text-receipt.test.ts rename to apps/sim/lib/mothership/request/worker/text-receipt.test.ts index ae624f68b61..78880c56ac5 100644 --- a/apps/sim/lib/mothership/request/go/text-receipt.test.ts +++ b/apps/sim/lib/mothership/request/worker/text-receipt.test.ts @@ -1,12 +1,12 @@ import { describe, expect, it } from 'vitest' +import { parsePersistedStreamEventEnvelope } from '@/lib/mothership/request/session/contract' +import type { StreamEvent } from '@/lib/mothership/request/types' import { FatalSseEventError, processSSEStream, StreamContinuityError, -} from '@/lib/mothership/request/go/parser' -import { reconcileTextEvent } from '@/lib/mothership/request/go/text-receipt' -import { parsePersistedStreamEventEnvelope } from '@/lib/mothership/request/session/contract' -import type { StreamEvent } from '@/lib/mothership/request/types' +} from '@/lib/mothership/request/worker/parser' +import { reconcileTextEvent } from '@/lib/mothership/request/worker/text-receipt' function text(content: string, textOffset: number): StreamEvent { return { type: 'text', payload: { channel: 'assistant', text: content, textOffset } } diff --git a/apps/sim/lib/mothership/request/go/text-receipt.ts b/apps/sim/lib/mothership/request/worker/text-receipt.ts similarity index 97% rename from apps/sim/lib/mothership/request/go/text-receipt.ts rename to apps/sim/lib/mothership/request/worker/text-receipt.ts index 673d31f98a0..948efd6ec8a 100644 --- a/apps/sim/lib/mothership/request/go/text-receipt.ts +++ b/apps/sim/lib/mothership/request/worker/text-receipt.ts @@ -1,5 +1,5 @@ -import { FatalSseEventError, StreamContinuityError } from '@/lib/mothership/request/go/parser' import type { StreamEvent } from '@/lib/mothership/request/types' +import { FatalSseEventError, StreamContinuityError } from '@/lib/mothership/request/worker/parser' /** Reconcile before forwarding or persistence so replay cannot duplicate visible text. */ export function reconcileTextEvent(event: StreamEvent, received: string): StreamEvent | null { diff --git a/apps/sim/lib/mothership/request/go/tool-call-identity.test.ts b/apps/sim/lib/mothership/request/worker/tool-call-identity.test.ts similarity index 81% rename from apps/sim/lib/mothership/request/go/tool-call-identity.test.ts rename to apps/sim/lib/mothership/request/worker/tool-call-identity.test.ts index 1375e4cb8f0..c3eb2473f4a 100644 --- a/apps/sim/lib/mothership/request/go/tool-call-identity.test.ts +++ b/apps/sim/lib/mothership/request/worker/tool-call-identity.test.ts @@ -1,17 +1,17 @@ import { describe, expect, it } from 'vitest' -import { - createProviderToolCallIdentity, - PROVIDER_TOOL_CALL_IDENTITY_LIMITS, - restoreProviderToolCallId, - scopeProviderToolCallEvent, - scopeProviderToolCallId, -} from '@/lib/mothership/request/go/tool-call-identity' import { markToolResultSeen, shouldSkipToolCallEvent, shouldSkipToolResultEvent, } from '@/lib/mothership/request/sse-utils' import type { StreamEvent } from '@/lib/mothership/request/types' +import { + createProviderToolCallIdentity, + PROVIDER_TOOL_CALL_IDENTITY_LIMITS, + restoreProviderToolCallId, + scopeProviderToolCallEvent, + scopeProviderToolCallId, +} from '@/lib/mothership/request/worker/tool-call-identity' function toolCall(toolCallId: string): StreamEvent { return { @@ -87,7 +87,7 @@ describe('provider tool-call identity boundary', () => { expect(restoreProviderToolCallId(rawId)).toBe(rawId) }) - it('refuses to send a canonical ID to Go after losing its reverse mapping', () => { + it('refuses to send a canonical ID to the worker after losing its reverse mapping', () => { const canonicalId = scopeProviderToolCallId('call-1', createProviderToolCallIdentity('run')) expect(() => restoreProviderToolCallId(canonicalId, createProviderToolCallIdentity('run')) @@ -137,14 +137,6 @@ describe('provider tool-call identity boundary', () => { executionId: 'execution', runId: 'provider-run', pendingToolCallIds: ['pending-only'], - frames: [ - { - parentToolCallId: 'parent', - parentToolName: 'files', - pendingToolIds: ['pending-only'], - checkpointId: 'child-checkpoint', - }, - ], }, } const mapped = scopeProviderToolCallEvent(checkpoint, identity) @@ -156,14 +148,6 @@ describe('provider tool-call identity boundary', () => { payload: { ...checkpoint.payload, pendingToolCallIds: [pendingId], - frames: [ - { - parentToolCallId: parentId, - parentToolName: 'files', - pendingToolIds: [pendingId], - checkpointId: 'child-checkpoint', - }, - ], }, }) expect(restoreProviderToolCallId(pendingId, identity)).toBe('pending-only') @@ -176,26 +160,4 @@ describe('provider tool-call identity boundary', () => { }) expect(scopeProviderToolCallEvent(call)).toBe(call) }) - - it('maps both subagent span ID spellings and leaves structured result data opaque', () => { - const identity = createProviderToolCallIdentity('span-run') - const data = { tool_call_id: 'parent', toolCallId: 'parent', nested: { toolCallId: 'parent' } } - const span: StreamEvent = { - type: 'span', - payload: { kind: 'subagent', event: 'start', data }, - } - const parentId = scopeProviderToolCallId('parent', identity) - expect(scopeProviderToolCallEvent(span, identity)).toEqual({ - ...span, - payload: { - ...span.payload, - data: { ...data, tool_call_id: parentId, toolCallId: parentId }, - }, - }) - const result: StreamEvent = { - type: 'span', - payload: { kind: 'structured_result', data }, - } - expect(scopeProviderToolCallEvent(result, identity)).toBe(result) - }) }) diff --git a/apps/sim/lib/mothership/request/go/tool-call-identity.ts b/apps/sim/lib/mothership/request/worker/tool-call-identity.ts similarity index 76% rename from apps/sim/lib/mothership/request/go/tool-call-identity.ts rename to apps/sim/lib/mothership/request/worker/tool-call-identity.ts index d42153c3c18..613584273cb 100644 --- a/apps/sim/lib/mothership/request/go/tool-call-identity.ts +++ b/apps/sim/lib/mothership/request/worker/tool-call-identity.ts @@ -1,5 +1,4 @@ import { createHash } from 'node:crypto' -import { isRecordLike } from '@sim/utils/object' import type { StreamEvent } from '@/lib/mothership/request/types' export const PROVIDER_TOOL_CALL_IDENTITY_LIMITS = { @@ -44,7 +43,7 @@ export function scopeProviderToolCallId( return toolCallId } -/** Only runs that opted into namespacing decode IDs on the return trip to Go. */ +/** Only runs that opted into namespacing decode IDs on the return trip to the worker. */ export function restoreProviderToolCallId( toolCallId: string, identity?: ProviderToolCallIdentity @@ -83,37 +82,8 @@ export function scopeProviderToolCallEvent( payload: { ...scoped.payload, pendingToolCallIds: scoped.payload.pendingToolCallIds.map(scopeId), - ...(scoped.payload.frames - ? { - frames: scoped.payload.frames.map((frame) => ({ - ...frame, - parentToolCallId: scopeId(frame.parentToolCallId), - pendingToolIds: frame.pendingToolIds.map(scopeId), - })), - } - : {}), }, } } - if ( - scoped.type === 'span' && - scoped.payload.kind === 'subagent' && - isRecordLike(scoped.payload.data) - ) { - const mapped = { ...scoped } - mapped.payload = { - ...scoped.payload, - data: { - ...scoped.payload.data, - ...(typeof scoped.payload.data.tool_call_id === 'string' - ? { tool_call_id: scopeId(scoped.payload.data.tool_call_id) } - : {}), - ...(typeof scoped.payload.data.toolCallId === 'string' - ? { toolCallId: scopeId(scoped.payload.data.toolCallId) } - : {}), - }, - } - return mapped - } return scoped } diff --git a/apps/sim/lib/mothership/server/api-keys.ts b/apps/sim/lib/mothership/server/api-keys.ts index a4d5a16ecac..dad1b8f1140 100644 --- a/apps/sim/lib/mothership/server/api-keys.ts +++ b/apps/sim/lib/mothership/server/api-keys.ts @@ -1,6 +1,6 @@ import { env } from '@/lib/core/config/env' -import { TraceAttr } from '@/lib/mothership/generated/trace-attributes-v1' -import { fetchGo } from '@/lib/mothership/request/go/fetch' +import { TraceAttr } from '@/lib/mothership/observability/trace-attributes' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url' /** @@ -62,14 +62,14 @@ async function callValidateKey({ }: ValidateKeyCall): Promise { const mothershipBaseURL = await getMothershipBaseURL({ userId }) - const res = await fetchGo(`${mothershipBaseURL}/api/validate-key/${path}`, { + const res = await fetchWorker(`${mothershipBaseURL}/api/validate-key/${path}`, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(env.COPILOT_API_KEY ? { 'x-api-key': env.COPILOT_API_KEY } : {}), }, body: JSON.stringify({ userId, ...body }), - spanName: `sim → go /api/validate-key/${path}`, + spanName: `sim → worker /api/validate-key/${path}`, operation, attributes: { [TraceAttr.UserId]: userId, ...attributes }, }) diff --git a/apps/sim/lib/mothership/tasks/application/read-status.ts b/apps/sim/lib/mothership/tasks/application/read-status.ts index 3f46b873e96..806a72c517f 100644 --- a/apps/sim/lib/mothership/tasks/application/read-status.ts +++ b/apps/sim/lib/mothership/tasks/application/read-status.ts @@ -4,8 +4,8 @@ import { OrchestrationError } from '@/lib/core/orchestration/types' import { defineAuthorizedChatUseCase } from '@/lib/mothership/chat/application/authorized-chat-use-case' import { resolveOwnedChatContext } from '@/lib/mothership/chat/application/context' import { InternalTaskStatus } from '@/lib/mothership/generated/tasks' -import { fetchGo } from '@/lib/mothership/request/go/fetch' import { mothershipRequestHeaders } from '@/lib/mothership/request/headers' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url' import { organizationTaskOperations, @@ -22,7 +22,7 @@ export class TaskServiceUnavailableError extends Error { async function fetchTaskStatus(base: string, input: { taskId: string }) { try { - const response = await fetchGo(`${base}/api/tasks/status`, { + const response = await fetchWorker(`${base}/api/tasks/status`, { method: 'POST', headers: mothershipRequestHeaders(), body: JSON.stringify(input), diff --git a/apps/sim/lib/mothership/tasks/application/tasks.test.ts b/apps/sim/lib/mothership/tasks/application/tasks.test.ts index 40b92511bd4..8920442838b 100644 --- a/apps/sim/lib/mothership/tasks/application/tasks.test.ts +++ b/apps/sim/lib/mothership/tasks/application/tasks.test.ts @@ -9,9 +9,9 @@ import { mothershipAgentUrlMockFns, } from '@sim/testing/mocks/mothership-agent-url.mock' import { - mothershipGoFetchMock, - mothershipGoFetchMockFns, -} from '@sim/testing/mocks/mothership-go-fetch.mock' + mothershipWorkerFetchMock, + mothershipWorkerFetchMockFns, +} from '@sim/testing/mocks/mothership-worker-fetch.mock' import { createMockRequest } from '@sim/testing/mocks/request.mock' import { workflowContextMock, @@ -34,7 +34,7 @@ vi.mock('next/server', async (original) => ({ ...(await original()), after: hoisted.after, })) -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) vi.mock('@/lib/auth/ban', () => authBanMock) vi.mock('@/lib/core/application/workspace-authorization', async (original) => ({ @@ -68,7 +68,7 @@ const mocks = { ...hoisted, banned: authBanMockFns.mockGetActivelyBannedUserIds, internalAuth: copilotHttpMockFns.mockCheckInternalApiKey, - worker: mothershipGoFetchMockFns.mockFetchGo, + worker: mothershipWorkerFetchMockFns.mockFetchWorker, workspace: workspaceContextMockFns.mockResolveActiveWorkspaceApplicationContext, execution: workflowContextMockFns.mockResolveActiveWorkflowRunApplicationContext, } diff --git a/apps/sim/lib/mothership/tasks/wake.ts b/apps/sim/lib/mothership/tasks/wake.ts index 23ef4c22d9e..931e2674862 100644 --- a/apps/sim/lib/mothership/tasks/wake.ts +++ b/apps/sim/lib/mothership/tasks/wake.ts @@ -78,7 +78,7 @@ export async function runWakeTurn(input: WakeRequest): Promise { userId, ...(organizationId ? { organizationId } : { workspaceId }), chatId, - goRoute: '/api/mothership', + workerRoute: '/api/mothership', autoExecuteTools: true, interactive: false, billingAttribution, diff --git a/apps/sim/lib/mothership/tool-executor/executor.test.ts b/apps/sim/lib/mothership/tool-executor/executor.test.ts index 49d72642c51..3afbeb914b7 100644 --- a/apps/sim/lib/mothership/tool-executor/executor.test.ts +++ b/apps/sim/lib/mothership/tool-executor/executor.test.ts @@ -542,7 +542,7 @@ describe('copilot tool executor fallback', () => { /** * An integration tool resolves `{{SECRET}}` into its user-only params, which is a real use - * of a workspace secret. This is the branch that carries it — a gateway call Go resolves to + * of a workspace secret. This is the branch that carries it — a gateway call the worker resolves to * `slack_send` is not in the copilot catalog, so it lands here rather than on a handler. */ it('records the secrets an integration tool call resolved', async () => { diff --git a/apps/sim/lib/mothership/tool-executor/router.ts b/apps/sim/lib/mothership/tool-executor/router.ts index e62c41028a7..d667b0d313e 100644 --- a/apps/sim/lib/mothership/tool-executor/router.ts +++ b/apps/sim/lib/mothership/tool-executor/router.ts @@ -26,7 +26,7 @@ export function isKnownTool(toolId: string): boolean { /** * Whether a call must wait for the user's approval. * - * Declared in the mothership tool catalog, which Go carries but never enforces, + * Declared in the mothership tool catalog, which the worker carries but never enforces, * except for browser tools: Sim owns their split between acting on a page and * only observing it. One that acts does so as the user, in a browser signed in * to their accounts, so it is gated like a terminal command. Reading the split diff --git a/apps/sim/lib/mothership/tools/client/browser-tool-execution.ts b/apps/sim/lib/mothership/tools/client/browser-tool-execution.ts index 1472dc0d857..2b2fd3b3945 100644 --- a/apps/sim/lib/mothership/tools/client/browser-tool-execution.ts +++ b/apps/sim/lib/mothership/tools/client/browser-tool-execution.ts @@ -2,7 +2,7 @@ * Client-side execution of `browser_*` copilot tools. * * Mirrors the other client-executed tool flows (run-tool, local filesystem): - * the Go orchestrator emits a client-executed tool call and blocks on Redis; + * the worker emits a client-executed tool call and waits for its result; * this module performs the action through the desktop app's built-in agent * browser and reports the outcome via the confirm endpoint, which wakes the * server-side waiter. diff --git a/apps/sim/lib/mothership/tools/client/desktop-tool-authorization.integration.ts b/apps/sim/lib/mothership/tools/client/desktop-tool-authorization.integration.ts index 72f75ce2e1b..6b93f295793 100644 --- a/apps/sim/lib/mothership/tools/client/desktop-tool-authorization.integration.ts +++ b/apps/sim/lib/mothership/tools/client/desktop-tool-authorization.integration.ts @@ -39,7 +39,6 @@ import { import { getAutoAllowedTools } from '@/lib/mothership/persistence/tool-permission/auto-allow' import { prePersistClientExecutableToolCall } from '@/lib/mothership/request/handlers' import { waitForClientToolCompletion } from '@/lib/mothership/request/tools/client' -import { TraceCollector } from '@/lib/mothership/request/trace' import type { StreamingContext } from '@/lib/mothership/request/types' import { POST as confirmPOST } from '@/app/api/copilot/confirm/route' import { POST as toolPermissionPOST } from '@/app/api/copilot/tool-permission/route' @@ -144,7 +143,6 @@ describe.runIf(Boolean(redisUrl))('desktop tool calls the server no longer admit accumulatedContent: '', finalAssistantContent: '', sawMainToolCall: false, - trace: new TraceCollector(), contentBlocks: [], toolCalls: new Map(), pendingToolPromises: new Map(), diff --git a/apps/sim/lib/mothership/tools/client/desktop-tool-chat-view-lease.integration.ts b/apps/sim/lib/mothership/tools/client/desktop-tool-chat-view-lease.integration.ts index c1cd8f4b5db..5e835ab141c 100644 --- a/apps/sim/lib/mothership/tools/client/desktop-tool-chat-view-lease.integration.ts +++ b/apps/sim/lib/mothership/tools/client/desktop-tool-chat-view-lease.integration.ts @@ -43,7 +43,6 @@ import { getChatViewDesktopLeaseRemainingMs, } from '@/lib/mothership/async-runs/repository' import { prePersistClientExecutableToolCall } from '@/lib/mothership/request/handlers' -import { TraceCollector } from '@/lib/mothership/request/trace' import type { StreamingContext } from '@/lib/mothership/request/types' import { chatViewDesktopLeaseOwnerToken } from '@/lib/mothership/tools/desktop-tools' import { POST as confirmPOST } from '@/app/api/copilot/confirm/route' @@ -143,7 +142,6 @@ describe.runIf(Boolean(redisUrl))('a chat-view import kept alive by its lease', accumulatedContent: '', finalAssistantContent: '', sawMainToolCall: false, - trace: new TraceCollector(), contentBlocks: [], toolCalls: new Map(), pendingToolPromises: new Map(), diff --git a/apps/sim/lib/mothership/tools/client/desktop-tool-pickup.integration.ts b/apps/sim/lib/mothership/tools/client/desktop-tool-pickup.integration.ts index e0d9701fbaf..1ad836c9dcf 100644 --- a/apps/sim/lib/mothership/tools/client/desktop-tool-pickup.integration.ts +++ b/apps/sim/lib/mothership/tools/client/desktop-tool-pickup.integration.ts @@ -31,7 +31,6 @@ import { NextRequest } from 'next/server' import { closeRedisConnection } from '@/lib/core/config/redis' import { SIM_TOOL_EXECUTION_VERSION } from '@/lib/mothership/async-runs/lifecycle' import { prePersistClientExecutableToolCall, sseHandlers } from '@/lib/mothership/request/handlers' -import { TraceCollector } from '@/lib/mothership/request/trace' import type { StreamEvent, StreamingContext } from '@/lib/mothership/request/types' import { POST as confirmPOST } from '@/app/api/copilot/confirm/route' import { POST as authorizePOST } from '@/app/api/desktop/tool/authorize/route' @@ -91,7 +90,6 @@ describe.runIf(Boolean(redisUrl)).concurrent('a desktop tool call nobody picks u accumulatedContent: '', finalAssistantContent: '', sawMainToolCall: false, - trace: new TraceCollector(), contentBlocks: [], toolCalls: new Map(), pendingToolPromises: new Map(), diff --git a/apps/sim/lib/mothership/tools/client/terminal-tool-execution.ts b/apps/sim/lib/mothership/tools/client/terminal-tool-execution.ts index 75f6ed499e8..236db6c41be 100644 --- a/apps/sim/lib/mothership/tools/client/terminal-tool-execution.ts +++ b/apps/sim/lib/mothership/tools/client/terminal-tool-execution.ts @@ -2,8 +2,8 @@ * Client-side execution of `terminal_*` copilot tools. * * Mirrors the other client-executed tool flows (browser, run-tool, local - * filesystem): the Go orchestrator emits a client-executed tool call and blocks - * on Redis; this module performs the action through the desktop app's terminal + * filesystem): the worker emits a client-executed tool call and waits for its + * result; this module performs the action through the desktop app's terminal * and reports the outcome via the confirm endpoint, which wakes the * server-side waiter. */ diff --git a/apps/sim/lib/mothership/tools/handlers/workbench-confidentiality.live.test.ts b/apps/sim/lib/mothership/tools/handlers/workbench-confidentiality.live.test.ts index d7251df34e6..db1a301914e 100644 --- a/apps/sim/lib/mothership/tools/handlers/workbench-confidentiality.live.test.ts +++ b/apps/sim/lib/mothership/tools/handlers/workbench-confidentiality.live.test.ts @@ -18,9 +18,9 @@ import { mothershipAsyncRunsMockFns, } from '@sim/testing/mocks/mothership-async-runs.mock' import { - mothershipGoFetchMock, - mothershipGoFetchMockFns, -} from '@sim/testing/mocks/mothership-go-fetch.mock' + mothershipWorkerFetchMock, + mothershipWorkerFetchMockFns, +} from '@sim/testing/mocks/mothership-worker-fetch.mock' import { mothershipWorkspaceTargetMock, mothershipWorkspaceTargetMockFns, @@ -51,7 +51,7 @@ vi.mock('@/lib/execution/remote-sandbox/resolve', () => ({ RUNTIME_INSTALL_TIMEOUT_MS: 60_000, })) vi.mock('@/lib/mothership/async-runs/repository', () => mothershipAsyncRunsMock) -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) vi.mock('@/lib/workspace-files/application/delegated-principal', () => ({ rebindWorkspaceFileDelegatedPrincipal: ({ principal }: { principal: unknown }) => principal, @@ -232,7 +232,7 @@ beforeEach(async () => { }) mothershipAsyncRunsMockFns.mockIsActiveSandboxResourceOwner.mockResolvedValue(true) mothershipAgentUrlMockFns.mockGetMothershipBaseURL.mockResolvedValue('https://worker.test') - mothershipGoFetchMockFns.mockFetchGo.mockImplementation(async () => + mothershipWorkerFetchMockFns.mockFetchWorker.mockImplementation(async () => Response.json({ version: 1, entrypoint: 'fixture-bootstrap' }) ) envFlagsMock.isMothershipSandboxEnabled = true diff --git a/apps/sim/lib/mothership/tools/sandbox-session.integration.ts b/apps/sim/lib/mothership/tools/sandbox-session.integration.ts index 0c45edf66b9..8c1a017a01b 100644 --- a/apps/sim/lib/mothership/tools/sandbox-session.integration.ts +++ b/apps/sim/lib/mothership/tools/sandbox-session.integration.ts @@ -14,9 +14,9 @@ import { mothershipAgentUrlMockFns, } from '@sim/testing/mocks/mothership-agent-url.mock' import { - mothershipGoFetchMock, - mothershipGoFetchMockFns, -} from '@sim/testing/mocks/mothership-go-fetch.mock' + mothershipWorkerFetchMock, + mothershipWorkerFetchMockFns, +} from '@sim/testing/mocks/mothership-worker-fetch.mock' import { generateId } from '@sim/utils/id' import { inArray } from 'drizzle-orm' import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' @@ -29,7 +29,7 @@ vi.mock('node:fs/promises', () => ({ readFile: async () => 'runtime' })) vi.mock('@/lib/mothership/transport/connection', () => ({ getSimConnection: () => ({ mode: 'direct' }), })) -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/request/headers', () => ({ mothershipRequestHeaders: () => ({}) })) vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) vi.mock('@/lib/mothership/tools/sandbox-resources', () => ({ @@ -71,7 +71,7 @@ describe('workbench entitlements for a Mothership Function execution', () => { parse({ 'workflow-code': { enabled: false, orgIds: [tenant.orgId] } }) ) mothershipAgentUrlMockFns.mockGetMothershipBaseURL.mockResolvedValue('https://worker.test') - mothershipGoFetchMockFns.mockFetchGo.mockImplementation(async () => + mothershipWorkerFetchMockFns.mockFetchWorker.mockImplementation(async () => Response.json({ version: 1, entrypoint: 'entry' }) ) const now = new Date() diff --git a/apps/sim/lib/mothership/tools/sandbox-session.test.ts b/apps/sim/lib/mothership/tools/sandbox-session.test.ts index db95e1050fe..896b3f2b5cf 100644 --- a/apps/sim/lib/mothership/tools/sandbox-session.test.ts +++ b/apps/sim/lib/mothership/tools/sandbox-session.test.ts @@ -5,9 +5,9 @@ import { mothershipAgentUrlMockFns, } from '@sim/testing/mocks/mothership-agent-url.mock' import { - mothershipGoFetchMock, - mothershipGoFetchMockFns, -} from '@sim/testing/mocks/mothership-go-fetch.mock' + mothershipWorkerFetchMock, + mothershipWorkerFetchMockFns, +} from '@sim/testing/mocks/mothership-worker-fetch.mock' import { urlsMockFns } from '@sim/testing/mocks/urls.mock' import { beforeEach, describe, expect, it, vi } from 'vitest' @@ -22,7 +22,7 @@ const { read, mint, connection, endpoint, workspaceFileCopy, entitlements } = vi vi.mock('@/lib/mothership/transport/connection', () => ({ getSimConnection: connection })) vi.mock('node:fs/promises', () => ({ readFile: read })) vi.mock('@/lib/mothership/chat/delegation', () => ({ mintDelegationToken: mint })) -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/request/headers', () => ({ mothershipRequestHeaders: () => ({ 'x-api-key': 'worker-test-key' }), })) @@ -32,7 +32,7 @@ vi.mock('@/lib/mothership/entitlements', () => ({ computeEntitlements: entitleme import { createTrustedOrganizationCopilotPrincipal } from '@/lib/mothership/auth/application-delegation' import { buildMothershipSandboxSession } from '@/lib/mothership/tools/sandbox-session' -const fetchBootstrap = mothershipGoFetchMockFns.mockFetchGo +const fetchBootstrap = mothershipWorkerFetchMockFns.mockFetchWorker const baseURL = mothershipAgentUrlMockFns.mockGetMothershipBaseURL urlsMockFns.mockGetBaseUrl.mockReturnValue('https://unused.test') diff --git a/apps/sim/lib/mothership/tools/sandbox-session.ts b/apps/sim/lib/mothership/tools/sandbox-session.ts index 165d1e395a0..98ffb6ae0d0 100644 --- a/apps/sim/lib/mothership/tools/sandbox-session.ts +++ b/apps/sim/lib/mothership/tools/sandbox-session.ts @@ -21,8 +21,8 @@ import { } from '@/lib/mothership/auth/application-delegation' import { computeEntitlements } from '@/lib/mothership/entitlements' import { WorkbenchBootstrap } from '@/lib/mothership/generated/workbench' -import { fetchGo } from '@/lib/mothership/request/go/fetch' import { mothershipRequestHeaders } from '@/lib/mothership/request/headers' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url' import { activeSandboxChatOwner, @@ -85,7 +85,7 @@ async function workbenchCli( const baseURL = activeSandboxChatOwner()?.mothershipBaseURL ?? (await getMothershipBaseURL({ userId })) const deadline = AbortSignal.timeout(15_000) - const response = await fetchGo(`${baseURL}/api/workbench/bootstrap`, { + const response = await fetchWorker(`${baseURL}/api/workbench/bootstrap`, { headers: mothershipRequestHeaders(), redirect: 'error', signal: signal ? AbortSignal.any([signal, deadline]) : deadline, diff --git a/apps/sim/lib/mothership/tools/server/files/intent-store.ts b/apps/sim/lib/mothership/tools/server/files/intent-store.ts index 7fdfdb1c833..4e733aec223 100644 --- a/apps/sim/lib/mothership/tools/server/files/intent-store.ts +++ b/apps/sim/lib/mothership/tools/server/files/intent-store.ts @@ -185,7 +185,7 @@ const INTENT_WAIT_INTERVAL_MS = 300 /** * Bounded wait for the paired prepare_file_edit to stage its intent. The model - * may batch prepare_file_edit and apply_file_edit into one round, and the Go + * may batch prepare_file_edit and apply_file_edit into one round, and the worker's * loop executes same-round tools concurrently — so apply_file_edit can reach * this executor before its prepare has run. Failing instantly turns that * ordinary race into a model-visible error, a wasted retry round, and a diff --git a/apps/sim/lib/mothership/tools/server/image/generate-image.ts b/apps/sim/lib/mothership/tools/server/image/generate-image.ts index 041d48f08ab..8dcd93eeca2 100644 --- a/apps/sim/lib/mothership/tools/server/image/generate-image.ts +++ b/apps/sim/lib/mothership/tools/server/image/generate-image.ts @@ -57,7 +57,6 @@ interface GenerateImageResult { fileName?: string vfsPath?: string downloadUrl?: string - _serviceCost?: { service: string; cost: number } } /** @@ -251,7 +250,6 @@ export const generateImageServerTool: BaseServerTool = { @@ -167,7 +166,6 @@ export const generateVideoServerTool: BaseServerTool ({ execute: vi.fn() })) -vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) +vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) vi.mock('@/lib/mothership/request/headers', () => ({ mothershipRequestHeaders: () => ({ 'x-api-key': 'worker-key' }), })) @@ -27,7 +27,7 @@ import { SIM_CONTROL_KINDS } from '@/lib/mothership/generated/sim-transport' import { receiveSimControls, startSimReceivers } from '@/lib/mothership/transport/receiver' const mocks = { - fetch: mothershipGoFetchMockFns.mockFetchGo, + fetch: mothershipWorkerFetchMockFns.mockFetchWorker, execute, sleep: utilsHelpersMockFns.mockInterruptibleSleep, } diff --git a/apps/sim/lib/mothership/transport/receiver.ts b/apps/sim/lib/mothership/transport/receiver.ts index ee341054c59..125721c8c51 100644 --- a/apps/sim/lib/mothership/transport/receiver.ts +++ b/apps/sim/lib/mothership/transport/receiver.ts @@ -14,8 +14,8 @@ import { SimControlRequestHeader, type SimControlResult, } from '@/lib/mothership/generated/sim-transport' -import { fetchGo } from '@/lib/mothership/request/go/fetch' import { mothershipRequestHeaders } from '@/lib/mothership/request/headers' +import { fetchWorker } from '@/lib/mothership/request/worker/fetch' import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url' import { getSimConnection } from '@/lib/mothership/transport/connection' import { executeSimControl } from '@/lib/mothership/transport/control' @@ -86,7 +86,7 @@ export async function receiveSimControls( while (!signal.aborted) { let phase: 'poll' | 'parse' | 'reply' = 'poll' try { - const response = await fetchGo(`${baseURL}/api/sim-transport/poll`, { + const response = await fetchWorker(`${baseURL}/api/sim-transport/poll`, { method: 'POST', headers: mothershipRequestHeaders(), body: JSON.stringify({ channelId, accepts: SIM_CONTROL_KINDS }), @@ -110,7 +110,7 @@ export async function receiveSimControls( controls.map(async (control) => { signal.throwIfAborted() const result = await control.answer() - const reply = await fetchGo(`${baseURL}/api/sim-transport/reply`, { + const reply = await fetchWorker(`${baseURL}/api/sim-transport/reply`, { method: 'POST', headers: mothershipRequestHeaders(), body: JSON.stringify({ channelId, id: control.id, result }), diff --git a/apps/sim/lib/tool-execution/application/execute-tool.ts b/apps/sim/lib/tool-execution/application/execute-tool.ts index 08116f01942..08240263e84 100644 --- a/apps/sim/lib/tool-execution/application/execute-tool.ts +++ b/apps/sim/lib/tool-execution/application/execute-tool.ts @@ -493,9 +493,10 @@ export const executeToolForCaller = defineAuthorizedWorkspaceUseCase({ * * `@/tools` computes the cost and hands it back on `output.cost.total`, but it * writes no ledger row: a workflow run bills through the execution ledger and - * Copilot bills through Go's `_serviceCost`, and this surface is neither. Its - * own doc comment says so — "any new caller of executeTool that is not Copilot - * must arrange its own metering" — and this is that arrangement. + * Copilot bills through service receipts recorded by `recordServiceCost`, and + * this surface is neither. Its own doc comment says so — "any new caller of + * executeTool that is not Copilot must arrange its own metering" — and this is + * that arrangement. * * The provider already ran and already charged Sim's key by the time this runs, * so a metering failure must not destroy the caller's result: it is logged for diff --git a/apps/sim/tools/index.test.ts b/apps/sim/tools/index.test.ts index d357c0ab5bf..7020b8c3ced 100644 --- a/apps/sim/tools/index.test.ts +++ b/apps/sim/tools/index.test.ts @@ -8738,7 +8738,7 @@ describe('Cost Field Handling', () => { Object.assign(tools, originalTools) }) - it('emits _serviceCost for copilot executions using a hosted key', async () => { + it('records a service receipt for copilot executions using a hosted key', async () => { const mockTool = { id: 'test_copilot_hosted_cost', name: 'Test Copilot Hosted Cost', @@ -8799,12 +8799,11 @@ describe('Cost Field Handling', () => { expect(result.success).toBe(true) expect(mockRateLimiterFns.acquireKey).toHaveBeenCalled() expect(result.output.cost).toEqual({ total: 0.005 }) - expect(result.output._serviceCost).toEqual({ service: 'exa', cost: 0.005 }) Object.assign(tools, originalTools) }) - it('does not emit _serviceCost for workflow executions using a hosted key', async () => { + it('records no service receipt for workflow executions using a hosted key', async () => { const mockTool = { id: 'test_workflow_hosted_cost', name: 'Test Workflow Hosted Cost', @@ -8846,17 +8845,15 @@ describe('Cost Field Handling', () => { ) as typeof fetch const mockContext = createToolExecutionContext({ userId: 'user-123' } as any) - const result = await executeTool( - 'test_workflow_hosted_cost', - {}, - { executionContext: mockContext } + const metered = vi.fn().mockResolvedValue(undefined) + const result = await observeServiceCosts(metered, () => + executeTool('test_workflow_hosted_cost', {}, { executionContext: mockContext }) ) expect(result.success).toBe(true) - // Workflow executions bill through the execution ledger; emitting - // _serviceCost here would double-bill via Go's service-charge path. + // Workflow executions bill through the execution ledger; a service receipt would double-bill. + expect(metered).not.toHaveBeenCalled() expect(result.output.cost).toEqual({ total: 0.005 }) - expect(result.output._serviceCost).toBeUndefined() Object.assign(tools, originalTools) }) diff --git a/apps/sim/tools/index.ts b/apps/sim/tools/index.ts index 7a9ab6348cc..15fbf79284e 100644 --- a/apps/sim/tools/index.ts +++ b/apps/sim/tools/index.ts @@ -1007,9 +1007,11 @@ interface HostedKeyCostResult { * Returns the cost and its metadata for the caller to attach to the tool * output. It does NOT write a usage-ledger row: the only `usageLog` insert is * `recordUsage` in `lib/billing/core/usage-log.ts`, which nothing on this path - * calls. Cost reaches the ledger only through the `_serviceCost` field - * `applyHostedKeyCostToResult` emits under `copilotToolExecution`, so any new - * caller of `executeTool` that is not Copilot must arrange its own metering. + * calls. Under `copilotToolExecution`, `applyHostedKeyCostToResult` meters it + * with `recordServiceCost` inside `withToolServiceMeter` + * (`lib/mothership/request/tools/executor.ts`), which sends a receipt to the + * worker's `/api/billing/services`. Any new caller of `executeTool` that is not + * Copilot must arrange its own metering. */ async function processHostedKeyCost( tool: ToolDefinition, @@ -1147,11 +1149,10 @@ async function scheduleExactCostSettlement( * Billing capture differs by caller: * - Workflow executions bill `output.cost.total` through trace spans and the * execution ledger (`recordUsage`), so the `cost` field alone suffices. - * - Copilot tool executions have no execution ledger. Their only billing hook - * is Go's `extractServiceCost`, which reads a top-level `_serviceCost` field - * from the tool result and charges it through the per-round update-cost - * callback (the same path the media tools use). Without it, hosted-key spend - * from copilot-dispatched integration tools is never charged. + * - Copilot tool executions have no execution ledger. Their spend is recorded + * by `recordServiceCost` inside `withToolServiceMeter` + * (`lib/mothership/request/tools/executor.ts`), which sends a receipt to the + * worker's `/api/billing/services` (the same path the media tools use). */ async function applyHostedKeyCostToResult( finalResult: ToolResponse, @@ -1197,17 +1198,15 @@ async function applyHostedKeyCostToResult( await scheduleExactCostSettlement(tool, metadata, scope, envVarName) if (hostedKeyCost > 0) { - const { copilotToolExecution } = scope - if (copilotToolExecution) await recordServiceCost(provider, hostedKeyCost) + // Copilot-only: workflow runs bill through the execution ledger, so a + // service receipt for them would bill the cost twice. + if (scope.copilotToolExecution) await recordServiceCost(provider, hostedKeyCost) finalResult.output = { ...finalResult.output, cost: { ...metadata, total: hostedKeyCost, }, - // Copilot-only: workflow runs must not emit _serviceCost or the cost - // would be billed twice (execution ledger + Go service charge). - ...(copilotToolExecution ? { _serviceCost: { service: provider, cost: hostedKeyCost } } : {}), } } } diff --git a/packages/logger/src/request-context.ts b/packages/logger/src/request-context.ts index 04cd36734da..0baaab7b26d 100644 --- a/packages/logger/src/request-context.ts +++ b/packages/logger/src/request-context.ts @@ -6,7 +6,7 @@ export interface RequestContext { path?: string /** * The 32-hex OTel trace id correlating this request across services - * (the same value the Go copilot logs as trace_id/request_id). Seeded + * (the trace id the worker's spans share). Seeded * from an incoming `traceparent` header, or stamped mid-request via * `setRequestTraceId` when the trace root is created locally. */ diff --git a/packages/testing/src/mocks/index.ts b/packages/testing/src/mocks/index.ts index 8bb5f081e68..6fe55411b82 100644 --- a/packages/testing/src/mocks/index.ts +++ b/packages/testing/src/mocks/index.ts @@ -555,10 +555,6 @@ export { mothershipEnvironmentContextMock, mothershipEnvironmentContextMockFns, } from './mothership-environment-context.mock' -export { - mothershipGoFetchMock, - mothershipGoFetchMockFns, -} from './mothership-go-fetch.mock' export { mothershipOrganizationChatsMock, mothershipOrganizationChatsMockFns, @@ -569,6 +565,10 @@ export { mothershipOtelMock, mothershipOtelMockFns, } from './mothership-otel.mock' +export { + mothershipWorkerFetchMock, + mothershipWorkerFetchMockFns, +} from './mothership-worker-fetch.mock' export { mothershipWorkspaceTargetMock, mothershipWorkspaceTargetMockFns, diff --git a/packages/testing/src/mocks/mothership-go-fetch.mock.ts b/packages/testing/src/mocks/mothership-go-fetch.mock.ts deleted file mode 100644 index 710e02ddb38..00000000000 --- a/packages/testing/src/mocks/mothership-go-fetch.mock.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { vi } from 'vitest' - -/** - * Controllable mock functions for `@/lib/mothership/request/go/fetch`. `mockFetchGo` is bare: - * set a `Response` per test. - * - * @example - * ```ts - * import { mothershipGoFetchMockFns } from '@sim/testing/mocks/mothership-go-fetch.mock' - * - * mothershipGoFetchMockFns.mockFetchGo.mockResolvedValue(new Response('{}', { status: 200 })) - * ``` - */ -export const mothershipGoFetchMockFns = { - mockFetchGo: vi.fn(), -} - -/** - * Static mock module for `@/lib/mothership/request/go/fetch`. - * - * @example - * ```ts - * vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) - * ``` - */ -export const mothershipGoFetchMock = { - fetchGo: mothershipGoFetchMockFns.mockFetchGo, -} diff --git a/packages/testing/src/mocks/mothership-otel.mock.ts b/packages/testing/src/mocks/mothership-otel.mock.ts index 65c65216122..9a92061e6b8 100644 --- a/packages/testing/src/mocks/mothership-otel.mock.ts +++ b/packages/testing/src/mocks/mothership-otel.mock.ts @@ -64,7 +64,7 @@ type SpanCallback = (span: MockOtelSpan) => unknown * Controllable mock functions for `@/lib/mothership/request/otel`. * * Defaults: - * - `mockWithCopilotSpan`, `mockWithIncomingGoSpan`, `mockWithCopilotToolSpan` invoke their callback + * - `mockWithCopilotSpan`, `mockWithIncomingWorkerSpan`, `mockWithCopilotToolSpan` invoke their callback * with a fresh {@link createMockOtelSpan} span and return its result (no span status bookkeeping). * - `mockWithCopilotOtelContext` invokes its callback with an inert context. * - `mockStartCopilotOtelRoot` returns `{ span, context, requestId, finish, setUserMessagePreview, @@ -108,7 +108,7 @@ export const mothershipOtelMockFns = { (rest[rest.length - 1] as SpanCallback)(createMockOtelSpan()) ), })), - mockWithIncomingGoSpan: vi.fn( + mockWithIncomingWorkerSpan: vi.fn( async (_headers: unknown, _spanName: string, _attributes: unknown, fn: SpanCallback) => fn(createMockOtelSpan()) ), @@ -148,7 +148,7 @@ export const mothershipOtelMock = { isActionableErrorStatus: mothershipOtelMockFns.mockIsActionableErrorStatus, markSpanForError: mothershipOtelMockFns.mockMarkSpanForError, getCopilotTracer: mothershipOtelMockFns.mockGetCopilotTracer, - withIncomingGoSpan: mothershipOtelMockFns.mockWithIncomingGoSpan, + withIncomingWorkerSpan: mothershipOtelMockFns.mockWithIncomingWorkerSpan, withCopilotSpan: mothershipOtelMockFns.mockWithCopilotSpan, withCopilotToolSpan: mothershipOtelMockFns.mockWithCopilotToolSpan, startCopilotOtelRoot: mothershipOtelMockFns.mockStartCopilotOtelRoot, diff --git a/packages/testing/src/mocks/mothership-worker-fetch.mock.ts b/packages/testing/src/mocks/mothership-worker-fetch.mock.ts new file mode 100644 index 00000000000..d33b840bd13 --- /dev/null +++ b/packages/testing/src/mocks/mothership-worker-fetch.mock.ts @@ -0,0 +1,28 @@ +import { vi } from 'vitest' + +/** + * Controllable mock functions for `@/lib/mothership/request/worker/fetch`. `mockFetchWorker` is bare: + * set a `Response` per test. + * + * @example + * ```ts + * import { mothershipWorkerFetchMockFns } from '@sim/testing/mocks/mothership-worker-fetch.mock' + * + * mothershipWorkerFetchMockFns.mockFetchWorker.mockResolvedValue(new Response('{}', { status: 200 })) + * ``` + */ +export const mothershipWorkerFetchMockFns = { + mockFetchWorker: vi.fn(), +} + +/** + * Static mock module for `@/lib/mothership/request/worker/fetch`. + * + * @example + * ```ts + * vi.mock('@/lib/mothership/request/worker/fetch', () => mothershipWorkerFetchMock) + * ``` + */ +export const mothershipWorkerFetchMock = { + fetchWorker: mothershipWorkerFetchMockFns.mockFetchWorker, +} diff --git a/packages/workflow-code/src/catalog/scaling.test.ts b/packages/workflow-code/src/catalog/scaling.test.ts index 9991cdad6c7..1fbfbc949fc 100644 --- a/packages/workflow-code/src/catalog/scaling.test.ts +++ b/packages/workflow-code/src/catalog/scaling.test.ts @@ -3,7 +3,7 @@ */ import { expectLinearGrowth } from '@sim/testing/helpers/scaling' -import { describe, expect, it } from 'vitest' +import { describe, it } from 'vitest' import { TEST_CATALOG } from '../compile/catalog.test-helpers' import { buildFieldIndex } from './surface' import type { CatalogBlock, CatalogSubBlock } from './types' diff --git a/packages/workflow-code/src/compile/scaling.test.ts b/packages/workflow-code/src/compile/scaling.test.ts index 409ec37c78f..a1412a8874c 100644 --- a/packages/workflow-code/src/compile/scaling.test.ts +++ b/packages/workflow-code/src/compile/scaling.test.ts @@ -3,7 +3,7 @@ */ import { expectLinearGrowth } from '@sim/testing/helpers/scaling' -import { describe, expect, it } from 'vitest' +import { describe, it } from 'vitest' import { stronglyConnected } from './edges' describe('compile scaling', () => { diff --git a/packages/workflow-code/src/print/scaling.test.ts b/packages/workflow-code/src/print/scaling.test.ts index 92be222febf..d32edcf6d86 100644 --- a/packages/workflow-code/src/print/scaling.test.ts +++ b/packages/workflow-code/src/print/scaling.test.ts @@ -3,7 +3,7 @@ */ import { expectLinearGrowth } from '@sim/testing/helpers/scaling' -import { describe, expect, it } from 'vitest' +import { describe, it } from 'vitest' import { compareEdges } from '../canonicalize/edges' import { compileWorkflow } from '../compile' import { TEST_CATALOG } from '../compile/catalog.test-helpers' diff --git a/scripts/check-explicit-any.baseline.json b/scripts/check-explicit-any.baseline.json index 4fdbbe06774..5a3dab11505 100644 --- a/scripts/check-explicit-any.baseline.json +++ b/scripts/check-explicit-any.baseline.json @@ -1510,7 +1510,6 @@ "apps/sim/lib/mothership/integrations/application/catalog.test.ts": 3, "apps/sim/lib/mothership/request/application/controls.ts": 1, "apps/sim/lib/mothership/request/application/recover-stream.ts": 2, - "apps/sim/lib/mothership/request/go/inline-images.ts": 1, "apps/sim/lib/mothership/request/handlers/tool.ts": 1, "apps/sim/lib/mothership/request/lifecycle/run.test.ts": 1, "apps/sim/lib/mothership/request/lifecycle/start.ts": 1, diff --git a/scripts/check-route-verbs.test.ts b/scripts/check-route-verbs.test.ts index 04cc5774061..e01ce9bc25a 100644 --- a/scripts/check-route-verbs.test.ts +++ b/scripts/check-route-verbs.test.ts @@ -13,7 +13,7 @@ describe('traced route declarations', () => { it('resolves the actual exported verb through a tracing callback', () => { const source = `${handler} export const POST = (request: NextRequest, context?: Parameters[1]) => - withIncomingGoSpan(request.headers, span, undefined, () => handler(request, context))` + withIncomingWorkerSpan(request.headers, span, undefined, () => handler(request, context))` expect(wrappedRouteSites(source)).toEqual([ { verb: 'POST', builder: 'defineInternalJsonRoute', optionsStart: source.indexOf('{') + 1 }, ]) diff --git a/scripts/check-source-conventions.baseline.json b/scripts/check-source-conventions.baseline.json index bc0c41b2690..0551b490537 100644 --- a/scripts/check-source-conventions.baseline.json +++ b/scripts/check-source-conventions.baseline.json @@ -201,7 +201,6 @@ "apps/sim/lib/mothership/chat/selection-context.test.ts": 1, "apps/sim/lib/mothership/chat/sim-key-redaction.test.ts": 1, "apps/sim/lib/mothership/chat/terminal-state.test.ts": 1, - "apps/sim/lib/mothership/request/go/inline-images.test.ts": 1, "apps/sim/lib/mothership/request/handlers/complete.ts": 2, "apps/sim/lib/mothership/request/handlers/error.ts": 2, "apps/sim/lib/mothership/request/handlers/index.ts": 9, diff --git a/scripts/check-test-types.baseline.json b/scripts/check-test-types.baseline.json index ec40781e0ec..17664850395 100644 --- a/scripts/check-test-types.baseline.json +++ b/scripts/check-test-types.baseline.json @@ -57,9 +57,6 @@ "apps/sim/app/api/billing/switch-plan/route.test.ts": { "TS2554": 1 }, - "apps/sim/app/api/billing/update-cost/replay.live.test.ts": { - "TS2554": 1 - }, "apps/sim/app/api/billing/update-cost/route.test.ts": { "TS2554": 50 }, @@ -525,7 +522,6 @@ "apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.dom.test.tsx": { "TS2304": 1, "TS2322": 4, - "TS2353": 1, "TS2550": 1 }, "apps/sim/app/workspace/[workspaceId]/knowledge/[id]/components/edit-connector-modal/use-connector-settings-form.test.tsx": { @@ -1869,9 +1865,6 @@ "apps/sim/lib/mothership/request/context/result.test.ts": { "TS2739": 1 }, - "apps/sim/lib/mothership/request/go/file-preview-adapter.test.ts": { - "TS2322": 1 - }, "apps/sim/lib/mothership/request/handlers/handlers.test.ts": { "TS2339": 1 }, @@ -1909,6 +1902,9 @@ "apps/sim/lib/mothership/request/tools/workflow-context.test.ts": { "TS2322": 1 }, + "apps/sim/lib/mothership/request/worker/file-preview-adapter.test.ts": { + "TS2322": 1 + }, "apps/sim/lib/mothership/resources/search.test.ts": { "TS2353": 1 }, diff --git a/scripts/check-unused-exports.baseline.json b/scripts/check-unused-exports.baseline.json index 0020779f67a..3b0d7476f4e 100644 --- a/scripts/check-unused-exports.baseline.json +++ b/scripts/check-unused-exports.baseline.json @@ -4672,8 +4672,6 @@ "apps/sim/lib/mothership/request/http.ts#NotificationStatus", "apps/sim/lib/mothership/request/http.ts#createRequestId", "apps/sim/lib/mothership/request/lifecycle/recovery-config.ts#DurableChatRequestSchema", - "apps/sim/lib/mothership/request/metrics.ts#recordFileRead", - "apps/sim/lib/mothership/request/metrics.ts#recordVfsMaterialize", "apps/sim/lib/mothership/request/otel.ts#isExplicitUserStopError", "apps/sim/lib/mothership/request/session/abort-reason.ts#AbortReasonValue", "apps/sim/lib/mothership/request/session/abort.ts#AbortReasonValue",