fix: preserve span input/metadata, auto-expand subspans, force-arm poll

Three fixes in response to user feedback:

1. Span input disappearing on End/Fail
The Upsert's DoUpdates always listed input/output/metadata, so calls
that only set output (EndSpan) or only set error_* (FailSpan) wrote
NULL into input/metadata, clobbering whatever Begin had recorded.
Build the column list dynamically: skip input/output/metadata when
the incoming row's value is nil. nil now means "preserve existing"
(matches user's intuition "Begin recorded it, End shouldn't erase it").

2. Subspans not auto-expanded
Stages with children (multimodal.image[*], postprocess.summary,
postprocess.question, postprocess.graph.chunk[*]) required a click
on the ▸ caret to surface — easy to miss. On the FIRST successful
fetch per (knowledgeId × attempt), auto-expand any stage that has
children. Subsequent polls honor whatever the user has collapsed,
so manual collapse mid-parse stays collapsed.

3. Auto-poll still not firing
Force-arm the polling interval in onMounted regardless of state.
The per-tick callback decides whether to actually fetch based on
current parse_status — so the loop can never get stranded waiting
on a status transition that already happened. Added a console.debug
when the interval arms, so we can verify from DevTools console that
polling is actually running.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
wizardchen
2026-05-27 23:20:28 +08:00
committed by lyingbug
parent baa4e75d4a
commit 2a4d6f9019
2 changed files with 68 additions and 14 deletions
@@ -66,6 +66,11 @@ const expandedJsonKeys = ref<Set<string>>(new Set())
const nowTick = ref(Date.now())
const detailTab = ref<'overview' | 'input' | 'output' | 'metadata' | 'raw'>('overview')
const lastFetchedAt = ref<number>(0)
// Tracks whether we've completed our FIRST successful fetch for the
// current knowledgeId. After that, the user owns expandedRows — we
// don't re-auto-expand on every poll (would un-collapse rows the user
// intentionally collapsed mid-parse).
let firstLoadDone = false
// Tracks consecutive fetch failures so the "更新于" caption can surface
// staleness. When the parse_status is mid-flight but every fetch is
// hitting an error, the loop keeps going silently — without this
@@ -144,6 +149,7 @@ function ensurePolling() {
if (unmounted) return
if (props.autoPoll === false) return
if (pollTimer) return
console.debug('[KnowledgeTimeline] arming poll interval', { knowledgeId: props.knowledgeId, every: POLL_INTERVAL_MS })
pollTimer = setInterval(() => {
// Guard against overlapping fetches AND state changes that have
// since flipped status out of polling. Keeps the loop conservative.
@@ -153,7 +159,14 @@ function ensurePolling() {
}
if (fetchInFlight) return
const status = data.value?.parse_status
if (!status || !isPolling(status)) {
if (!status) {
// First fetch hasn't completed yet — give it another tick to
// settle. Common when the drawer opens against a slow network.
return
}
if (!isPolling(status)) {
// Terminal state: stop the interval. The watcher will re-arm
// it if status flips back to polling (e.g. user triggers reparse).
clearTimer()
return
}
@@ -176,6 +189,21 @@ async function fetchSpans(opts: { manual?: boolean } = {}) {
if (selectedAttempt.value === undefined) {
selectedAttempt.value = data.value.attempt
}
// First-load auto-expand: surface any stage that already has
// subspans (multimodal.image[i], postprocess.summary, etc.) so
// users don't have to click ▸ on every stage to discover them.
// Only runs once per (knowledgeId × attempt) — subsequent polls
// honor whatever the user has collapsed.
if (!firstLoadDone) {
const expanded = new Set<string>(['__root__'])
for (const stage of data.value.trace?.children || []) {
if ((stage.children || []).length > 0) {
expanded.add(stage.span_id || `stage:${stage.name}`)
}
}
expandedRows.value = expanded
firstLoadDone = true
}
const traceStatus = data.value.trace?.status || data.value.parse_status || 'running'
attemptStatuses.set(data.value.attempt, traceStatus)
ensureAttemptStatuses()
@@ -286,6 +314,8 @@ function onAttemptChange(n: number) {
if (Number.isNaN(n)) return
selectedAttempt.value = n
selectedSpanId.value = null
// Re-trigger auto-expand on the new attempt's stages.
firstLoadDone = false
fetchSpans()
}
@@ -297,6 +327,7 @@ watch(
expandedRows.value = new Set(['__root__'])
selectedSpanId.value = null
attemptStatuses.clear()
firstLoadDone = false
fetchSpans()
},
)
@@ -328,6 +359,13 @@ function onKeydown(ev: KeyboardEvent) {
onMounted(() => {
fetchSpans()
// Arm the polling interval unconditionally — the per-tick callback
// decides whether to actually fetch based on current parse_status.
// This way the loop can never get stranded waiting on a status
// transition that already happened. The callback self-clears when
// status reaches a terminal state, and the watcher re-arms if it
// flips back to polling.
ensurePolling()
// Tick at 1Hz; running bars and "next refresh" countdown depend on it.
nowTimer = setInterval(() => {
nowTick.value = Date.now()
@@ -52,25 +52,41 @@ func (r *knowledgeSpanRepository) Upsert(ctx context.Context, row *types.Knowled
// transition between calls — name/kind/parent are immutable once
// set so we don't list them in DoUpdates (saves a few bytes per
// write, and any mismatch indicates a programming error).
//
// CRITICAL: input / output / metadata are CONTENT fields that
// individual call sites only fill when they have something to set.
// EndSpan e.g. only sets `output`; if we always listed `input` in
// DoUpdates, the End call would clobber the input set by Begin with
// NULL. Same for metadata. Build the DoUpdates list dynamically and
// skip these three columns when the incoming row has nothing to
// write — so "no value" preserves the existing column instead of
// nuking it.
cols := []string{
"status",
"error_code",
"error_message",
"error_detail",
"started_at",
"finished_at",
"duration_ms",
"updated_at",
}
if row.Input != nil {
cols = append(cols, "input")
}
if row.Output != nil {
cols = append(cols, "output")
}
if row.Metadata != nil {
cols = append(cols, "metadata")
}
return r.db.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{
{Name: "knowledge_id"},
{Name: "attempt"},
{Name: "span_id"},
},
DoUpdates: clause.AssignmentColumns([]string{
"status",
"input",
"output",
"metadata",
"error_code",
"error_message",
"error_detail",
"started_at",
"finished_at",
"duration_ms",
"updated_at",
}),
DoUpdates: clause.AssignmentColumns(cols),
}).Create(row).Error
}