Skip to content

feat: Copilot governance engine, VS Code extension, recall parser, and Copilot CLI integration#45

Open
bilersan wants to merge 12 commits intoActiveMemory:mainfrom
bilersan:feat/copilot-governance
Open

feat: Copilot governance engine, VS Code extension, recall parser, and Copilot CLI integration#45
bilersan wants to merge 12 commits intoActiveMemory:mainfrom
bilersan:feat/copilot-governance

Conversation

@bilersan
Copy link
Copy Markdown
Contributor

@bilersan bilersan commented Mar 17, 2026

Summary

Three-layer governance architecture for VS Code Copilot and full GitHub Copilot CLI integration that closes the governance gap with Claude Code hooks, plus Copilot Chat session parser and Windows compatibility fixes.

Resolves #44
Closes #28
Closes #30
Closes #51
Supersedes #29, #31, #36

Architecture

Layer 1: Behavioral Rules (copilot-instructions.md)

Preventive/advisory rules in the system prompt — session lifecycle, when to call tools, governance response handling.

Layer 2: MCP Governance-in-Response

CheckGovernance() appends contextual warnings to every MCP tool response:

  • Session not started → nudge ctx_session_event(start)
  • Context not loaded → nudge ctx_status()
  • Drift stale (>15min or >5 calls) → nudge ctx_drift()
  • Persist nudge (>=10 calls since last write) → nudge ctx_add()
  • Violations from detection ring → CRITICAL escalation

Layer 3: VS Code Detection Ring

Extension monitors terminal commands and file edits:

  • Terminal watcher: dangerous commands (rm -rf /, git push --force, etc.)
  • File edit watcher: sensitive files (.env, .pem, credentials)
  • Records violations → MCP escalates with CRITICAL warnings

GitHub Copilot CLI Integration (Phases 1–4)

Phase 1 — Hook Generation

  • ctx hook copilot-cli --write generates .github/hooks/ctx-hooks.json
  • Dual bash/PowerShell scripts for sessionStart, preToolUse, postToolUse, sessionEnd
  • Dangerous command blocking and per-hook timeout control

Phase 2 — Agent Instructions

  • AGENTS.md generation with universal agent instructions
  • .github/agents/ctx.md custom agent + .github/instructions/context.instructions.md
  • Marker-based idempotent merge

Phase 3 — MCP Registration & Session Parser

  • Auto-register ctx MCP server in ~/.copilot/mcp-config.json
  • CopilotCLIParser for ctx recall with cross-platform session dir detection

Phase 4 — Skills

  • 5 skill templates: ctx-status, ctx-recall, ctx-drift, ctx-compact, ctx-next
  • Written to .github/skills/<name>/SKILL.md alongside hooks

Feature Matrix

Category Claude Code VS Code Copilot CLI
Context Injection ✅ 6/6 ✅ 4/6 ✅ 5/6
Hook System ✅ 5/15 ✅ 1/15 ✅ 10/15
MCP Server (11 tools)
Session Recall ✅ 5/7 ✅ 5/7 ✅ 5/7
Skills ✅ 37 ✅ 5
Cross-Platform ✅ 6/9 ✅ 8/9 ✅ 9/9

Full matrix: specs/copilot-cli-integration.md

Commits

  1. fix: Windows cross-platform compatibility — Case-insensitive path comparison
  2. feat: Copilot Chat session parser — CopilotParser for ctx recall
  3. feat: VS Code extension with MCP integration@ctx chat participant, detection ring
  4. feat: MCP governance engine — CheckGovernance with 5 checks, 24 tests
  5. fix: resolve build errors after rebase onto v0.8.0 — Variable rename, import path, entity types
  6. feat: Copilot CLI hook generation (Phase 1) — Cross-platform hooks with 8 scripts
  7. feat: Phase 2 agent instructions — AGENTS.md, agents/ctx.md, instructions/context.md
  8. feat: Phase 3 MCP registration + session parser — mcp-config.json + CopilotCLIParser
  9. feat: Phase 4 Copilot CLI skills — 5 embedded skill templates

Testing

  • 24 governance tests (all checks, edge cases, violations)
  • 39 MCP server tests (all pass including session priming)
  • Init/hook integration tests for mcp.json generation
  • Compliance tests pass (goconst, gosec, literal newline)

Copy link
Copy Markdown
Member

@josealekhine josealekhine left a comment

Choose a reason for hiding this comment

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

@bilersan if you fix the merge conflicts, I will give it a whirl.

Sorry about the 20K-line refactoring; that's partially the reason for the merge conflicts.

Alternatively you can close the PR and re-patch the changes from top-of-tree.

I'm assuming main will be "relatively" stable for a while as I will have lesser "Batman time" from next week onwards ;).

Thanks for all your help 🙏 .

- Case-insensitive path comparison on Windows for boundary validation
- Extract osWindows constant for goconst compliance

Closes ActiveMemory#30
Supersedes ActiveMemory#31

Signed-off-by: ersan bilik <ersanbilik@gmail.com>
- CopilotParser implementing SessionParser interface with Matches() and ParseFile()
- Platform-aware directory discovery for Code and Code Insiders
- Typed Go structs for Copilot Chat JSONL format (snapshot+patch model)
- Registration in parser.go and query scanning in query.go
- ToolCopilot constant in config/session

Closes ActiveMemory#28
Supersedes ActiveMemory#29

Signed-off-by: ersan bilik <ersanbilik@gmail.com>
- VS Code extension with @ctx chat participant and slash commands
- .vscode/mcp.json generation in init and hook paths
- VS Code-specific context overrides (CONSTITUTION, CONVENTIONS, etc.)
- Session event CLI command for VS Code lifecycle integration
- Detection ring: terminal watcher for dangerous commands
- Detection ring: file edit watcher for sensitive file patterns
- Violation recording to .context/state/violations.json
- Extension tests and init/hook integration tests

Supersedes ActiveMemory#36

Signed-off-by: ersan bilik <ersanbilik@gmail.com>
- Governance checker (CheckGovernance) appends contextual warnings to
  every MCP tool response: session-not-started, context-not-loaded,
  drift-stale, persist-nudge, and violation escalation
- Per-tool state recording: RecordSessionStart, RecordContextLoaded,
  RecordDriftCheck, RecordContextWrite, IncrementCallsSinceWrite
- Violation reading from .context/state/violations.json with automatic
  cleanup after escalation (read-and-clear pattern)
- Governance wired into route/tool dispatch with appendGovernance
- RecordSessionStart called on session event handler
- copilot-instructions.md template with governance rules and
  detection ring documentation
- 24 governance tests covering all check paths and edge cases

Signed-off-by: ersan bilik <ersanbilik@gmail.com>
- Set activationEvents to onStartupFinished for reliable activation
- Add /diag slash command for extension diagnostics
- Add OutputChannel logging for activation tracing
- Pass --force --caller vscode to init to prevent stdin hang
- Skip stdin overwrite prompt when caller is set (run.go)
- Add test-insiders-sim.js to .vscodeignore

Root cause: VS Code Insiders 1.112 uses extensions.experimental.affinity
to run GitHub.copilot-chat in a separate extension host process. Chat
requests only route to that host. The fix requires adding
activememory.ctx-context to the same affinity group in user settings.

Signed-off-by: ersan bilik <ersanbilik@gmail.com>
- Fix gosec G101 lint path in .golangci.yml (constants moved to config/embed)

- Fix TestTextDescKeysResolve path in embed_test.go

- Add license header and doc.go for internal/config/embed

- Add VS Code marketplace fields (extensionDependencies, pricing)

- Add CHANGELOG.md and update .vscodeignore

Signed-off-by: ersan bilik <ersanbilik@gmail.com>
- cmd.go: rename cmd.Flags() to c.Flags() (upstream variable rename)
- sessionevent/cmd.go: update import core -> core/state (package split)
- copilot.go: prefix Session/Message/ToolUse with entity. (types moved)

DCO-1.1-Signed-off-by: ersan bilik <ersanbilik@gmail.com>

Signed-off-by: ersan bilik <ersanbilik@gmail.com>
@bilersan bilersan force-pushed the feat/copilot-governance branch from ce018c9 to bf18bb0 Compare March 25, 2026 20:11
DCO-1.1-Signed-off-by: ersan bilik <ersanbilik@gmail.com>

Signed-off-by: ersan bilik <ersanbilik@gmail.com>
Add cross-platform hook generation for GitHub Copilot CLI integration.
ctx hook copilot-cli --write generates .github/hooks/ctx-hooks.json
with dual bash/PowerShell scripts for sessionStart, preToolUse,
postToolUse, and sessionEnd lifecycle events.

New files:
- Embedded hook templates: ctx-hooks.json + 8 scripts (.sh/.ps1)
- Asset readers: CopilotCLIHooksJSON(), CopilotCLIScripts()
- WriteCopilotCLIHooks() in hook command run.go
- Feature matrix spec: specs/copilot-cli-integration.md

Constants added: ToolCopilotCLI, CLI event names, asset paths,
text description keys, write output functions.

DCO-1.1-Signed-off-by: ersan bilik <ersanbilik@gmail.com>

Signed-off-by: ersan bilik <ersanbilik@gmail.com>
Add AGENTS.md generation (ctx hook agents --write) with universal
agent instructions readable by all AI coding tools. Add Copilot CLI
extras: .github/agents/ctx.md custom agent and
.github/instructions/context.instructions.md path-specific guide.

WriteCopilotCLIHooks now also generates agents/ctx.md and
instructions/context.instructions.md alongside the hook scripts.
WriteAgentsMd supports marker-based merge with existing AGENTS.md.

New templates: agents.md, agents-ctx.md, instructions-context.md
New constants: ToolAgents, AgentsMarker*, text keys, write functions

DCO-1.1-Signed-off-by: ersan bilik <ersanbilik@gmail.com>

Signed-off-by: ersan bilik <ersanbilik@gmail.com>
Phase 3A: MCP Registration
- Add ensureCopilotCLIMCPConfig() to register ctx MCP server in
  ~/.copilot/mcp-config.json (respects COPILOT_HOME env var)
- Merge-safe: reads existing config, adds ctx server, writes back
- Uses official Copilot CLI mcpServers format with type=local
- Called automatically from WriteCopilotCLIHooks()
- Add DirCopilotHome, EnvCopilotHome, FileMCPConfigJSON constants

Phase 3B: Copilot CLI Session Parser
- Create CopilotCLIParser implementing SessionParser interface
- Add ToolCopilotCLI constant to config/session/tool.go
- Register parser in registeredParsers (parser.go)
- Add CopilotCLISessionDirs() scanning ~/.copilot/sessions/,
  ~/.copilot/history/, and Windows LOCALAPPDATA paths
- Wire into findSessionsWithFilter() query scanning (query.go)
- Extract osWindows constant to satisfy goconst linter
- Parser skeleton ready for format discovery as Copilot CLI
  session storage format stabilizes

Ref: specs/copilot-cli-integration.md Phase 3
Signed-off-by: ersan bilik <ersanbilik@gmail.com>
Add 5 embedded skill templates for GitHub Copilot CLI integration:
  - ctx-status: context summary and health check
  - ctx-recall: session history browsing
  - ctx-drift: context drift detection
  - ctx-compact: archive completed tasks
  - ctx-next: advance to next task

Each skill is written to .github/skills/<name>/SKILL.md when
'ctx hook copilot-cli --write' is invoked. Skills use YAML
frontmatter (name, description) with Markdown instructions,
following the Copilot CLI skill format.

Wiring:
  - embed.go: glob for hooks/copilot-cli/skills/*/SKILL.md
  - agent.go: CopilotCLISkills() reader function
  - run.go: writeCopilotCLISkills() called from WriteCopilotCLIHooks()
  - asset.go: DirHooksCopilotCLISkills constant
  - hook.go: DirGitHubSkills, FileSKILLMd constants
  - text/hook.go: DescKeyWriteHookCopilotCLISkills key
Signed-off-by: ersan bilik <ersanbilik@gmail.com>
@bilersan bilersan force-pushed the feat/copilot-governance branch from 1d54a06 to 31fdab2 Compare March 25, 2026 22:00
@bilersan bilersan changed the title feat: Copilot governance engine, VS Code extension, and recall parser feat: Copilot governance engine, VS Code extension, recall parser, and Copilot CLI integration Mar 25, 2026
@bilersan bilersan requested a review from josealekhine March 25, 2026 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants