Skip to content

fix(loop): enrich trace entries with call_id for tool-call correlation#168

Closed
zwrong wants to merge 2 commits into
HKUDS:mainfrom
zwrong:feature/trace-enrich
Closed

fix(loop): enrich trace entries with call_id for tool-call correlation#168
zwrong wants to merge 2 commits into
HKUDS:mainfrom
zwrong:feature/trace-enrich

Conversation

@zwrong

@zwrong zwrong commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add call_id to tool_call and tool_result trace entries, enabling clear correlation between tool calls and their results in trace.jsonl via call_id
  • Remove 200-char arg truncation in trace writes for better post-hoc debugging

Why

When replaying agent sessions from trace.jsonl, there is currently no way to correlate a tool_call entry with its corresponding tool_result. The existing iter field is not sufficient because _execute_parallel runs multiple tool calls within the same iteration:

iteration=3:
  tool_call   { iter: 3, tool: "web_search", call_id: "call_abc" }
  tool_call   { iter: 3, tool: "read_file",  call_id: "call_def" }
  tool_call   { iter: 3, tool: "web_search", call_id: "call_ghi" }   ← same iter, same tool
  tool_result { iter: 3, tool: "read_file",  call_id: "call_def" }
  tool_result { iter: 3, tool: "web_search", call_id: "call_abc" }
  tool_result { iter: 3, tool: "web_search", call_id: "call_ghi" }

In this scenario, iter alone (or even iter + tool) cannot distinguish the two web_search calls. call_id (the LLM-assigned tool-call identifier) provides a 1:1 join key for request ↔ response correlation regardless of parallelism or duplicate tool names.

Separately, the 200-char arg truncation in trace makes post-hoc debugging difficult for tools with large payloads (e.g. code generation).

Changes

agent/src/agent/loop.py (3 lines changed):

Location Change
_execute_parallel (L749) Add call_id: tc.id to trace, remove [:200] on args
_execute_single (L794) Same as above
_finalize_tool_result (L873) Add call_id: tc.id to tool_result trace entry

Before

trace.write({"type": "tool_call", ..., "args": {k: str(v)[:200] for ...}})
trace.write({"type": "tool_result", ..., "preview": result[:200]})

After

trace.write({"type": "tool_call", ..., "call_id": tc.id, "args": {k: str(v) for ...}})
trace.write({"type": "tool_result", ..., "call_id": tc.id, "preview": result[:200]})

Note: black reformatting of loop.py is a pre-existing issue on main, not addressed in this PR to keep the diff minimal.

Test Plan

  • Existing tests pass (pytest --ignore=agent/tests/e2e_backtest --tb=short -q) — 2845 passed, 2 skipped, 0 failed. The 2 skipped tests are pre-existing (alpha101_096 NaN ratio, TUSHARE_TOKEN not set), unrelated to this PR.
  • New tests added (if applicable) — N/A, no new behavior, only trace metadata enrichment
  • Tested manually: ran an agent session, inspected trace.jsonl and confirmed call_id appears on both tool_call and tool_result entries, and args are untruncated in trace

Checklist

  • No changes to protected areas (src/agent/, src/session/, src/providers/) without prior discussion
  • No hardcoded values (API keys, file paths, magic numbers)
  • Code follows CONTRIBUTING.md guidelines
  • Documentation updated (if user-facing change) — N/A, internal trace format only
zwrong added 2 commits June 3, 2026 18:32
- Add call_id to tool_call traces in both single and parallel execution
- Replace truncated result preview with full result in tool_result trace

Signed-off-by: zwrong <1003912034@qq.com>
"result" replaces "preview" means full payload goes into trace.jsonl,
which breaks the CLI trace replay consumer that reads entry.get("preview", "").

200-char preview is sufficient for quick CLI replay; full result would
force every consumer to handle both old "preview" and new "result" fields.

Signed-off-by: zwrong <1003912034@qq.com>
warren618 added a commit that referenced this pull request Jun 3, 2026
Adds the tool call_id to the tool_call and tool_result trace records so a
result can be correlated back to its originating call when replaying a trace.
Keeps the existing [:200] truncation on serialized args to avoid bloating
trace files with large tool inputs.

Equivalent to #168 (arg truncation preserved).

Co-authored-by: zwrong <1003912034@qq.com>
@warren618

Copy link
Copy Markdown
Collaborator

Thanks for spotting this, @zwrong — correlating tool_calltool_result via call_id is a genuinely useful trace improvement. We've landed an equivalent change in abbbab5, crediting you via Co-authored-by. The only tweak from your patch: we kept the existing [:200] truncation on the serialized tool args, since dropping it would write full untruncated inputs into every trace entry and bloat trace files for tools with large arguments. Closing in favor of that commit — thanks again for the contribution! 🙏

@warren618 warren618 closed this Jun 3, 2026
warren618 added a commit that referenced this pull request Jun 3, 2026
…ales

Adds the 2026-06-03 News entry (trace call_id correlation #168, dead-doc
reference cleanup #166, langchain-community install-warning clarification
#167, Gemini thoughtSignature scoped to help-wanted #170) to the English,
Chinese, Japanese, Korean, and Arabic READMEs, and collapses the 2026-05-31
entry into the "Earlier news" section to keep three latest entries visible.
@zwrong zwrong deleted the feature/trace-enrich branch June 14, 2026 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants