Skip to content

feat: add CLI commands for managing chat context from workspaces#24105

Merged
ibetitsmike merged 40 commits intomainfrom
kylecarbs/chat-context-add-clear
Apr 9, 2026
Merged

feat: add CLI commands for managing chat context from workspaces#24105
ibetitsmike merged 40 commits intomainfrom
kylecarbs/chat-context-add-clear

Conversation

@kylecarbs
Copy link
Copy Markdown
Member

@kylecarbs kylecarbs commented Apr 7, 2026

Adds coder exp chat context add and coder exp chat context clear commands that run inside a workspace to manage chat context files via the agent token.

add reads instruction and skill files from a directory (defaulting to cwd) and inserts them as context-file messages into an active chat. Multiple calls are additive — instructionFromContextFiles already accumulates all context-file parts across messages.

clear soft-deletes all context-file messages, causing contextFileAgentID() to return !found on the next turn, which triggers needsInstructionPersist=true and re-fetches defaults from the agent.

Both commands auto-detect the target chat via CODER_CHAT_ID (already set by agentproc on chat-spawned processes), or fall back to single-active-chat resolution for the agent. The --chat flag overrides both.

Also adds sub-agent context inheritance: createChildSubagentChat now copies parent context-file messages to child chats at spawn time, so delegated sub-agents share the same instruction context without independently re-fetching from the workspace agent.

Implementation details

New files:

  • cli/exp_chat.go — CLI command tree under coder exp chat context

Modified files:

  • agent/agentcontextconfig/api.goConfigFromDir() reads context from an arbitrary directory without env vars
  • codersdk/agentsdk/agentsdk.goAddChatContext/ClearChatContext SDK methods
  • coderd/workspaceagents.go — POST/DELETE handlers on /workspaceagents/me/chat-context
  • coderd/coderd.go — Route registration
  • coderd/database/queries/chats.sqlGetActiveChatsByAgentID, SoftDeleteContextFileMessages
  • coderd/database/dbauthz/dbauthz.go — RBAC implementations for new queries
  • coderd/x/chatd/subagent.gocopyParentContextFiles for sub-agent inheritance
  • cli/root.go — Register chatCommand() in AGPLExperimental()

Auth pattern: Uses AgentAuth (same as coder external-auth) — agent token via CODER_AGENT_TOKEN + CODER_AGENT_URL env vars.

🤖 Generated by Coder Agents

Copy link
Copy Markdown
Member Author

@kylecarbs kylecarbs left a comment

Choose a reason for hiding this comment

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

Good implementation structure — the auto-detect pattern (0/1/N chats), AgentAuth reuse, and graceful sub-agent context copy are well done.

Deep review found one showstopper and several issues to address: 1 P0, 2 P1, 3 P2 across 6 inline comments.

This review contains findings that may need attention before merge.

🤖 Generated by Coder Agents

Comment thread coderd/workspaceagents.go
Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go Outdated
Comment thread cli/exp_chat.go
@ibetitsmike ibetitsmike requested a review from mafredri April 7, 2026 16:33
Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Round 1 review from a 10-reviewer panel (one timed out). The design is sound: agent-authenticated endpoints with explicit ownership checks and AsSystemRestricted bypass. The sub-agent context inheritance is a nice addition. Prior-round findings (RBAC scope, ownership check, Count field, part filtering, parts cap, serpent env fallback) are all resolved in f75c9a9.

Severity count: 4x P1, 5x P2, 2x P3, plus notes.

What this does well: the resolveAgentChat auto-detection pattern is clean. The dbauthz wrappers follow existing patterns correctly. The clear endpoint's graceful handling of zero active chats (return OK with nil ChatID) is good UX.

Notable convergences: all 9 reviewers independently flagged the string-based error dispatch; 7 flagged the skill-parts asymmetry between add/clear/copy; 4 flagged the ContentVersion 0 issue; 4 flagged the missing SanitizePromptText.

Process note: 884 lines of new code across 5 subsystems (CLI, handlers, DB queries, SDK, chatd) with zero handler/integration tests. The dbauthz mocks verify RBAC wiring but not behavior. This is the single largest risk for regression.

Batching: copyParentContextFiles inserts one message per parent context-file message. InsertChatMessages supports batch params. Accumulating into a single call would reduce round-trips. (Knov P3, Knuckle P2, Kite P3, Zoro Note)

"Oh, this test suite looks lovely! Fifty rows, full coverage, green across the board. It's fake. Every row hits the same code path. You dressed up one test in fifty outfits." ...except here there are zero outfits. -- Bisky

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go
Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/database/queries/chats.sql
Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/database/queries/chats.sql
Comment thread coderd/x/chatd/subagent.go Outdated
Comment thread cli/exp_chat.go Outdated
@coder-tasks
Copy link
Copy Markdown
Contributor

coder-tasks bot commented Apr 7, 2026

Documentation Check

Updates Needed

  • docs/ai-coder/agents/extending-agents.md — Add a section describing the new coder exp chat context add and coder exp chat context clear commands. These commands let agents (or scripts running inside a workspace) dynamically inject or clear context during an active chat session. add reads both instruction files and skills from a directory (defaulting to cwd; reads skills from .agents/skills). clear soft-deletes all context-file and skill messages from the chat. The CODER_CHAT_ID env var (auto-set on chat-spawned processes), the --dir flag, and the auto-detection fallback for single-active-chat scenarios are all worth calling out.

    ⚠️ Still no documentation changes found in this PR for this item

New Documentation Needed

  • docs/ai-coder/agents/index.md (Sub-agents section) — The PR adds sub-agent context inheritance: createChildSubagentChat now copies parent context-file and skill messages to child chats at spawn time. The current sub-agents description does not mention how context propagates to delegated sub-agents. A sentence or two noting that sub-agents inherit the parent chat's context (instruction files and skills) would keep this section accurate.

    ⚠️ Still no documentation changes found in this PR for this item


Automated review via Coder Tasks

@ibetitsmike ibetitsmike force-pushed the kylecarbs/chat-context-add-clear branch from 0640b30 to b25e5fd Compare April 7, 2026 22:18
Copy link
Copy Markdown

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

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: 0640b30d09

ℹ️ 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 coderd/x/chatd/subagent.go Outdated
@ibetitsmike ibetitsmike force-pushed the kylecarbs/chat-context-add-clear branch from b25e5fd to f990c4d Compare April 7, 2026 22:29
@ibetitsmike ibetitsmike requested a review from mafredri April 7, 2026 22:55
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Round 2 review from a 4-reviewer panel. All 18 prior findings from rounds 0 and 1 are resolved. The fixes are solid: proper sentinel errors, active-status checks, SanitizePromptText, MaxBytesReader, ContentVersion V1, partial index on agent_id, transactional child chat creation, and skills covered by both clear and copy.

Severity count: 2x P1, 3x P2, 1x P3.

What was addressed well: the refactored resolveAgentChat now returns a full database.Chat with ownership and active-status validation. The sentinel error system (errChatNotFound, errChatNotActive, errChatDoesNotBelongToAgent, multipleActiveChatsError) is clean. The transactional createChildSubagentChat with post-commit signalWake correctly eliminates the race condition reported by chatgpt-codex-connector.

What remains: the handler constructs and serializes messages via a parallel code path (raw json.Marshal + manual params construction) instead of using the canonical chatprompt.MarshalParts + newChatMessage/appendChatMessage builder that every other insertion site uses. Using the builder would fix three findings (NUL encoding, manual construction, content field handling) in a single change. The copyParentContextMessages code in subagent.go, refactored in this same PR, correctly uses the builder, which makes the handler's omission more visible.

Test coverage: 919 lines of new code across 5 subsystems, still zero handler/integration tests after 3 rounds of review. Every prior finding (ContentVersion=0, string-based dispatch, active-status bypass) was a bug that tests would have caught.

"919 lines of untested production code behind agent auth." -- Kite

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go
Comment thread coderd/x/chatd/subagent.go Outdated
Comment thread coderd/workspaceagents.go
Copy link
Copy Markdown

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

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: 4b328df938

ℹ️ 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 coderd/x/chatd/subagent.go Outdated
Comment thread coderd/workspaceagents.go
Comment thread coderd/workspaceagents.go
@ibetitsmike ibetitsmike requested a review from mafredri April 7, 2026 23:22
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Round 3 review from a 3-reviewer panel. All 24 prior findings from rounds 0-2 are verified resolved and correct. The round 2 fixes are solid.

Severity count: 1x P1, 2x P3.

All 7 fixes from 6d17906 confirmed by all 3 reviewers: MarshalParts for NUL encoding, BuildSingleChatMessageInsertParams for canonical builder, ContextFileAgentID on all parts, copyParentContextMessages error propagation, transactional clearAgentChatContext with row lock, correct message ordering (system -> context -> user), and last_injected_context cache updates.

The refactored createChildSubagentChat transaction was audited in detail (Kite) and matches CreateChat's structure: usage limit check, InsertChat params, pubsub/signalWake after commit, nil-slice normalization.

One finding persists: zero test coverage for 977 lines of new code after 3 rounds. Everything else is clean.

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go Outdated
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

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

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: 6d17906cdb

ℹ️ 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 coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go Outdated
@ibetitsmike ibetitsmike requested a review from mafredri April 8, 2026 00:13
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Round 4 review from a 2-reviewer panel. All 30 prior findings verified resolved. The compression-boundary fix (6f4aca5) is confirmed correct by both reviewers.

Severity count: 1x P1, 1x P3.

The code is structurally sound. Every correctness, security, and consistency finding raised across 4 rounds has been addressed. One P3 consistency issue and the persistent test coverage gap remain.

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/subagent.go Outdated
Copy link
Copy Markdown

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

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: 6f4aca5ab7

ℹ️ 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 cli/exp_chat.go
Comment thread coderd/x/chatd/subagent.go Outdated
@ibetitsmike ibetitsmike requested a review from mafredri April 8, 2026 00:47
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Round 5 review. All 32 prior findings verified resolved. Both delta fixes (copyParentContextMessages compression boundary, CLI dir validation) confirmed correct.

Severity count: 1x P2, 1x P3 (new), plus the persistent P1 test coverage gap.

The code is mature. Every correctness, security, consistency, and edge-case finding raised across 5 rounds has been addressed. Two minor observations remain.

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/workspaceagents.go Outdated
Comment thread coderd/workspaceagents.go Outdated
Copy link
Copy Markdown

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

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: 013c2b689c

ℹ️ 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 coderd/workspaceagents.go Outdated
@ibetitsmike ibetitsmike requested a review from mafredri April 8, 2026 01:25
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Round 6 review from a 2-reviewer panel. All 34 prior findings verified resolved or acknowledged. Both reviewers independently verified every sentinel code path (skill-only, context-only, mixed, empty) and confirmed the sentinel is transparent to callers, properly cleared by SoftDeleteContextFileMessages, and correctly detected by contextFileAgentID in the turn pipeline.

Severity count: 0x P0-P2, 2x P3 (cosmetic).

The cache rebuild (updateAgentChatLastInjectedContextFromMessagesBestEffort) correctly reads all persisted messages and writes the cumulative set. responsePartCount is captured before sentinel prepend. clearAgentChatContext transaction properly handles sentinel+skill messages. New slice allocation in prependAgentChatContextSentinelIfNeeded avoids mutating the caller's slice. All verified.

Two cosmetic P3s noted below. The code is ready for merge pending the persistent test coverage gap (P1, every round).

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/workspaceagents.go Outdated
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

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

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: d4180fd1ee

ℹ️ 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 coderd/workspaceagents.go
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

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

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: 7830c22bd7

ℹ️ 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 coderd/workspaceagents.go Outdated
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

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

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: 1dcd6d4940

ℹ️ 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 coderd/x/chatd/instruction.go Outdated
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

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

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: 1dcd6d4940

ℹ️ 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 coderd/x/chatd/chatd.go
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

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

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: 0302d6fc18

ℹ️ 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 coderd/x/chatd/subagent.go
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

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

@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

Copy link
Copy Markdown

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

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: 9363409f13

ℹ️ 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 coderd/workspaceagents.go
@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

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

@ibetitsmike
Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

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

@ibetitsmike ibetitsmike merged commit 391b22a into main Apr 9, 2026
26 checks passed
@ibetitsmike ibetitsmike deleted the kylecarbs/chat-context-add-clear branch April 9, 2026 14:33
@github-actions github-actions bot locked and limited conversation to collaborators Apr 9, 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.

4 participants