managarten/services/mana-ai/src/db
Till JS 0af50f0166 feat(mana-ai): agent-aware tick loop + snapshot projection (Phase 3)
Third phase of the Multi-Agent Workbench. The background mission
runner now respects the owning Agent: agent state gates whether
a mission runs, concurrency is capped per-agent, and server-produced
iterations carry the agent's identity as their Actor.

Data layer:
- db/migrate.ts: new mana_ai.agent_snapshots table (mirrors
  mission_snapshots) with indexes on (user_id, last_applied_at) and
  a partial index on active agents.
- db/agents-projection.ts: refreshAgentSnapshots (incremental LWW
  replay over sync_changes appId='ai' table='agents') +
  loadActiveAgents / loadAgent helpers. mergeRaw exported for tests.
- db/missions-projection.ts: ServerMission.agentId + projection
  reads the JSONB field (undefined for legacy missions).

Tick integration (cron/tick.ts):
- Refreshes both snapshot tables on every pass (parallel).
- Per-user in-tick agent cache (Map<userId, Map<agentId, Agent>>)
  so N missions for one user hit the DB once.
- Gate order: agent archived → skip silently; agent paused → skip;
  per-agent maxConcurrentMissions exhausted this tick → defer to next.
  All skip paths bump mana_ai_agent_decisions_total{decision}.
- Prompt injection: withAgentContext prepends an <agent_context>
  block to the system prompt with the agent's name + role, and
  plaintext systemPrompt + memory when available. Ciphertext
  (enc:1:… blobs) are skipped — server has no key by design. Mirrors
  the Mission Grant privacy stance: encrypted context belongs to the
  foreground runner.

Iteration writer (db/iteration-writer.ts):
- New optional `agent` + `iterationId` + `rationale` inputs.
- When agent is present, the sync_changes row is stamped with a
  makeAgentActor actor (principalId=agentId, displayName=agent.name)
  so the webapp timeline groups the write under the right agent.
- Falls back to an AI actor with LEGACY_AI_PRINCIPAL + 'Mana' when
  the mission has no owning agent; ultimate fallback to the
  mission-runner system actor when iterationId is also missing.

Metrics:
- mana_ai_agent_decisions_total{decision=ran|skipped-paused|
  skipped-archived|skipped-concurrency}. Missions without an agent
  don't produce this metric — plansWrittenBackTotal is the universal
  "did we run" counter.

Tests: 41/41 (was 35) including 6 new cases for the agent LWW merge.
mana-ai type-check clean. Webapp svelte-check: 0 errors (4 unrelated
warnings in a different module).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 20:46:57 +02:00
..
resolvers feat(mana-ai): encrypted resolver + tick uses Mission Grant to decrypt scoped inputs 2026-04-15 13:42:31 +02:00
agents-projection.test.ts feat(mana-ai): agent-aware tick loop + snapshot projection (Phase 3) 2026-04-15 20:46:57 +02:00
agents-projection.ts feat(mana-ai): agent-aware tick loop + snapshot projection (Phase 3) 2026-04-15 20:46:57 +02:00
audit-read.ts feat(ai): Mission Grant consent UI + Workbench audit tab 2026-04-15 13:53:11 +02:00
audit.ts feat(mana-ai): encrypted resolver + tick uses Mission Grant to decrypt scoped inputs 2026-04-15 13:42:31 +02:00
connection.ts feat(ai): close the loop — server write-back + webapp staging effect 2026-04-15 00:29:30 +02:00
iteration-writer.ts feat(mana-ai): agent-aware tick loop + snapshot projection (Phase 3) 2026-04-15 20:46:57 +02:00
migrate.ts feat(mana-ai): agent-aware tick loop + snapshot projection (Phase 3) 2026-04-15 20:46:57 +02:00
missions-projection.test.ts refactor(mana-ai): RLS-scope mission reads via per-user two-phase query 2026-04-15 01:06:17 +02:00
missions-projection.ts feat(mana-ai): agent-aware tick loop + snapshot projection (Phase 3) 2026-04-15 20:46:57 +02:00
snapshot-refresh.test.ts perf(mana-ai): materialize mission snapshots, drop per-tick full replay 2026-04-15 01:28:24 +02:00
snapshot-refresh.ts perf(mana-ai): materialize mission snapshots, drop per-tick full replay 2026-04-15 01:28:24 +02:00