Skip to content

fix(cli): match context show resources by scan-root containment#26617

Open
kylecarbs wants to merge 1 commit into
mainfrom
fix/cli-context-show-attribution
Open

fix(cli): match context show resources by scan-root containment#26617
kylecarbs wants to merge 1 commit into
mainfrom
fix/cli-context-show-attribution

Conversation

@kylecarbs

Copy link
Copy Markdown
Member

Problem

coder exp chat context show <path> reports 0 resources for many valid sources (for example a built-in root like ~/.coder or the workspace working directory), even though those resources are present in the snapshot and injected into chat context.

Root cause

The show handler filtered the snapshot with res.SourcePath == src.Path. Built-in and working-directory scan roots overlap user sources, and the resolver (agent/agentcontext/resolve.go) deduplicates overlapping scan roots by attributing each resource to the first (built-in / working-dir) root. That leaves SourcePath empty on the resource, so an exact-SourcePath filter matches nothing for the overlapping source.

Fix

Match a resource to a source when:

  • SourcePath equals the source path (unchanged, explicit attribution), or
  • the resource's own path (Source) is equal to, or lies under, the source root.

Containment uses POSIX path semantics (strings.HasPrefix(p, dir+"/") after trimming a trailing slash) because agent context paths are absolute POSIX paths even when the CLI runs on a Windows host, and the boundary check avoids false positives like /a/bc matching /a/b.

MCP servers are keyed by a server name rather than a filesystem path, so they continue to match only by SourcePath and are never matched by containment.

Tests

Adds TestContextResourceFromSource (table-driven) covering: explicit SourcePath match, built-in-root containment with empty SourcePath, skills under a skills root, exact file source, different-subtree non-match, a symlinked resource resolved outside a root, prefix-boundary safety, and MCP-server matching by SourcePath only.

go test ./cli/ -run 'TestContextResourceFromSource|TestResolveContextSourcePath' -count=1
ok  github.com/coder/coder/v2/cli

gofmt, go vet ./cli/, and go build ./cli/... are clean.

Related investigation

This is one of several issues found while investigating reports about the recently merged chat-context system. It is a self-contained, low-risk fix opened independently of the others:

Remaining items from the investigation are architectural (lazy-load/bloat, skill scope precedence, raw-path plumbing, backend server-to-config linkage, startup-timing re-pin, stale-pin rebind-on-open) and are deferred for design sign-off.

Generated by Coder Agents on behalf of @kylecarbs.

`coder exp chat context show <path>` filtered the snapshot by
res.SourcePath == src.Path. Built-in and working-directory scan roots
overlap user sources, and the agent attributes each overlapping resource
to the first (built-in / working-dir) root, leaving SourcePath empty. As
a result, showing such a source reported zero resources.

Match a resource to a source when SourcePath is equal, or when the
resource's own path lies at or under the source root. Path containment
uses POSIX semantics because agent context paths are absolute POSIX
paths even when the CLI runs on a Windows host. MCP servers are keyed by
a server name rather than a filesystem path, so they continue to match
only by SourcePath.

Add TestContextResourceFromSource covering explicit SourcePath matches,
built-in-root containment, symlinked resources outside a root,
prefix-boundary safety, and MCP-server matching.

Generated by Coder Agents on behalf of @kylecarbs.
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.

1 participant