Files
waggle-os/packages/team-server/tests/daemons/subconscious.test.ts
shumeiko.it c25efe6c44 refactor(server): move the team runtime into a postponed package (P1.1)
Team mode stays in the repository but is not built or shipped (ADR-0003).

- The team server (index.ts, config, db, daemons, plugins, proactive,
  routes, scheduler, services, ws, drizzle migrations) and its tests move
  from @waggle/server to packages/team-server (@waggle/team-server,
  private). It keeps its own copies of the logger and the agent-group
  executor, so it does not import @waggle/server. @clerk/fastify, bullmq,
  ioredis, postgres, drizzle-orm, drizzle-kit, cron-parser and
  @waggle/waggle-dance leave @waggle/server; the launcher drops the same
  team-only dependencies.
- build:packages no longer builds waggle-dance or the worker: nothing in
  the community edition imports them, and the worker's project reference
  would otherwise build the team server. The worker now imports from
  team-server; the team Dockerfile builds both explicitly.
- The sidecar no longer starts the team server when DATABASE_URL is set,
  and loses the team routes (/api/team/*, /api/teams), the WaggleDance
  routes and bridge (/api/waggle-dance/*), the Team server binding and
  egress guard, the WebSocket team client, the TeamSync cache and pushes,
  team audit forwarding, team workspace registration and the team
  capability-policy lookup in chat. The in-process room signal bus stays:
  agent groups, fleet runs and external tool runs use it.
  middleware/audit.ts was dead and is deleted.
- @waggle/core loses TeamSync and the teamServer part of WaggleConfig. A
  teamServer key in config.json is ignored and kept on save, as P0.1 does
  for the tier keys. Workspaces lose teamServerUrl and teamUserId; teamId
  and teamRole stay because workspaces from a shared data directory still
  carry them and the viewer write guards read them. POST /api/workspaces
  ignores team fields.
- The sidecar bundle no longer marks the team packages as external.

The agent still references the TeamSync type in orchestrator.ts and
pattern-write-back.ts; those files are being rewritten in P1.5 and drop
it there.

Logs UF-030 and UF-031 (team sync and team policy defects found while
removing the code).
2026-10-24 12:17:41 +02:00

131 lines
5.8 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { buildServer } from '../../src/index.js';
import { users, teams, teamMembers, agentJobs, agentAuditLog } from '../../src/db/schema.js';
import { sql } from 'drizzle-orm';
import { SubconsciousAgent } from '../../src/daemons/subconscious.js';
describe('Subconscious Agent (Task 3.19)', () => {
let server: Awaited<ReturnType<typeof buildServer>>;
let userId: string;
let teamId: string;
let subconscious: SubconsciousAgent;
beforeAll(async () => {
server = await buildServer();
// Clean up leftover test data
await server.db.execute(sql`DELETE FROM agent_audit_log WHERE user_id IN (SELECT id FROM users WHERE clerk_id LIKE 'subcon_%')`);
await server.db.execute(sql`DELETE FROM agent_jobs WHERE user_id IN (SELECT id FROM users WHERE clerk_id LIKE 'subcon_%')`);
await server.db.execute(sql`DELETE FROM team_members WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'subcontest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_requests WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'subcontest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_overrides WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'subcontest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_policies WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'subcontest-%')`);
await server.db.execute(sql`DELETE FROM teams WHERE slug LIKE 'subcontest-%'`);
await server.db.execute(sql`DELETE FROM users WHERE clerk_id LIKE 'subcon_%'`);
// Create test user
const [user] = await server.db.insert(users).values({
clerkId: 'subcon_user1',
displayName: 'Subconscious User',
email: 'subcon_user1@test.com',
}).returning();
userId = user.id;
// Create team
const [team] = await server.db.insert(teams).values({
name: 'Subconscious Test Team',
slug: 'subcontest-team',
ownerId: userId,
}).returning();
teamId = team.id;
await server.db.insert(teamMembers).values({
teamId,
userId,
role: 'owner',
});
subconscious = new SubconsciousAgent(server.db);
});
afterAll(async () => {
await server.db.execute(sql`DELETE FROM agent_audit_log WHERE user_id IN (SELECT id FROM users WHERE clerk_id LIKE 'subcon_%')`);
await server.db.execute(sql`DELETE FROM agent_jobs WHERE user_id IN (SELECT id FROM users WHERE clerk_id LIKE 'subcon_%')`);
await server.db.execute(sql`DELETE FROM team_members WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'subcontest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_requests WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'subcontest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_overrides WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'subcontest-%')`);
await server.db.execute(sql`DELETE FROM team_capability_policies WHERE team_id IN (SELECT id FROM teams WHERE slug LIKE 'subcontest-%')`);
await server.db.execute(sql`DELETE FROM teams WHERE slug LIKE 'subcontest-%'`);
await server.db.execute(sql`DELETE FROM users WHERE clerk_id LIKE 'subcon_%'`);
await server.close();
});
it('shouldReflect returns false when under threshold', async () => {
// No completed jobs yet
const result = await subconscious.shouldReflect(userId);
expect(result).toBe(false);
});
it('shouldReflect returns true after N completed jobs', async () => {
// Insert 10 completed jobs (SUBCONSCIOUS_INTERACTION_THRESHOLD = 10)
const jobValues = Array.from({ length: 10 }, (_, i) => ({
teamId,
userId,
jobType: 'chat',
status: 'completed',
input: { prompt: `test ${i}` },
completedAt: new Date(),
}));
await server.db.insert(agentJobs).values(jobValues);
const result = await subconscious.shouldReflect(userId);
expect(result).toBe(true);
});
it('reflect creates audit entry', async () => {
const { auditEntry, insights } = await subconscious.reflect(userId);
expect(auditEntry).toBeTruthy();
expect(auditEntry.agentName).toBe('subconscious');
expect(auditEntry.actionType).toBe('subconscious_reflection');
expect(auditEntry.description).toContain('Reflected on');
expect(Array.isArray(insights)).toBe(true);
});
it('detects repeated job types pattern', async () => {
// Insert 5 more jobs of same type to trigger pattern detection
const repeatedJobs = Array.from({ length: 5 }, (_, i) => ({
teamId,
userId,
jobType: 'task',
status: 'completed',
input: { prompt: `repeated ${i}` },
completedAt: new Date(),
}));
await server.db.insert(agentJobs).values(repeatedJobs);
const { insights } = await subconscious.reflect(userId);
// Should find a prompt_change insight for the repeated 'chat' type (10 jobs)
const promptInsight = insights.find(i => i.type === 'prompt_change');
expect(promptInsight).toBeTruthy();
expect(promptInsight!.description).toContain('executed');
expect(promptInsight!.description).toContain('times recently');
});
it('prompt change insight requires approval in audit entry', async () => {
// The previous reflect should have created an audit entry with requiresApproval
const entries = await server.db.select().from(agentAuditLog)
.where(sql`${agentAuditLog.userId} = ${userId} AND ${agentAuditLog.actionType} = 'subconscious_reflection'`);
// Find one that has insights with prompt_change
const withApproval = entries.find(e => {
const state = e.afterState as { insights?: Array<{ type: string }> } | null;
return state?.insights?.some((i) => i.type === 'prompt_change');
});
expect(withApproval).toBeTruthy();
expect(withApproval!.requiresApproval).toBe(true);
});
});