Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 120 additions & 4 deletions browser_tests/fixtures/agentConversationFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import type {
AgentWsEvent
} from '@/workbench/extensions/agent/schemas/agentApiSchema'
import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema'
import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient'
import {
DOC_PROTOCOL_VERSION,
parseServerDocFrame
} from '@/workbench/extensions/agent/crdt/docFrameClient'
import type { GraphOperation } from '@/workbench/extensions/agent/crdt/graphOperations'
import type { useAgentWorkflowTabBindingStore } from '@/workbench/extensions/agent/stores/agent/agentWorkflowTabBindingStore'

Expand Down Expand Up @@ -65,7 +68,7 @@ import { mockSavedWorkflowPersistence } from '@e2e/fixtures/utils/savedWorkflowP
import { loadSeedIntoActiveTab } from '@e2e/fixtures/utils/seedActiveTab'
import { nextFrame } from '@e2e/fixtures/utils/timing'

const THREAD_ID = 'e9a2f3d1-7c44-4b2e-9a01-5f6d8c7b3a10'
export const THREAD_ID = 'e9a2f3d1-7c44-4b2e-9a01-5f6d8c7b3a10'
// One synthetic message id per turn; the recorded ids never reach the page.
const turnId = (turn: number): string =>
`0c5b1e77-2d4a-4f9e-8b63-1a2c3d4e5${turn.toString(16).padStart(3, '0')}`
Expand Down Expand Up @@ -771,6 +774,67 @@ export class AgentConversationHarness {
return this.hostSocket.clientDocFrames()
}

async activateEmptyWorkflow(workflowId: string, name: string): Promise<void> {
const seed = structuredClone(this.conversation.workflow.seed)
seed.nodes = []
seed.links = []
seed.groups = []
this.hostSocket.addWorkflow(
workflowId,
new HostDoc(workflowId, seed, this.conversation.workflow.catalog)
)
const subscribes = this.subscribeCount(workflowId)
this.hostSocket.send({
type: 'agent_active_tab',
data: { workflow_id: workflowId, name }
})
await expect(this.topbar.getActiveTab()).toContainText(name)
await expect
.poll(() => this.subscribeCount(workflowId))
.toBeGreaterThanOrEqual(subscribes + 1)
}

resumeWorkflowSubscribe(workflowId: string): void {
this.hostSocket.resumeSubscribe(workflowId)
}

sendDocumentReset(workflowId: string): void {
this.hostSocket.send({
type: 'doc_reset',
data: {
v: DOC_PROTOCOL_VERSION,
workflow_id: workflowId,
seq: 1,
lineage_seq: 1,
actor: 'system:test-reset'
}
})
}

/** Returns to the recorded workflow's tab (the first) and holds its subscribe ack. */
async activateRecordedWorkflowBeforeSubscribeAck(): Promise<void> {
const { id, name } = this.conversation.workflow
this.hostSocket.pauseSubscribes(id)
const sent = this.clientDocFrames().filter(
(frame) => frame.type === 'doc_subscribe' && frame.workflowId === id
).length
this.hostSocket.send({
type: 'agent_active_tab',
data: { workflow_id: id, name }
})
await expect
.poll(
() =>
this.clientDocFrames().filter(
(frame) => frame.type === 'doc_subscribe' && frame.workflowId === id
).length
)
.toBeGreaterThanOrEqual(sent + 1)
await expect(
this.topbar.getTab(0).and(this.topbar.getActiveTab())
).toBeVisible()
}

/** The applier's verdict on every human op the host has judged so far. */
humanOpOutcomes(): ApplyOutcome[] {
return this.hostSocket.humanOpOutcomes()
Expand Down Expand Up @@ -838,16 +902,68 @@ export class AgentConversationHarness {
])
}

applyOpsOnHostUnsent(operations: RecordedGraphOperation[]): void {
this.host.apply(operations)
for (const id of Object.keys(this.host.graph().nodes)) this.seenIds.add(id)
}

/** Holds `threadId`'s transcript reads until `release` is called. */
async holdTranscript(
threadId: string
): Promise<{ requested: Promise<void>; release: () => void }> {
let release!: () => void
let reached!: () => void
const held = new Promise<void>((resolve) => (release = resolve))
const requested = new Promise<void>((resolve) => (reached = resolve))
await this.page.route(
`**/api/agent/threads/${threadId}/messages`,
async (route) => {
if (route.request().method() !== 'GET') return route.fallback()
reached()
await held
return route.fallback()
}
)
return { requested, release }
}

/** Serves a second workflow's document beside the recorded one. */
serveWorkflow(workflowId: string, seed: WorkflowJSON): void {
this.hostSocket.addWorkflow(
workflowId,
new HostDoc(workflowId, seed, this.conversation.workflow.catalog)
)
}

// Rises once per follower subscribe; a tab return re-subscribes and the
// host answers with the catch-up frame this counter has just sent.
subscribeCount(): number {
return this.hostSocket.subscribeCount()
subscribeCount(workflowId?: string): number {
return this.hostSocket.subscribeCount(workflowId)
}

humanOpFrames(): ClientDocFrame[] {
return this.clientDocFrames().filter((frame) => frame.type === 'doc_ops')
}

async humanAddNodeBaseVersion(
workflowId: string,
nodeId: string
): Promise<number> {
const find = () => {
for (const frame of this.humanOpFrames()) {
if (frame.workflowId !== workflowId) continue
const index = frame.ops.indexOf(`add_node:${nodeId}`)
if (index !== -1) return frame.baseVersions[index]
}
return undefined
}
await expect.poll(find).toEqual(expect.any(Number))
const baseVersion = find()
if (typeof baseVersion !== 'number')
throw new Error(`no numeric base_version for add_node:${nodeId}`)
return baseVersion
}

leaveNextHumanBatchUnanswered(): void {
this.hostSocket.leaveHumanBatchesUnanswered(1)
}
Expand Down
52 changes: 52 additions & 0 deletions browser_tests/fixtures/agentFollowerHostSocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@ describe('AgentFollowerHostSocket human doc_ops handling', () => {
})
})

it('records each op base_version, null when it is not a number', async () => {
const { hostSocket, emitClientFrame } = await installHost()

const numbered = validOp('b'.repeat(32), 2)
const missing = validOp('c'.repeat(32), 3)
const malformed = validOp('d'.repeat(32), 4)
delete missing.base_version
malformed.base_version = 'not-a-number'
emitClientFrame({
type: 'doc_ops',
data: {
workflow_id: WORKFLOW_ID,
tab: 'tab-1',
ops: [numbered, missing, malformed]
}
})

expect(hostSocket.clientDocFrames().at(-1)?.baseVersions).toEqual([
1,
null,
null
])
})

it('still applies a well-formed doc_ops batch through the real applier', async () => {
const { hostSocket, emitClientFrame, sentFrames } = await installHost()

Expand Down Expand Up @@ -175,6 +199,34 @@ describe('AgentFollowerHostSocket human doc_ops handling', () => {
node: { id: nodeId, type: 'TestNode', pos: [10, 20] }
})

it('releases a held batch against the workflow that sent it', async () => {
const {
host: primaryHost,
hostSocket,
emitClientFrame
} = await installHost('hold')
const secondaryWorkflowId = 'wf-2'
const secondaryHost = new HostDoc(
secondaryWorkflowId,
{ nodes: [], links: [] },
{ types: {} }
)
hostSocket.addWorkflow(secondaryWorkflowId, secondaryHost)

emitClientFrame({
type: 'doc_ops',
data: {
workflow_id: secondaryWorkflowId,
tab: 'tab-1',
ops: [validOp('c'.repeat(32), 3)]
}
})
hostSocket.releaseHeldClientOps()

expect(primaryHost.graph().nodes).toEqual({})
expect(secondaryHost.graph().nodes).toHaveProperty('3')
})

it.for([
{
label: 'a missing tab',
Expand Down
Loading
Loading