🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

feat: add chat context source CLI and agent-token refresh - #26577

Merged
kylecarbs merged 2 commits into
mainfrom
kylecarbs/chat-context-cli
Jun 22, 2026
Merged

feat: add chat context source CLI and agent-token refresh#26577
kylecarbs merged 2 commits into
mainfrom
kylecarbs/chat-context-cli

Conversation

@kylecarbs

Copy link
Copy Markdown
Member

Adds the coder exp chat context CLI for managing workspace context sources, plus the agent-token refresh endpoint the in-workspace refresh relies on. Part of breaking the "Workspace Context Sources for Coder Agents" RFC (#26466) into small, reviewable PRs.

What this adds

CLI (coder exp chat context), talking to the agent's local IPC socket from inside the workspace:

  • list lists the registered scan roots (built-in defaults are not shown).
  • show <path> shows a source and the resources the agent resolves from it, including failures.
  • add <path> registers a path as an additional context source. With --chat, it keeps the legacy one-shot behavior (read context from the path once and inject it into a single chat).
  • remove <path> unregisters a source.
  • refresh [<chat>] re-pins chat context to the agent's latest snapshot.

Agent-token refresh path for the no-argument refresh:

  • refresh <chat> uses the existing user-facing ExperimentalClient.RefreshChatContext (already on main) and works from anywhere.
  • refresh with no argument runs inside the workspace: it re-resolves the agent's sources over the context socket (catching freshly-cloned repos and startup-script writes), then asks the agent, authenticating with its own token, to re-pin every drifted chat. No coder login required.
  • This adds agentsdk.RefreshChatContext and POST /api/v2/workspaceagents/me/experimental/chat-context/refresh (workspaceAgentRefreshChatContext), mirroring the existing clear endpoint's agent-token auth model.

Testing

  • go test ./cli (TestExpChatContextAdd, TestParseChatID, TestResolveContextSourcePath)
  • go test ./coderd/x/chatd -run TestChatContextRefreshFromAgentToken (end-to-end: echo-provisioned agent pushes a snapshot, drifts a bound chat, the agent-token refresh re-pins it, and an agent-less chat stays untouched)
  • go build ./..., go vet, golangci-lint, make gen (no generated changes; experimental commands are excluded from CLI golden/doc generation)
Design notes

This is Split 4 of #26466. Split sequence:

  1. feat(coderd/x/chatd): consume the pinned chat context in prompt generation #26558 - prompt pin consumption (merged)
  2. feat: report pinned chat context resources on chat API #26570 - codersdk context resource types (merged)
  3. feat(site/src): surface pinned chat workspace context in the UI #26573 - the context indicator UI (merged)
  4. This PR - the CLI + agent-token refresh.
  5. The context diff (changes, ChatContextResourceChange, the changes dialog, buildContentPatch) - last.

Key points:

  • The agent-local context subsystem (agent/agentsocket IPC for source CRUD, snapshot, resync), the user-facing ExperimentalClient.RefreshChatContext, and the per-chat chatd.RefreshChatContext all already exist on main, so this split is the CLI surface plus the small agent-token refresh endpoint that fans out per-chat refresh across an agent's drifted chats.
  • add <path> resolves relative paths to absolute before handing them to the agent (which requires canonical paths) but preserves a leading ~ for the agent to expand against its own home. TestResolveContextSourcePath covers this.
  • The agent endpoint is annotated @x-apidocgen {"skip": true}, matching the other agent-token chat-context endpoints.
  • No diff/changes rendering is involved; that lands in the final split.

This PR was created by Coder Agents on behalf of @kylecarbs.

Add `coder exp chat context` subcommands for managing the workspace
context sources the agent resolves, over its local IPC socket:

- list: show the registered scan roots
- show <path>: show a source and the resources it contributes
- add <path>: register a source, or with --chat keep the legacy one-shot
  inject into a single chat
- remove <path>: unregister a source
- refresh [<chat>]: re-pin a chat to the agent's latest snapshot

With a <chat> argument, refresh uses the existing user-facing endpoint.
With no argument it runs inside the workspace: it re-resolves the agent's
sources over the context socket, then asks the agent (using its own
token) to re-pin every drifted chat. That path adds
agentsdk.RefreshChatContext and the
POST /api/v2/workspaceagents/me/experimental/chat-context/refresh handler,
mirroring the clear endpoint's auth model, covered by
TestChatContextRefreshFromAgentToken.

Split from #26466. The context changes/diff view is deferred to the final
split.
@datadog-coder

This comment has been minimized.

…osts

resolveContextSourcePath used filepath.Abs, which is host-OS specific:
on a Windows CLI host a POSIX absolute path like /home/coder/AGENTS.md is
not absolute, so filepath.Abs prepended the drive and produced
C:\home\coder\AGENTS.md, failing TestResolveContextSourcePath on
windows-2022. The agent's filesystem is always POSIX, so a leading / is
already workspace-absolute. Clean and pass it through, reserving
filepath.Abs for relative paths. This also renames the one-shot helper's
path parameter to dir to avoid shadowing the new path import.
@kylecarbs
kylecarbs merged commit 966dd89 into main Jun 22, 2026
29 of 30 checks passed
@kylecarbs
kylecarbs deleted the kylecarbs/chat-context-cli branch June 22, 2026 18:15
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants