Skip to content

feat(engine): config mutation API — cog_read/write/rollback_config + HTTP#14

Merged
chazmaniandinkle merged 1 commit intocogos-dev:mainfrom
chazmaniandinkle:feat/config-mutation
Apr 21, 2026
Merged

feat(engine): config mutation API — cog_read/write/rollback_config + HTTP#14
chazmaniandinkle merged 1 commit intocogos-dev:mainfrom
chazmaniandinkle:feat/config-mutation

Conversation

@chazmaniandinkle
Copy link
Copy Markdown
Contributor

Summary

Config mutation surface — closes Agent F's #5 critical MCP blindspot. Claude-as-primary-UI can now read, patch, and roll back `kernel.yaml` via MCP or HTTP. Design spec produced by Agent O earlier this session.

What landed

  • Three MCP tools: `cog_read_config`, `cog_write_config`, `cog_rollback_config`.
  • `cogos://config` MCP resource alongside the read tool.
  • Three HTTP routes: `GET /v1/config`, `PATCH /v1/config`, `POST /v1/config/rollback`.

Design highlights

  • RFC 7396 JSON merge-patch semantics — explicit `null` deletes a key; missing keys preserved.
  • v1 = write-to-disk + `requires_restart: true`. The `*Config` pointer threads through subsystems without a mutex, so hot-reload is v1.5 work (avoids data-race risk under `-race`).
  • Atomic write via a duplicated `atomicWriteConfigFile` helper. The root-package `atomicWriteFile` couldn't be imported from `internal/engine` (package boundary); duplication is a collapse candidate for Track 5's future root-package sweep.
  • `.bak-` rotation keeping 10 for rollback.
  • Write-side validation refuses to overwrite a corrupt `kernel.yaml` — forces the operator through `cog_rollback_config` first. Extends the design's "validate before persisting" rule to also cover pre-existing-unparseable state.
  • `LoadConfig`'s silent parse-error swallow is left untouched (per design, intentional ADR history); only the write path is strict.

Test plan

Not in scope

  • Hot reload of running kernel (v1.5)
  • Auth on config writes (kernel is localhost-trusted by design)

Design reference

Agent O's design CogDoc: `~/cog-workspace/.cog/mem/semantic/surveys/2026-04-21-consolidation/agent-O-config-mutation-design.cog.md`

…HTTP

Implements the v1 Config Mutation API per Agent O's design (closes Agent F
gap #5). Exposes kernel.yaml read + RFC 7396 merge-patch write + backup
rollback over both MCP (three tools + one resource) and HTTP (GET / PATCH /
POST /v1/config[/rollback]).

Key decisions per spec:
- RFC 7396 merge-patch semantics: missing keys preserved, explicit null
  deletes. Handler decodes raw JSON (preserves null-vs-absent distinction).
- Pre-write validation against the merged state (not the patch alone).
  Invalid patches return violations without touching disk.
- Atomic write via temp-file + rename. Dedicated package-scope helper in
  config_write.go; hook_working_memory.go's helper lives in package main
  and is unreachable from internal/engine, so we duplicate rather than
  promote.
- Rotating .bak-<timestamp> backups, keep the most recent 10.
- Pre-write refusal when the existing file is unparseable — LoadConfig's
  silent-swallow behaviour is preserved on read (not load-bearing) but we
  refuse to clobber corrupt YAML without an explicit rollback first.
- Global writeConfigMu serializes concurrent writes; last-writer-wins.
- v1 is write-to-disk + requires_restart: true. Reload-safety hint lives
  in the response's changed_fields so callers can reason about v1.5.
- Authentication: none — kernel assumes a trusted localhost caller.

20 new tests (14 unit + 6 wire) cover: full/sparse/null/v3 patches,
reload-safe hint, dry-run, validation rejection for port + heartbeat,
atomic write, 10-deep backup rotation, corrupt-file refusal, concurrent
writes, read+defaults, MCP roundtrip (read/write/rollback/resource), HTTP
GET/PATCH success + validation-failure shapes.

Tests pass with -race. No hot-reload, no auth — both are out-of-scope per
design.
@chazmaniandinkle chazmaniandinkle merged commit e801e06 into cogos-dev:main Apr 21, 2026
5 checks passed
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