Skip to content

feat: tag chat-originating agent logs with chat_id#25019

Open
ethanndickson wants to merge 4 commits intomainfrom
agent-tools-e6j4
Open

feat: tag chat-originating agent logs with chat_id#25019
ethanndickson wants to merge 4 commits intomainfrom
agent-tools-e6j4

Conversation

@ethanndickson
Copy link
Copy Markdown
Member

@ethanndickson ethanndickson commented May 7, 2026

Workspace-agent logs emitted while serving chatd-driven requests were not correlated with the originating chat, making agent logs hard to attribute to the corresponding/originating chat.

This adds agent-side chat context middleware that parses Coder-Chat-Id once, enriches agent access logs and structured handler/background logs, and adds a chatd bridge log when chat headers are attached to an agent connection.

Closes CODAGT-324

Adds an agentchat package that propagates Coder-Chat-Id and
Coder-Ancestor-Chat-Ids from chatd-originated requests through the
agent's HTTP middleware and into structured handler/background logs.

- New agent/agentchat package with ExtractContext, FromContext,
  WithContext, Fields, and Middleware. Middleware enriches the
  loggermw access log and stores the chat identity on ctx.
- agent/api.go installs agentchat.Middleware after loggermw.Logger.
- Handler and background logs in agentfiles, agentproc, agentgit,
  agentdesktop, and agentmcp derive a per-request slog.Logger via
  api.logger.With(agentchat.Fields(ctx)...) so emitted lines carry
  chat_id and ancestor_chat_ids.
- agentproc.process stores a chat-tagged logger on the process struct
  so post-cmd.Wait goroutines retain chat identity.
- chatd's getWorkspaceConn emits a Debug bridge log when chat headers
  are attached to the agent connection, easing diagnosis of cases
  where a chat-driven action lacks chat_id on the agent side.

Existing chatheaders parser moved from agentgit to agentchat under
the new name (ExtractContext) since the helper is now used by
multiple agent subsystems unrelated to git.
Use the chat context populated by agentchat.Middleware in file and process handlers instead of reparsing chat headers in each handler. This lets the header parser become package-private and keeps chat identity parsing centralized in middleware.

Also documents that agentchat.Middleware must run after loggermw.Logger for access-log enrichment and avoids allocating an empty ancestor slice when no ancestors are present.
Restore the straightforward make/copy in agentchat.WithContext instead of branching on empty slices. Update test expectations for middleware-normalized empty ancestors accordingly while preserving order-sensitive equality for non-empty ancestor lists.
@ethanndickson
Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

ℹ️ 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".

@ethanndickson ethanndickson marked this pull request as ready for review May 7, 2026 03:32
@ethanndickson ethanndickson changed the title feat: tag chat-originated agent logs with chat_id feat: tag chat-originating agent logs with chat_id May 7, 2026
Comment thread agent/agentchat/log.go
Comment on lines +65 to +67
if requestLogger := loggermw.RequestLoggerFromContext(r.Context()); requestLogger != nil {
requestLogger.WithFields(fields...)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've seen some other middlewares panic if they're missing other middlewares upon which they depend. I'm not sure that logging is something worth panicking over though... dealer's choice here. Just flagging this as a potential future foot-gun if someone forgets the logger middleware before adding this on another route.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants