Gateway Module — `agent-tune/`
1. Purpose
The agent-tune module backs the Agent Self-Improvement (ASI) pipeline's Phase 5a monitoring stage. When an approved agent-definition change lands, a monitoring window is opened recording the pre-change baseline (quality, stall rate, affinity rate) and the commit SHA; the window is later closed as completed or rolled_back after post-change telemetry is compared. Its defining invariant is global serialisation: only ONE active monitoring window may exist across ALL agents at any time, so tune effects are never confounded by overlapping changes. Smallest of the seven shard modules (~267 lines).
2. File Inventory
| File | Lines | Responsibility |
|---|---|---|
repository.ts | 111 | CRUD + serialisation-constraint enforcement |
routes.ts | 88 | 3 endpoints; 409 on serialisation violation |
types.ts | 68 | Zod schemas + row/record interfaces + repository contract |
| Total | 267 |
No index.ts factory — the repository + router are wired directly in the main server/index.ts (createAgentTuneRepository + createAgentTuneRouter, mounted at index.ts:754).
3. Public API Surface
REST Endpoints (mount: index.ts:754 → /api/agent-tune)
| Method | Path | Auth | Body Schema | Response | Side Effects |
|---|---|---|---|---|---|
| GET | /api/agent-tune/monitoring | Bearer | — | MonitoringWindowRecord[] (raw array, not {data}) | Reads status='active' windows |
| POST | /api/agent-tune/monitoring | Bearer | CreateMonitoringWindowSchema | 201 MonitoringWindowRecord | INSERT; 409 if any active window exists (SERIALISATION_VIOLATION) |
| PUT | /api/agent-tune/monitoring/:id/close | Bearer | CloseMonitoringWindowSchema ({status}) | MonitoringWindowRecord | UPDATE status → completed/rolled_back; 404 if absent |
MCP Tools
None. This surface is driven by the agent-tune skill (ASI orchestration), not by an MCP tool.
4. Internal API
createAgentTuneRepository(db) → AgentTuneRepository { getActiveMonitoringWindows, createMonitoringWindow, closeMonitoringWindow }. createAgentTuneRouter(repo) → Express Router.
5. Background Services
None. The window expires_at field is set by the caller; there is no gateway-side expiry sweep — expiry enforcement is the ASI skill's responsibility (see §9).
6. Data Contracts
CreateMonitoringWindowSchema:agent_name,rule_id,change_date,pre_quality(number),pre_stall_rate(number),pre_affinity_rate(number),commit_sha,expires_at— all required.CloseMonitoringWindowSchema:status(completed|rolled_back).MonitoringStatus=active|completed|rolled_back. Serialisation constraint enforced increateMonitoringWindowviaSELECT COUNT(*) WHERE status='active'> 0 → throwSERIALISATION_VIOLATION(route maps to 409).
7. Dependencies
- Gateway modules:
shared/zod-error.js. - External:
better-sqlite3,express,zod. - Environment variables: none. Table
agent_tune_monitoring(created via boot DDL).
8. Test Coverage
| Layer | File | Notes |
|---|---|---|
| Repository | test/agent-tune-repository.test.ts | Serialisation constraint, close transitions |
| Routes | test/agent-tune-routes.test.ts | 201/409/404 paths, Zod validation |
9. Known Limitations
expires_atis advisory only. No gateway timer auto-closes an expired window; a monitoring window left open indefinitely will block every subsequentcreateMonitoringWindowwith a 409 until manually closed. Expiry enforcement lives entirely in the ASI/agent-tuneskill layer.- The serialisation check + insert are not wrapped in a single transaction — a race between two concurrent POSTs could theoretically admit two windows. In practice the ASI pipeline is single-threaded (one tune at a time by design), so the window for the race is negligible, but it is not structurally closed.
- GET returns a bare array rather than the
{data: ...}envelope used by most other gateway routes — a minor contract inconsistency for dashboard consumers.
10. Change History
| Date | Dispatch | Summary |
|---|---|---|
| 2026-07-04 | 2296 | Module spec authored (Phase 4 §5.2), smoke-clone-2, HEAD 5c9a304 |
| — | 813 | ASI Phase 5a monitoring window origin |
| — | 1009 | R-7 vault-drift reconciliation — restored to lisa-os main |