Skip to content

PR5: agent run IDs and trace correlation - #61

Open
Coldaine wants to merge 3 commits into
mainfrom
feat/pr5-agent-run-ids
Open

PR5: agent run IDs and trace correlation#61
Coldaine wants to merge 3 commits into
mainfrom
feat/pr5-agent-run-ids

Conversation

@Coldaine

@Coldaine Coldaine commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

User description

Summary

Implements PR5 from the remaining implementation master plan: agent run IDs and trace correlation.

Adds:

  • --run-id CLI option to stamp each invocation with an explicit run id (auto-generated when omitted).
  • run_id threaded through normalized search/extract/crawl output, agent steps, usage records, and history entries so a single logical run can be traced end-to-end.
  • Agent context carries the run id into prompt context for traceability.
  • Fanout engine passes the run id to each fanout branch.
  • Execution backend associates run id with executions.
  • History types extended with runId.
  • Usage logger records run_id per usage event.
  • New tests: agent-mode run-id propagation and CLI integration coverage.

Validation

  • npm run typecheck — pass
  • npm run test:docs — pass
  • npm test — 239/239 pass

Closes the PR5 item of the master plan. Reviewers: this PR is based on current main; PR4 config/status UX (#59) and its late-review follow-up (#60) are separate.


CodeAnt-AI Description

Correlate each agent research run across output, history, and usage records

What Changed

  • Agent runs now receive a generated run ID, or use an explicit ID supplied with --run-id
  • The same run ID appears in agent output, research steps, provider usage entries, and search, extract, and crawl history
  • Surrounding whitespace is removed from explicit IDs so correlation remains consistent
  • Empty IDs and --run-id on non-agent commands now fail with clear errors
  • Added coverage for generated IDs, propagation, validation, and unchanged non-agent behavior

Impact

✅ Traceable agent research runs
✅ Correlated usage and execution history
✅ Clearer invalid run ID errors

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

Copilot AI lite review requested due to automatic review settings August 11, 2026 16:19
@codeant-ai

codeant-ai Bot commented Aug 11, 2026

Copy link
Copy Markdown

🤖 CodeAnt AI — Review Status

Status Commit Started (UTC) Finished (UTC)
✅ Incremental review completed f793e3a Aug 14, 2026 · 00:25 00:26
✅ Reviewed your PR 5defc39 Aug 11, 2026 · 16:19 16:22

@codeant-ai

codeant-ai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@qodo-code-review

Copy link
Copy Markdown

ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing

@codeant-ai codeant-ai Bot added the size:L This PR changes 100-499 lines, ignoring generated files label Aug 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR implements PR5 from the master plan by introducing an agent run ID concept and threading it through agent-mode output, step recording, usage logging, execution history, and execution backend recording to enable end-to-end trace correlation for a single logical agent run.

Changes:

  • Added --run-id (agent mode) plus createRunId() auto-generation, and propagated the run ID through agent results and step records.
  • Recorded run_id in usage log entries and execution history records when calls are agent-triggered.
  • Added test coverage for run-id propagation in agent mode and CLI integration.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/cli-integration.test.mjs Adds CLI-level integration tests for --run-id, generated IDs, usage/history correlation, and invalid inputs.
test/agent-mode.test.mjs Adds unit tests validating generated/explicit run IDs and per-step run ID consistency.
src/types.ts Extends CLI options with runId.
src/logging/usage.ts Adds run_id field to usage log entry schema.
src/history/types.ts Extends execution history record schema with optional run_id.
src/execution/backend.ts Persists run_id on execution records when options.runId is present.
src/engine/fanout.ts Threads runId into provider calls and stamps usage log entries with run_id.
src/cli.ts Adds --run-id parsing and includes run_id in agent-mode CLI JSON output.
src/agent/context.ts Stores run ID in research context and stamps steps with run_id.
src/agent/agent.ts Generates/validates run IDs and passes them into backend searches for correlation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/cli.ts
Comment on lines +353 to +357
case "--run-id":
i++;
const runId = args[i];
// Empty/whitespace-only explicit run IDs fail early: a generated ID
// is always non-empty, so a blank explicit value is a caller bug. A
Comment thread src/cli.ts
Comment on lines +353 to +365
case "--run-id":
i++;
const runId = args[i];
// Empty/whitespace-only explicit run IDs fail early: a generated ID
// is always non-empty, so a blank explicit value is a caller bug. A
// flag-looking token means the value was omitted (like --config).
if (runId === undefined || runId.startsWith("--") || runId.trim() === "") {
throw new Error(
`Invalid --run-id: ${runId ?? "(none)"}. Use a non-empty run ID, e.g. --run-id run_20260622T173012Z_7f3a9c.`
);
}
options.runId = runId;
break;
Comment thread src/agent/agent.ts Outdated
Comment on lines +222 to +226
const explicitRunId = options.runId ?? this.runId;
if (explicitRunId !== undefined && explicitRunId.trim() === "") {
throw new Error("Explicit run ID must not be empty or whitespace-only");
}
const runId = explicitRunId ?? createRunId();

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d4293f0ae1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/history/types.ts
* without it. `run_id` never replaces `id` — every execution keeps its own
* PR 2 execution ID.
*/
run_id?: string;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Expose run IDs through history discovery

When an operator starts with the run_id returned by agent mode, history recent drops this field in summarizeExecution, while searchHistory never matches it, so coldsearch history search <run_id> returns no executions and there is no CLI-supported way to discover the correlated execution IDs. Include run_id in history summaries and index it as a history-search predicate so the newly persisted correlation can actually be followed.

Useful? React with 👍 / 👎.

Comment thread src/cli.ts Outdated
Comment on lines +353 to +364
case "--run-id":
i++;
const runId = args[i];
// Empty/whitespace-only explicit run IDs fail early: a generated ID
// is always non-empty, so a blank explicit value is a caller bug. A
// flag-looking token means the value was omitted (like --config).
if (runId === undefined || runId.startsWith("--") || runId.trim() === "") {
throw new Error(
`Invalid --run-id: ${runId ?? "(none)"}. Use a non-empty run ID, e.g. --run-id run_20260622T173012Z_7f3a9c.`
);
}
options.runId = runId;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject --run-id outside agent mode

When --run-id is supplied to a non-agent command such as coldsearch search --run-id run_x query, parsing succeeds, but the normal search/extract/crawl paths never pass options.runId to the backend, so the command silently produces history and usage entries without the requested ID. Since this option is documented as agent-only, reject it unless --agent is active rather than accepting a correlation identifier that is then discarded.

Useful? React with 👍 / 👎.

Comment thread src/agent/context.ts
Comment on lines 66 to +69
type,
description,
timestamp: new Date(),
...(this.runId ? { run_id: this.runId } : {}),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Persist agent steps instead of keeping them in memory

When an agent uses fetch or refine, or returns a final answer without searching, this in-memory ResearchContext is the only place where the run's steps exist: runAgentMode emits only steps.length, and backend history is written only for provider searches. Once the process exits, those actions, their ordering, timing, and outcomes cannot be inspected by run_id, so the change still cannot reconstruct the durable agent/tool flow it is intended to correlate; persist structured step events keyed by the run ID.

AGENTS.md reference: AGENTS.md:L16-L16

Useful? React with 👍 / 👎.

Address PR #61 review findings:
- Trim surrounding whitespace from explicit --run-id in the CLI parser and
  the agent boundary so the stored ID always matches output, steps, usage
  logs, and history records.
- Reject --run-id unless --agent is active: non-agent commands never thread
  the ID into backend calls, so accepting it silently produced history and
  usage entries without the requested correlation ID.
@codeant-ai

codeant-ai Bot commented Aug 14, 2026

Copy link
Copy Markdown

Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@codeant-ai codeant-ai Bot added size:L This PR changes 100-499 lines, ignoring generated files and removed size:L This PR changes 100-499 lines, ignoring generated files labels Aug 14, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f793e3a850

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/cli.ts
// Run IDs are an agent-mode feature: non-agent commands never pass the ID
// into backend calls, so accepting it there would silently produce history
// and usage entries without the requested correlation ID.
if (options.runId !== undefined && !options.agent) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject run IDs when agent mode will not execute

Fresh evidence in the current tree shows this check can still be bypassed: commands such as coldsearch history recent --agent --run-id run_x pass because options.agent is true, but main() dispatches history, tool, cache, config, batch, and status commands before reaching agent mode, so the run ID is silently ignored. Validate that no higher-priority command mode is selected, rather than checking the flag alone.

Useful? React with 👍 / 👎.

Comment thread src/agent/agent.ts
}
// Trim surrounding whitespace so the canonical ID is stable for
// correlation; a padded value would mismatch across logs and output.
const runId = explicitRunId?.trim() ?? createRunId();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve generated run IDs when agent execution fails

When --run-id is omitted and a later uncaught LLM request fails—such as the second completion after a successful search—this ID already tags the provider history and usage records, but research() rejects before returning it and the CLI error path prints only the exception. The operator therefore receives no generated ID with which to correlate the surviving records from the failed run; generate the ID at a boundary that can include it in failure output or attach it to propagated errors.

Useful? React with 👍 / 👎.

@kilo-code-bot

kilo-code-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

Code Review Summary

Status: 4 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 2
SUGGESTION 2
Issue Details (click to expand)

WARNING

File Line Issue
src/cli.ts 435 --run-id validation is bypassable: --agent --run-id combined with non-agent commands (e.g. history recent --agent --run-id run_x) passes the !options.agent check, but main() dispatches to the other command before reaching agent mode, so the run ID is silently ignored — violates Fail Visible
src/history/types.ts 57 run_id is persisted in ExecutionRecord but omitted from summarizeExecution (so history recent/history search output drops it) and is not a matchable predicate in searchHistory, making the correlation ID undiscoverable from the CLI

SUGGESTION

File Line Issue
src/agent/context.ts 69 Agent steps are held in-memory only via ResearchContext; run_id-tagged steps from fetch/refine/synthesize are lost when the process exits, so the durable agent/tool-flow trace cannot be fully reconstructed from history
src/agent/agent.ts 228 When --run-id is omitted and a later LLM call fails, the generated run_id (already tagged in history/usage records) is never returned to the caller; the CLI error path prints only the exception, preventing correlation of surviving records from a failed run
Files Reviewed (10 files)
  • src/agent/agent.ts - 1 issue
  • src/agent/context.ts - 1 issue
  • src/cli.ts - 1 issue
  • src/engine/fanout.ts - no issues
  • src/execution/backend.ts - no issues
  • src/history/types.ts - 1 issue
  • src/logging/usage.ts - no issues
  • src/types.ts - no issues
  • test/agent-mode.test.mjs - no issues
  • test/cli-integration.test.mjs - no issues

Fix these issues in Kilo Cloud


Reviewed by laguna-s-2.1:free · Input: 531.2K · Output: 68.3K · Cached: 2.1M

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

Labels

size:L This PR changes 100-499 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants