refactor(actor): identity-aware Actor for Multi-Agent Workbench (Phase 1)

Foundation for the Multi-Agent Workbench roadmap
(docs/plans/multi-agent-workbench.md). Every event, record, and
sync_changes row now carries a principal identity + cached display
name in addition to the three-kind discriminator.

Shape change (source of truth in @mana/shared-ai):
  Before: { kind: 'user' | 'ai' | 'system', ...kind-specific fields }
  After:  discriminated union on kind, with
            - common:  principalId, displayName
            - 'user':  principalId = userId
            - 'ai':    principalId = agentId + missionId/iterationId/rationale
            - 'system': principalId = one of SYSTEM_* sentinel strings
                        ('system:projection', 'system:mission-runner', etc.)

Key design calls (from the plan's Q&A):
- System sub-sources get distinct principalIds (not a shared 'system'
  bucket) — lets Workbench filter + revert distinguish projection
  writes from migration writes from server-iteration writes
- displayName cached on the record so renaming an agent doesn't
  rewrite history
- normalizeActor() compat shim fills principalId/displayName on
  legacy rows with 'legacy:*' sentinels so historical events never
  crash the timeline

New exports:
- BaseActor / UserActor / AiActor / SystemActor (narrowed types)
- makeUserActor, makeAgentActor, makeSystemActor (factories with
  typed return)
- SYSTEM_PROJECTION, SYSTEM_RULE, SYSTEM_MIGRATION, SYSTEM_STREAM,
  SYSTEM_MISSION_RUNNER (principalId constants)
- LEGACY_USER_PRINCIPAL, LEGACY_AI_PRINCIPAL, LEGACY_SYSTEM_PRINCIPAL
- isUserActor / isFromMissionRunner predicates

Webapp:
- data/events/actor.ts now re-exports from shared-ai, keeps runtime
  ambient-context (runAs, getCurrentActor) local
- bindDefaultUser(userId, displayName) lets the auth layer replace
  the legacy placeholder with the real logged-in user actor at login
- Mission runner + server-iteration-staging stamp LEGACY_AI_PRINCIPAL
  as the agentId placeholder — Phase 2 will thread the real agent
- Streaks projection uses makeSystemActor(SYSTEM_PROJECTION)
- All test fixtures migrated to factories

Service:
- mana-ai/db/iteration-writer.ts stamps makeSystemActor(
  SYSTEM_MISSION_RUNNER) instead of the old { kind:'system',
  source:'mission-runner' } shape. Phase 3 will switch this to an
  agent actor per mission.

Tests: 26 shared-ai + 21 webapp vitest + 35 mana-ai — all green.
svelte-check: 0 errors, 0 warnings.

No behavior change; purely a type + shape upgrade. Old sync_changes
rows parse via the normalizeActor compat shim at read time.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Till JS 2026-04-15 20:13:57 +02:00
parent f7b5c9b3a4
commit 1771063df4
13 changed files with 570 additions and 115 deletions

View file

@ -17,6 +17,7 @@
import type { Sql } from './connection';
import { withUser } from './connection';
import { makeSystemActor, SYSTEM_MISSION_RUNNER } from '@mana/shared-ai';
import type { AiPlanOutput, MissionIteration, PlanStep } from '@mana/shared-ai';
export interface AppendIterationInput {
@ -36,9 +37,13 @@ export interface AppendIterationInput {
}
/** Actor blob stamped on the sync_changes row. JSON string already
* we pass it as `json.RawMessage` equivalent through pgx. */
* we pass it as `json.RawMessage` equivalent through pgx. Uses the
* identity-aware Actor shape from @mana/shared-ai so the webapp's
* timeline can group + filter server-produced iterations alongside
* agent/user writes. Phase 2 will switch this to a per-agent actor
* when the mission carries `agentId`. */
function systemActorJson(): string {
return JSON.stringify({ kind: 'system', source: 'mission-runner' });
return JSON.stringify(makeSystemActor(SYSTEM_MISSION_RUNNER));
}
export async function appendServerIteration(sql: Sql, input: AppendIterationInput): Promise<void> {