style(dify-builder): format trace files and satisfy noUncheckedIndexedAccess in specs

The Task 1-3 implementers ran vp test but not vp check, so the trace
modules/specs shipped with formatter drift and TS2532 (possibly-undefined
index access) errors that vp test does not catch. Apply the formatter and
use optional chaining on array-index access in the specs, matching the
existing spec convention (calls[0]?.[0]).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Charles YaoandClaude Opus 4.8 committed 2026-09-04 06:59:45 -07:00
1 parent 829bc277e0
commit d9b775b193
4 files changed
+72 -20

No files matched your search

@@ -4,12 +4,23 @@ describe('createTraceBuffer', () => {
it('stamps a monotonic seq (starting at 1) and an ISO timestamp on each append', () => {
const buffer = createTraceBuffer()
buffer.append({ dir: 'out', kind: 'action', payload: { a: 1 } })
buffer.append({ dir: 'in', kind: 'progress', payload: { b: 2 }, version: 3, state: 'build.execution' })
buffer.append({
dir: 'in',
kind: 'progress',
payload: { b: 2 },
version: 3,
state: 'build.execution',
})
const { entries, truncated } = buffer.snapshot()
expect(entries.map((e) => e.seq)).toEqual([1, 2])
expect(entries[0].dir).toBe('out')
expect(entries[1]).toMatchObject({ dir: 'in', kind: 'progress', version: 3, state: 'build.execution' })
expect(entries[1].ts).toMatch(/^\d{4}-\d{2}-\d{2}T.*Z$/)
expect(entries[0]?.dir).toBe('out')
expect(entries[1]).toMatchObject({
dir: 'in',
kind: 'progress',
version: 3,
state: 'build.execution',
})
expect(entries[1]?.ts).toMatch(/^\d{4}-\d{2}-\d{2}T.*Z$/)
expect(truncated).toBe(false)
})
@@ -39,7 +50,7 @@ describe('createTraceBuffer', () => {
buffer.clear()
expect(buffer.snapshot()).toEqual({ entries: [], truncated: false })
buffer.append({ dir: 'in', kind: 'c', payload: {} })
expect(buffer.snapshot().entries[0].seq).toBe(1)
expect(buffer.snapshot().entries[0]?.seq).toBe(1)
})
it('readTraceVersion prefers version, falls back to at_version, else undefined', () => {
@@ -1,27 +1,51 @@
import { buildTraceExport, serializeTraceExport } from '../trace-export'
import type { TraceSnapshot } from '../trace-buffer'
import type { SessionView } from '../../types'
import type { TraceSnapshot } from '../trace-buffer'
import { buildTraceExport, serializeTraceExport } from '../trace-export'
const snapshot: TraceSnapshot = {
entries: [
{ seq: 1, ts: '2026-09-04T00:00:00.000Z', dir: 'out', kind: 'action', payload: { action_id: 'approve_plan' } },
{ seq: 2, ts: '2026-09-04T00:00:01.000Z', dir: 'in', kind: 'state', payload: { state: 'build.plan_approval' } },
{
seq: 1,
ts: '2026-09-04T00:00:00.000Z',
dir: 'out',
kind: 'action',
payload: { action_id: 'approve_plan' },
},
{
seq: 2,
ts: '2026-09-04T00:00:01.000Z',
dir: 'in',
kind: 'state',
payload: { state: 'build.plan_approval' },
},
],
truncated: true,
}
const view = {
session_id: 's1', app_id: 'app-1', version: 7, state: 'build.plan_approval',
entry_mode: 'build', run_status: 'waiting_input', canvas_read_only: false,
interrupted: false, conversation_last_seq: 2, model: { provider: 'anthropic', name: 'claude' },
session_id: 's1',
app_id: 'app-1',
version: 7,
state: 'build.plan_approval',
entry_mode: 'build',
run_status: 'waiting_input',
canvas_read_only: false,
interrupted: false,
conversation_last_seq: 2,
model: { provider: 'anthropic', name: 'claude' },
} as unknown as SessionView
describe('buildTraceExport', () => {
it('assembles meta from the view and counts entries', () => {
const result = buildTraceExport(snapshot, view)
expect(result.meta).toMatchObject({
session_id: 's1', app_id: 'app-1', entry_mode: 'build', state: 'build.plan_approval',
version: 7, entry_count: 2, truncated: true,
session_id: 's1',
app_id: 'app-1',
entry_mode: 'build',
state: 'build.plan_approval',
version: 7,
entry_count: 2,
truncated: true,
})
expect(result.meta.captured_at).toMatch(/^\d{4}-\d{2}-\d{2}T.*Z$/)
expect(result.trace).toHaveLength(2)
@@ -29,7 +53,15 @@ describe('buildTraceExport', () => {
it('produces safe defaults when there is no view', () => {
const result = buildTraceExport({ entries: [], truncated: false }, null)
expect(result.meta).toMatchObject({ session_id: '', app_id: '', state: '', version: 0, entry_count: 0, truncated: false, model: null })
expect(result.meta).toMatchObject({
session_id: '',
app_id: '',
state: '',
version: 0,
entry_count: 0,
truncated: false,
model: null,
})
})
})
@@ -44,7 +76,10 @@ describe('serializeTraceExport', () => {
it('does not throw on a circular payload', () => {
const circular: Record<string, unknown> = {}
circular.self = circular
const snap: TraceSnapshot = { entries: [{ seq: 1, ts: 't', dir: 'in', kind: 'node', payload: circular }], truncated: false }
const snap: TraceSnapshot = {
entries: [{ seq: 1, ts: 't', dir: 'in', kind: 'node', payload: circular }],
truncated: false,
}
const json = serializeTraceExport(buildTraceExport(snap, view))
expect(json).toContain('[Circular]')
})
@@ -151,8 +151,10 @@ describe('useDifyBuilderSessionController trace capture', () => {
const { entries } = result.current.getTrace()
expect(entries).toHaveLength(2)
expect(entries[0]).toMatchObject({ dir: 'out', kind: 'session_start', seq: 1 })
expect(entries[0].payload).toMatchObject({ goal_text: 'second goal' })
expect(entries.some((entry) => JSON.stringify(entry.payload).includes('first goal'))).toBe(false)
expect(entries[0]?.payload).toMatchObject({ goal_text: 'second goal' })
expect(entries.some((entry) => JSON.stringify(entry.payload).includes('first goal'))).toBe(
false,
)
expect(entries.some((entry) => JSON.stringify(entry.payload).includes('session-1'))).toBe(false)
})
})
@@ -16,7 +16,10 @@ export type TraceExport = {
trace: TraceEntry[]
}
export const buildTraceExport = (snapshot: TraceSnapshot, view: SessionView | null): TraceExport => ({
export const buildTraceExport = (
snapshot: TraceSnapshot,
view: SessionView | null,
): TraceExport => ({
meta: {
session_id: view?.session_id ?? '',
app_id: view?.app_id ?? '',
@@ -40,7 +43,8 @@ export const serializeTraceExport = (data: TraceExport): string => {
data,
(_key, value) => {
if (typeof value === 'bigint') return value.toString()
if (typeof value === 'function') return `[Function ${(value as { name?: string }).name || 'anonymous'}]`
if (typeof value === 'function')
return `[Function ${(value as { name?: string }).name || 'anonymous'}]`
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return '[Circular]'
seen.add(value)