Multiplayer agent harness for work.
Dismiss pending onboarding after three personal conversations (#1333)
Regan Bellcb4a4145581f
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.tsindex 2c820f7b0..8d6b40a88 100644--- a/src/core/orchestrator.ts+++ b/src/core/orchestrator.ts@@ -105,7 +105,7 @@ import { createMemoryMap } from "../persistence/durable-map.ts";import { collectBlob, createMemoryBlobTransferStore } from "../persistence/blob-transfer.ts";import { createSkillMaterializer, skillsIndex, SKILLS_DIR } from "../skills/materialize.ts";import {- detectOnboardingStatus,+ resolveOnboardingStatus,onboardingSkillVisible,isIdeasConversation,PROACTIVE_OPENER_PROMPT,@@ -1218,8 +1218,9 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {? "## Ideas conversation\nThe user chose to explore ideas in this conversation. Skip the onboarding skill and setup flow for this entire conversation, including follow-ups. Do not mark onboarding completed or dismissed in memory. Use available authorized company context and answer their request directly.": "";if (!onboardingBlock && useMemory && conversation.kind === "dm" && onboardingSkillVisible(visibleSkills)) {- const fullMemory = await deps.memory.read(memoryScopeId).catch(swallowAs("orchestrator: memory read", ""));- onboardingBlock = renderPendingOnboardingPrompt(detectOnboardingStatus(fullMemory));+ onboardingBlock = await resolveOnboardingStatus(deps.memory, deps.sessions, memoryScopeId)+ .then(renderPendingOnboardingPrompt)+ .catch(swallowAs("orchestrator: onboarding status", ""));}let type: SessionType = "channel";diff --git a/src/onboarding/onboarding.ts b/src/onboarding/onboarding.tsindex 787fff892..316261435 100644--- a/src/onboarding/onboarding.ts+++ b/src/onboarding/onboarding.ts@@ -1,5 +1,7 @@-import type { TurnRequest } from "../types.ts";+import type { ScopeId, TurnRequest } from "../types.ts";import type { SkillResolution } from "../skills/skill-store.ts";+import type { MemoryService } from "../memory/memory-service.ts";+import type { SessionStore } from "../sessions/session-store.ts";const ONBOARDING_SKILL_NAME = "onboarding";const ONBOARDING_VERSION = "v2";@@ -57,6 +59,26 @@ export function onboardingSkillVisible(skills: SkillResolution[]): boolean {return skills.some((r) => r.skill?.manifest.name === ONBOARDING_SKILL_NAME);}+export async function resolveOnboardingStatus(+ memory: MemoryService,+ sessions: SessionStore,+ scope: ScopeId,+): Promise<OnboardingStatus> {+ const status = detectOnboardingStatus(await memory.read(scope));+ if (status === "completed" || status === "dismissed") return status;+ if (!scope.startsWith("personal:") || (await sessions.countPersonalConversations(scope, 3)) < 3) return status;+ if (memory.readHead && memory.replaceIfRevision) {+ for (let attempt = 0; attempt < 3; attempt++) {+ const head = await memory.readHead(scope);+ const current = detectOnboardingStatus(head.content);+ if (current === "completed" || current === "dismissed") return current;+ const next = setOnboardingStatus(head.content, "dismissed", new Date().toISOString().slice(0, 10));+ if (await memory.replaceIfRevision(scope, next, head.revision, "system:onboarding")) break;+ }+ }+ return "dismissed";+}+export function renderPendingOnboardingPrompt(status: OnboardingStatus, version = ONBOARDING_VERSION): string {if (status === "completed" || status === "dismissed") return "";const marker =diff --git a/src/sessions/memory-session-store.ts b/src/sessions/memory-session-store.tsindex 8bcfbc45d..5b278671c 100644--- a/src/sessions/memory-session-store.ts+++ b/src/sessions/memory-session-store.ts@@ -646,6 +646,41 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStorereturn [...sessions.values()].some((s) => s.scopeId === scope);},+ async countPersonalConversations(scope, limit = 3) {+ const boundedLimit = Math.max(0, Math.floor(limit));+ if (!boundedLimit) return 0;+ let count = 0;+ for (const session of sessions.values()) {+ if (+ session.scopeId !== scope ||+ session.type !== "dm" ||+ session.parentSessionId ||+ sessionOrigin(session.threadRef) !== "conversation"+ )+ continue;+ const log = new Map((entries.get(session.id) ?? []).map((entry) => [entry.seq, entry]));+ for (const row of tape.get(session.id) ?? []) {+ const entry = transcriptEntryFromTape(row);+ if (entry) log.set(entry.seq, entry);+ }+ if (+ [...log.values()].some((entry) => {+ const payload = entry.payload as { hidden?: unknown; overheard?: unknown } | null;+ return (+ entry.seq > (session.forkBoundarySeq ?? -1) &&+ entry.type === "user" &&+ payload?.hidden !== true &&+ payload?.overheard !== true+ );+ })+ ) {+ count++;+ if (count >= boundedLimit) break;+ }+ }+ return count;+ },+async sessionsByThreadRefs(threadRefs) {const wanted = new Set(threadRefs);return [...sessions.values()]diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.tsindex 453b73285..637d73846 100644--- a/src/sessions/postgres-session-store.ts+++ b/src/sessions/postgres-session-store.ts@@ -1355,6 +1355,33 @@ export function createPostgresSessionStore(connectionString: string, opts: Storereturn Boolean(rows[0]?.present);},+ async countPersonalConversations(scope, limit = 3): Promise<number> {+ const boundedLimit = Math.max(0, Math.floor(limit));+ if (!boundedLimit) return 0;+ const rows = await q(+ `SELECT COUNT(*) AS n FROM (+ SELECT s.id FROM sessions s+ WHERE s.scope_id = $1 AND s.type = 'dm' AND s.parent_session_id IS NULL+ AND ${hasOrigin("s", "conversation")}+ AND EXISTS (+ SELECT 1 FROM (+ SELECT DISTINCT ON (seq) seq, type, payload FROM (+ SELECT seq, type, payload, 1 AS priority FROM session_transcript_entries WHERE session_id = s.id+ UNION ALL+ SELECT seq, type, payload, 0 AS priority FROM session_entries WHERE session_id = s.id+ ) sources ORDER BY seq, priority DESC+ ) e+ WHERE e.seq > COALESCE(s.fork_boundary_seq, -1) AND e.type = 'user'+ AND (safe_json(replace(e.payload, '\\u0000', ''))->'hidden')::text IS DISTINCT FROM 'true'+ AND (safe_json(replace(e.payload, '\\u0000', ''))->'overheard')::text IS DISTINCT FROM 'true'+ )+ LIMIT $2+ ) conversations`,+ [scope, boundedLimit],+ );+ return Number(rows[0]?.n ?? 0);+ },+async sessionsByThreadRefs(threadRefs): Promise<SessionRef[]> {if (threadRefs.length === 0) return [];const rows = await q("SELECT id, thread_ref, scope_id, type, title FROM sessions WHERE thread_ref = ANY($1)", [diff --git a/src/sessions/session-store.ts b/src/sessions/session-store.tsindex dcabc2da5..204f0e462 100644--- a/src/sessions/session-store.ts+++ b/src/sessions/session-store.ts@@ -759,6 +759,8 @@ export interface SessionStore {scopeHasSessions(scope: ScopeId): Promise<boolean>;+ countPersonalConversations(scope: ScopeId, limit?: number): Promise<number>;+sessionsByThreadRefs(threadRefs: readonly string[]): Promise<SessionRef[]>;distinctScopes(): Promise<DistinctScope[]>;diff --git a/test/onboarding.test.ts b/test/onboarding.test.tsindex 1f728292a..c0647ff25 100644--- a/test/onboarding.test.ts+++ b/test/onboarding.test.ts@@ -8,16 +8,22 @@ import { join } from "node:path";import { buildApp } from "../src/wiring.ts";import type { Config } from "../src/config.ts";import { scopeId, type TurnRequest } from "../src/types.ts";-import { detectOnboardingStatus, setOnboardingStatus, PROACTIVE_OPENER_PROMPT } from "../src/onboarding/onboarding.ts";+import {+ detectOnboardingStatus,+ resolveOnboardingStatus,+ setOnboardingStatus,+ PROACTIVE_OPENER_PROMPT,+} from "../src/onboarding/onboarding.ts";import { testConfig } from "./support/test-config.ts";const actor = { externalId: "U1" };const onboardingSkillDir = join(process.cwd(), "plugins/onboarding/skills");-function freshApp() {+function freshApp(overrides: Partial<Config> = {}) {const config: Config = testConfig({dataDir: mkdtempSync(join(tmpdir(), "ap-onboarding-")),pluginSkillDirs: [onboardingSkillDir],+ ...overrides,});return buildApp(config);}@@ -152,3 +158,110 @@ test("ideas web conversations bypass onboarding on every turn without completing} as TurnRequest);assert.match(slack.reply ?? "", /## Pending Onboarding/);});++for (const initialStatus of ["not_started", "pending"] as const) {+ test(`three personal chats durably dismiss ${initialStatus} onboarding`, async () => {+ const { app, skills, memory, sessions } = freshApp();+ await waitForOnboardingSkill(skills);+ const scope = scopeId("personal", "U1");+ await memory.replace(scope, setOnboardingStatus("## Notes\n\nKeep my preferences.\n", initialStatus, "2026-09-17"));+ const prompt = async () =>+ (+ await app.turn({+ surface: "test",+ actor,+ conversation: { kind: "dm", threadRef: "dm:U1:current" },+ text: "!sysprompt",+ } as TurnRequest)+ ).reply ?? "";+ for (let i = 0; i < 3; i++) {+ assert.equal(await resolveOnboardingStatus(memory, sessions, scope), initialStatus);+ const session = await sessions.getOrCreateByThread(`web:U1:past-${i}`, "dm", scope);+ const { lease } = await sessions.acquireLease(session.id);+ assert.ok(lease);+ await sessions.append(lease, { type: "user", payload: { text: "Help with my work" }, scopeLabel: scope });+ await sessions.releaseLease(lease);+ }+ assert.doesNotMatch(await prompt(), /## Pending Onboarding/);+ assert.equal(detectOnboardingStatus(await memory.read(scope)), "dismissed");+ assert.match(await memory.read(scope), /Keep my preferences/);+ const dismissed = await memory.read(scope);+ for (const session of await sessions.listByScope(scope)) await sessions.deleteSession(session.id);+ assert.doesNotMatch(await prompt(), /## Pending Onboarding/);+ assert.equal(await memory.read(scope), dismissed);+ });+}++test("automatic dismissal preserves concurrent completion and memory changes", async () => {+ const { memory, sessions } = freshApp();+ const scope = scopeId("personal", "U1");+ const countingSessions = {+ ...sessions,+ async countPersonalConversations() {+ await memory.replace(scope, "- Onboarding: completed v2 on 2026-09-17.\n- A concurrent preference.\n");+ return 3;+ },+ };+ await resolveOnboardingStatus(memory, countingSessions, scope);+ assert.equal(detectOnboardingStatus(await memory.read(scope)), "completed");+ assert.match(await memory.read(scope), /A concurrent preference/);+});++test("completed and dismissed onboarding do not recount or rewrite history", async () => {+ const { memory, sessions } = freshApp();+ const scope = scopeId("personal", "U1");+ const noCounting = {+ ...sessions,+ async countPersonalConversations() {+ throw new Error("unexpected count");+ },+ };+ for (const status of ["completed", "dismissed"] as const) {+ const content = setOnboardingStatus("Keep this.", status, "2026-09-17");+ await memory.replace(scope, content);+ assert.equal(await resolveOnboardingStatus(memory, noCounting, scope), status);+ assert.equal(await memory.read(scope), content);+ }+});++test("automatic dismissal writes the permanent notebook with scratch-promote memory", async () => {+ const { memory, sessions } = freshApp({ memoryStrategy: "scratch-promote" });+ const scope = scopeId("personal", "U1");+ let count = 3;+ const history = {+ ...sessions,+ async countPersonalConversations() {+ return count;+ },+ };+ await resolveOnboardingStatus(memory, history, scope);+ assert.equal(detectOnboardingStatus(await memory.read(scope)), "dismissed");+ count = 0;+ assert.equal(await resolveOnboardingStatus(memory, history, scope), "dismissed");+});++test("automatic dismissal retries a notebook revision conflict without losing new facts", async () => {+ const { memory, sessions } = freshApp();+ const scope = scopeId("personal", "U1");+ let attempts = 0;+ const racingMemory = {+ ...memory,+ async replaceIfRevision(s: typeof scope, body: string, revision: string) {+ if (attempts++ === 0) await memory.replace(s, "- A concurrently saved fact.\n");+ return memory.replaceIfRevision!(s, body, revision);+ },+ };+ await resolveOnboardingStatus(+ racingMemory,+ {+ ...sessions,+ async countPersonalConversations() {+ return 3;+ },+ },+ scope,+ );+ assert.equal(attempts, 2);+ assert.equal(detectOnboardingStatus(await memory.read(scope)), "dismissed");+ assert.match(await memory.read(scope), /A concurrently saved fact/);+});diff --git a/test/postgres-store.test.ts b/test/postgres-store.test.tsindex 281b9a3d9..4943d7228 100644--- a/test/postgres-store.test.ts+++ b/test/postgres-store.test.ts@@ -1,3 +1,4 @@+import { assertPersonalConversationParity } from "./support/personal-conversation-parity.ts";import "./run-availability.ts";import { migrateTranscriptPage } from "../scripts/lib/transcript-tape-migration.ts";import { test, before } from "node:test";@@ -2484,3 +2485,34 @@ test("pg child parentage and spawn metadata survive reopening", { skip }, asyncawait reopened.setParentSession(child.id, null);assert.equal((await reopened.get(child.id))?.parentSessionId, undefined);});++test("pg personal conversation counts exclude synthetic and inherited chats", { skip }, async () => {+ await assertPersonalConversationParity(createPostgresSessionStore(URL!), "pg-personal-count");+});++test("pg personal conversation counts tolerate legacy null characters", { skip }, async () => {+ const store = createPostgresSessionStore(URL!);+ const scope = scopeId("personal", "legacy-null-count");+ const session = await store.getOrCreateByThread("legacy-null-count", "dm", scope);+ const { lease } = await store.acquireLease(session.id);+ assert.ok(lease);+ await store.append(lease, { type: "user", payload: { text: "Hello" }, scopeLabel: scope });+ await store.releaseLease(lease);+ const pg = (await import("pg")).default;+ const raw = new pg.Pool({ connectionString: URL });+ try {+ await raw.query("DELETE FROM session_tape WHERE session_id = $1", [session.id]);+ await raw.query("UPDATE session_entries SET payload = $2 WHERE session_id = $1", [+ session.id,+ JSON.stringify({ text: "Hello\u0000", hidden: "true", overheard: "true" }),+ ]);+ assert.equal(await store.countPersonalConversations(scope), 1);+ await raw.query("UPDATE session_entries SET payload = $2 WHERE session_id = $1", [+ session.id,+ JSON.stringify({ text: "Hello\u0000", hidden: true }),+ ]);+ assert.equal(await store.countPersonalConversations(scope), 0);+ } finally {+ await raw.end();+ }+});diff --git a/test/session-store.test.ts b/test/session-store.test.tsindex 8aeb537b6..4bccd76e9 100644--- a/test/session-store.test.ts+++ b/test/session-store.test.ts@@ -1,3 +1,4 @@+import { assertPersonalConversationParity } from "./support/personal-conversation-parity.ts";import { test } from "node:test";import assert from "node:assert/strict";import { createMemorySessionStore } from "../src/sessions/memory-session-store.ts";@@ -1126,3 +1127,7 @@ test("renewLease keeps a live turn's lock fresh and refuses stale or supersededassert.ok(taken.lease, "an expired lease can still be taken over");assert.equal(await store.renewLease(lease!), false, "the superseded holder cannot renew the new lock");});++test("personal conversation counts exclude synthetic and inherited chats", async () => {+ await assertPersonalConversationParity(createMemorySessionStore(), "personal-count");+});diff --git a/test/support/personal-conversation-parity.ts b/test/support/personal-conversation-parity.tsnew file mode 100644index 000000000..dfa066d32--- /dev/null+++ b/test/support/personal-conversation-parity.ts@@ -0,0 +1,58 @@+import assert from "node:assert/strict";+import { scopeId, type SessionType } from "../../src/types.ts";+import { tapeTranscriptEntryRecord, type SessionStore } from "../../src/sessions/session-store.ts";++export async function assertPersonalConversationParity(store: SessionStore, prefix: string): Promise<void> {+ const scope = scopeId("personal", prefix);+ const create = async (+ name: string,+ payload?: Record<string, unknown>,+ type: SessionType = "dm",+ sessionScope = scope,+ threadRef = `${prefix}:${name}`,+ ) => {+ const session = await store.getOrCreateByThread(threadRef, type, sessionScope);+ if (payload) {+ const { lease } = await store.acquireLease(session.id);+ assert.ok(lease);+ await store.append(lease, { type: "user", payload, scopeLabel: sessionScope });+ await store.releaseLease(lease);+ }+ return session;+ };+ await create("empty");+ await create("proactive", { text: "Greet the user", hidden: true });+ await create("overheard", { text: "Ambient chatter", overheard: true });+ await create("channel", { text: "Hello" }, "channel");+ await create("other", { text: "Hello" }, "dm", scopeId("personal", `${prefix}-other`));+ for (const origin of ["cron", "webhook", "monitor"])+ await create(origin, { text: "Hello" }, "dm", scope, `agent:main:${origin}:${prefix}`);+ const child = await create("child", { text: "Delegated work" });+ await store.setParentSession(child.id, "parent");+ const fork = await create("fork", { text: "Copied user request" });+ await store.updateForkProvenance(fork.id, { forkedFrom: { sessionId: "source" }, forkBoundarySeq: 0 });+ assert.equal(await store.countPersonalConversations(scope), 0);+ const archived = await create("archived", { text: "Hello" });+ await store.addParticipant(archived.id, prefix);+ await store.updateParticipantView(archived.id, prefix, { archived: true });+ assert.equal(await store.countPersonalConversations(scope), 1);+ const { lease } = await store.acquireLease(fork.id);+ assert.ok(lease);+ await store.append(lease, { type: "user", payload: { text: "A new request" }, scopeLabel: scope });+ await store.append(lease, { type: "user", payload: { text: "Another turn" }, scopeLabel: scope });+ await store.releaseLease(lease);+ assert.equal(await store.countPersonalConversations(scope), 2);+ const canonical = await create("canonical", { text: "Synthetic", hidden: true });+ const canonicalLease = (await store.acquireLease(canonical.id)).lease;+ assert.ok(canonicalLease);+ const [original] = await store.getEntries(canonical.id);+ assert.ok(original);+ await store.appendTape(canonicalLease, tapeTranscriptEntryRecord({ ...original, payload: { text: "Real request" } }));+ await store.releaseLease(canonicalLease);+ assert.equal(await store.countPersonalConversations(scope), 3);+ await create("fourth", { text: "Hello\u0000", hidden: "true", overheard: "true" });+ assert.equal(await store.countPersonalConversations(scope), 3);+ assert.equal(await store.countPersonalConversations(scope, 4), 4);+ assert.equal(await store.countPersonalConversations(scope, 1), 1);+ assert.equal(await store.countPersonalConversations(scope, 0), 0);+}