managarten/packages/shared-ai/src/actor.test.ts
Till JS 1771063df4 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>
2026-04-15 20:13:57 +02:00

133 lines
4 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
makeUserActor,
makeAgentActor,
makeSystemActor,
normalizeActor,
isUserActor,
isAiActor,
isSystemActor,
isFromMissionRunner,
SYSTEM_PROJECTION,
SYSTEM_MISSION_RUNNER,
LEGACY_USER_PRINCIPAL,
LEGACY_DISPLAY_NAME,
USER_ACTOR,
} from './actor';
describe('factories', () => {
it('makeUserActor carries userId + displayName', () => {
const a = makeUserActor('user-1', 'Till');
expect(a.kind).toBe('user');
expect(a.principalId).toBe('user-1');
expect(a.displayName).toBe('Till');
});
it('makeUserActor default displayName is "Du"', () => {
expect(makeUserActor('u').displayName).toBe('Du');
});
it('makeAgentActor carries mission context', () => {
const a = makeAgentActor({
agentId: 'agent-1',
displayName: 'Cashflow Watcher',
missionId: 'm1',
iterationId: 'it1',
rationale: 'weekly review',
});
expect(a.kind).toBe('ai');
expect(a.principalId).toBe('agent-1');
expect(a.displayName).toBe('Cashflow Watcher');
expect(a.missionId).toBe('m1');
expect(a.iterationId).toBe('it1');
expect(a.rationale).toBe('weekly review');
});
it('makeSystemActor picks a default displayName per source', () => {
expect(makeSystemActor(SYSTEM_PROJECTION).displayName).toBe('Projektion');
expect(makeSystemActor(SYSTEM_MISSION_RUNNER).displayName).toBe('Mission-Runner');
});
it('makeSystemActor accepts a displayName override', () => {
expect(makeSystemActor(SYSTEM_PROJECTION, 'Streak-Tracker').displayName).toBe('Streak-Tracker');
});
});
describe('predicates', () => {
const u = makeUserActor('u');
const a = makeAgentActor({
agentId: 'a1',
displayName: 'x',
missionId: 'm',
iterationId: 'i',
rationale: 'r',
});
const s = makeSystemActor(SYSTEM_PROJECTION);
const mr = makeSystemActor(SYSTEM_MISSION_RUNNER);
it('identifies kinds', () => {
expect(isUserActor(u)).toBe(true);
expect(isUserActor(a)).toBe(false);
expect(isAiActor(a)).toBe(true);
expect(isSystemActor(s)).toBe(true);
});
it('isFromMissionRunner only fires for mission-runner principalId', () => {
expect(isFromMissionRunner(mr)).toBe(true);
expect(isFromMissionRunner(s)).toBe(false);
});
it('predicates handle undefined', () => {
expect(isUserActor(undefined)).toBe(false);
expect(isAiActor(undefined)).toBe(false);
expect(isSystemActor(undefined)).toBe(false);
});
});
describe('normalizeActor', () => {
it('passes modern actors through unchanged', () => {
const a = makeUserActor('u', 'Till');
expect(normalizeActor(a)).toBe(a);
});
it('fills in principalId+displayName for legacy {kind:"user"} events', () => {
const legacy = { kind: 'user' };
const n = normalizeActor(legacy);
expect(n.kind).toBe('user');
expect(n.principalId).toBe(LEGACY_USER_PRINCIPAL);
expect(n.displayName).toBe('Du');
});
it('fills in AI fields for legacy {kind:"ai", missionId, ...} without identity', () => {
const legacy = { kind: 'ai', missionId: 'm1', iterationId: 'i1', rationale: 'r' };
const n = normalizeActor(legacy);
expect(n.kind).toBe('ai');
expect(n.principalId).toBe('legacy:ai-default');
expect(n.displayName).toBe(LEGACY_DISPLAY_NAME);
if (n.kind !== 'ai') throw new Error('narrowed to ai');
expect(n.missionId).toBe('m1');
});
it('maps old system.source field into new system:<source> principalId', () => {
const legacy = { kind: 'system', source: 'projection' };
const n = normalizeActor(legacy);
expect(n.kind).toBe('system');
expect(n.principalId).toBe('system:projection');
expect(n.displayName).toBe('Projektion');
});
it('handles completely garbage input', () => {
expect(normalizeActor(null).kind).toBe('user');
expect(normalizeActor('hello').kind).toBe('user');
expect(normalizeActor(undefined).principalId).toBe(LEGACY_USER_PRINCIPAL);
expect(normalizeActor({ kind: 'alien' }).kind).toBe('user');
});
});
describe('USER_ACTOR legacy export', () => {
it('uses legacy principal + "Du" display', () => {
expect(USER_ACTOR.kind).toBe('user');
expect(USER_ACTOR.principalId).toBe(LEGACY_USER_PRINCIPAL);
expect(USER_ACTOR.displayName).toBe('Du');
});
});