chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { toError } from '@sim/utils/errors'
|
||||
import {
|
||||
ASYNC_TOOL_STATUS,
|
||||
type AsyncCompletionEnvelope,
|
||||
type AsyncConfirmationState,
|
||||
isAsyncEphemeralConfirmationStatus,
|
||||
} from '@/lib/copilot/async-runs/lifecycle'
|
||||
import { getAsyncToolCalls } from '@/lib/copilot/async-runs/repository'
|
||||
import { MothershipStreamV1ToolOutcome } from '@/lib/copilot/generated/mothership-stream-v1'
|
||||
import { getRedisClient } from '@/lib/core/config/redis'
|
||||
import { createPubSubChannel, type PubSubChannel } from '@/lib/events/pubsub'
|
||||
|
||||
const logger = createLogger('CopilotOrchestratorPersistence')
|
||||
const TOOL_CONFIRMATION_TTL_SECONDS = 60 * 10
|
||||
const toolConfirmationKey = (toolCallId: string) => `copilot:tool-confirmation:${toolCallId}`
|
||||
|
||||
type ToolConfirmGlobal = typeof globalThis & {
|
||||
_toolConfirmationChannel?: PubSubChannel<AsyncCompletionEnvelope>
|
||||
}
|
||||
|
||||
const _g = globalThis as ToolConfirmGlobal
|
||||
if (!_g._toolConfirmationChannel) {
|
||||
_g._toolConfirmationChannel = createPubSubChannel<AsyncCompletionEnvelope>({
|
||||
channel: 'copilot:tool-confirmation',
|
||||
label: 'CopilotToolConfirmation',
|
||||
})
|
||||
}
|
||||
const toolConfirmationChannel = _g._toolConfirmationChannel
|
||||
|
||||
/**
|
||||
* Get a tool call confirmation state from the durable async tool row.
|
||||
*
|
||||
* The durable row reconstructs only the live execution lifecycle plus the
|
||||
* `background` detach signal.
|
||||
*/
|
||||
export async function getToolConfirmation(
|
||||
toolCallId: string
|
||||
): Promise<AsyncConfirmationState | null> {
|
||||
const [row] = await getAsyncToolCalls([toolCallId]).catch((err) => {
|
||||
logger.warn('Failed to fetch async tool calls', {
|
||||
toolCallId,
|
||||
error: toError(err).message,
|
||||
})
|
||||
return []
|
||||
})
|
||||
if (!row) return null
|
||||
if (row.status === ASYNC_TOOL_STATUS.delivered) {
|
||||
logger.warn('Delivered async tool rows are outside request confirmation flow', {
|
||||
toolCallId,
|
||||
})
|
||||
return null
|
||||
}
|
||||
return {
|
||||
status:
|
||||
row.status === ASYNC_TOOL_STATUS.completed
|
||||
? MothershipStreamV1ToolOutcome.success
|
||||
: row.status === ASYNC_TOOL_STATUS.failed
|
||||
? MothershipStreamV1ToolOutcome.error
|
||||
: row.status === ASYNC_TOOL_STATUS.cancelled
|
||||
? MothershipStreamV1ToolOutcome.cancelled
|
||||
: row.status,
|
||||
message: row.error || undefined,
|
||||
...(row.result !== undefined ? { data: row.result } : {}),
|
||||
timestamp: row.updatedAt?.toISOString?.(),
|
||||
}
|
||||
}
|
||||
|
||||
export function publishToolConfirmation(event: AsyncCompletionEnvelope): void {
|
||||
logger.info('Publishing tool confirmation event', {
|
||||
toolCallId: event.toolCallId,
|
||||
status: event.status,
|
||||
})
|
||||
const redis = getRedisClient()
|
||||
if (redis) {
|
||||
void redis
|
||||
.set(
|
||||
toolConfirmationKey(event.toolCallId),
|
||||
JSON.stringify(event),
|
||||
'EX',
|
||||
TOOL_CONFIRMATION_TTL_SECONDS
|
||||
)
|
||||
.then(() => {
|
||||
logger.info('Persisted tool confirmation in Redis', {
|
||||
toolCallId: event.toolCallId,
|
||||
status: event.status,
|
||||
redisKey: toolConfirmationKey(event.toolCallId),
|
||||
})
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.warn('Failed to persist tool confirmation in Redis', {
|
||||
toolCallId: event.toolCallId,
|
||||
error: toError(error).message,
|
||||
})
|
||||
})
|
||||
} else {
|
||||
logger.warn('Redis unavailable while publishing tool confirmation', {
|
||||
toolCallId: event.toolCallId,
|
||||
status: event.status,
|
||||
})
|
||||
}
|
||||
toolConfirmationChannel.publish(event)
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for an async tool confirmation update.
|
||||
*
|
||||
* `background` still arrives as a request-local detach signal, so it is checked
|
||||
* from pubsub before falling back to the durable async tool row.
|
||||
*/
|
||||
export async function waitForToolConfirmation(
|
||||
toolCallId: string,
|
||||
timeoutMs: number,
|
||||
abortSignal?: AbortSignal,
|
||||
options: {
|
||||
acceptStatus?: (status: AsyncConfirmationState['status']) => boolean
|
||||
} = {}
|
||||
): Promise<AsyncConfirmationState | null> {
|
||||
const acceptStatus = options.acceptStatus ?? (() => true)
|
||||
return new Promise((resolve) => {
|
||||
let settled = false
|
||||
let timeoutId: ReturnType<typeof setTimeout> | null = null
|
||||
let unsubscribe: (() => void) | null = null
|
||||
|
||||
const cleanup = () => {
|
||||
if (timeoutId) clearTimeout(timeoutId)
|
||||
if (unsubscribe) unsubscribe()
|
||||
abortSignal?.removeEventListener('abort', onAbort)
|
||||
}
|
||||
|
||||
const settle = (value: AsyncConfirmationState | null) => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
cleanup()
|
||||
resolve(value)
|
||||
}
|
||||
|
||||
const onAbort = () => settle(null)
|
||||
|
||||
unsubscribe = toolConfirmationChannel.subscribe((event) => {
|
||||
if (event.toolCallId !== toolCallId) return
|
||||
if (isAsyncEphemeralConfirmationStatus(event.status) && acceptStatus(event.status)) {
|
||||
settle({
|
||||
status: event.status,
|
||||
...(event.message !== undefined ? { message: event.message } : {}),
|
||||
...(event.data !== undefined ? { data: event.data } : {}),
|
||||
...(event.timestamp !== undefined ? { timestamp: event.timestamp } : {}),
|
||||
})
|
||||
return
|
||||
}
|
||||
void getToolConfirmation(toolCallId).then((latest) => {
|
||||
if (!latest || !acceptStatus(latest.status)) return
|
||||
logger.info('Resolved tool confirmation from pubsub', {
|
||||
toolCallId,
|
||||
status: latest.status,
|
||||
})
|
||||
settle(latest)
|
||||
})
|
||||
})
|
||||
|
||||
timeoutId = setTimeout(() => settle(null), timeoutMs)
|
||||
if (abortSignal?.aborted) {
|
||||
settle(null)
|
||||
return
|
||||
}
|
||||
abortSignal?.addEventListener('abort', onAbort, { once: true })
|
||||
|
||||
void getToolConfirmation(toolCallId).then((latest) => {
|
||||
if (latest && acceptStatus(latest.status)) {
|
||||
logger.info('Resolved tool confirmation after subscribe', {
|
||||
toolCallId,
|
||||
status: latest.status,
|
||||
})
|
||||
settle(latest)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { getAsyncToolCalls } = vi.hoisted(() => ({
|
||||
getAsyncToolCalls: vi.fn(),
|
||||
}))
|
||||
|
||||
const channelHandlers = new Set<(event: any) => void>()
|
||||
|
||||
vi.mock('@/lib/copilot/async-runs/repository', () => ({
|
||||
getAsyncToolCalls,
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/events/pubsub', () => ({
|
||||
createPubSubChannel: () => ({
|
||||
publish(event: any) {
|
||||
for (const handler of channelHandlers) handler(event)
|
||||
},
|
||||
subscribe(handler: (event: any) => void) {
|
||||
channelHandlers.add(handler)
|
||||
return () => {
|
||||
channelHandlers.delete(handler)
|
||||
}
|
||||
},
|
||||
dispose() {},
|
||||
}),
|
||||
}))
|
||||
|
||||
import {
|
||||
getToolConfirmation,
|
||||
publishToolConfirmation,
|
||||
waitForToolConfirmation,
|
||||
} from '@/lib/copilot/persistence/tool-confirm'
|
||||
|
||||
describe('copilot orchestrator persistence', () => {
|
||||
let row: {
|
||||
status: string
|
||||
error?: string | null
|
||||
result?: unknown
|
||||
updatedAt: Date
|
||||
} | null
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
channelHandlers.clear()
|
||||
row = null
|
||||
getAsyncToolCalls.mockImplementation(async () => (row ? [row] : []))
|
||||
})
|
||||
|
||||
it('reads the durable DB row as the source of truth', async () => {
|
||||
row = {
|
||||
status: 'completed',
|
||||
result: { ok: true },
|
||||
error: null,
|
||||
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
|
||||
}
|
||||
|
||||
await expect(getToolConfirmation('tool-1')).resolves.toEqual({
|
||||
status: 'success',
|
||||
message: undefined,
|
||||
data: { ok: true },
|
||||
timestamp: '2026-01-01T00:00:00.000Z',
|
||||
})
|
||||
})
|
||||
|
||||
it('preserves primitive durable results in confirmations', async () => {
|
||||
row = {
|
||||
status: 'completed',
|
||||
result: 'done',
|
||||
error: null,
|
||||
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
|
||||
}
|
||||
|
||||
await expect(getToolConfirmation('tool-1')).resolves.toEqual({
|
||||
status: 'success',
|
||||
message: undefined,
|
||||
data: 'done',
|
||||
timestamp: '2026-01-01T00:00:00.000Z',
|
||||
})
|
||||
})
|
||||
|
||||
it('ignores delivered rows in request confirmation flow', async () => {
|
||||
row = {
|
||||
status: 'delivered',
|
||||
result: { ok: true },
|
||||
error: null,
|
||||
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
|
||||
}
|
||||
|
||||
await expect(getToolConfirmation('tool-1')).resolves.toBeNull()
|
||||
})
|
||||
|
||||
it('ignores background when waiting for a foreground terminal status', async () => {
|
||||
row = {
|
||||
status: 'pending',
|
||||
error: null,
|
||||
result: null,
|
||||
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
|
||||
}
|
||||
|
||||
const waitPromise = waitForToolConfirmation('tool-1', 5_000, undefined, {
|
||||
acceptStatus: (status) =>
|
||||
status === 'success' || status === 'error' || status === 'cancelled',
|
||||
})
|
||||
|
||||
publishToolConfirmation({
|
||||
toolCallId: 'tool-1',
|
||||
status: 'background',
|
||||
message: 'Client disconnected, execution continuing server-side',
|
||||
timestamp: '2026-01-01T00:00:01.000Z',
|
||||
})
|
||||
|
||||
await Promise.resolve()
|
||||
|
||||
row = {
|
||||
status: 'completed',
|
||||
error: null,
|
||||
result: { ok: true },
|
||||
updatedAt: new Date('2026-01-01T00:00:02.000Z'),
|
||||
}
|
||||
|
||||
publishToolConfirmation({
|
||||
toolCallId: 'tool-1',
|
||||
status: 'success',
|
||||
timestamp: '2026-01-01T00:00:02.000Z',
|
||||
})
|
||||
|
||||
await expect(waitPromise).resolves.toEqual({
|
||||
status: 'success',
|
||||
message: undefined,
|
||||
data: { ok: true },
|
||||
timestamp: '2026-01-01T00:00:02.000Z',
|
||||
})
|
||||
})
|
||||
|
||||
it('resolves background from the pubsub event when the durable row stays pending', async () => {
|
||||
row = {
|
||||
status: 'pending',
|
||||
error: null,
|
||||
result: null,
|
||||
updatedAt: new Date('2026-01-01T00:00:00.000Z'),
|
||||
}
|
||||
|
||||
const waitPromise = waitForToolConfirmation('tool-1', 5_000, undefined, {
|
||||
acceptStatus: (status) => status === 'background',
|
||||
})
|
||||
|
||||
await Promise.resolve()
|
||||
|
||||
publishToolConfirmation({
|
||||
toolCallId: 'tool-1',
|
||||
status: 'background',
|
||||
message: 'Client disconnected, execution continuing server-side',
|
||||
timestamp: '2026-01-01T00:00:01.000Z',
|
||||
})
|
||||
|
||||
await expect(waitPromise).resolves.toEqual({
|
||||
status: 'background',
|
||||
message: 'Client disconnected, execution continuing server-side',
|
||||
timestamp: '2026-01-01T00:00:01.000Z',
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user