Compare commits

...

87 Commits

Author SHA1 Message Date
ecd86fa357 Merge pull request 'smoke: reconcile assertions with ungated-global + ambient clock' (#140) from fix/smoke-ungated-global-reconcile into main
Some checks failed
CI/CD / lint (push) Successful in 1m16s
CI/CD / test (push) Successful in 1m27s
CI/CD / typecheck (push) Successful in 3m11s
CI/CD / smoke (push) Failing after 1m59s
CI/CD / build (push) Successful in 2m25s
CI/CD / publish (push) Has been skipped
2026-10-09 15:06:39 +00:00
Michal
ed54aa1464 fix(smoke): reconcile assertions with ungated-global + ambient clock
Some checks failed
CI/CD / lint (pull_request) Successful in 1m19s
CI/CD / test (pull_request) Successful in 1m27s
CI/CD / typecheck (pull_request) Successful in 3m12s
CI/CD / smoke (pull_request) Failing after 2m0s
CI/CD / build (pull_request) Successful in 4m51s
CI/CD / publish (pull_request) Has been skipped
Three smoke tests predated PR #137 (ungated-global pre-gate servers) and
the ambient-context clock, and were never exercised against a prod that
had calcmcp seeded — the 5415060 fix was cut over via kubectl, bypassing
the suite. They failed on today's a783869 deploy:

- proxy-pipeline: a fresh gated session legitimately sees begin_session
  plus the ungated-global project's tools. Now discovers the gate-exempt
  server set and asserts nothing ELSE leaks pre-gate.
- tool-drilldown: get_current_time is a router-dispatched virtual tool
  (ambient clock) that never paginates; added to the exempt set.
- backup: command is optional for dockerImage STDIO servers (entrypoint
  images — calcmcp, dav-michal, jmap-michal run with none). The real
  contract is fidelity: every live server WITH a command must keep it
  through backup. Asserted that instead.
- audit: 'Gated (only begin_session)' label said something the >=1 check
  never verified; renamed.

Full suite against live a783869: 32 files, 181/181 green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-10-09 16:06:24 +01:00
a783869e7c Merge pull request 'agentic-teams: epics, tasks, git forges, agent authority' (#139) from feat/agentic-teams into main
Some checks failed
CI/CD / lint (push) Successful in 1m16s
CI/CD / test (push) Successful in 1m30s
CI/CD / typecheck (push) Successful in 3m15s
CI/CD / smoke (push) Failing after 2m1s
CI/CD / build (push) Successful in 2m16s
CI/CD / publish (push) Has been skipped
2026-10-09 14:47:33 +00:00
Michal
527983d87e fix(db-tests): seed a SecretBackend for Secret model tests
Some checks failed
CI/CD / lint (pull_request) Successful in 1m17s
CI/CD / test (pull_request) Successful in 1m36s
CI/CD / typecheck (pull_request) Successful in 3m13s
CI/CD / smoke (pull_request) Failing after 2m4s
CI/CD / build (pull_request) Successful in 4m43s
CI/CD / publish (pull_request) Has been skipped
The four Secret tests in models.test.ts created rows via raw
prisma.secret.create without a backendId, so the schema default ''
violated Secret_backendId_fkey (that default only exists so 'db push'
can add the column to pre-existing rows; real writes always carry a
valid FK). Seed a plaintext backend per test, the same shape
bootstrapSecretBackends creates, and clear SecretBackend rows in
clearAllTables so nothing leaks between tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-10-09 15:40:11 +01:00
Michal
24f202cb79 Merge origin/main into feat/agentic-teams
Conflict in src/mcpd/src/main.ts: both sides touched the import block.
Kept this branch's mapUrlToPermission plus main's bootstrapUngatedGlobal;
dropped main's SET_OWN_PASSWORD_OPERATION import — this branch moved that
usage into middleware/url-permission.ts, which imports it itself.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-10-09 15:36:32 +01:00
5415060de6 Merge pull request 'fix(ungated-global): create settings secret with a valid backend FK' (#138) from fix/ungated-global-secret-fk into main
Some checks failed
CI/CD / lint (push) Successful in 1m18s
CI/CD / test (push) Successful in 1m23s
CI/CD / typecheck (push) Successful in 3m15s
CI/CD / smoke (push) Failing after 1m56s
CI/CD / build (push) Successful in 5m7s
CI/CD / publish (push) Has been skipped
2026-10-03 00:58:57 +00:00
Michal
fe00f42bce fix(ungated-global): create settings secret with a valid backend FK
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m19s
CI/CD / test (pull_request) Failing after 27s
CI/CD / lint (pull_request) Successful in 2m50s
CI/CD / smoke (pull_request) Has been skipped
CI/CD / build (pull_request) Has been skipped
CI/CD / publish (pull_request) Has been skipped
bootstrapUngatedGlobal wrote its seed marker into the mcpctl-system-settings
Secret, creating the row with the default backendId '' when it did not exist.
The Secret_backendId_fkey constraint is enforced at insert time, so on a DB
where that secret was absent, mcpd crash-looped on startup (P2003).

Fix: run the seed after bootstrapSecretBackends (so the default plaintext
backend exists) and set backendId to the resolved default on create. If no
backend exists at all, skip writing the marker rather than crash.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-03 01:57:37 +01:00
7bf9d49a02 Merge pull request 'ungated-global: system project whose servers ride every project, pre-gate' (#137) from feat/ungated-global into main
Some checks failed
CI/CD / lint (push) Successful in 1m13s
CI/CD / test (push) Successful in 1m25s
CI/CD / typecheck (push) Successful in 3m6s
CI/CD / smoke (push) Failing after 1m58s
CI/CD / build (push) Successful in 2m11s
CI/CD / publish (push) Has been skipped
2026-10-03 00:46:08 +00:00
Michal
7b70c91c41 ungated-global: system project whose servers ride every project, pre-gate
Some checks failed
CI/CD / lint (pull_request) Successful in 1m12s
CI/CD / test (pull_request) Successful in 1m26s
CI/CD / typecheck (pull_request) Successful in 3m22s
CI/CD / smoke (pull_request) Failing after 1m57s
CI/CD / build (pull_request) Successful in 5m15s
CI/CD / publish (pull_request) Has been skipped
New seed-once system project `ungated-global`: every server attached to it
is merged into every project's MCP endpoint in front of the gate (listed
and callable before begin_session), and its project prompt is prepended to
every project's instructions.

- mcpd: bootstrapUngatedGlobal seeds the project once-ever (marker key
  `ungatedGlobalSeeded` in mcpctl-system-settings), with default server
  `calcmcp` (patched CalcMCP fork, mysources.co.uk/michal/calcmcp:1.1.0)
  and a "never do math yourself, always use calcmcp" prompt. Deletions are
  durable: `mcpctl delete server calcmcp -p ungated-global` stays deleted.
- mcpd: GET /projects/:id/servers appends global servers flagged
  `ungatedGlobal: true` (direct attachment wins, no self-merge);
  GET /projects/:id/instructions prepends the global prompt and lists the
  global servers.
- mcplocal: discovery surfaces the flagged names; the gate plugin exempts
  them — gated tools/list returns begin_session + exempt tools, calls to
  them pass through without ungating, and initialize instructions list
  them under "Available immediately (no begin_session required)".
- templates/calcmcp.yaml so a deleted default can be re-created by hand.
- docs/ungated-global.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-10-03 01:45:44 +01:00
23c8218811 Merge pull request 'templates/ttrss: image 1.0.1 (Sec-Fetch fix + isError)' (#136) from fix/ttrss-template-101 into main
Some checks failed
CI/CD / lint (push) Successful in 1m11s
CI/CD / test (push) Successful in 1m40s
CI/CD / typecheck (push) Successful in 2m56s
CI/CD / smoke (push) Failing after 1m58s
CI/CD / build (push) Successful in 2m10s
CI/CD / publish (push) Has been skipped
2026-10-02 22:56:26 +00:00
Michal
b2aaa9d18d templates/ttrss: image 1.0.1 (node:http Sec-Fetch fix + isError)
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m17s
CI/CD / test (pull_request) Successful in 1m24s
CI/CD / lint (pull_request) Successful in 2m51s
CI/CD / smoke (pull_request) Failing after 1m57s
CI/CD / build (pull_request) Successful in 5m11s
CI/CD / publish (pull_request) Has been skipped
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-02 23:56:13 +01:00
45b875ae2c Merge pull request 'ambient context: give every project's model a clock' (#135) from feat/ambient-context into main
Some checks failed
CI/CD / lint (push) Successful in 1m16s
CI/CD / test (push) Successful in 1m25s
CI/CD / typecheck (push) Successful in 3m16s
CI/CD / smoke (push) Failing after 1m57s
CI/CD / build (push) Successful in 2m18s
CI/CD / publish (push) Has been skipped
2026-10-01 23:33:38 +00:00
Michal
979e9720c0 ambient context: give every project's model a clock
Some checks failed
CI/CD / lint (pull_request) Successful in 1m16s
CI/CD / test (pull_request) Successful in 1m26s
CI/CD / typecheck (pull_request) Successful in 3m25s
CI/CD / smoke (pull_request) Failing after 1m57s
CI/CD / build (pull_request) Successful in 5m9s
CI/CD / publish (pull_request) Has been skipped
Models consuming projects have no idea what day it is. Three always-on
layers from mcplocal plus one from mcpd:

- initialize instructions and the begin_session briefing carry
  '[context] now: <date/time> — project: <name>' (frozen at session
  start by design);
- a get_current_time virtual tool (ungated catalogs only, same pattern
  as read_prompts);
- every tools/call result gets a trailing '[context] now: …' content
  item at the endpoint send site — the only chokepoint that also covers
  virtual tools, gate intercepts and failure results, and the layer
  that keeps a days-old chat session current;
- mcpd agent/project chat system blocks append the same line each turn.

Time renders as UTC plus an optional local timezone from
MCPLOCAL_AMBIENT_TZ (IANA name; both services read it; invalid or unset
means UTC only). ~15 tokens per stamp, minute precision.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-10-02 00:33:20 +01:00
f9ef4ebcb9 Merge pull request 'templates: jmap, dav, karakeep, ttrss for personal-service projects' (#134) from feat/personal-service-templates into main
Some checks failed
CI/CD / lint (push) Successful in 1m12s
CI/CD / test (push) Successful in 1m49s
CI/CD / typecheck (push) Successful in 3m13s
CI/CD / build (push) Successful in 2m36s
CI/CD / smoke (push) Failing after 3m51s
CI/CD / publish (push) Has been skipped
2026-10-01 22:59:23 +00:00
Michal
1180bbdc1d templates/dav: baked stdio image instead of npx
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m19s
CI/CD / lint (pull_request) Successful in 2m44s
CI/CD / test (pull_request) Successful in 1m25s
CI/CD / build (pull_request) Successful in 2m46s
CI/CD / smoke (pull_request) Failing after 3m48s
CI/CD / publish (pull_request) Has been skipped
dav-mcp declares tsdav/tsdav-utils as GitHub-URL deps — npx on the
git-less node-runner dies with 'spawn git ENOENT' (observed: dav-michal
CrashLoopBackOff on first deploy). Upstream's ghcr image PID-1s the HTTP
server, so mysources.co.uk/michal/dav-mcp bakes the install and execs
src/server-stdio.js.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-10-01 23:47:12 +01:00
Michal
709068ce68 templates/jmap: drafts3 image + JMAP_REBASE_SESSION_URLS
Some checks failed
CI/CD / lint (pull_request) Successful in 1m14s
CI/CD / test (pull_request) Successful in 1m28s
CI/CD / typecheck (pull_request) Successful in 2m53s
CI/CD / smoke (pull_request) Failing after 2m5s
CI/CD / build (pull_request) Successful in 4m43s
CI/CD / publish (pull_request) Has been skipped
Stalwart's session advertises publicUrl-absolute endpoints; over the
ClusterIP those hairpin through Envoy and 403. Verified in-cluster:
apiUrl came back as https://smtp.ad.itaz.eu/jmap/.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-30 01:42:05 +01:00
Michal
c1f0117b80 templates: jmap, dav, karakeep, ttrss for personal-service projects
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m20s
CI/CD / lint (pull_request) Successful in 2m38s
CI/CD / test (pull_request) Successful in 1m28s
CI/CD / build (pull_request) Successful in 2m26s
CI/CD / smoke (pull_request) Failing after 3m16s
CI/CD / publish (pull_request) Has been skipped
jmap and ttrss are custom images (mysources.co.uk/michal/{jmap-mcp,ttrss-mcp});
dav-mcp and @karakeep/mcp run on the node runner. Health checks are real
backend calls. Applied live 2026-09-30; these seeds keep the next mcpd
image rebuild from reverting them.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-30 01:35:43 +01:00
0515f99e17 Merge pull request 'fix(mcplocal): rebind upstream auth token on every project refresh' (#133) from fix/stale-upstream-token into main
Some checks failed
CI/CD / lint (push) Successful in 1m11s
CI/CD / test (push) Successful in 1m25s
CI/CD / typecheck (push) Successful in 2m46s
CI/CD / smoke (push) Failing after 1m58s
CI/CD / build (push) Successful in 2m10s
CI/CD / publish (push) Has been skipped
2026-09-22 22:46:02 +00:00
Michal
6fbd9c19fc fix(mcplocal): rebind upstream auth token on every project refresh
Some checks failed
CI/CD / lint (pull_request) Successful in 1m13s
CI/CD / test (pull_request) Successful in 1m23s
CI/CD / typecheck (pull_request) Successful in 2m35s
CI/CD / smoke (pull_request) Failing after 2m0s
CI/CD / build (pull_request) Successful in 4m24s
CI/CD / publish (pull_request) Has been skipped
A cached project router outlives the caller's bearer token. syncUpstreams
only created a McpdUpstream (which binds the token at construction) for
server names not already registered, so after a token rotation every
pre-rotation upstream kept posting the old bearer: six attached, healthy
servers 401'd on tools/list forever — no error anywhere the client could
see — while the two servers attached after the rotation worked.

- discovery.ts: rebuild every upstream on every sync, so it always carries
  the current caller token and the current server id (also fixes a stale
  serverId after delete+recreate under the same name).
- router.ts: record per-server discovery failures with their messages and
  expose them (getToolDiscoveryFailures).
- gate.ts: a partial catalog is now loud — initialize instructions and the
  begin_session response name every attached server that contributed no
  tools and the error that silenced it.
- tests: regression tests for token rebinding and serverId refresh.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-22 23:44:52 +01:00
0076fa0d37 Merge pull request 'feat(servers): per-server memory ceiling, because 512Mi OOMKills silently' (#132) from feat/server-memory-limit into main
Some checks failed
CI/CD / typecheck (push) Successful in 1m17s
CI/CD / lint (push) Successful in 2m22s
CI/CD / test (push) Successful in 1m22s
CI/CD / smoke (push) Failing after 1m56s
CI/CD / build (push) Successful in 3m59s
CI/CD / publish (push) Has been skipped
2026-09-16 23:24:32 +00:00
Michal
859150770f feat(servers): per-server memory ceiling, because 512Mi OOMKills silently
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m16s
CI/CD / lint (pull_request) Successful in 2m16s
CI/CD / test (pull_request) Successful in 1m24s
CI/CD / build (pull_request) Successful in 2m10s
CI/CD / smoke (pull_request) Failing after 3m15s
CI/CD / publish (pull_request) Has been skipped
Server pods have always had a hardcoded 512Mi limit. That is right for a
server that proxies an API and fatal for one that drives a browser: the
`docs` server (docs-mcp-server, which scrapes with headless Chromium)
idles at ~228Mi and crosses 512Mi within seconds of its first scrape.

An OOMKill is the quietest failure we have. The kernel kills it, the pod
restarts, the readiness probe passes, and the instance reads `healthy`
again — while the six scrape jobs whose queue lived in memory are gone
and the index has 17 pages in it. Nothing is logged, because the process
never got to say anything.

memoryLimitMb is declared per server, in MiB, and converted to bytes in
the container spec. NULL keeps DEFAULT_MEMORY_LIMIT, so every existing
server is bit-for-bit unchanged — a bigger default would have cost real
memory on every node for servers that do not need it.

  mcpctl create server docs --memory-limit-mb 2048 --force

Tests assert the two places a new column dies quietly: the repository's
field-by-field mapping (a column not mapped there returns "patched" and
changes nothing) and the spec built in instance.service. docs/reliability
gains the OOMKill section — the `lastState.terminated.reason` check is
what turns "it restarted again" into an answer.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JPjtnE6Gd343oRNtMU9Bcd
2026-09-17 00:24:18 +01:00
3e534854f4 Merge pull request 'fix(templates): search templates stop probing with searches; add firecrawl' (#131) from fix/search-templates into main
Some checks failed
CI/CD / lint (push) Successful in 1m10s
CI/CD / test (push) Successful in 1m22s
CI/CD / typecheck (push) Successful in 2m50s
CI/CD / smoke (push) Failing after 1m59s
CI/CD / build (push) Successful in 2m7s
CI/CD / publish (push) Has been skipped
2026-09-15 21:18:28 +00:00
Michal
61e52403a3 fix(templates): search templates stop probing with searches; add firecrawl
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m17s
CI/CD / test (pull_request) Successful in 1m24s
CI/CD / lint (pull_request) Successful in 2m56s
CI/CD / smoke (pull_request) Failing after 1m54s
CI/CD / build (pull_request) Successful in 4m19s
CI/CD / publish (pull_request) Has been skipped
Readiness probes ran real searches every interval: searxng's
searxng_web_search probe fanned a query out to every engine 1,440 times a
day, billing API-key engines (braveapi, kagi), and duckduckgo's `search`
probe scraped DuckDuckGo from the same IP agents search from.

- searxng: probe searxng_instance_info, which reads /config only
- duckduckgo: probe fetch_content on example.com every 300s
- firecrawl (new): firecrawl-mcp against a self-hosted or cloud
  Firecrawl, for reading pages as main-content markdown; probe
  firecrawl_scrape on example.com every 300s

Every probe still names a readiness tool, as templates.test.ts requires;
each was run against the real package before being written down.
docs/web-search.md now pairs searxng (search) with firecrawl (reading)
and says why duckduckgo does not hold up under agent traffic.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JPjtnE6Gd343oRNtMU9Bcd
2026-09-15 22:16:17 +01:00
Michal
70bc7f1598 Merge origin/main into feat/agentic-teams
124 commits of main against 33 of agentic-teams. 22 files overlapped, 7
conflicted. Three needed real reconciliation rather than a side-pick:

**The `volumes` collision.** Both branches independently added a `volumes`
field to ContainerSpec meaning different things: main's is persistent storage
keyed by a server-derived `claimName` that deliberately outlives an instance;
mine is emptyDir scratch shared between an init container and the main
container. Worse, each emitted the pod-level `volumes` list from its own
helper, so spreading both would have silently kept only the last — a server
with a PVC would have lost it the moment a session scratch volume appeared.

Resolved by renaming the unreleased side: mine is now `scratchVolumes`, main's
keeps `volumes` since it is already shipped and in use. `buildPodVolumes` now
owns the whole pod-level list and emits both kinds; `buildPodExtras` is init
containers only. fsGroup/securityContext stays tied to the PVC case, because
an emptyDir is created writable by the container's own user and chowning it
is work with no purpose. Five tests pin the coexistence so a later change
cannot drop one kind again.

**The extracted permission mapper.** I moved `mapUrlToPermission` out of
main.ts into middleware/url-permission.ts so it could be tested; main kept it
inline and kept editing it. Kept the extraction and ported main's one change
across — the `/api/v1/audit/events/purge` mapping onto the existing
`audit-purge` operation. Verified rule-by-rule that the extracted version is a
superset: all 15 of main's rules plus the 5 agentic-teams ones.

**The NUL byte in create.ts.** Both branches fixed the same defect
independently, mine as `''` and main's as `'\0'`. Took main's.

The rest were one-liners: .gitignore comment wording, and an add/add on
proxy-long-running.test.ts where main's copy carries a progressive-delivery
test mine lacked — took main's in full.

Not changed, but worth flagging: main's generateDeploymentSpec hardcodes
`automountServiceAccountToken: false` while generatePodSpec honours
`spec.automountServiceAccountToken`. That asymmetry is main's own and predates
this merge, so it is left exactly as-is rather than altered inside a merge
commit.

Verified: 3138 tests pass (237 files), build clean, prisma schema valid.
Lint 809/80 vs 794/77 pre-merge — the delta is main's own debt; every file
touched during conflict resolution was checked and introduces no new errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-09-06 23:40:04 +01:00
ad00e49262 Merge pull request #130: smoke _resultId regex
Some checks failed
CI/CD / lint (push) Successful in 2m17s
CI/CD / typecheck (push) Successful in 1m24s
CI/CD / test (push) Successful in 3m14s
CI/CD / smoke (push) Failing after 2m40s
CI/CD / build (push) Successful in 2m24s
CI/CD / publish (push) Has been skipped
2026-09-05 17:04:09 +00:00
Michal
22d8e13390 fix(smoke): match the _resultId format the pipeline actually emits
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m23s
CI/CD / lint (pull_request) Successful in 2m19s
CI/CD / test (pull_request) Successful in 1m41s
CI/CD / smoke (pull_request) Failing after 2m6s
CI/CD / build (pull_request) Successful in 2m22s
CI/CD / publish (pull_request) Has been skipped
The drill-down checks matched /_resultId:\s*(\S+)/ — the legacy paginator's
JSON spelling. The content pipeline writes _resultId="pm-abc", so the regex
found nothing: "Has _resultId for drill-down" reported a false failure on
every run, and the drill-down tests that used the same regex to extract an id
skipped themselves as "not large enough for section-split". Both were
invisible in a green suite.

One shared extractResultId() in the smoke harness now accepts either
spelling and returns the bare id, so the ad-hoc character strip on the
captured group goes away too. proxy-pipeline's check block reports 21/21
instead of 20 with a phantom failure.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQr5Z9gYrqBQXTGBBuemZ2
2026-09-05 18:03:56 +01:00
b5d0234d0b Merge pull request #129: drill-down required params
Some checks failed
CI/CD / typecheck (push) Successful in 1m22s
CI/CD / lint (push) Successful in 2m13s
CI/CD / test (push) Has started running
CI/CD / smoke (push) Has been cancelled
CI/CD / build (push) Has been cancelled
CI/CD / publish (push) Has been cancelled
2026-09-05 16:59:46 +00:00
Michal
0e12637271 fix(mcplocal): stop upstream required params from blocking a drill-down
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m27s
CI/CD / lint (pull_request) Successful in 2m22s
CI/CD / test (pull_request) Successful in 1m31s
CI/CD / smoke (pull_request) Failing after 2m41s
CI/CD / build (pull_request) Successful in 2m21s
CI/CD / publish (pull_request) Has been skipped
A paginated tool result tells the model to call the same tool again with
only _resultId/_section. The advertised schema still carried the upstream's
own `required` list, so a client that validates arguments rejected exactly
that call — "Received tool input did not match expected schema" — and the
model concluded pagination was broken and gave up
(websearch/fetch_content requires `url`; verified live on llm-model-tester).

`required` is ANDed with everything else in a schema, so no property
declaration could rescue it. It now becomes an alternation: either the
upstream's requirements (a fresh call) or `_resultId` (a re-read, which is
answered from cache and never reaches the upstream). Validators that
understand anyOf enforce that; naive ones that only read top-level
`required` now find none and accept both. Schemas that already carry a
combinator get the alternation appended to `allOf`, and a schema with no
required params is left untouched.

Covered by unit tests on a required-bearing upstream schema plus a smoke
assertion that no advertised tool keeps a drill-down-blocking `required`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQr5Z9gYrqBQXTGBBuemZ2
2026-09-05 17:58:57 +01:00
33d7007af5 Merge pull request #128: learn sampling-param rejection
Some checks failed
CI/CD / typecheck (push) Successful in 1m20s
CI/CD / lint (push) Successful in 2m32s
CI/CD / test (push) Successful in 1m26s
CI/CD / smoke (push) Failing after 2m3s
CI/CD / build (push) Successful in 4m34s
CI/CD / publish (push) Has been skipped
2026-08-25 23:12:08 +00:00
bc7eb5a0ad fix(mcplocal): learn which models reject sampling params, don't hardcode them
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m22s
CI/CD / lint (pull_request) Successful in 2m34s
CI/CD / test (pull_request) Successful in 1m28s
CI/CD / build (pull_request) Successful in 2m27s
CI/CD / smoke (pull_request) Failing after 3m6s
CI/CD / publish (pull_request) Has been skipped
Auto-following to the newest Opus moved the failure rather than removing it.
The deploy's own smoke output showed it: 404 "model not found" became

  HTTP 400: `temperature` is deprecated for this model.

Anthropic removed temperature/top_p/top_k on the current generation (Opus 5,
Sonnet 5, Opus 4.7/4.8, Fable 5), and the adapter sends temperature: 0
unconditionally. So the gate's prompt-selection was still degrading on every
call -- just with a different status code.

A list of which models accept sampling would rot exactly the way the pinned
model ids did, which is the whole thing this branch is trying to stop. So the
provider learns it instead: the first 400 naming a sampling parameter drops it
and retries, and remembers the model so every later call omits it up front.
One wasted call, once, rather than a hardcoded table to maintain.

The match is deliberately narrow -- a 400 must actually name temperature/top_p/
top_k. An unrelated 400 (missing max_tokens, bad schema) propagates untouched;
there is a test for that, because a broad match here would silently swallow
real request errors and retry them pointlessly.

Verified live: claude-opus-latest -> claude-opus-5, first attempt 400 on
temperature, retry succeeds past it. The retry then hit HTTP 429 -- the
personal OAuth token's rate limit, which is the pre-existing credential-tiering
issue, not this path.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
2026-08-26 00:11:50 +01:00
cd20d8b980 Merge pull request #127: bounded, legible MCP failures
Some checks failed
CI/CD / typecheck (push) Successful in 1m24s
CI/CD / lint (push) Successful in 2m32s
CI/CD / test (push) Successful in 1m32s
CI/CD / smoke (push) Has started running
CI/CD / publish (push) Has been cancelled
CI/CD / build (push) Has been cancelled
2026-08-25 23:03:50 +00:00
47d7c779d0 feat: per-server tool-call timeout, on the server resource
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m25s
CI/CD / lint (pull_request) Successful in 2m39s
CI/CD / test (pull_request) Successful in 1m29s
CI/CD / smoke (pull_request) Failing after 3m6s
CI/CD / build (pull_request) Successful in 2m27s
CI/CD / publish (pull_request) Has been skipped
Completes the plan's last item. The deadline shipped with a global default;
this makes it overridable per server, where the knowledge actually lives -- a
server with genuinely slow tools declares its own budget instead of forcing the
global up for everyone.

Source of truth is the server resource, following healthCheck exactly:
Prisma column + migration (NULL keeps today's behaviour, so no existing server
changes), zod validation on create and update, the repository, an
`--tool-call-timeout` flag mirroring `--health-check-timeout`, and the apply
schema so `apply -f` accepts what `get server -o yaml` emits.

That round-trip needed care: get emits `toolCallTimeoutSeconds: null` for every
server without an override, so the apply schema is nullable, not merely
optional -- otherwise the very first `get -o yaml | apply -f` on an untouched
server would have failed validation.

Bounded at one hour. A deadline exists so a wedged call answers instead of
hanging; a value beyond an hour is indistinguishable from having none.

It reaches mcplocal through server discovery rather than the project-scoped
serverOverrides map, and is applied on EVERY sync rather than only at first
registration -- raising a server's timeout should take effect at the next
refresh, not require the upstream to be dropped and rebuilt.

The endpoint sees the wire name (`docmost_search`), so it decodes to the
canonical `server/tool` before resolving. The override is keyed by server, so
every tool on that server inherits it without being enumerated, and the
failure message quotes the deadline actually applied rather than the global.

Smoke tests (tests/smoke/bounded-failures.smoke.test.ts) cover the two
production faults against the live proxy: a stale session recovers instead of
stranding the client on an uncorrelatable 404, every request is answered, and
`mcpctl trace` responds for an unknown code.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
2026-08-26 00:03:06 +01:00
08b451b7aa feat(mcplocal): Anthropic models auto-follow the newest in their family
`claude-opus-4-20250514` was pinned as the heavy provider and had been
returning 404 on every gate ranking and every pagination title. The only
visible symptom was a fallback that looked like an ordinary one -- it surfaced
here because the degradation notice added earlier in this branch finally
printed the reason.

Checked against GET /v1/models: BOTH pins were dead. The fast tier's
claude-haiku-3-5-20241022 is gone too, so that tier had been silently 404ing
as well.

The provider's listModels() asserted "Anthropic doesn't have a models listing
endpoint" and returned four hardcoded dated ids. That endpoint does exist and
answers fine with the OAuth token this deployment uses; the hardcoded list was
simply stale, and a test pinned it in place.

So: `claude-<family>-latest` (or a bare `opus` / `sonnet` / `haiku` / `fable`)
now resolves against the live list. Exact ids pass through untouched, so
pinning still works when someone wants it.

Newest is decided by `created_at`, never by parsing the version out of the id.
That is not incidental: `claude-opus-4-5` sorts ABOVE `claude-opus-5` as a
string, and "4-5" parses as a larger minor than "5". There is a test for
exactly that trap.

Resolution is cached (12h, MCPCTL_ANTHROPIC_MODEL_TTL_MS) so it is not a
per-call network hop, and shared across instances since the model list is
account-wide. When the endpoint is unreachable it falls back to a pinned
known-good id per family and says so on stderr -- the map going stale can then
only cost availability, never correctness.

The constructor default was `claude-sonnet-4-20250514`, also retired; it now
tracks the family too.

Local config: heavy -> claude-opus-latest, fast -> claude-haiku-latest.
Verified live: opus-latest -> claude-opus-5, haiku-latest ->
claude-haiku-4-5-20251001, sonnet-latest -> claude-sonnet-5, and an exact id
passes through.

Also found while there: both anthropic entries were named "anthropic", and the
registry keys by name, so the second silently OVERWROTE the first and one
tier's model was discarded entirely. Renamed to anthropic-fast /
anthropic-heavy so NamedProvider keeps them distinct and the tier split is
real.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
2026-08-25 23:55:44 +01:00
14eb2623bc fix(cli): trace's 'slowest' names a step, not the aggregate
tool_call_trace and pipeline_execution are aggregates OF the stages, so letting
them compete always named the total and told you nothing. Verified live: now
reports 'slowest: paginate (252ms)' rather than 'tool_call_trace (509ms)'.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
2026-08-25 23:48:07 +01:00
89db09a12d feat(cli): mcpctl trace <code>, and bound the table it reads
The trace codes are now emitted in every deadline message and degradation
notice, so they need somewhere to go.

`mcpctl trace <code>` queries the audit events mcpd already stores and renders
the request as a waterfall: each step with its offset, duration, byte delta,
and any ⚠ degradation or ✗ error, then a summary naming the slowest step. No
new endpoint was needed -- GET /api/v1/audit/events?correlationId= already
existed and correlationId is already an indexed column; it just had nothing
writing meaningful values into it until this branch, and nothing reading it.

--strict exits non-zero when the trace contains an error or a degraded step, so
it composes into scripts. An empty result explains itself rather than printing
nothing: batches flush up to 5s late, old requests predate trace codes, and the
codes exclude I/L/O/U so a mistyped one is worth calling out.

Retention: AuditEvent had no prune at all. That was defensible while the table
was write-only; it is not now that a command reads it. Mirrors the AuditLog
convention exactly -- POST /api/v1/audit/events/purge, triggered rather than
scheduled, guarded by the existing audit-purge RBAC operation so it cannot be
granted by accident separately from the log purge. 30 days by default rather
than AuditLog's 90: these are several rows per MCP call, not a record of
administrative mutations.

Note for the plan's sake: I had assumed AuditLog retention was a scheduled job
registered in main.ts. It is not -- it is a manual endpoint. Mirroring what the
codebase actually does beat inventing a scheduler that exists for neither table.

completions are generated, so `trace` is registered in PROJECT_SCOPED_COMMANDS
and both shells regenerated; the freshness test passes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
2026-08-25 23:44:36 +01:00
b6e270ee48 feat(mcplocal): every request answers, even when the pipeline wedges
The LLM budgets bound the failure we actually hit. This is the backstop for the
ones they cannot see: a wedged plugin hook, a virtual-tool handler, an upstream
path without its own timeout.

transport.onmessage IS the whole request pipeline, and it writes to a socket
that has already been hijacked from Fastify. The SDK does not await it, so a
throw became an unhandled rejection and a hang became silence -- in both cases
the client got no response at all and waited out its own timeout. onmessage is
now structured so that reaching transport.send() is unconditional: the route
call is raced against a deadline, and the notification flush and the send each
carry their own catch, so a failure while flushing can no longer cost the client
its response.

transport.onerror was never assigned, so SDK-level transport errors were
swallowed entirely. It is now.

Shape of the answer is deliberate. A tools/call comes back as a SUCCESSFUL
result with isError and readable text naming the trace code -- the same shape
router.ts already uses for an expired _resultId, because a transport error tends
to surface as a hard failure while a tool error is something the model reads and
acts on. It also says the upstream may still be running, which is true and
matters. Other methods get a JSON-RPC error (-32001).

MCPLOCAL_TOOLCALL_DEADLINE_MS defaults to TOOLCALL_TIMEOUT_MS + 30s rather than
120s. The watchdog arms earlier in the request than mcpd's fetch does, so at
equal values it would always fire first, masking mcpd's specific
UpstreamTimeoutError with a generic message and cutting off pagination and the
rest of the post-processing. There is a test asserting the two stay ordered.

The pause queue is exempt, but not bypassed. It blocks until a human operator
releases, edits or drops a response, so the deadline SUSPENDS -- clock stopped,
then re-armed with whatever was left. A test asserts the re-arm, because an
exemption that forgot to re-arm would silently make every paused request
immortal.

End-to-end: an upstream that never settles now yields a response in under a
second carrying ⚠, the deadline, and `mcpctl trace <code>`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
2026-08-25 23:41:09 +01:00
0eca4148e3 fix(mcplocal): recreate a stale session instead of 404ing outside the envelope
Second of the two faults that both presented as a 1800s hang.

mcplocal keeps MCP sessions in memory only, so any restart -- a deploy, an RPM
install, scripts/release.sh -- invalidates every connected client's session id.
The response was a bare 404 {"error":"Session not found"}: 3.5ms server-side,
but it lands OUTSIDE the JSON-RPC envelope with no id, so the client cannot
correlate it to its request and simply waits out its own timeout. Every mcpctl
tool call in that session became a 30-minute stall while the server was
perfectly healthy and answering other traffic in milliseconds.

Now the session is recreated, keeping the id the client already holds -- a
re-handshake would change it and invalidate everything the client has cached.
The SDK assigns sessionId/_initialized only while handling an `initialize`
(webStandardStreamableHttp.js:419-420), so adoptSession() replays exactly those
two assignments. It reaches into SDK internals, so it fails LOUDLY: the guard
throws with a pointer to itself, and session-adopt.test.ts is the canary that
goes red at `pnpm test` rather than in production on the next SDK bump.

Recreation is not silent. Doing it quietly would hand an agent an ungated tool
list with no signal that its gate state had vanished, so the first tools/call
result carries the established "⚠ Session state unavailable (...)" notice
telling it to call begin_session again.

onsessioninitialized does NOT fire on the adopt path, so its body is factored
into registerSession() and called explicitly. That is the subtle part: skipping
it would have produced recreated sessions with null userName on every audit
event, silently.

Also fixed, and it is the CAUSE rather than the symptom: MCP clients close a
session with DELETE + Content-Type: application/json and an empty body, which
Fastify's default parser rejected with FST_ERR_CTP_EMPTY_JSON_BODY before the
route handler ran. sessions.delete() therefore never happened and every
"closed" session leaked -- manufacturing the stale-session condition this
commit handles. Fixing recreation without fixing teardown would have shipped
the workaround and kept the cause.

project-mcp-endpoint.test.ts's "returns 404 for unknown session ID" inverts to
"recreates an unknown session instead of 404ing it", and asserts the id comes
back unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
2026-08-25 23:36:27 +01:00
c06c34f0a2 feat(mcplocal): a trace code that actually reaches the audit trail
The plumbing for request tracing was all present and almost entirely inert.
correlationId was generated at the endpoint and threaded into traffic events,
but:

  - emitTrace() dropped it, so every durable tool_call_trace row had
    correlationId = null and could not be joined to anything;
  - ExecuteOptions.correlationId existed but its only production caller never
    passed it, making every stage_execution and pipeline_execution row
    unjoinable too -- dead code that looked like a feature;
  - plugin events (all three gate emits) had no access to it at all;
  - and the value itself was `<session-uuid>:<jsonrpc-id>`, which nobody can
    retype from a screenshot.

Also fixed: onToolCallBefore intercepts returned without emitTrace, so every
tool call made while a session was gated produced no trace whatsoever.

The trace code now IS the correlationId. Nothing parses the old format --
checked across mcpd's audit repository and route, mcplocal's router and traffic
capture, and the CLI console -- sessionId is its own audit column and the
JSON-RPC id is in the traffic body, so nothing is lost. AuditEvent.correlationId
is already an indexed Postgres column, so lookup works with no migration.
8 chars of Crockford base32 without I/L/O/U: readable aloud, typeable from a
screenshot, 40 bits (a collision shows two traces, it corrupts nothing).

Getting it to the plugins and the executor needed AsyncLocalStorage, not a
parameter. Both are constructed per SESSION, not per request -- processContent
is a closure with no RouteContext -- so threading an argument would have touched
PluginSessionContext, every plugin, ExecuteOptions and StageContext. ALS also
stays correct with two requests in flight on one session, which a mutable field
on the session-scoped context would not; there is a test for exactly that.

PluginContextImpl.emitAuditEvent auto-fills it the way it already auto-fills
sessionId, so this covers every gate event and every plugin written later
without a per-callsite edit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
2026-08-25 23:32:10 +01:00
c2a419b4f7 fix(mcplocal): one LLM budget per stage, so a loop cannot multiply it
A per-call timeout bounds one call. It does not bound a stage that calls the
LLM in a loop -- and summarize-tree does worse than loop: buildTree recurses to
maxDepth (3 by default) and iterates per section at every level, then
groupSections iterates again. Hundreds of sequential calls are reachable, so a
10s per-call cap is a multiplier, not a bound.

StageBudget is one wall-clock budget shared across everything a single stage
invocation does. Once it is gone the stage takes its deterministic path --
first-line excerpts, numbered pages -- and says so. The executor builds one per
stage from config.budgetMs ?? MCPCTL_STAGE_LLM_BUDGET_MS (30s) and disposes it
in a finally, so a long-lived mcplocal never accumulates timers.

Two details that matter more than they look:

  - A warm cache is never budget-gated. cachedSummarize and generatePageTitles
    used ctx.cache.getOrCompute, which makes the budget check impossible to
    place correctly; split into get/set so a cached summary -- which costs
    nothing -- is still returned when the budget is spent.
  - "No LLM configured" is NOT a degradation. It is a deliberate choice, and
    numbered pages are the expected output there, so it gets no warning. Only
    failures, timeouts and exhausted budgets do. Crying wolf on a working
    configuration would train people to ignore the notice.

summarize-tree's single-block path previously had no try/catch at all, so a
failure escaped the stage entirely and executor.ts discarded the whole stage's
work. It now degrades like the others.

Degradation is reported three ways, all from util/degrade.ts: the reason in the
content behind the established "⚠ <feature> unavailable (<reason>)" prefix, the
count of affected sections (the recursion can hit one exhausted budget dozens of
times, so the user needs the reason once plus what it cost), and
degraded/degradedReason on the stage_execution audit event -- previously a
degradation was invisible in the trace.

docs/reliability.md now names ONE helper rather than a list of compliant call
sites. That list is exactly how this drifted: the doc named the gate and
llm/pagination.ts, and the newer stages never joined it.

Tests: the 20-section recursion completes in ~300ms against a 300ms budget;
a warm cache still serves real titles with the budget exhausted.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
2026-08-25 23:29:45 +01:00
2c90f5971d fix(mcplocal): no LLM call can hang a tool call forever
docs/reliability.md has stated the rule since it was written: "LLM-optional
operations must be time-bounded, fall back deterministically, and report the
degradation". The gate and llm/pagination.ts obey it. The newer proxymodel
stages never adopted it -- stages/paginate.ts:72 awaited ctx.llm.complete()
with no timeout and no signal, and its try/catch caught errors, not hangs.

So when a provider hung rather than erroring, the promise never settled, the
tool call never returned, mcplocal never wrote to the already-hijacked socket,
and the client waited out its own 1800s timeout. In the journal that is three
POST /projects/docmost/mcp requests accepted and never answered -- the only
three such requests in 9,600 across every project, each followed immediately
by [llm-adapter] "trying next" and [paginate] "Smart page titles failed".

It read as a Docmost transport fault for two sessions. It was neither: Docmost
answers /search in 203ms, and through mcplocal on a fresh session in 461ms.
docmost_search returns ~14KB and crosses the pagination threshold; update_page
returns a small ack and does not. One tool paginated, one did not, and only the
paginating one could hang.

Bound it centrally in the adapter rather than at each call site, so a stage
that passes no options is still bounded and a stage written next year inherits
the guarantee:

  - LLMCompleteOptions gains optional budgetMs/perCallTimeoutMs/signal. Optional
    matters -- seven inline stubs implement LLMProvider structurally and a
    required member would break every one.
  - LLMProviderAdapter.complete() runs one budget with a deadline across the
    whole failover chain, each attempt capped at min(perCall, remaining). A
    per-provider timeout would have made the worst case N x timeout; wrapping
    the method would have killed failover mid-chain.
  - Every attempt now gets a real AbortSignal. anthropic and openai (which back
    vllm) honour it; deepseek/ollama/gemini ignore it, and withTimeout's race
    unblocks the caller anyway -- exactly what its doc comment anticipated.
  - A caller abort stops failover: if nobody is waiting, don't burn the chain.

util/degrade.ts generalises the gate's bounded -> fallback -> loud log ->
degradedReason pattern into bounded(), which cannot throw, so the deterministic
fallback is unconditional rather than something a catch block must remember.
gate.ts is refactored onto it; plugin-gate and router-gate pass UNMODIFIED (61
tests), which is the proof the generalisation is faithful.

anySignal() rather than AbortSignal.any: that landed in Node 20.3 and
package.json declares >=20.0.0.

proxymodel-llm-adapter.test.ts now asserts signal: expect.any(AbortSignal) --
every provider attempt being cancellable is the guarantee, so it is pinned.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
2026-08-25 23:23:16 +01:00
a87c4faf21 Merge pull request 'test(mcplocal): smoke asserts the wire-form tool-name contract' (#126) from test/wire-safe-smoke into main
Some checks failed
CI/CD / typecheck (push) Successful in 1m23s
CI/CD / lint (push) Successful in 2m49s
CI/CD / test (push) Successful in 1m28s
CI/CD / smoke (push) Failing after 2m0s
CI/CD / build (push) Successful in 4m32s
CI/CD / publish (push) Has been skipped
2026-08-25 21:06:29 +00:00
Michal
b4ad95ca66 test(mcplocal): smoke asserts the wire-form tool-name contract
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m21s
CI/CD / lint (pull_request) Successful in 2m43s
CI/CD / test (pull_request) Successful in 1m29s
CI/CD / build (pull_request) Successful in 2m41s
CI/CD / smoke (pull_request) Failing after 3m27s
CI/CD / publish (pull_request) Has been skipped
The smoke suite talks to the live mcplocal through the HTTP boundary, which
serves wire names since #124 — assertions expecting `favourite/`, `all/` and
`smoke-aws-docs/` prefixes now check the underscore wire form (config pins
stay canonical). These two files were the release-gate failures after the
bbd3188 rollout; 166/166 green against the live fleet after the update.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JaFvfHrQyUKCGv6o3N2Wir
2026-08-25 22:06:18 +01:00
bbd31883e8 Merge pull request 'fix(mcplocal): wire-form tool names in gate and favourite-index prose' (#125) from fix/wire-safe-prose into main
Some checks failed
CI/CD / typecheck (push) Successful in 1m25s
CI/CD / lint (push) Successful in 2m40s
CI/CD / test (push) Successful in 1m30s
CI/CD / smoke (push) Has been cancelled
CI/CD / build (push) Has been cancelled
CI/CD / publish (push) Has been cancelled
2026-08-25 20:59:00 +00:00
Michal
c014fcdd82 fix(mcplocal): name tools in wire form in gate and favourite-index prose
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m24s
CI/CD / lint (pull_request) Successful in 2m34s
CI/CD / test (pull_request) Successful in 1m44s
CI/CD / smoke (pull_request) Failing after 3m18s
CI/CD / build (pull_request) Successful in 2m30s
CI/CD / publish (pull_request) Has been skipped
Follow-up to #124: the boundary WireNameCodec serves underscore-joined
names, but the gate plugin's tool inventories (initialize instructions and
begin_session response) still listed canonical `server/tool`, and the
favourite-index instruction told the model to prefer `favourite/<tool>` —
prose naming functions the model cannot call. Inventories now run through
sanitizeWireName and the instruction describes the favourite_/all_ prefixes.

Cosmetic for routing (slash names still pass through the codec) but
load-bearing for tool selection: models copy names out of prose.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JaFvfHrQyUKCGv6o3N2Wir
2026-08-25 21:58:52 +01:00
7fbb827aa5 Merge pull request 'fix(mcplocal): serve OpenAI-safe tool names on the wire' (#124) from fix/wire-safe-tool-names into main
Some checks failed
CI/CD / lint (push) Successful in 1m20s
CI/CD / typecheck (push) Successful in 1m21s
CI/CD / test (push) Successful in 1m28s
CI/CD / build (push) Successful in 2m28s
CI/CD / smoke (push) Failing after 3m1s
CI/CD / publish (push) Has been skipped
2026-08-25 19:23:19 +00:00
Michal
eb0e97e76e test(mcplocal): end-to-end wire-name coverage on the project endpoint
Some checks failed
CI/CD / lint (pull_request) Successful in 1m18s
CI/CD / typecheck (pull_request) Successful in 1m21s
CI/CD / test (pull_request) Successful in 3m42s
CI/CD / smoke (pull_request) Failing after 3m5s
CI/CD / build (pull_request) Successful in 2m26s
CI/CD / publish (pull_request) Has been skipped
Drives /projects/:name/mcp over the real Streamable HTTP transport with a
fake websearch upstream: tools/list must serve `websearch_fetch_content`
(every name matching the OpenAI function-name charset), calling that wire
name must reach the upstream as bare `fetch_content`, and a legacy client
echoing the canonical `websearch/fetch_content` must still route.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JaFvfHrQyUKCGv6o3N2Wir
2026-08-25 20:23:09 +01:00
Michal
065ce02a60 fix(mcplocal): serve OpenAI-safe tool names on the wire
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m22s
CI/CD / lint (pull_request) Successful in 2m40s
CI/CD / test (pull_request) Successful in 1m27s
CI/CD / smoke (pull_request) Failing after 2m3s
CI/CD / build (pull_request) Successful in 4m58s
CI/CD / publish (pull_request) Has been skipped
The proxy namespaces tools as `server/tool` (and favourite-index presents
`favourite/<tool>` / `all/<server>/<tool>`). A `/` is not a valid character
in an OpenAI-style function name, so hosts that forward MCP tool names
verbatim as LLM function names depend on the model faithfully echoing an
illegal name. LibreChat did exactly that: deepseek-v4-flash intermittently
dropped the `websearch/` prefix, LibreChat's registry lookup failed, and it
reported "This tool's MCP server is temporarily unavailable" while nothing
was down — the calls never reached mcplocal at all (confirmed against the
AuditEvent table, 2026-08-25). Claude Code and the pi extension only dodge
this because they sanitize names client-side.

Fix at the HTTP boundary only: a WireNameCodec rewrites tools/list responses
to wire-safe names (`/` -> `_`, exact-match reverse map, deterministic
suffix on collision) and maps tools/call names back before routing. Wired
into both /mcp and /projects/:name/mcp. Everything inside the proxy —
routing maps, plugins, favourites config, audit events — keeps canonical
names, and unknown inbound names (legacy clients echoing slash names,
virtual tools) pass through unchanged, so existing clients keep working.

Codecs are keyed per project and outlive the router cache TTL so a client
can call a tool it listed minutes earlier; after a restart the client's
initialize-time tools/list repopulates the map.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JaFvfHrQyUKCGv6o3N2Wir
2026-08-25 20:17:33 +01:00
9c5d0d1861 Merge pull request 'fix(gitea): pin rebuilt image by digest, match upstream CMD form' (#123) from fix/gitea-mcp-digest-pin into main
Some checks failed
CI/CD / typecheck (push) Successful in 1m18s
CI/CD / lint (push) Successful in 2m30s
CI/CD / test (push) Successful in 1m27s
CI/CD / smoke (push) Failing after 2m1s
CI/CD / build (push) Successful in 4m27s
CI/CD / publish (push) Has been skipped
2026-08-21 16:07:31 +00:00
Michal
c16d7964c9 fix(gitea): pin the rebuilt image by digest and match upstream's CMD form
Some checks failed
CI/CD / lint (pull_request) Successful in 1m17s
CI/CD / typecheck (pull_request) Successful in 2m39s
CI/CD / test (pull_request) Successful in 1m27s
CI/CD / build (pull_request) Successful in 2m28s
CI/CD / smoke (pull_request) Failing after 3m4s
CI/CD / publish (pull_request) Has been skipped
Two corrections to the shell-bearing rebuild.

**Pin by digest.** It copied from `:latest`, so a rebuild silently ships
whatever upstream has moved to. When a probe started failing right after a
rebuild I could not tell a version change from a broken build, and burned
time on the wrong one — the binary's own `--version` prints 1.1.0 while
the image label says 1.6.0, so that was a red herring too. Now pinned to
sha256:dda8d56e…, which IS the running 1.6.0.

**CMD, not ENTRYPOINT.** Upstream sets `Cmd: ["/app/gitea-mcp"]` with no
entrypoint. mcpd maps a server's `command` to k8s `args`, which REPLACES
Cmd but only APPENDS to an ENTRYPOINT — so the ENTRYPOINT form would have
changed how the binary is invoked for any server that sets a command.
Matching upstream's shape keeps the non-injected path byte-identical.

gitea also needs `entrypoint` on its server row: its `command` is [], so
there is nothing for the injector wrapper to wrap without it. Set to
["/usr/local/bin/gitea-mcp"] via apply -f (patch cannot express an array).

Verified live: gitea RUNNING/healthy on secretDelivery: injector, with
vault-agent-init present and the command wrapped as
  ["/bin/sh","-c",". /vault/secrets/gitea-creds; exec \"$0\" \"$@\"",
   "/usr/local/bin/gitea-mcp"]
`get_me` — which needs read:user, the scope that started this whole
session — returns the real account. Plaintext credentials across all
mcpctl server pod specs: 4 -> 0.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vybEitX4FykeMatKe5Xki
2026-08-21 17:07:23 +01:00
7716e424f9 Merge pull request 'feat(gitea): rebuild gitea-mcp on a shell-bearing base' (#122) from feat/gitea-mcp-shell-base into main
Some checks failed
CI/CD / lint (push) Successful in 1m14s
CI/CD / test (push) Successful in 1m26s
CI/CD / typecheck (push) Successful in 2m48s
CI/CD / smoke (push) Failing after 3m7s
CI/CD / build (push) Successful in 2m12s
CI/CD / publish (push) Has been skipped
2026-08-21 10:16:28 +00:00
Michal
f097c0f4d5 feat(gitea): rebuild gitea-mcp on a shell-bearing base
Some checks failed
CI/CD / lint (pull_request) Successful in 1m20s
CI/CD / test (pull_request) Successful in 1m30s
CI/CD / typecheck (pull_request) Successful in 2m52s
CI/CD / smoke (pull_request) Failing after 2m1s
CI/CD / build (pull_request) Successful in 2m23s
CI/CD / publish (pull_request) Has been skipped
Upstream docker.gitea.com/gitea-mcp-server is distroless — `Cmd` is
["/app/gitea-mcp"] and there is no /bin/sh at any path (verified by
exec'ing every candidate against the running pod).

That is fine until the server wants secretDelivery: injector. The OpenBao
agent renders secrets to a FILE, so mcpd wraps the container command as
`sh -c '. /vault/secrets/<name>; exec "$0" "$@"'`, which needs a shell.
gitea was the only server in the fleet blocked on this, and so the only
one whose token had to stay inline in its pod spec.

Copying one static Go binary onto debian:stable-slim is cheaper than
building and maintaining a static envexec shim, and follows the precedent
in deploy/Dockerfile.docmost-mcp — this repo already rebuilds third-party
MCP servers when it needs to change how they run.

ca-certificates is required rather than incidental: the binary talks HTTPS
to mysources.co.uk and the distroless base shipped a trust store we are
leaving behind. Verified in the built image: shell present, binary runs,
ca-certificates.crt present.

ENTRYPOINT is kept so the plain (non-injected) path behaves exactly like
upstream; mcpd replaces it with the sourcing wrapper only when the server
opts in.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vybEitX4FykeMatKe5Xki
2026-08-21 11:16:23 +01:00
c79bdab51b Merge pull request 'fix(secrets): wrap an image server's own command, not just entrypoint' (#121) from fix/injector-image-server-argv into main
Some checks failed
CI/CD / lint (push) Has been cancelled
CI/CD / typecheck (push) Has been cancelled
CI/CD / test (push) Has been cancelled
CI/CD / smoke (push) Has been cancelled
CI/CD / build (push) Has been cancelled
CI/CD / publish (push) Has been cancelled
2026-08-21 10:14:42 +00:00
Michal
913c0fbdc6 fix(secrets): wrap an image server's own command, not just entrypoint
Some checks failed
CI/CD / lint (pull_request) Successful in 1m25s
CI/CD / test (pull_request) Successful in 1m32s
CI/CD / typecheck (pull_request) Successful in 3m0s
CI/CD / smoke (pull_request) Failing after 2m0s
CI/CD / build (pull_request) Successful in 4m34s
CI/CD / publish (pull_request) Has been skipped
Migrating docmost and my-home-assistant, both rendered their secret and
neither picked it up. The pod spec showed why:

  command: (empty)
  args:    ["node","build/index.js"]
  server.entrypoint: (unset)

wrapCommand consulted only `server.entrypoint` for non-package servers, so
with `entrypoint` unset it returned undefined, the wrapper was skipped
entirely, and the container ran its normal command — which never sourced
/vault/secrets/<name>. The failure is silent by construction: the agent
init container succeeds, the file is there, and the server simply starts
with empty credentials.

An explicit `command` on an image server is already a complete command
line — mcpd's exec mode would run exactly it — so it should be wrapped
verbatim. `entrypoint` is only needed when there is no command at all and
the image's own ENTRYPOINT would take over.

Now branches on the three real shapes: package server (prepend the runner
entrypoint mcpd owns), image + command (use verbatim), image only (require
the declared entrypoint).

Seven tests, one per shape plus the two undefined cases. Reintroducing the
old logic fails two of them — checked before keeping.

Both servers were rolled back to secretDelivery: env and are healthy; they
can migrate once this ships.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vybEitX4FykeMatKe5Xki
2026-08-21 11:14:39 +01:00
beb57baf58 Merge pull request 'fix(secrets): injector-delivered servers must attach, not exec' (#120) from feat/injector-attach-mode into main
Some checks failed
CI/CD / typecheck (push) Successful in 1m19s
CI/CD / lint (push) Successful in 2m24s
CI/CD / test (push) Successful in 1m27s
CI/CD / smoke (push) Failing after 1m59s
CI/CD / build (push) Successful in 4m40s
CI/CD / publish (push) Has been skipped
2026-08-21 01:05:53 +00:00
Michal
553834dbb1 docs(epics): document linking a task to its pull request
Accept only merges if the task carries a prNumber, and nothing said how a
human sets one. It goes through the generic patch command; without this the
merge gate reads as unreachable from the CLI.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-16 00:16:07 +01:00
Michal
6d38953a96 docs(agentic-teams): document the new resources, and make the webhook reachable
Five resources shipped with no documentation and one documented lie:
docs/inference-tasks.md still told people `mcpctl get tasks` lists the
inference queue. Work items own that name now.

Adds docs/epics.md (the Epic -> EpicTask -> Task hierarchy, the state machine,
risk-scaled review gates, the accept-merges gate, and reconciliation),
docs/gitproviders.md (forge registration, discovery/import, merge-on-accept,
post-merge reopen) and docs/agent-authority.md (the three run modes, and
plainly which two are not implemented).

Two things found while writing them, both fixed rather than filed:

- `webhookSecretKey` was read by the webhook route but writable from NOWHERE
  -- absent from the create schema, update schema, service, CLI and apply
  spec. Since the endpoint fails closed on a null key, post-merge CI failures
  could never reopen their task; the board would keep claiming `merged` for
  work that broke the build. Adds --webhook-secret-key, the schema fields and
  the apply key, with tests covering persist / omit / explicit-null /
  reject-empty. Omission deliberately does not clear the column, so an
  unrelated edit cannot silently disarm the webhook.

- src/cli/src/commands/create.ts contained a literal NUL byte inside a string
  literal (a binary-file check written with the raw character rather than an escape). Identical behaviour, but it made the whole file read as
  binary, so `grep -I` skipped it entirely and 1120 lines of CLI were
  invisible to every text search. Replaced with the escape.

The Epic write path (criteria/status/tradeoffs) is API-only -- documented as
such rather than papered over, and filed as task 180.

Full suite 2612 green; lint at baseline 794/77.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-16 00:14:58 +01:00
Michal
2a85447fac feat(rbac): bound what an agent may DO by whoever invoked it
Closes 177. An agent carries a project's prompts and its MCP tools, and the only
check on a chat turn was run:agents:<name> - so anyone who could talk to an
agent inherited tool access to every project that agent had joined. Membership
was the capability. AgentProject widened it from one pinned project to many.

The rule now: knowledge travels with the agent, action is bounded by the
authority of the run. Prompts and skills still load regardless of who is asking -
a QA reviewer that forgets the project's conventions when a different person
asks is not a reviewer. Every MCP tool call is gated.

Authority is resolved once per run (services/run-authority.ts) and is per-RUN,
not per-agent: the same agent answers a human under that human's authority and,
once a scheduler exists, runs unattended under its own. `caller` intersects the
agent's memberships with the caller's run:projects - reusing the exact grant the
project-chat route already demands, rather than inventing a parallel permission.
`agent` takes every membership without consulting a user. Both are implemented;
`service-account` throws 501 rather than honouring a forgeable x-service-account
header, and task 179 records what it needs first.

Gated in BOTH listTools and callTool, deliberately: withholding a tool from the
list is presentation, not enforcement - a model can invent a tool name it was
never shown, and callTool is what would run it.

The accepted consequence is stated plainly rather than papered over: a user can
ask about a project they cannot reach and the agent will answer from its
knowledge. The system block tells the model when it is holding such context so a
persona can apply it without reciting it. That is guidance to a model, not a
boundary - the boundary is that the tools are absent.

Two sibling holes found while exploring and closed here:

- Thread hijack. resolveThreadId on the agent path had no owner check, so anyone
  with run:agents:<name> could append to another user's thread by id and read
  its history back. The project path already did this correctly; mirrored, 404
  not 403 so ids cannot be probed.
- McpToken project escape. `project` travels in the request BODY, so the
  URL-based token scope check in main.ts never saw it - a token bound to project
  A could drive an agent into B's tools. The binding is now a ceiling.

Tests: 19 authority tests (intersection, fail-closed with no user and with no
RBAC wired, token ceiling, agent authority bypassing RBAC entirely, the 501) and
4 through the real chat path asserting prompts load while tools do not.

Three existing assertions updated for the new dispatcher signature - a
deliberate interface change, not tests bent to fit.

Local: build clean, workspace 2608 passed (+16), lint unchanged at 794 errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-15 23:54:53 +01:00
Michal
f0ba6e8096 feat(git): reopen a task when CI fails on the commit its merge produced
Closes 151 — the last loop in the lifecycle. A task reaches `merged`, CI then
fails on the merge commit, and the same task comes back as `reopened` instead of
the breakage going unnoticed because the board already said done.

Two properties make this safe rather than a liability.

**It fails closed.** The endpoint is unauthenticated by nature — a forge calls
it — so it accepts nothing unless the provider has a configured webhook signing
secret AND the HMAC verifies. No secret, no effect. The signature is computed
over the RAW body: re-serializing a parsed object changes the bytes, which is
the classic way this check silently stops working, and there is a test for it.
A 404 for an unknown provider is deliberately vague, because confirming which
provider names exist is free reconnaissance on a public endpoint.

**It matches precisely.** Accept now records `merge_commit_sha` — the commit the
merge actually produced, which is what CI runs against — so a failing status
finds exactly the task that caused it. The alternative, reopening "whatever
merged most recently", would reopen innocent work. A failure on a commit mcpctl
did not merge is somebody else's business and is ignored, as is a task that has
already moved on from `merged`.

Capturing the merge commit is best-effort by design: the merge already
succeeded, so failing to re-read the sha must not fail the accept — it only
costs the webhook its precise match later.

The route is skipped by the RBAC hook, since the forge has no mcpctl identity.
That is only safe because of the fail-closed rule above, and both the skip and
the reason are asserted in tests.

Tests: 12 covering every refusal path (no secret, unsigned, wrong key, signature
over different bytes), the vague 404, the RBAC skip, and the reopen matrix.

Local: build clean, workspace 2592 passed (+12), db 128 passed (same 4
pre-existing), lint unchanged at 794 errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-15 23:10:49 +01:00
Michal
ac9423e9c8 feat(git): merge the PR when a task is accepted
Closes 148/149 — the accept gate now lands code instead of just moving a row.

Driver gains createPullRequest / getPullRequest / mergePullRequest, verified
against the real Gitea API shapes rather than guessed: MergePullRequestOption
requires a capitalised `Do`, and supports `head_commit_id`.

That `head_commit_id` is the point. A human approved a SPECIFIC commit; if
someone pushed after that, merging anyway lands code nobody reviewed. So the
reviewed sha travels with the request and is checked twice — once by us for a
clear MergeStaleError naming both commits, and again by the forge, which closes
the race between our read and the merge. When it trips, the message says what
happened and what to do: re-review the current head, then accept again.

Accept merges BEFORE it transitions. If the forge refuses — conflicts, failing
checks, a moved branch — the task stays in awaiting_review and no event is
written. Reporting `merged` while main does not have the code is the single
worst thing this gate could do, and there is a test asserting the transition
never happens on a failed merge.

Failure modes are answers, not stack traces: no PR attached is approval-only and
succeeds; a repo with no forge connection explains that mcpctl cannot merge it;
an already-merged PR is treated as success so a retry after a timeout is safe.

owner/repo comes from `sourceFullName` when discovery recorded it, falling back
to parsing https, ssh:// and scp-style clone URLs, and returning null rather
than guessing when it cannot tell.

The merger is injected and optional, so an mcpd without a git provider still
runs and accept degrades to approval-only — which is also what the help text
now says, restored after the earlier commit that removed a claim we could not
back up.

Tests: 7 driver PR tests (Do field, head pinning, stale refusal, idempotent
re-merge, 405 → refusal), 10 merger tests incl. URL parsing, 7 accept-gate tests.

Local: build clean, workspace 2580 passed (+23), lint unchanged at 794 errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-15 00:05:31 +01:00
Michal
821c45b2ec fix(cli): stop claiming that accept merges anything
`mcpctl tasks accept --help` said "fires the merge". Nothing merges. There is
no createPullRequest or mergePullRequest anywhere in the codebase — the
git-provider client is task 148 and is not built.

Accept records human approval and moves the task to `merged`. Until 148/149
land, `merged` means "a human approved this", not "this is in main", and the
help text and service comments now say so. A user who accepted a task expecting
a PR to land would have been silently wrong.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-14 23:57:46 +01:00
Michal
2a305f7045 feat(cli): describe formatters for the agentic-teams resources
Closes 167. The five new resources fell through to the generic key-value dump,
against the project rule that describe shows clean sections and tables. More to
the point, two things built earlier had no way to reach a human at all: the
roadmap endpoint had no client, and the Epic reconciliation was only visible as
raw JSON.

`describe epic` puts the delivery mismatch directly under the completeness
percentage it contradicts, because reading either one alone is the bug. Rendered
against a live mcpd:

    Completeness:  [....................] 0%  (0/3)
    ...
    Delivery:      MISMATCH between what is claimed and what shipped
      Tasks:                1 merged, 0 outstanding, 0 cancelled
      Delivered, not claimed:
        - wheels  (1 merged, 0 outstanding)
      No roadmap node owns it:
        - engine, feel

plus the criteria checklist, accepted tradeoffs, and the roadmap with per-node
completeness and READY flags.

`describe task` derives the gates a task still owes from its risk and recorded
verdicts ("awaiting security_review, sre_review" vs "all passed"), and shows the
legal next transitions, PR linkage, verdicts and full history — so the answer to
"why won't this move" is on screen rather than requiring a failed attempt.

`describe repo` / `describe gitprovider` show the credential REFERENCE only.
The resolved value exists solely on the session-provision path and must never
reach a describe.

The roadmap, verdict and event fetches are all best-effort: an older mcpd 404s
and the resource still renders, rather than the whole describe failing over an
enrichment.

One bug found by looking at the output rather than the types: the Git section
opened on `!== undefined`, but JSON hands back null, so every task without a PR
printed an empty `Git:` header.

Local: build clean, workspace 2557 passed, lint unchanged at 794 errors.
Verified live against a local mcpd for epic, epictask and task.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-10 12:11:24 +01:00
Michal
60db1be531 feat(epictask): make the criterion-id convention an invariant
Closes 175. Epic.doneCriteria and EpicTask.doneCriteria are both JSON blobs
joined only by a free-form id, and nothing checked that a node's ids existed on
its parent Epic. A typo produced a criterion belonging to nobody.

The reconciliation added in 170 surfaces those as `unowned`, which is honest but
late: the mistake was made at write time and is far cheaper to refuse there. Now
create and update both reject unknown ids, naming every one of them and listing
what the Epic actually has — or saying plainly that the Epic has no criteria yet,
which is the likelier cause when a whole roadmap comes back empty.

Enforced in the service, not the schema: production provisions with
`prisma db push`, so a constraint would never exist where it matters.

A node with no criteria of its own is still fine — not every roadmap node claims
one — and an update that doesn't touch criteria doesn't pay for the check.

This deliberately stops short of the EpicCriterion table with a real FK. That
remains the stronger answer if criteria ever need to be addressed independently,
but it costs a schema change plus migrating everything currently in the JSON
blobs, and the invariant closes the actual failure mode today.

Also files 177: agent chat --project reaches project prompts and MCP tools while
checking only run:agents:<name>. Raised by the agent that built membership,
which correctly refused to invent the policy mid-task. Pre-existing behaviour for
a legacy pin, but membership widens the blast radius considerably — it should be
a deliberate decision with a test.

Local: build clean, workspace 2557 passed (+7), lint unchanged at 794 errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-09 23:36:53 +01:00
Michal
4515eb4517 fix: don't commit agent worktrees
`git add -A` swept .claude/worktrees/ into the previous commit as embedded git
repositories. They are transient checkouts created for parallel agent runs and
have no business in the tree — removed from the index and gitignored.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-09 23:34:28 +01:00
Michal
a869f77070 merge: AgentProject membership — the junction is now load-bearing
Merges feat/agent-project-membership (task 166/152). An agent can finally join
many projects: membership routes, per-membership role and optional brain
override, chat resolving context from the membership, and CLI attach-agent /
detach-agent / get memberships. The legacy Agent.projectId fallback is kept for
this release as the schema comment promises.

Conflict resolved in main.ts: that branch added its RBAC case to
mapUrlToPermission inline, while this branch had extracted the function to
middleware/url-permission.ts so it could be tested. Kept the extraction and
ported the case into it — joining a team is `edit:agents:<name>`, not
`create:agents`, which is the authority to mint a whole agent.

Also fixes something the extraction makes possible. security.test.ts contained a
hand-copied reproduction of mapUrlToPermission, because main.ts self-invokes
main() and nothing in it was importable. A security test asserting against a
copy stops testing the system the moment the real one changes, and this copy had
already drifted. It now imports the real function; all 18 assertions still pass,
against the code that actually runs.

Local: build clean, workspace 2550 passed, lint unchanged at 794 errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-09 23:34:18 +01:00
Michal
1a28cc5098 feat(mcpd): make AgentProject real — an agent joins many projects
The AgentProject junction has existed since the data-model commit and nothing
read it. `grep -c agentProject src/mcpd/src/services/chat.service.ts` returned
0; chat resolved an agent's project from the legacy single `Agent.projectId`
scalar. So the schema claimed "an agent is a person who joins zero, one, or
many projects" (docs/agentic-teams.md §4) while the running system still gave
every agent exactly one project. A schema that lies about behaviour is worse
than a missing feature: it reads as done, so nobody builds it.

This makes the junction load-bearing.

Membership is now a real surface (task 152):

  GET    /api/v1/agents/:name/projects          memberships
  POST   /api/v1/agents/:name/projects/:project join, body {role?, llm?}
  DELETE /api/v1/agents/:name/projects/:project leave
  GET    /api/v1/projects/:name/memberships     the roster, with roles

`role` is TEXT (developer|qa|security|sre in v1) and is validated in the
service layer, not the database — production provisions with `prisma db push`,
so a CHECK constraint written into a migration would never run there. Adding a
fifth role stays a one-line change.

RBAC: joining/leaving is `edit:agents:<name>`, not the `create:agents` the
default method→action mapping would have produced. Minting a whole new agent
and adding an existing one to a team are not the same authority; this mirrors
project↔server attach/detach already being `edit:projects`. Reading a roster is
`view:` on the side you asked from.

Chat honours it. `prepareContext` resolves the project from the membership when
the caller names one, and the resolution runs *before* the Llm lookup so a
per-membership `llmId` can replace the agent's pinned brain — and therefore its
whole failover pool — rather than patching the dispatch list afterwards. That
override is why the brain-ownership question (§15 Q1) can still go either way
without a migration: today the answer is per membership.

Naming a project the agent has not joined is an error that names the fix, not a
silent fallback. Answering from a *different* project's tools and prompts than
the one asked for is the failure mode worth being loud about.

The legacy scalar keeps working this release, as the schema comment promises:
`--project` accepts a membership *or* a legacy pin naming that same project,
bare `mcpctl chat <agent>` is unchanged, and `GET /projects/:p/agents` returns
the union of members and legacy-pinned agents so nothing disappears from a
project mid-migration (mcplocal's agents plugin reads that endpoint).

CLI: `mcpctl chat <agent> --project <p>` (previously rejected as "not both"),
plus `attach-agent` / `detach-agent` alongside the existing attach-server pair,
`get agents --project`, and `get memberships` with ROLE and BRAIN columns.
Completions come from the generator, not hand-edits.

Threads stay scoped to the agent, not to (agent, project) — §4 wants the
latter, but that needs a schema change and a story for what happens to those
threads when a membership is revoked. Deliberately not decided here.

Tests: 51 new (43 mcpd, 8 CLI). Full suite 2514 pass. Lint unchanged at
794 errors / 77 warnings.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-09 22:14:45 +01:00
Michal
fc524583b1 chore(completions): regenerate after the epictask --criterion flag
The guard test caught it, which is its job — the generator derives flags from
the commander program, so any new option makes the committed files stale.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-09 22:09:56 +01:00
Michal
f2239852a1 feat(cli): --criterion on create epictask, and file the reconciliation follow-ups
Merges the Epic reconciliation (task 170) and closes the gap that made it inert
from the terminal.

`mcpctl create epictask` had no `--criterion` flag while `create epic` did, so a
roadmap built from the CLI had no criteria on its nodes. The reconciliation
attributes a criterion to a node by id, so every criterion would have reported
as `unowned` and both mismatch lists would always have been empty — the feature
would have looked like it worked and detected nothing.

Files two follow-ups the reconciliation surfaced:

- 175: criteria and roadmap nodes are linked only by a free-form id convention
  that nothing enforces. EpicTaskService never validates ids against the parent
  Epic, ids can repeat across nodes, and a node can carry an id the Epic never
  had. The reconciliation reports those as `unowned` rather than guessing an
  owner, which is right, but the convention should become an invariant before
  more is built on it.
- 176: `get epic -o yaml` now emits computed `completeness`/`reconciliation`,
  which are not settable. apply tolerates them, but the round-trip rule says get
  output is apply input.

Local: build clean, workspace 2499 passed, lint unchanged at 794 errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-09 22:09:27 +01:00
Michal
b6f3ff8a26 Merge branch 'feat/epic-reconciliation' into feat/agentic-teams 2026-08-09 22:06:56 +01:00
Michal
00373d2b27 feat(epics): reconcile claimed completeness against delivered work
An Epic could report 0% while every Task under it was merged. That is not a
cosmetic gap — it is exactly the blind spot the Epic exists to remove
(docs/agentic-teams.md §1b: "we completed all the tasks and got a stick with
wheels instead of a car"). An Epic that cannot see its own delivered work
cannot detect that, because `completeness()` reads only `doneCriteria[].met`
and nothing flips those except a human hitting the criteria endpoint.

So read the same question a second time, from the delivery side, and report
where the two readings disagree — in both directions:

  - deliveredButUnclaimed — unmet criteria whose owning roadmap node has
    merged work. Somebody delivered and nobody ticked the box.
  - claimedButUndelivered — criteria marked met with nothing merged
    underneath. The box is ticked and nothing shipped. The dangerous
    direction, and the one that is easy to forget to look for.
  - unowned — criteria no roadmap node claims at all. They cannot appear in
    either list above, so without a bucket of their own they would vanish
    inside a clean-looking reconciliation.

Nothing is written. `met` stays exactly as a human or agent set it: deriving
it from task state would assert that finishing the tasks means delivering the
vision, which is the assumption under audit. The mismatch is reported, not
silently resolved.

Attribution is by criterion id, because that is the only link the schema
offers — EpicTask.doneCriteria is documented as "the decomposed subset of the
Epic's doneCriteria this node owns" and carries the same ids, but nothing
enforces it. Rather than guess an owner by name similarity or ordering, the
convention is treated as a claim and its failures are surfaced as `unowned`.
A foreign key from criterion to epictask would end the ambiguity; this reports
the ambiguity instead of hiding it.

Judgement calls, made explicit so they can be argued with:
  - `merged` alone counts as delivered. `done` only leaves the developer's
    loop and `awaiting_review` still needs a human; `reopened` means a merge
    did not hold, so it counts as outstanding.
  - `cancelled` is neither delivered nor outstanding. Counting it as owed
    would leave a node permanently undelivered because someone abandoned a
    task; counting it as delivered is a lie. It is reported on its own.
  - a node with no tasks is undelivered, not exempt — a met criterion over an
    undecomposed node is still a claim with nothing behind it. The empty task
    rollup is what distinguishes it from "built but not merged".
  - an epictask's own `status` is another claim, not evidence, so it is
    carried for context and never counted as delivery.

Surfaced on GET /api/v1/epics/:id next to the completeness it contradicts —
a mismatch is only useful where the misleading percentage is already on
screen — and standalone at GET /api/v1/epics/:id/reconciliation for callers
that do not want a 200KB Epic body with it. Deliberately kept off the list
route, which would pay a query per roadmap node per Epic.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-09 22:05:46 +01:00
Michal
1220b974dc fix(rbac): correct the non-CRUD verbs, and make the mapping testable
Closes 134/164. The five agentic-teams resources had RBAC slots but the
non-CRUD verbs had no cases, so they fell through to the method default and
every one of them read as `create:<resource>`. That is wrong in both
directions: it let anyone who may CREATE a task also ACCEPT one — the human gate
that fires a merge — and it refused a reviewer who may drive tasks but not
create them.

Corrected, following the verb the codebase already uses for "use this resource
to do work" (llms/infer, agents/chat, projects/chat):

- POST /tasks/:id/{transition,accept,reject,verdicts} → run:tasks:<name>
- {GET,POST} /gitproviders/:id/{repos,import,probe} → run:gitproviders:<name>.
  Discovery calls OUT to the forge with its stored token, so holding
  view:gitproviders should let you see that a forge is registered, not use its
  credential.
- POST /epics/:id/{status,tradeoffs,criteria} → edit:epics:<name>. These rewrite
  the vision record.
- GET /epics/:id/roadmap → view:epics:<name>
- {POST,DELETE} /epictasks/:id/edges → edit:epictasks:<name>

mapUrlToPermission moved from main.ts to middleware/url-permission.ts. main.ts
calls main() at module load, so nothing in it is importable from a test — an
authorization mapping that cannot be tested is an authorization mapping nobody
has checked, which is exactly how these verbs shipped mis-scoped.

15 tests now assert the mapping rather than the mere presence of the strings,
including that a bare create grant does NOT cover the gated verbs, that work
items and the inference queue never resolve to the same resource for any method,
and that the pre-existing mappings (llm infer, agent chat, operations, the
personalities/skills piggybacks) still hold.

Local: build clean, workspace 2478 passed (+15), lint back to the repo's
baseline 794 errors after making the five new match guards explicit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-09 21:55:31 +01:00
Michal
d9535bc51f chore(taskmaster): file 174 — scope project-only commands out of root --help
The tasks/repos command groups added with the CLI work sit at the root of
`mcpctl --help`, but both only function inside a project context. Beyond the
clutter, it teaches the wrong data model: a task reads as a peer of servers and
secrets rather than a leaf of the epic tree.

Convention recorded as the `scope-commands-to-their-context` prompt in the
mcpctl-development project. Note `attach-server`/`detach-server` are already
hidden, so the pattern exists and was simply not applied consistently.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-09 21:50:38 +01:00
Michal
5f5efd1144 fix(backup): cover the agentic-teams resources — closes the data-loss gap
The audit found that none of the new resources were in BACKUP_KINDS, so
git-backup never captured them and a restore would lose every epic, roadmap
node, repo and forge registration. Closes 135/165.

Backed up: gitprovider, repo, epic, epictask — configuration and the vision
layer. NOT tasks: a work item carries in-flight state (lifecycle position,
verdicts, PR links) and restoring a stale one from git would resurrect work that
has since moved on. That is the same reasoning that keeps McpInstance out of the
backup, and there is a test asserting it stays out.

Two properties the tests pin, because both are the kind of thing that fails
silently:

- **Credentials serialize as a REFERENCE, never a value.** The backup repo is a
  plaintext git checkout; emitting a resolved token would export every forge
  credential to disk in cleartext. credSecretId/credSecretKey are internal
  fields and become `credRef: {name, key}`; restore re-resolves against whatever
  the secret backend holds now.
- **Relations serialize by name, not foreign key.** A restore into a different
  database has different cuids, so a backed-up projectId would dangle.

Restore ordering follows dependencies: a forge before the repos that reference
it, an epic before its roadmap nodes.

Two deliberate refusals on the restore path: an Epic's `status` is never written
by a restore, so a stale backup cannot silently mark it delivered — that
terminal state is only reachable through setStatus and its rules; and a
GitProvider is not probed on restore, because the forge may legitimately be
unreachable while a backup replays and failing the whole restore for that is
worse than a row that needs a later `probe`.

The pre-existing APPLY_ORDER length guard caught this change, which is exactly
what it is for; updated 9 → 13 and tied it to BACKUP_KINDS.length so the two
cannot drift apart again.

Also marks 153 done — `mcpctl tasks accept/reject` shipped with the CLI work; the
generated graph had it blocked behind the git-provider merge, but the gate itself
does not need it.

Local: build clean, workspace 2463 passed (+6), lint unchanged at 794 errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-09 20:44:20 +01:00
Michal
300c42b404 fix(backup): restore skills on git restore, and record the audited gaps
Found while auditing what this branch is missing: skills are serialized into the
backup repo and enqueued on every mutation, but `importResource` and
`deleteResource` in main.ts have no `skill` case — so a git restore silently
drops every skill. SkillService has had upsertByName/deleteByName the whole
time; the switch arms were simply never added. Pre-existing, not introduced
here.

Also records ten tasks (164-173) for gaps found by auditing the branch against
the codebase rather than against the plan:

- 164 RBAC enforcement tests. Five resources and a namespace split shipped with
  no test proving the gating works. Also flags that the non-CRUD verbs
  (transition/accept/reject, import/probe) currently fall through to the default
  method mapping and are probably mis-scoped.
- 165 Backup coverage for the agentic-teams resources — same class of silent
  data loss as the skill bug above, for five more kinds.
- 166 AgentProject is inert: chat still resolves context from the legacy
  Agent.projectId scalar, so "an agent joins many projects" — a core idea of the
  design — is not actually usable yet.
- 167 describe formatters (currently a generic key-value dump, against the
  project rule) including a client for the roadmap endpoint, which has no caller.
- 168 Docs, including the breaking `mcpctl get tasks` rename that exists only in
  a commit message today.
- 169 Smoke tests for the forge flow.
- 170 Epic completeness ignores delivered work: an Epic can read 0% while every
  task under it is merged, which is precisely the blind spot the Epic exists to
  remove.
- 171 `mcpctl chat mcpctl` has never been run end to end — the tool layer is
  verified, the LLM-picks-a-tool loop and the mcplocal token hop are not.
- 172 Nothing on this branch is live; everything was verified against a local
  mcpd on the test database.
- 173 Web UI + Pulumi kinds (cosmetic).

Local: build clean, workspace 2457 passed, lint unchanged at 794 errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-09 20:38:23 +01:00
Michal
53507e876a feat(cli): apply -f support for agentic-teams resources, plus vertical smoke tests
Closes two stated project rules this work had been violating.

**`get -o yaml` must round-trip through `apply -f`.** The five new resources
were creatable and listable but absent from apply, so what `get` emitted was not
valid apply input. They are now applied in dependency order — gitproviders →
repos → epics → epictasks → tasks — since each references the previous by name,
and each block is independently fault-tolerant so one bad entry does not abort
the rest of a manifest. `edit` accepts them too.

`state` is deliberately NOT in the task spec. Lifecycle moves go through
`mcpctl tasks advance`, which enforces the transition table and the risk gate; a
manifest that could set state would route straight around both. epictask
`dependsOn` edges are re-applied on every run, which is safe because the server
treats a duplicate edge as a no-op.

**Every feature ships smoke tests.** Adds the vertical: create an epic with
mixed outcome/quality criteria, round-trip it through get -o yaml → apply -f,
chain an epictask, then drive a task through the lifecycle asserting the risk
gate blocks `done` without a passing QA verdict, that an illegal transition is
refused, that history carries every transition and the verdict, and that reject
without a comment fails. Skips when mcpd is unreachable; a failure against a
reachable daemon means it predates the feature.

The round-trip assertion is the interesting one: it feeds `get`'s own output
straight back into `apply` and fails if the two surfaces have drifted, rather
than trusting that they agree.

Local: build clean, workspace 2457 passed, db 128 passed (same 4 pre-existing
Secret failures), lint back to the repo's baseline 794 errors after replacing
destructured discards with an explicit omit() helper.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-09 19:56:11 +01:00
Michal
77c45a2715 feat(mcpd): mcpctl system agent — operates mcpctl with the caller's permissions
A system agent, seeded like the system project and system skills, whose entire
job is running mcpctl for the user:

  mcpctl chat mcpctl -m 'Make a prompt in the SRE project saying we run multiple
    agents on the same source at once, so always use git worktrees'

## The permission model is the design

Every tool call loops back through mcpd's own HTTP API carrying the CALLING
USER's bearer token. The agent holds no credential of its own and never touches
the service layer directly, so the existing auth hook, RBAC hook, zod
validation, ownership scoping and audit trail all apply verbatim. It is exactly
as privileged as the person talking to it, never more.

Calling services directly would have been simpler and would have meant
re-implementing every one of those checks at a second call site — precisely the
RBAC carve-out this project refuses to make. A 403 is surfaced as a 403, not
retried or routed around; the system prompt tells the agent to report it.

Verified live, not just mocked: the same tool with an admin token created a
project, and with a plain user's token returned "Permission denied (403)".
No token at all refuses before any request is made; a bad token is rejected by
mcpd's own auth, because there is no ambient credential to fall back on.

## Shape

- `Agent.toolset` (TEXT, default 'project') selects the surface. 'mcpctl' means
  no project prompts and no MCP tools — managing mcpctl is the whole job, and an
  unrelated project's context would be noise it might act on.
- Five generic tools (list/get/create/update/delete + resource) rather than one
  per resource. mcpctl is a REST API over named resources, so that is the honest
  surface, and this repo's own tool-selection findings say a scoped catalogue
  beats a large flat one.
- Names are resolved to ids by listing first, and a miss reports what IS
  available, so the model never guesses an id.
- Resource is a closed enum: the model cannot invent an endpoint.

Also exercised end to end against a live mcpd: created the worktrees prompt in
the `sre` project, listed it back, and read its content — the user's original
example, working.

Tests: 17 unit tests covering the token plumbing, the no-token and 403 paths,
resource allowlisting, name resolution, and request shaping.

Local: build clean, workspace 2457 passed (+17), lint unchanged at 794 errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-09 19:13:43 +01:00
Michal
772c25f605 feat(cli): make the agentic-teams stack usable from the terminal
Everything so far was API-only. This adds the CLI surface and proves the whole
vertical works end to end against a real mcpd and a real Gitea.

  mcpctl create gitprovider mysources --url https://mysources.co.uk \
      --secret gitea-token --key TOKEN
  mcpctl repos discover mysources --query mcp --project demo
  mcpctl repos import   mysources --project demo michal/mcpctl
  mcpctl create epic ship-agents --project demo --content "..." \
      --criterion "cli=CLI can drive a task" --criterion "q:feel=reads at a glance"
  mcpctl create epictask foundation --epic ship-agents
  mcpctl create task wire-cli --epictask foundation --title "Wire the CLI"
  mcpctl tasks advance wire-cli in_progress
  mcpctl tasks verdict wire-cli --stage qa_review --commit 3f2ab19 --pass --summary "..."
  mcpctl tasks accept  wire-cli --note "ship it"
  mcpctl tasks history wire-cli

`repos` and `tasks` are their own verb groups rather than more create/edit
flags. Discovery is inherently two-step — look at what is there, then pick — and
accept/reject are the human gate, not field writes.

`get` gained columns for all five resources. The Epic row shows completeness as
"40% (2/5)" and CodeRepo shows SOURCE/FROM, so imported repos say where they
came from at a glance.

BREAKING: `mcpctl get tasks` now lists WORK ITEMS. The inference queue keeps
`inference-task(s)` and gains `itask(s)`; the short `task`/`tasks` aliases move
to work items. This completes the split already made in RBAC (a186d95) — having
the CLI and RBAC disagree about what "tasks" means would be worse than the
rename.

Verified end to end, not just unit tested: mcpd run locally against the test
database, admin bootstrapped, then the full flow driven through the built CLI.
Discovery returned the real repositories from https://mysources.co.uk, import
created the CodeRepo with provenance, re-discovery marked it `imported`, and the
task walked todo → in_progress → building → self_testing → qa_review with a
FAILING QA verdict correctly refusing `done`, then bounced back, passed on a
second commit, and merged through the accept gate. `tasks history` shows both
verdicts and all twelve transitions.

Two bugs the walkthrough caught that unit tests had not: `get repos` and
`get epics` crashed on `padEnd` because Prisma includes hand back `{ name }`
rather than a bare string, and the completions guard test failed because the
resource lists had changed without regenerating.

Local: build clean, workspace 2440 passed, db 128 passed (same 4 pre-existing
Secret failures), lint errors unchanged at the repo's pre-existing 794 (+2
no-console warnings, the same shape every other command registration uses).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-08 17:00:25 +01:00
Michal
b70300d389 feat(mcpd): CodeRepo + GitProvider — register a forge, discover and import repos
Two resources landing together, because a repo you have to hand-type is a repo
nobody adds.

CodeRepo is a git repository attached to a project. GitProvider is a configured
forge instance you register once — URL and credential in one place — and then
list and import from.

Nothing above the driver layer knows what a Gitea is. src/mcpd/src/services/git/
holds the GitProviderDriver interface, the Gitea implementation and a registry
that resolves by type, mirroring LlmAdapterRegistry.build. Adding GitHub or
GitLab is one file plus one case. The instance URL always comes from the
GitProvider row; there is no hardcoded host anywhere.

Follows the mcpservertemplates convention: a declarative resource applied via
apply -f, with instances keeping denormalized provenance that outlives the
source. CodeRepo stores sourceProvider + sourceFullName alongside the FK, so
"where did this repo come from" stays answerable after the forge is
disconnected — the same reasoning as McpServer.templateName/templateVersion.
The FK is SetNull, so disconnecting a forge never deletes the repos, or the
tasks hanging off them, that came from it.

Discovery uses /repos/search, deliberately NOT /user/repos. The latter needs the
read:user scope, which repo-scoped automation tokens do not carry — the token
in this repo's .env has write:repository and 403s on /user/repos. The driver
also passes Gitea's 403 scope message through verbatim, so an under-scoped
token says which scope it is missing instead of looking like an empty forge.

Import is partial-success by contract: one bad name must not lose the other
nineteen. Everything importable is imported; everything skipped comes back with
a reason, and a re-import is distinguished from a name collision with an
unrelated repo. Imported repos store no credential of their own and inherit the
forge's, so rotating one token covers every repo from that forge.

Credentials never leave as plaintext: reads return the reference only, and
resolveCheckouts() — the single path that produces a token — exists for the
session provisioner and is called by no route. References are verified at
definition time, so a wrong key fails with "secret has no key 'X'" rather than
as a clone that mysteriously cannot authenticate.

Clone URLs must be https/ssh/scp-style and checkout paths must be relative with
no ".." segment: both are inputs to the clone step, and neither should be able
to point it at mcpd's own filesystem or escape the workspace.

Tests: 18 CodeRepo + 22 GitProvider unit tests. Gitea driver additionally
verified live against https://mysources.co.uk (Gitea 1.25.4, 10 repos
discovered, query filter working).

Local: build clean, workspace 2440 passed (+40), db 128 passed (same 4
pre-existing Secret failures), lint unchanged at 869.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-08 16:42:40 +01:00
Michal
05abe3522c feat(mcpd): Task lifecycle — CAS transitions, risk-scaled done gate, accept gate
The work-item spine: repository, validation, service and routes for Task, plus
its verdicts and append-only transition log.

Transitions are compare-and-swap. The expected current state is part of the
WHERE clause and the affected-row count is the answer, so two agents racing to
advance the same task cannot both win — a read-then-write would let both
"succeed" and silently lose one. Losing the race is a 409 naming the state the
task is actually in, not a 500, because on a shared board that is a normal
outcome the caller retries. Same idiom as InferenceTaskRepository's claim.

QA / Security / SRE are stages inside the task's own loop, not post-submit
reviewers, so each can bounce straight back to in_progress — that is what
"feedback" means here. A failed build likewise goes backward, not forward.
`merged` allows only `reopened`, so a merged task cannot be quietly edited;
post-merge failure is the one way back in.

The gate on `done` scales with risk instead of running a fixed pipeline:
trivial needs nothing, normal needs a passing QA verdict, high needs QA +
Security + SRE. Forcing a one-line docs fix through security review is how a
process gets routed around, so the cost has to match the risk. A FAILING
verdict does not clear a gate — a recorded failure is evidence the gate was
tried and did not hold. The 400 names which gates are still missing.

The human accept gate is a task-level yes/no on a resolved outcome, not
per-tool approval: accept fires the merge and stores the comment, reject sends
it back to in_progress and REQUIRES a comment, because the comment is the
direction the agent works from.

GET /tasks/:id returns `legalTransitions` so no client has to hardcode the table.

Tests: 25 service tests — full happy path, every stage's bounce-back, illegal
and no-op transitions, the CAS race (including that no event is written when
the race is lost), the complete risk matrix including failing-verdict-does-not-
count, and the accept/reject gate.

Local: build clean, workspace 2400 passed (+25), db 128 passed (same 4
pre-existing Secret failures), lint unchanged at 869.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-08 16:28:26 +01:00
Michal
da4752b90f fix(mcplocal): stop the 30s proxy timeout killing agent turns
`mcpctl chat <agent>` failed with

    HTTP 503 {"error":"service_unavailable","message":"Cannot reach mcpd daemon. Is it running?"}

while mcpd was answering /healthz in 32ms. The message was wrong in a way that
cost real debugging time: mcplocal was reaching mcpd fine and giving up after
30s. journalctl shows the signature plainly — statusCode 503 with
responseTime 30003.87 on POST /api/v1/agents/reviewer/chat.

This blocks the agentic-teams epic outright. An agent turn is a multi-turn
tool-use loop that runs for minutes by design, so a 30s ceiling on the chat path
is not a safety net, it is a guaranteed failure for every non-trivial turn.

Three defects, all in the same path:

1. One blanket budget for every forwarded route. DEFAULT_TIMEOUT_MS = 30_000 is
   right for CRUD and wrong for chat. Chat, project chat, llm infer and
   inference-task streams now get LONG_RUNNING_TIMEOUT_MS (600_000, override
   with MCPLOCAL_LONG_TIMEOUT_MS) — matching STREAM_TIMEOUT_MS, which the CLI
   already allowed. mcplocal in the middle was the binding constraint.

2. Timeouts were reported as connection failures. Split UpstreamTimeoutError
   out of ConnectionError and map it to 504 with an accurate message that says
   the daemon IS reachable. ConnectionError still means unreachable and still
   returns 503. Verified nothing else branches on ConnectionError.

3. SSE was buffered. `forward()` reads the whole body through res.text(), so
   even turns that finished in time arrived as one blob and the CLI's live
   token output never appeared. Streaming routes now use forwardStream() and
   pipe the body straight through, preserving content-type and
   x-accel-buffering (dropping the latter lets intermediaries re-buffer and
   reintroduces the stall).

Also closes the escape that produced the sibling `500 code:23` failure: the body
read in forward() was outside the try, so when mcpd had already written SSE
headers the raw DOMException reached Fastify unhandled.

Tests: 9 new proxy tests. The two that matter — "does not abort an agent chat
that outlives the CRUD budget" and "streams SSE through instead of buffering" —
were confirmed to FAIL against the pre-fix behaviour and pass after. Three
existing mcpd-client tests asserted the old taxonomy and were updated to assert
the new one deliberately.

Local: build clean, workspace 2375 passed, lint unchanged at 869.

NOT YET LIVE: mcplocal runs from the installed RPM, so this needs a package
rebuild + `systemctl --user restart mcplocal` to take effect.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-08 11:35:16 +01:00
Michal
7f64316d59 feat(mcpd): EpicTask resource — roadmap nodes, edges, and the roadmap view
Roadmap nodes plus the edges that chain them into the dependency graph the
design doc calls the spider web. Edge direction is "from depends on to".

The database cannot enforce graph integrity here — production provisions with
`prisma db push`, so a trigger or CHECK would never exist there, and the schema
test added earlier documents that boundary explicitly. So the service rejects
the three things that would otherwise corrupt the roadmap:

- self-edges,
- edges crossing Epics, since a roadmap is one Epic's graph,
- cycles, which make the graph unschedulable and hang any topological walk.

Cycle detection is a BFS over the prerequisite direction: adding from→to closes
a loop exactly when `to` already depends transitively on `from`. The error names
the offending path, not just "cycle", so a failed apply says which nodes to look
at. The traversal carries a seen-set that also guarantees termination if the
stored graph ALREADY contains a cycle — from a hand-edited database or an
earlier bug — rather than spinning forever. There is a test for that case.

Adding an existing edge is a no-op rather than an error, so applying a manifest
twice is safe.

GET /api/v1/epics/:id/roadmap returns nodes with per-node completeness, both
edge directions, and `ready`: the nodes whose prerequisites are all satisfied,
which is the question anyone actually asks of a roadmap. A cancelled
prerequisite counts as satisfied — it is not coming. Completeness reuses
EpicService.completeness verbatim so a node and its Epic can never disagree
about what "met" means.

Tests: 19 service tests — the full cycle matrix (self, 2-cycle, long transitive,
diamond-is-fine, pre-existing-cycle termination), roadmap edge directions,
readiness including the cancelled-prerequisite case, and CRUD guards.

Local: build clean, workspace 2366 passed (+19), db 128 passed (same 4
pre-existing Secret failures), lint unchanged at 869.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-08 11:10:39 +01:00
Michal
909506317e feat(mcpd): Epic resource — the vision layer, with an honest-done gate
Full CRUD for Epic: repository, zod validation, service, routes, main.ts
wiring, and revision tracking (ResourceType gains 'epic', which is a plain
string union by design so this needed no migration).

The substance is the done rule, not the CRUD. An Epic exists to stop "every
task completed, still no car", so status is deliberately NOT settable through
PUT — it goes through POST /epics/:id/status, which enforces:

- `delivered` requires every done-criterion met. The 400 names the unmet ids
  (truncated past 5) so the caller knows what is actually missing.
- `delivered` with recorded tradeoffs is refused — if you accepted a gap, the
  status has to say so.
- `delivered_with_tradeoffs` requires at least one recorded tradeoff, otherwise
  it is `delivered` wearing a disguise.
- `delivered` with zero criteria is refused: nothing was claimed, so nothing
  can have been delivered.
- `cancelled` and `open` are always allowed — abandoning needs no ceremony, and
  reopening is how a gap gets fixed.

Completeness counts `quality` criteria alongside `outcome` ones. Grading only
the mechanically checkable half is exactly the failure the Epic exists to
prevent, so both feed the percentage, and it is computed server-side and
attached to every response rather than left for clients to re-derive.

Tradeoffs are append-only and stamped with who and when — the record is the
audit trail for why "done" was honest.

Scope changes patch-bump semver, so creep is versioned as visibly as
under-delivery.

Tests: 26 service tests (the full done-rule matrix, tradeoff append/stamping,
criterion flipping, semver bumping, and that a plain update cannot smuggle a
status through) and 11 route tests (completeness attachment, owner stamping,
400 vs 404 vs 500 behaviour, the status gate over HTTP).

Local: build clean, workspace 2347 passed (+37), lint unchanged at 869.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-08 11:05:28 +01:00
Michal
ffcd0f985b feat(orchestrator): ephemeral session pods — restart policy, volumes, init containers
An MCP server container is long-lived and restarts forever; an agent session pod
is the opposite — it does a unit of work and dies. ContainerSpec could not
express that: restartPolicy was hardcoded 'Always' in generatePodSpec, and there
were no volume, workdir, or init-container fields at all.

Adds four optional fields to ContainerSpec (restartPolicy, workdir, volumes,
initContainers) and implements them in both backends. All optional, so every
existing caller is byte-identical — generatePodSpec still emits 'Always' unless
asked otherwise, and omits volumes/initContainers keys entirely when unused.

Kubernetes gets this nearly free: initContainers run to completion in
declaration order before any app container starts, so "clone the repos, then
work them" needs no sequencing logic from us. generateDeploymentSpec
deliberately ignores spec.restartPolicy — k8s rejects anything but 'Always' on a
Deployment pod template — and says so in a comment.

Docker has no init containers, so they are emulated as sequential runs sharing
the same volumes. The important property is the failure path: a non-zero init
exit aborts BEFORE the main container is created, and the error carries the init
container's last 50 log lines. Otherwise a failed git clone would leave an empty
workspace that looks exactly like a successful one, and the agent would start
work against nothing. Named Docker volumes are namespaced by container name to
stand in for emptyDir.

Tests: 10 k8s manifest tests (defaults unchanged, each restart policy, volume
and mount emission, init ordering, name sanitization, security context still
hardened) and 9 Docker tests including the abort-before-main case and log
propagation.

Local: build clean, workspace 2310 passed (+19), lint unchanged at the repo's
pre-existing 869.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-08 10:36:21 +01:00
Michal
fceb3cfb95 feat(db): agentic-teams data model — epics, work items, repos, membership
Adds the foundation entities from docs/agentic-teams.md §5/§7: a versioned
vision layer (Epic -> EpicTask -> Task), git repositories attached to projects,
agents as many-to-many project members, and the ephemeral session that IS an
agent's workspace.

9 tables + 5 enums, purely additive — no drops and no column changes on
existing tables, since every new relation is held on the new side.

Shape notes:

- EpicTask chaining uses an explicit EpicTaskEdge table rather than a
  self-referencing array, so the roadmap graph walks in both directions and
  edges can carry a type later.
- TaskVerdict is commit-pinned and immutable: "QA verified 3f2ab" stays
  reproducible, which is what lets later coverage claims be audited rather than
  believed. TaskEvent is the append-only transition log.
- Task survives losing its repo and its assignee (both SetNull) — a work item
  outlives whoever happened to be working it — but cascades from its EpicTask.
- CodeRepo, not Repository: `repository.repository.ts` next to 24 other
  *.repository.ts data-access files would be actively confusing. CLI resource is
  `repos`, with repo/repository/repositories aliased onto it.
- AgentProject.llmId exists so the brain-ownership question
  (docs/agentic-teams.md §15 Q1) can be settled either way without a migration.
- Agent.projectId is retained and coexists with AgentProject for one release so
  findByProjectId / prepareContext keep working. Note this partially reverses
  20260223120000_remove_project_members — deliberately, and only for agents;
  human membership stays RBAC-driven.

No CHECK constraints anywhere, on purpose: deploy/entrypoint.sh provisions with
`prisma db push`, so hand-written migration SQL never runs in production. The
migration file is kept for local `migrate dev` parity and as a readable record,
and says so in its header. Invariants that cannot be expressed in the schema
(EpicTaskEdge cycle rejection) belong to the service layer; a schema test
documents that boundary rather than pretending the database enforces it.

Tests: 23 schema tests covering defaults, per-project name scoping, cascade vs
SetNull on every FK, all 12 lifecycle states, and the GC-sweep predicate.
clearAllTables extended for all 9 models.

Verified locally against postgres:16-alpine on :5433 — db suite 128 passed
(baseline was 105; the same 4 pre-existing Secret_backendId_fkey failures
remain, unrelated). Workspace 2291 passed, build clean, lint unchanged at the
repo's pre-existing 869.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-08 10:30:02 +01:00
Michal
a186d95a85 feat(rbac): split the tasks namespace into tasks + inference-tasks
v5 gave the durable inference queue the RBAC resource `tasks`, on the
reasoning that inference was the only kind of task we had. Agentic teams
introduces work-item tasks (Epic -> epictask -> task), which need that name.

The queue moves to its own `inference-tasks` resource, matching its existing
/api/v1/inference-tasks route prefix, and `task`/`tasks` now mean work items.
Also lands the agentic-teams aliases: epic, epictask/et, repo/repository, session.

The rename is not purely a code change. ResourceBindingSchema runs
normalizeResource at parse time and stores the normalized string, so every
RbacDefinition row written before the split literally contains "tasks" meaning
inference. Shipping the alias change alone would silently stop those grants
authorizing the queue and start them authorizing work items -- a privilege
change in both directions. bootstrap/rbac-tasks-backfill.ts rewrites them once
at startup, alongside migrateAdminRole.

The run-once marker is claimed BEFORE the rewrite, not after. A failed marker
write after a successful rewrite would let the next startup rewrite again,
converting genuine post-split work-item grants into inference grants. Losing
the rewrite is recoverable by re-granting; running it twice silently is not --
so if the marker cannot be persisted we skip the rewrite and warn loudly.

mapUrlToPermission moves in lockstep: /api/v1/inference-tasks maps to
`inference-tasks`, otherwise the split would break queue authorization on the
first request.

Adds generic getBooleanSetting/setSetting helpers to bootstrap/system-settings.ts,
following the existing "global settings live in a Secret" pattern. setSetting
refuses to write when the settings secret has been migrated onto a non-plaintext
backend, where a direct `data` write would read back empty.

Out of scope, tracked separately: the CLI still routes `mcpctl get tasks` to the
inference queue (src/cli/src/commands/shared.ts). Repointing it needs the
work-item resource to exist first, or the command breaks with no replacement.

Tests: 14 unit tests cover alias resolution, the rewrite, wildcard and
operation bindings being left alone, idempotency, and both marker-failure paths.
Full workspace suite green (2291 tests). The new smoke test is RED until mcpd
is deployed with this change -- it asserts post-split storage against a live
daemon, and the currently deployed one predates the split.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-08 10:21:56 +01:00
Michal
797df2299d docs(taskmaster): PRD + task graph for agentic teams foundation
Converts docs/agentic-teams.md §16 steps 1-3 into a trackable PRD and 48
appended tasks (IDs 115-162) in the master tag. Steps 4-8 land as 5 low
priority placeholders wired onto the spine, pending the §15 open questions.

The PRD is grounded in the current codebase rather than the design doc's
indicative sketches, which changed the shape of the work:

- RBAC `tasks` is already owned by the inference queue, and bindings are
  normalized at parse time, so existing RbacDefinition rows literally hold
  "tasks". Taking the name for work items needs a one-shot guarded backfill
  to `inference-tasks` (task 116).
- deploy/entrypoint.sh uses `prisma db push`, so migration SQL never runs in
  production. Invariants are service-layer, not CHECK constraints.
- Ephemeral pods do not exist: restartPolicy is hardcoded Always and
  ContainerSpec has no volume/workdir fields (tasks 140-142).
- `exec` is cheap: KubernetesOrchestrator already implements exec/attach over
  k8s.Exec/k8s.Attach; only an API + CLI surface is missing (tasks 144-145).
- No git provider client exists in product code (task 148).
- ChatService caps its tool loop at 12 iterations, so the AgentRuntime runs a
  headless coding agent in-pod rather than extending chat (tasks 146-147).

Names the git-repo entity CodeRepo (CLI `repos`) to avoid colliding with the
repository/ data-access layer; AgentProject carries an optional llmId so the
brain-ownership question can be settled either way without a migration.

Generated task_*.md files are left untracked, matching how this repo has
treated them since the first commit.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N4wNHWf7xSwnZCWpJcyv9p
2026-08-08 09:58:20 +01:00
Michal
d24c30761b docs: keep superseded design-round doc history
Supporting documents folded into agentic-teams.md (the canonical unified
design record), retained as historical context:

- agentic-teams-review.md (round 2)
- vision-fidelity.md (round 3 diagnosis + research)
- epic-task-artifact-model.md (Epic/epictask/task + context philosophy)
- session-summary-vision-fidelity.md (session narrative)

All superseded by docs/agentic-teams.md, which remains the source of truth.
2026-08-08 09:03:58 +01:00
Michal
5e1c6b3681 docs: unified agentic-teams design document
Consolidates agentic-teams (rounds 1-2), vision-fidelity, Epic/artifact
model, and session summary into one canonical design record.

Supersedes: agentic-teams-review.md, vision-fidelity.md,
epic-task-artifact-model.md, session-summary-vision-fidelity.md
2026-08-08 09:03:35 +01:00
344 changed files with 29249 additions and 617 deletions

View File

@@ -0,0 +1,808 @@
# PRD: Agentic Teams — Foundation
## Overview
mcpctl today orchestrates MCP servers, projects, prompts, skills, and chat agents. It does not
orchestrate *work*. This PRD adds the foundation that lets human developers and autonomous agents
be peers in the same project: a versioned vision layer (Epic → epictask → task), git repositories
attached to projects, agents as many-to-many project members, ephemeral per-session pods where
agents do real work, a swappable agent runtime, a task lifecycle with QA/Security/SRE gates, and a
single human accept gate that fires a merge through the git provider's API.
The design record is `docs/agentic-teams.md`. This PRD converts §16 steps 1–3 of that document
into an implementation spec. Steps 4–8 are named as deferred work at the end, deliberately
without detail — they depend on open design questions (`docs/agentic-teams.md` §15) that are not
closed.
## Problem
Two failure classes motivate this work.
**Nothing tracks work.** There is no Epic, EpicTask, Task, Repository, or AgentProject model in
`src/db/prisma/schema.prisma`, and no repository, service, route, or CLI code for any of them.
Agents can chat and call tools, but there is no unit of work to assign, no lifecycle to advance,
no branch or PR to attach, and no gate for a human to accept.
**Completed tasks do not add up to the vision.** From the design doc: you have a grand vision, you
split it into tasks, you complete all the tasks, and you are left with a stick with wheels instead
of a car. The root cause is not execution — it is that decomposition is lossy, tasks are graded
mechanically ("compiles / lint / renders") rather than against intent, and integration is nobody's
job. The fix is an authoritative, versioned Epic that every task can walk up to, so "what am I
serving, and what does done-for-this look like" is always answerable.
A third, practical problem shapes the ordering: the design document's "don't rebuild, delegate"
principle is correct in spirit, but several primitives it assumes do not exist in mcpctl yet.
Those are enumerated under **Execution Substrate Changes** and must land before any agent can work
a task.
## Scope & Non-Goals
**In scope** (design doc §16 steps 1–3):
1. Epic / EpicTask / Task / CodeRepo entities + AgentProject membership, with project linkage,
mcpd CRUD, CLI verbs, RBAC, backup, docs, and tests.
2. An `AgentRuntime` adapter interface and a first implementation running a headless coding agent
inside an ephemeral per-session pod. Prove: pod spawn → clone repos → work a task branch →
push → report.
3. Task lifecycle with QA / Security / SRE stages, PR linkage, the human accept gate, and merge
through the git provider API, with post-merge failure reopening the task.
**Explicitly out of scope** — these are named in *Deferred* below and get placeholder tasks only:
the Tier-2 design/product critic and Epic-comparison loop; collaboration modes (standup, joint
`exec` sessions as a workflow); the confidence signal and escalation dashboard; per-persona memory
and the coverage ledger; script artifacts and the baseline scorecard.
**Not being built at all:** a permission engine. mcpctl enforces coarse gates (who may transition
a task, what RBAC a session needs); fine-grained per-tool approval stays inside the agent runtime.
## Decisions Already Made
| Decision | Choice | Rationale |
|---|---|---|
| Work-item RBAC name | Work items take `tasks`; the inference queue moves to `inference-tasks` | Matches the design doc's vocabulary. The `inference-tasks` alias already exists, so the URL prefix `/api/v1/inference-tasks` is unchanged |
| Roadmap-node spelling | Model `EpicTask`, resource `epictasks`, aliases `epictask`, `et` | Design doc §15 Q2; one word, matches the doc's own usage |
| Git-repo resource name | Model `CodeRepo`, resource `repos`, aliases `repo`, `repository`, `repositories` | Avoids `repository.repository.ts` and the collision with the data-access-layer term used throughout `src/mcpd/src/repositories/` |
| Epic revisions | Epic joins the existing `ResourceRevision` system as `resourceType: 'epic'` | The Epic is the auditable source of truth for "is this done"; scope changes must be visible. `ResourceType` is a string union by design, so this is a non-migration change |
| Agent membership | New `AgentProject` junction; `Agent.projectId` retained and kept in sync for one release | Design doc §4. `ProjectMember` was deliberately dropped in migration `20260223120000_remove_project_members`; this is a narrower reversal — agent membership only, not human membership, which stays RBAC-driven |
| Invariants | Enforced in the service layer, not CHECK constraints | See *Deployment constraint* below |
| Agent brain | Home brain (`Agent.llmId`) for bare chat; the project's `llmProvider`/`llmModel` when the agent acts inside a project; optional per-membership override on `AgentProject.llmId` | Design doc §15 Q1 leans this way. **This is the one open question that blocks the data model** — the `AgentProject.llmId` column exists so the decision can be reversed without a migration |
### Deployment constraint (affects every schema task)
`deploy/entrypoint.sh` provisions the database with `prisma db push --accept-data-loss`, not
`prisma migrate deploy`. Hand-written SQL in `src/db/prisma/migrations/` therefore **never runs in
production**. The single existing CHECK constraint, `ChatThread_agent_xor_project` (migration
`20260717234728_add_project_chat_threads`), is absent from any `db push`-provisioned database and
is enforced only by `ChatService`.
Consequence: every invariant introduced here must be either expressible in the Prisma schema
(unique constraints, non-null columns, enums, foreign keys) or enforced in the service layer with
a test. Do not write CHECK constraints and assume they hold.
## Core Concepts
### The vision layer
```
Epic (the vision — project-scoped, versioned, revision-tracked)
└── EpicTask — a roadmap node; chains to other epictasks (dependency graph)
└── Task — the unit of work, with its own lifecycle
```
An **Epic** holds the agreed done-criteria written at vision time, its scope-change history, and
accepted-tradeoff decisions. An Epic is done only when 100% of the vision is delivered, **or** the
user explicitly accepted the tradeoffs — recorded so "done" is honest and auditable.
An **EpicTask** is a roadmap node. EpicTasks chain to one another, so visualising them yields a
dependency graph rather than a flat sequence. Each carries the decomposed subset of the Epic's
acceptance criteria, so per-node completeness is computable.
A **Task** is the unit of work, owned by an EpicTask. Because Task → EpicTask → Epic, every task
can resolve what it is serving by walking up the chain.
### Two-level verification
| Level | Checks | Who | When |
|---|---|---|---|
| Tier 1 — correctness / security / ops | Builds, runs, passes tests, stays secure, and the deployment-as-code is right for this project's real infrastructure | Developer + QA + Security + SRE personas | Inside the task's own lifecycle, before Done |
| Tier 2 — vision fidelity | Does the assembled result match the vision | Design/product critic | At the Epic level — **deferred, not in this PRD** |
Tier 1 stages are gates *inside* the task loop. Their feedback bounces the task back to
`InProgress`. They are not post-submit reviewers.
The **SRE/Release stage** reviews the deployment-as-code — is it targeting the right
infrastructure, by the right method, with the right tooling. It reviews the deployment
*definition*; it does not deploy. It exists because a large share of failures come from implicit
operational rules a developer LLM does not know. Concrete example: this cluster runs default-deny
network policies, so any new workload needs explicit egress and ingress rules. A developer LLM
watching a connection fail will deep-dive the network-debugging path when the real cause is a
missing egress policy silently dropping traffic. The SRE persona, holding per-project infra
knowledge, flags that in review.
### Agents as people
An agent exists independently of any project — a persistent identity with a name, description,
system prompt, and a home brain. It joins zero, one, or many projects. Per-project role and
scope hang off the membership junction, not off the agent.
### Pod per session
Each agent session gets its own ephemeral pod. The pod *is* the workspace: an init container
clones the project's repositories, the session is attached to project prompts, MCP tools, and
scoped secrets, and the pod dies at session end. Agents do not share a live filesystem with each
other — handoffs happen via git (branch + diff + verdict pinned to a commit). A single human may
share one agent's environment via `exec`.
## Schema Changes
All new models follow existing house conventions: `id String @id @default(cuid())`,
`version Int @default(1)` as an optimistic-concurrency counter bumped unconditionally in the
repository layer (see `agent.repository.ts` `updateData.version = { increment: 1 }`), plus
`createdAt` / `updatedAt`.
### Epic
```prisma
model Epic {
id String @id @default(cuid())
name String
description String @default("")
// The agreed statement of the vision. Body of the Epic.
content String @db.Text
// Checkable outcomes + intent/quality attributes that define 100% delivery.
// Array of { id, text, kind: 'outcome' | 'quality', met: boolean }.
doneCriteria Json @default("[]")
// Accepted-tradeoff records: { what, why, who, deScopedBy, at }.
acceptedTradeoffs Json @default("[]")
projectId String
status EpicStatus @default(open)
semver String @default("0.1.0")
currentRevisionId String?
ownerId String
version Int @default(1)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
epicTasks EpicTask[]
@@unique([name, projectId])
@@index([projectId])
@@index([name])
}
enum EpicStatus {
open
delivered // 100% of doneCriteria met
delivered_with_tradeoffs
cancelled
}
```
### EpicTask
```prisma
model EpicTask {
id String @id @default(cuid())
name String
description String @default("")
epicId String
// Decomposed subset of the Epic's doneCriteria this node is responsible for.
doneCriteria Json @default("[]")
status EpicTaskStatus @default(todo)
ownerId String
version Int @default(1)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
epic Epic @relation(fields: [epicId], references: [id], onDelete: Cascade)
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
tasks Task[]
dependsOn EpicTaskEdge[] @relation("EpicTaskEdgeFrom")
blocks EpicTaskEdge[] @relation("EpicTaskEdgeTo")
@@unique([name, epicId])
@@index([epicId])
}
// Chaining between roadmap nodes. Modelled as an explicit edge table rather
// than a self-referencing array so the graph can be walked in both directions
// and edges can later carry a type.
model EpicTaskEdge {
id String @id @default(cuid())
fromId String // the dependent node
toId String // the prerequisite node
createdAt DateTime @default(now())
from EpicTask @relation("EpicTaskEdgeFrom", fields: [fromId], references: [id], onDelete: Cascade)
to EpicTask @relation("EpicTaskEdgeTo", fields: [toId], references: [id], onDelete: Cascade)
@@unique([fromId, toId])
@@index([fromId])
@@index([toId])
}
enum EpicTaskStatus {
todo
in_progress
done
cancelled
}
```
Cycle prevention in the edge graph is a service-layer check (see *Deployment constraint*): before
creating an edge, walk `to`'s transitive dependencies and reject if `from` is reachable.
### Task
```prisma
model Task {
id String @id @default(cuid())
name String
title String
description String @default("") @db.Text
epicTaskId String
repoId String?
// The agent currently responsible. Null while unassigned.
assigneeId String?
state TaskState @default(todo)
// Drives how much Tier-1 verification is engaged: trivial work may pass on a
// light self-check; risky work engages QA / Security / SRE.
risk TaskRisk @default(normal)
// Git linkage, populated when the agent pushes.
branch String?
headCommit String?
prUrl String?
prNumber Int?
// Free-form reviewer comment attached at the accept gate.
reviewNote String? @db.Text
ownerId String
version Int @default(1)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
epicTask EpicTask @relation(fields: [epicTaskId], references: [id], onDelete: Cascade)
repo CodeRepo? @relation(fields: [repoId], references: [id], onDelete: SetNull)
assignee Agent? @relation(fields: [assigneeId], references: [id], onDelete: SetNull)
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
verdicts TaskVerdict[]
sessions AgentSession[]
events TaskEvent[]
@@unique([name, epicTaskId])
@@index([epicTaskId])
@@index([state])
@@index([assigneeId])
}
enum TaskState {
todo
in_progress
building // compile, run, smoke
self_testing // developer's own tests
qa_review
security_review
sre_review
done // leaves the developer's loop
awaiting_review // human accept gate
merged
reopened // post-merge failure
cancelled
}
enum TaskRisk {
trivial // docs, formatting — light self-check may suffice
normal
high // auth, secrets, migrations, cross-contract
}
// A Tier-1 persona's verdict, pinned to the commit it judged. Immutable —
// this is the provenance record the design doc calls "QA verified 3f2ab".
model TaskVerdict {
id String @id @default(cuid())
taskId String
stage TaskState // qa_review | security_review | sre_review
agentId String?
commit String
passed Boolean
summary String @db.Text
// Structured findings: [{ severity, message, path?, line? }]
findings Json @default("[]")
createdAt DateTime @default(now())
task Task @relation(fields: [taskId], references: [id], onDelete: Cascade)
agent Agent? @relation(fields: [agentId], references: [id], onDelete: SetNull)
@@index([taskId])
@@index([taskId, stage])
}
// Append-only transition log. Every state change writes one row.
model TaskEvent {
id String @id @default(cuid())
taskId String
fromState TaskState?
toState TaskState
actorType String // 'user' | 'agent' | 'system'
actorId String?
note String? @db.Text
createdAt DateTime @default(now())
task Task @relation(fields: [taskId], references: [id], onDelete: Cascade)
@@index([taskId, createdAt])
}
```
### CodeRepo
```prisma
model CodeRepo {
id String @id @default(cuid())
name String
projectId String
// Clone URL. https for token auth, ssh for key auth.
url String
defaultBranch String @default("main")
// Relative path under the session workspace root. Defaults to `name`.
path String @default("")
// Git provider, for PR/merge API calls. 'gitea' is the only driver in v1.
provider String @default("gitea")
// Secret holding the provider credential. Resolved through SecretService at
// session-provision time — never stored in plaintext on the row.
credSecretId String?
credSecretKey String?
ownerId String
version Int @default(1)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
cred Secret? @relation(fields: [credSecretId], references: [id])
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
tasks Task[]
@@unique([name, projectId])
@@index([projectId])
}
```
### AgentProject
Mirrors `ProjectServer` (`schema.prisma`): explicit join model with its own cuid `id`, `createdAt`,
cascade on both sides, `@@unique([a, b])`. Indexes on both columns follow `PersonalityPrompt`,
because both reverse lookups are needed ("agents of project", "projects of agent").
```prisma
model AgentProject {
id String @id @default(cuid())
agentId String
projectId String
// Per-project persona role. TEXT, not an enum, so adding a role later is a
// non-migration change (same reasoning as ResourceProposal.resourceType).
// v1 values: 'developer' | 'qa' | 'security' | 'sre'.
role String @default("developer")
// Optional per-membership brain override. Present so the open question in
// docs/agentic-teams.md §15 Q1 can be answered either way without a migration.
llmId String?
createdAt DateTime @default(now())
agent Agent @relation(fields: [agentId], references: [id], onDelete: Cascade)
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
llm Llm? @relation(fields: [llmId], references: [id], onDelete: SetNull)
@@unique([agentId, projectId])
@@index([agentId])
@@index([projectId])
}
```
`Agent.projectId` is **retained** and written in sync with the junction for one release, so
existing `prepareContext` / `findByProjectId` paths keep working. A follow-up removes it.
### AgentSession
```prisma
model AgentSession {
id String @id @default(cuid())
agentId String
projectId String
taskId String?
// Orchestrator handle — pod name on k8s, container id on docker.
containerId String?
state SessionState @default(pending)
image String
// Reaped when now() > expiresAt. Swept by the same style of GC loop as the
// inference-task sweep in main.ts.
expiresAt DateTime
lastError String? @db.Text
ownerId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
endedAt DateTime?
agent Agent @relation(fields: [agentId], references: [id], onDelete: Cascade)
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
task Task? @relation(fields: [taskId], references: [id], onDelete: SetNull)
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
@@index([state])
@@index([taskId])
@@index([agentId])
}
enum SessionState {
pending
provisioning
running
ended
failed
}
```
### Back-relations on existing models
Prisma requires both sides of every relation. The blocks above show only the owning side; the
following fields must be added to existing models or `prisma generate` fails:
| Model | Fields to add |
|---|---|
| `Project` | `epics Epic[]`, `codeRepos CodeRepo[]`, `agentMemberships AgentProject[]`, `sessions AgentSession[]` |
| `User` | `ownedEpics Epic[]`, `ownedEpicTasks EpicTask[]`, `ownedTasks Task[]`, `ownedRepos CodeRepo[]`, `ownedSessions AgentSession[]` |
| `Agent` | `projectMemberships AgentProject[]`, `assignedTasks Task[]`, `verdicts TaskVerdict[]`, `sessions AgentSession[]` |
| `Llm` | `agentProjects AgentProject[]` |
| `Secret` | `codeRepos CodeRepo[]` |
`Agent.projectId` and its `project Project?` relation stay as-is during the transition release, so
`Project` carries both `agents Agent[]` (existing) and `agentMemberships AgentProject[]` (new).
### Test helper
`src/db/tests/helpers.ts` `clearAllTables` must gain `deleteMany` calls for every new model —
globals with nullable foreign keys do not cascade.
## API Changes
All routes sit under `/api/v1`, follow the existing Fastify + zod-in-the-service pattern, and
resolve `project` / `agent` name strings to ids at the route layer (as `routes/skills.ts` does).
### New endpoints
```
# Vision layer
GET|POST /api/v1/epics ?project=<name>
GET|PUT|DELETE /api/v1/epics/:id
GET /api/v1/epics/:id/roadmap → epictask graph + per-node completeness
POST /api/v1/epics/:id/tradeoffs → append an accepted-tradeoff record
GET|POST /api/v1/epictasks ?epic=<name>&project=<name>
GET|PUT|DELETE /api/v1/epictasks/:id
POST|DELETE /api/v1/epictasks/:id/edges → { toId } add/remove a dependency edge
# Work items
GET|POST /api/v1/tasks ?project=&epictask=&state=&assignee=
GET|PUT|DELETE /api/v1/tasks/:id
POST /api/v1/tasks/:id/transition → { toState, note? }
POST /api/v1/tasks/:id/verdict → record a QA/Security/SRE verdict
POST /api/v1/tasks/:id/accept → { note? } human accept gate → merge
POST /api/v1/tasks/:id/reject → { note } → back to in_progress
GET /api/v1/tasks/:id/events
# Repositories
GET|POST /api/v1/repos ?project=<name>
GET|PUT|DELETE /api/v1/repos/:id
# Agent membership
GET /api/v1/agents/:name/projects
POST|DELETE /api/v1/agents/:name/projects/:project → { role?, llm? }
GET /api/v1/projects/:name/agents
# Sessions & exec
GET|POST /api/v1/sessions ?project=&agent=&task=
GET|DELETE /api/v1/sessions/:id
POST /api/v1/sessions/:id/exec → one-shot exec, returns { exitCode, stdout, stderr }
GET /api/v1/sessions/:id/attach → WebSocket, bidirectional stdio
# Git provider webhook
POST /api/v1/webhooks/git/:provider → post-merge failure reopens the task
```
### Changed endpoints
`/api/v1/inference-tasks/*` is unchanged in shape. Only its RBAC resource name changes (see
below).
## RBAC Changes
### The `tasks` namespace split
`src/mcpd/src/validation/rbac-definition.schema.ts` currently declares `tasks` in
`RBAC_RESOURCES` and aliases `task`, `inference-task`, and `inference-tasks` all onto it — with
the comment "the only 'task' we have is inference for now". That is no longer true.
Required changes:
1. `RBAC_RESOURCES` gains `epics`, `epictasks`, `tasks` (now meaning work items), `repos`,
`sessions`, and `inference-tasks`.
2. `RESOURCE_ALIASES` maps `task → tasks`, `epic → epics`, `epictask → epictasks`,
`et → epictasks`, `repo → repos`, `repository → repos`, `repositories → repos`,
`session → sessions`, and `inference-task → inference-tasks`. The entry
`'inference-tasks': 'tasks'` is **removed**.
3. `mapUrlToPermission` in `src/mcpd/src/main.ts` gains `epics`, `epictasks`, `tasks`, `repos`,
`sessions`, and maps `/api/v1/inference-tasks` to `inference-tasks`. Non-CRUD verbs need
explicit cases above the map: `POST /tasks/:id/accept|reject|transition` → `run:tasks:<name>`,
`POST /sessions/:id/exec` and `GET /sessions/:id/attach` → `run:sessions:<name>`.
4. `nameResolvers` (`main.ts`) gains `epics`, `epictasks`, `tasks`, `repos`, `sessions` so
name-scoped grants match when the URL carries a cuid.
### One-shot binding backfill
`ResourceBindingSchema` transforms `resource` through `normalizeResource` **at parse time** and
stores the normalized string, so existing `RbacDefinition.roleBindings` rows already contain the
literal `"tasks"` meaning *inference*. After the split, those grants would silently become
work-item grants.
A one-shot startup migration must rewrite `resource: "tasks"` → `"inference-tasks"` across all
`RbacDefinition` rows. Model the walk on `migrateAdminRole` in `src/mcpd/src/main.ts` (it iterates
`rbacRepo.findAll()` and rewrites `roleBindings` in place).
It must be **guarded so it runs exactly once** — a blanket rewrite on every boot would clobber
legitimate new work-item grants. Use the existing system-settings pattern: a boolean key in the
`mcpctl-system-settings` Secret (`SYSTEM_SETTINGS_SECRET` in
`src/mcpd/src/bootstrap/system-settings.ts`), read before the walk and written after. Wildcard
bindings (`resource: '*'`) are untouched — they already cover both.
Document the rename in `docs/inference-tasks.md`.
## CLI Changes
Two project rules govern every resource added here:
- Everything settable via `apply -f` **must** also be a `create` flag.
- `mcpctl get RESOURCE NAME -o yaml` **must** round-trip through `mcpctl apply -f`.
### New commands
```
mcpctl get epics|epictasks|tasks|repos|sessions [--project NAME]
mcpctl describe epic|epictask|task|repo|session NAME
mcpctl create epic|epictask|task|repo ...
mcpctl edit epic|epictask|task|repo NAME
mcpctl delete epic|epictask|task|repo NAME
mcpctl describe epic NAME # roadmap: epictask graph + per-node completeness
mcpctl tasks accept NAME [--note ..] # human accept gate → merge via provider API
mcpctl tasks reject NAME --note .. # → back to in_progress
mcpctl exec --project NAME AGENT -- bash
```
`mcpctl exec` semantics (design doc §10): exec drops the human into the **working environment** —
the cloned repos, branches, tools, and deps. It does **not** attach to the agent's reasoning loop.
The agent remains a separate process the human can still talk to.
### Files to touch
| Concern | File |
|---|---|
| Resource aliases | `src/cli/src/commands/shared.ts` `RESOURCE_ALIASES` |
| `get` columns + yaml kind | `src/cli/src/commands/get.ts` — row interfaces, `getColumnsForResource`, `RESOURCE_KIND`, `toApplyDocs` strip blocks |
| `describe` formatters | `src/cli/src/commands/describe.ts` — clean sections and tables, not key-value dumps |
| `create` subcommands | `src/cli/src/commands/create.ts` |
| `apply -f` | `src/cli/src/commands/apply.ts` — spec schemas, `ApplyConfigSchema` keys, `KIND_TO_RESOURCE`, dry-run counts, apply loops |
| `edit` | `src/cli/src/commands/edit.ts` `validResources` |
| Scoped fetch | `src/cli/src/index.ts` `fetchResource` / `fetchSingleResource` for `--project` scoping |
| Completions | `scripts/generate-completions.ts` `CANONICAL_RESOURCES` + `ALIAS_ENTRIES`, then `pnpm completions:generate` |
Completions are generated, never hand-edited; `src/cli/tests/completions.test.ts` runs the
generator with `--check` and fails on drift.
## Execution Substrate Changes
These are the primitives the design document assumes but mcpctl does not have. They gate step 2.
### 1. Ephemeral pods
`ContainerSpec` in `src/mcpd/src/services/orchestrator.ts` has only image, name, command, env,
ports, limits, labels, and network. `generatePodSpec` in
`src/mcpd/src/services/k8s/manifest-generator.ts` hardcodes `restartPolicy: 'Always'`, and no code
anywhere uses the Kubernetes Job API.
`ContainerSpec` must gain:
- `restartPolicy?: 'Always' | 'Never' | 'OnFailure'`
- `workdir?: string`
- `volumes?: Array<{ name: string; mountPath: string; kind: 'emptyDir' }>` — the session
workspace
- `initContainers?: Array<{ name; image; command; env?; volumeMounts? }>` — the repo-clone step
Both backends implement them: `KubernetesOrchestrator` and `DockerContainerManager`.
### 2. Session provisioning
A `SessionService` that: creates the `AgentSession` row; resolves the project's `CodeRepo` list and
their credentials through `SecretService` (resolution failure is fatal for the attempt, matching
`InstanceService.attemptStart`); builds a spec with an init container that clones each repo to its
`path` under the workspace volume and checks out the task branch; starts the pod; and transitions
the row to `running`.
Reaping runs as a GC sweep in the same style as the existing inference-task sweep in
`src/mcpd/src/main.ts`: sessions past `expiresAt` are stopped, removed, and marked `ended`. Note
these loops assume a single mcpd replica — there is no leader election, and this inherits that.
### 3. Exec surface
The hard part already exists. `McpOrchestrator` declares `execInContainer`, `execInteractive`, and
`attachInteractive`; `KubernetesOrchestrator` implements the latter two over `k8s.Exec` and
`k8s.Attach`, and `src/mcpd/src/services/transport/persistent-stdio.ts` uses them in production
today. This work is an **API and CLI surface over a proven primitive**: a one-shot exec route, a
WebSocket attach route with terminal resize and proper exit-code propagation, and the
`mcpctl exec` client.
### 4. Git provider client
No Gitea or GitHub API client, PR creation, or webhook handling exists in product code. PRs are
made today by the root-level `pr.sh` (curl against the Gitea API) or by an agent calling the
`gitea-mcp-server` MCP tool.
A `GitProvider` interface in mcpd with a Gitea driver: `createPullRequest`, `getPullRequest`,
`getCombinedStatus`, `mergePullRequest`. Credentials resolve from the `CodeRepo`'s Secret.
`pr.sh` is the reference for the endpoint shapes (`POST /api/v1/repos/:owner/:repo/pulls`).
A webhook receiver validates the provider signature and reopens the task on post-merge failure.
## AgentRuntime Interface
mcpctl builds an *interface*, not a runtime. `ChatService` is not a candidate — its tool loop is
capped at `MAX_ITERATIONS = 12` (clamped 1..50), which is a soft-DoS guard appropriate for chat
and far too small for a coding task.
```ts
export interface AgentRuntime {
startSession(opts: RuntimeSessionOpts): Promise<RuntimeSession>;
send(sessionId: string, message: string): Promise<void>;
status(sessionId: string): Promise<RuntimeStatus>;
result(sessionId: string): Promise<RuntimeResult>; // branch, head commit, summary
endSession(sessionId: string): Promise<void>;
}
```
Implementation #1 runs a battle-tested headless coding agent inside the session pod. It already
solves per-tool permission approval, git and branch mechanics, subagents, streaming, and MCP
client behaviour — all things the design document explicitly declines to rebuild. The persona is
delivered as prompt files in the container, which suits a file-aware coding runtime.
A second adapter is explicitly deferred; the interface exists so it stays possible.
## Task Lifecycle
```
todo → in_progress → building → self_testing
→ qa_review (feedback → in_progress)
→ security_review (feedback → in_progress)
→ sre_review (feedback → in_progress)
→ done → awaiting_review
accept → merged
reject → in_progress
merged --post-merge failure (webhook)--> reopened
```
Verification effort scales with `Task.risk`: `trivial` work may pass on a light self-check;
`high` engages all three review stages. The stages decide their own depth.
Transitions are guarded by a table of legal `(from, to)` pairs and applied as **compare-and-swap**
— `updateMany` with the expected `state` in the `where` clause, then check the affected-row count.
This is the same idiom `InferenceTaskRepository` uses so two racing claimants cannot both win.
Every transition appends a `TaskEvent`.
The human approval gate is **not** a per-tool tri-state. It is a task-level yes/no plus a comment
on the resolved outcome, and accepting it fires the merge. The human should not have to go to git.
## Testing Strategy
### Unit — `src/db/tests/`
- Schema tests per model (pattern: `skill-schema.test.ts`): create, unique constraints, cascade
behaviour on project/epic/task delete, `AgentProject` uniqueness.
- `clearAllTables` in `helpers.ts` covers every new model.
### Unit — `src/mcpd/tests/`
- Service tests: Epic done-criteria roll-up; EpicTask edge cycle rejection; task transition table
including every illegal pair; compare-and-swap under concurrent transition.
- Route tests per resource (pattern: `prompt-routes.test.ts`, `agent-routes.test.ts`).
- RBAC: the `tasks` → `inference-tasks` backfill runs once and is idempotent on a second boot;
a pre-existing `view:tasks` grant still authorizes `/api/v1/inference-tasks` afterwards and does
**not** authorize `/api/v1/tasks`; name-scoped grants resolve for all new resources.
- Orchestrator: `generatePodSpec` emits `restartPolicy`, workdir, volumes, and init containers.
- Git provider: Gitea driver against a mocked HTTP server.
### Unit — `src/cli/tests/`
- `get -o yaml` → `apply -f` round-trip for every new resource.
- `create` flag coverage: every field settable in `apply` has a matching flag.
- `completions.test.ts` assertions for the new resource words.
### Smoke — `src/mcplocal/tests/smoke/`
Every feature ships smoke tests. Follow the CRUD pattern in `personality.smoke.test.ts`: a
`Date.now().toString(36)` name suffix, a `healthz()` preflight that **skips rather than fails**
when the daemon is unreachable, `mcpctl --direct` shelling, and explicit `afterAll` cleanup.
- `epic.smoke.test.ts` — Epic → EpicTask → Task create/list/describe/delete against live mcpd.
- `repo.smoke.test.ts` — CodeRepo CRUD, credential reference resolves.
- `session.smoke.test.ts` — provision a session pod against the real cluster, confirm the repo is
cloned, exec a command, confirm the pod is reaped.
- `task-lifecycle.smoke.test.ts` — drive a task through the full transition chain including a
rejected accept-gate bounce.
When a smoke test fails, fix the environment (deploy, backend, RBAC) — never weaken the assertion.
## Deferred
Named here so the boundary is explicit. Each gets a single placeholder task with no detail, to be
expanded once the corresponding open questions in `docs/agentic-teams.md` §15 are closed.
- **Tier-2 vision critic and Epic-comparison.** A review-only, non-executing design/product
persona that judges the assembled result against the Epic in its native medium, producing
reopen / new-task / `exec` outcomes. Blocked on §15 Q13 (does this need render/screenshot
infrastructure, or can a design-aware persona judge from a structured artifact?).
- **Collaboration modes.** On-demand async standup (`mcpctl standup --project NAME`) and `exec`
as a workflow rather than a command. Blocked on §15 Q4 and Q5.
- **Confidence signal and escalation dashboard.** Evidence-anchored confidence (retrieve K similar
past tasks with their *outcomes*, ask how confident we are this resolves cleanly), thresholds
driving autonomy, and a single cross-project human decision queue. Blocked on §15 Q12
(web / TUI / CLI?). Retrieval quality is the crux — weak retrieval quietly poisons the model.
- **Memory and the coverage ledger.** Global per-agent memory plus per-project knowledge, and a
commit-pinned coverage ledger enabling delta-verification. Non-negotiable prerequisite:
invalidation when a covered region changes, or the ledger becomes false confidence. Keep the
trust boundary — the ledger is platform-verified machine fact; agent prose notes are opinion and
never the gate.
- **Script artifacts and the baseline scorecard.** Reusable executable `script` artifacts at task,
project, and agent scope, plus a provisioned baseline-rules scorecard. Blocked on §15 Q15 and
Q16. Watch the incentive integrity risk: reward *reused* tools, not tool proliferation.
## Implementation Phases
**Phase A — foundation (blocks everything)**
1. RBAC `tasks` namespace split, alias repoint, and the one-shot guarded binding backfill.
2. Prisma models Epic, EpicTask, EpicTaskEdge, Task, TaskVerdict, TaskEvent, CodeRepo,
AgentSession + enums + migration + `clearAllTables`.
3. `AgentProject` junction, with `Agent.projectId` kept in sync for one release.
**Phase B — mcpd CRUD**
4. Epic resource: repository, service, zod validation, routes, `main.ts` wiring, revision opt-in.
5. EpicTask resource plus the edge graph and cycle rejection.
6. Task resource plus the transition table and compare-and-swap transitions.
7. CodeRepo resource with credential resolution.
8. RBAC wiring for all new resources: `mapUrlToPermission`, `nameResolvers`, non-CRUD verb cases.
9. Backup and YAML kinds for the new resources — `BACKUP_KINDS`, `APPLY_ORDER`, `kindMap`, the
export loop, `serializeResource`, and `main.ts` `kindFromSegment` / `importResource` /
`deleteResource`. Fix the pre-existing gap where `skill` is missing from the latter two.
**Phase C — CLI**
10. `get` columns and `describe` formatters for all new resources.
11. `create`, `apply -f`, `edit`, `delete`, with the yaml round-trip test.
12. Shell completions and their guard test.
13. Roadmap view: `describe epic` rendering the epictask graph with per-node completeness.
**Phase D — execution substrate**
14. Extend `ContainerSpec` and both orchestrator backends with restart policy, workdir, volumes,
and init containers.
15. `SessionService`: provisioning, repo-clone init container, credential resolution, TTL and GC.
16. `AgentRuntime` interface plus adapter #1 running a headless coding agent in-pod.
17. Exec surface: one-shot exec route, WebSocket attach, and `mcpctl exec`.
**Phase E — lifecycle and PR**
18. Task lifecycle state machine, transition API, and `TaskEvent` log.
19. `GitProvider` interface plus the Gitea driver.
20. PR linkage and the human accept gate firing merge through the provider API.
21. QA / Security / SRE stage verdicts, risk-scaled, pinned to a commit.
22. Post-merge failure webhook reopening the task.
**Phase F — cross-cutting**
23. Documentation: `docs/epics.md`, `docs/tasks.md`, `docs/repos.md`, `docs/sessions.md`, the
README resources table, and the CLAUDE.md resource cheatsheet.
24. Smoke tests for every new resource plus the session and lifecycle flows.
**Phase G — deferred placeholders**
25. Tier-2 vision critic and Epic-comparison loop.
26. Collaboration modes: async standup and `exec` joint sessions.
27. Confidence signal and escalation dashboard.
28. Per-agent memory and the coverage ledger.
29. Script artifacts and the baseline scorecard.

View File

@@ -0,0 +1,35 @@
# Task ID: 25
**Title:** Rename local-proxy to mcplocal
**Status:** done
**Dependencies:** None
**Priority:** high
**Description:** Rename the src/local-proxy directory to src/mcplocal and update all package references, imports, and build configurations throughout the monorepo.
**Details:**
1. Rename directory: `mv src/local-proxy src/mcplocal`
2. Update package.json name from `@mcpctl/local-proxy` to `@mcpctl/mcplocal`
3. Update pnpm-workspace.yaml if needed
4. Update all imports in other packages that reference local-proxy:
- Search for `@mcpctl/local-proxy` and replace with `@mcpctl/mcplocal`
- Check tsconfig references and path mappings
5. Update any scripts in package.json root that reference local-proxy
6. Update docker-compose files in deploy/ if they reference local-proxy
7. Update documentation and README references
8. Run `pnpm install` to regenerate lockfile with new package name
9. Verify TypeScript compilation succeeds: `pnpm build`
10. Run existing tests to ensure nothing broke: `pnpm test`
**Test Strategy:**
1. Verify directory rename completed: `ls src/mcplocal`
2. Verify package.json has correct name
3. Run `pnpm install` - should complete without errors
4. Run `pnpm build` - all packages should compile
5. Run `pnpm test` - all existing tests should pass
6. Grep codebase for 'local-proxy' - should find no stale references except git history

View File

@@ -0,0 +1,46 @@
# Task ID: 26
**Title:** Add HTTP REST server to mcplocal
**Status:** done
**Dependencies:** 25 ✓
**Priority:** high
**Description:** Add a Fastify HTTP server to mcplocal that runs alongside the existing stdio server, providing REST endpoints for mcpctl management commands.
**Details:**
1. Add Fastify dependency to mcplocal package.json: `@fastify/cors`, `fastify`
2. Create `src/mcplocal/src/http/server.ts` with Fastify app setup:
```typescript
import Fastify from 'fastify';
import cors from '@fastify/cors';
export async function createHttpServer(config: HttpServerConfig) {
const app = Fastify({ logger: true });
await app.register(cors, { origin: true });
// Register routes
return app;
}
```
3. Create `src/mcplocal/src/http/routes/` directory structure
4. Create health check endpoint: `GET /health`
5. Create config types in `src/mcplocal/src/config.ts`:
- `httpPort`: number (default 3200)
- `httpHost`: string (default '127.0.0.1')
- `mcpdUrl`: string (default 'http://localhost:3100')
6. Update mcplocal entry point to start both servers:
- stdio server for Claude MCP protocol
- HTTP server for mcpctl REST API
7. Add graceful shutdown handling for both servers
**Test Strategy:**
1. Unit test: HTTP server starts on configured port
2. Unit test: Health endpoint returns 200 OK
3. Integration test: Both stdio and HTTP servers can run simultaneously
4. Test graceful shutdown stops both servers cleanly
5. Test CORS headers are present on responses
6. Manual test: curl http://localhost:3200/health

View File

@@ -0,0 +1,130 @@
# Task ID: 27
**Title:** Implement mcplocal management proxy routes
**Status:** done
**Dependencies:** 26 ✓
**Priority:** high
**Description:** Add REST endpoints to mcplocal that mirror mcpd's API and proxy management requests to mcpd without LLM processing. All requests must include proper authentication to mcpd using a Bearer token read from mcplocal config.
**Details:**
1. Create HTTP client for mcpd communication with auth: `src/local-proxy/src/http/mcpd-client.ts`
```typescript
export class McpdClient {
private token: string;
constructor(private baseUrl: string, token: string) {
this.token = token;
}
private getHeaders(): Record<string, string> {
return {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.token}`
};
}
async get<T>(path: string): Promise<T> {
const response = await fetch(`${this.baseUrl}${path}`, {
method: 'GET',
headers: this.getHeaders()
});
await this.handleAuthError(response);
return response.json();
}
async post<T>(path: string, body: unknown): Promise<T> {
const response = await fetch(`${this.baseUrl}${path}`, {
method: 'POST',
headers: this.getHeaders(),
body: JSON.stringify(body)
});
await this.handleAuthError(response);
return response.json();
}
async put<T>(path: string, body: unknown): Promise<T> {
const response = await fetch(`${this.baseUrl}${path}`, {
method: 'PUT',
headers: this.getHeaders(),
body: JSON.stringify(body)
});
await this.handleAuthError(response);
return response.json();
}
async delete<T>(path: string): Promise<T> {
const response = await fetch(`${this.baseUrl}${path}`, {
method: 'DELETE',
headers: this.getHeaders()
});
await this.handleAuthError(response);
return response.json();
}
private async handleAuthError(response: Response): Promise<void> {
if (response.status === 401) {
throw new AuthenticationError('Invalid or expired token. Please check mcplocal config.');
}
}
}
export class AuthenticationError extends Error {
constructor(message: string) {
super(message);
this.name = 'AuthenticationError';
}
}
```
2. Add token to mcplocal config type (extend ProxyConfig or similar):
```typescript
export interface McpdAuthConfig {
/** Bearer token for mcpd API authentication */
mcpdToken: string;
}
```
3. Create proxy routes in `src/local-proxy/src/http/routes/`:
- `servers.ts`: GET/POST /api/v1/servers, GET/PUT/DELETE /api/v1/servers/:id
- `profiles.ts`: GET/POST /api/v1/profiles, GET/PUT/DELETE /api/v1/profiles/:id
- `instances.ts`: GET/POST /api/v1/instances, GET/POST/DELETE /api/v1/instances/:id, etc.
- `projects.ts`: GET/POST /api/v1/projects, etc.
- `audit.ts`: GET /api/v1/audit-logs
- `backup.ts`: POST /api/v1/backup, POST /api/v1/restore
4. Each route handler forwards to mcpd with auth:
```typescript
app.get('/api/v1/servers', async (req, reply) => {
try {
const result = await mcpdClient.get('/api/v1/servers');
return result;
} catch (error) {
if (error instanceof AuthenticationError) {
return reply.status(401).send({ error: error.message });
}
throw error;
}
});
```
5. Add comprehensive error handling:
- If mcpd is unreachable, return 503 Service Unavailable
- If mcpd returns 401, return 401 with clear message about token configuration
- Forward other HTTP errors from mcpd with appropriate status codes
6. Add request/response logging for debugging
**Test Strategy:**
1. Unit test: McpdClient attaches Authorization header to all request methods (GET, POST, PUT, DELETE)
2. Unit test: McpdClient throws AuthenticationError on 401 response from mcpd
3. Unit test: Each proxy route forwards requests correctly with auth headers
4. Unit test: Error handling when mcpd is unreachable (503 response)
5. Unit test: Error handling when mcpd returns 401 (clear error message returned)
6. Integration test: Full request flow mcpctl -> mcplocal -> mcpd with valid token
7. Integration test: Full request flow with invalid token returns 401
8. Test query parameters are forwarded correctly
9. Test request body is forwarded correctly for POST/PUT
10. Test path parameters (:id) are passed through correctly
11. Mock mcpd responses and verify mcplocal returns them unchanged
12. Test token is read correctly from mcplocal config

View File

@@ -0,0 +1,149 @@
# Task ID: 28
**Title:** Add MCP proxy endpoint to mcpd
**Status:** done
**Dependencies:** None
**Priority:** high
**Description:** Create a new endpoint in mcpd at /api/v1/mcp/proxy that accepts MCP tool call requests and executes them on managed MCP server instances. Also add authentication endpoints (login/logout) that mcpctl will use to authenticate users.
**Details:**
## MCP Proxy Endpoint
1. Create new route file: `src/mcpd/src/routes/mcp-proxy.ts`
2. Define request schema:
```typescript
interface McpProxyRequest {
serverId: string; // or instanceId
method: string; // e.g., 'tools/call', 'resources/read'
params: Record<string, unknown>;
}
```
3. Create McpProxyService in `src/mcpd/src/services/mcp-proxy-service.ts`:
- Look up instance by serverId (auto-start if profile allows)
- Connect to the container via stdio or HTTP (depending on transport type)
- Execute the MCP JSON-RPC call
- Return the result
4. Handle MCP JSON-RPC protocol:
```typescript
async executeCall(instanceId: string, method: string, params: unknown) {
const instance = await this.instanceService.getInstance(instanceId);
const connection = await this.getOrCreateConnection(instance);
const result = await connection.call(method, params);
return result;
}
```
5. Connection pooling: maintain persistent connections to running instances
6. Add route: `POST /api/v1/mcp/proxy` (must be behind auth middleware)
7. Add audit logging for all MCP proxy calls - include authenticated userId from request.userId
8. Handle errors: instance not found, instance not running, MCP call failed
## Authentication Endpoints
9. Create auth routes file: `src/mcpd/src/routes/auth.ts`
10. Implement `POST /api/v1/auth/login`:
- Request body: `{ username: string, password: string }`
- Validate credentials against User table (use bcrypt for password comparison)
- Create new Session record with token (use crypto.randomUUID or similar)
- Response: `{ token: string, expiresAt: string }`
11. Implement `POST /api/v1/auth/logout`:
- Requires Bearer token in Authorization header
- Delete/invalidate the Session record
- Response: `{ success: true }`
## Auth Integration Notes
- Existing auth middleware in `src/mcpd/src/middleware/auth.ts` validates Bearer tokens against Session table
- It sets `request.userId` on successful authentication
- MCP proxy endpoint MUST use this auth middleware
- Auth endpoints (login) should NOT require auth middleware
- Logout endpoint SHOULD require auth middleware to validate the session being invalidated
**Test Strategy:**
1. Unit test: Proxy service looks up correct instance
2. Unit test: JSON-RPC call is formatted correctly
3. Integration test: Full flow with a mock MCP server container
4. Test error handling: non-existent server returns 404
5. Test error handling: stopped instance returns appropriate error
6. Test audit log entries include authenticated userId
7. Test connection reuse for multiple calls to same instance
8. Test login endpoint: valid credentials return session token
9. Test login endpoint: invalid credentials return 401
10. Test logout endpoint: valid session is invalidated
11. Test logout endpoint: invalid/missing token returns 401
12. Test MCP proxy endpoint without auth token returns 401
13. Test MCP proxy endpoint with expired token returns 401
14. Test MCP proxy endpoint with valid token succeeds and logs userId in audit
## Subtasks
### 28.1. Create auth routes with login/logout endpoints
**Status:** pending
**Dependencies:** None
Create src/mcpd/src/routes/auth.ts with POST /api/v1/auth/login and POST /api/v1/auth/logout endpoints for mcpctl authentication.
**Details:**
Implement login endpoint: validate username/password against User table using bcrypt, create Session record with generated token and expiry. Implement logout endpoint: require auth middleware, delete/invalidate Session record. Login does NOT require auth, logout DOES require auth. Export registerAuthRoutes function and update routes/index.ts.
### 28.2. Create MCP proxy route file with auth middleware
**Status:** pending
**Dependencies:** 28.1
Create src/mcpd/src/routes/mcp-proxy.ts with POST /api/v1/mcp/proxy endpoint protected by auth middleware.
**Details:**
Define McpProxyRequest interface (serverId, method, params). Register route handler that extracts userId from request.userId (set by auth middleware). Apply auth middleware using preHandler hook. Validate request body schema.
### 28.3. Create McpProxyService for instance lookup and connection
**Status:** pending
**Dependencies:** None
Create src/mcpd/src/services/mcp-proxy-service.ts to handle instance lookup, connection management, and MCP call execution.
**Details:**
Implement getInstance to look up by serverId, auto-start if profile allows. Implement getOrCreateConnection for connection pooling. Handle both stdio and HTTP transports. Implement executeCall method that formats JSON-RPC call and returns result.
### 28.4. Implement MCP JSON-RPC call execution
**Status:** pending
**Dependencies:** 28.3
Implement the core JSON-RPC call logic in McpProxyService to execute tool calls on MCP server instances.
**Details:**
Format JSON-RPC 2.0 request with method and params. Send request over established connection (stdio/HTTP). Parse JSON-RPC response and handle errors. Return result or throw appropriate error for failed calls.
### 28.5. Add audit logging with userId for MCP proxy calls
**Status:** pending
**Dependencies:** 28.2, 28.4
Ensure all MCP proxy calls are logged to audit log including the authenticated userId from the session.
**Details:**
Use existing audit middleware/service. Include userId from request.userId in audit log entry. Log serverId, method, and outcome (success/failure). Log any errors that occur during MCP call execution.
### 28.6. Integrate auth and proxy routes into server.ts
**Status:** pending
**Dependencies:** 28.1, 28.2, 28.5
Register the new auth and mcp-proxy routes in the Fastify server with proper auth middleware wiring.
**Details:**
Update server.ts to register auth routes (no auth required for login). Register mcp-proxy routes with auth middleware. Ensure auth middleware is wired with findSession dependency from Prisma. Update routes/index.ts exports.

View File

@@ -0,0 +1,74 @@
# Task ID: 29
**Title:** Implement LLM pre-processing pipeline in mcplocal
**Status:** done
**Dependencies:** 25 ✓, 27 ✓, 28 ✓
**Priority:** high
**Description:** Create the core LLM pre-processing pipeline that intercepts MCP tool calls, uses a local LLM to optimize requests before sending to mcpd, and filters responses before returning to Claude.
**Details:**
1. Create `src/mcplocal/src/llm/processor.ts` - the core pipeline:
```typescript
export class LlmProcessor {
constructor(
private providerRegistry: ProviderRegistry,
private config: LlmProcessorConfig
) {}
async preprocessRequest(toolName: string, params: unknown): Promise<ProcessedRequest> {
// Use LLM to interpret and optimize the request
const prompt = this.buildRequestPrompt(toolName, params);
const result = await this.providerRegistry.getActiveProvider().complete({
systemPrompt: REQUEST_OPTIMIZATION_SYSTEM_PROMPT,
userPrompt: prompt
});
return this.parseOptimizedRequest(result);
}
async filterResponse(toolName: string, originalRequest: unknown, rawResponse: unknown): Promise<FilteredResponse> {
// Use LLM to filter/summarize the response
const prompt = this.buildFilterPrompt(toolName, originalRequest, rawResponse);
const result = await this.providerRegistry.getActiveProvider().complete({
systemPrompt: RESPONSE_FILTER_SYSTEM_PROMPT,
userPrompt: prompt
});
return this.parseFilteredResponse(result);
}
}
```
2. Create system prompts in `src/mcplocal/src/llm/prompts.ts`:
- REQUEST_OPTIMIZATION_SYSTEM_PROMPT: instruct LLM to generate optimal queries
- RESPONSE_FILTER_SYSTEM_PROMPT: instruct LLM to extract relevant information
3. Integrate into router.ts - wrap tools/call handler:
```typescript
async handleToolsCall(request: JsonRpcRequest) {
if (this.shouldPreprocess(request.params.name)) {
const processed = await this.llmProcessor.preprocessRequest(...);
// Call mcpd with processed request
const rawResponse = await this.callMcpd(processed);
const filtered = await this.llmProcessor.filterResponse(...);
return filtered;
}
return this.callMcpd(request.params);
}
```
4. Add configuration options:
- `enablePreprocessing`: boolean
- `preprocessingExclude`: string[] (tool names to skip)
- `preferredProvider`: string (ollama, gemini, deepseek, etc.)
5. Add bypass logic for simple operations (list, create, delete)
**Test Strategy:**
1. Unit test: Request preprocessing generates optimized queries
2. Unit test: Response filtering reduces data volume
3. Unit test: Bypass logic works for excluded tools
4. Integration test: Full pipeline with mock LLM provider
5. Test error handling: LLM failure falls back to unfiltered pass-through
6. Test configuration options are respected
7. Measure: response size reduction percentage

View File

@@ -0,0 +1,61 @@
# Task ID: 30
**Title:** Add Gemini CLI LLM provider
**Status:** done
**Dependencies:** 25 ✓
**Priority:** medium
**Description:** Implement a new LLM provider that uses the Gemini CLI binary for local, free LLM inference as the preferred provider for pre-processing.
**Details:**
1. Create `src/mcplocal/src/providers/gemini-cli.ts`:
```typescript
import { spawn } from 'child_process';
export class GeminiCliProvider implements LlmProvider {
readonly name = 'gemini-cli';
private binaryPath: string;
constructor(config: GeminiCliConfig) {
this.binaryPath = config.binaryPath || 'gemini';
}
async isAvailable(): Promise<boolean> {
// Check if gemini binary exists and is executable
try {
await this.runCommand(['--version']);
return true;
} catch {
return false;
}
}
async complete(options: CompletionOptions): Promise<CompletionResult> {
const input = this.formatPrompt(options);
const output = await this.runCommand(['--prompt', input]);
return { content: output, model: 'gemini-cli' };
}
private async runCommand(args: string[]): Promise<string> {
// Spawn gemini CLI process and capture output
}
}
```
2. Research actual Gemini CLI interface and adjust implementation
3. Add to provider registry with high priority (prefer over API providers)
4. Add configuration: `geminiCliBinaryPath`
5. Handle timeout for slow inference
6. Add fallback to next provider if Gemini CLI fails
**Test Strategy:**
1. Unit test: Provider correctly detects CLI availability
2. Unit test: Prompt formatting is correct
3. Unit test: Output parsing handles various formats
4. Integration test: Full completion with actual Gemini CLI (if available)
5. Test timeout handling for slow responses
6. Test fallback when CLI is not installed

View File

@@ -0,0 +1,59 @@
# Task ID: 31
**Title:** Add DeepSeek API LLM provider
**Status:** done
**Dependencies:** 25 ✓
**Priority:** medium
**Description:** Implement DeepSeek API provider as a cheap cloud-based fallback when local LLMs are unavailable.
**Details:**
1. Create `src/mcplocal/src/providers/deepseek.ts`:
```typescript
export class DeepSeekProvider implements LlmProvider {
readonly name = 'deepseek';
private apiKey: string;
private baseUrl = 'https://api.deepseek.com/v1';
constructor(config: DeepSeekConfig) {
this.apiKey = config.apiKey || process.env.DEEPSEEK_API_KEY;
}
async isAvailable(): Promise<boolean> {
return !!this.apiKey;
}
async complete(options: CompletionOptions): Promise<CompletionResult> {
// DeepSeek uses OpenAI-compatible API
const response = await fetch(`${this.baseUrl}/chat/completions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'deepseek-chat',
messages: [{ role: 'user', content: options.userPrompt }]
})
});
// Parse and return
}
}
```
2. Add DEEPSEEK_API_KEY to configuration
3. Register in provider registry with medium priority
4. Support both deepseek-chat and deepseek-coder models
5. Add rate limiting handling
**Test Strategy:**
1. Unit test: Provider correctly checks API key availability
2. Unit test: Request formatting matches DeepSeek API spec
3. Unit test: Response parsing handles all fields
4. Integration test: Full completion with actual API (with valid key)
5. Test error handling for rate limits
6. Test error handling for invalid API key

View File

@@ -0,0 +1,70 @@
# Task ID: 32
**Title:** Implement smart context optimization
**Status:** done
**Dependencies:** 29 ✓
**Priority:** medium
**Description:** Add token counting and decision logic to intelligently skip LLM filtering when responses are small enough, and cache filtering decisions for repeated queries.
**Details:**
1. Create `src/mcplocal/src/llm/token-counter.ts`:
```typescript
export function estimateTokens(text: string): number {
// Simple estimation: ~4 chars per token for English
// More accurate: use tiktoken or similar library
return Math.ceil(text.length / 4);
}
```
2. Create `src/mcplocal/src/llm/filter-cache.ts`:
```typescript
export class FilterCache {
private cache: LRUCache<string, FilterDecision>;
shouldFilter(toolName: string, params: unknown, responseSize: number): boolean {
const key = this.computeKey(toolName, params);
const cached = this.cache.get(key);
if (cached) return cached.shouldFilter;
// No cache hit - use default threshold logic
return responseSize > this.tokenThreshold;
}
recordDecision(toolName: string, params: unknown, decision: FilterDecision): void {
const key = this.computeKey(toolName, params);
this.cache.set(key, decision);
}
}
```
3. Add configuration options:
- `tokenThreshold`: number (default 1000 tokens)
- `filterCacheSize`: number (default 1000 entries)
- `filterCacheTtl`: number (default 3600 seconds)
4. Integrate into LlmProcessor:
```typescript
async filterResponse(...) {
const tokens = estimateTokens(JSON.stringify(rawResponse));
if (tokens < this.config.tokenThreshold) {
// Not worth filtering - return as-is
return { filtered: false, response: rawResponse };
}
// Proceed with LLM filtering
}
```
5. Add metrics tracking:
- Total tokens processed
- Tokens saved by filtering
- Filter cache hit rate
- Average latency added by filtering
**Test Strategy:**
1. Unit test: Token estimation is reasonably accurate
2. Unit test: Cache correctly stores and retrieves decisions
3. Unit test: Threshold logic skips filtering for small responses
4. Unit test: Cache TTL expiration works correctly
5. Integration test: Metrics are recorded accurately
6. Performance test: Cache improves latency for repeated queries

View File

@@ -0,0 +1,171 @@
# Task ID: 33
**Title:** Update mcpctl to use mcplocal as daemon
**Status:** done
**Dependencies:** 27 ✓
**Priority:** high
**Description:** Modify mcpctl CLI to connect to mcplocal instead of mcpd directly, update configuration options, add dual connectivity status checking, and implement authentication commands (login/logout) with secure credential storage.
**Details:**
1. Update `src/cli/src/config/schema.ts`:
```typescript
export interface McpctlConfig {
mcplocalUrl: string; // NEW: default 'http://localhost:3200'
mcpdUrl: string; // Keep for reference/direct access if needed
// ... other fields
}
```
2. Update `src/cli/src/config/defaults.ts`:
- Change default daemonUrl to http://localhost:3200 (mcplocal)
3. Update `src/cli/src/api-client.ts`:
- Default baseUrl now points to mcplocal
4. Add new config commands in `src/cli/src/commands/config.ts`:
```typescript
.command('set-mcplocal-url <url>')
.command('set-mcpd-url <url>')
.command('get-mcplocal-url')
.command('get-mcpd-url')
```
5. Update `src/cli/src/commands/status.ts` to show both connections and auth status:
```
$ mcpctl status
mcplocal: connected (localhost:3200)
mcpd: connected (nas.local:3100) via mcplocal
Auth: logged in as user@example.com
LLM Provider: ollama (llama3.2)
Token savings: 45% (last 24h)
```
6. Update CLI --daemon-url flag to point to mcplocal
7. Add --direct flag to bypass mcplocal and talk to mcpd directly (for debugging)
8. Create `src/cli/src/commands/auth.ts` with login/logout commands:
- `mcpctl login`: Prompt for mcpd URL (if not configured) and credentials
- Call POST /api/v1/auth/login with { email, password }
- Store session token in ~/.mcpctl/credentials with 0600 permissions
- `mcpctl logout`: Invalidate session and delete stored token
9. Create `src/cli/src/auth/credentials.ts` for secure token storage:
- Use fs.chmod to set 0600 permissions on credentials file
- Token format: { token: string, mcpdUrl: string, user: string, expiresAt?: string }
10. Update api-client.ts to include stored token in requests to mcplocal
- mcplocal passes this token to mcpd for authentication
**Test Strategy:**
1. Unit test: Default config points to mcplocal URL
2. Unit test: Config commands update correct fields
3. Integration test: CLI commands work through mcplocal proxy
4. Test status command shows both mcplocal and mcpd status
5. Test --direct flag bypasses mcplocal
6. Test backward compatibility with existing config files
7. Unit test: login command stores token with correct permissions (0600)
8. Unit test: logout command removes credentials file
9. Integration test: login flow with POST /api/v1/auth/login
10. Test status command shows auth status (logged in as user)
11. Test token is passed to mcplocal in API requests
12. Test invalid credentials return appropriate error message
13. Test expired token handling
## Subtasks
### 33.1. Update config schema for mcplocal and mcpd URLs
**Status:** pending
**Dependencies:** None
Modify McpctlConfigSchema in src/cli/src/config/schema.ts to include separate mcplocalUrl and mcpdUrl fields with appropriate defaults.
**Details:**
Update the Zod schema to add mcplocalUrl (default: http://localhost:3200) and mcpdUrl (default: http://localhost:3100). Update DEFAULT_CONFIG and ensure backward compatibility with existing daemonUrl field by mapping it to mcplocalUrl.
### 33.2. Create auth credentials storage module
**Status:** pending
**Dependencies:** None
Create src/cli/src/auth/credentials.ts to handle secure storage and retrieval of session tokens in ~/.mcpctl/credentials.
**Details:**
Implement saveCredentials(token, mcpdUrl, user), loadCredentials(), and deleteCredentials() functions. Use fs.chmod to set 0600 permissions. Store JSON format: { token, mcpdUrl, user, expiresAt }. Handle file not found gracefully in loadCredentials.
### 33.3. Implement login command
**Status:** pending
**Dependencies:** 33.2
Create src/cli/src/commands/auth.ts with mcpctl login command that prompts for mcpd URL and credentials, calls POST /api/v1/auth/login, and stores the session token.
**Details:**
Use inquirer or prompts library for interactive credential input (email, password). If mcpdUrl not configured, prompt for it. Call POST /api/v1/auth/login with credentials. On success, save token using credentials module. Display 'Logged in as {user}' on success. Handle errors (invalid credentials, network errors) with clear messages.
### 33.4. Implement logout command
**Status:** pending
**Dependencies:** 33.2
Add mcpctl logout command to auth.ts that invalidates the session and removes stored credentials.
**Details:**
Load stored credentials, optionally call a logout endpoint on mcpd to invalidate server-side session, then delete the local credentials file. Display 'Logged out successfully' or 'Not logged in' as appropriate.
### 33.5. Update api-client to include auth token
**Status:** pending
**Dependencies:** 33.2
Modify src/cli/src/api-client.ts to load and include stored session token in Authorization header for requests to mcplocal.
**Details:**
Import loadCredentials from auth module. Add Authorization: Bearer {token} header to requests when credentials exist. Handle expired token by returning appropriate error suggesting re-login.
### 33.6. Update status command to show auth status
**Status:** pending
**Dependencies:** 33.2, 33.5
Modify src/cli/src/commands/status.ts to display authentication status (logged in as user X or not logged in) along with mcplocal and mcpd connectivity.
**Details:**
Load credentials and display auth status line: 'Auth: logged in as {user}' or 'Auth: not logged in'. Update status output format to show mcplocal and mcpd status separately with the auth info.
### 33.7. Add config commands for mcplocal and mcpd URLs
**Status:** pending
**Dependencies:** 33.1
Add set-mcplocal-url, set-mcpd-url, get-mcplocal-url, and get-mcpd-url commands to src/cli/src/commands/config.ts.
**Details:**
Add four new subcommands to the config command for setting and getting the mcplocal and mcpd URLs independently. Update the generic 'set' command to handle these new schema fields.
### 33.8. Add --direct flag for mcpd bypass
**Status:** pending
**Dependencies:** 33.1, 33.5
Add --direct flag to CLI commands that bypasses mcplocal and connects directly to mcpd for debugging purposes.
**Details:**
Add global --direct option to the main CLI. When set, api-client uses mcpdUrl instead of mcplocalUrl. Useful for debugging connectivity issues between mcplocal and mcpd.
### 33.9. Register auth commands in CLI entry point
**Status:** pending
**Dependencies:** 33.3, 33.4
Import and register the login and logout commands in src/cli/src/index.ts.
**Details:**
Import createAuthCommand from commands/auth.ts and add it to the main program with program.addCommand(createAuthCommand()).

View File

@@ -0,0 +1,63 @@
# Task ID: 34
**Title:** Connect mcplocal MCP router to mcpd proxy endpoint
**Status:** done
**Dependencies:** 28 ✓, 29 ✓
**Priority:** high
**Description:** Update mcplocal's MCP router to forward tool calls to mcpd's new /api/v1/mcp/proxy endpoint instead of connecting to MCP servers directly.
**Details:**
1. Update `src/mcplocal/src/router.ts` to use mcpd proxy:
```typescript
class Router {
private mcpdClient: McpdClient;
async handleToolsCall(request: JsonRpcRequest) {
const { name, arguments: args } = request.params;
const [serverName, toolName] = name.split('/');
// Pre-process with LLM if enabled
const processedArgs = this.config.enablePreprocessing
? await this.llmProcessor.preprocessRequest(toolName, args)
: args;
// Call mcpd proxy endpoint
const result = await this.mcpdClient.post('/api/v1/mcp/proxy', {
serverId: serverName,
method: 'tools/call',
params: { name: toolName, arguments: processedArgs }
});
// Post-process response with LLM if enabled
return this.config.enablePreprocessing
? await this.llmProcessor.filterResponse(toolName, args, result)
: result;
}
}
```
2. Update upstream configuration:
- Remove direct upstream connections for managed servers
- Keep option for local/unmanaged upstreams
3. Add server discovery from mcpd:
```typescript
async refreshServerList() {
const servers = await this.mcpdClient.get('/api/v1/servers');
this.updateAvailableTools(servers);
}
```
4. Handle tools/list by aggregating from mcpd servers
5. Handle resources/list and prompts/list similarly
**Test Strategy:**
1. Unit test: Tool calls are forwarded to mcpd proxy correctly
2. Unit test: Server name is extracted from namespaced tool name
3. Integration test: Full flow Claude -> mcplocal -> mcpd -> container
4. Test tools/list aggregates from all mcpd servers
5. Test error handling when mcpd is unreachable
6. Test LLM preprocessing is applied when enabled

View File

@@ -0,0 +1,64 @@
# Task ID: 35
**Title:** Implement health monitoring across all tiers
**Status:** done
**Dependencies:** 33 ✓, 34 ✓
**Priority:** medium
**Description:** Extend health monitoring to track connectivity and status across mcplocal, mcpd, and individual MCP server instances.
**Details:**
1. Update mcplocal health monitor in `src/mcplocal/src/health.ts`:
```typescript
export class TieredHealthMonitor {
async checkHealth(): Promise<TieredHealthStatus> {
return {
mcplocal: {
status: 'healthy',
llmProvider: await this.checkLlmProvider(),
uptime: process.uptime()
},
mcpd: await this.checkMcpdHealth(),
instances: await this.checkInstancesHealth()
};
}
private async checkMcpdHealth(): Promise<McpdHealth> {
try {
const health = await this.mcpdClient.get('/api/v1/health');
return { status: 'connected', ...health };
} catch {
return { status: 'disconnected' };
}
}
private async checkInstancesHealth(): Promise<InstanceHealth[]> {
const instances = await this.mcpdClient.get('/api/v1/instances');
return instances.map(i => ({
name: i.name,
status: i.status,
lastHealthCheck: i.lastHealthCheck
}));
}
}
```
2. Add health endpoint to mcplocal HTTP server: `GET /health`
3. Update mcpctl status command to display tiered health
4. Add degraded state detection:
- LLM provider unavailable but mcpd reachable
- Some instances down but others healthy
5. Add health event notifications for state transitions
6. Add configurable health check intervals
**Test Strategy:**
1. Unit test: Health check correctly identifies all states
2. Unit test: Degraded state is detected correctly
3. Integration test: Full health check across all tiers
4. Test health endpoint returns correct format
5. Test mcpctl status displays health correctly
6. Test state transition events are emitted

View File

@@ -0,0 +1,58 @@
# Task ID: 36
**Title:** End-to-end integration testing
**Status:** done
**Dependencies:** 29 ✓, 33 ✓, 34 ✓, 35 ✓
**Priority:** high
**Description:** Create comprehensive integration tests that validate the full data flow from mcpctl through mcplocal to mcpd to MCP server containers and back.
**Details:**
1. Create test fixtures in `src/mcplocal/test/fixtures/`:
- Mock MCP server that returns predictable responses
- Test configuration files
- Sample tool call payloads
2. Create integration test suite in `src/mcplocal/test/integration/`:
```typescript
describe('End-to-end flow', () => {
it('mcpctl -> mcplocal -> mcpd -> mcp_server', async () => {
// Start mock MCP server
// Start mcpd with test config
// Start mcplocal pointing to mcpd
// Execute mcpctl command
// Verify response flows back correctly
});
it('LLM pre-processing reduces response size', async () => {
// Send query that returns large dataset
// Verify LLM filtering reduces token count
// Verify relevant data is preserved
});
it('credentials never leave mcpd', async () => {
// Monitor all traffic from mcplocal
// Verify no credentials appear in requests/responses
});
});
```
3. Test scenarios:
- Management commands (get servers, instances, etc.)
- MCP tool calls with LLM preprocessing
- MCP tool calls without preprocessing
- Error handling (mcpd down, instance down, LLM failure)
- Health monitoring accuracy
4. Add CI integration test workflow
5. Create docker-compose.test.yml for test environment
**Test Strategy:**
1. All integration tests pass in CI environment
2. Test coverage includes happy path and error scenarios
3. Performance benchmarks: measure latency at each tier
4. Security test: verify credential isolation
5. Load test: multiple concurrent requests
6. Chaos test: random component failures

View File

@@ -0,0 +1,36 @@
# Task ID: 37
**Title:** Add priority, summary, chapters, and linkTarget fields to Prompt schema
**Status:** done
**Dependencies:** None
**Priority:** high
**Description:** Extend the Prisma schema for the Prompt model to include priority (integer 1-10, default 5), summary (nullable string), chapters (nullable JSON array), and linkTarget (nullable string for prompt links).
**Details:**
1. Update `/src/db/prisma/schema.prisma` to add fields to the Prompt model:
- `priority Int @default(5)` with check constraint 1-10
- `summary String? @db.Text`
- `chapters Json?` (stored as JSON array of strings)
- `linkTarget String?` (format: `project/server:resource-uri`)
2. Create Prisma migration:
```bash
pnpm --filter db exec prisma migrate dev --name add-prompt-priority-summary-chapters-link
```
3. Update TypeScript types in shared package to reflect new fields
4. Add validation for priority range (1-10) at the database level if possible, otherwise enforce in application layer
**Test Strategy:**
- Unit test: Verify migration creates columns with correct types and defaults
- Unit test: Verify priority default is 5
- Unit test: Verify nullable fields accept null
- Unit test: Verify chapters stores/retrieves JSON arrays correctly
- Integration test: Create prompt with all new fields, retrieve and verify values

View File

@@ -0,0 +1,35 @@
# Task ID: 38
**Title:** Add priority field to PromptRequest schema
**Status:** done
**Dependencies:** 37 ✓
**Priority:** high
**Description:** Extend the Prisma schema for the PromptRequest model to include the priority field (integer 1-10, default 5) to match the Prompt model.
**Details:**
1. Update `/src/db/prisma/schema.prisma` to add to PromptRequest:
- `priority Int @default(5)`
2. Create Prisma migration:
```bash
pnpm --filter db exec prisma migrate dev --name add-promptrequest-priority
```
3. Update the `CreatePromptRequestSchema` in `/src/mcpd/src/validation/prompt.schema.ts` to include priority validation:
```typescript
priority: z.number().int().min(1).max(10).default(5).optional(),
```
4. Update TypeScript types in shared package
**Test Strategy:**
- Unit test: Migration creates priority column with default 5
- Unit test: PromptRequest creation with explicit priority
- Unit test: PromptRequest creation uses default priority when not specified
- Unit test: Validation rejects priority outside 1-10 range

View File

@@ -0,0 +1,38 @@
# Task ID: 39
**Title:** Add gated field to Project schema
**Status:** done
**Dependencies:** None
**Priority:** high
**Description:** Extend the Prisma schema for the Project model to include the gated boolean field (default true) that controls whether sessions go through the keyword-driven prompt retrieval flow.
**Details:**
1. Update `/src/db/prisma/schema.prisma` to add to Project:
- `gated Boolean @default(true)`
2. Create Prisma migration:
```bash
pnpm --filter db exec prisma migrate dev --name add-project-gated
```
3. Update project-related TypeScript types
4. Update project validation schemas to include gated field:
```typescript
gated: z.boolean().default(true).optional(),
```
5. Update project API routes to accept and return the gated field
**Test Strategy:**
- Unit test: Migration creates gated column with default true
- Unit test: Project creation with gated=false
- Unit test: Project creation uses default gated=true when not specified
- Unit test: Project update can toggle gated field
- Integration test: GET /api/v1/projects/:name returns gated field

View File

@@ -0,0 +1,49 @@
# Task ID: 40
**Title:** Update Prompt CRUD API to handle priority and linkTarget
**Status:** done
**Dependencies:** 37 ✓
**Priority:** high
**Description:** Modify prompt API endpoints to accept, validate, and return the priority and linkTarget fields. LinkTarget should be immutable after creation.
**Details:**
1. Update `/src/mcpd/src/validation/prompt.schema.ts`:
```typescript
export const CreatePromptSchema = z.object({
name: z.string().min(1).max(100).regex(/^[a-z0-9-]+$/),
content: z.string().min(1).max(50000),
projectId: z.string().optional(),
priority: z.number().int().min(1).max(10).default(5).optional(),
linkTarget: z.string().regex(/^[a-z0-9-]+\/[a-z0-9-]+:[\S]+$/).optional(),
});
export const UpdatePromptSchema = z.object({
content: z.string().min(1).max(50000).optional(),
priority: z.number().int().min(1).max(10).optional(),
// Note: linkTarget is NOT included - links are immutable
});
```
2. Update `/src/mcpd/src/routes/prompts.ts`:
- POST /api/v1/prompts: Accept priority, linkTarget
- PUT /api/v1/prompts/:id: Accept priority only (not linkTarget)
- GET endpoints: Return priority, linkTarget in response
3. Update repository layer to handle new fields
4. Add linkTarget format validation: `project/server:resource-uri`
**Test Strategy:**
- Unit test: POST /api/v1/prompts with priority creates prompt with correct priority
- Unit test: POST /api/v1/prompts with linkTarget creates linked prompt
- Unit test: PUT /api/v1/prompts/:id with priority updates priority
- Unit test: PUT /api/v1/prompts/:id rejects linkTarget (immutable)
- Unit test: GET /api/v1/prompts returns priority and linkTarget fields
- Unit test: Invalid linkTarget format rejected (validation error)
- Unit test: Priority outside 1-10 range rejected

View File

@@ -0,0 +1,42 @@
# Task ID: 41
**Title:** Update PromptRequest API to handle priority
**Status:** done
**Dependencies:** 38 ✓
**Priority:** high
**Description:** Modify prompt request API endpoints to accept, validate, and return the priority field for proposed prompts.
**Details:**
1. Update validation in `/src/mcpd/src/validation/prompt.schema.ts`:
```typescript
export const CreatePromptRequestSchema = z.object({
name: z.string().min(1).max(100).regex(/^[a-z0-9-]+$/),
content: z.string().min(1).max(50000),
projectId: z.string().optional(),
createdBySession: z.string().optional(),
createdByUserId: z.string().optional(),
priority: z.number().int().min(1).max(10).default(5).optional(),
});
```
2. Update `/src/mcpd/src/routes/prompts.ts` for PromptRequest endpoints:
- POST /api/v1/promptrequests: Accept priority
- GET /api/v1/promptrequests: Return priority
- POST /api/v1/promptrequests/:id/approve: Preserve priority when creating Prompt
3. Update PromptService.approve() to copy priority from request to prompt
4. Update repository layer
**Test Strategy:**
- Unit test: POST /api/v1/promptrequests with priority creates request with correct priority
- Unit test: POST /api/v1/promptrequests uses default priority 5 when not specified
- Unit test: GET /api/v1/promptrequests returns priority field
- Unit test: Approve preserves priority from request to created prompt
- Unit test: Priority validation (1-10 range)

View File

@@ -0,0 +1,58 @@
# Task ID: 42
**Title:** Implement prompt summary generation service
**Status:** done
**Dependencies:** 37 ✓
**Priority:** high
**Description:** Create a service that auto-generates summary (20 words) and chapters (key sections) for prompts, using fast LLM when available or regex fallback.
**Details:**
1. Create `/src/mcpd/src/services/prompt-summary.service.ts`:
```typescript
export class PromptSummaryService {
constructor(
private llmClient: LlmClient | null,
private promptRepo: IPromptRepository
) {}
async generateSummary(content: string): Promise<{ summary: string; chapters: string[] }> {
if (this.llmClient) {
return this.generateWithLlm(content);
}
return this.generateWithRegex(content);
}
private async generateWithLlm(content: string): Promise<...> {
// Send content to fast LLM with prompt:
// "Generate a 20-word summary and extract key section topics..."
}
private generateWithRegex(content: string): { summary: string; chapters: string[] } {
// summary: first sentence of content (truncated to ~20 words)
// chapters: extract markdown headings via regex /^#+\s+(.+)$/gm
}
}
```
2. Integrate with PromptService:
- Call generateSummary on prompt create
- Call generateSummary on prompt update (when content changes)
- Cache results on the prompt record
3. Handle LLM availability check via existing LlmConfig patterns
**Test Strategy:**
- Unit test: generateWithRegex extracts first sentence as summary
- Unit test: generateWithRegex extracts markdown headings as chapters
- Unit test: generateWithLlm calls LLM with correct prompt (mock LLM)
- Unit test: generateSummary uses LLM when available
- Unit test: generateSummary falls back to regex when no LLM
- Unit test: Empty content handled gracefully
- Unit test: Content without headings returns empty chapters array
- Integration test: Creating prompt triggers summary generation

View File

@@ -0,0 +1,41 @@
# Task ID: 43
**Title:** Add regenerate-summary API endpoint
**Status:** done
**Dependencies:** 42 ✓
**Priority:** medium
**Description:** Create POST /api/v1/prompts/:id/regenerate-summary endpoint to force re-generation of summary and chapters for a prompt.
**Details:**
1. Add route in `/src/mcpd/src/routes/prompts.ts`:
```typescript
fastify.post('/api/v1/prompts/:id/regenerate-summary', async (request, reply) => {
const { id } = request.params as { id: string };
const prompt = await promptService.findById(id);
if (!prompt) {
return reply.status(404).send({ error: 'Prompt not found' });
}
const { summary, chapters } = await summaryService.generateSummary(prompt.content);
const updated = await promptService.updateSummary(id, summary, chapters);
return reply.send(updated);
});
```
2. Add `updateSummary(id, summary, chapters)` method to PromptRepository and PromptService
3. Return the updated prompt with new summary/chapters in response
**Test Strategy:**
- Unit test: POST to valid prompt ID regenerates summary
- Unit test: Returns updated prompt with new summary/chapters
- Unit test: 404 for non-existent prompt ID
- Unit test: Uses LLM when available, regex fallback otherwise
- Integration test: End-to-end regeneration updates database

View File

@@ -0,0 +1,48 @@
# Task ID: 44
**Title:** Create prompt-index API endpoint
**Status:** done
**Dependencies:** 42 ✓
**Priority:** medium
**Description:** Create GET /api/v1/projects/:name/prompt-index endpoint that returns a compact index of prompts (name, priority, summary, chapters) for a project.
**Details:**
1. Add route in `/src/mcpd/src/routes/prompts.ts`:
```typescript
fastify.get('/api/v1/projects/:name/prompt-index', async (request, reply) => {
const { name } = request.params as { name: string };
const project = await projectService.findByName(name);
if (!project) {
return reply.status(404).send({ error: 'Project not found' });
}
const prompts = await promptService.findByProject(project.id);
const index = prompts.map(p => ({
name: p.name,
priority: p.priority,
summary: p.summary,
chapters: p.chapters,
linkTarget: p.linkTarget,
}));
return reply.send({ prompts: index });
});
```
2. Consider adding global prompts to the index (inherited by all projects)
3. Sort by priority descending in response
**Test Strategy:**
- Unit test: Returns compact index for valid project
- Unit test: Index contains name, priority, summary, chapters, linkTarget
- Unit test: 404 for non-existent project
- Unit test: Empty array for project with no prompts
- Unit test: Results sorted by priority descending
- Integration test: End-to-end retrieval matches database state

View File

@@ -0,0 +1,73 @@
# Task ID: 45
**Title:** Implement tag-matching algorithm for prompt selection
**Status:** done
**Dependencies:** 44 ✓
**Priority:** high
**Description:** Create a deterministic keyword-based tag matching algorithm as the no-LLM fallback for prompt selection, with byte-budget allocation and priority weighting.
**Details:**
1. Create `/src/mcplocal/src/services/tag-matcher.service.ts`:
```typescript
interface MatchedPrompt {
prompt: PromptIndex;
score: number;
matchedTags: string[];
}
export class TagMatcherService {
constructor(private byteBudget: number = 8192) {}
matchPrompts(tags: string[], promptIndex: PromptIndex[]): {
fullContent: PromptIndex[]; // Prompts to include in full
indexOnly: PromptIndex[]; // Prompts to include as index entries
remaining: PromptIndex[]; // Non-matched prompts (names only)
} {
// 1. Priority 10 prompts: always included (score = Infinity)
// 2. For each prompt, compute score:
// - Check tags against summary + chapters (case-insensitive substring)
// - score = matching_tags_count * priority
// 3. Sort by score descending
// 4. Fill byte budget from top:
// - Include full content until budget exhausted
// - Remaining matched: include as index entries
// - Non-matched: names only
}
private computeScore(tags: string[], prompt: PromptIndex): number {
if (prompt.priority === 10) return Infinity;
const matchingTags = tags.filter(tag =>
this.matchesPrompt(tag.toLowerCase(), prompt)
);
return matchingTags.length * prompt.priority;
}
private matchesPrompt(tag: string, prompt: PromptIndex): boolean {
const searchText = [
prompt.summary || '',
...(prompt.chapters || [])
].join(' ').toLowerCase();
return searchText.includes(tag);
}
}
```
2. Handle edge cases: empty tags, no prompts, all priority 10, etc.
**Test Strategy:**
- Unit test: Priority 10 prompts always included regardless of tags
- Unit test: Score calculation: matching_tags * priority
- Unit test: Case-insensitive matching
- Unit test: Substring matching in summary and chapters
- Unit test: Byte budget exhaustion stops full content inclusion
- Unit test: Matched prompts beyond budget become index entries
- Unit test: Non-matched prompts listed as names only
- Unit test: Sorting by score descending
- Unit test: Empty tags returns priority 10 only
- Unit test: No prompts returns empty result

View File

@@ -0,0 +1,59 @@
# Task ID: 46
**Title:** Implement LLM-based prompt selection service
**Status:** done
**Dependencies:** 45 ✓
**Priority:** high
**Description:** Create a service that uses the heavy LLM to intelligently select relevant prompts based on tags and the full prompt index, understanding synonyms and context.
**Details:**
1. Create `/src/mcplocal/src/services/llm-prompt-selector.service.ts`:
```typescript
export class LlmPromptSelectorService {
constructor(
private llmClient: LlmClient,
private fastLlmClient: LlmClient | null,
private tagMatcher: TagMatcherService // fallback
) {}
async selectPrompts(tags: string[], promptIndex: PromptIndex[]): Promise<{
selected: Array<{ name: string; reason: string }>;
priority10: PromptIndex[]; // Always included
}> {
// 1. Extract priority 10 prompts (always included)
// 2. Generate missing summaries using fast LLM if needed
// 3. Send to heavy LLM:
const prompt = `
Given these keywords: ${tags.join(', ')}
And this prompt index:
${promptIndex.map(p => `- ${p.name}: ${p.summary}`).join('\n')}
Select the most relevant prompts for someone working on tasks
related to these keywords. Consider synonyms and related concepts.
Return a ranked JSON array: [{name: string, reason: string}]
`;
// 4. Parse LLM response
// 5. On LLM error, fall back to tag matcher
}
}
```
2. Handle LLM timeouts and errors gracefully with fallback
3. Validate LLM response format
**Test Strategy:**
- Unit test: Priority 10 prompts always returned regardless of LLM selection
- Unit test: LLM called with correct prompt format (mock)
- Unit test: LLM response parsed correctly
- Unit test: Invalid LLM response falls back to tag matcher
- Unit test: LLM timeout falls back to tag matcher
- Unit test: Missing summaries trigger fast LLM generation
- Unit test: No LLM available uses tag matcher directly
- Integration test: End-to-end selection with mock LLM

View File

@@ -0,0 +1,61 @@
# Task ID: 47
**Title:** Implement session state management for gating
**Status:** done
**Dependencies:** 39 ✓
**Priority:** high
**Description:** Extend the McpRouter to track per-session gating state including gated status, accumulated tags, and retrieved prompts set.
**Details:**
1. Update `/src/mcplocal/src/router.ts` to add session state:
```typescript
interface SessionState {
gated: boolean; // starts true if project is gated
tags: string[]; // accumulated from begin_session + read_prompts
retrievedPrompts: Set<string>; // prompts already sent (avoid duplicates)
}
export class McpRouter {
private sessionStates: Map<string, SessionState> = new Map();
getSessionState(sessionId: string): SessionState {
if (!this.sessionStates.has(sessionId)) {
this.sessionStates.set(sessionId, {
gated: this.projectConfig?.gated ?? true,
tags: [],
retrievedPrompts: new Set(),
});
}
return this.sessionStates.get(sessionId)!;
}
ungateSession(sessionId: string): void {
const state = this.getSessionState(sessionId);
state.gated = false;
}
addRetrievedPrompts(sessionId: string, names: string[]): void {
const state = this.getSessionState(sessionId);
names.forEach(n => state.retrievedPrompts.add(n));
}
}
```
2. Clean up session state when session closes
3. Handle session state for non-gated projects (gated=false from start)
**Test Strategy:**
- Unit test: New session starts with gated=true for gated project
- Unit test: New session starts with gated=false for non-gated project
- Unit test: ungateSession changes gated to false
- Unit test: addRetrievedPrompts adds to set
- Unit test: retrievedPrompts prevents duplicates
- Unit test: Session state isolated per sessionId
- Unit test: Session cleanup removes state

View File

@@ -0,0 +1,67 @@
# Task ID: 48
**Title:** Implement begin_session tool for gated sessions
**Status:** done
**Dependencies:** 46 ✓, 47 ✓
**Priority:** high
**Description:** Create the begin_session MCP tool that accepts 5 keywords, triggers prompt selection, returns matched content with encouragement, and ungates the session.
**Details:**
1. Add begin_session tool definition in `/src/mcplocal/src/router.ts`:
```typescript
private getBeginSessionTool(): Tool {
return {
name: 'begin_session',
description: 'Start your session by providing 5 keywords that describe your current task. You\'ll receive relevant project context, policies, and guidelines. Required before using other tools.',
inputSchema: {
type: 'object',
properties: {
tags: {
type: 'array',
items: { type: 'string' },
maxItems: 10,
description: '5 keywords describing your current task'
}
},
required: ['tags']
}
};
}
```
2. Implement begin_session handler:
- Validate tags array (1-10 items)
- Call LlmPromptSelector or TagMatcher based on LLM availability
- Fetch full content for selected prompts
- Build response with matched content + index + encouragement
- Ungate session
- Send `notifications/tools/list_changed`
3. Response format:
```
[Priority 10 prompts - full content]
[Tag-matched prompts - full content, priority-ordered]
Other prompts available that may become relevant...
- name: summary
...
If any seem related, request them with read_prompts({ tags: [...] }).
```
**Test Strategy:**
- Unit test: begin_session with valid tags returns matched prompts
- Unit test: begin_session includes priority 10 prompts always
- Unit test: begin_session response includes encouragement text
- Unit test: begin_session response includes prompt index
- Unit test: Session ungated after successful begin_session
- Unit test: notifications/tools/list_changed sent after ungating
- Unit test: Empty tags handled (returns priority 10 only)
- Unit test: Invalid tags rejected with error
- Unit test: begin_session while already ungated returns error

View File

@@ -0,0 +1,58 @@
# Task ID: 49
**Title:** Implement read_prompts tool for ongoing retrieval
**Status:** done
**Dependencies:** 48 ✓
**Priority:** high
**Description:** Create the read_prompts MCP tool that allows clients to request additional context by keywords after the session is ungated.
**Details:**
1. Add read_prompts tool definition:
```typescript
private getReadPromptsTool(): Tool {
return {
name: 'read_prompts',
description: 'Request additional project context by keywords. Use this whenever you need guidelines, policies, or conventions related to your current work.',
inputSchema: {
type: 'object',
properties: {
tags: {
type: 'array',
items: { type: 'string' },
description: 'Keywords describing what context you need'
}
},
required: ['tags']
}
};
}
```
2. Implement read_prompts handler:
- Always use keyword matching (not LLM) for precision
- Exclude already-retrieved prompts from response
- Add newly retrieved prompts to session state
- Include reminder about more prompts available
3. Response format:
```
[Matched prompt content - deduplicated]
Remember: you can request more prompts at any time with read_prompts({ tags: [...] }).
The project may have additional guidelines relevant to your current approach.
```
**Test Strategy:**
- Unit test: read_prompts returns matched prompts by keyword
- Unit test: Already retrieved prompts excluded from response
- Unit test: Newly retrieved prompts added to session state
- Unit test: Response includes reminder text
- Unit test: read_prompts while gated returns error
- Unit test: Empty tags returns empty response
- Unit test: Uses keyword matching not LLM

View File

@@ -0,0 +1,55 @@
# Task ID: 50
**Title:** Implement progressive tool exposure for gated sessions
**Status:** done
**Dependencies:** 48 ✓, 49 ✓
**Priority:** high
**Description:** Modify tools/list behavior to only expose begin_session while gated, and expose all tools plus read_prompts after ungating.
**Details:**
1. Update tools/list handling in `/src/mcplocal/src/router.ts`:
```typescript
async handleToolsList(sessionId: string): Promise<Tool[]> {
const state = this.getSessionState(sessionId);
if (state.gated) {
// Only show begin_session while gated
return [this.getBeginSessionTool()];
}
// After ungating: all upstream tools + read_prompts
const upstreamTools = await this.discoverTools();
return [...upstreamTools, this.getReadPromptsTool()];
}
```
2. Block direct tool calls while gated:
```typescript
async handleToolCall(sessionId: string, toolName: string, args: any): Promise<any> {
const state = this.getSessionState(sessionId);
if (state.gated && toolName !== 'begin_session') {
// Intercept: extract keywords, match prompts, inject briefing
return this.handleInterceptedCall(sessionId, toolName, args);
}
// Normal routing
return this.routeToolCall(toolName, args);
}
```
3. Ensure notifications/tools/list_changed is sent after ungating
**Test Strategy:**
- Unit test: tools/list while gated returns only begin_session
- Unit test: tools/list after ungating returns all tools + read_prompts
- Unit test: begin_session not visible after ungating
- Unit test: Tool call while gated (not begin_session) triggers intercept
- Unit test: Tool call after ungating routes normally
- Unit test: notifications/tools/list_changed sent on ungate

View File

@@ -0,0 +1,54 @@
# Task ID: 51
**Title:** Implement keyword extraction from tool calls
**Status:** done
**Dependencies:** None
**Priority:** medium
**Description:** Create a service that extracts keywords from tool names and arguments for the intercept fallback path when clients skip begin_session.
**Details:**
1. Create `/src/mcplocal/src/services/keyword-extractor.service.ts`:
```typescript
export class KeywordExtractorService {
extractKeywords(toolName: string, args: Record<string, any>): string[] {
const keywords: string[] = [];
// Extract from tool name (split on / and -)
// e.g., "home-assistant/get_entities" -> ["home", "assistant", "get", "entities"]
keywords.push(...this.extractFromName(toolName));
// Extract from argument values
// e.g., { domain: "light", entity_id: "light.kitchen" } -> ["light", "kitchen"]
keywords.push(...this.extractFromArgs(args));
// Deduplicate and sanitize
return [...new Set(keywords.map(k => this.sanitize(k)))];
}
private sanitize(keyword: string): string {
// Remove special characters, lowercase, limit length
return keyword.toLowerCase().replace(/[^a-z0-9]/g, '').slice(0, 50);
}
}
```
2. Handle various argument types: strings, arrays, nested objects
3. Prevent injection by sanitizing extracted keywords
**Test Strategy:**
- Unit test: Extracts keywords from tool name with /
- Unit test: Extracts keywords from tool name with -
- Unit test: Extracts keywords from string argument values
- Unit test: Extracts keywords from array argument values
- Unit test: Handles nested object arguments
- Unit test: Sanitizes special characters
- Unit test: Deduplicates keywords
- Unit test: Handles empty arguments
- Unit test: Limits keyword length to prevent abuse

View File

@@ -0,0 +1,63 @@
# Task ID: 52
**Title:** Implement tool call intercept with briefing injection
**Status:** done
**Dependencies:** 50 ✓, 51 ✓
**Priority:** high
**Description:** When a gated session calls a tool without first calling begin_session, intercept the call, extract keywords, match prompts, and inject the briefing alongside the real tool result.
**Details:**
1. Implement handleInterceptedCall in `/src/mcplocal/src/router.ts`:
```typescript
async handleInterceptedCall(
sessionId: string,
toolName: string,
args: any
): Promise<ToolResult> {
// 1. Extract keywords from tool call
const keywords = this.keywordExtractor.extractKeywords(toolName, args);
// 2. Match prompts using keywords
const { fullContent, indexOnly, remaining } =
await this.promptSelector.selectPrompts(keywords, this.promptIndex);
// 3. Execute the actual tool call
const actualResult = await this.routeToolCall(toolName, args);
// 4. Build briefing with intercept preamble
const briefing = this.buildBriefing(fullContent, indexOnly, remaining, 'intercept');
// 5. Ungate session
this.ungateSession(sessionId);
// 6. Send notifications/tools/list_changed
await this.sendToolsListChanged();
// 7. Return combined result
return {
content: [{
type: 'text',
text: `${briefing}\n\n---\n\n${actualResult.content[0].text}`
}]
};
}
```
2. Use gate-intercept-preamble system prompt for the briefing prefix
**Test Strategy:**
- Unit test: Tool call while gated triggers intercept
- Unit test: Keywords extracted from tool name and args
- Unit test: Prompts matched using extracted keywords
- Unit test: Actual tool still executes and returns result
- Unit test: Briefing prepended to tool result
- Unit test: Session ungated after intercept
- Unit test: notifications/tools/list_changed sent
- Unit test: Intercept preamble included in briefing
- Integration test: End-to-end intercept flow

View File

@@ -0,0 +1,54 @@
# Task ID: 53
**Title:** Add prompt index to initialize instructions
**Status:** done
**Dependencies:** 47 ✓, 44 ✓
**Priority:** high
**Description:** Modify the initialize handler to include the compact prompt index and gate message in instructions for gated projects.
**Details:**
1. Update initialize handling in `/src/mcplocal/src/router.ts`:
```typescript
async handleInitialize(sessionId: string): Promise<InitializeResult> {
const state = this.getSessionState(sessionId);
let instructions = this.projectConfig.prompt || '';
if (state.gated) {
// Add gate instructions
const gateInstructions = await this.getSystemPrompt('gate-instructions');
// Build prompt index (cap at 50, priority 7+ if over)
const index = this.buildPromptIndex();
instructions += `\n\n${gateInstructions.replace('{{prompt_index}}', index)}`;
}
return {
protocolVersion: '2024-11-05',
capabilities: { ... },
serverInfo: { ... },
instructions,
};
}
```
2. Build prompt index with cap:
- If <= 50 prompts: include all
- If > 50 prompts: include only priority 7+
- Format: `- <name>: <summary>` (~100 chars per entry)
**Test Strategy:**
- Unit test: Gated project includes gate instructions in initialize
- Unit test: Prompt index included in instructions
- Unit test: Index capped at 50 entries
- Unit test: Over 50 prompts shows priority 7+ only
- Unit test: Non-gated project skips gate instructions
- Unit test: {{prompt_index}} template replaced
- Integration test: End-to-end initialize with gated project

View File

@@ -0,0 +1,58 @@
# Task ID: 54
**Title:** Create mcpctl-system project with system prompts
**Status:** done
**Dependencies:** 40 ✓, 39 ✓
**Priority:** high
**Description:** Implement bootstrap logic to create the mcpctl-system project and its required system prompts on first startup, with protection against deletion.
**Details:**
1. Create seed migration or startup hook:
```typescript
async function bootstrapSystemProject() {
const systemProject = await projectRepo.findByName('mcpctl-system');
if (systemProject) return; // Already exists
// Create mcpctl-system project
const project = await projectRepo.create({
name: 'mcpctl-system',
description: 'System prompts for mcpctl gating and encouragement',
gated: false, // System project is not gated
ownerId: SYSTEM_USER_ID,
});
// Create required system prompts
const systemPrompts = [
{ name: 'gate-instructions', priority: 10, content: GATE_INSTRUCTIONS },
{ name: 'gate-encouragement', priority: 10, content: GATE_ENCOURAGEMENT },
{ name: 'read-prompts-reminder', priority: 10, content: READ_PROMPTS_REMINDER },
{ name: 'gate-intercept-preamble', priority: 10, content: GATE_INTERCEPT_PREAMBLE },
{ name: 'session-greeting', priority: 10, content: SESSION_GREETING },
];
for (const p of systemPrompts) {
await promptRepo.create({ ...p, projectId: project.id });
}
}
```
2. Add delete protection in prompt delete endpoint:
- Check if prompt belongs to mcpctl-system
- Return 403 error if attempting to delete system prompt
3. Define default content for each system prompt per PRD
**Test Strategy:**
- Unit test: System project created on first startup
- Unit test: All 5 system prompts created
- Unit test: Subsequent startups don't duplicate
- Unit test: Delete system prompt returns 403
- Unit test: System prompts have priority 10
- Unit test: mcpctl-system project has gated=false
- Integration test: End-to-end bootstrap flow

View File

@@ -0,0 +1,61 @@
# Task ID: 55
**Title:** Implement system prompt fetching and caching in mcplocal
**Status:** done
**Dependencies:** 54 ✓
**Priority:** high
**Description:** Add functionality to mcplocal router to fetch system prompts from mcpd and cache them with 60s TTL, supporting template variable replacement.
**Details:**
1. Add system prompt fetching in `/src/mcplocal/src/router.ts`:
```typescript
private systemPromptCache: Map<string, { content: string; expiresAt: number }> = new Map();
async getSystemPrompt(name: string): Promise<string> {
const cached = this.systemPromptCache.get(name);
if (cached && cached.expiresAt > Date.now()) {
return cached.content;
}
const prompts = await this.mcpdClient.fetch(
'/api/v1/projects/mcpctl-system/prompts/visible'
);
const prompt = prompts.find(p => p.name === name);
if (!prompt) {
throw new Error(`System prompt not found: ${name}`);
}
this.systemPromptCache.set(name, {
content: prompt.content,
expiresAt: Date.now() + 60000, // 60s TTL
});
return prompt.content;
}
```
2. Add template variable replacement:
```typescript
replaceTemplateVariables(content: string, vars: Record<string, string>): string {
return content
.replace(/\{\{prompt_index\}\}/g, vars.prompt_index || '')
.replace(/\{\{project_name\}\}/g, vars.project_name || '')
.replace(/\{\{matched_prompts\}\}/g, vars.matched_prompts || '')
.replace(/\{\{remaining_prompts\}\}/g, vars.remaining_prompts || '');
}
```
**Test Strategy:**
- Unit test: System prompt fetched from mcpd
- Unit test: Cached prompt returned within TTL
- Unit test: Cache miss triggers fresh fetch
- Unit test: Missing system prompt throws error
- Unit test: Template variables replaced correctly
- Unit test: Unknown template variables left as-is
- Integration test: End-to-end fetch and cache

View File

@@ -0,0 +1,67 @@
# Task ID: 56
**Title:** Implement prompt link resolution service
**Status:** done
**Dependencies:** 40 ✓
**Priority:** medium
**Description:** Create a service that fetches linked prompt content from source MCP servers using the project's service account, with dead link detection.
**Details:**
1. Create `/src/mcplocal/src/services/link-resolver.service.ts`:
```typescript
export class LinkResolverService {
constructor(private mcpdClient: McpdClient) {}
async resolveLink(linkTarget: string): Promise<{
content: string | null;
status: 'alive' | 'dead' | 'unknown';
error?: string;
}> {
// Parse linkTarget: project/server:resource-uri
const { project, server, uri } = this.parseLink(linkTarget);
try {
// Use service account for source project
const content = await this.fetchResource(project, server, uri);
return { content, status: 'alive' };
} catch (error) {
this.logDeadLink(linkTarget, error);
return {
content: null,
status: 'dead',
error: error.message
};
}
}
private parseLink(linkTarget: string): { project: string; server: string; uri: string } {
const match = linkTarget.match(/^([^/]+)\/([^:]+):(.+)$/);
if (!match) throw new Error('Invalid link format');
return { project: match[1], server: match[2], uri: match[3] };
}
private async fetchResource(project: string, server: string, uri: string): Promise<string> {
// Call mcpd to fetch resource via service account
// mcpd routes to the source project's MCP server
}
}
```
2. Log dead links as errors
3. Cache resolution results
**Test Strategy:**
- Unit test: Valid link parsed correctly
- Unit test: Invalid link format throws error
- Unit test: Successful resolution returns content and status='alive'
- Unit test: Failed resolution returns status='dead' with error
- Unit test: Dead link logged as error
- Unit test: Service account header included in request
- Integration test: End-to-end link resolution

View File

@@ -0,0 +1,50 @@
# Task ID: 57
**Title:** Add linkStatus to prompt GET responses
**Status:** done
**Dependencies:** 56 ✓
**Priority:** medium
**Description:** Modify the GET /api/v1/prompts endpoint to include linkStatus (alive/dead/unknown) for linked prompts by checking link health.
**Details:**
1. Update `/src/mcpd/src/routes/prompts.ts` GET endpoint:
```typescript
fastify.get('/api/v1/prompts', async (request, reply) => {
const prompts = await promptService.findAll(filter);
// Check link status for linked prompts
const promptsWithStatus = await Promise.all(
prompts.map(async (p) => {
if (!p.linkTarget) {
return { ...p, linkStatus: null };
}
const status = await linkResolver.checkLinkHealth(p.linkTarget);
return { ...p, linkStatus: status };
})
);
return reply.send(promptsWithStatus);
});
```
2. Consider caching link health to avoid repeated checks
3. Add `linkStatus` field to prompt response schema:
- `null` for non-linked prompts
- `'alive'` for working links
- `'dead'` for broken links
- `'unknown'` for unchecked links
**Test Strategy:**
- Unit test: Non-linked prompt has linkStatus=null
- Unit test: Linked prompt with working link has linkStatus='alive'
- Unit test: Linked prompt with broken link has linkStatus='dead'
- Unit test: Link health cached to avoid repeated checks
- Unit test: All prompts in response have linkStatus field
- Integration test: End-to-end GET with linked prompts

View File

@@ -0,0 +1,55 @@
# Task ID: 58
**Title:** Add RBAC for prompt link creation
**Status:** done
**Dependencies:** 56 ✓
**Priority:** medium
**Description:** Implement RBAC checks requiring edit permission on the target project to create prompt links, and auto-create service account permission on the source project.
**Details:**
1. Update prompt creation in `/src/mcpd/src/services/prompt.service.ts`:
```typescript
async createPrompt(data: CreatePromptInput, userId: string): Promise<Prompt> {
if (data.linkTarget) {
// Verify user has edit permission on target project RBAC
const hasPermission = await this.rbacService.checkPermission(
userId, data.projectId, 'edit'
);
if (!hasPermission) {
throw new ForbiddenError('Edit permission required to create prompt links');
}
// Parse link target
const { project: sourceProject, server, uri } = this.parseLink(data.linkTarget);
// Create service account permission on source project
await this.rbacService.createServiceAccountPermission(
data.projectId, // target project
sourceProject, // source project
server,
uri,
'read'
);
}
return this.promptRepo.create(data);
}
```
2. Clean up service account permission when link is deleted
3. Handle permission denied from source project
**Test Strategy:**
- Unit test: Link creation requires edit permission
- Unit test: Link creation without permission throws 403
- Unit test: Service account permission created on source project
- Unit test: Service account permission deleted when link deleted
- Unit test: Non-link prompts skip RBAC checks
- Integration test: End-to-end link creation with RBAC

View File

@@ -0,0 +1,64 @@
# Task ID: 59
**Title:** Update CLI create prompt command for priority and link
**Status:** done
**Dependencies:** 40 ✓
**Priority:** medium
**Description:** Extend the mcpctl create prompt command to accept --priority (1-10) and --link (project/server:uri) flags.
**Details:**
1. Update `/src/cli/src/commands/create.ts` for prompt:
```typescript
.command('prompt <name>')
.description('Create a new prompt')
.option('-p, --project <name>', 'Project to create prompt in')
.option('--priority <number>', 'Priority level (1-10, default: 5)', '5')
.option('--link <target>', 'Link to MCP resource (project/server:uri)')
.option('-f, --file <path>', 'Read content from file')
.action(async (name, options) => {
const priority = parseInt(options.priority, 10);
if (priority < 1 || priority > 10) {
console.error('Priority must be between 1 and 10');
process.exit(1);
}
let content = '';
if (options.link) {
// Linked prompts don't need content (fetched from source)
content = `[Link: ${options.link}]`;
} else if (options.file) {
content = await fs.readFile(options.file, 'utf-8');
} else {
content = await promptForContent();
}
const body = {
name,
content,
projectId: options.project,
priority,
linkTarget: options.link,
};
await api.post('/api/v1/prompts', body);
});
```
2. Validate link format: `project/server:resource-uri`
3. Add shell completions for new flags
**Test Strategy:**
- Unit test: --priority flag sets prompt priority
- Unit test: --priority validation (1-10 range)
- Unit test: --link flag sets linkTarget
- Unit test: --link validation (format check)
- Unit test: Linked prompt skips content prompt
- Unit test: Default priority is 5
- Integration test: End-to-end create with flags

View File

@@ -0,0 +1,54 @@
# Task ID: 60
**Title:** Update CLI get prompt command for -A flag and link columns
**Status:** done
**Dependencies:** 57 ✓, 59 ✓
**Priority:** medium
**Description:** Extend the mcpctl get prompt command with -A (all projects) flag and add link target and status columns to output.
**Details:**
1. Update `/src/cli/src/commands/get.ts` for prompt:
```typescript
.command('prompt [name]')
.option('-A, --all-projects', 'Show prompts from all projects')
.option('-p, --project <name>', 'Filter by project')
.action(async (name, options) => {
let url = '/api/v1/prompts';
if (options.allProjects) {
url += '?all=true';
} else if (options.project) {
url += `?project=${options.project}`;
}
const prompts = await api.get(url);
// Format table with new columns
formatPromptsTable(prompts, {
columns: ['PROJECT', 'NAME', 'PRIORITY', 'LINK', 'STATUS']
});
});
```
2. Update table formatter to handle link columns:
```
PROJECT NAME PRIORITY LINK STATUS
homeautomation security-policies 8 - -
homeautomation architecture-adr 6 system-public/docmost-mcp:docmost://pages/a1 alive
```
3. Add shell completions for -A flag
**Test Strategy:**
- Unit test: -A flag shows all projects
- Unit test: --project flag filters by project
- Unit test: PRIORITY column displayed
- Unit test: LINK column shows linkTarget or -
- Unit test: STATUS column shows linkStatus or -
- Unit test: Table formatted correctly
- Integration test: End-to-end get with flags

View File

@@ -0,0 +1,49 @@
# Task ID: 61
**Title:** Update CLI describe project command for gated status
**Status:** done
**Dependencies:** 44 ✓, 54 ✓
**Priority:** medium
**Description:** Extend mcpctl describe project to show gated status, session greeting, and prompt table with priority and link information.
**Details:**
1. Update `/src/cli/src/commands/get.ts` describe project:
```typescript
async function describeProject(name: string) {
const project = await api.get(`/api/v1/projects/${name}`);
const prompts = await api.get(`/api/v1/projects/${name}/prompt-index`);
const greeting = await getSessionGreeting(name);
console.log(`Name: ${project.name}`);
console.log(`Gated: ${project.gated}`);
console.log(`LLM Provider: ${project.llmProvider || '-'}`);
console.log(`...`);
console.log();
console.log(`Session greeting:`);
console.log(` ${greeting}`);
console.log();
console.log(`Prompts:`);
console.log(` NAME PRIORITY TYPE LINK`);
for (const p of prompts) {
const type = p.linkTarget ? 'link' : 'local';
const link = p.linkTarget || '-';
console.log(` ${p.name.padEnd(20)} ${p.priority.toString().padEnd(9)} ${type.padEnd(7)} ${link}`);
}
}
```
2. Fetch session greeting from system prompts or project config
**Test Strategy:**
- Unit test: Gated status displayed
- Unit test: Session greeting displayed
- Unit test: Prompt table with PRIORITY, TYPE, LINK columns
- Unit test: TYPE shows 'local' or 'link'
- Unit test: LINK shows target or -
- Integration test: End-to-end describe project

View File

@@ -0,0 +1,51 @@
# Task ID: 62
**Title:** Update CLI edit project command for gated field
**Status:** done
**Dependencies:** 39 ✓
**Priority:** medium
**Description:** Extend mcpctl edit project to allow editing the gated boolean field.
**Details:**
1. Update `/src/cli/src/commands/edit.ts` for project:
```typescript
async function editProject(name: string) {
const project = await api.get(`/api/v1/projects/${name}`);
// Add gated to editable fields
const yaml = `
name: ${project.name}
description: ${project.description}
gated: ${project.gated}
llmProvider: ${project.llmProvider || ''}
...`;
const edited = await openEditor(yaml);
const parsed = YAML.parse(edited);
// Validate gated is boolean
if (typeof parsed.gated !== 'boolean') {
console.error('gated must be true or false');
process.exit(1);
}
await api.put(`/api/v1/projects/${name}`, parsed);
}
```
2. Update project validation schema to accept gated
3. Handle conversion from string 'true'/'false' to boolean
**Test Strategy:**
- Unit test: Gated field appears in editor YAML
- Unit test: Gated field saved on edit
- Unit test: Boolean validation (true/false only)
- Unit test: String 'true'/'false' converted to boolean
- Integration test: End-to-end edit project gated

View File

@@ -0,0 +1,68 @@
# Task ID: 63
**Title:** Add unit tests for prompt priority and link CRUD
**Status:** done
**Dependencies:** 40 ✓, 41 ✓
**Priority:** high
**Description:** Create comprehensive unit tests for all prompt CRUD operations with the new priority and linkTarget fields.
**Details:**
1. Add tests in `/src/mcpd/tests/services/prompt-service.test.ts`:
```typescript
describe('Prompt Priority', () => {
it('creates prompt with explicit priority', async () => {
const prompt = await service.createPrompt({ ...data, priority: 8 });
expect(prompt.priority).toBe(8);
});
it('uses default priority 5 when not specified', async () => {
const prompt = await service.createPrompt(data);
expect(prompt.priority).toBe(5);
});
it('validates priority range 1-10', async () => {
await expect(service.createPrompt({ ...data, priority: 11 }))
.rejects.toThrow();
});
it('updates priority', async () => {
const updated = await service.updatePrompt(id, { priority: 3 });
expect(updated.priority).toBe(3);
});
});
describe('Prompt Links', () => {
it('creates linked prompt', async () => {
const prompt = await service.createPrompt({
...data,
linkTarget: 'project/server:uri'
});
expect(prompt.linkTarget).toBe('project/server:uri');
});
it('rejects invalid link format', async () => {
await expect(service.createPrompt({
...data,
linkTarget: 'invalid'
})).rejects.toThrow();
});
it('linkTarget is immutable on update', async () => {
// linkTarget not accepted in update schema
});
});
```
**Test Strategy:**
This task IS the test implementation. Verify:
- All priority CRUD tests pass
- All link CRUD tests pass
- Validation tests cover edge cases
- Tests use proper mocking patterns
- Coverage meets project standards

View File

@@ -0,0 +1,58 @@
# Task ID: 64
**Title:** Add unit tests for tag matching algorithm
**Status:** done
**Dependencies:** 45 ✓
**Priority:** high
**Description:** Create comprehensive unit tests for the deterministic tag matching algorithm covering score calculation, byte budget, and priority handling.
**Details:**
1. Add tests in `/src/mcplocal/tests/services/tag-matcher.test.ts`:
```typescript
describe('TagMatcherService', () => {
describe('score calculation', () => {
it('priority 10 prompts have infinite score', () => {
const score = matcher.computeScore(['any'], { priority: 10, ... });
expect(score).toBe(Infinity);
});
it('score = matching_tags * priority', () => {
const score = matcher.computeScore(
['tag1', 'tag2'],
{ priority: 5, summary: 'tag1 tag2', chapters: [] }
);
expect(score).toBe(10); // 2 tags * 5 priority
});
});
describe('matching', () => {
it('matches case-insensitively', () => {
const matches = matcher.matchesPrompt('ZIGBEE', { summary: 'zigbee setup' });
expect(matches).toBe(true);
});
it('matches substring in summary', () => { ... });
it('matches substring in chapters', () => { ... });
});
describe('byte budget', () => {
it('includes full content until budget exhausted', () => { ... });
it('matched prompts beyond budget become index entries', () => { ... });
it('non-matched prompts listed as names only', () => { ... });
});
});
```
**Test Strategy:**
This task IS the test implementation. Verify:
- Score calculation tests pass
- Matching tests cover all cases
- Byte budget tests verify allocation
- Edge cases handled (empty tags, no prompts, etc.)
- Tests are deterministic

View File

@@ -0,0 +1,63 @@
# Task ID: 65
**Title:** Add unit tests for gating state machine
**Status:** done
**Dependencies:** 50 ✓, 52 ✓
**Priority:** high
**Description:** Create comprehensive unit tests for the session gating state machine covering all transitions and edge cases.
**Details:**
1. Add tests in `/src/mcplocal/tests/router-gating.test.ts`:
```typescript
describe('Gating State Machine', () => {
describe('initial state', () => {
it('starts gated for gated project', () => {
const router = createRouter({ gated: true });
const state = router.getSessionState('session1');
expect(state.gated).toBe(true);
});
it('starts ungated for non-gated project', () => {
const router = createRouter({ gated: false });
const state = router.getSessionState('session1');
expect(state.gated).toBe(false);
});
});
describe('begin_session transition', () => {
it('ungates session on successful begin_session', async () => {
const router = createGatedRouter();
await router.handleBeginSession('session1', { tags: ['test'] });
expect(router.getSessionState('session1').gated).toBe(false);
});
it('returns matched prompts', async () => { ... });
it('sends notifications/tools/list_changed', async () => { ... });
});
describe('intercept transition', () => {
it('ungates session on tool call intercept', async () => { ... });
it('extracts keywords from tool call', async () => { ... });
it('injects briefing with tool result', async () => { ... });
});
describe('tools/list behavior', () => {
it('returns only begin_session while gated', async () => { ... });
it('returns all tools + read_prompts after ungating', async () => { ... });
});
});
```
**Test Strategy:**
This task IS the test implementation. Verify:
- Initial state tests pass
- Transition tests cover happy paths
- Edge case tests (already ungated, etc.)
- Notification tests verify signals sent
- Tests use proper mocking

View File

@@ -0,0 +1,61 @@
# Task ID: 66
**Title:** Add unit tests for LLM prompt selection
**Status:** done
**Dependencies:** 46 ✓
**Priority:** high
**Description:** Create unit tests for the LLM-based prompt selection service covering LLM interactions, fallback behavior, and priority 10 handling.
**Details:**
1. Add tests in `/src/mcplocal/tests/services/llm-prompt-selector.test.ts`:
```typescript
describe('LlmPromptSelectorService', () => {
describe('priority 10 handling', () => {
it('always includes priority 10 prompts', async () => {
const result = await selector.selectPrompts(['unrelated'], promptIndex);
expect(result.priority10).toContain(priority10Prompt);
});
});
describe('LLM selection', () => {
it('sends tags and index to heavy LLM', async () => {
await selector.selectPrompts(['zigbee', 'mqtt'], promptIndex);
expect(mockLlm.complete).toHaveBeenCalledWith(
expect.stringContaining('zigbee')
);
});
it('parses LLM response correctly', async () => {
mockLlm.complete.mockResolvedValue(
'[{"name": "prompt1", "reason": "relevant"}]'
);
const result = await selector.selectPrompts(['test'], promptIndex);
expect(result.selected[0].name).toBe('prompt1');
});
});
describe('fallback behavior', () => {
it('falls back to tag matcher on LLM error', async () => { ... });
it('falls back on LLM timeout', async () => { ... });
it('falls back when no LLM available', async () => { ... });
});
describe('summary generation', () => {
it('generates missing summaries with fast LLM', async () => { ... });
});
});
```
**Test Strategy:**
This task IS the test implementation. Verify:
- Priority 10 tests pass
- LLM interaction tests use proper mocks
- Fallback tests cover all error scenarios
- Summary generation tests pass
- Response parsing handles edge cases

View File

@@ -0,0 +1,64 @@
# Task ID: 67
**Title:** Add integration tests for gated session flow
**Status:** done
**Dependencies:** 65 ✓
**Priority:** high
**Description:** Create end-to-end integration tests for the complete gated session flow including connect, begin_session, tool calls, and read_prompts.
**Details:**
1. Add tests in `/src/mcplocal/tests/integration/gated-flow.test.ts`:
```typescript
describe('Gated Session Flow Integration', () => {
let app: FastifyInstance;
let mcpClient: McpClient;
beforeAll(async () => {
app = await createTestApp();
// Seed test project with gated=true and test prompts
});
describe('end-to-end gated flow', () => {
it('connect → begin_session with tags → tools available → correct prompts', async () => {
// 1. Connect to MCP endpoint
const session = await mcpClient.connect(app, 'test-project');
// 2. Verify only begin_session available
const toolsBefore = await session.listTools();
expect(toolsBefore.map(t => t.name)).toEqual(['begin_session']);
// 3. Call begin_session
const briefing = await session.callTool('begin_session', {
tags: ['test', 'integration']
});
expect(briefing).toContain('matched prompt content');
// 4. Verify all tools now available
const toolsAfter = await session.listTools();
expect(toolsAfter.map(t => t.name)).toContain('read_prompts');
});
});
describe('end-to-end intercept flow', () => {
it('connect → skip begin_session → call tool → keywords extracted → briefing injected', async () => { ... });
});
describe('end-to-end read_prompts', () => {
it('after ungating → request more context → additional prompts → no duplicates', async () => { ... });
});
});
```
**Test Strategy:**
This task IS the test implementation. Verify:
- Happy path tests pass
- Intercept path tests pass
- read_prompts deduplication works
- Tests use realistic data
- Tests clean up properly

View File

@@ -0,0 +1,58 @@
# Task ID: 68
**Title:** Add integration tests for prompt links
**Status:** done
**Dependencies:** 57 ✓, 58 ✓
**Priority:** medium
**Description:** Create end-to-end integration tests for prompt link creation, resolution, and dead link detection.
**Details:**
1. Add tests in `/src/mcplocal/tests/integration/prompt-links.test.ts`:
```typescript
describe('Prompt Links Integration', () => {
describe('link creation', () => {
it('creates link with RBAC permission', async () => {
// Setup: user with edit permission on target project
const prompt = await api.post('/api/v1/prompts', {
name: 'linked-prompt',
content: '[Link]',
projectId: targetProject.id,
linkTarget: 'source-project/server:uri'
});
expect(prompt.linkTarget).toBe('source-project/server:uri');
});
it('rejects link creation without RBAC permission', async () => { ... });
});
describe('link resolution', () => {
it('fetches content from source server', async () => { ... });
it('uses service account for RBAC', async () => { ... });
});
describe('dead link lifecycle', () => {
it('detects dead link when source unavailable', async () => {
// Kill source server
const prompts = await api.get('/api/v1/prompts');
const linked = prompts.find(p => p.linkTarget);
expect(linked.linkStatus).toBe('dead');
});
it('recovers when source restored', async () => { ... });
});
});
```
**Test Strategy:**
This task IS the test implementation. Verify:
- RBAC tests cover permission scenarios
- Resolution tests verify content fetched
- Dead link tests cover full lifecycle
- Tests properly mock/control source servers
- Tests clean up resources

View File

@@ -0,0 +1,59 @@
# Task ID: 69
**Title:** Add CLI unit tests for new prompt and project flags
**Status:** done
**Dependencies:** 59 ✓, 60 ✓, 61 ✓, 62 ✓
**Priority:** medium
**Description:** Create unit tests for the new CLI flags: --priority, --link for prompts, -A for get, and gated field for projects.
**Details:**
1. Add tests in `/src/cli/tests/commands/prompt.test.ts`:
```typescript
describe('create prompt command', () => {
it('--priority sets prompt priority', async () => {
await cli('create prompt test --priority 8');
expect(mockApi.post).toHaveBeenCalledWith(
'/api/v1/prompts',
expect.objectContaining({ priority: 8 })
);
});
it('--priority validates range 1-10', async () => {
await expect(cli('create prompt test --priority 15'))
.rejects.toThrow('Priority must be between 1 and 10');
});
it('--link sets linkTarget', async () => {
await cli('create prompt test --link proj/srv:uri');
expect(mockApi.post).toHaveBeenCalledWith(
'/api/v1/prompts',
expect.objectContaining({ linkTarget: 'proj/srv:uri' })
);
});
});
describe('get prompt command', () => {
it('-A shows all projects', async () => {
await cli('get prompt -A');
expect(mockApi.get).toHaveBeenCalledWith('/api/v1/prompts?all=true');
});
});
```
2. Add tests for project gated field editing
3. Add tests for describe project output
**Test Strategy:**
This task IS the test implementation. Verify:
- Flag parsing tests pass
- Validation tests cover edge cases
- API call tests verify correct parameters
- Output formatting tests verify columns
- Tests mock API properly

View File

@@ -0,0 +1,37 @@
# Task ID: 70
**Title:** Add shell completions for new CLI flags
**Status:** done
**Dependencies:** 59 ✓, 60 ✓
**Priority:** low
**Description:** Update shell completion scripts (bash, zsh, fish) to include completions for new flags: --priority, --link, -A, and gated values.
**Details:**
1. Update `/completions/mcpctl.fish`:
```fish
# create prompt completions
complete -c mcpctl -n '__fish_seen_subcommand_from create; and __fish_seen_subcommand_from prompt' -l priority -d 'Priority level (1-10)' -a '(seq 1 10)'
complete -c mcpctl -n '__fish_seen_subcommand_from create; and __fish_seen_subcommand_from prompt' -l link -d 'Link to MCP resource (project/server:uri)'
# get prompt completions
complete -c mcpctl -n '__fish_seen_subcommand_from get; and __fish_seen_subcommand_from prompt' -s A -l all-projects -d 'Show prompts from all projects'
```
2. Update bash completions similarly
3. Update zsh completions similarly
4. Add dynamic completion for priority values (1-10)
**Test Strategy:**
- Manual test: Fish completions suggest --priority with values 1-10
- Manual test: Fish completions suggest --link flag
- Manual test: Fish completions suggest -A/--all-projects
- Manual test: Bash completions work similarly
- Manual test: Zsh completions work similarly

View File

@@ -0,0 +1,70 @@
# Task ID: 71
**Title:** Define ProxyModel Public Type Contract
**Status:** done
**Dependencies:** None
**Priority:** high
**Description:** Create the core TypeScript types for the ProxyModel framework that stages will import from `mcpctl/proxymodel`. This establishes the public API contract that stage authors write against.
**Details:**
Create `src/mcplocal/src/proxymodel/types.ts` with:
```typescript
export interface StageHandler {
(content: string, ctx: StageContext): Promise<StageResult>;
}
export interface StageContext {
contentType: 'prompt' | 'toolResult' | 'resource';
sourceName: string;
projectName: string;
sessionId: string;
originalContent: string;
llm: LLMProvider;
cache: CacheProvider;
log: Logger;
config: Record<string, unknown>;
}
export interface StageResult {
content: string;
sections?: Section[];
metadata?: Record<string, unknown>;
}
export interface Section {
id: string;
title: string;
content: string;
}
export interface LLMProvider {
complete(prompt: string, options?: { system?: string; maxTokens?: number }): Promise<string>;
available(): boolean;
}
export interface CacheProvider {
getOrCompute(key: string, compute: () => Promise<string>): Promise<string>;
hash(content: string): string;
get(key: string): Promise<string | null>;
set(key: string, value: string): Promise<void>;
}
export interface Logger {
debug(msg: string): void;
info(msg: string): void;
warn(msg: string): void;
error(msg: string): void;
}
```
Also create `src/mcplocal/src/proxymodel/index.ts` as the public entrypoint that re-exports these types. Update `package.json` exports to expose `mcpctl/proxymodel`.
**Test Strategy:**
Unit tests verifying type exports are accessible from the public entrypoint. Create a sample stage file that imports from `mcpctl/proxymodel` and verify it compiles without errors.

View File

@@ -0,0 +1,49 @@
# Task ID: 72
**Title:** Implement LLMProvider Adapter
**Status:** done
**Dependencies:** 71 ✓
**Priority:** high
**Description:** Create an adapter that wraps the existing ProviderRegistry to implement the StageContext.llm interface, providing stages with a simplified LLM access API.
**Details:**
Create `src/mcplocal/src/proxymodel/llm-adapter.ts`:
```typescript
import type { LLMProvider } from './types';
import type { ProviderRegistry } from '../providers/registry';
export function createLLMAdapter(registry: ProviderRegistry, projectName: string): LLMProvider {
return {
async complete(prompt: string, options?: { system?: string; maxTokens?: number }): Promise<string> {
const provider = registry.getProvider('heavy');
if (!provider) throw new Error('No LLM provider configured');
const messages = options?.system
? [{ role: 'system', content: options.system }, { role: 'user', content: prompt }]
: [{ role: 'user', content: prompt }];
const result = await provider.complete({
messages,
maxTokens: options?.maxTokens ?? 1000,
});
return result.content;
},
available(): boolean {
return registry.getProvider('heavy') !== null;
}
};
}
```
This adapter uses the 'heavy' tier from the existing registry, preserving the project-level LLM configuration.
**Test Strategy:**
Unit test with mocked ProviderRegistry verifying complete() calls are delegated correctly. Test available() returns false when no provider is configured. Integration test with a real provider.

View File

@@ -0,0 +1,67 @@
# Task ID: 73
**Title:** Implement In-Memory CacheProvider
**Status:** done
**Dependencies:** 71 ✓
**Priority:** high
**Description:** Create the CacheProvider implementation that stages use for caching expensive computations. Start with in-memory cache for Phase 1, with content-addressed keys.
**Details:**
Create `src/mcplocal/src/proxymodel/cache-provider.ts`:
```typescript
import { createHash } from 'crypto';
import type { CacheProvider } from './types';
export class InMemoryCacheProvider implements CacheProvider {
private cache = new Map<string, { value: string; timestamp: number }>();
private maxSize: number;
private ttlMs: number;
constructor(options: { maxSize?: number; ttlMs?: number } = {}) {
this.maxSize = options.maxSize ?? 1000;
this.ttlMs = options.ttlMs ?? 3600000; // 1 hour default
}
hash(content: string): string {
return createHash('sha256').update(content).digest('hex').slice(0, 16);
}
async get(key: string): Promise<string | null> {
const entry = this.cache.get(key);
if (!entry) return null;
if (Date.now() - entry.timestamp > this.ttlMs) {
this.cache.delete(key);
return null;
}
return entry.value;
}
async set(key: string, value: string): Promise<void> {
if (this.cache.size >= this.maxSize) this.evictOldest();
this.cache.set(key, { value, timestamp: Date.now() });
}
async getOrCompute(key: string, compute: () => Promise<string>): Promise<string> {
const cached = await this.get(key);
if (cached !== null) return cached;
const value = await compute();
await this.set(key, value);
return value;
}
private evictOldest(): void {
const oldest = [...this.cache.entries()].sort((a, b) => a[1].timestamp - b[1].timestamp)[0];
if (oldest) this.cache.delete(oldest[0]);
}
}
```
**Test Strategy:**
Unit tests for: hash() produces consistent output, get() returns null for missing keys, set()/get() round-trip works, TTL expiration works, LRU eviction triggers at maxSize, getOrCompute() caches and returns cached values.

View File

@@ -0,0 +1,57 @@
# Task ID: 74
**Title:** Implement Content Type Detection
**Status:** done
**Dependencies:** None
**Priority:** high
**Description:** Create a utility that detects content type (JSON, YAML, XML, code, prose) for structural splitting in the section-split stage.
**Details:**
Create `src/mcplocal/src/proxymodel/content-detection.ts`:
```typescript
export type ContentType = 'json' | 'yaml' | 'xml' | 'code' | 'prose';
export function detectContentType(content: string): ContentType {
const trimmed = content.trimStart();
// JSON detection
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
try {
JSON.parse(content);
return 'json';
} catch { /* not valid JSON, continue */ }
}
// XML detection
if (trimmed.startsWith('<?xml') || /^<[a-zA-Z][^>]*>/.test(trimmed)) {
return 'xml';
}
// YAML detection (key: value at start of lines)
if (/^[a-zA-Z_][a-zA-Z0-9_]*:\s/m.test(trimmed) && !trimmed.includes('{')) {
return 'yaml';
}
// Code detection (common patterns)
const codePatterns = [
/^(function |class |def |const |let |var |import |export |package |pub fn |fn |impl )/m,
/^#include\s+[<"]/m,
/^(public |private |protected )?(static )?(void |int |string |bool )/m,
];
if (codePatterns.some(p => p.test(trimmed))) {
return 'code';
}
return 'prose';
}
```
**Test Strategy:**
Unit tests with sample content for each type: valid JSON objects/arrays, XML documents, YAML configs, code snippets in multiple languages (JS, Python, Rust, Go, Java), and prose markdown. Edge cases: JSON-like strings that aren't valid JSON, mixed content.

View File

@@ -0,0 +1,77 @@
# Task ID: 75
**Title:** Implement section-split Stage
**Status:** done
**Dependencies:** 71 ✓, 74 ✓
**Priority:** high
**Description:** Create the built-in section-split stage that splits content based on detected content type, using structural boundaries for JSON/YAML/XML and headers for prose.
**Details:**
Create `src/mcplocal/src/proxymodel/stages/section-split.ts`:
```typescript
import type { StageHandler, Section } from '../types';
import { detectContentType } from '../content-detection';
const handler: StageHandler = async (content, ctx) => {
const minSize = (ctx.config.minSectionSize as number) ?? 2000;
const maxSize = (ctx.config.maxSectionSize as number) ?? 15000;
const contentType = detectContentType(content);
let sections: Section[];
switch (contentType) {
case 'json':
sections = splitJson(content, minSize, maxSize);
break;
case 'yaml':
sections = splitYaml(content, minSize, maxSize);
break;
case 'xml':
sections = splitXml(content, minSize, maxSize);
break;
case 'code':
sections = splitCode(content, minSize);
break;
default:
sections = splitProse(content, minSize);
}
if (sections.length === 0) {
return { content, sections: [{ id: 'main', title: 'Content', content }] };
}
const toc = sections.map((s, i) => `[${s.id}] ${s.title}`).join('\n');
return {
content: `${sections.length} sections (${contentType}):\n${toc}`,
sections,
};
};
function splitJson(content: string, minSize: number, maxSize: number): Section[] {
const parsed = JSON.parse(content);
if (Array.isArray(parsed)) {
return parsed.map((item, i) => ({
id: item.id ?? item.name ?? `item-${i}`,
title: item.label ?? item.title ?? item.name ?? `Item ${i}`,
content: JSON.stringify(item, null, 2),
}));
}
return Object.entries(parsed).map(([key, value]) => ({
id: key,
title: key,
content: JSON.stringify(value, null, 2),
}));
}
// Similar implementations for splitYaml, splitXml, splitCode, splitProse
```
**Test Strategy:**
Unit tests for each content type: JSON arrays split by element, JSON objects split by key, YAML split by top-level keys, XML split by elements, prose split by markdown headers. Test minSize/maxSize thresholds. Test fallback when content can't be parsed.

View File

@@ -0,0 +1,84 @@
# Task ID: 76
**Title:** Implement summarize-tree Stage
**Status:** done
**Dependencies:** 71 ✓, 72 ✓, 73 ✓, 74 ✓
**Priority:** high
**Description:** Create the built-in summarize-tree stage that recursively summarizes sections, using structural summaries for programmatic content and LLM summaries for prose.
**Details:**
Create `src/mcplocal/src/proxymodel/stages/summarize-tree.ts`:
```typescript
import type { StageHandler, Section, StageContext } from '../types';
import { detectContentType } from '../content-detection';
const handler: StageHandler = async (content, ctx) => {
const maxTokens = (ctx.config.maxSummaryTokens as number) ?? 200;
const maxGroup = (ctx.config.maxGroupSize as number) ?? 5;
const maxDepth = (ctx.config.maxDepth as number) ?? 3;
// Parse sections from previous stage or create single section
const inputSections = parseSectionsFromContent(content);
const tree = await buildTree(inputSections, ctx, { maxTokens, maxGroup, maxDepth, depth: 0 });
const toc = tree.map(s =>
`[${s.id}] ${s.title} — ${s.metadata?.summary ?? ''}` +
(s.sections?.length ? `\n → ${s.sections.length} sub-sections` : '')
).join('\n');
return {
content: `${tree.length} sections:\n${toc}\n\nUse section parameter to read details.`,
sections: tree,
};
};
async function buildTree(
sections: Section[],
ctx: StageContext,
opts: { maxTokens: number; maxGroup: number; maxDepth: number; depth: number }
): Promise<Section[]> {
for (const section of sections) {
const contentType = detectContentType(section.content);
// Structural summary for programmatic content (no LLM needed)
if (contentType !== 'prose') {
section.metadata = { summary: generateStructuralSummary(section.content, contentType) };
} else {
// LLM summary for prose (cached)
const cacheKey = `summary:${ctx.cache.hash(section.content)}:${opts.maxTokens}`;
const summary = await ctx.cache.getOrCompute(cacheKey, () =>
ctx.llm.complete(
`Summarize in ${opts.maxTokens} tokens, preserve MUST/REQUIRED items:\n\n${section.content}`
)
);
section.metadata = { summary };
}
// Recurse if large and not at max depth
if (section.content.length > 5000 && opts.depth < opts.maxDepth) {
section.sections = await buildTree(
splitContent(section.content),
ctx,
{ ...opts, depth: opts.depth + 1 }
);
}
}
return sections;
}
function generateStructuralSummary(content: string, type: string): string {
// Generate summary from structure: key names, array lengths, types
// No LLM needed for JSON/YAML/XML/code
}
```
**Test Strategy:**
Unit tests: prose content gets LLM summary (mock LLM), JSON content gets structural summary without LLM call, recursive splitting triggers at 5000 chars, maxDepth is respected, cache is used for repeated content. Integration test with real LLM provider.

View File

@@ -0,0 +1,63 @@
# Task ID: 77
**Title:** Implement passthrough and paginate Stages
**Status:** done
**Dependencies:** 71 ✓
**Priority:** high
**Description:** Create the built-in passthrough (no-op) and paginate (large response splitting) stages that form the default proxymodel.
**Details:**
Create `src/mcplocal/src/proxymodel/stages/passthrough.ts`:
```typescript
import type { StageHandler } from '../types';
const handler: StageHandler = async (content, ctx) => {
return { content };
};
export default handler;
```
Create `src/mcplocal/src/proxymodel/stages/paginate.ts`:
```typescript
import type { StageHandler, Section } from '../types';
const handler: StageHandler = async (content, ctx) => {
const pageSize = (ctx.config.pageSize as number) ?? 8000;
if (content.length <= pageSize) {
return { content };
}
const pages: Section[] = [];
let offset = 0;
let pageNum = 1;
while (offset < content.length) {
const pageContent = content.slice(offset, offset + pageSize);
pages.push({
id: `page-${pageNum}`,
title: `Page ${pageNum}`,
content: pageContent,
});
offset += pageSize;
pageNum++;
}
return {
content: `Content split into ${pages.length} pages (${content.length} chars total). Use section parameter to read specific pages.`,
sections: pages,
};
};
export default handler;
```
**Test Strategy:**
passthrough: verify content returned unchanged. paginate: verify content under threshold returns unchanged, content over threshold splits correctly, page boundaries are correct, section IDs are sequential.

View File

@@ -0,0 +1,78 @@
# Task ID: 78
**Title:** Create ProxyModel YAML Schema and Loader
**Status:** done
**Dependencies:** 71 ✓
**Priority:** high
**Description:** Define the YAML schema for proxymodel definitions and implement the loader that reads from ~/.mcpctl/proxymodels/ and merges with built-ins.
**Details:**
Create `src/mcplocal/src/proxymodel/schema.ts`:
```typescript
import { z } from 'zod';
export const ProxyModelSchema = z.object({
kind: z.literal('ProxyModel'),
metadata: z.object({
name: z.string(),
}),
spec: z.object({
controller: z.string().optional().default('gate'),
controllerConfig: z.record(z.unknown()).optional(),
stages: z.array(z.object({
type: z.string(),
config: z.record(z.unknown()).optional(),
})),
appliesTo: z.array(z.enum(['prompts', 'toolResults', 'resource'])).optional(),
cacheable: z.boolean().optional().default(true),
}),
});
export type ProxyModelDefinition = z.infer<typeof ProxyModelSchema>;
```
Create `src/mcplocal/src/proxymodel/loader.ts`:
```typescript
import { readdir, readFile } from 'fs/promises';
import { join } from 'path';
import { parse as parseYaml } from 'yaml';
import { ProxyModelSchema, type ProxyModelDefinition } from './schema';
import { getBuiltInProxyModels } from './built-in-models';
const PROXYMODELS_DIR = join(process.env.HOME ?? '', '.mcpctl', 'proxymodels');
export async function loadProxyModels(): Promise<Map<string, ProxyModelDefinition>> {
const models = new Map<string, ProxyModelDefinition>();
// Load built-ins first
for (const [name, model] of getBuiltInProxyModels()) {
models.set(name, model);
}
// Load local (overrides built-ins)
try {
const files = await readdir(PROXYMODELS_DIR);
for (const file of files.filter(f => f.endsWith('.yaml') || f.endsWith('.yml'))) {
const content = await readFile(join(PROXYMODELS_DIR, file), 'utf-8');
const parsed = parseYaml(content);
const validated = ProxyModelSchema.parse(parsed);
models.set(validated.metadata.name, validated);
}
} catch (e) {
// Directory doesn't exist or can't be read - use built-ins only
}
return models;
}
```
**Test Strategy:**
Unit tests: valid YAML parses correctly, invalid YAML throws validation error, local models override built-ins with same name, missing directory doesn't throw. Create test fixtures for various YAML configurations.

View File

@@ -0,0 +1,68 @@
# Task ID: 79
**Title:** Implement Stage Registry and Dynamic Loader
**Status:** done
**Dependencies:** 71 ✓, 75 ✓, 76 ✓, 77 ✓
**Priority:** high
**Description:** Create the stage registry that resolves stage names to handlers, loading from ~/.mcpctl/stages/ for custom stages and falling back to built-ins.
**Details:**
Create `src/mcplocal/src/proxymodel/stage-registry.ts`:
```typescript
import { readdir, stat } from 'fs/promises';
import { join } from 'path';
import type { StageHandler } from './types';
const STAGES_DIR = join(process.env.HOME ?? '', '.mcpctl', 'stages');
const builtInStages: Map<string, StageHandler> = new Map();
const customStages: Map<string, StageHandler> = new Map();
// Register built-ins at module load
import passthrough from './stages/passthrough';
import paginate from './stages/paginate';
import sectionSplit from './stages/section-split';
import summarizeTree from './stages/summarize-tree';
builtInStages.set('passthrough', passthrough);
builtInStages.set('paginate', paginate);
builtInStages.set('section-split', sectionSplit);
builtInStages.set('summarize-tree', summarizeTree);
export async function loadCustomStages(): Promise<void> {
customStages.clear();
try {
const files = await readdir(STAGES_DIR);
for (const file of files.filter(f => f.endsWith('.ts') || f.endsWith('.js'))) {
const name = file.replace(/\.(ts|js)$/, '');
const module = await import(join(STAGES_DIR, file));
customStages.set(name, module.default);
}
} catch { /* directory doesn't exist */ }
}
export function getStage(name: string): StageHandler | null {
return customStages.get(name) ?? builtInStages.get(name) ?? null;
}
export function listStages(): { name: string; source: 'built-in' | 'local' }[] {
const result: { name: string; source: 'built-in' | 'local' }[] = [];
for (const name of builtInStages.keys()) {
result.push({ name, source: customStages.has(name) ? 'local' : 'built-in' });
}
for (const name of customStages.keys()) {
if (!builtInStages.has(name)) result.push({ name, source: 'local' });
}
return result;
}
```
**Test Strategy:**
Unit tests: built-in stages are registered, getStage() returns correct handler, custom stages override built-ins, listStages() shows correct sources, missing stages return null. Integration test with actual stage files in temp directory.

View File

@@ -0,0 +1,90 @@
# Task ID: 80
**Title:** Implement Pipeline Executor
**Status:** done
**Dependencies:** 71 ✓, 72 ✓, 73 ✓, 79 ✓
**Priority:** high
**Description:** Create the pipeline executor that runs content through a sequence of stages, managing context, caching, and error handling.
**Details:**
Create `src/mcplocal/src/proxymodel/executor.ts`:
```typescript
import type { StageContext, StageResult, Section } from './types';
import type { ProxyModelDefinition } from './schema';
import { getStage } from './stage-registry';
import { createLLMAdapter } from './llm-adapter';
import { InMemoryCacheProvider } from './cache-provider';
import type { ProviderRegistry } from '../providers/registry';
export interface ExecuteOptions {
content: string;
contentType: 'prompt' | 'toolResult' | 'resource';
sourceName: string;
projectName: string;
sessionId: string;
proxyModel: ProxyModelDefinition;
providerRegistry: ProviderRegistry;
cache?: InMemoryCacheProvider;
}
export async function executePipeline(opts: ExecuteOptions): Promise<StageResult> {
const { content, proxyModel, providerRegistry } = opts;
const cache = opts.cache ?? new InMemoryCacheProvider();
const llm = createLLMAdapter(providerRegistry, opts.projectName);
let currentContent = content;
let sections: Section[] | undefined;
let metadata: Record<string, unknown> = {};
for (const stageConfig of proxyModel.spec.stages) {
const handler = getStage(stageConfig.type);
if (!handler) {
console.warn(`Stage '${stageConfig.type}' not found, skipping`);
continue;
}
const ctx: StageContext = {
contentType: opts.contentType,
sourceName: opts.sourceName,
projectName: opts.projectName,
sessionId: opts.sessionId,
originalContent: content,
llm,
cache,
log: createLogger(stageConfig.type),
config: stageConfig.config ?? {},
};
try {
const result = await handler(currentContent, ctx);
currentContent = result.content;
if (result.sections) sections = result.sections;
if (result.metadata) metadata = { ...metadata, ...result.metadata };
} catch (err) {
console.error(`Stage '${stageConfig.type}' failed:`, err);
// Continue with previous content on error
}
}
return { content: currentContent, sections, metadata };
}
function createLogger(stageName: string) {
return {
debug: (msg: string) => console.debug(`[${stageName}] ${msg}`),
info: (msg: string) => console.info(`[${stageName}] ${msg}`),
warn: (msg: string) => console.warn(`[${stageName}] ${msg}`),
error: (msg: string) => console.error(`[${stageName}] ${msg}`),
};
}
```
**Test Strategy:**
Unit tests: single stage executes correctly, multiple stages chain output to input, originalContent preserved across stages, missing stage logs warning and continues, stage error doesn't break pipeline, sections/metadata accumulate correctly.

View File

@@ -0,0 +1,59 @@
# Task ID: 81
**Title:** Define Built-in ProxyModels (default, subindex)
**Status:** done
**Dependencies:** 78 ✓
**Priority:** high
**Description:** Create the built-in proxymodel definitions for 'default' (current behavior) and 'subindex' (hierarchical navigation).
**Details:**
Create `src/mcplocal/src/proxymodel/built-in-models.ts`:
```typescript
import type { ProxyModelDefinition } from './schema';
export function getBuiltInProxyModels(): Map<string, ProxyModelDefinition> {
const models = new Map<string, ProxyModelDefinition>();
models.set('default', {
kind: 'ProxyModel',
metadata: { name: 'default' },
spec: {
controller: 'gate',
controllerConfig: { byteBudget: 8192 },
stages: [
{ type: 'passthrough' },
{ type: 'paginate', config: { pageSize: 8000 } },
],
appliesTo: ['prompts', 'toolResults'],
cacheable: false,
},
});
models.set('subindex', {
kind: 'ProxyModel',
metadata: { name: 'subindex' },
spec: {
controller: 'gate',
controllerConfig: { byteBudget: 8192 },
stages: [
{ type: 'section-split', config: { minSectionSize: 2000, maxSectionSize: 15000 } },
{ type: 'summarize-tree', config: { maxSummaryTokens: 200, maxGroupSize: 5, maxDepth: 3 } },
],
appliesTo: ['prompts', 'toolResults'],
cacheable: true,
},
});
return models;
}
```
**Test Strategy:**
Unit tests: both models are returned by getBuiltInProxyModels(), 'default' has passthrough+paginate stages, 'subindex' has section-split+summarize-tree stages, both schemas validate correctly.

View File

@@ -0,0 +1,66 @@
# Task ID: 82
**Title:** Integrate Pipeline Executor into Router
**Status:** done
**Dependencies:** 80 ✓, 81 ✓
**Priority:** high
**Description:** Modify the McpRouter to route content through the proxymodel pipeline, keeping the gating logic cleanly separated from content processing.
**Details:**
Modify `src/mcplocal/src/router.ts` to:
1. Add proxyModel resolution during router creation:
```typescript
import { loadProxyModels } from './proxymodel/loader';
import { executePipeline } from './proxymodel/executor';
interface RouterOptions {
proxyModelName?: string;
// ... existing options
}
async function createRouter(opts: RouterOptions): Promise<McpRouter> {
const proxyModels = await loadProxyModels();
const proxyModel = proxyModels.get(opts.proxyModelName ?? 'default');
// ...
}
```
2. Add content processing method:
```typescript
async processContent(
content: string,
type: 'prompt' | 'toolResult',
sourceName: string,
sessionId: string
): Promise<StageResult> {
if (!this.proxyModel) return { content };
const appliesTo = this.proxyModel.spec.appliesTo ?? ['prompts', 'toolResults'];
if (!appliesTo.includes(type === 'prompt' ? 'prompts' : 'toolResults')) {
return { content };
}
return executePipeline({
content,
contentType: type,
sourceName,
projectName: this.projectName,
sessionId,
proxyModel: this.proxyModel,
providerRegistry: this.providerRegistry,
cache: this.cache,
});
}
```
3. Call processContent at the appropriate points in the request flow (prompt serving, tool result handling) WITHOUT interweaving with gating logic.
**Test Strategy:**
Integration tests: default proxymodel passes content through unchanged, subindex proxymodel produces summaries, appliesTo filtering works correctly, gating still works as before with proxymodel processing happening at the right stage.

View File

@@ -0,0 +1,49 @@
# Task ID: 83
**Title:** Implement Section Drill-Down for Prompts
**Status:** cancelled
**Dependencies:** 82 ✓
**Priority:** high
**Description:** Extend read_prompts to support section parameter for drilling into specific sections produced by proxymodel stages.
**Details:**
Modify the read_prompts handler in `src/mcplocal/src/router.ts`:
```typescript
// In the read_prompts tool handler
if (args.section) {
// Look up section in the processed result
const sectionId = args.section;
const cachedResult = this.sectionCache.get(promptName);
if (cachedResult?.sections) {
const section = findSection(cachedResult.sections, sectionId);
if (section) {
return { content: [{ type: 'text', text: section.content }] };
}
return { content: [{ type: 'text', text: `Section '${sectionId}' not found` }], isError: true };
}
}
// Helper to find section by ID (supports nested sections)
function findSection(sections: Section[], id: string): Section | null {
for (const s of sections) {
if (s.id === id) return s;
if (s.sections) {
const nested = findSection(s.sections, id);
if (nested) return nested;
}
}
return null;
}
```
Also add a sectionCache Map to store processed results with their sections for drill-down.
**Test Strategy:**
Integration tests: read_prompts with section parameter returns correct section content, nested section lookup works, missing section returns error, section cache populated after initial processing.

View File

@@ -0,0 +1,47 @@
# Task ID: 84
**Title:** Implement Section Drill-Down for Tool Results
**Status:** done
**Dependencies:** 82 ✓
**Priority:** medium
**Description:** Extend tool result handling to support _section parameter for drilling into specific sections of large tool responses.
**Details:**
Modify tool call handling in `src/mcplocal/src/router.ts`:
```typescript
// When processing tool calls
if (args._section) {
const sectionId = args._section;
delete args._section; // Don't pass to upstream
// Check cache for previous full result
const cacheKey = `tool:${serverName}/${toolName}:${JSON.stringify(args)}`;
const cachedResult = this.toolResultCache.get(cacheKey);
if (cachedResult?.sections) {
const section = findSection(cachedResult.sections, sectionId);
if (section) {
return { content: [{ type: 'text', text: section.content }] };
}
}
// If no cache, make the full call and process, then serve section
}
// After receiving tool result, process through pipeline
const processed = await this.processContent(result, 'toolResult', `${serverName}/${toolName}`, sessionId);
if (processed.sections) {
this.toolResultCache.set(cacheKey, processed);
}
```
Add a toolResultCache Map with appropriate TTL.
**Test Strategy:**
Integration tests: large tool result gets processed into sections, _section parameter returns specific section, _section removed before upstream call, cache hit serves from cache, cache miss processes and caches.

View File

@@ -0,0 +1,65 @@
# Task ID: 85
**Title:** Implement Hot-Reload for Stages
**Status:** done
**Dependencies:** 79 ✓
**Priority:** medium
**Description:** Add file watching for ~/.mcpctl/stages/ to automatically reload custom stages when they change without restarting mcplocal.
**Details:**
Modify `src/mcplocal/src/proxymodel/stage-registry.ts`:
```typescript
import { watch, FSWatcher } from 'fs';
import { join, basename } from 'path';
let watcher: FSWatcher | null = null;
const stageFileHashes: Map<string, string> = new Map();
export function startStageWatcher(): void {
if (watcher) return;
try {
watcher = watch(STAGES_DIR, async (eventType, filename) => {
if (!filename || (!filename.endsWith('.ts') && !filename.endsWith('.js'))) return;
const name = filename.replace(/\.(ts|js)$/, '');
const fullPath = join(STAGES_DIR, filename);
if (eventType === 'rename') {
// File added or removed
await loadCustomStages();
console.info(`[proxymodel] Stages reloaded due to ${filename} change`);
} else if (eventType === 'change') {
// File modified - invalidate module cache and reload
delete require.cache[require.resolve(fullPath)];
try {
const module = await import(fullPath + '?t=' + Date.now());
customStages.set(name, module.default);
console.info(`[proxymodel] Stage '${name}' hot-reloaded`);
} catch (err) {
console.error(`[proxymodel] Failed to reload stage '${name}':`, err);
}
}
});
} catch {
// Directory doesn't exist - no watching needed
}
}
export function stopStageWatcher(): void {
watcher?.close();
watcher = null;
}
```
Call startStageWatcher() during mcplocal initialization.
**Test Strategy:**
Integration tests: modify a stage file and verify the new version is loaded without restart, add a new stage file and verify it becomes available, remove a stage file and verify it's no longer available, syntax errors in stage file don't crash the watcher.

View File

@@ -0,0 +1,57 @@
# Task ID: 86
**Title:** Implement Hot-Reload for ProxyModels
**Status:** done
**Dependencies:** 78 ✓
**Priority:** medium
**Description:** Add file watching for ~/.mcpctl/proxymodels/ to automatically reload proxymodel definitions when they change.
**Details:**
Create `src/mcplocal/src/proxymodel/model-watcher.ts`:
```typescript
import { watch, FSWatcher } from 'fs';
import { join } from 'path';
import { readFile } from 'fs/promises';
import { parse as parseYaml } from 'yaml';
import { ProxyModelSchema } from './schema';
const PROXYMODELS_DIR = join(process.env.HOME ?? '', '.mcpctl', 'proxymodels');
let watcher: FSWatcher | null = null;
const modelUpdateCallbacks: Set<() => void> = new Set();
export function onModelUpdate(callback: () => void): () => void {
modelUpdateCallbacks.add(callback);
return () => modelUpdateCallbacks.delete(callback);
}
export function startModelWatcher(): void {
if (watcher) return;
try {
watcher = watch(PROXYMODELS_DIR, async (eventType, filename) => {
if (!filename || (!filename.endsWith('.yaml') && !filename.endsWith('.yml'))) return;
console.info(`[proxymodel] Model file ${filename} changed, reloading...`);
// Notify all subscribers to reload their models
for (const cb of modelUpdateCallbacks) {
try { cb(); } catch (err) { console.error('Model update callback failed:', err); }
}
});
} catch {
// Directory doesn't exist
}
}
```
Integrate with router to reload proxymodels when files change.
**Test Strategy:**
Integration tests: modify a proxymodel YAML and verify changes take effect, add a new proxymodel and verify it becomes available, invalid YAML logs error but doesn't crash.

View File

@@ -0,0 +1,48 @@
# Task ID: 87
**Title:** Add proxyModel Field to Project Schema
**Status:** cancelled
**Dependencies:** None
**Priority:** high
**Description:** Extend the Project database schema and API to support proxyModel field and proxyModelOverrides for per-content-type configuration.
**Details:**
Update `src/db/prisma/schema.prisma`:
```prisma
model Project {
// ... existing fields
proxyModel String? @default("default")
proxyModelOverrides Json? // { prompts: { "prompt-name": "model" }, toolResults: { "server/tool": "model" } }
}
```
Run `npx prisma migrate dev --name add_proxymodel_field`.
Update `src/mcpd/src/routes/projects.ts` to include the new fields in CRUD operations.
Update `src/cli/src/commands/get.ts` and `describe.ts` to display proxyModel.
Update `src/cli/src/commands/patch.ts` to support `--set proxyModel=<name>`.
**Test Strategy:**
Database migration test: verify migration applies cleanly. API tests: verify proxyModel field is returned in project GET, can be updated via PATCH. CLI tests: verify `mcpctl describe project <name>` shows proxyModel.
## Subtasks
### 87.1. Minimal placeholder subtask
**Status:** pending
**Dependencies:** None
This task requires complete rewrite before expansion.
**Details:**
Task 87 has been marked as DO NOT EXPAND and needs to be completely rewritten first. No subtasks should be generated until the task is properly redefined.

View File

@@ -0,0 +1,38 @@
# Task ID: 88
**Title:** Rename proxyMode: filtered to proxyMode: proxy
**Status:** cancelled
**Dependencies:** 87 ✗
**Priority:** low
**Description:** Rename the existing proxyMode value 'filtered' to 'proxy' for clarity, with backwards compatibility for existing configs.
**Details:**
Update `src/db/prisma/schema.prisma`:
```prisma
enum ProxyMode {
direct
proxy // renamed from 'filtered'
}
```
Create migration that updates existing 'filtered' values to 'proxy':
```sql
UPDATE Project SET proxyMode = 'proxy' WHERE proxyMode = 'filtered';
```
Update all code references from 'filtered' to 'proxy':
- `src/mcplocal/src/http/project-mcp-endpoint.ts`
- `src/cli/src/commands/create.ts`
- Documentation and help text
For backwards compatibility in config files, add a normalization step that treats 'filtered' as 'proxy'.
**Test Strategy:**
Migration test: existing projects with proxyMode='filtered' are updated to 'proxy'. Config parsing test: both 'filtered' and 'proxy' values work. CLI test: help text shows 'proxy' not 'filtered'.

View File

@@ -0,0 +1,53 @@
# Task ID: 89
**Title:** Implement mcpctl get proxymodels Command
**Status:** cancelled
**Dependencies:** 78 ✓, 79 ✓
**Priority:** medium
**Description:** Add CLI command to list all available proxymodels (built-in + local) with source, stages, and requirements.
**Details:**
Create `src/cli/src/commands/get-proxymodels.ts`:
```typescript
import { Command } from 'commander';
import { loadProxyModels } from 'mcplocal/proxymodel/loader';
import { listStages } from 'mcplocal/proxymodel/stage-registry';
import Table from 'cli-table3';
export function registerGetProxymodels(program: Command): void {
program
.command('get proxymodels')
.description('List all available proxymodels')
.action(async () => {
const models = await loadProxyModels();
const stageInfo = new Map(listStages().map(s => [s.name, s]));
const table = new Table({
head: ['NAME', 'SOURCE', 'STAGES', 'REQUIRES-LLM', 'CACHEABLE'],
});
for (const [name, model] of models) {
const source = isBuiltIn(name) ? 'built-in' : 'local';
const stages = model.spec.stages.map(s => s.type).join(',');
const requiresLlm = model.spec.stages.some(s => stageRequiresLlm(s.type));
const cacheable = model.spec.cacheable ? 'yes' : 'no';
table.push([name, source, stages, requiresLlm ? 'yes' : 'no', cacheable]);
}
console.log(table.toString());
});
}
```
Register in `src/cli/src/commands/get.ts` as a subcommand.
**Test Strategy:**
CLI test: `mcpctl get proxymodels` outputs table with expected columns. Test with only built-ins, test with local overrides, verify correct source detection.

View File

@@ -0,0 +1,47 @@
# Task ID: 90
**Title:** Implement mcpctl get stages Command
**Status:** cancelled
**Dependencies:** 79 ✓
**Priority:** medium
**Description:** Add CLI command to list all available stages (built-in + custom) with source and LLM requirements.
**Details:**
Create `src/cli/src/commands/get-stages.ts`:
```typescript
import { Command } from 'commander';
import { listStages } from 'mcplocal/proxymodel/stage-registry';
import Table from 'cli-table3';
const LLM_REQUIRING_STAGES = ['summarize', 'summarize-tree', 'enhance', 'compress'];
export function registerGetStages(program: Command): void {
program
.command('get stages')
.description('List all available stages')
.action(async () => {
const stages = listStages();
const table = new Table({
head: ['NAME', 'SOURCE', 'REQUIRES-LLM'],
});
for (const stage of stages) {
const requiresLlm = LLM_REQUIRING_STAGES.includes(stage.name);
table.push([stage.name, stage.source, requiresLlm ? 'yes' : 'no']);
}
console.log(table.toString());
});
}
```
**Test Strategy:**
CLI test: `mcpctl get stages` outputs table with expected columns. Test with only built-ins, test with custom stages in ~/.mcpctl/stages/, verify custom overrides show 'local' source.

View File

@@ -0,0 +1,57 @@
# Task ID: 91
**Title:** Implement mcpctl describe proxymodel Command
**Status:** cancelled
**Dependencies:** 78 ✓
**Priority:** medium
**Description:** Add CLI command to show detailed information about a specific proxymodel including full stage configuration.
**Details:**
Create `src/cli/src/commands/describe-proxymodel.ts`:
```typescript
import { Command } from 'commander';
import { loadProxyModels } from 'mcplocal/proxymodel/loader';
import { stringify as yamlStringify } from 'yaml';
export function registerDescribeProxymodel(program: Command): void {
program
.command('describe proxymodel <name>')
.description('Show detailed information about a proxymodel')
.action(async (name: string) => {
const models = await loadProxyModels();
const model = models.get(name);
if (!model) {
console.error(`Proxymodel '${name}' not found`);
process.exit(1);
}
console.log(`Name: ${model.metadata.name}`);
console.log(`Source: ${isBuiltIn(name) ? 'built-in' : 'local'}`);
console.log(`Controller: ${model.spec.controller ?? 'gate'}`);
console.log(`Cacheable: ${model.spec.cacheable ? 'yes' : 'no'}`);
console.log(`Applies to: ${(model.spec.appliesTo ?? ['prompts', 'toolResults']).join(', ')}`);
console.log('');
console.log('Stages:');
for (const stage of model.spec.stages) {
console.log(` - ${stage.type}`);
if (stage.config) {
console.log(` config:`);
for (const [k, v] of Object.entries(stage.config)) {
console.log(` ${k}: ${JSON.stringify(v)}`);
}
}
}
});
}
```
**Test Strategy:**
CLI test: `mcpctl describe proxymodel default` shows expected output. Test with proxymodel that has stage configs, verify all fields displayed correctly.

View File

@@ -0,0 +1,57 @@
# Task ID: 92
**Title:** Implement mcpctl describe stage Command
**Status:** cancelled
**Dependencies:** 79 ✓
**Priority:** medium
**Description:** Add CLI command to show detailed information about a specific stage including its source location.
**Details:**
Create `src/cli/src/commands/describe-stage.ts`:
```typescript
import { Command } from 'commander';
import { listStages, getStage } from 'mcplocal/proxymodel/stage-registry';
import { join } from 'path';
const STAGES_DIR = join(process.env.HOME ?? '', '.mcpctl', 'stages');
const STAGE_DESCRIPTIONS: Record<string, string> = {
'passthrough': 'Returns content unchanged. No processing.',
'paginate': 'Splits large content into pages with navigation.',
'section-split': 'Splits content on structural boundaries (headers, JSON keys, etc.).',
'summarize-tree': 'Recursively summarizes sections with hierarchical navigation.',
};
export function registerDescribeStage(program: Command): void {
program
.command('describe stage <name>')
.description('Show detailed information about a stage')
.action(async (name: string) => {
const stages = listStages();
const stageInfo = stages.find(s => s.name === name);
if (!stageInfo) {
console.error(`Stage '${name}' not found`);
process.exit(1);
}
console.log(`Name: ${name}`);
console.log(`Source: ${stageInfo.source}`);
if (stageInfo.source === 'local') {
console.log(`Path: ${join(STAGES_DIR, name + '.ts')}`);
}
console.log(`Description: ${STAGE_DESCRIPTIONS[name] ?? 'Custom stage'}`);
console.log(`Requires LLM: ${requiresLlm(name) ? 'yes' : 'no'}`);
});
}
```
**Test Strategy:**
CLI test: `mcpctl describe stage passthrough` shows expected output. Test with custom stage, verify path is shown correctly.

View File

@@ -0,0 +1,73 @@
# Task ID: 93
**Title:** Implement mcpctl create stage Command
**Status:** cancelled
**Dependencies:** 71 ✓
**Priority:** medium
**Description:** Add CLI command to scaffold a new custom stage with boilerplate TypeScript code.
**Details:**
Create `src/cli/src/commands/create-stage.ts`:
```typescript
import { Command } from 'commander';
import { mkdir, writeFile, access } from 'fs/promises';
import { join } from 'path';
const STAGES_DIR = join(process.env.HOME ?? '', '.mcpctl', 'stages');
const STAGE_TEMPLATE = `import type { StageHandler } from 'mcpctl/proxymodel';
/**
* Custom stage: {{name}}
*
* Modify this handler to transform content as needed.
* Available in ctx:
* - ctx.llm.complete(prompt) - call the configured LLM
* - ctx.cache.getOrCompute(key, fn) - cache expensive computations
* - ctx.config - stage configuration from proxymodel YAML
* - ctx.originalContent - raw content before any stage processing
* - ctx.log - structured logging
*/
const handler: StageHandler = async (content, ctx) => {
// TODO: Implement your transformation
return { content };
};
export default handler;
`;
export function registerCreateStage(program: Command): void {
program
.command('create stage <name>')
.description('Create a new custom stage')
.action(async (name: string) => {
await mkdir(STAGES_DIR, { recursive: true });
const filePath = join(STAGES_DIR, `${name}.ts`);
try {
await access(filePath);
console.error(`Stage '${name}' already exists at ${filePath}`);
process.exit(1);
} catch {
// File doesn't exist, good
}
const code = STAGE_TEMPLATE.replace(/\{\{name\}\}/g, name);
await writeFile(filePath, code);
console.log(`Created ${filePath}`);
console.log('Edit the file to implement your stage logic.');
});
}
```
**Test Strategy:**
CLI test: `mcpctl create stage my-filter` creates file at expected path with correct template. Test error when stage already exists. Verify generated code compiles.

View File

@@ -0,0 +1,66 @@
# Task ID: 94
**Title:** Implement mcpctl create proxymodel Command
**Status:** cancelled
**Dependencies:** 78 ✓
**Priority:** medium
**Description:** Add CLI command to scaffold a new proxymodel YAML file with specified stages.
**Details:**
Create `src/cli/src/commands/create-proxymodel.ts`:
```typescript
import { Command } from 'commander';
import { mkdir, writeFile, access } from 'fs/promises';
import { join } from 'path';
import { stringify as yamlStringify } from 'yaml';
const PROXYMODELS_DIR = join(process.env.HOME ?? '', '.mcpctl', 'proxymodels');
export function registerCreateProxymodel(program: Command): void {
program
.command('create proxymodel <name>')
.description('Create a new proxymodel')
.option('--stages <stages>', 'Comma-separated list of stage names', 'passthrough')
.option('--controller <controller>', 'Session controller (gate or none)', 'gate')
.action(async (name: string, opts) => {
await mkdir(PROXYMODELS_DIR, { recursive: true });
const filePath = join(PROXYMODELS_DIR, `${name}.yaml`);
try {
await access(filePath);
console.error(`Proxymodel '${name}' already exists at ${filePath}`);
process.exit(1);
} catch {
// File doesn't exist, good
}
const stages = opts.stages.split(',').map((s: string) => ({ type: s.trim() }));
const model = {
kind: 'ProxyModel',
metadata: { name },
spec: {
controller: opts.controller,
stages,
appliesTo: ['prompts', 'toolResults'],
cacheable: true,
},
};
await writeFile(filePath, yamlStringify(model));
console.log(`Created ${filePath}`);
});
}
```
**Test Strategy:**
CLI test: `mcpctl create proxymodel my-pipeline --stages summarize,compress` creates valid YAML. Test default values. Verify generated YAML validates against schema.

View File

@@ -0,0 +1,70 @@
# Task ID: 95
**Title:** Implement mcpctl proxymodel validate Command
**Status:** cancelled
**Dependencies:** 78 ✓, 79 ✓
**Priority:** medium
**Description:** Add CLI command to validate a proxymodel definition, checking that all stages resolve and config is valid.
**Details:**
Create `src/cli/src/commands/proxymodel-validate.ts`:
```typescript
import { Command } from 'commander';
import { loadProxyModels } from 'mcplocal/proxymodel/loader';
import { getStage, loadCustomStages } from 'mcplocal/proxymodel/stage-registry';
export function registerProxymodelValidate(program: Command): void {
program
.command('proxymodel validate <name>')
.description('Validate a proxymodel definition')
.action(async (name: string) => {
await loadCustomStages();
const models = await loadProxyModels();
const model = models.get(name);
if (!model) {
console.error(`Proxymodel '${name}' not found`);
process.exit(1);
}
let valid = true;
const errors: string[] = [];
// Check all stages resolve
for (const stageConfig of model.spec.stages) {
const stage = getStage(stageConfig.type);
if (!stage) {
errors.push(`Stage '${stageConfig.type}' not found`);
valid = false;
}
}
// Check controller is valid
const validControllers = ['gate', 'none'];
if (model.spec.controller && !validControllers.includes(model.spec.controller)) {
errors.push(`Unknown controller '${model.spec.controller}'`);
valid = false;
}
if (valid) {
console.log(`✓ Proxymodel '${name}' is valid`);
} else {
console.error(`✗ Proxymodel '${name}' has errors:`);
for (const err of errors) {
console.error(` - ${err}`);
}
process.exit(1);
}
});
}
```
**Test Strategy:**
CLI test: valid proxymodel passes, proxymodel with unknown stage fails with clear error, proxymodel with unknown controller fails. Test with both built-in and custom stages.

View File

@@ -0,0 +1,53 @@
# Task ID: 96
**Title:** Implement mcpctl delete stage Command
**Status:** cancelled
**Dependencies:** 79 ✓
**Priority:** low
**Description:** Add CLI command to delete a custom stage file (cannot delete built-ins).
**Details:**
Create `src/cli/src/commands/delete-stage.ts`:
```typescript
import { Command } from 'commander';
import { unlink, access } from 'fs/promises';
import { join } from 'path';
import { listStages } from 'mcplocal/proxymodel/stage-registry';
const STAGES_DIR = join(process.env.HOME ?? '', '.mcpctl', 'stages');
export function registerDeleteStage(program: Command): void {
program
.command('delete stage <name>')
.description('Delete a custom stage')
.action(async (name: string) => {
const stages = listStages();
const stageInfo = stages.find(s => s.name === name);
if (!stageInfo) {
console.error(`Stage '${name}' not found`);
process.exit(1);
}
if (stageInfo.source === 'built-in') {
console.error(`Cannot delete built-in stage '${name}'`);
process.exit(1);
}
const filePath = join(STAGES_DIR, `${name}.ts`);
await unlink(filePath);
console.log(`Deleted ${filePath}`);
});
}
```
**Test Strategy:**
CLI test: can delete custom stage, cannot delete built-in stage (error message), deleting non-existent stage shows error.

View File

@@ -0,0 +1,64 @@
# Task ID: 97
**Title:** Implement mcpctl delete proxymodel Command
**Status:** cancelled
**Dependencies:** 78 ✓
**Priority:** low
**Description:** Add CLI command to delete a local proxymodel YAML file (cannot delete built-ins).
**Details:**
Create `src/cli/src/commands/delete-proxymodel.ts`:
```typescript
import { Command } from 'commander';
import { unlink, access } from 'fs/promises';
import { join } from 'path';
import { loadProxyModels } from 'mcplocal/proxymodel/loader';
import { getBuiltInProxyModels } from 'mcplocal/proxymodel/built-in-models';
const PROXYMODELS_DIR = join(process.env.HOME ?? '', '.mcpctl', 'proxymodels');
export function registerDeleteProxymodel(program: Command): void {
program
.command('delete proxymodel <name>')
.description('Delete a local proxymodel')
.action(async (name: string) => {
const models = await loadProxyModels();
const builtIns = getBuiltInProxyModels();
if (!models.has(name)) {
console.error(`Proxymodel '${name}' not found`);
process.exit(1);
}
const filePath = join(PROXYMODELS_DIR, `${name}.yaml`);
try {
await access(filePath);
} catch {
if (builtIns.has(name)) {
console.error(`Cannot delete built-in proxymodel '${name}'`);
} else {
console.error(`Proxymodel '${name}' file not found at ${filePath}`);
}
process.exit(1);
}
await unlink(filePath);
console.log(`Deleted ${filePath}`);
if (builtIns.has(name)) {
console.log(`Note: Built-in '${name}' will still be available`);
}
});
}
```
**Test Strategy:**
CLI test: can delete local proxymodel, cannot delete built-in (error message), deleting local override shows note about built-in fallback.

View File

@@ -0,0 +1,79 @@
# Task ID: 98
**Title:** Implement Persistent File Cache for Stages
**Status:** done
**Dependencies:** 73 ✓
**Priority:** high
**Description:** Extend CacheProvider with file-based persistence in ~/.mcpctl/cache/proxymodel/ for cross-session caching.
**Details:**
Create `src/mcplocal/src/proxymodel/file-cache.ts`:
```typescript
import { mkdir, readFile, writeFile, readdir, stat, unlink } from 'fs/promises';
import { join } from 'path';
import { createHash } from 'crypto';
import type { CacheProvider } from './types';
const CACHE_DIR = join(process.env.HOME ?? '', '.mcpctl', 'cache', 'proxymodel');
export class FileCacheProvider implements CacheProvider {
private memCache = new Map<string, string>();
private maxSizeBytes: number;
constructor(options: { maxSizeBytes?: number } = {}) {
this.maxSizeBytes = options.maxSizeBytes ?? 100 * 1024 * 1024; // 100MB default
}
hash(content: string): string {
return createHash('sha256').update(content).digest('hex').slice(0, 16);
}
private keyToPath(key: string): string {
const safeKey = key.replace(/[^a-zA-Z0-9-_]/g, '_');
return join(CACHE_DIR, safeKey);
}
async get(key: string): Promise<string | null> {
// Check memory first
if (this.memCache.has(key)) return this.memCache.get(key)!;
// Check file
try {
const content = await readFile(this.keyToPath(key), 'utf-8');
this.memCache.set(key, content); // Warm memory cache
return content;
} catch {
return null;
}
}
async set(key: string, value: string): Promise<void> {
await mkdir(CACHE_DIR, { recursive: true });
this.memCache.set(key, value);
await writeFile(this.keyToPath(key), value);
await this.enforceMaxSize();
}
async getOrCompute(key: string, compute: () => Promise<string>): Promise<string> {
const cached = await this.get(key);
if (cached !== null) return cached;
const value = await compute();
await this.set(key, value);
return value;
}
private async enforceMaxSize(): Promise<void> {
// LRU eviction based on file mtime when cache exceeds maxSizeBytes
}
}
```
**Test Strategy:**
Unit tests: file-based persistence survives process restart, memory cache is warmed on file read, LRU eviction works when size exceeded, concurrent access is safe. Integration test with real filesystem.

View File

@@ -0,0 +1,61 @@
# Task ID: 99
**Title:** Add Cache Key with Stage File Hash
**Status:** done
**Dependencies:** 98 ✓
**Priority:** medium
**Description:** Include the stage file hash in cache keys so cached artifacts are automatically invalidated when stage code changes.
**Details:**
Modify `src/mcplocal/src/proxymodel/executor.ts`:
```typescript
import { readFile, stat } from 'fs/promises';
import { createHash } from 'crypto';
import { join } from 'path';
const STAGES_DIR = join(process.env.HOME ?? '', '.mcpctl', 'stages');
const stageFileHashes: Map<string, string> = new Map();
async function getStageFileHash(stageName: string): Promise<string> {
// Check if custom stage file exists
const filePath = join(STAGES_DIR, `${stageName}.ts`);
try {
const content = await readFile(filePath, 'utf-8');
const hash = createHash('sha256').update(content).digest('hex').slice(0, 8);
stageFileHashes.set(stageName, hash);
return hash;
} catch {
// Built-in stage, use version-based hash or fixed value
return 'builtin-v1';
}
}
// In executePipeline, compute cache key:
const stageHash = await getStageFileHash(stageConfig.type);
const cacheKey = [
'stage',
stageConfig.type,
stageHash,
cache.hash(currentContent),
cache.hash(JSON.stringify(stageConfig.config ?? {})),
].join(':');
// Use pipeline-level cache wrapping:
if (proxyModel.spec.cacheable) {
const cached = await cache.get(cacheKey);
if (cached) {
currentContent = cached;
continue; // Skip stage execution
}
}
```
**Test Strategy:**
Unit tests: changing stage file content changes the hash, built-in stages have stable hash, cache miss when stage file changes, cache hit when stage file unchanged.

View File

@@ -0,0 +1,77 @@
# Task ID: 100
**Title:** Implement mcpctl cache list Command
**Status:** done
**Dependencies:** 98 ✓
**Priority:** low
**Description:** Add CLI command to list cached proxymodel artifacts with size and age information.
**Details:**
Create `src/cli/src/commands/cache-list.ts`:
```typescript
import { Command } from 'commander';
import { readdir, stat } from 'fs/promises';
import { join } from 'path';
import Table from 'cli-table3';
const CACHE_DIR = join(process.env.HOME ?? '', '.mcpctl', 'cache', 'proxymodel');
export function registerCacheList(program: Command): void {
program
.command('cache list')
.description('List cached proxymodel artifacts')
.option('--project <name>', 'Filter by project')
.action(async (opts) => {
try {
const files = await readdir(CACHE_DIR);
const table = new Table({
head: ['KEY', 'SIZE', 'AGE'],
});
let totalSize = 0;
for (const file of files) {
const filePath = join(CACHE_DIR, file);
const stats = await stat(filePath);
const age = formatAge(Date.now() - stats.mtimeMs);
const size = formatSize(stats.size);
totalSize += stats.size;
if (opts.project && !file.includes(opts.project)) continue;
table.push([file, size, age]);
}
console.log(table.toString());
console.log(`Total: ${formatSize(totalSize)}`);
} catch {
console.log('No cache entries found');
}
});
}
function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes}B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`;
return `${(bytes / 1024 / 1024).toFixed(1)}MB`;
}
function formatAge(ms: number): string {
const mins = Math.floor(ms / 60000);
if (mins < 60) return `${mins}m`;
const hours = Math.floor(mins / 60);
if (hours < 24) return `${hours}h`;
return `${Math.floor(hours / 24)}d`;
}
```
**Test Strategy:**
CLI test: list shows cache entries with correct format, --project filter works, empty cache shows appropriate message, size/age formatting is correct.

View File

@@ -0,0 +1,61 @@
# Task ID: 101
**Title:** Implement mcpctl cache clear Command
**Status:** done
**Dependencies:** 98 ✓
**Priority:** low
**Description:** Add CLI command to clear the proxymodel cache, optionally filtered by project.
**Details:**
Create `src/cli/src/commands/cache-clear.ts`:
```typescript
import { Command } from 'commander';
import { readdir, unlink, rmdir } from 'fs/promises';
import { join } from 'path';
const CACHE_DIR = join(process.env.HOME ?? '', '.mcpctl', 'cache', 'proxymodel');
export function registerCacheClear(program: Command): void {
program
.command('cache clear')
.description('Clear the proxymodel cache')
.option('--project <name>', 'Clear only cache for a specific project')
.option('--force', 'Skip confirmation', false)
.action(async (opts) => {
try {
const files = await readdir(CACHE_DIR);
const toDelete = opts.project
? files.filter(f => f.includes(opts.project))
: files;
if (toDelete.length === 0) {
console.log('No cache entries to clear');
return;
}
if (!opts.force) {
console.log(`This will delete ${toDelete.length} cache entries.`);
// Add confirmation prompt
}
for (const file of toDelete) {
await unlink(join(CACHE_DIR, file));
}
console.log(`Cleared ${toDelete.length} cache entries`);
} catch {
console.log('Cache directory does not exist');
}
});
}
```
**Test Strategy:**
CLI test: clears all entries without --project, clears filtered entries with --project, confirmation required without --force, --force skips confirmation.

View File

@@ -0,0 +1,60 @@
# Task ID: 102
**Title:** Implement mcpctl cache stats Command
**Status:** done
**Dependencies:** 98 ✓
**Priority:** low
**Description:** Add CLI command to show cache statistics including hit rates, total size, and entry counts.
**Details:**
Create `src/cli/src/commands/cache-stats.ts`:
```typescript
import { Command } from 'commander';
import { readdir, stat } from 'fs/promises';
import { join } from 'path';
const CACHE_DIR = join(process.env.HOME ?? '', '.mcpctl', 'cache', 'proxymodel');
export function registerCacheStats(program: Command): void {
program
.command('cache stats')
.description('Show cache statistics')
.action(async () => {
try {
const files = await readdir(CACHE_DIR);
let totalSize = 0;
let oldest = Date.now();
let newest = 0;
for (const file of files) {
const filePath = join(CACHE_DIR, file);
const stats = await stat(filePath);
totalSize += stats.size;
oldest = Math.min(oldest, stats.mtimeMs);
newest = Math.max(newest, stats.mtimeMs);
}
console.log(`Entries: ${files.length}`);
console.log(`Total size: ${formatSize(totalSize)}`);
console.log(`Oldest entry: ${formatAge(Date.now() - oldest)} ago`);
console.log(`Newest entry: ${formatAge(Date.now() - newest)} ago`);
// Note: hit rate tracking would require runtime instrumentation
console.log('\nNote: Hit rate statistics require runtime instrumentation.');
} catch {
console.log('No cache data available');
}
});
}
```
**Test Strategy:**
CLI test: shows correct stats for populated cache, handles empty cache gracefully, size formatting is correct.

View File

@@ -0,0 +1,47 @@
# Task ID: 103
**Title:** Add Shell Completions for ProxyModel Commands
**Status:** cancelled
**Dependencies:** 89 ✗, 90 ✗, 91 ✗, 92 ✗, 93 ✗, 94 ✗, 95 ✗, 96 ✗, 97 ✗, 100 ✓, 101 ✓, 102 ✓
**Priority:** low
**Description:** Extend shell completions to include all new proxymodel-related commands, resources, and flags.
**Details:**
Update `src/cli/src/completions.ts` to add completions for:
```typescript
// Resource types
const RESOURCE_TYPES = [...existing, 'proxymodels', 'stages'];
// Command completions
const COMMANDS = {
'get': ['proxymodels', 'stages', ...existing],
'describe': ['proxymodel', 'stage', ...existing],
'create': ['proxymodel', 'stage', ...existing],
'delete': ['proxymodel', 'stage', ...existing],
'proxymodel': ['validate'],
'cache': ['list', 'clear', 'stats'],
};
// Dynamic completions for proxymodel/stage names
async function completeProxymodelName(partial: string): Promise<string[]> {
const models = await loadProxyModels();
return [...models.keys()].filter(n => n.startsWith(partial));
}
async function completeStageName(partial: string): Promise<string[]> {
const stages = listStages();
return stages.map(s => s.name).filter(n => n.startsWith(partial));
}
```
Generate completion scripts for bash, zsh, and fish.
**Test Strategy:**
Manual test: completions work in bash/zsh/fish for all new commands. Test proxymodel name completion, stage name completion, subcommand completion.

View File

@@ -0,0 +1,67 @@
# Task ID: 104
**Title:** Extend Traffic Events for ProxyModel Processing
**Status:** cancelled
**Dependencies:** 80 ✓
**Priority:** medium
**Description:** Add new traffic event types for proxymodel processing: content_original, content_transformed, stage timing, cache hits/misses.
**Details:**
Modify `src/mcplocal/src/http/traffic.ts`:
```typescript
export type TrafficEventType =
| 'client_request'
| 'client_response'
| 'upstream_request'
| 'upstream_response'
| 'client_notification'
// New proxymodel events:
| 'content_original'
| 'content_transformed'
| 'stage_executed'
| 'stage_cache_hit'
| 'stage_cache_miss';
export interface ContentOriginalEvent {
eventType: 'content_original';
sessionId: string;
contentType: 'prompt' | 'toolResult';
sourceName: string;
content: string;
charCount: number;
}
export interface ContentTransformedEvent {
eventType: 'content_transformed';
sessionId: string;
contentType: 'prompt' | 'toolResult';
sourceName: string;
content: string;
charCount: number;
proxyModel: string;
stages: string[];
durationMs: number;
}
export interface StageExecutedEvent {
eventType: 'stage_executed';
sessionId: string;
stageName: string;
inputChars: number;
outputChars: number;
durationMs: number;
cacheHit: boolean;
}
```
Emit these events from the pipeline executor.
**Test Strategy:**
Unit tests: events emitted at correct points in pipeline execution, event payloads contain correct data, cache hit/miss events distinguish correctly. Integration test with inspector showing new events.

View File

@@ -0,0 +1,83 @@
# Task ID: 105
**Title:** Implement Model Studio TUI Base
**Status:** done
**Dependencies:** 104 ✗
**Priority:** medium
**Description:** Create the base TUI for mcpctl console --model-studio that extends --inspect with original vs transformed view.
**Details:**
Create `src/cli/src/commands/console/model-studio.tsx`:
```typescript
import React, { useState, useEffect } from 'react';
import { Box, Text, useInput } from 'ink';
import { TrafficEvent } from './types';
interface ModelStudioProps {
projectName: string;
events: TrafficEvent[];
}
export function ModelStudio({ projectName, events }: ModelStudioProps) {
const [selectedIdx, setSelectedIdx] = useState(0);
const [viewMode, setViewMode] = useState<'original' | 'transformed' | 'diff'>('transformed');
const [pauseMode, setPauseMode] = useState(false);
useInput((input, key) => {
if (input === 'j') setSelectedIdx(i => Math.min(i + 1, events.length - 1));
if (input === 'k') setSelectedIdx(i => Math.max(i - 1, 0));
if (input === 'o') setViewMode(m => m === 'original' ? 'transformed' : m === 'transformed' ? 'diff' : 'original');
if (input === 'p') setPauseMode(p => !p);
if (input === 'G') setSelectedIdx(events.length - 1);
});
const selected = events[selectedIdx];
const isContentEvent = selected?.eventType === 'content_original' || selected?.eventType === 'content_transformed';
return (
<Box flexDirection="column" height="100%">
<Box borderStyle="single" padding={1}>
<Text>Model Studio: {projectName}</Text>
<Text> | </Text>
<Text>View: {viewMode}</Text>
<Text> | </Text>
<Text color={pauseMode ? 'red' : 'green'}>{pauseMode ? '⏸ PAUSED' : '▶ LIVE'}</Text>
</Box>
<Box flexGrow={1} flexDirection="row">
{/* Event list sidebar */}
<Box width="30%" borderStyle="single">
{events.map((e, i) => (
<Text key={i} inverse={i === selectedIdx}>
{formatEventLine(e)}
</Text>
))}
</Box>
{/* Content view */}
<Box width="70%" borderStyle="single">
{isContentEvent && (
<ContentView event={selected} mode={viewMode} />
)}
</Box>
</Box>
<Box borderStyle="single">
<Text>[o] toggle view [p] pause [j/k] navigate [G] latest [q] quit</Text>
</Box>
</Box>
);
}
```
Add --model-studio flag to console command.
**Test Strategy:**
Manual test: TUI renders correctly, keyboard navigation works, original/transformed/diff views switch correctly, pause indicator shows correctly.

View File

@@ -0,0 +1,97 @@
# Task ID: 106
**Title:** Implement Pause Queue for Model Studio
**Status:** done
**Dependencies:** 105 ✓
**Priority:** medium
**Description:** Add a pause queue in mcplocal that holds outgoing responses when model studio pause mode is active.
**Details:**
Create `src/mcplocal/src/proxymodel/pause-queue.ts`:
```typescript
interface PausedResponse {
id: string;
sessionId: string;
contentType: 'prompt' | 'toolResult';
sourceName: string;
original: string;
transformed: string;
resolve: (content: string) => void;
timestamp: number;
}
class PauseQueue {
private paused = false;
private queue: PausedResponse[] = [];
private listeners = new Set<(items: PausedResponse[]) => void>();
setPaused(paused: boolean): void {
this.paused = paused;
if (!paused) {
// Release all paused items with their transformed content
for (const item of this.queue) {
item.resolve(item.transformed);
}
this.queue = [];
}
this.notifyListeners();
}
isPaused(): boolean {
return this.paused;
}
async enqueue(item: Omit<PausedResponse, 'resolve' | 'id' | 'timestamp'>): Promise<string> {
if (!this.paused) return item.transformed;
return new Promise(resolve => {
this.queue.push({
...item,
id: crypto.randomUUID(),
timestamp: Date.now(),
resolve,
});
this.notifyListeners();
});
}
editAndRelease(id: string, editedContent: string): void {
const idx = this.queue.findIndex(q => q.id === id);
if (idx >= 0) {
const item = this.queue.splice(idx, 1)[0];
item.resolve(editedContent);
this.notifyListeners();
}
}
releaseOne(id: string): void {
const idx = this.queue.findIndex(q => q.id === id);
if (idx >= 0) {
const item = this.queue.splice(idx, 1)[0];
item.resolve(item.transformed);
this.notifyListeners();
}
}
dropOne(id: string): void {
const idx = this.queue.findIndex(q => q.id === id);
if (idx >= 0) {
const item = this.queue.splice(idx, 1)[0];
item.resolve(''); // Empty response
this.notifyListeners();
}
}
}
export const pauseQueue = new PauseQueue();
```
**Test Strategy:**
Unit tests: enqueue returns immediately when not paused, enqueue blocks when paused, releaseOne/editAndRelease/dropOne work correctly, setPaused(false) releases all.

View File

@@ -0,0 +1,70 @@
# Task ID: 107
**Title:** Implement Edit Mode for Model Studio
**Status:** done
**Dependencies:** 106 ✓
**Priority:** medium
**Description:** Add inline editing capability to model studio for modifying paused responses before release.
**Details:**
Extend `src/cli/src/commands/console/model-studio.tsx`:
```typescript
import { spawn } from 'child_process';
import { writeFileSync, readFileSync, unlinkSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
async function editContent(original: string): Promise<string> {
const editor = process.env.EDITOR ?? 'vim';
const tmpFile = join(tmpdir(), `mcpctl-edit-${Date.now()}.txt`);
writeFileSync(tmpFile, original);
return new Promise((resolve, reject) => {
const proc = spawn(editor, [tmpFile], {
stdio: 'inherit',
});
proc.on('close', (code) => {
if (code === 0) {
const edited = readFileSync(tmpFile, 'utf-8');
unlinkSync(tmpFile);
resolve(edited);
} else {
unlinkSync(tmpFile);
reject(new Error(`Editor exited with code ${code}`));
}
});
});
}
// In the TUI component:
useInput(async (input, key) => {
if (input === 'e' && pauseMode && selectedPausedItem) {
const edited = await editContent(selectedPausedItem.transformed);
pauseQueue.editAndRelease(selectedPausedItem.id, edited);
// Emit correction event
trafficCapture.emit({
eventType: 'content_edited',
sessionId: selectedPausedItem.sessionId,
contentType: selectedPausedItem.contentType,
sourceName: selectedPausedItem.sourceName,
original: selectedPausedItem.original,
transformed: selectedPausedItem.transformed,
edited,
timestamp: Date.now(),
});
}
});
```
**Test Strategy:**
Integration test: pressing 'e' opens editor with content, saving and closing applies edit, edit event is emitted with correct before/after content.

View File

@@ -0,0 +1,75 @@
# Task ID: 108
**Title:** Implement Model Switch for Model Studio
**Status:** done
**Dependencies:** 105 ✓, 82 ✓
**Priority:** medium
**Description:** Add ability to switch the active proxymodel for a project mid-session from model studio.
**Details:**
Extend `src/cli/src/commands/console/model-studio.tsx`:
```typescript
function ModelPicker({ models, current, onSelect }: {
models: string[];
current: string;
onSelect: (name: string) => void;
}) {
const [selectedIdx, setSelectedIdx] = useState(models.indexOf(current));
useInput((input, key) => {
if (key.upArrow) setSelectedIdx(i => Math.max(0, i - 1));
if (key.downArrow) setSelectedIdx(i => Math.min(models.length - 1, i + 1));
if (key.return) onSelect(models[selectedIdx]);
});
return (
<Box flexDirection="column" borderStyle="single">
<Text bold>Select ProxyModel:</Text>
{models.map((m, i) => (
<Text key={m} inverse={i === selectedIdx}>
{m === current ? '✓ ' : ' '}{m}
</Text>
))}
</Box>
);
}
// Add to main component:
const [showModelPicker, setShowModelPicker] = useState(false);
const [activeModel, setActiveModel] = useState('default');
useInput((input) => {
if (input === 'm') setShowModelPicker(true);
});
async function switchModel(name: string) {
// Call mcplocal API to switch model
await fetch(`http://localhost:${port}/projects/${projectName}/proxymodel`, {
method: 'PUT',
body: JSON.stringify({ proxyModel: name }),
});
setActiveModel(name);
setShowModelPicker(false);
// Emit model_switched event
trafficCapture.emit({
eventType: 'model_switched',
projectName,
previousModel: activeModel,
newModel: name,
timestamp: Date.now(),
});
}
```
Add PUT endpoint to mcplocal for switching proxymodel.
**Test Strategy:**
Integration test: 'm' opens model picker, selecting a model updates the active model, subsequent content flows through new model, model_switched event is emitted.

View File

@@ -0,0 +1,94 @@
# Task ID: 109
**Title:** Implement Studio MCP Server Tools
**Status:** done
**Dependencies:** 104 ✗, 106 ✓, 107 ✓, 108 ✓
**Priority:** medium
**Description:** Create MCP tools for Claude Monitor to observe traffic, get corrections, switch models, and modify stages.
**Details:**
Extend `src/cli/src/commands/console/inspect-mcp.ts` with studio tools:
```typescript
const studioTools: Tool[] = [
{
name: 'get_content_diff',
description: 'Get original vs transformed vs edited content for a specific event',
inputSchema: {
type: 'object',
properties: {
eventId: { type: 'string', description: 'Event ID' },
},
required: ['eventId'],
},
},
{
name: 'get_corrections',
description: 'Get all user corrections (edits) in a session',
inputSchema: {
type: 'object',
properties: {
sessionId: { type: 'string', description: 'Optional session filter' },
},
},
},
{
name: 'get_active_model',
description: 'Get current proxymodel name and stage list for a project',
inputSchema: {
type: 'object',
properties: {
project: { type: 'string' },
},
required: ['project'],
},
},
{
name: 'switch_model',
description: 'Hot-swap the active proxymodel on a project',
inputSchema: {
type: 'object',
properties: {
project: { type: 'string' },
model: { type: 'string' },
},
required: ['project', 'model'],
},
},
{
name: 'reload_stages',
description: 'Force reload all stages from ~/.mcpctl/stages/',
inputSchema: { type: 'object', properties: {} },
},
{
name: 'get_stage_source',
description: 'Read the source code of a stage file',
inputSchema: {
type: 'object',
properties: {
name: { type: 'string' },
},
required: ['name'],
},
},
{
name: 'list_models',
description: 'List available proxymodels',
inputSchema: { type: 'object', properties: {} },
},
{
name: 'list_stages',
description: 'List available stages',
inputSchema: { type: 'object', properties: {} },
},
];
```
**Test Strategy:**
Integration test with MCP client: each tool returns expected data format, switch_model actually changes the model, reload_stages picks up file changes, get_corrections returns user edits.

View File

@@ -0,0 +1,62 @@
# Task ID: 110
**Title:** Implement RBAC for ProxyModels
**Status:** deferred
**Dependencies:** 87 ✗
**Priority:** low
**Description:** Add 'run' permission on proxymodels resource controlling which proxymodels users can activate on projects.
**Details:**
Update RBAC schema and enforcement:
1. Add to `src/db/prisma/schema.prisma`:
```prisma
// Extend existing RbacBinding or Permission model
enum RbacResource {
// ... existing
proxymodels
}
enum RbacPermission {
// ... existing
run // permission to use a proxymodel
cache // permission to push to shared cache
}
```
2. Add enforcement in `src/mcplocal/src/router.ts`:
```typescript
async function resolveProxyModel(
requestedModel: string,
projectName: string,
userId: string
): Promise<ProxyModelDefinition> {
const models = await loadProxyModels();
const model = models.get(requestedModel);
if (!model) {
console.warn(`Proxymodel '${requestedModel}' not found, using default`);
return models.get('default')!;
}
// Check RBAC permission
const hasPermission = await checkPermission(userId, 'run', 'proxymodels', requestedModel);
if (!hasPermission) {
console.warn(`User lacks 'run' permission for proxymodel '${requestedModel}', using default`);
return models.get('default')!;
}
return model;
}
```
3. 'default' proxymodel requires no permission (always allowed).
**Test Strategy:**
Integration test: user with 'run' permission can use proxymodel, user without permission falls back to default, 'default' always works, permission check logs reason for fallback.

View File

@@ -0,0 +1,76 @@
# Task ID: 111
**Title:** Write Integration Tests for subindex Model
**Status:** done
**Dependencies:** 75 ✓, 76 ✓, 82 ✓, 83 ✗
**Priority:** high
**Description:** Create comprehensive integration tests for the subindex proxymodel processing real content through section-split and summarize-tree.
**Details:**
Create `src/mcplocal/tests/proxymodel/subindex.test.ts`:
```typescript
import { describe, it, expect, beforeAll } from 'vitest';
import { executePipeline } from '../../src/proxymodel/executor';
import { loadProxyModels } from '../../src/proxymodel/loader';
import { createMockProviderRegistry } from '../mocks/providers';
describe('subindex proxymodel', () => {
let proxyModel;
let mockRegistry;
beforeAll(async () => {
const models = await loadProxyModels();
proxyModel = models.get('subindex');
mockRegistry = createMockProviderRegistry({
complete: async (prompt) => 'Mock summary of the content',
});
});
it('splits JSON array into sections', async () => {
const content = JSON.stringify([
{ id: 'flow1', label: 'Thermostat', nodes: [] },
{ id: 'flow2', label: 'Lighting', nodes: [] },
]);
const result = await executePipeline({
content,
contentType: 'toolResult',
sourceName: 'test/get_flows',
projectName: 'test',
sessionId: 'test-session',
proxyModel,
providerRegistry: mockRegistry,
});
expect(result.sections).toHaveLength(2);
expect(result.sections[0].id).toBe('flow1');
expect(result.content).toContain('2 sections');
});
it('provides drill-down to exact JSON content', async () => {
// Test that drilling into a section returns exact original JSON
});
it('uses structural summaries for JSON (no LLM call)', async () => {
// Verify LLM not called for JSON content
});
it('uses LLM summaries for prose content', async () => {
// Verify LLM called for markdown content
});
it('caches summaries across requests', async () => {
// Verify cache hit on second request with same content
});
});
```
**Test Strategy:**
Run with vitest, verify all test cases pass, check LLM call counts are as expected (structural vs prose), verify cache behavior.

View File

@@ -0,0 +1,47 @@
# Task ID: 112
**Title:** Write Documentation for ProxyModel Authoring
**Status:** done
**Dependencies:** 71 ✓, 78 ✓, 93 ✗, 94 ✗
**Priority:** low
**Description:** Create comprehensive documentation for users wanting to create custom stages and proxymodels.
**Details:**
Create documentation covering:
1. `docs/proxymodels/authoring-guide.md` - Complete guide from PRD's "Authoring Guide" section:
- Concepts: stages, proxymodels, framework
- File locations
- Step-by-step stage creation
- Step-by-step proxymodel creation
- Testing with mcpctl proxymodel validate
- Section drill-down
- Cache usage
- Error handling
- Full example
2. `docs/proxymodels/built-in-stages.md` - Reference for all built-in stages:
- passthrough
- paginate
- section-split
- summarize-tree
- Config options for each
3. `docs/proxymodels/api-reference.md` - Type reference:
- StageHandler
- StageContext
- StageResult
- Section
- LLMProvider
- CacheProvider
4. Update main README with proxymodels overview.
**Test Strategy:**
Review documentation for completeness, verify all code examples compile, test example stage from documentation works end-to-end.

View File

@@ -0,0 +1,31 @@
# Task ID: 113
**Title:** Write Documentation for Model Studio
**Status:** done
**Dependencies:** 105 ✓, 106 ✓, 107 ✓, 108 ✓, 109 ✓
**Priority:** low
**Description:** Create documentation for using model studio for live proxymodel development and debugging.
**Details:**
Create `docs/proxymodels/model-studio.md` covering:
1. Overview: Three-window setup (Claude Client, Model Studio, Claude Monitor)
2. Starting Model Studio: `mcpctl console --model-studio <project>`
3. Keyboard shortcuts reference
4. Viewing original vs transformed content
5. Pause mode: when and why to use it
6. Editing paused responses
7. Switching proxymodels mid-session
8. Using Claude Monitor to observe and modify stages
9. The correction workflow: edit → observe → adjust stage → verify
10. MCP tools available to Claude Monitor
11. Troubleshooting common issues
**Test Strategy:**
Review documentation for completeness, verify all described features work as documented.

View File

@@ -0,0 +1,74 @@
# Task ID: 114
**Title:** ProxyModel v2: Code-based MCP middleware plugin system
**Status:** done
**Dependencies:** None
**Priority:** high
**Description:** Redesign the ProxyModel framework from a YAML-configured content transformation pipeline into a full code-based MCP middleware plugin system. Proxy models become TypeScript files that can intercept any MCP request/response, create synthetic tools, maintain per-session state, and compose via multiple inheritance with compile-time conflict detection. The existing gate functionality (begin_session, tools/list filtering, prompt scoring, ungating) becomes the first proxy model implementation, proving the framework works by implementing gate entirely as a plugin with zero gate-specific code in router.ts.
**Details:**
## Vision
A proxy model is a TypeScript code file (not YAML) that acts as full MCP middleware. It can:
- Intercept any MCP request (initialize, tools/list, tools/call, resources/*, prompts/*)
- Modify any response before it reaches the client
- Create synthetic tools (e.g. begin_session doesn't exist upstream)
- Maintain per-session state (gated/ungated, accumulated tags, etc.)
- Access project resources (prompts, servers, config)
- Transform content (what stages do today: paginate, section-split, etc.)
## Key design decisions
1. Code not YAML: Proxy models live as .ts files in a known directory (e.g. ~/.mcpctl/proxymodels/). File exists = model exists. No create/delete via CLI.
2. Stages deprecated: No separate stage resource. Content transformation is just code inside the proxy model.
3. Multiple inheritance: A model can extend [gate, subindex] to compose behaviors from multiple parents. Conflicts (two parents intercepting the same method incompatibly) detected at load/compile time, not runtime.
4. Gate is just a proxy model: The ~300 lines of gate logic in router.ts move into a gate.ts proxy model file. Router becomes thin plumbing (~100 lines).
5. gated:true replaced by proxyModel field: Projects get a proxyModel: gate field. If the assigned model implements gating, the project is gated. No separate boolean.
6. Discoverable as resources: mcpctl get proxymodels lists available models (discovered from files). mcpctl describe proxymodel gate shows details. But no create/delete commands.
7. Attached to projects: mcpctl edit project foo --proxyModel gate or via apply YAML.
## Framework interface (sketch)
export interface ProxyModelContext {
session: SessionState;
project: ProjectConfig;
upstream: UpstreamClient;
llm?: LLMProvider;
cache?: CacheProvider;
}
export interface ProxyModel {
name: string;
extends?: string[];
onInitialize?(ctx, request): Promise<InitializeResult>;
onToolsList?(ctx): Promise<Tool[]>;
onToolCall?(ctx, name, args): Promise<ToolResult | null>;
onResourceRead?(ctx, uri): Promise<ResourceContent | null>;
transformContent?(ctx, content, contentType): Promise<string>;
createSessionState?(): Record<string, unknown>;
}
## Migration path
1. Define the ProxyModel TypeScript interface
2. Implement the plugin loader (discover .ts files, compile, validate inheritance, detect conflicts)
3. Implement the router integration (router delegates to loaded proxy model)
4. Extract gate logic from router.ts into gate.ts proxy model
5. Extract content pipeline (passthrough, paginate, section-split) into proxy model code
6. Add proxyModel field to Project schema (replaces gated boolean)
7. Add CLI: get proxymodels, describe proxymodel, edit project --proxyModel
8. Add smoke tests: gate proxy model produces identical behavior to current hardcoded gate
9. Deprecate gated field (backward compat: gated:true maps to proxyModel:gate)
## Supersedes
This task supersedes deferred tasks 83, 85-97, 98-99, 103, 104, 110, 111-112 which assumed the old YAML/stage architecture.
**Test Strategy:**
1. Gate proxy model smoke test: identical behavior to current hardcoded gate (begin_session, tools/list filtering, ungating). 2. Composition test: model extending [gate, paginate] inherits both behaviors. 3. Conflict detection test: two parents intercepting same hook differently = compile-time error. 4. Discovery test: drop a .ts file in proxymodels dir, mcpctl get proxymodels shows it. 5. Existing smoke tests (proxy-pipeline.test.ts) pass unchanged after migration.

View File

@@ -0,0 +1,29 @@
# Task ID: 115
**Title:** RBAC tasks namespace split and alias repoint
**Status:** pending
**Dependencies:** None
**Priority:** high
**Description:** Split the RBAC 'tasks' resource into 'tasks' (work items) and 'inference-tasks', update RBAC_RESOURCES and RESOURCE_ALIASES in rbac-definition.schema.ts
**Details:**
In src/mcpd/src/validation/rbac-definition.schema.ts:
1. Update RBAC_RESOURCES array to include: 'epics', 'epictasks', 'tasks', 'repos', 'sessions', 'inference-tasks'
2. Update RESOURCE_ALIASES:
- task → tasks (work items)
- epic → epics
- epictask → epictasks, et → epictasks
- repo → repos, repository → repos, repositories → repos
- session → sessions
- inference-task → inference-tasks
3. REMOVE the entry 'inference-tasks': 'tasks'
4. Export normalizeResource unchanged (it uses the alias map)
**Test Strategy:**
Unit tests in src/mcpd/tests/validation/: verify normalizeResource('task') returns 'tasks', normalizeResource('inference-task') returns 'inference-tasks', normalizeResource('et') returns 'epictasks'. Test that RBAC_RESOURCES includes all new entries.

View File

@@ -0,0 +1,26 @@
# Task ID: 116
**Title:** One-shot RBAC binding backfill migration
**Status:** pending
**Dependencies:** 115
**Priority:** high
**Description:** Implement guarded startup migration to rewrite existing 'tasks' bindings to 'inference-tasks' so pre-existing grants continue authorizing inference endpoints
**Details:**
In src/mcpd/src/main.ts or a new bootstrap module:
1. Read system settings from SYSTEM_SETTINGS_SECRET (see src/mcpd/src/bootstrap/system-settings.ts pattern)
2. Check for boolean key 'rbacTasksBackfillComplete'
3. If false/missing: iterate rbacRepo.findAll(), for each RbacDefinition row, rewrite roleBindings where resource === 'tasks' to resource: 'inference-tasks'
4. Use rbacRepo.update() to persist changes
5. Set 'rbacTasksBackfillComplete': true in system settings after completion
6. Wildcard bindings (resource: '*') are untouched
7. Model on migrateAdminRole pattern in main.ts
**Test Strategy:**
Unit test: create RbacDefinition with resource:'tasks', run backfill, verify resource changed to 'inference-tasks'. Test idempotency: run twice, verify no duplicate rewrites. Test wildcard unchanged.

View File

@@ -0,0 +1,25 @@
# Task ID: 117
**Title:** Add Epic, EpicTask, EpicTaskEdge Prisma models
**Status:** pending
**Dependencies:** None
**Priority:** high
**Description:** Add Epic, EpicTask, EpicTaskEdge models with enums EpicStatus and EpicTaskStatus to schema.prisma following PRD specifications
**Details:**
In src/db/prisma/schema.prisma add:
1. enum EpicStatus { open, delivered, delivered_with_tradeoffs, cancelled }
2. enum EpicTaskStatus { todo, in_progress, done, cancelled }
3. model Epic with: id, name, description, content (@db.Text), doneCriteria (Json), acceptedTradeoffs (Json), projectId, status, semver, currentRevisionId, ownerId, version, createdAt, updatedAt. Relations: project, owner (User), epicTasks[]. @@unique([name, projectId]), @@index([projectId]), @@index([name])
4. model EpicTask with: id, name, description, epicId, doneCriteria (Json), status, ownerId, version, createdAt, updatedAt. Relations: epic, owner, tasks[], dependsOn (EpicTaskEdge[]), blocks (EpicTaskEdge[]). @@unique([name, epicId]), @@index([epicId])
5. model EpicTaskEdge with: id, fromId, toId, createdAt. Relations using @relation names. @@unique([fromId, toId]), @@index([fromId]), @@index([toId])
6. Add reverse relations to User and Project models
**Test Strategy:**
Schema tests in src/db/tests/: create Epic, verify unique constraint on (name, projectId), test cascade delete from Project, test EpicTask creation with Epic reference, test EpicTaskEdge uniqueness.

View File

@@ -0,0 +1,25 @@
# Task ID: 118
**Title:** Add Task, TaskVerdict, TaskEvent Prisma models
**Status:** pending
**Dependencies:** 117
**Priority:** high
**Description:** Add Task, TaskVerdict, TaskEvent models with enums TaskState and TaskRisk to schema.prisma for work item tracking
**Details:**
In src/db/prisma/schema.prisma add:
1. enum TaskState { todo, in_progress, building, self_testing, qa_review, security_review, sre_review, done, awaiting_review, merged, reopened, cancelled }
2. enum TaskRisk { trivial, normal, high }
3. model Task with: id, name, title, description (@db.Text), epicTaskId, repoId?, assigneeId?, state, risk, branch?, headCommit?, prUrl?, prNumber?, reviewNote? (@db.Text), ownerId, version, createdAt, updatedAt. Relations: epicTask, repo (CodeRepo?), assignee (Agent?), owner, verdicts[], sessions[], events[]. @@unique([name, epicTaskId]), indexes on epicTaskId, state, assigneeId
4. model TaskVerdict with: id, taskId, stage (TaskState), agentId?, commit, passed, summary (@db.Text), findings (Json), createdAt. Relations: task, agent. @@index([taskId]), @@index([taskId, stage])
5. model TaskEvent with: id, taskId, fromState?, toState, actorType, actorId?, note? (@db.Text), createdAt. @@index([taskId, createdAt])
6. Add reverse relations to Agent model (assignedTasks[], verdicts[])
**Test Strategy:**
Schema tests: create Task linked to EpicTask, verify cascade on EpicTask delete, test TaskVerdict immutability pattern, test TaskEvent append-only by creating multiple events for same task.

View File

@@ -0,0 +1,25 @@
# Task ID: 119
**Title:** Add CodeRepo Prisma model
**Status:** pending
**Dependencies:** None
**Priority:** high
**Description:** Add CodeRepo model to schema.prisma for git repository management within projects
**Details:**
In src/db/prisma/schema.prisma add:
1. model CodeRepo with: id, name, projectId, url, defaultBranch (@default("main")), path (@default("")), provider (@default("gitea")), credSecretId?, credSecretKey?, ownerId, version, createdAt, updatedAt
2. Relations: project (Project), cred (Secret?), owner (User), tasks (Task[])
3. @@unique([name, projectId]), @@index([projectId])
4. Add repos CodeRepo[] relation to Project model
5. Add codeRepos CodeRepo[] relation to Secret model
6. Add ownedRepos CodeRepo[] relation to User model
**Test Strategy:**
Schema tests: create CodeRepo linked to Project, verify unique constraint on (name, projectId), test cascade delete from Project, test SetNull behavior when Secret is deleted.

View File

@@ -0,0 +1,26 @@
# Task ID: 120
**Title:** Add AgentProject junction model
**Status:** pending
**Dependencies:** None
**Priority:** high
**Description:** Add AgentProject many-to-many junction table linking Agents to Projects with role and optional LLM override
**Details:**
In src/db/prisma/schema.prisma add:
1. model AgentProject with: id, agentId, projectId, role (@default("developer")), llmId?, createdAt
2. Relations: agent (Agent), project (Project), llm (Llm?)
3. @@unique([agentId, projectId]), @@index([agentId]), @@index([projectId])
4. Add projectMemberships AgentProject[] to Agent model
5. Add agentMemberships AgentProject[] to Project model
6. Add agentProjects AgentProject[] to Llm model
7. Keep Agent.projectId for backward compatibility (one release)
**Test Strategy:**
Schema tests: create AgentProject junction, verify unique constraint prevents duplicate membership, test cascade on both Agent and Project delete, test SetNull on Llm delete.

View File

@@ -0,0 +1,24 @@
# Task ID: 121
**Title:** Add AgentSession Prisma model
**Status:** pending
**Dependencies:** 118, 120
**Priority:** high
**Description:** Add AgentSession model with SessionState enum for ephemeral per-session pod tracking
**Details:**
In src/db/prisma/schema.prisma add:
1. enum SessionState { pending, provisioning, running, ended, failed }
2. model AgentSession with: id, agentId, projectId, taskId?, containerId?, state, image, expiresAt, lastError? (@db.Text), ownerId, createdAt, updatedAt, endedAt?
3. Relations: agent (Agent), project (Project), task (Task?), owner (User)
4. @@index([state]), @@index([taskId]), @@index([agentId])
5. Add sessions AgentSession[] to Agent, Project, Task, User models
**Test Strategy:**
Schema tests: create AgentSession with all relations, test cascade on Agent/Project delete, test SetNull on Task delete, verify state enum transitions.

View File

@@ -0,0 +1,30 @@
# Task ID: 122
**Title:** Update clearAllTables test helper
**Status:** pending
**Dependencies:** 117, 118, 119, 120, 121
**Priority:** high
**Description:** Add deleteMany calls for all new models to clearAllTables in src/db/tests/helpers.ts
**Details:**
In src/db/tests/helpers.ts clearAllTables function:
1. Add deleteMany for TaskEvent (child first)
2. Add deleteMany for TaskVerdict
3. Add deleteMany for AgentSession
4. Add deleteMany for Task
5. Add deleteMany for EpicTaskEdge
6. Add deleteMany for EpicTask
7. Add deleteMany for Epic
8. Add deleteMany for CodeRepo
9. Add deleteMany for AgentProject
10. Order matters: delete children before parents to avoid FK violations
11. Follow existing pattern in the file
**Test Strategy:**
Run existing test suite to verify clearAllTables works correctly with new models. Add explicit test that creates rows in all new tables then calls clearAllTables and verifies tables are empty.

View File

@@ -0,0 +1,25 @@
# Task ID: 123
**Title:** Epic repository layer
**Status:** pending
**Dependencies:** 117
**Priority:** medium
**Description:** Create EpicRepository in src/mcpd/src/repositories/ following existing repository patterns
**Details:**
Create src/mcpd/src/repositories/epic.repository.ts:
1. Export EpicRepository class following agent.repository.ts pattern
2. Methods: create, findById, findByName, findByProjectId, findAll, update, delete
3. update must increment version (optimistic concurrency)
4. Include relations in queries where needed (epicTasks, project, owner)
5. Add type exports for CreateEpicData, UpdateEpicData
6. Register in repository index file
**Test Strategy:**
Unit tests in src/mcpd/tests/repositories/: test CRUD operations, test optimistic concurrency version increment, test findByProjectId filtering, test cascade behavior.

Some files were not shown because too many files have changed in this diff Show More