Skip to content

[feat] agent: persist and display per-turn duration (#468) - #476

Open
yefuyou wants to merge 14 commits into
ThinkFlowLab:mainfrom
yefuyou:feat/468-turn-timing
Open

yefuyou wants to merge 14 commits into
ThinkFlowLab:mainfrom
yefuyou:feat/468-turn-timing

Conversation

@yefuyou

@yefuyou yefuyou commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refs #468 (Slice A: per-turn timing only).

Persist authoritative timing metadata for each accepted Agent turn and show the same recorded duration in Chat and the existing conversation-wide Trace page. This is a small, reviewable first slice of the DSH-inspired timing work requested by @FayeSpica; the visual timing overview (Slice B) remains follow-up work.

  • Persist optional startedAt and finishedAt (ISO 8601 UTC) and durationMs (non-negative integer) on the assistant reply.
  • Capture the start after successful admission inside the conversation begin transaction; measure elapsed time with the server's monotonic clock and persist at finish. Finish updates preserve the recorded start.
  • Expose nullable fields in the existing read-only conversation Trace projection; display the persisted result in Chat and per-turn Trace headers with shared formatting and en-US/zh-CN translations.
  • Support complete, stopped, interrupted, legacy missing timestamps and post-restart unknown durations without inventing elapsed time.
  • No new timing database, provider requests, replay controls or changes to asynchronous generation Job semantics.

Screenshots

Captured from Chromium with fixture-backed demo data. Chat and Trace show the same persisted turn duration in both UI locales. The sample turn contains no model/tool trace entries, so the Trace detail area correctly displays the empty-trace message.

English UI

Chat showing completed turn duration

Trace showing the same completed turn duration

简体中文 UI

聊天界面显示已完成的轮次耗时

轨迹界面显示相同的轮次耗时

Timing semantics and limitations

  • Duration is turn admission → terminal reply, not completion of linked generation Jobs, and excludes the begin/finish disk writes.
  • durationMs uses an in-process monotonic measurement. A restart/multi-process handoff may leave only startedAt with an unknown duration; historical turns are not backfilled.
  • For comparison with [perf] agent: record where a turn spends its time (#449) #469: that PR measures tool/CLI breakdown and reports turnSeconds under agent-runs/timing/, with a different boundary. This PR records user-visible turn duration in conversation history. Both can coexist, but their values should not be presented as identical.
  • On a failed finish persistence write, a durable duration is not guaranteed.

Validation

  • Python targeted regressions: six related test files, 109 passed. An expanded run reported 154 passed, 1 skipped.
  • Broader pytest comparison: the full suite was not green in the sandbox (213 failed, 130 errors). A clean-upstream-main comparison found no failure unique to this branch; one test_creation_storage.py in-place case failed only on the clean-main run and was treated as flaky.
  • Frontend/static checks: npm test, TypeScript, production build, i18n, browser manifest and locale checks passed.
  • Browser regressions: assistant_turn_timing.mjs passed; assistant_sessions.mjs passed three consecutive runs on this branch; trace_original_export.mjs passed, preserving [feat] agent: download original conversation traces as a ZIP archive (#421) #422's ZIP export.
  • GitHub Actions on current HEAD 0f55fa0608d776422510f4de30ce65a185dccd16: all seven Release checks passed; DSH plugin checks and CodeQL also succeeded.
  • Independent review of the integrated branch: APPROVED.
  • No paid model/provider calls.

Current PR HEAD: 0f55fa0608d776422510f4de30ce65a185dccd16.

Parallel development / merge notes

Out of scope

Slice B DSH-inspired visual overview, trace export, TTFA/TTC benchmarking, new tool-level instrumentation, Job durations, performance optimization, and execution semantics changes.

Review focus

Please review the admission/terminal timing boundaries, cancellation and restart cases, compatibility with #469 and in-flight conversation lifecycle PRs, and the presentation of stopped/interrupted replies.

The PR is currently Ready for Review; CI has passed on the current head.

…hinkFlowLab#468)

## summary
- Conversations.update stamps startedAt on the assistant placeholder at acceptance and finishedAt/durationMs (monotonic) at finish entry, without touching produce()/turn().
- Conversations.trace() returns startedAt/finishedAt/durationMs per turn, null when unknown.
- Tests cover complete, stop, runtime failure, rejected turns, archive, legacy data, and restart.
@yefuyou
yefuyou marked this pull request as ready for review November 23, 2026 13:26
@github-actions github-actions Bot added area:agent Agent runtime, assistant chat and tools type:feat New capability or behaviour labels Nov 23, 2026
@github-actions
github-actions Bot requested review from FayeSpica and wuzhf9 November 23, 2026 13:26
# Conflicts:
#	tests/test_assistant_conversation_trace.py
Comment thread apps/api/src/opendirector_api/agent.py Outdated
'role': 'assistant', 'text': '', 'status': 'streaming',
reply_id = str(uuid5(UUID(begin['id']), 'assistant'))
with self._started_lock:
self._started[reply_id] = _monotonic()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timer starts before the initial conversation write. With a simulated 2 s write delay, a 250 ms turn was recorded as 2.25 s. Please start timing after the write succeeds and add a regression test with a delayed write.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the precise reproduction, @FayeSpica. Fixed in f92e430e.

The monotonic timer now starts after the initial durable_json() write succeeds, while the finish boundary remains before finish persistence. I added controlled-clock regression cases with 0 ms, 200 ms, and 2,000 ms initial write delays; each records 250 ms of turn time. A separate delayed finish write is also excluded. Tests cover write failure, complete/stopped/interrupted, restart, duplicate begin, and concurrent turns. The English/Chinese timing docs now distinguish persisted startedAt (acceptance timestamp) from the post-write monotonic timing start.

The branch is updated, and all 13/13 GitHub checks pass on f92e430e. Would appreciate another look when you have a chance. Thanks again!

durationMs no longer includes initial conversation persistence latency.
Keep the start when a finish write fails so a retry still records a duration.
Add delayed-write, failure, duplicate-begin, restart and concurrency tests.
# Conflicts:
#	apps/api/src/opendirector_api/agent.py
#	src/canvas/AssistantTracePage.tsx

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent runtime, assistant chat and tools area:i18n Localisation and bilingual UI type:feat New capability or behaviour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants