Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Items E: cut session history over to the items table

Regan Bell08b1566e5434
diff --git a/AGENTS.md b/AGENTS.md
index 03189db56..77a6f1164 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -76,12 +76,17 @@ Two habits that keep task-focused changes from scarring the rest of the repo:
until the description's report covers every lens for the PR head: relevant lenses with
an Accept verdict, and the rest as `Not relevant` with the triage model as reviewer and
its one-line reason as the assessment.
-- **Disclose config-matrix and database-schema changes in the PR description.** Name
- added, removed, or changed settings, flags, modes, and providers, including changes to
- defaults, precedence, and supported combinations. Name changed tables, columns,
- indexes, and constraints, and describe migrations or backfills and any rollout or
- compatibility implications. Explain why the changes are needed. If either area is
- unchanged, say so explicitly; keep the disclosure proportional to the change.
+- **Disclose configuration changes in the PR description.** Name added, removed,
+ or changed settings, flags, modes, and providers, including defaults, precedence,
+ and supported combinations. Explain why; keep this proportional to the change.
+- **Keep humans in the loop on schema and data types.** Humans must see and review
+ changes to tables and persisted/API payload shapes. Surface them in chat and the
+ PR description as you make them: complete affected old and new shapes, including
+ keys, indexes, constraints, enum variants, and JSON fields, with representative
+ before/after rows. Label invented examples. Account for every field kept, moved,
+ transformed, or dropped; explain migration, backfill, rollout, and compatibility
+ effects. Distinguish approved design, implemented code, and live schema. A migration
+ diff alone is not enough. If configuration or schema is unchanged, say so explicitly.
- **Verify locally with the affected tests, not the whole suite.** Run the tests covering
what you changed plus typecheck and lint, then push and let CI be the full gate — CI
shards the suite across parallel runners, and reproducing that serially costs several
diff --git a/package.json b/package.json
index 7aaba0d2f..c946db626 100644
--- a/package.json
+++ b/package.json
@@ -48,7 +48,7 @@
"sandbox:local:build": "bash scripts/local-sandbox-build.sh",
"sandbox:porter:build": "docker build --platform linux/amd64 -t \"${PORTER_SANDBOX_IMAGE:?Set PORTER_SANDBOX_IMAGE to the registry tag to publish}\" porter && docker push \"$PORTER_SANDBOX_IMAGE\"",
"deploy:porter-runner-image": "docker build --platform linux/amd64 -t \"${PORTER_DEPLOY_RUNNER_IMAGE:?Set PORTER_DEPLOY_RUNNER_IMAGE to the registry tag to publish}\" -f deploy/porter-runner/Dockerfile . && docker push \"$PORTER_DEPLOY_RUNNER_IMAGE\"",
- "test:pg": "node scripts/run-pg-tests.mjs test/batch-paths.test.ts test/session-append-many.test.ts test/cron-queue.test.ts test/postgres-migration-startup.test.ts test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-tapeless-store.test.ts test/postgres-legacy-tape-steps.test.ts test/postgres-items-copy.test.ts test/semantic-memory-pg.test.ts test/semantic-memory-http.test.ts test/semantic-memory-large-pg.test.ts test/postgres-instance-registry.test.ts test/readiness.test.ts test/postgres-grant-store.test.ts test/postgres-acl-file-id-migration.test.ts test/postgres-admin-grants.test.ts test/postgres-teams.test.ts test/postgres-decisions.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-linked-identity-membership.test.ts test/postgres-map.test.ts test/postgres-background-ownership.test.ts test/postgres-resource-search.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-cron-run-as-migration.test.ts test/postgres-trigger-recipient-consent-migration.test.ts test/postgres-legacy-harness-migration.test.ts test/postgres-migration-adoption.test.ts test/postgres-migrations.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-app-page-views.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/loop-ingress-pg.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/slack-mirror-fidelity.test.ts test/run-retry-delay.test.ts test/run-priority.test.ts test/postgres-run-stream.test.ts test/migrate-main.test.ts",
+ "test:pg": "node scripts/run-pg-tests.mjs test/batch-paths.test.ts test/session-append-many.test.ts test/cron-queue.test.ts test/postgres-migration-startup.test.ts test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-tapeless-store.test.ts test/postgres-legacy-tape-steps.test.ts test/postgres-items-copy.test.ts test/semantic-memory-pg.test.ts test/semantic-memory-http.test.ts test/semantic-memory-large-pg.test.ts test/postgres-instance-registry.test.ts test/readiness.test.ts test/postgres-grant-store.test.ts test/postgres-acl-file-id-migration.test.ts test/postgres-admin-grants.test.ts test/postgres-teams.test.ts test/postgres-decisions.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-linked-identity-membership.test.ts test/postgres-map.test.ts test/postgres-background-ownership.test.ts test/postgres-resource-search.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-cron-run-as-migration.test.ts test/postgres-trigger-recipient-consent-migration.test.ts test/postgres-legacy-harness-migration.test.ts test/postgres-migration-adoption.test.ts test/postgres-migrations.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-app-page-views.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/loop-ingress-pg.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/slack-mirror-fidelity.test.ts test/run-retry-delay.test.ts test/run-priority.test.ts test/postgres-run-stream.test.ts test/postgres-items-migration.test.ts test/postgres-items-contract.test.ts test/pi-harness-replay-fidelity.test.ts test/migrate-main.test.ts",
"livetest": "node scripts/api-livetest.ts",
"bench:memory": "node --env-file-if-exists=.env scripts/memory-bench.ts",
"prepare": "node .husky/install.mjs",
diff --git a/plugins/admin/ui/transcript.ts b/plugins/admin/ui/transcript.ts
index b6c897822..60fc10cd6 100644
--- a/plugins/admin/ui/transcript.ts
+++ b/plugins/admin/ui/transcript.ts
@@ -73,7 +73,6 @@ export function prepare(data: Row, requests: Row[]) {
const units: Row[] = [];
for (let i = 0; i < entries.length; i++) {
const e = entries[i];
- if (e.type === "soul") continue;
if (e.type === "tool_call" || e.type === "tool_result") {
if (e.type === "tool_result" && pairedSeqs.has(e.seq)) continue;
let paired: Row | undefined;
@@ -539,7 +538,7 @@ export async function show(sessionId: string, limit: number | undefined, expand:
? s.deliveryFileBadges(e.payload?.files || [])
: nothing;
return html`<div
- class=${classMap({ entry: true, noise, collapsed: noise && !open.has(unit), "thinking-entry": e.type === "thinking", "message-entry": e.type === "user" || e.type === "assistant", "user-entry": e.type === "user", "assistant-entry": e.type === "assistant", "cron-prompt-entry": !!isOrigin, "system-entry": e.type === "soul" || e.type === "system", filtered: s.entryHidden([e.type], false) })}
+ class=${classMap({ entry: true, noise, collapsed: noise && !open.has(unit), "thinking-entry": e.type === "thinking", "message-entry": e.type === "user" || e.type === "assistant", "user-entry": e.type === "user", "assistant-entry": e.type === "assistant", "cron-prompt-entry": !!isOrigin, "system-entry": e.type === "system", filtered: s.entryHidden([e.type], false) })}
>
<div class="who">
${unit.llmReqs?.length ? contextButton(unit.llmReqs[0].turnSeq, unit.llmReqs.length) : nothing}${noise ? html`<button type="button" class="disclosure" aria-expanded=${String(open.has(unit))} aria-label=${open.has(unit) ? "Collapse entry" : "Expand entry"} title=${open.has(unit) ? "Collapse entry" : "Expand entry"} @click=${toggle}>${open.has(unit) ? "▾" : "▸"}</button>` : nothing}<span
diff --git a/plugins/web-ui/src/core-bridge.ts b/plugins/web-ui/src/core-bridge.ts
index c28b8d352..c61db3885 100644
--- a/plugins/web-ui/src/core-bridge.ts
+++ b/plugins/web-ui/src/core-bridge.ts
@@ -409,7 +409,6 @@ export interface SessionEntry {
| "text"
| "tool_call"
| "tool_result"
- | "soul"
| "system"
| "approval_request"
| "approval_resolved"
@@ -2197,6 +2196,7 @@ export function entriesToMessages(entries: SessionEntry[], model?: Model<Api>):
const payload = e.payload as {
text?: string;
display?: string;
+ tool?: string;
callId?: string;
attachments?: Array<{ name?: string; mimetype?: string; sizeBytes?: number; artifactId?: string }>;
files?: Array<{ name?: string; mimetype?: string; sizeBytes?: number; artifactId?: string }>;
diff --git a/plugins/web-ui/test/activity-presentation.test.ts b/plugins/web-ui/test/activity-presentation.test.ts
index 01a4dc647..8124819c3 100644
--- a/plugins/web-ui/test/activity-presentation.test.ts
+++ b/plugins/web-ui/test/activity-presentation.test.ts
@@ -187,7 +187,7 @@ test("ordinary command exits stay neutral while tool errors retain failure label
test("historical completed nonzero exits do not inherit the old error flag", () => {
for (const identity of [{ tool: "execute" }, { tool: "sandbox", action: "exec" }]) {
- for (const output of [{ result: "[exit 1]" }, { result: "[NOT security-screened]\n[exit 1]" }]) {
+ for (const output of [{ result: "[exit 1]" }, { unscreened: true, result: "[NOT security-screened]\n[exit 1]" }]) {
const tool = row("[ -d dir ]", { ...identity, ...output, code: 1, timedOut: false, isError: true });
tool.call!.payload = { ...identity, command: "[ -d dir ]", purpose: "Check directory" };
assert.equal(activityLabel(tool, "complete"), "Check directory · exit 1");
diff --git a/plugins/web-ui/test/history.test.ts b/plugins/web-ui/test/history.test.ts
index 442b37294..3d83f3e5f 100644
--- a/plugins/web-ui/test/history.test.ts
+++ b/plugins/web-ui/test/history.test.ts
@@ -492,26 +492,41 @@ test("a hidden proactive-opener user entry never renders, but its assistant gree
assert.equal((msgs[0] as { role?: string }).role, "assistant");
});
-test("a materialized delivery entry (cron reply written into the thread) renders as an assistant reply", () => {
+test("a delivered cron reply renders as an assistant reply", () => {
const entries: SessionEntry[] = [
{ type: "user", payload: { text: "remind me later" }, createdAt: 100 },
{ type: "assistant", payload: { text: "will do" }, createdAt: 110 },
{
type: "assistant",
- payload: {
- text: "Reminder: standup in 10 minutes",
- deliveryKey: "agent:main:cron:c1:100",
- via: "cron",
- },
+ payload: { text: "Reminder: standup in 10 minutes", via: "cron" },
createdAt: 200,
seq: 2,
},
];
const msgs = entriesToMessages(entries, MODEL);
assert.equal(msgs.length, 3, "user + reply + delivered reminder");
- const delivered = msgs[2] as { role?: string; content?: Array<{ text?: string }> };
+ const delivered = msgs[2] as AssistantWork & { role?: string; content?: Array<{ text?: string }> };
assert.equal(delivered.role, "assistant");
assert.equal(delivered.content?.[0]?.text, "Reminder: standup in 10 minutes");
+ assert.equal(delivered.work, undefined, "a delivery is a reply, not tool activity");
+});
+
+test("a migrated reply and delivered-file entry render text and file chips", () => {
+ const entries: SessionEntry[] = [
+ { type: "user", payload: { text: "make gifs" }, createdAt: 100 },
+ { type: "assistant", payload: { text: "rsi.gif" }, createdAt: 120, seq: 1 },
+ {
+ type: "delivery",
+ payload: { files: [{ name: "rsi.gif", mimetype: "image/gif", sizeBytes: 42, artifactId: "art-1" }] },
+ createdAt: 121,
+ seq: 2,
+ },
+ ];
+ const msgs = entriesToMessages(entries, MODEL);
+ assert.equal(msgs.length, 2);
+ assert.deepEqual((msgs[1] as AssistantWork).deliveredFiles, [
+ { name: "rsi.gif", mimetype: "image/gif", sizeBytes: 42, artifactId: "art-1" },
+ ]);
});
test("a durable turn_failure entry renders like the live inline error (survives reload)", () => {
@@ -676,26 +691,6 @@ test("a legacy user entry without artifactId keeps a metadata-only attachment (c
assert.equal(att!.artifactId, undefined, "no fabricated artifact link");
});
-test("delivery entries attach openable files to the preceding assistant message", () => {
- const entries: SessionEntry[] = [
- { type: "user", payload: { text: "make gifs" }, createdAt: 100 },
- { type: "assistant", payload: { text: "Here are the gifs." }, createdAt: 120 },
- {
- type: "delivery",
- payload: {
- text: "rsi.gif",
- files: [{ name: "rsi.gif", mimetype: "image/gif", sizeBytes: 42, artifactId: "art-1" }],
- },
- createdAt: 121,
- },
- ];
- const msgs = entriesToMessages(entries, MODEL);
- assert.equal(msgs.length, 2);
- assert.deepEqual((msgs[1] as AssistantWork).deliveredFiles, [
- { name: "rsi.gif", mimetype: "image/gif", sizeBytes: 42, artifactId: "art-1" },
- ]);
-});
-
test("an attach tool result attaches openable files to the turn's assistant message", () => {
const entries: SessionEntry[] = [
{ type: "user", payload: { text: "make a report" }, createdAt: 100 },
@@ -761,27 +756,6 @@ test("a failed attach tool result attaches no files", () => {
assert.equal((msgs[1] as AssistantWork).deliveredFiles, undefined);
});
-test("delivery-only turns still rebuild an assistant message for the file chips", () => {
- const entries: SessionEntry[] = [
- { type: "user", payload: { text: "make gif only" }, createdAt: 100 },
- {
- type: "delivery",
- payload: {
- text: "rsi.gif",
- files: [{ name: "rsi.gif", mimetype: "image/gif", sizeBytes: 42, artifactId: "art-1" }],
- },
- createdAt: 121,
- },
- ];
- const msgs = entriesToMessages(entries, MODEL);
- assert.equal(msgs.length, 2);
- assert.equal((msgs[1] as { role?: string }).role, "assistant");
- assert.equal(((msgs[1] as AssistantWork).content[0] as { text?: string }).text, "");
- assert.deepEqual((msgs[1] as AssistantWork).deliveredFiles, [
- { name: "rsi.gif", mimetype: "image/gif", sizeBytes: 42, artifactId: "art-1" },
- ]);
-});
-
test("tool entries from a prior turn don't leak into the next turn's assistant reply", () => {
const entries: SessionEntry[] = [
{ type: "user", payload: { text: "one" }, createdAt: 100 },
diff --git a/scripts/mine-slack-scenarios.ts b/scripts/mine-slack-scenarios.ts
index beb9bf2ab..64e97c448 100644
--- a/scripts/mine-slack-scenarios.ts
+++ b/scripts/mine-slack-scenarios.ts
@@ -65,7 +65,11 @@ async function main(): Promise<void> {
const mined: Mined[] = [];
for (const s of sessions) {
const { rows: entries } = await client.query<Entry>(
- `SELECT seq, type, payload, created_at FROM session_entries WHERE session_id = $1 ORDER BY seq`,
+ `SELECT seq, CASE WHEN kind IN ('UserMessage', 'SteeringMessage') THEN 'user'
+ WHEN kind = 'AgentMessage' AND body ->> 'variant' IS NULL THEN 'assistant'
+ ELSE kind END AS type,
+ body::text AS payload, (EXTRACT(EPOCH FROM created_at) * 1000)::bigint AS created_at
+ FROM items WHERE session_id = $1 ORDER BY seq`,
[s.id],
);
const transcript: Array<{ author: string; text: string }> = [];
diff --git a/scripts/ops/items-copy.ts b/scripts/ops/items-copy.ts
index 8ab4033a0..8df114067 100644
--- a/scripts/ops/items-copy.ts
+++ b/scripts/ops/items-copy.ts
@@ -26,16 +26,13 @@ export class ItemsCopyOperator {
throw new Error("batch must be between 1 and 10000");
}
- async transaction<T>(
- run: () => Promise<T>,
- migrationLock: "exclusive" | "shared" | "none" = "exclusive",
- ): Promise<T> {
+ async transaction<T>(run: () => Promise<T>, shared = false): Promise<T> {
await this.client.query(
"BEGIN; SET LOCAL lock_timeout='2s'; SET LOCAL statement_timeout='120s'; SET LOCAL search_path=public",
);
try {
- const lock = `SELECT pg_try_advisory_xact_lock${migrationLock === "shared" ? "_shared" : ""}(hashtext('qm:schema-migrations')) AS acquired`;
- for (let attempt = 0; migrationLock !== "none" && !(await this.client.query(lock)).rows[0].acquired; attempt++) {
+ const lock = `SELECT pg_try_advisory_xact_lock${shared ? "_shared" : ""}(hashtext('qm:schema-migrations')) AS acquired`;
+ for (let attempt = 0; !(await this.client.query(lock)).rows[0].acquired; attempt++) {
if (attempt >= 300) throw new Error("schema migration is running; retry after it completes");
await sleep(1000);
}
@@ -225,7 +222,7 @@ export class ItemsCopyOperator {
[slice, last.session_id, last.seq, last.n, copied.rows],
);
return last.n as number;
- }, "shared");
+ }, true);
}
async completeCopy() {
@@ -285,11 +282,19 @@ export class ItemsCopyOperator {
async indexes() {
await this.transaction(async () => {
await this.assertState(["catchup", "verified"]);
- }, "none");
- await this.client.query(
- "SET search_path=public; SET statement_timeout=0; SET lock_timeout='5s'; SET maintenance_work_mem='1GB'",
- );
+ });
+ let acquired = false;
try {
+ for (let attempt = 0; !acquired; attempt++) {
+ acquired = (
+ await this.client.query("SELECT pg_try_advisory_lock_shared(hashtext('qm:schema-migrations')) AS ok")
+ ).rows[0].ok;
+ if (!acquired) {
+ if (attempt >= 300) throw new Error("schema migration is running; retry after it completes");
+ await sleep(1000);
+ }
+ }
+ await this.client.query("SET search_path=public; SET statement_timeout=0; SET lock_timeout='5s'");
for (const index of ITEM_INDEXES) {
const {
rows: [existing],
@@ -299,11 +304,11 @@ export class ItemsCopyOperator {
);
if (existing && !existing.ours) throw new Error(`index ${index.name} belongs to another table`);
if (existing?.indisvalid) continue;
- if (existing) await this.client.query(`DROP INDEX public.${index.name}`);
- await this.client.query(itemIndexSql(index));
+ if (existing) await this.client.query(`DROP INDEX CONCURRENTLY public.${index.name}`);
+ await this.client.query(itemIndexSql(index).replace("INDEX IF NOT EXISTS", "INDEX CONCURRENTLY IF NOT EXISTS"));
}
} finally {
- await this.client.query("RESET maintenance_work_mem");
+ if (acquired) await this.client.query("SELECT pg_advisory_unlock_shared(hashtext('qm:schema-migrations'))");
}
}
diff --git a/src/api/app-sessions.ts b/src/api/app-sessions.ts
index 717e3188e..f53636265 100644
--- a/src/api/app-sessions.ts
+++ b/src/api/app-sessions.ts
@@ -1,5 +1,4 @@
import { composeSoul } from "../resolution/resolution-service.ts";
-import { principalEntitledToScope } from "../resolution/context-filter.ts";
import { createCurrentScopeMembers } from "../resolution/scope-membership.ts";
import { notifyDeploymentShared } from "../deploy/share-notice.ts";
import { deploymentShareScope } from "../deploy/email-access.ts";
@@ -168,7 +167,7 @@ export function createSessionMethods(
const storedEntryAt = async (sessionId: string, seq: number) => {
const entry = await deps.sessions.getEntry(sessionId, seq);
- return entry && entry.type !== "soul" ? entry : undefined;
+ return entry;
};
const viewerStoredEntryAt = async (sessionId: string, principalId: string, seq: number) => {
@@ -554,19 +553,9 @@ export function createSessionMethods(
),
),
);
- const teamIds = [...new Set(hits.map((hit) => hit.entryScopeLabel))]
- .map(parseScopeId)
- .filter((label) => label.kind === "team")
- .map((label) => label.ref);
- const memberships = await Promise.all(
- teamIds.map(async (id) => ((await deps.teams?.isMember(principalId, id)) ? id : null)),
- );
- const principal = { ...deps.identity.classify(principalId), teamIds: memberships.filter((id) => id !== null) };
const terms = searchTerms(query);
const entryHits: SessionSearchHit[] = hits.flatMap((hit) => {
if (!allowed.get(hit.scopeId)) return [];
- if (!principalEntitledToScope(principal, hit.entryScopeLabel, hit.scopeId, scopeId("org", orgIdOf())))
- return [];
const session = hit;
return [
{
diff --git a/src/classify/scope-classifier.ts b/src/classify/scope-classifier.ts
deleted file mode 100644
index 260dd2e85..000000000
--- a/src/classify/scope-classifier.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import type { EntryType, ScopeId } from "../types.ts";
-
-export interface ClassifyInput {
- type: EntryType;
- sessionScopeId: ScopeId;
- orgScopeId: ScopeId;
- sourceScopeId?: ScopeId | null;
-}
-
-export function classifyScopeLabel(input: ClassifyInput): ScopeId {
- if (input.type === "soul") return input.orgScopeId;
- if (input.type === "tool_result" && input.sourceScopeId) return input.sourceScopeId;
- return input.sessionScopeId;
-}
diff --git a/src/core/attachments.ts b/src/core/attachments.ts
index 7b90068f2..580a8952d 100644
--- a/src/core/attachments.ts
+++ b/src/core/attachments.ts
@@ -225,21 +225,8 @@ export function inboundManifest(metas: AttachmentMeta[], inboxDir = INBOX_DIR, u
return `${lead}\n${list}`;
}
-const FILE_EVENT_KIND = "file_event";
-
-export interface FileEventPayload {
- kind: typeof FILE_EVENT_KIND;
- direction: FileDirection;
- issues: string[];
- text: string;
-}
-
-export function fileEventPayload(direction: FileDirection, issues: string[]): FileEventPayload {
- const lead =
- direction === "in"
- ? `Files received this turn that did not reach ./${INBOX_DIR}/`
- : "Files the agent tried to send that did not go out";
- return { kind: FILE_EVENT_KIND, direction, issues, text: `${lead}: ${issues.join("; ")}` };
+export function inboundIssuesNote(issues: readonly string[]): string {
+ return issues.length ? `Files received this turn that did not reach ./${INBOX_DIR}/: ${issues.join("; ")}` : "";
}
export function inboundIssueList(opts: {
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.ts
index 0e8f69b7f..a857004ba 100644
--- a/src/core/orchestrator.ts
+++ b/src/core/orchestrator.ts
@@ -4,7 +4,6 @@ import { scopeTeamsToTurn } from "../teams/teams.ts";
import { externalSlackRequestAllowed, currentExternalSlackRun } from "../resolution/external-slack.ts";
import { desktopComputerTool } from "../../plugins/chassis/src/desktop-computer.ts";
import { externalTools } from "./orchestrator/external-tools.ts";
-import { memoryBoundedEntries, memoryContextPayload, nextMemoryContext } from "../memory/context-boundary.ts";
import { isBackendCredential } from "../credentials/keychain.ts";
import { resolveBrowserModel } from "../model/browser-model.ts";
import { memoryRecallDelta } from "../memory/recall-delta.ts";
@@ -120,7 +119,7 @@ import { isIdeasConversation, PROACTIVE_OPENER_PROMPT } from "../onboarding/onbo
import { createToolContext, NeedsApproval, CommandDenied, type CommandCredential } from "../tools/primitives.ts";
import type { FileArtifact } from "../files/file-artifact-store.ts";
import { artifactImageLoader } from "../harness/replay-images.ts";
-import { filterHistoryForAudience, principalEntitledToScope } from "../resolution/context-filter.ts";
+import { principalEntitledToScope } from "../resolution/context-filter.ts";
import { openSessionEntry, searchSessionEntries } from "../sessions/history-search.ts";
import { createTranscriptSource } from "../sessions/transcript-source.ts";
import { defaultPublishAudience } from "../resolution/publish-audience.ts";
@@ -128,7 +127,7 @@ import {
INBOX_DIR,
TURN_FILES_DIR,
environmentNote,
- fileEventPayload,
+ inboundIssuesNote,
inboundIssueList,
inboundManifest,
isVisionAttachment,
@@ -218,7 +217,6 @@ export type { Orchestrator, OrchestratorDeps, OrchestratorInput, SurfaceContextP
class ProjectRosterChanged extends Error {}
const ACTIVITY_ENTRY_TYPES = new Set<EntryType>([
- "text_start",
"text",
"tool_call",
"tool_result",
@@ -375,7 +373,8 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
{ waitFor: (heldBy) => heldBy !== undefined && heldBy !== "turn" },
);
const waitedMs = attempt.waitedMs ?? 0;
- if (attempt.lease) return { lease: attempt.lease, waitedMs };
+ if (attempt.lease)
+ return { lease: args.runId ? { ...attempt.lease, turnId: args.runId } : attempt.lease, waitedMs };
if (attempt.heldUntil === undefined) {
deps.errors?.record({
category: "sessions",
@@ -707,15 +706,13 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
});
let participantHistorySeqs: Set<number> | undefined;
let participantHistoryMaxSeq = -1;
- const filterHistory = (entries: SessionEntry[]): SessionEntry[] =>
- filterHistoryForAudience(
- participantHistorySeqs
- ? entries.filter((entry) => entry.seq > participantHistoryMaxSeq || participantHistorySeqs!.has(entry.seq))
- : entries,
- conversation.audience,
- scopeId,
- resolution.orgScopeId,
+ const filterHistory = (entries: SessionEntry[]): SessionEntry[] => {
+ if (!conversation.audience.length) return [];
+ if (!participantHistorySeqs) return entries;
+ return entries.filter(
+ (entry) => entry.seq > participantHistoryMaxSeq || participantHistorySeqs!.has(entry.seq),
);
+ };
const reconcileSessionParticipants = async (sessionId: string): Promise<void> => {
const participantIds = input.sessionParticipantIds;
if (!participantIds?.length) return;
@@ -1298,24 +1295,14 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
if (!input.runId || !deps.runs) return null;
const seq = (await deps.runs.get(input.runId))?.turnUserSeq;
if (seq == null) return null;
- const entries = memoryBoundedEntries(await deps.sessions.getEntries(session.id));
+ const entries = await deps.sessions.getEntries(session.id);
if (!entries.some((entry) => entry.seq === seq)) return null;
return turnAtSeq(
entries.filter((entry) => entry.seq >= seq),
seq,
);
};
- const initialMemoryWindow = await deps.sessions.getContextWindow(session.id);
- const initialMemoryContext = initialMemoryWindow.entries.findLast((entry) => memoryContextPayload(entry));
- const candidateMemoryContext = nextMemoryContext(
- initialMemoryWindow.entries,
- memoryView.snapshot,
- await deps.sessions.latestEntrySeq(session.id),
- );
- let memoryContextChanged =
- candidateMemoryContext.throughSeq >
- (initialMemoryContext ? memoryContextPayload(initialMemoryContext)!.throughSeq : -1);
- const recordedTurn = isRetry && !memoryContextChanged ? await recordedTurnForRun() : null;
+ const recordedTurn = isRetry ? await recordedTurnForRun() : null;
if (recordedTurn?.answer) {
const recordedAnswer = recordedTurn.answer;
deps.auditLog.record({
@@ -1996,19 +1983,10 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
const memoryWindow = await deps.sessions.getContextWindow(session.id);
memoryView = await context.memorySnapshot();
- const latestMemoryContext = memoryWindow.entries.findLast((entry) => memoryContextPayload(entry));
- const nextContext = nextMemoryContext(
- memoryWindow.entries,
- memoryView.snapshot,
- await deps.sessions.latestEntrySeq(session.id),
- );
- memoryContextChanged =
- nextContext.throughSeq > (latestMemoryContext ? memoryContextPayload(latestMemoryContext)!.throughSeq : -1);
- const memoryHistoryReset = nextContext.throughSeq >= 0;
const captureDependencies = () => [
...memoryView.facts,
...(!memoryView.complete ||
- memoryBoundedEntries(memoryWindow.entries).length > 0 ||
+ memoryWindow.entries.length > 0 ||
toolCalls > 0 ||
input.priorTurns?.length ||
input.overheard?.length ||
@@ -2017,23 +1995,10 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
? [UNTRACKED_PROVENANCE]
: []),
];
- if (memoryContextChanged) {
- await deps.harness.turns.resetSession?.(session.id);
- await deps.sessions.append(lease, { type: "system", scopeLabel: scopeId, payload: nextContext });
- }
+ const historyWindow = () => deps.sessions.getContextWindow(session.id);
if (input.approval) {
const p = await pending.get(input.approval.requestId);
const decision = input.approval.approved ? "approve" : "deny";
- const cutoffEntry = memoryHistoryReset
- ? await deps.sessions.getEntry(session.id, nextContext.throughSeq)
- : undefined;
- if (cutoffEntry && (!p?.createdAt || p.createdAt <= cutoffEntry.createdAt))
- return {
- status: "refused",
- sessionId: session.id,
- reason:
- "Conversation audience changed; submit a fresh request rather than resuming the previous approval.",
- };
if (!p || p.sessionId !== session.id) {
deps.auditLog.record({
at: Date.now(),
@@ -2076,6 +2041,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
type: "approval_resolved",
payload: { requestId: input.approval!.requestId, command: p.command, approved: false },
scopeLabel: scopeId as ScopeId,
+ author: actor.id,
}),
);
await mirrorRunActivity(decisionEntry);
@@ -2180,6 +2146,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
type: "approval_resolved",
payload: { requestId: input.approval!.requestId, command: p.command, approved: true, scope },
scopeLabel: scopeId as ScopeId,
+ author: actor.id,
}),
);
await mirrorRunActivity(decisionEntry);
@@ -2369,22 +2336,16 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
deps.harness.models.shouldRespond &&
!(
isRetry &&
- (recordedTurn ??
- (!memoryContextChanged &&
- findTrailingPartialTurn((await transcripts.forRender(session.id)).entries, input.runId)))
+ (recordedTurn ?? findTrailingPartialTurn((await transcripts.forRender(session.id)).entries, input.runId))
)
) {
- const detectHistory = filterHistory(
- memoryBoundedEntries((await deps.sessions.getContextWindow(session.id)).entries)
- .slice(-DETECT_HISTORY_TAIL)
- .filter((e) => e.type !== "soul"),
- );
+ const detectHistory = filterHistory((await historyWindow()).entries.slice(-DETECT_HISTORY_TAIL));
const detectStart = Date.now();
const decision = await deps.harness.models.shouldRespond({
session,
message: input.text,
- recentContext: memoryHistoryReset ? "" : (input.detectContext ?? ""),
- ...(!memoryHistoryReset && input.detectOpener ? { threadOpener: input.detectOpener } : {}),
+ recentContext: input.detectContext ?? "",
+ ...(input.detectOpener ? { threadOpener: input.detectOpener } : {}),
systemPrompt: resolution.systemPrompt,
...(input.gatewayContext?.reactionGuidance
? { reactionGuidance: input.gatewayContext.reactionGuidance }
@@ -2742,6 +2703,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
: {}),
...(deps.webhookPublicUrl ? { webhookPublicUrl: deps.webhookPublicUrl } : {}),
...(humanInput && !external ? { humanTurn: true } : {}),
+ ...(deps.toolOutput ? { toolOutput: deps.toolOutput, sessionId: session.id } : {}),
...(surfaceToolDeps ? { surface: surfaceToolDeps } : {}),
...(sessionSyscalls
? {
@@ -2750,7 +2712,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
scopeId: scopeId as ScopeId,
orgScopeId: resolution.orgScopeId,
liveTurn,
- memoryContext: memoryView.snapshot,
request: { ...input, readOnly: strictReadOnly, cancel: turnAbort.signal },
}),
}
@@ -2842,18 +2803,9 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
: []),
],
});
- if (inboundIssues.length) {
- await withManagedRosterVersion(() =>
- deps.sessions.append(lease, {
- type: "system",
- payload: fileEventPayload("in", inboundIssues),
- scopeLabel: scopeId,
- }),
- ).catch(swallowAs("orchestrator: inbound file-event log", undefined));
- }
const importedOverheard: OverheardEntryPayload[] = [];
- if (!memoryHistoryReset && (!input.envelopeWrapped || humanInput) && input.overheard?.length) {
+ if ((!input.envelopeWrapped || humanInput) && input.overheard?.length) {
const toImport = await transcripts
.forRender(session.id)
.then((read) => selectOverheardToImport(input.overheard!, recordedMessageTimestamps(read.entries)))
@@ -2873,8 +2825,8 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
if (importedEntries.length) importedOverheard.push(...toImport);
}
- const rawEntries = (await deps.sessions.getContextWindow(session.id)).entries;
- const priorTurns = memoryHistoryReset ? undefined : input.priorTurns;
+ const rawEntries = (await historyWindow()).entries;
+ const priorTurns = input.priorTurns;
let visibleHistory = filterHistory(forModelContext(rawEntries));
const recallStart = Date.now();
const recalled = await context.recallMemory(memoryRecallQuery(input.text, visibleHistory));
@@ -3026,14 +2978,15 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
const approvalReplay =
!!pausedTurnUserEntry &&
String((pausedTurnUserEntry.payload as { text?: string } | null)?.text ?? "").trim() === input.text.trim();
- if (partial)
- postKeys.seed(
- completedSurfaceEnqueues(
- filterHistory(await deps.sessions.getEntries(session.id, { sinceSeq: partial.userSeq })),
- partial.userSeq,
- surfaceName,
- ),
+ if (partial) {
+ const posted = completedSurfaceEnqueues(
+ filterHistory(await deps.sessions.getEntries(session.id, { sinceSeq: partial.userSeq })),
+ partial.userSeq,
+ surfaceName,
);
+ postKeys.seed(posted);
+ spine.surfaceOutboundCount += posted;
+ }
if (partial) {
deps.auditLog.record({
at: Date.now(),
@@ -3083,9 +3036,10 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
let turnRuntime: TurnRuntime | undefined;
const syntheticPrompt = syntheticInput || partial || approvalReplay || !!releasedToolOutput;
failureUserPayload =
- !syntheticPrompt && input.text.trim()
+ !syntheticPrompt && (input.text.trim() || inboundIssues.length)
? {
text: input.text,
+ ...(inboundIssues.length ? { environment: environmentNote(inboundIssuesNote(inboundIssues)) } : {}),
...(humanInput ? { speakerId: actor.id } : {}),
...((messageTs ?? entryTs) ? { ts: messageTs ?? entryTs } : {}),
...(actor.displayName?.trim() ? { name: actor.displayName.trim() } : {}),
@@ -3252,7 +3206,12 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
);
const snapshot = environmentDelta(turnContextBlocks, continuation?.history ?? history, session.id);
const turnEnvironment = environmentNote(
- [turnEnvironmentContents, snapshot.text, recall.text ? `${memoryHeading}${recall.text}` : ""]
+ [
+ turnEnvironmentContents,
+ snapshot.text,
+ recall.text ? `${memoryHeading}${recall.text}` : "",
+ inboundIssuesNote(inboundIssues),
+ ]
.filter(Boolean)
.join("\n\n"),
);
@@ -3281,7 +3240,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
const payload = isObj(entry.payload) ? { ...entry.payload } : {};
if (!recordedRecall && !payload.steered && !payload.overheard) {
recordedRecall = true;
- payload.memoryContext = nextContext;
payload.environmentSnapshot = snapshot.record;
if (environment) payload.environment = environment;
if (recall.text) payload.memoryRecall = recall.record;
@@ -3333,6 +3291,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
loadImage: artifactImageLoader(deps.files, mayReadArtifact),
...(turnTimezone ? { timezone: turnTimezone } : {}),
prepareSteer: async (text, request) => {
+ const author = request ? deps.identity.resolve(request.actor).id : actor.id;
const refreshedInbox =
input.surface === "web" &&
conversation.kind === "dm" &&
@@ -3360,7 +3319,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
}
text = `${text}\n\n${allowed ? refreshedInbox : "Updated inbox context was withheld by the security screen."}`;
}
- if (!request?.attachments?.length) return { text };
+ if (!request?.attachments?.length) return { text, author };
const seed = `${fileRegistration.seed}:steer:${randomUUID()}`;
const inboxDir = `${turnInboxDir}/${randomUUID()}`;
const received = strictReadOnly
@@ -3418,7 +3377,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
text,
inboundManifest(received.metas, inboxDir, received.unstaged),
...steeredDocuments.notices,
- issues.length ? fileEventPayload("in", issues).text : "",
+ inboundIssuesNote(issues),
enforceScreen &&
(steeredUnscreened ||
received.unscreened.length ||
@@ -3431,6 +3390,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
attachments: received.metas,
images: received.images,
documents: steeredDocuments.documents,
+ author,
};
},
...(userProviderKeys ? { providerKeys: userProviderKeys } : {}),
@@ -3669,12 +3629,16 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
input.runId && deps.turnStream
? deps.turnStream.startOffset(input.runId) + (deps.turnStream.snapshot(input.runId) ?? "").length
: undefined;
- const started = emit({
- type: "text_start",
- payload: { ...(phase ? { phase } : {}), ...(streamOffset !== undefined ? { streamOffset } : {}) },
- scopeLabel: scopeId,
- });
- await (phase ? started : started.catch(swallowAs("orchestrator: text-start activity", undefined)));
+ if (input.runId && deps.runActivity) {
+ const started = deps.runActivity.append(input.runId, {
+ seq: emittedEntries.at(-1)?.seq ?? 0,
+ parentSeq: null,
+ type: "text_start",
+ payload: { ...(phase ? { phase } : {}), ...(streamOffset !== undefined ? { streamOffset } : {}) },
+ createdAt: Date.now(),
+ });
+ await (phase ? started : started.catch(swallowAs("orchestrator: text-start activity", undefined)));
+ }
}
},
onGapWork: (cb) => {
@@ -3713,9 +3677,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
await adoptRuntime(segment.runtimeHandoff.choice, segment.runtimeHandoff.lifetime);
await deps.harness.turns.resetSession?.(session.id);
}
- let resumedHistory = filterHistory(
- forModelContext((await deps.sessions.getContextWindow(session.id)).entries),
- );
+ let resumedHistory = filterHistory(forModelContext((await historyWindow()).entries));
if (recovery)
resumedHistory = await withManagedRosterVersion(() =>
compactRecent({
@@ -3794,9 +3756,7 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
}
}
if (spine.surfaceOutboundCount === 0 && !silentPollNarration) {
- const nudgeHistory = filterHistory(
- forModelContext((await deps.sessions.getContextWindow(session.id)).entries),
- );
+ const nudgeHistory = filterHistory(forModelContext((await historyWindow()).entries));
result = await runHarnessTurn(
"[system] You were addressed directly. Reply with the `slack` tool's `post` action, or decline explicitly with finish_silently — ending the turn without either is not allowed here.",
inbound.images.length ? { images: inbound.images } : {},
@@ -4164,7 +4124,12 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {
if ((isNonRetryable(err) || input.finalAttempt) && !input.cancel?.aborted) {
if (failureUserPayload) {
await deps.sessions
- .append(lease, { type: "user", payload: failureUserPayload, scopeLabel: scopeId as ScopeId })
+ .append(lease, {
+ type: "user",
+ payload: failureUserPayload,
+ scopeLabel: scopeId as ScopeId,
+ author: actor.id,
+ })
.catch(swallowAs("orchestrator: turn failure user back-fill", undefined));
}
const payload: TurnFailurePayload = {
diff --git a/src/core/orchestrator/compaction.ts b/src/core/orchestrator/compaction.ts
index a9d2aa66d..516fcc24e 100644
--- a/src/core/orchestrator/compaction.ts
+++ b/src/core/orchestrator/compaction.ts
@@ -1,4 +1,3 @@
-import { memoryContextPayload } from "../../memory/context-boundary.ts";
import type { ScopeId, Session, SessionEntry } from "../../types.ts";
import { parseScopeId } from "../../types.ts";
import type { Lease } from "../../sessions/session-store.ts";
@@ -296,7 +295,7 @@ export function createCompaction(deps: OrchestratorDeps): CompactionContext {
lease = (await acquireLeaseWithin(deps.sessions, input.sessionId, "compaction", WRITE_LEASE_WAIT_MS)).lease;
if (!lease) return;
const since = await deps.sessions.getEntries(input.sessionId, { sinceSeq: snapshotSeq + 1 });
- if (!since.some((entry) => !!contextSummaryPayload(entry) || !!memoryContextPayload(entry))) {
+ if (!since.some((entry) => !!contextSummaryPayload(entry))) {
await writeCompaction({ session, lease, summarized });
}
} catch (e) {
diff --git a/src/core/orchestrator/turn-helpers.ts b/src/core/orchestrator/turn-helpers.ts
index 99b7662d2..2cf267725 100644
--- a/src/core/orchestrator/turn-helpers.ts
+++ b/src/core/orchestrator/turn-helpers.ts
@@ -255,7 +255,9 @@ export function completedSurfaceEnqueues(
done: false,
});
} else if (e.type === "tool_result") {
- const cid = (e.payload as { callId?: unknown } | null)?.callId;
+ const payload = e.payload as { callId?: unknown; interrupted?: unknown } | null;
+ if (payload?.interrupted === true) continue;
+ const cid = payload?.callId;
const call =
typeof cid === "string" ? calls.find((c) => c.callId === cid && !c.done) : calls.findLast((c) => !c.done);
if (call) call.done = true;
diff --git a/src/core/orchestrator/types.ts b/src/core/orchestrator/types.ts
index 97fa93702..77418855f 100644
--- a/src/core/orchestrator/types.ts
+++ b/src/core/orchestrator/types.ts
@@ -64,6 +64,7 @@ import type { OAuthClientResolver } from "../../connectors/oauth.ts";
import type { SkillBundleStore } from "../../skills/skill-bundle-store.ts";
import type { BrokeredLayerTool, DeploymentLayerRuntime } from "../../deployment/load-layer.ts";
import type { FileArtifactStore } from "../../files/file-artifact-store.ts";
+import type { ToolOutputStore } from "../../sessions/tool-output.ts";
import type { DeployService } from "../../deploy/deploy-service.ts";
import type { AclStore } from "../../acl/acl-store.ts";
import type { SwarmService, SwarmTurn } from "../../swarms/swarm-service.ts";
@@ -129,6 +130,7 @@ export interface OrchestratorDeps {
sessionSyscalls?: SessionSyscallsFactory;
workspace: WorkspaceStore;
files: FileArtifactStore;
+ toolOutput?: ToolOutputStore;
sandbox: Sandbox;
sandboxResources?: SandboxResources;
modelGateway: ModelGateway;
diff --git a/src/core/turn-resume.ts b/src/core/turn-resume.ts
index 15ed8915a..0d5609525 100644
--- a/src/core/turn-resume.ts
+++ b/src/core/turn-resume.ts
@@ -3,7 +3,11 @@ import type { RuntimeChoice } from "../harness/harness.ts";
import { unrecordedToolResult } from "../harness/context-compaction.ts";
import type { RunStop } from "../runs/run-store.ts";
import type { SessionEntry } from "../types.ts";
-import { entryDeliveryKey, isOverheardEntry, type NewEntry } from "../sessions/session-store.ts";
+import { isOverheardEntry, type NewEntry } from "../sessions/session-store.ts";
+
+const entryTurnId = (entry: SessionEntry): unknown => (entry.payload as { runId?: unknown } | null)?.runId;
+const differentTurn = (entry: SessionEntry, turn: SessionEntry): boolean =>
+ entryTurnId(entry) != null && entryTurnId(turn) != null && entryTurnId(entry) !== entryTurnId(turn);
export interface PartialTurn {
userSeq: number;
@@ -30,7 +34,16 @@ export function findTrailingPartialTurn(
let workEntries = 0;
for (let i = entries.length - 1; i >= 0; i--) {
const e = entries[i]!;
- if (e.type === "assistant" || isTurnStoppedEntry(e)) return null;
+ if (isTurnStoppedEntry(e)) return null;
+ if (e.type === "assistant") {
+ const start = entries
+ .slice(0, i)
+ .findLast(
+ (item) => item.type === "user" && !isOverheardEntry(item) && !isSteerEntry(item) && !isResumeNote(item),
+ );
+ if (!start || !differentTurn(e, start)) return null;
+ continue;
+ }
if (e.type === "tool_call" || e.type === "tool_result") {
workEntries += 1;
continue;
@@ -60,8 +73,9 @@ export function turnAtSeq(entries: readonly SessionEntry[], userSeq: number): Re
let answer: RecordedTurn["answer"];
for (let i = start + 1; i < entries.length; i++) {
const e = entries[i]!;
+ if (e.type !== "user" && differentTurn(e, entries[start]!)) continue;
if (e.type === "assistant") {
- if (!entryDeliveryKey(e)) answer = { seq: e.seq, text: entryText(e) };
+ answer = { seq: e.seq, text: entryText(e) };
continue;
}
if (e.type === "tool_call" || e.type === "tool_result") {
@@ -93,7 +107,8 @@ export type ResumeStrategy =
export function resumeStrategy(entries: readonly SessionEntry[], partial: PartialTurn): ResumeStrategy {
if (partial.workEntries === 0) return { kind: "restart" };
- const turn = entries.filter((e) => e.seq > partial.userSeq);
+ const anchor = entries.find((e) => e.seq === partial.userSeq);
+ const turn = entries.filter((e) => e.seq > partial.userSeq && (!anchor || !differentTurn(e, anchor)));
const answered = new Set<unknown>();
const withResult = new Set<unknown>();
for (const e of turn) {
@@ -142,7 +157,13 @@ export function unrecordedResultEntries(
): NewEntry[] {
return strategy.calls.map((call) => ({
type: "tool_result",
- payload: { tool: call.tool, callId: call.callId, isError: true, result: unrecordedToolResult(stop) },
+ payload: {
+ tool: call.tool,
+ callId: call.callId,
+ interrupted: true,
+ isError: true,
+ result: unrecordedToolResult(stop),
+ },
scopeLabel,
}));
}
diff --git a/src/delivery/run-result-delivery.ts b/src/delivery/run-result-delivery.ts
index d99ab09f7..83de54396 100644
--- a/src/delivery/run-result-delivery.ts
+++ b/src/delivery/run-result-delivery.ts
@@ -6,7 +6,7 @@ import { isSessionTurn, resolveTurnOrigin } from "../core/turn-origin.ts";
import type { TurnFailurePayload } from "../core/turn-error.ts";
import { standaloneFailureText, userFacingFailureClause } from "../core/failure-copy.ts";
import { conversationScope } from "../resolution/resolution-service.ts";
-import { acquireLeaseWithin, entryDeliveryKey, type SessionStore } from "../sessions/session-store.ts";
+import { acquireLeaseWithin, type SessionStore } from "../sessions/session-store.ts";
import { turnRecordedFailure } from "./web-transcript-delivery.ts";
import { reportFailureAs } from "../util/errors.ts";
@@ -115,7 +115,6 @@ export async function recordRunFailureEntry(sessions: TurnFailureSessions, run:
try {
const tail = await sessions.getEntries(session.id, { limit: FAILURE_RECORD_SCAN_LIMIT });
if (turnRecordedFailure(tail, { notBefore: run.startedAt ?? run.createdAt, runId: run.id })) return false;
- if (tail.some((entry) => entryDeliveryKey(entry) === `run:${run.id}`)) return false;
const payload: TurnFailurePayload = {
kind: "turn_failure",
message: `I couldn't finish that turn: ${userFacingFailureClause(run.result ?? { status: "failed" })}`,
diff --git a/src/delivery/web-transcript-delivery.ts b/src/delivery/web-transcript-delivery.ts
index a36af3b29..482f5da21 100644
--- a/src/delivery/web-transcript-delivery.ts
+++ b/src/delivery/web-transcript-delivery.ts
@@ -1,6 +1,6 @@
import type { Delivery, ScopeId, SessionEntry } from "../types.ts";
import type { DeliveryStore } from "./delivery-store.ts";
-import { entryDeliveryKey, type SessionStore } from "../sessions/session-store.ts";
+import { type NewEntry, type SessionStore } from "../sessions/session-store.ts";
import { reportFailure, swallow } from "../util/errors.ts";
const DEDUPE_SCAN_LIMIT = 200;
@@ -9,7 +9,7 @@ const WRITE_GIVEUP_MS = 10 * 60_000;
type WebTranscriptSessions = Pick<
SessionStore,
- "append" | "hasDeliveryEntry" | "get" | "getByThread" | "acquireLease" | "releaseLease" | "getEntries"
+ "appendMany" | "hasDeliveryEntry" | "get" | "getByThread" | "acquireLease" | "releaseLease" | "getEntries"
>;
export function turnRecordedFailure(
@@ -21,7 +21,7 @@ export function turnRecordedFailure(
const payload = entry.payload as { kind?: unknown; runId?: unknown } | null;
if (payload?.kind !== "turn_failure") return false;
if (typeof payload.runId === "string") return payload.runId === note.runId;
- return entryDeliveryKey(entry) === undefined && entry.createdAt >= note.notBefore;
+ return entry.createdAt >= note.notBefore;
});
}
@@ -49,16 +49,24 @@ export function withWebTranscriptDeliveries(store: DeliveryStore, sessions: WebT
const { lease } = await sessions.acquireLease(session.id, "backfill");
if (!lease) return false;
try {
- const tail = await sessions.getEntries(session.id, { limit: DEDUPE_SCAN_LIMIT });
const note = d.destination.webTranscript;
const reply = note?.kind === "slack_reply" ? note : undefined;
if (await sessions.hasDeliveryEntry(session.id, d.idempotencyKey, reply?.ts)) return true;
const failure = note?.kind === "turn_failure" ? note : undefined;
- if (failure && turnRecordedFailure(tail, failure)) return true;
+ if (failure && turnRecordedFailure(await sessions.getEntries(session.id, { limit: DEDUPE_SCAN_LIMIT }), failure))
+ return true;
+ const turnId = failure?.runId ?? d.id;
+ if (
+ !failure &&
+ (await sessions.getEntries(session.id, { turnId })).some(
+ (entry) => entry.type === (reply ? "user" : "assistant"),
+ )
+ )
+ return true;
const scopeLabel = session.scopeId;
const ownReply = d.provenance?.sourceThreadRef === d.destination.target;
const via = d.provenance?.trigger && !ownReply ? d.provenance.trigger : undefined;
- let entry: Pick<SessionEntry, "type" | "payload">;
+ let entry: NewEntry;
if (reply) {
entry = {
type: "user",
@@ -69,24 +77,18 @@ export function withWebTranscriptDeliveries(store: DeliveryStore, sessions: WebT
slackReplyReceipt: true,
deliveryKey: d.idempotencyKey,
},
+ scopeLabel,
};
} else if (failure) {
entry = {
type: "system",
- payload: {
- kind: "turn_failure",
- message: d.text,
- deliveryKey: d.idempotencyKey,
- ...(failure.runId ? { runId: failure.runId } : {}),
- },
+ payload: { kind: "turn_failure", message: d.text, ...(failure.runId ? { runId: failure.runId } : {}) },
+ scopeLabel,
};
} else {
- entry = {
- type: "assistant",
- payload: { text: d.text, deliveryKey: d.idempotencyKey, ...(via ? { via } : {}) },
- };
+ entry = { type: "assistant", payload: { text: d.text, ...(via ? { via } : {}) }, scopeLabel };
}
- await sessions.append(lease, { ...entry, scopeLabel });
+ await sessions.appendMany({ ...lease, turnId }, [entry]);
return true;
} finally {
await sessions.releaseLease(lease);
diff --git a/src/harness/agent-tools.ts b/src/harness/agent-tools.ts
index 90e5c789b..9fe65beb6 100644
--- a/src/harness/agent-tools.ts
+++ b/src/harness/agent-tools.ts
@@ -1,4 +1,5 @@
import { adminOperations } from "../tools/admin.ts";
+import { parkIfOverCap } from "../sessions/tool-output.ts";
import { handbackStop } from "../runs/run-store.ts";
import { MAX_OUTBOUND_FILES, type OutboundFile } from "../core/attachments.ts";
import { MaskedExecutionError } from "../security/secret-masking.ts";
@@ -14,7 +15,6 @@ import type { ClientToolDeclaration, CronFireLogEntry, EntryType, ScopeId } from
import type { ToolContext, PublishInput, PublishAudienceDescriptor } from "../tools/primitives.ts";
import type { GapWork } from "../sessions/session-store.ts";
import { NeedsApproval, CommandDenied } from "../tools/primitives.ts";
-import { classifyScopeLabel } from "../classify/scope-classifier.ts";
import type { McpToolDescriptor } from "../mcp/mcp-tool-service.ts";
import { splitToScope } from "../api/artifact-share.ts";
import { errMessage } from "../util/errors.ts";
@@ -383,19 +383,13 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
capabilityTtlMs === 0
? "Two limits: this deployment does not expire those turn tokens, but authorization checks still apply (publish checkpoints to durable Files and retain the file ID for the later turn or cron)"
: `Two limits: those turn tokens expire ${capabilityTtlHours} hours after the turn that launched the job started (past that they 401 — publish checkpoints to durable Files and verify success before expiry, retaining the file ID for the later turn or cron; if publication is unavailable, report it and retain needed local state on a scoped computer)`;
- const log = async (type: EntryType, payload: unknown, sourceScopeId?: ScopeId | null): Promise<void> => {
+ const log = async (type: EntryType, payload: unknown): Promise<void> => {
if (!ref.emit || !ref.scopeLabel) return;
- const scopeLabel = classifyScopeLabel({
- type,
- sessionScopeId: ref.scopeLabel,
- orgScopeId: ref.orgScopeId ?? ref.scopeLabel,
- sourceScopeId,
- });
if (type === "tool_result" && isObj(payload)) {
if (ref.shutdown?.aborted) payload = { ...payload, interrupted: true, stop: handbackStop(ref.shutdown) };
else if (ref.abortSignal?.aborted) payload = { ...payload, stopped: true };
}
- await ref.emit({ type, payload, scopeLabel });
+ await ref.emit({ type, payload, scopeLabel: ref.scopeLabel });
};
const retryMarks = new Map<string, RetryMark>();
@@ -426,7 +420,9 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
.filter((c) => c.type === "text")
.map((c) => c.text ?? "")
.join("\n");
- let result = capResultText(t);
+ const parked = ref.current?.toolOutput ? await parkIfOverCap(ref.current.toolOutput, t, callId) : null;
+ const output = parked?.output;
+ let result = parked?.text ?? capResultText(t);
const resultTruncated = result !== t;
if (result !== t) {
const firstText = ret.content.findIndex((c) => c.type === "text");
@@ -532,18 +528,15 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):
result += `\n\n${text}`;
delivered.push(message.id);
}
- await log(
- "tool_result",
- {
- ...(capPayloadStrings(persistedSummary) as Record<string, unknown>),
- callId,
- isError,
- ...(resultTruncated ? { resultTruncated: true } : {}),
- ...(display ? { display } : {}),
- result,
- },
- delivered.length ? ref.scopeLabel : sourceScopeId,
- );
+ await log("tool_result", {
+ ...(capPayloadStrings(persistedSummary) as Record<string, unknown>),
+ callId,
+ isError,
+ ...(resultTruncated ? { resultTruncated: true } : {}),
+ ...(display ? { display } : {}),
+ result,
+ ...(output ? { output } : {}),
+ });
if (delivered.length) await ref.current?.sessionSyscalls?.acknowledge?.(delivered).catch(() => undefined);
return ret;
});
diff --git a/src/harness/claude-harness.ts b/src/harness/claude-harness.ts
index 353984e81..df6485d88 100644
--- a/src/harness/claude-harness.ts
+++ b/src/harness/claude-harness.ts
@@ -20,8 +20,8 @@ import {
import { fromJSONSchema, type ZodObject } from "zod";
import { contentText, createAssistantMessageEventStream } from "@earendil-works/pi-ai";
import { CONFIG_DEFAULTS, type Config } from "../config.ts";
-import { isDeliveryNote } from "../core/attachments.ts";
import { NonRetryableTurnError } from "../core/turn-error.ts";
+import { isDeliveryNote } from "../core/attachments.ts";
import {
contextTokenBudgetForModel,
getRequiredModel,
@@ -376,6 +376,7 @@ export function createClaudeHarness(opts: ClaudeHarnessOptions = {}): Harness {
...(turn.attachments?.length ? { attachments: turn.attachments } : {}),
},
scopeLabel: turn.scopeLabel,
+ ...(turn.runtimeActorId ? { author: turn.runtimeActorId } : {}),
});
const requestedModel = turn.runtime?.modelId;
const model = modelSupportedByHarness(requestedModel, "claude") ? requestedModel! : resolveModelId(turn.scopeLabel);
@@ -536,7 +537,13 @@ export function createClaudeHarness(opts: ClaudeHarnessOptions = {}): Harness {
if (settled || interrupted) return false;
steerPrompts.push({
message: baseMessage,
- intake: { text: steer, ts, attachments: prepared?.attachments, acknowledge },
+ intake: {
+ text: steer,
+ ts,
+ attachments: prepared?.attachments,
+ author: prepared?.author,
+ acknowledge,
+ },
});
pendingPrompts++;
queue.push(message);
diff --git a/src/harness/context-compaction.ts b/src/harness/context-compaction.ts
index b48e873a7..ab0064af8 100644
--- a/src/harness/context-compaction.ts
+++ b/src/harness/context-compaction.ts
@@ -1,5 +1,4 @@
import type { RunStop } from "../runs/run-store.ts";
-import { memoryBoundedEntries } from "../memory/context-boundary.ts";
import type { SessionEntry } from "../types.ts";
import { contextSummaryPayload } from "../sessions/session-store.ts";
import { headSlice, tailSlice } from "../util/text.ts";
@@ -47,10 +46,9 @@ export function withoutWire(payload: unknown): Record<string, unknown> {
}
function modelReplayable(entries: SessionEntry[]): SessionEntry[] {
- return memoryBoundedEntries(entries).filter(
+ return entries.filter(
(e) =>
((e.type !== "thinking" && e.type !== "text") || isObj((e.payload as { step?: unknown } | null)?.step)) &&
- e.type !== "soul" &&
(e.payload as { slackReplyReceipt?: unknown } | null)?.slackReplyReceipt !== true &&
(e.payload as { kind?: unknown } | null)?.kind !== "turn_failure",
);
diff --git a/src/harness/harness-shared.ts b/src/harness/harness-shared.ts
index f2267dfde..c3b45df13 100644
--- a/src/harness/harness-shared.ts
+++ b/src/harness/harness-shared.ts
@@ -47,6 +47,7 @@ export interface SteerIntake {
ts?: string;
attachments?: HarnessTurnInput["attachments"];
images?: HarnessTurnInput["images"];
+ author?: string;
acknowledge?: () => Promise<void>;
}
@@ -72,6 +73,7 @@ export async function recordSteerIntake(turn: HarnessTurnInput, steer: SteerInta
...imageRefs(steer.images),
},
scopeLabel: turn.scopeLabel,
+ ...(steer.author ? { author: steer.author } : {}),
});
await steer.acknowledge?.().catch(swallowAs("steer acknowledge", undefined));
}
diff --git a/src/harness/harness.ts b/src/harness/harness.ts
index fd52b3ab5..245ab2c39 100644
--- a/src/harness/harness.ts
+++ b/src/harness/harness.ts
@@ -102,7 +102,13 @@ export interface HarnessTurnInput {
prepareSteer?(
text: string,
request?: TurnRequest,
- ): Promise<{ text: string; attachments?: AttachmentMeta[]; images?: HarnessImage[]; documents?: DocumentInput[] }>;
+ ): Promise<{
+ text: string;
+ attachments?: AttachmentMeta[];
+ images?: HarnessImage[];
+ documents?: DocumentInput[];
+ author?: string;
+ }>;
documents?: DocumentInput[];
runtime?: Partial<RuntimeChoice>;
runtimePurpose?: import("../resolution/config-store.ts").RuntimePurpose;
diff --git a/src/harness/mock-harness.ts b/src/harness/mock-harness.ts
index a6d24a156..6bb128a07 100644
--- a/src/harness/mock-harness.ts
+++ b/src/harness/mock-harness.ts
@@ -7,7 +7,6 @@ import {
type HarnessTurnInput,
type HarnessTurnResult,
} from "./harness.ts";
-import { classifyScopeLabel } from "../classify/scope-classifier.ts";
import { NonRetryableTurnError, ProviderTurnError, TitleRejected } from "../core/turn-error.ts";
import { NeedsApproval } from "../tools/primitives.ts";
import { deterministicCompactSummary, estimateHistoryTokens } from "./context-compaction.ts";
@@ -82,6 +81,21 @@ function mockProviderMessages(
});
}
+function withCallIds(emit: HarnessTurnInput["emit"]): HarnessTurnInput["emit"] {
+ const open: string[] = [];
+ return (entry) => {
+ if (entry.type !== "tool_call" && entry.type !== "tool_result") return emit(entry);
+ const payload = { ...(entry.payload as Record<string, unknown>) };
+ if (typeof payload.callId !== "string") {
+ payload.callId =
+ entry.type === "tool_call" ? `mock-call-${randomUUID()}` : (open.shift() ?? `mock-call-${randomUUID()}`);
+ if (entry.type === "tool_call") open.push(payload.callId as string);
+ } else if (entry.type === "tool_call") open.push(payload.callId);
+ else open.splice(open.indexOf(payload.callId), open.includes(payload.callId) ? 1 : 0);
+ return emit({ ...entry, payload });
+ };
+}
+
export function createMockHarness(): Harness {
const shedSessions = new Set<string>();
const boomAlwaysSessions = new Set<string>();
@@ -97,7 +111,8 @@ export function createMockHarness(): Harness {
capabilities: new Set(),
},
{
- async runTurn(turn: HarnessTurnInput): Promise<HarnessTurnResult> {
+ async runTurn(input: HarnessTurnInput): Promise<HarnessTurnResult> {
+ let turn: HarnessTurnInput = { ...input, emit: withCallIds(input.emit) };
const continued = turn.continueTurn ? turn.history.find((e) => e.seq === turn.resumeUserSeq) : undefined;
if (continued) turn = { ...turn, input: "", environment: undefined };
const userEntry =
@@ -110,6 +125,7 @@ export function createMockHarness(): Harness {
...(turn.attachments?.length ? { attachments: turn.attachments } : {}),
},
scopeLabel: turn.scopeLabel,
+ ...(turn.runtimeActorId ? { author: turn.runtimeActorId } : {}),
}));
const modelPrompt = [turn.input, turn.environment].filter((s) => s && s.trim()).join("\n\n");
@@ -562,16 +578,11 @@ export function createMockHarness(): Harness {
reply = "";
} else if (command0.startsWith("!read ")) {
const path = command0.slice(6).trim();
- const { content, sourceScopeId } = await turn.tools.read(path);
+ const { content } = await turn.tools.read(path);
await turn.emit({
type: "tool_result",
payload: { tool: "read", path, found: content != null },
- scopeLabel: classifyScopeLabel({
- type: "tool_result",
- sessionScopeId: turn.scopeLabel,
- orgScopeId: turn.orgScopeId,
- sourceScopeId,
- }),
+ scopeLabel: turn.scopeLabel,
});
usedTool = true;
reply = content ?? `(no file: ${path})`;
@@ -610,12 +621,7 @@ export function createMockHarness(): Harness {
await turn.emit({
type: "tool_result",
payload: { tool: "skill", name, found: r.content != null, ...(r.dir ? { dir: r.dir } : {}) },
- scopeLabel: classifyScopeLabel({
- type: "tool_result",
- sessionScopeId: turn.scopeLabel,
- orgScopeId: turn.orgScopeId,
- sourceScopeId: r.sourceScopeId,
- }),
+ scopeLabel: turn.scopeLabel,
});
usedTool = true;
reply =
diff --git a/src/harness/pi-harness.ts b/src/harness/pi-harness.ts
index 668bfc362..b335c9daf 100644
--- a/src/harness/pi-harness.ts
+++ b/src/harness/pi-harness.ts
@@ -1840,6 +1840,7 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {
...imageRefs(turn.images),
},
scopeLabel: turn.scopeLabel,
+ ...(turn.runtimeActorId ? { author: turn.runtimeActorId } : {}),
});
const modelPrompt =
(userEntry
@@ -2049,6 +2050,7 @@ export function createPiHarness(opts?: PiHarnessOptions): Harness {
ts,
attachments: prepared?.attachments,
images: prepared?.images,
+ author: prepared?.author,
acknowledge,
};
pendingSteers.push(steer);
diff --git a/src/harness/pi-step-recorder.ts b/src/harness/pi-step-recorder.ts
index 7a2eefbfb..41351c485 100644
--- a/src/harness/pi-step-recorder.ts
+++ b/src/harness/pi-step-recorder.ts
@@ -16,11 +16,21 @@ type TurnEntry = Parameters<HarnessTurnInput["emit"]>[0];
type Persist = (write: () => Promise<unknown>) => void;
type Positioned = { index: number; block: TextContent };
+function jsonbKeyOrder(value: unknown): unknown {
+ if (Array.isArray(value)) return value.map(jsonbKeyOrder);
+ if (!value || typeof value !== "object") return value;
+ const keys = Object.keys(value).sort(
+ (a, b) => Buffer.byteLength(a) - Buffer.byteLength(b) || Buffer.compare(Buffer.from(a), Buffer.from(b)),
+ );
+ return Object.fromEntries(keys.map((key) => [key, jsonbKeyOrder((value as Record<string, unknown>)[key])]));
+}
+
function toolCallWire(payload: Record<string, unknown>, block: ToolCall): { wire?: ToolCallWire } {
const { type: _type, id: _id, name, arguments: args, ...extra } = block;
const recorded = recordedToolArguments(payload);
const argumentsJson = JSON.stringify(args);
- const reproducible = argumentsJson === JSON.stringify(recorded);
+ const reproducible =
+ argumentsJson === JSON.stringify(recorded) && argumentsJson === JSON.stringify(jsonbKeyOrder(recorded));
const wire: ToolCallWire = {
...extra,
...(name !== payload.tool ? { name } : {}),
@@ -61,7 +71,7 @@ function resultText(content: ToolResultMessage["content"]): string {
export type StepRecorder = ReturnType<typeof createStepRecorder>;
-export function createStepRecorder(turn: Pick<HarnessTurnInput, "emit" | "scopeLabel">, persist: Persist) {
+export function createStepRecorder(turn: Pick<HarnessTurnInput, "emit" | "scopeLabel" | "shutdown">, persist: Persist) {
const calls = new Map<string, { step: ReplayStep; block: ToolCall }>();
const called = new Set<string>();
const executing = new Map<string, RecordedEntry>();
diff --git a/src/harness/replay.ts b/src/harness/replay.ts
index 29b564506..9fd8d93be 100644
--- a/src/harness/replay.ts
+++ b/src/harness/replay.ts
@@ -267,7 +267,7 @@ function stepBlocks(e: SessionEntry, step: ReplayStep): Positioned[] {
}
export function recordedToolArguments(p: Record<string, unknown>): Record<string, unknown> {
- const { tool: _tool, callId: _c, rerun, step: _s, wire: _w, ...recorded } = p;
+ const { tool: _tool, callId: _c, rerun, result: _r, step: _s, wire: _w, ...recorded } = p;
const input = (rerun as { input?: unknown } | undefined)?.input;
if (isObj(input)) return { ...input, retrySafe: true };
if (typeof recorded.bytes !== "number" || typeof recorded.text === "string") return recorded;
@@ -288,13 +288,19 @@ function resultContent(content: StoredResultContent[]): ToolResultMessage["conte
function toolResultMessage(
call: ToolCall,
+ recordedCall: SessionEntry | undefined,
re: SessionEntry | undefined,
at: number,
missing: string = INTERRUPTED_TOOL_RESULT,
): ToolResultMessage {
const base = { role: "toolResult" as const, toolCallId: call.id, toolName: call.name };
const text = (value: string) => [{ type: "text" as const, text: value }];
- if (!re) return { ...base, content: text(missing), isError: true, timestamp: at };
+ if (!re) {
+ const recorded = (recordedCall?.payload as { result?: unknown } | null)?.result;
+ return typeof recorded === "string"
+ ? { ...base, content: text(recorded), isError: false, timestamp: at }
+ : { ...base, content: text(missing), isError: true, timestamp: at };
+ }
const p = (re.payload ?? {}) as {
interrupted?: unknown;
stop?: RunStop;
@@ -322,10 +328,11 @@ export function reconstructMessagesFromHistory(
opts?: { requireUserStart?: boolean },
): PiReplayMessage[] {
const resultByCallId = new Map<string, SessionEntry>();
+ const callById = new Map<string, SessionEntry>();
for (const e of history) {
- if (e.type !== "tool_result") continue;
+ if (e.type !== "tool_result" && e.type !== "tool_call") continue;
const cid = (e.payload as { callId?: unknown } | null)?.callId;
- if (typeof cid === "string" && cid) resultByCallId.set(cid, e);
+ if (typeof cid === "string" && cid) (e.type === "tool_call" ? callById : resultByCallId).set(cid, e);
}
const stopped = stoppedToolCalls(history, resultByCallId);
const raw: PiReplayMessage[] = [];
@@ -448,10 +455,11 @@ export function reconstructMessagesFromHistory(
if (c.type !== "toolCall") continue;
const reason = stopped.reasons.get(c.id);
const re = resultByCallId.get(c.id);
- if (reason === undefined) out.push(toolResultMessage(c, re, m.timestamp));
- else if (!re) out.push(toolResultMessage(c, undefined, m.timestamp, stoppedToolResult(reason)));
+ if (reason === undefined) out.push(toolResultMessage(c, callById.get(c.id), re, m.timestamp));
+ else if (!re)
+ out.push(toolResultMessage(c, callById.get(c.id), undefined, m.timestamp, stoppedToolResult(reason)));
else {
- const result = toolResultMessage(c, re, m.timestamp);
+ const result = toolResultMessage(c, callById.get(c.id), re, m.timestamp);
out.push({ ...result, content: [...result.content, { type: "text", text: turnStoppedNote(reason) }] });
}
}
@@ -521,7 +529,6 @@ export function replayPreamble(history: SessionEntry[]): string {
}
} else if (e.type === "user" && textOf(e)) lines.push(`User: ${textOf(e)}`);
else if (e.type === "assistant" && textOf(e)) lines.push(`Assistant: ${textOf(e)}`);
- else if (e.type === "delivery" && textOf(e)) lines.push(deliveryNote(textOf(e)));
}
if (!lines.length) return "";
return [
diff --git a/src/memory/context-boundary.ts b/src/memory/context-boundary.ts
deleted file mode 100644
index b01e04f03..000000000
--- a/src/memory/context-boundary.ts
+++ /dev/null
@@ -1,58 +0,0 @@
-import { createHash } from "node:crypto";
-import type { Principal, ScopeId, SessionEntry } from "../types.ts";
-
-export interface MemoryContextSnapshot {
- audience: string;
-}
-
-interface MemoryContext {
- kind: "memory_context";
- snapshot: MemoryContextSnapshot;
- throughSeq: number;
-}
-
-export function memoryContextPayload(entry: SessionEntry): MemoryContext | null {
- let value = entry.type === "system" ? entry.payload : null;
- if (entry.type === "user") value = (entry.payload as { memoryContext?: unknown } | null)?.memoryContext;
- const p = value as Partial<MemoryContext> | null;
- return p?.kind === "memory_context" &&
- Number.isSafeInteger(p.throughSeq) &&
- p.throughSeq! >= -1 &&
- typeof p.snapshot?.audience === "string"
- ? (p as MemoryContext)
- : null;
-}
-
-function memoryContextFingerprint(value: unknown): string {
- return createHash("sha256").update(JSON.stringify(value)).digest("hex");
-}
-
-export function buildMemoryContextSnapshot(input: {
- targetScope: ScopeId;
- audience: readonly Principal[];
-}): MemoryContextSnapshot {
- return {
- audience: memoryContextFingerprint({
- scope: input.targetScope,
- audience: input.audience.map((person) => [person.id, person.type]).sort(),
- }),
- };
-}
-
-export function memoryBoundedEntries(entries: SessionEntry[]): SessionEntry[] {
- const boundary = entries.findLast((entry) => memoryContextPayload(entry));
- const through = boundary ? memoryContextPayload(boundary)!.throughSeq : -1;
- return entries.filter((entry) => entry.seq > through && !(entry.type === "system" && memoryContextPayload(entry)));
-}
-
-export function nextMemoryContext(
- entries: SessionEntry[],
- snapshot: MemoryContextSnapshot,
- latestSeq: number,
-): MemoryContext {
- const previous = entries.findLast((entry) => memoryContextPayload(entry));
- const payload = previous && memoryContextPayload(previous);
- let throughSeq = -1;
- if (payload) throughSeq = payload.snapshot.audience === snapshot.audience ? payload.throughSeq : latestSeq;
- return { kind: "memory_context", snapshot, throughSeq };
-}
diff --git a/src/resolution/context-filter.ts b/src/resolution/context-filter.ts
index 2fa637eee..d7d5bf922 100644
--- a/src/resolution/context-filter.ts
+++ b/src/resolution/context-filter.ts
@@ -1,4 +1,4 @@
-import type { Principal, ScopeId, SessionEntry } from "../types.ts";
+import type { Principal, ScopeId } from "../types.ts";
import { parseScopeId } from "../types.ts";
export function principalEntitledToScope(
@@ -15,15 +15,3 @@ export function principalEntitledToScope(
if (kind === "team") return (p.teamIds ?? []).includes(ref);
return false;
}
-
-export function filterHistoryForAudience(
- entries: SessionEntry[],
- audience: Principal[],
- sessionScopeId: ScopeId,
- orgScopeId: ScopeId,
-): SessionEntry[] {
- if (audience.length === 0) return [];
- return entries.filter((e) =>
- audience.every((p) => principalEntitledToScope(p, e.scopeLabel, sessionScopeId, orgScopeId)),
- );
-}
diff --git a/src/resolution/turn-context.ts b/src/resolution/turn-context.ts
index 3b2436bf6..9befd9faf 100644
--- a/src/resolution/turn-context.ts
+++ b/src/resolution/turn-context.ts
@@ -1,8 +1,6 @@
import { memoryRecallLabels } from "../memory/recall-labels.ts";
import type { DirectoryStore } from "../directory/directory-store.ts";
import type { ProjectStore } from "../projects/project-store.ts";
-import { isProjectGroupRef } from "../projects/project-store.ts";
-import { buildMemoryContextSnapshot } from "../memory/context-boundary.ts";
import { disclosedMemory, type MemoryDisclosure } from "../memory/disclosure.ts";
import { readNotebook, recallAcrossScopes, searchNotebooks } from "../memory/cross-scope.ts";
import { shownFacts } from "../memory/recall-delta.ts";
@@ -17,7 +15,7 @@ import type { AuditLog } from "../audit/audit-log.ts";
import type { MemoryService } from "../memory/memory-service.ts";
import { recallMemoryScopes, writableMemoryScope, type MemoryPolicy } from "../memory/policy.ts";
import type { SkillStore, GrantedSkillRef } from "../skills/skill-store.ts";
-import { parseScopeId, type Resolution, type ScopeId, type Principal } from "../types.ts";
+import { type Resolution, type ScopeId, type Principal } from "../types.ts";
import { carriedFileHandles, sharingSourcesForTurn } from "./sharing-access.ts";
import type { CurrentScopeMembers } from "./scope-membership.ts";
@@ -91,13 +89,6 @@ export async function resolveTurnContext(input: ContextInput) {
held: new Map(notebooks.map(({ scope, facts }) => [scope, shownFacts(facts ?? [])])),
facts: notebooks.flatMap(({ facts }) => facts ?? []),
complete: notebooks.every(({ facts }) => !!facts),
- snapshot: buildMemoryContextSnapshot({
- targetScope: input.targetScope,
- audience: isProjectGroupRef(parseScopeId(input.targetScope).ref ?? "")
- ? []
- : ((await input.currentScopeMembers?.(input.targetScope)) ??
- (parseScopeId(input.targetScope).kind === "channel" ? [] : input.audience)),
- }),
};
},
recallMemory: (query: string) =>
diff --git a/src/sessions/entry-search.ts b/src/sessions/entry-search.ts
index 15dfc11dd..64225b97c 100644
--- a/src/sessions/entry-search.ts
+++ b/src/sessions/entry-search.ts
@@ -2,7 +2,11 @@ import type { EntryType, SessionEntry } from "../types.ts";
import type { NewSearchEntry } from "./session-store.ts";
export const SEARCH_HIT_LIMIT = 40;
-export const SEARCHABLE_ENTRY_TYPES: ReadonlySet<EntryType> = new Set<EntryType>(["user", "assistant", "text"]);
+const SEARCHABLE_ENTRY_TYPES: ReadonlySet<EntryType> = new Set<EntryType>(["user", "assistant", "text"]);
+
+export function searchEntryType(entry: Pick<SessionEntry, "type" | "payload">): EntryType | null {
+ return SEARCHABLE_ENTRY_TYPES.has(entry.type) ? entry.type : null;
+}
const MAX_TERMS = 8;
/** Lowercased alphanumeric terms, in query order — the shared tokenizer for both stores. */
@@ -76,14 +80,15 @@ export function searchSnippet(text: string, terms: readonly string[], max = 240)
export function searchRowsFromEntries(entries: readonly SessionEntry[], sinceSeq: number): NewSearchEntry[] {
return entries.flatMap((entry) => {
- if (entry.seq <= sinceSeq || !SEARCHABLE_ENTRY_TYPES.has(entry.type)) return [];
+ const type = searchEntryType(entry);
+ if (entry.seq <= sinceSeq || !type) return [];
const text = entrySearchText(entry.payload);
if (!text || !text.trim()) return [];
const author = entrySearchAuthor(entry);
return [
{
seq: entry.seq,
- type: entry.type,
+ type,
...(author ? { author } : {}),
text,
createdAt: entry.createdAt,
diff --git a/src/sessions/items-cutover.ts b/src/sessions/items-cutover.ts
new file mode 100644
index 000000000..aec1b32df
--- /dev/null
+++ b/src/sessions/items-cutover.ts
@@ -0,0 +1,65 @@
+import { msOf, SEARCHABLE_ITEM, searchTypeExpr, USER_KINDS } from "./items.ts";
+import { ITEM_INDEXES, itemIndexSql } from "./items-schema.ts";
+
+export const ITEMS_CUTOVER_SQL: readonly string[] = [
+ `SET LOCAL lock_timeout = '60s'`,
+ `DO $index_guard$
+ DECLARE unready text;
+ BEGIN
+ SELECT string_agg(name, ', ') INTO unready
+ FROM unnest(ARRAY[${ITEM_INDEXES.map((index) => `'${index.name}'`).join(", ")}]) AS name
+ WHERE NOT EXISTS (SELECT 1 FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid
+ WHERE c.relname = name AND c.relnamespace = current_schema()::regnamespace AND i.indisvalid);
+ IF unready IS NOT NULL AND EXISTS (SELECT 1 FROM items) THEN
+ RAISE EXCEPTION 'items cutover: build the items indexes with scripts/ops/items-copy.ts first; missing or invalid: %', unready;
+ END IF;
+ END $index_guard$`,
+ ...ITEM_INDEXES.map((index) => itemIndexSql(index)),
+ `CREATE OR REPLACE FUNCTION qm_tenant_id() RETURNS uuid LANGUAGE sql STABLE PARALLEL SAFE
+ AS $qm_tenant_id$ SELECT id FROM qm_tenant $qm_tenant_id$`,
+ `CREATE OR REPLACE FUNCTION forbid_mutation() RETURNS trigger LANGUAGE plpgsql AS $forbid_mutation$
+ BEGIN
+ RAISE EXCEPTION '% on % is forbidden', TG_OP, TG_TABLE_NAME USING ERRCODE = 'integrity_constraint_violation';
+ END $forbid_mutation$`,
+ `CREATE OR REPLACE FUNCTION forbid_direct_delete() RETURNS trigger LANGUAGE plpgsql AS $forbid_direct_delete$
+ BEGIN
+ IF pg_trigger_depth() > 1 THEN RETURN OLD; END IF;
+ RAISE EXCEPTION 'DELETE on % is only allowed through its session', TG_TABLE_NAME
+ USING ERRCODE = 'integrity_constraint_violation';
+ END $forbid_direct_delete$`,
+ `CREATE TRIGGER items_append_only BEFORE UPDATE ON items FOR EACH ROW EXECUTE FUNCTION forbid_mutation()`,
+ `CREATE TRIGGER items_no_truncate BEFORE TRUNCATE ON items FOR EACH STATEMENT EXECUTE FUNCTION forbid_mutation()`,
+ `CREATE TRIGGER items_delete_through_session BEFORE DELETE ON items
+ FOR EACH ROW EXECUTE FUNCTION forbid_direct_delete()`,
+ `CREATE TRIGGER blobs_immutable BEFORE UPDATE OR DELETE ON blobs FOR EACH ROW EXECUTE FUNCTION forbid_mutation()`,
+ `CREATE TRIGGER blobs_no_truncate BEFORE TRUNCATE ON blobs FOR EACH STATEMENT EXECUTE FUNCTION forbid_mutation()`,
+ `CREATE OR REPLACE FUNCTION sync_item_search() RETURNS trigger
+ LANGUAGE plpgsql AS $sync_item_search$
+ BEGIN
+ IF TG_OP = 'DELETE' THEN
+ DELETE FROM session_entry_search WHERE session_id = OLD.session_id AND seq = OLD.seq;
+ RETURN OLD;
+ END IF;
+ IF ${SEARCHABLE_ITEM("NEW")} AND btrim(NEW.text) <> '' THEN
+ INSERT INTO session_entry_search(session_id, seq, type, author, text, created_at)
+ VALUES (NEW.session_id, NEW.seq, ${searchTypeExpr("NEW")},
+ CASE WHEN NEW.kind IN ${USER_KINDS} AND jsonb_typeof(NEW.body -> 'name') = 'string'
+ THEN NEW.body ->> 'name' END,
+ NEW.text, ${msOf("NEW.created_at")})
+ ON CONFLICT (session_id, seq) DO NOTHING;
+ END IF;
+ RETURN NEW;
+ END $sync_item_search$`,
+ `CREATE TRIGGER session_entry_search AFTER INSERT OR DELETE ON items
+ FOR EACH ROW EXECUTE FUNCTION sync_item_search()`,
+ `DROP TRIGGER IF EXISTS session_entries_search_write_through ON session_entries`,
+ `DROP FUNCTION IF EXISTS sync_session_entry_search()`,
+ `DROP TRIGGER IF EXISTS participant_activity_boundary_insert_v3 ON participants`,
+ `DROP TRIGGER IF EXISTS participant_activity_reopen_v3 ON participants`,
+ `DROP TRIGGER IF EXISTS participant_activity_entry_v3 ON session_entries`,
+ `DROP TRIGGER IF EXISTS session_entry_boundary_v1 ON session_entries`,
+ `DROP FUNCTION IF EXISTS participant_activity_on_insert_v3(), participant_activity_on_reopen_v3(),
+ participant_activity_on_entry_v3(), session_entry_boundary_v1()`,
+ `ALTER TABLE session_entries RENAME TO legacy_session_entries`,
+ `ANALYZE items`,
+];
diff --git a/src/sessions/items-schema.ts b/src/sessions/items-schema.ts
index 5d6f07bdd..38d883321 100644
--- a/src/sessions/items-schema.ts
+++ b/src/sessions/items-schema.ts
@@ -17,9 +17,9 @@ const ITEM_KINDS = [
"RuntimeChange",
] as const;
-type ItemKind = (typeof ITEM_KINDS)[number];
+export type ItemKind = (typeof ITEM_KINDS)[number];
-const WAKE_CAUSES = ["cron", "webhook", "child_done", "system"] as const;
+export const WAKE_CAUSES = ["cron", "webhook", "child_done", "system"] as const;
const sqlList = (values: readonly string[]): string => values.map((value) => `'${value}'`).join(", ");
diff --git a/src/sessions/items.ts b/src/sessions/items.ts
new file mode 100644
index 000000000..399821dda
--- /dev/null
+++ b/src/sessions/items.ts
@@ -0,0 +1,249 @@
+import type { EntryType, ScopeId, SessionEntry } from "../types.ts";
+import { isObj } from "../util/objects.ts";
+import { type ItemKind, WAKE_CAUSES } from "./items-schema.ts";
+
+type WakeCause = (typeof WAKE_CAUSES)[number];
+
+export interface ItemRow {
+ kind: ItemKind;
+ turnId: string | null;
+ author: string | null;
+ callId: string | null;
+ elicitId: string | null;
+ wakeCause: WakeCause | null;
+ errorCode: string | null;
+ text: string | null;
+ tool: string | null;
+ step: number | null;
+ variant: string | null;
+ isError: boolean | null;
+ hidden: boolean | null;
+ externalId: string | null;
+ throughSeq: number | null;
+ targetSeq: number | null;
+ approved: boolean | null;
+ blobSha: string | null;
+ body: Record<string, unknown>;
+}
+
+export const ITEM_COLUMNS = [
+ ["turn_id", "turnId", "uuid"],
+ ["kind", "kind", "item_kind"],
+ ["author", "author", "text"],
+ ["call_id", "callId", "text"],
+ ["elicit_id", "elicitId", "text"],
+ ["wake_cause", "wakeCause", "wake_cause"],
+ ["error_code", "errorCode", "text"],
+ ["text", "text", "text"],
+ ["tool", "tool", "text"],
+ ["step", "step", "int"],
+ ["variant", "variant", "text"],
+ ["is_error", "isError", "boolean"],
+ ["hidden", "hidden", "boolean"],
+ ["external_id", "externalId", "text"],
+ ["through_seq", "throughSeq", "bigint"],
+ ["target_seq", "targetSeq", "bigint"],
+ ["approved", "approved", "boolean"],
+ ["blob_sha", "blobSha", "text"],
+ ["body", "body", "jsonb"],
+] as const;
+
+export interface ItemContext {
+ turnId?: string | null;
+ author?: string | null;
+}
+
+const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
+const str = (value: unknown): string | null => (typeof value === "string" && value !== "" ? value : null);
+const AUTHORED_KINDS: ReadonlySet<ItemKind> = new Set([
+ "UserMessage",
+ "SteeringMessage",
+ "ElicitationResponse",
+ "Stop",
+ "RuntimeChange",
+]);
+const TEXT_KEYS: Partial<Record<ItemKind, string>> = {
+ UserMessage: "text",
+ SteeringMessage: "text",
+ AgentMessage: "text",
+ ContextCompaction: "text",
+ Reasoning: "thinking",
+ ToolResult: "result",
+ Error: "message",
+};
+const TYPES: Partial<Record<EntryType, ItemKind>> = {
+ user: "UserMessage",
+ assistant: "AgentMessage",
+ text: "AgentMessage",
+ delivery: "AgentMessage",
+ thinking: "Reasoning",
+ tool_call: "ToolCall",
+ tool_result: "ToolResult",
+ approval_request: "Elicitation",
+ approval_resolved: "ElicitationResponse",
+};
+const SYSTEM_KINDS: Record<string, ItemKind> = {
+ context_summary: "ContextCompaction",
+ turn_failure: "Error",
+ turn_stopped: "Stop",
+ message_revision: "Retraction",
+ wake: "Wake",
+ goal_cancelled: "Wake",
+};
+
+export function itemFromEntry(type: EntryType, payload: unknown, context: ItemContext = {}): ItemRow {
+ let raw: Record<string, unknown> = {};
+ if (isObj(payload) && !Array.isArray(payload)) raw = payload;
+ else if (typeof payload === "string") raw = { text: payload };
+ else if (payload != null) raw = { value: payload };
+ const body = { ...raw };
+ let kind = TYPES[type];
+ if (type === "system") kind = SYSTEM_KINDS[String(raw.kind)];
+ else if (type === "user" && raw.steered === true) kind = "SteeringMessage";
+ if (!kind) throw new Error(`items: entry type ${type}/${String(raw.kind)} is not stored`);
+ const turnId = context.turnId ?? str(raw.runId);
+ const row: ItemRow = {
+ kind,
+ turnId: turnId && UUID.test(turnId) ? turnId.toLowerCase() : null,
+ author: AUTHORED_KINDS.has(kind) ? str(context.author) : null,
+ callId: null,
+ elicitId: null,
+ wakeCause: null,
+ errorCode: null,
+ text: null,
+ tool: null,
+ step: null,
+ variant: ({ text: "narration", delivery: "files_delivered" } as Partial<Record<EntryType, string>>)[type] ?? null,
+ isError: null,
+ hidden: null,
+ externalId: null,
+ throughSeq: null,
+ targetSeq: null,
+ approved: null,
+ blobSha: null,
+ body,
+ };
+ const take = (key: string): unknown => {
+ const value = body[key];
+ delete body[key];
+ return value;
+ };
+ const required = (key: string): string => {
+ const value = str(take(key));
+ if (!value) throw new Error(`items: ${type} entry has no ${key}`);
+ return value;
+ };
+ const textKey = TEXT_KEYS[kind];
+ if (textKey && typeof raw[textKey] === "string") row.text = str(take(textKey));
+ if (kind === "ToolCall" || kind === "ToolResult") {
+ row.callId = required("callId");
+ row.tool = str(take("tool"));
+ }
+ if (kind === "ToolResult") {
+ if (typeof body.isError === "boolean") row.isError = take("isError") as boolean;
+ for (const key of ["stdout", "stderr"])
+ if (typeof body[key] === "string" && typeof raw.result === "string" && raw.result.includes(body[key])) take(key);
+ }
+ if (kind === "Elicitation" || kind === "ElicitationResponse") row.elicitId = required("requestId");
+ if (kind === "ElicitationResponse" && typeof body.approved === "boolean") row.approved = take("approved") as boolean;
+ if (kind === "UserMessage") {
+ if (typeof body.hidden === "boolean") row.hidden = take("hidden") as boolean;
+ if (typeof body.ts === "string") row.externalId = str(take("ts"));
+ }
+ if (type === "user") take("steered");
+ if (type === "system" && raw.kind !== "goal_cancelled") take("kind");
+ if (kind === "Wake") {
+ const cause = take("cause") ?? "system";
+ row.wakeCause = WAKE_CAUSES.find((v) => v === cause) ?? null;
+ if (!row.wakeCause) throw new Error(`items: wake cause ${String(cause)} is not a wake_cause`);
+ }
+ if (kind === "Error") row.errorCode = "turn_failure";
+ if (kind === "ContextCompaction" && typeof body.throughSeq === "number")
+ row.throughSeq = take("throughSeq") as number;
+ if (row.turnId) take("runId");
+ if (["ToolCall", "ToolResult", "Reasoning", "AgentMessage"].includes(kind) && isObj(body.step)) {
+ const { index, ...message } = body.step;
+ row.step = typeof index === "number" ? index : null;
+ body.wire = { ...(isObj(body.wire) ? body.wire : {}), message };
+ take("step");
+ }
+ if (kind === "ToolResult" && isObj(body.output) && typeof body.output.blob === "string") {
+ row.blobSha = body.output.blob;
+ const { blob: _blob, ...output } = body.output;
+ body.output = output;
+ }
+ take("securityTainted");
+ if (type === "assistant" || kind === "Error") take("deliveryKey");
+ return row;
+}
+
+const ENTRY_TYPES: Partial<Record<ItemKind, EntryType>> = {
+ UserMessage: "user",
+ SteeringMessage: "user",
+ AgentMessage: "assistant",
+ Reasoning: "thinking",
+ ToolCall: "tool_call",
+ ToolResult: "tool_result",
+ Elicitation: "approval_request",
+ ElicitationResponse: "approval_resolved",
+};
+
+export function entryFromItem(
+ row: ItemRow & { sessionId: string; seq: number; scopeLabel: ScopeId; createdAt: number },
+): SessionEntry {
+ const payload = { ...row.body };
+ if (row.blobSha) payload.output = { ...(isObj(payload.output) ? payload.output : {}), blob: row.blobSha };
+ const put = (key: string, value: unknown): void => {
+ if (value != null) payload[key] = value;
+ };
+ let type = ENTRY_TYPES[row.kind] ?? "system";
+ if (row.kind === "AgentMessage") {
+ if (row.variant === "narration") type = "text";
+ else if (row.variant === "files_delivered") type = "delivery";
+ }
+ const textKey = TEXT_KEYS[row.kind];
+ if (textKey && (row.text !== null || !(textKey in payload))) put(textKey, row.text ?? "");
+ put("runId", row.turnId);
+ put("callId", row.callId);
+ put("tool", row.tool);
+ put("requestId", row.elicitId);
+ put("isError", row.isError);
+ put("hidden", row.hidden);
+ put("ts", row.externalId);
+ put("throughSeq", row.throughSeq);
+ put("approved", row.approved);
+ if (row.kind === "SteeringMessage") payload.steered = true;
+ if (type === "system") {
+ const systemKinds: Partial<Record<ItemKind, string>> = {
+ ContextCompaction: "context_summary",
+ Retraction: "message_revision",
+ Error: "turn_failure",
+ Stop: "turn_stopped",
+ };
+ payload.kind = systemKinds[row.kind] ?? (payload.kind === "goal_cancelled" ? "goal_cancelled" : "wake");
+ }
+ put("cause", row.wakeCause);
+ if (row.step != null || (isObj(payload.wire) && isObj(payload.wire.message))) {
+ payload.step = {
+ ...(isObj(payload.wire) && isObj(payload.wire.message) ? payload.wire.message : {}),
+ ...(row.step == null ? {} : { index: row.step }),
+ };
+ }
+ return {
+ sessionId: row.sessionId,
+ seq: row.seq,
+ parentSeq: row.seq === 0 ? null : row.seq - 1,
+ type,
+ payload,
+ scopeLabel: row.scopeLabel,
+ createdAt: row.createdAt,
+ };
+}
+
+export const USER_KINDS = "('UserMessage', 'SteeringMessage')";
+export const AGENT_REPLY_ITEM = (col: string): string => `${col}.kind = 'AgentMessage'`;
+export const SEARCHABLE_ITEM = (col: string): string =>
+ `(${col}.kind IN ('UserMessage','SteeringMessage','AgentMessage') AND ${col}.variant IS DISTINCT FROM 'files_delivered' AND ${col}.text IS NOT NULL)`;
+export const searchTypeExpr = (col: string): string =>
+ `CASE WHEN ${col}.kind IN ${USER_KINDS} THEN 'user' WHEN ${col}.variant = 'narration' THEN 'text' ELSE 'assistant' END`;
+export const msOf = (col: string): string => `(EXTRACT(EPOCH FROM ${col}) * 1000)::bigint`;
diff --git a/src/sessions/memory-session-store.ts b/src/sessions/memory-session-store.ts
index 3cffb9157..8781ecc72 100644
--- a/src/sessions/memory-session-store.ts
+++ b/src/sessions/memory-session-store.ts
@@ -29,7 +29,7 @@ import {
entrySearchAuthor,
entrySearchText,
matchesSearchTerms,
- SEARCHABLE_ENTRY_TYPES,
+ searchEntryType,
searchTerms,
} from "./entry-search.ts";
import {
@@ -37,6 +37,7 @@ import {
cronIdOf,
entryWithinTenure,
isOverheardEntry,
+ DuplicateCallError,
promptEnvelopeBody,
sessionBucket,
sessionCategory,
@@ -44,6 +45,7 @@ import {
userMessagePreview,
isWebThread,
} from "./session-store.ts";
+import { entryFromItem, itemFromEntry } from "./items.ts";
import { SECURITY_SCREEN_STEP, screenPayloadFromEnvelope } from "../security/security-posture.ts";
function toParticipantWindow(
@@ -108,7 +110,7 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
const log = all.filter((e) => e.type === "user");
const lastActivityAt = log.length ? Math.max(s.createdAt, ...log.map((e) => e.createdAt)) : s.createdAt;
const visible = view ? all.some((e) => entryWithinTenure(e, view)) : false;
- const replies = all.filter((e) => e.type === "assistant" || e.type === "delivery");
+ const replies = all.filter((e) => e.type === "assistant" || e.type === "text");
const lastReply = replies.length ? Math.max(...replies.map((e) => e.createdAt)) : null;
const unread = lastReply !== null && lastReply > (view?.readAt ?? 0) && isWebThread(s.threadRef);
return {
@@ -267,25 +269,42 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
const log = entries.get(lease.sessionId);
if (!log) throw new Error(`unknown session: ${lease.sessionId}`);
const copied = structuredClone(batch);
- return copied.map((entry) => {
+ const scopeLabel = sessions.get(lease.sessionId)?.scopeId ?? (copied[0]?.scopeLabel as ScopeId);
+ const items = copied.map((entry) =>
+ itemFromEntry(entry.type, entry.payload, { turnId: lease.turnId, author: entry.author }),
+ );
+ const calls = new Set(
+ log.flatMap((e) => {
+ const id = (e.payload as { callId?: unknown } | null)?.callId;
+ return (e.type === "tool_call" || e.type === "tool_result") && typeof id === "string"
+ ? [`${e.type}:${id}`]
+ : [];
+ }),
+ );
+ for (const [index, item] of items.entries()) {
+ if (!item.callId) continue;
+ const key = `${copied[index]!.type}:${item.callId}`;
+ if (calls.has(key)) throw new DuplicateCallError(lease.sessionId, [item.callId]);
+ calls.add(key);
+ }
+ return items.map((item) => {
const seq = log.length;
- const full: SessionEntry = {
+ const full = entryFromItem({
sessionId: lease.sessionId,
seq,
- parentSeq: seq === 0 ? null : seq - 1,
- type: entry.type,
- payload: entry.payload,
- scopeLabel: entry.scopeLabel as ScopeId,
+ ...item,
+ scopeLabel,
createdAt: now(),
- };
+ });
log.push(full);
- const text = SEARCHABLE_ENTRY_TYPES.has(full.type) ? entrySearchText(full.payload) : null;
- if (text?.trim()) {
+ const searchType = searchEntryType(full);
+ const text = searchType ? entrySearchText(full.payload) : null;
+ if (searchType && text?.trim()) {
const index = searchIndex.get(full.sessionId) ?? [];
const author = entrySearchAuthor(full);
index.push({
seq: full.seq,
- type: full.type,
+ type: searchType,
text,
createdAt: full.createdAt,
...(author ? { author } : {}),
@@ -307,7 +326,9 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
},
async getEntries(sessionId, opts?: GetEntriesOptions) {
- const all = entries.get(sessionId) ?? [];
+ const all = (entries.get(sessionId) ?? []).filter(
+ (e) => opts?.turnId === undefined || (e.payload as { runId?: string } | null)?.runId === opts.turnId,
+ );
const log = opts?.transcript ? transcriptEntries(all) : all;
const since = opts?.sinceSeq ?? 0;
const filtered = log.filter((e) => e.seq >= since && (opts?.beforeSeq === undefined || e.seq < opts.beforeSeq));
@@ -567,7 +588,6 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
if (!matchesSearchTerms(row.text, terms)) continue;
hits.push({
sessionId,
- entryScopeLabel: entry.scopeLabel,
scopeId: session.scopeId,
...((win.title ?? session.title) != null ? { title: win.title ?? session.title } : {}),
...(session.channelName ? { channelName: session.channelName } : {}),
@@ -610,8 +630,7 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
async missingSearchEntries(sessionId): Promise<number> {
const indexed = new Set((searchIndex.get(sessionId) ?? []).map((row) => row.seq));
return (entries.get(sessionId) ?? []).filter(
- (entry) =>
- SEARCHABLE_ENTRY_TYPES.has(entry.type) && entrySearchText(entry.payload)?.trim() && !indexed.has(entry.seq),
+ (entry) => searchEntryType(entry) && entrySearchText(entry.payload)?.trim() && !indexed.has(entry.seq),
).length;
},
@@ -619,7 +638,7 @@ export function createMemorySessionStore(opts: StoreOptions = {}): SessionStore
const log = entries.get(sessionId) ?? [];
for (let i = log.length - 1; i >= 0; i--) {
const e = log[i]!;
- if (!SEARCHABLE_ENTRY_TYPES.has(e.type)) continue;
+ if (!searchEntryType(e)) continue;
const text = entrySearchText(e.payload);
if (text && text.trim()) return e.seq;
}
diff --git a/src/sessions/postgres-session-store.ts b/src/sessions/postgres-session-store.ts
index 695dc7141..c8868c8b0 100644
--- a/src/sessions/postgres-session-store.ts
+++ b/src/sessions/postgres-session-store.ts
@@ -1,5 +1,6 @@
import { DIRECTORY_MIGRATIONS } from "../directory/postgres-directory-store.ts";
import { randomUUID } from "node:crypto";
+import { jsonbStringify } from "../persistence/durable-map.ts";
import {
createPgPool,
type PgPool,
@@ -58,7 +59,20 @@ import {
threadRefCronIdExpr,
userMessagePreview,
isWebThread,
+ isOverheardEntry,
+ DuplicateCallError,
} from "./session-store.ts";
+import {
+ entryFromItem,
+ itemFromEntry,
+ ITEM_COLUMNS,
+ type ItemRow,
+ msOf,
+ AGENT_REPLY_ITEM,
+ SEARCHABLE_ITEM,
+ USER_KINDS,
+} from "./items.ts";
+import { ITEMS_CUTOVER_SQL } from "./items-cutover.ts";
import { SECURITY_SCREEN_STEP, screenPayloadFromEnvelope } from "../security/security-posture.ts";
const threadRefOriginExpr = (threadRef: string): string =>
@@ -168,17 +182,25 @@ function rowToParticipantWindow(r: Record<string, unknown>): ParticipantWindow {
}
function rowToEntry(r: Record<string, unknown>): SessionEntry {
- return {
+ return entryFromItem({
sessionId: r.session_id as string,
seq: Number(r.seq),
- parentSeq: r.parent_seq === null ? null : Number(r.parent_seq),
- type: r.type as SessionEntry["type"],
- payload: r.payload != null ? JSON.parse(r.payload as string) : null,
+ ...(Object.fromEntries(
+ ITEM_COLUMNS.map(([, key, type]) => [
+ key,
+ type === "bigint" && r[key] != null ? Number(r[key]) : (r[key] ?? null),
+ ]),
+ ) as unknown as ItemRow),
scopeLabel: r.scope_label as ScopeId,
createdAt: Number(r.created_at),
- };
+ });
}
+const ITEM_FIELDS = `i.session_id, i.seq, ${ITEM_COLUMNS.map(([column, key]) => `${column === "blob_sha" ? "encode(i.blob_sha, 'hex')" : `i.${column}`} AS "${key}"`).join(",")}, ts.scope_id AS scope_label, ${msOf("i.created_at")} AS created_at`;
+const ITEM_FROM = "items i JOIN sessions ts ON ts.id = i.session_id";
+const TENANT = "qm_tenant_id()";
+const tsOf = (ms: string): string => `to_timestamp((${ms})::double precision / 1000)`;
+
function rowToSpendRow(r: Record<string, unknown>): SpendRow {
return {
day: Number(r.day),
@@ -205,47 +227,40 @@ const LAST_PREVIEW_LEN = 100;
const storedPreview = (payload: unknown, maxLen: number): string => pgTextSafe(userMessagePreview(payload, maxLen));
-function parsedPayload(raw: unknown): unknown {
- if (typeof raw !== "string") return null;
- try {
- return JSON.parse(raw);
- } catch {
- return null;
- }
-}
-
export function createPostgresSessionStore(connectionString: string, opts: StoreOptions = {}): SessionStore {
const now = opts.now ?? (() => Date.now());
const leaseTtlMs = opts.leaseTtlMs ?? 5 * 60_000;
- const notOverheard = (col: string): string =>
- `(${col}.payload IS NULL OR ${col}.payload NOT LIKE '%"overheard":true%')`;
- const userTurn = (col: string): string => `${col}.type = 'user' AND ${notOverheard(col)}`;
+ const userTurn = (col: string): string =>
+ `${col}.kind IN ${USER_KINDS} AND NOT (${col}.body @> '{"overheard":true}')`;
const lastActivityExpr = (col: string): string => `COALESCE(${col}.last_activity, ${col}.created_at)`;
- const participantWindowBounds = (entry: string, participant: string) =>
- [
+ const participantWindowBounds = (entry: string, participant: string, item = true) => {
+ const at = (ms: string): string => (item ? tsOf(ms) : ms);
+ return [
[
`(${participant}.valid_from_seq IS NOT NULL AND ${entry}.seq >= ${participant}.valid_from_seq)`,
- `(${participant}.valid_from_seq IS NULL AND ${entry}.created_at >= ${participant}.valid_from)`,
+ `(${participant}.valid_from_seq IS NULL AND ${entry}.created_at >= ${at(`${participant}.valid_from`)})`,
],
[
`(${participant}.valid_to_seq IS NOT NULL AND ${entry}.seq < ${participant}.valid_to_seq)`,
- `(${participant}.valid_to_seq IS NULL AND ${participant}.valid_to IS NOT NULL AND ${entry}.created_at < ${participant}.valid_to)`,
+ `(${participant}.valid_to_seq IS NULL AND ${participant}.valid_to IS NOT NULL AND ${entry}.created_at < ${at(`${participant}.valid_to`)})`,
`(${participant}.valid_to_seq IS NULL AND ${participant}.valid_to IS NULL)`,
],
] as const;
- const withinParticipantWindow = (entry: string, participant: string): string =>
- participantWindowBounds(entry, participant)
+ };
+ const withinParticipantWindow = (entry: string, participant: string, item = true): string =>
+ participantWindowBounds(entry, participant, item)
.map((bounds) => `(${bounds.join(" OR ")})`)
.join(" AND ");
- function transcriptEntryPredicate(alias: string, participant?: string): string {
- return `${alias}.type <> 'soul' AND (
- ${alias}.payload::jsonb -> 'slackReplyReceipt' IS DISTINCT FROM 'true'::jsonb
- OR NOT EXISTS (
- SELECT 1 FROM session_entries received
- WHERE received.session_id = ${alias}.session_id AND received.type = 'user'
- AND received.payload::jsonb ->> 'ts' = ${alias}.payload::jsonb ->> 'ts'
- AND received.payload::jsonb -> 'slackReplyReceipt' IS DISTINCT FROM 'true'::jsonb
+ function transcriptItemPredicate(alias: string, participant?: string): string {
+ const receipt = (item: string): string => `COALESCE(${item}.body @> '{"slackReplyReceipt":true}'::jsonb, false)`;
+ return `NOT (
+ ${alias}.kind = 'UserMessage' AND ${receipt(alias)}
+ AND EXISTS (
+ SELECT 1 FROM items received
+ WHERE received.tenant_id = ${alias}.tenant_id AND received.session_id = ${alias}.session_id
+ AND received.kind = 'UserMessage' AND received.external_id = ${alias}.external_id
+ AND NOT ${receipt("received")}
${participant ? `AND ${withinParticipantWindow("received", participant)}` : ""}
)
)`;
@@ -253,16 +268,18 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
const [fromBounds, toBounds] = participantWindowBounds("x", "p");
const participantPresenceSql = fromBounds
.flatMap((from) =>
- toBounds.map((to) => `SELECT 1 FROM session_entries x WHERE x.session_id = s.id AND ${from} AND ${to}`),
+ toBounds.map(
+ (to) => `SELECT 1 FROM items x WHERE x.tenant_id = ${TENANT} AND x.session_id = s.id AND ${from} AND ${to}`,
+ ),
)
.join(" UNION ALL ");
const participantSessionsSql = (extraWhere: string): string =>
`SELECT s.*, p.title AS p_title, p.archived AS p_archived, p.pinned AS p_pinned, p.color AS p_color,
p.read_at AS p_read_at,
- (SELECT MAX(e.created_at) FROM session_entries e
- WHERE e.session_id = s.id AND e.type IN ('assistant', 'delivery')) AS agent_last_reply,
- COALESCE((SELECT MAX(e.created_at) FROM session_entries e
- WHERE e.session_id = s.id AND e.type = 'user'), s.created_at) AS user_last_activity,
+ (SELECT ${msOf("MAX(e.created_at)")} FROM items e
+ WHERE e.tenant_id = ${TENANT} AND e.session_id = s.id AND ${AGENT_REPLY_ITEM("e")}) AS agent_last_reply,
+ COALESCE((SELECT ${msOf("MAX(e.created_at)")} FROM items e
+ WHERE e.tenant_id = ${TENANT} AND e.session_id = s.id AND e.kind IN ${USER_KINDS}), s.created_at) AS user_last_activity,
EXISTS (${participantPresenceSql}) AS has_entries
FROM sessions s
JOIN participants p ON p.session_id = s.id
@@ -288,12 +305,6 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
const hasOrigin = (alias: string, origin: SessionOrigin): string => `${originExpr(alias)} = '${origin}'`;
const originFilterClause = (alias: string, origin: SessionOriginFilter): string =>
origin === "other_background" ? `${originExpr(alias)} NOT IN ('conversation', 'cron')` : hasOrigin(alias, origin);
- const previewExpr = (col: string): string =>
- `(SELECT CASE WHEN json_typeof(j -> 'text') = 'string' THEN j ->> 'text'
- WHEN json_typeof(j) = 'string' THEN j #>> '{}'
- ELSE NULL END
- FROM (SELECT safe_json(replace(${col}, '\\u0000', '')) AS j) _)`;
-
const spendSql = `WITH RECURSIVE ancestry AS (
SELECT s.id AS session_id, s.parent_session_id, ${originExpr("s")} AS origin, ARRAY[s.id] AS path
FROM sessions s
@@ -884,6 +895,10 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
statements: [`ALTER TABLE sessions ADD COLUMN IF NOT EXISTS client_tools JSONB`],
},
itemsMigration,
+ {
+ id: "sessions/store/0036-items-cutover",
+ statements: ITEMS_CUTOVER_SQL,
+ },
],
[
{
@@ -932,61 +947,76 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
client: PoolClient,
sessionId: string,
entries: readonly NewEntry[],
+ turnId?: string,
): Promise<SessionEntry[]> => {
if (!entries.length) return [];
const max = await client.query(
- "SELECT COALESCE(MAX(seq), -1) + 1 AS n FROM session_entries WHERE session_id = $1",
+ `SELECT GREATEST(
+ COALESCE((SELECT MAX(seq) FROM items WHERE tenant_id = ${TENANT} AND session_id = $1), -1) + 1,
+ COALESCE((SELECT messages FROM sessions WHERE id = $1), 0)) AS n`,
[sessionId],
);
const startSeq = Number(max.rows[0]!.n);
const createdAt = now();
- const full = entries.map((entry, index): SessionEntry => {
+ const items = entries.map((entry, index) => {
const seq = startSeq + index;
+ const item = itemFromEntry(entry.type, JSON.parse(jsonbSafeStringify(entry.payload ?? null)), {
+ turnId: turnId ?? null,
+ author: entry.author ?? null,
+ });
return {
- sessionId,
+ ...item,
seq,
- parentSeq: seq === 0 ? null : seq - 1,
- type: entry.type,
- payload: JSON.parse(jsonbSafeStringify(entry.payload ?? null)),
scopeLabel: entry.scopeLabel as ScopeId,
- createdAt,
};
});
- const rows = full.map((entry) => ({
- seq: entry.seq,
- parent_seq: entry.parentSeq,
- type: entry.type,
- payload: JSON.stringify(entry.payload),
- scope_label: entry.scopeLabel,
- created_at: entry.createdAt,
- }));
- const encoded = JSON.stringify(rows);
- const userTurns = full.filter(
- (entry, index) => entry.type === "user" && !rows[index]!.payload.includes('"overheard":true'),
- );
+ for (const item of items) {
+ if (item.kind !== "Retraction" || typeof item.body.ts !== "string") continue;
+ const inBatch = items.findLast(
+ (other) => other.seq < item.seq && other.kind === "UserMessage" && other.externalId === item.body.ts,
+ );
+ const {
+ rows: [prior],
+ } = await client.query(
+ `SELECT max(seq) AS seq FROM items WHERE tenant_id=${TENANT} AND session_id=$1 AND external_id=$2 AND seq<$3`,
+ [sessionId, item.body.ts, item.seq],
+ );
+ item.targetSeq = inBatch?.seq ?? (prior.seq == null ? null : Number(prior.seq));
+ }
+ const full = items.map((item) => entryFromItem({ sessionId, createdAt, ...item }));
+ const userTurns = full.filter((entry) => entry.type === "user" && !isOverheardEntry(entry));
const firstUser = userTurns[0];
const lastUser = userTurns[userTurns.length - 1];
- await client.query(
- `INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at)
- SELECT $1, e.seq, e.parent_seq, e.type, e.payload, e.scope_label, e.created_at
- FROM jsonb_to_recordset($2::jsonb) AS e(
- seq bigint, parent_seq bigint, type text, payload text, scope_label text, created_at bigint)
- ORDER BY e.seq`,
- [sessionId, encoded],
- );
+ await client
+ .query(
+ `INSERT INTO items(tenant_id, session_id, seq, ${ITEM_COLUMNS.map(([column]) => column).join(",")}, created_at)
+ SELECT ${TENANT}, $1, i.seq, ${ITEM_COLUMNS.map(([column, key]) => (column === "blob_sha" ? `decode(i."${key}", 'hex')` : `i."${key}"`)).join(",")}, ${tsOf("$3::bigint")}
+ FROM jsonb_to_recordset($2::jsonb) AS i(seq bigint, ${ITEM_COLUMNS.map(([, key, type]) => `"${key}" ${type}`).join(",")})
+ ORDER BY i.seq`,
+ [sessionId, jsonbStringify(items), createdAt],
+ )
+ .catch((err: unknown) => {
+ const e = err as { code?: unknown; constraint?: unknown };
+ if (e?.code !== "23505" || e.constraint !== "items_one_call_per_kind") throw err;
+ throw new DuplicateCallError(
+ sessionId,
+ items.flatMap((item) => (item.callId ? [item.callId] : [])),
+ { cause: err },
+ );
+ });
await client.query(
`UPDATE sessions
- SET last_activity = CASE WHEN last_activity >= $2::bigint - ${LAST_ACTIVITY_DEBOUNCE_MS}
- THEN last_activity
- ELSE GREATEST(COALESCE(last_activity, 0), $2::bigint) END,
- messages = $3,
- turns = CASE WHEN turns IS NULL OR messages IS DISTINCT FROM $5
- THEN (SELECT COUNT(*) FROM session_entries t WHERE t.session_id = $1 AND ${userTurn("t")})
- ELSE turns + $4 END,
- first_user_preview = CASE WHEN first_user_preview IS NULL AND turns = 0 AND messages = $5
- THEN $6 ELSE first_user_preview END,
- last_user_preview = COALESCE($7, last_user_preview)
- WHERE id = $1`,
+ SET last_activity = CASE WHEN last_activity >= $2::bigint - ${LAST_ACTIVITY_DEBOUNCE_MS}
+ THEN last_activity
+ ELSE GREATEST(COALESCE(last_activity, 0), $2::bigint) END,
+ messages = $3,
+ turns = CASE WHEN turns IS NULL OR messages IS DISTINCT FROM $5
+ THEN (SELECT COUNT(*) FROM items t WHERE t.tenant_id = ${TENANT} AND t.session_id = $1 AND ${userTurn("t")})
+ ELSE turns + $4 END,
+ first_user_preview = CASE WHEN first_user_preview IS NULL AND turns = 0 AND messages = $5
+ THEN $6 ELSE first_user_preview END,
+ last_user_preview = COALESCE($7, last_user_preview)
+ WHERE id = $1`,
[
sessionId,
createdAt,
@@ -1013,10 +1043,10 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
if (!missing.length) return out;
const derived = await q(
`SELECT s.id, s.first_user_preview, s.last_user_preview,
- (SELECT fe.payload FROM session_entries fe
- WHERE fe.session_id = s.id AND ${userTurn("fe")} ORDER BY fe.seq ASC LIMIT 1) AS first_user,
- (SELECT le.payload FROM session_entries le
- WHERE le.session_id = s.id AND ${userTurn("le")} ORDER BY le.seq DESC LIMIT 1) AS last_user
+ (SELECT jsonb_build_object('text',fe.text) FROM items fe
+ WHERE fe.tenant_id = ${TENANT} AND fe.session_id = s.id AND ${userTurn("fe")} ORDER BY fe.seq ASC LIMIT 1) AS first_user,
+ (SELECT jsonb_build_object('text',le.text) FROM items le
+ WHERE le.tenant_id = ${TENANT} AND le.session_id = s.id AND ${userTurn("le")} ORDER BY le.seq DESC LIMIT 1) AS last_user
FROM sessions s WHERE s.id = ANY($1)`,
[missing],
options,
@@ -1026,8 +1056,8 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
});
for (const r of derived) {
out.set(r.id as string, {
- first: (r.first_user_preview as string | null) ?? storedPreview(parsedPayload(r.first_user), FIRST_PREVIEW_LEN),
- last: (r.last_user_preview as string | null) ?? storedPreview(parsedPayload(r.last_user), LAST_PREVIEW_LEN),
+ first: (r.first_user_preview as string | null) ?? storedPreview(r.first_user ?? null, FIRST_PREVIEW_LEN),
+ last: (r.last_user_preview as string | null) ?? storedPreview(r.last_user ?? null, LAST_PREVIEW_LEN),
});
}
return out;
@@ -1183,7 +1213,7 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
async appendMany(lease, entries: readonly NewEntry[]): Promise<SessionEntry[]> {
return withLease(lease, "append without a valid session lease", (client) =>
- insertEntries(client, lease.sessionId, entries),
+ insertEntries(client, lease.sessionId, entries, lease.turnId),
);
},
@@ -1191,22 +1221,23 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
const rows =
principalId === undefined
? await q(
- `SELECT count(*)::int AS n FROM session_entries e WHERE session_id = $1 AND seq < $2 AND ${transcriptEntryPredicate("e")}`,
+ `SELECT count(*)::int AS n FROM items i WHERE i.tenant_id = ${TENANT} AND i.session_id = $1 AND i.seq < $2 AND ${transcriptItemPredicate("i")}`,
[sessionId, beforeSeq],
)
: await q(
- `SELECT count(*)::int AS n FROM session_entries e
- JOIN participants p ON p.session_id = e.session_id AND p.principal_id = $3
- WHERE e.session_id = $1 AND e.seq < $2 AND ${transcriptEntryPredicate("e", "p")} AND ${withinParticipantWindow("e", "p")}`,
+ `SELECT count(*)::int AS n FROM items i
+ JOIN participants p ON p.session_id = i.session_id AND p.principal_id = $3
+ WHERE i.tenant_id = ${TENANT} AND i.session_id = $1 AND i.seq < $2 AND ${transcriptItemPredicate("i", "p")} AND ${withinParticipantWindow("i", "p")}`,
[sessionId, beforeSeq, principalId],
);
return Number(rows[0]?.n ?? 0);
},
async latestEntrySeq(sessionId): Promise<number> {
- const rows = await q("SELECT COALESCE(MAX(seq), -1) AS n FROM session_entries WHERE session_id = $1", [
- sessionId,
- ]);
+ const rows = await q(
+ `SELECT COALESCE(MAX(seq), -1) AS n FROM items WHERE tenant_id = ${TENANT} AND session_id = $1`,
+ [sessionId],
+ );
return Number(rows[0]?.n ?? -1);
},
@@ -1220,7 +1251,7 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
async setGoal(lease, goal, notices = []) {
await withLease(lease, "set goal without a valid session lease", async (client) => {
- await insertEntries(client, lease.sessionId, notices);
+ await insertEntries(client, lease.sessionId, notices, lease.turnId);
await client.query(
`INSERT INTO session_goals(session_id, objective, status, floor, token_cap, tokens_used, active_ms,
active_since, completion_note, cancel_note, governor, pause_reason,
@@ -1254,9 +1285,8 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
async hasDeliveryEntry(sessionId, deliveryKey, slackTs) {
const rows = await q(
- `SELECT 1 FROM session_entries WHERE session_id = $1
- AND (payload::jsonb ->> 'deliveryKey' = $2
- OR (type = 'user' AND payload::jsonb ->> 'ts' = $3)) LIMIT 1`,
+ `SELECT 1 FROM items WHERE tenant_id = ${TENANT} AND session_id = $1 AND kind = 'UserMessage'
+ AND (body ->> 'deliveryKey' = $2 OR external_id = $3) LIMIT 1`,
[sessionId, deliveryKey, slackTs ?? null],
);
return rows.length > 0;
@@ -1264,13 +1294,17 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
async getEntries(sessionId, opts?: GetEntriesOptions): Promise<SessionEntry[]> {
const params: unknown[] = [sessionId, opts?.sinceSeq ?? 0];
- let sql = "SELECT e.* FROM session_entries e WHERE session_id = $1 AND seq >= $2";
- if (opts?.transcript) sql += ` AND ${transcriptEntryPredicate("e")}`;
+ let sql = `SELECT ${ITEM_FIELDS} FROM ${ITEM_FROM} WHERE i.tenant_id = ${TENANT} AND i.session_id = $1 AND i.seq >= $2`;
+ if (opts?.transcript) sql += ` AND ${transcriptItemPredicate("i")}`;
if (opts?.beforeSeq !== undefined) {
params.push(opts.beforeSeq);
- sql += ` AND seq < $${params.length}`;
+ sql += ` AND i.seq < $${params.length}`;
+ }
+ if (opts?.turnId !== undefined) {
+ params.push(opts.turnId);
+ sql += ` AND i.turn_id = $${params.length}::uuid`;
}
- sql += " ORDER BY seq DESC";
+ sql += " ORDER BY i.seq DESC";
if (opts?.limit !== undefined) {
params.push(opts.limit);
sql += ` LIMIT $${params.length}`;
@@ -1279,40 +1313,30 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
},
async getContextWindow(sessionId) {
- const [meta, summary, memoryContext] = await Promise.all([
- q("SELECT count(*)::int AS total FROM session_entries WHERE session_id = $1", [sessionId]),
- q(
- `SELECT (payload::jsonb ->> 'throughSeq')::int AS through
- FROM session_entries
- WHERE session_id = $1 AND type = 'system'
- AND payload::jsonb ->> 'kind' = 'context_summary'
- AND jsonb_typeof(payload::jsonb -> 'throughSeq') = 'number'
- AND jsonb_typeof(payload::jsonb -> 'text') = 'string'
- ORDER BY seq DESC LIMIT 1`,
- [sessionId],
- ),
+ const [meta, summary] = await Promise.all([
+ q(`SELECT count(*)::int AS total FROM items i WHERE i.tenant_id = ${TENANT} AND i.session_id = $1`, [
+ sessionId,
+ ]),
q(
- "SELECT * FROM session_entries WHERE session_id = $1 AND ((type = 'system' AND payload::jsonb ->> 'kind' = 'memory_context') OR (type = 'user' AND payload::jsonb -> 'memoryContext' ->> 'kind' = 'memory_context')) ORDER BY seq DESC LIMIT 1",
+ `SELECT through_seq AS through FROM items
+ WHERE tenant_id = ${TENANT} AND session_id = $1 AND kind = 'ContextCompaction'
+ AND through_seq IS NOT NULL AND text IS NOT NULL ORDER BY seq DESC LIMIT 1`,
[sessionId],
),
]);
- const through = summary[0]?.through;
- const sinceSeq = typeof through === "number" ? through + 1 : 0;
- const rows = await q("SELECT * FROM session_entries WHERE session_id = $1 AND seq >= $2 ORDER BY seq ASC", [
- sessionId,
- sinceSeq,
- ]);
- return {
- entries: [
- ...(memoryContext[0] && Number(memoryContext[0].seq) < sinceSeq ? [rowToEntry(memoryContext[0])] : []),
- ...rows.map(rowToEntry),
- ],
- totalEntries: Number(meta[0]?.total ?? 0),
- };
+ const sinceSeq = summary[0]?.through == null ? 0 : Number(summary[0].through) + 1;
+ const rows = await q(
+ `SELECT ${ITEM_FIELDS} FROM ${ITEM_FROM} WHERE i.tenant_id = ${TENANT} AND i.session_id = $1 AND i.seq >= $2 ORDER BY i.seq`,
+ [sessionId, sinceSeq],
+ );
+ return { entries: rows.map(rowToEntry), totalEntries: Number(meta[0]?.total ?? 0) };
},
async getEntry(sessionId, seq): Promise<SessionEntry | undefined> {
- const rows = await q("SELECT * FROM session_entries WHERE session_id = $1 AND seq = $2", [sessionId, seq]);
+ const rows = await q(
+ `SELECT ${ITEM_FIELDS} FROM ${ITEM_FROM} WHERE i.tenant_id = ${TENANT} AND i.session_id = $1 AND i.seq = $2`,
+ [sessionId, seq],
+ );
return rows[0] ? rowToEntry(rows[0]) : undefined;
},
@@ -1439,7 +1463,7 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
await q(
`WITH boundary AS (
SELECT CASE WHEN $5 THEN 0 ELSE COALESCE(MAX(seq) + 1, 0) END AS seq
- FROM session_entries WHERE session_id = $1
+ FROM items WHERE tenant_id = ${TENANT} AND session_id = $1
)
INSERT INTO participants(session_id, principal_id, valid_from, valid_to, valid_from_seq, valid_to_seq, title)
SELECT $1,$2,$3,NULL,boundary.seq,NULL,$4 FROM boundary
@@ -1464,7 +1488,7 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
async removeParticipant(sessionId, principalId): Promise<void> {
await q(
- "UPDATE participants SET valid_to = $3, valid_to_seq = (SELECT COALESCE(MAX(seq) + 1, 0) FROM session_entries WHERE session_id = $1) WHERE session_id = $1 AND principal_id = $2 AND valid_to IS NULL",
+ `UPDATE participants SET valid_to = $3, valid_to_seq = (SELECT COALESCE(MAX(seq) + 1, 0) FROM items WHERE tenant_id = ${TENANT} AND session_id = $1) WHERE session_id = $1 AND principal_id = $2 AND valid_to IS NULL`,
[sessionId, principalId, now()],
);
},
@@ -1475,7 +1499,6 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
await client.query("DELETE FROM session_llm_requests WHERE session_id = $1", [sessionId]);
await client.query("DELETE FROM session_leases WHERE session_id = $1", [sessionId]);
await client.query("DELETE FROM participants WHERE session_id = $1", [sessionId]);
- await client.query("DELETE FROM session_entries WHERE session_id = $1", [sessionId]);
await client.query("DELETE FROM session_entry_search WHERE session_id = $1", [sessionId]);
await client.query("DELETE FROM session_pins WHERE session_id = $1", [sessionId]);
await client.query("DELETE FROM sessions WHERE id = $1", [sessionId]);
@@ -1488,7 +1511,7 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
const gone = await client.query(
`DELETE FROM sessions
WHERE id = $1
- AND NOT EXISTS (SELECT 1 FROM session_entries WHERE session_id = $1)
+ AND NOT EXISTS (SELECT 1 FROM items WHERE tenant_id = ${TENANT} AND session_id = $1)
AND NOT EXISTS (SELECT 1 FROM session_leases WHERE session_id = $1 AND expires_at > $2)`,
[sessionId, now()],
);
@@ -1589,16 +1612,16 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
async visibleEntries(sessionId, principalId, opts?: GetEntriesOptions): Promise<SessionEntry[]> {
const params: unknown[] = [sessionId, principalId, opts?.sinceSeq ?? 0];
- let sql = `SELECT e.* FROM session_entries e
- JOIN participants p ON p.session_id = e.session_id AND p.principal_id = $2
- WHERE e.session_id = $1 AND e.seq >= $3
- AND ${withinParticipantWindow("e", "p")}`;
- if (opts?.transcript) sql += ` AND ${transcriptEntryPredicate("e", "p")}`;
+ let sql = `SELECT ${ITEM_FIELDS} FROM ${ITEM_FROM}
+ JOIN participants p ON p.session_id = i.session_id AND p.principal_id = $2
+ WHERE i.tenant_id = ${TENANT} AND i.session_id = $1 AND i.seq >= $3
+ AND ${withinParticipantWindow("i", "p")}`;
+ if (opts?.transcript) sql += ` AND ${transcriptItemPredicate("i", "p")}`;
if (opts?.beforeSeq !== undefined) {
params.push(opts.beforeSeq);
- sql += ` AND e.seq < $${params.length}`;
+ sql += ` AND i.seq < $${params.length}`;
}
- sql += " ORDER BY e.seq DESC";
+ sql += " ORDER BY i.seq DESC";
if (opts?.limit !== undefined) {
params.push(opts.limit);
sql += ` LIMIT $${params.length}`;
@@ -1623,14 +1646,13 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
const ts = tsPrefixQuery(query);
if (!ts) return [];
const rows = await q(
- `SELECT h.session_id, h.seq, h.type, h.author, h.text, h.created_at, e.scope_label,
+ `SELECT h.session_id, h.seq, h.type, h.author, h.text, h.created_at,
s.scope_id, COALESCE(p.title, s.title) AS title, s.channel_name, s.surface, p.archived
FROM session_entry_search h
- JOIN session_entries e ON e.session_id = h.session_id AND e.seq = h.seq
JOIN participants p ON p.session_id = h.session_id AND p.principal_id = $1
JOIN sessions s ON s.id = h.session_id
WHERE h.search_tsv @@ to_tsquery('simple', $2)
- AND ${withinParticipantWindow("h", "p")}
+ AND ${withinParticipantWindow("h", "p", false)}
ORDER BY h.created_at DESC, h.session_id, h.seq DESC
LIMIT $3`,
[principalId, ts, Math.max(1, Math.min(limit, 200))],
@@ -1642,7 +1664,6 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
return [
{
sessionId: r.session_id as string,
- entryScopeLabel: r.scope_label as ScopeId,
scopeId: r.scope_id as ScopeId,
...(r.title != null ? { title: r.title as string } : {}),
...(r.channel_name ? { channelName: r.channel_name as string } : {}),
@@ -1688,8 +1709,8 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
async missingSearchEntries(sessionId): Promise<number> {
const rows = await q(
`WITH missing AS MATERIALIZED (
- SELECT entry_search_text(e.payload) AS text FROM session_entries e
- WHERE e.session_id = $1 AND e.type IN ('user', 'assistant', 'text')
+ SELECT e.text AS text FROM items e
+ WHERE e.tenant_id = ${TENANT} AND e.session_id = $1 AND ${SEARCHABLE_ITEM("e")}
AND NOT EXISTS (SELECT 1 FROM session_entry_search s WHERE s.session_id = e.session_id AND s.seq = e.seq)
)
SELECT COUNT(*) AS n FROM missing WHERE COALESCE(btrim(text), '') <> ''`,
@@ -1700,9 +1721,9 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
async lastSearchableEntrySeq(sessionId): Promise<number> {
const rows = await q(
- `SELECT COALESCE(MAX(seq), -1) AS n FROM session_entries
- WHERE session_id = $1 AND type IN ('user', 'assistant', 'text')
- AND COALESCE(btrim(entry_search_text(payload)), '') <> ''`,
+ `SELECT COALESCE(MAX(i.seq), -1) AS n FROM items i
+ WHERE i.tenant_id = ${TENANT} AND i.session_id = $1 AND ${SEARCHABLE_ITEM("i")}
+ AND btrim(i.text) <> ''`,
[sessionId],
);
return Number(rows[0]?.n ?? -1);
@@ -1798,9 +1819,9 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
const out = new Map<string, string>();
if (sessionIds.length === 0) return out;
const rows = await q(
- `SELECT DISTINCT ON (le.session_id) le.session_id, ${previewExpr("le.payload")} AS last_user
- FROM session_entries le
- WHERE le.session_id = ANY($1) AND ${userTurn("le")}
+ `SELECT DISTINCT ON (le.session_id) le.session_id, left(le.text, ${LAST_PREVIEW_LEN}) AS last_user
+ FROM items le
+ WHERE le.tenant_id = ${TENANT} AND le.session_id = ANY($1) AND ${userTurn("le")}
ORDER BY le.session_id, le.seq DESC`,
[sessionIds],
);
@@ -1917,10 +1938,10 @@ export function createPostgresSessionStore(connectionString: string, opts: Store
async attributedTurns(): Promise<AttributedTurn[]> {
const rows = await q(
- `SELECT p.principal_id, e.session_id, (e.created_at / 86400000)::bigint AS day,
- COUNT(*) AS turns, MIN(e.created_at) AS first_at, MAX(e.created_at) AS last_at
+ `SELECT p.principal_id, e.session_id, (${msOf("e.created_at")} / 86400000)::bigint AS day,
+ COUNT(*) AS turns, ${msOf("MIN(e.created_at)")} AS first_at, ${msOf("MAX(e.created_at)")} AS last_at
FROM participants p
- JOIN session_entries e ON e.session_id = p.session_id
+ JOIN items e ON e.tenant_id = ${TENANT} AND e.session_id = p.session_id
WHERE ${userTurn("e")}
AND ${withinParticipantWindow("e", "p")}
GROUP BY p.principal_id, e.session_id, day`,
diff --git a/src/sessions/session-store.ts b/src/sessions/session-store.ts
index ebba25198..347eff9ab 100644
--- a/src/sessions/session-store.ts
+++ b/src/sessions/session-store.ts
@@ -1,4 +1,3 @@
-import { memoryContextPayload } from "../memory/context-boundary.ts";
import { createHash } from "node:crypto";
import type { EntryType, ScopeId, Session, SessionEntry, SessionType, SpawnMeta } from "../types.ts";
import { sleep } from "../util/async.ts";
@@ -50,12 +49,8 @@ export function contextWindowFromEntries(entries: SessionEntry[]): ContextWindow
let latest: ContextSummaryPayload | null = null;
for (const entry of entries) latest = contextSummaryPayload(entry) ?? latest;
const throughSeq = latest?.throughSeq;
- const memoryContext = entries.findLast((entry) => memoryContextPayload(entry));
return {
- entries:
- throughSeq === undefined
- ? [...entries]
- : entries.filter((entry) => entry.seq > throughSeq || entry === memoryContext),
+ entries: throughSeq === undefined ? [...entries] : entries.filter((entry) => entry.seq > throughSeq),
totalEntries: entries.length,
};
}
@@ -63,6 +58,7 @@ export function contextWindowFromEntries(entries: SessionEntry[]): ContextWindow
export interface Lease {
sessionId: string;
token: string;
+ turnId?: string;
}
export type LeaseHolder = "turn" | "compaction" | "fork" | "backfill";
@@ -121,6 +117,7 @@ export interface NewEntry {
type: EntryType;
payload: unknown;
scopeLabel: ScopeId;
+ author?: string;
}
interface ParticipantViewPatch {
@@ -147,6 +144,7 @@ export const isWebThread = (threadRef: string): boolean => threadRef.startsWith(
export interface GetEntriesOptions {
transcript?: boolean;
+ turnId?: string;
sinceSeq?: number;
beforeSeq?: number;
limit?: number;
@@ -389,7 +387,6 @@ export interface DistinctScope {
}
export interface EntrySearchHit extends Pick<Session, "scopeId" | "title" | "channelName" | "surface" | "archived"> {
- entryScopeLabel: ScopeId;
sessionId: string;
seq: number;
type: EntryType;
@@ -545,9 +542,11 @@ export function isOverheardEntry(e: Pick<SessionEntry, "type" | "payload">): boo
return e.type === "user" && (e.payload as { overheard?: unknown } | null)?.overheard === true;
}
-export function entryDeliveryKey(entry: Pick<SessionEntry, "payload">): string | undefined {
- const key = (entry.payload as { deliveryKey?: unknown } | null)?.deliveryKey;
- return typeof key === "string" ? key : undefined;
+export class DuplicateCallError extends Error {
+ constructor(sessionId: string, callIds: readonly string[], options?: { cause?: unknown }) {
+ super(`session ${sessionId} already records one of calls ${callIds.join(", ")}`, options);
+ this.name = "DuplicateCallError";
+ }
}
interface AddParticipantOptions {
diff --git a/src/sessions/session-syscalls.ts b/src/sessions/session-syscalls.ts
index 085a8834b..13110f70d 100644
--- a/src/sessions/session-syscalls.ts
+++ b/src/sessions/session-syscalls.ts
@@ -1,5 +1,4 @@
import { isSessionTurn, sessionTurnOrigin } from "../core/turn-origin.ts";
-import { memoryContextPayload, type MemoryContextSnapshot } from "../memory/context-boundary.ts";
import { TURN_STOPPED_KIND } from "../harness/replay.ts";
import { principalDestination } from "../reach/reach.ts";
import type { DeliveryStore } from "../delivery/delivery-store.ts";
@@ -10,7 +9,6 @@ import { sleep } from "../util/async.ts";
import { pgTextSafe } from "../util/text.ts";
import { createMemoryAdvisoryLock, type AdvisoryLock } from "../persistence/advisory-lock.ts";
import { errMessage, swallowAs } from "../util/errors.ts";
-import { filterHistoryForAudience } from "../resolution/context-filter.ts";
import { randomUUID } from "node:crypto";
import { hashId } from "../util/crypto.ts";
import { canonicalJson } from "../../cli/src/canonical-json.ts";
@@ -186,7 +184,6 @@ type SessionReadResult =
| { ok: false; message: string };
interface SessionSyscallBinding {
- memoryContext?: MemoryContextSnapshot;
session: Session;
scopeId: ScopeId;
orgScopeId?: ScopeId;
@@ -516,9 +513,7 @@ export function createSessionSyscalls(deps: SessionSyscallDeps): SessionSyscalls
return {
rejectMessage: (sessionId, messageId) => deps.mailbox.acknowledge(sessionId, [messageId]),
forTurn(binding) {
- const callerTitle = binding.memoryContext
- ? binding.session.id
- : binding.session.title?.trim() || "this conversation";
+ const callerTitle = binding.session.title?.trim() || "this conversation";
async function resolveTarget(ref: string): Promise<Session | null> {
const trimmed = ref.trim();
@@ -566,12 +561,7 @@ export function createSessionSyscalls(deps: SessionSyscallDeps): SessionSyscalls
),
);
const allowed = views.slice(1).map((view) => new Set(view.map((entry) => entry.seq)));
- return filterHistoryForAudience(
- (views[0] ?? []).filter((entry) => allowed.every((seqs) => seqs.has(entry.seq))),
- audience,
- binding.scopeId,
- binding.orgScopeId ?? binding.scopeId,
- );
+ return (views[0] ?? []).filter((entry) => allowed.every((seqs) => seqs.has(entry.seq)));
}
async function currentCaller(): Promise<OrchestratorInput> {
@@ -612,14 +602,6 @@ export function createSessionSyscalls(deps: SessionSyscallDeps): SessionSyscalls
{ actor: message.actor, conversation: { ...caller.conversation, audience: message.audience } },
caller,
);
- if (binding.memoryContext) {
- const history = await visibleHistory(sender);
- if (
- message.sourceEntrySeq !== undefined &&
- !history.some((entry) => entry.seq === message.sourceEntrySeq)
- )
- continue;
- }
if (message.sourceEntrySeq !== undefined) {
const viewers = new Set([
caller.actor.id,
@@ -1021,16 +1003,6 @@ export function createSessionSyscalls(deps: SessionSyscallDeps): SessionSyscalls
if (!input.fork && !text)
return { ok: false, message: "new requires `text`: the new session's first message." };
if (text && text.length > 16_000) return { ok: false, message: "text exceeds 16000 characters" };
- if (input.fork && binding.memoryContext) {
- const entries = await deps.sessions.getEntries(binding.session.id);
- const checkpoint = entries.map(memoryContextPayload).findLast(Boolean);
- if (checkpoint?.snapshot.audience !== binding.memoryContext.audience || checkpoint.throughSeq >= 0)
- return {
- ok: false,
- message:
- "this conversation's earlier history was shared with a different audience, so it can't be forked from here.",
- };
- }
const out = await conversations
.start(binding.request.actor.id, {
scopeId: binding.scopeId,
@@ -1100,8 +1072,7 @@ export async function deliverSubagentMail(deps: SubagentMailDeps, run: Run): Pro
},
unattendedGrants: undefined,
};
- const memoryContext = (await deps.sessions.getEntries(child.id)).findLast((entry) => memoryContextPayload(entry));
- const title = memoryContext ? child.id : child.title?.trim() || child.id;
+ const title = child.title?.trim() || child.id;
const result = run.result;
let kind: SubagentMailKind;
let body: string;
@@ -1184,7 +1155,7 @@ export async function deliverSubagentMail(deps: SubagentMailDeps, run: Run): Pro
if (outputSeq !== undefined && outputSeq !== null) {
for (const id of new Set([prepared.actor.id, ...prepared.conversation.audience.map((person) => person.id)])) {
const visible = await deps.sessions.visibleEntries(child.id, id);
- if (!visible.some((entry) => entry.seq === outputSeq && entry.scopeLabel === child.scopeId))
+ if (!visible.some((entry) => entry.seq === outputSeq))
throw new Error("the current parent audience cannot read this child result");
}
}
diff --git a/src/sessions/tool-output.ts b/src/sessions/tool-output.ts
new file mode 100644
index 000000000..0e1ccdebb
--- /dev/null
+++ b/src/sessions/tool-output.ts
@@ -0,0 +1,173 @@
+import { createHash } from "node:crypto";
+import type { DurableByteStore } from "../files/durable-byte-store.ts";
+import type { PgPool } from "../persistence/pg-pool.ts";
+import { headSlice, tailSlice } from "../util/text.ts";
+import { collectBytes } from "../util/bytes.ts";
+import { swallowAs } from "../util/errors.ts";
+import type { SessionEntry } from "../types.ts";
+
+const TOOL_OUTPUT_SETTING = "tool_result_inline_tokens";
+export const DEFAULT_INLINE_TOKENS = 4096;
+const CHARS_PER_TOKEN = 4;
+const TOOL_OUTPUT_RETENTION_MS = 30 * 24 * 60 * 60_000;
+const SETTING_CACHE_MS = 30_000;
+const TOOL_OUTPUT_PATH = "tool-output/";
+
+export interface ToolOutputRef {
+ blob: string;
+ chars: number;
+ shownChars: number;
+}
+
+export interface ToolOutputStore {
+ inlineTokens(): Promise<number>;
+ save(text: string): Promise<string>;
+ read(sessionId: string, callId: string): Promise<{ text: string } | { expired: true } | null>;
+}
+
+export function inlineTokensFrom(value: unknown): number {
+ if (value === undefined || value === null) return DEFAULT_INLINE_TOKENS;
+ const n = typeof value === "string" ? Number(value) : value;
+ return typeof n === "number" && Number.isFinite(n) && n >= 0 ? Math.floor(n) : DEFAULT_INLINE_TOKENS;
+}
+
+export function toolOutputCallId(path: string): string | null {
+ if (!path.startsWith(TOOL_OUTPUT_PATH)) return null;
+ const callId = path.slice(TOOL_OUTPUT_PATH.length);
+ return callId && !callId.includes("/") ? callId : null;
+}
+
+export function cutToolOutput(
+ text: string,
+ inlineTokens: number,
+ callId: string,
+): { text: string; shownChars: number } | null {
+ if (inlineTokens <= 0) return null;
+ const budget = inlineTokens * CHARS_PER_TOKEN;
+ if (text.length <= budget) return null;
+ const head = headSlice(text, Math.floor(budget / 2));
+ const tail = tailSlice(text, Math.floor(budget / 2));
+ const marker =
+ `\n…[output cut: showing the first ${head.length} and last ${tail.length} of ${text.length} chars. ` +
+ `The full output is in ${TOOL_OUTPUT_PATH}${callId}; reading it with the files tool copies it onto the computer (kept 30 days)]…\n`;
+ return { text: head + marker + tail, shownChars: head.length + tail.length };
+}
+
+export async function parkIfOverCap(
+ store: Pick<ToolOutputStore, "inlineTokens" | "save">,
+ text: string,
+ callId: string,
+): Promise<{ text: string; output: ToolOutputRef } | null> {
+ const inlineTokens = await store.inlineTokens().catch(swallowAs("tool output: inline cap", DEFAULT_INLINE_TOKENS));
+ const cut = cutToolOutput(text, inlineTokens, callId);
+ if (!cut) return null;
+ const blob = await store.save(text).catch(swallowAs("tool output: park full text", null));
+ return blob ? { text: cut.text, output: { blob, chars: text.length, shownChars: cut.shownChars } } : null;
+}
+
+const sha256Hex = (text: string): string => createHash("sha256").update(text, "utf8").digest("hex");
+
+async function readText(bytes: DurableByteStore, blobKey: string): Promise<string | null> {
+ const opened = await bytes.open(blobKey);
+ if (!opened) return null;
+ const { data } = await collectBytes(opened.stream, {});
+ return data.toString("utf8");
+}
+
+export function createPostgresToolOutputStore(opts: {
+ pool: Pick<PgPool, "q">;
+ bytesFor: (prefix: string) => DurableByteStore;
+ now?: () => number;
+}): ToolOutputStore {
+ const now = opts.now ?? (() => Date.now());
+ let setting: { value: number; at: number } | null = null;
+ let tenant: Promise<{ id: string; bytes: DurableByteStore }> | null = null;
+ const tenantBytes = (): Promise<{ id: string; bytes: DurableByteStore }> =>
+ (tenant ??= opts.pool.q("SELECT qm_tenant_id()::text AS id").then((rows) => {
+ const id = rows[0]?.id as string;
+ return { id, bytes: opts.bytesFor(`${TOOL_OUTPUT_PATH}${id}/`) };
+ })).catch((err: unknown) => {
+ tenant = null;
+ throw err;
+ });
+ return {
+ async inlineTokens() {
+ if (setting && now() - setting.at < SETTING_CACHE_MS) return setting.value;
+ const rows = await opts.pool.q("SELECT value FROM qm_settings WHERE key = $1", [TOOL_OUTPUT_SETTING]);
+ setting = { value: inlineTokensFrom(rows[0]?.value), at: now() };
+ return setting.value;
+ },
+ async save(text) {
+ const { id, bytes } = await tenantBytes();
+ const put = await bytes.put(Buffer.from(text, "utf8"));
+ await opts.pool.q(
+ `INSERT INTO blobs(tenant_id, sha256, size, mime, storage_key, created_at, expires_at)
+ VALUES ($1::uuid, decode($2, 'hex'), $3, 'text/plain; charset=utf-8', $4,
+ to_timestamp($5::double precision / 1000), to_timestamp($6::double precision / 1000))
+ ON CONFLICT (tenant_id, sha256) DO NOTHING`,
+ [
+ id,
+ put.sha256,
+ put.sizeBytes,
+ `${TOOL_OUTPUT_PATH}${id}/${put.blobKey}`,
+ now(),
+ now() + TOOL_OUTPUT_RETENTION_MS,
+ ],
+ );
+ return put.sha256;
+ },
+ async read(sessionId, callId) {
+ const rows = await opts.pool.q(
+ `SELECT b.storage_key, i.created_at <= now() - interval '30 days' AS expired
+ FROM items i
+ LEFT JOIN blobs b ON b.tenant_id = i.tenant_id AND b.sha256 = i.blob_sha
+ WHERE i.tenant_id = qm_tenant_id() AND i.session_id = $1 AND i.kind = 'ToolResult' AND i.call_id = $2
+ AND i.blob_sha IS NOT NULL`,
+ [sessionId, callId],
+ );
+ const row = rows[0];
+ if (!row) return null;
+ if (row.expired === true) return { expired: true };
+ if (typeof row.storage_key !== "string")
+ throw new Error(`tool output ${callId}: the result names a parked blob with no blobs row`);
+ const { bytes, id } = await tenantBytes();
+ const key = String(row.storage_key).slice(`${TOOL_OUTPUT_PATH}${id}/`.length);
+ const text = await readText(bytes, key);
+ if (text === null)
+ throw new Error(`tool output ${callId}: parked bytes ${row.storage_key} are missing from storage`);
+ return { text };
+ },
+ };
+}
+
+export function createMemoryToolOutputStore(
+ opts: { inlineTokens?: number; sessions?: { getEntries(sessionId: string): Promise<SessionEntry[]> } } = {},
+): ToolOutputStore & { setInlineTokens(n: number): void; parked: Map<string, string> } {
+ let cap = opts.inlineTokens ?? DEFAULT_INLINE_TOKENS;
+ const parked = new Map<string, string>();
+ return {
+ parked,
+ setInlineTokens(n) {
+ cap = n;
+ },
+ async inlineTokens() {
+ return cap;
+ },
+ async save(text) {
+ const sha = sha256Hex(text);
+ parked.set(sha, text);
+ return sha;
+ },
+ async read(sessionId, callId) {
+ const entries = (await opts.sessions?.getEntries(sessionId)) ?? [];
+ const result = entries.find(
+ (e) => e.type === "tool_result" && (e.payload as { callId?: unknown } | null)?.callId === callId,
+ );
+ const blob = (result?.payload as { output?: { blob?: unknown } } | undefined)?.output?.blob;
+ if (typeof blob !== "string") return null;
+ const text = parked.get(blob);
+ if (text === undefined) throw new Error(`tool output ${callId}: parked blob ${blob} is missing`);
+ return { text };
+ },
+ };
+}
diff --git a/src/sessions/transcript-source.ts b/src/sessions/transcript-source.ts
index eb2a8f17f..051505ecc 100644
--- a/src/sessions/transcript-source.ts
+++ b/src/sessions/transcript-source.ts
@@ -22,7 +22,7 @@ export function transcriptEntries(entries: readonly SessionEntry[]): SessionEntr
);
return entries.filter((entry) => {
const payload = entry.payload as { ts?: string; slackReplyReceipt?: boolean } | null;
- return entry.type !== "soul" && !(payload?.slackReplyReceipt && payload.ts && received.has(payload.ts));
+ return !(payload?.slackReplyReceipt && payload.ts && received.has(payload.ts));
});
}
@@ -35,11 +35,10 @@ export function createTranscriptSource(sessions: TranscriptStore): TranscriptSou
opts: GetEntriesOptions | undefined,
principalId?: string,
): Promise<TranscriptRead> => {
- const entries = transcriptEntries(
+ const entries =
principalId === undefined
? await sessions.getEntries(sessionId, { ...opts, transcript: true })
- : await sessions.visibleEntries(sessionId, principalId, { ...opts, transcript: true }),
- );
+ : await sessions.visibleEntries(sessionId, principalId, { ...opts, transcript: true });
const windowed = opts?.sinceSeq !== undefined || (opts?.limit !== undefined && entries.length >= opts.limit);
const earlier = windowed
? await sessions.countEntriesBefore(sessionId, entries[0]?.seq ?? emptyWindowStart(opts), principalId)
diff --git a/src/tools/primitives.ts b/src/tools/primitives.ts
index 461a6ac01..9f2c57e26 100644
--- a/src/tools/primitives.ts
+++ b/src/tools/primitives.ts
@@ -1,4 +1,5 @@
import type { AdminToolAccess } from "./admin.ts";
+import { toolOutputCallId, type ToolOutputStore } from "../sessions/tool-output.ts";
import { rankedSkills } from "../skills/materialize.ts";
import type { SkillResolution } from "../skills/skill-store.ts";
import {
@@ -198,6 +199,7 @@ export interface ToolContext extends SurfaceToolDeps {
sessionSyscalls?: SessionSyscalls;
/** True when a person's own message started this turn (not a cron, webhook, ambient or delegated wake). */
humanTurn?: boolean;
+ toolOutput?: Pick<ToolOutputStore, "inlineTokens" | "save">;
commandCredentialHandles?: readonly string[];
execute(
command: string,
@@ -546,6 +548,8 @@ export interface ToolContextDeps {
attach?: AttachFiles;
sessionSyscalls?: SessionSyscalls;
humanTurn?: boolean;
+ toolOutput?: ToolOutputStore;
+ sessionId?: string;
}
export function createToolContext(deps: ToolContextDeps): ToolContext {
@@ -757,6 +761,7 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {
return {
...(deps.admin ? { admin: deps.admin } : {}),
...(deps.installSharedCanvas ? { installSharedCanvas: deps.installSharedCanvas } : {}),
+ ...(deps.toolOutput ? { toolOutput: deps.toolOutput } : {}),
...(deps.commandCredentials?.length
? { commandCredentialHandles: deps.commandCredentials.map((credential) => credential.handle) }
: {}),
@@ -983,6 +988,23 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {
};
}
if (path.startsWith("shared/open-")) return { content: null, sourceScopeId: null };
+ const parkedCallId = toolOutputCallId(path);
+ if (parkedCallId && deps.toolOutput && deps.sessionId) {
+ const parked = await withAbort(() => deps.toolOutput!.read(deps.sessionId!, parkedCallId), signal);
+ if (parked && "expired" in parked)
+ return {
+ content: `[the full output of ${parkedCallId} expired after 30 days; only the cut shown in the transcript remains]`,
+ sourceScopeId: writableScopeId,
+ };
+ if (parked) {
+ const { handle } = await machine(sandboxId);
+ await withAbort(() => deps.sandbox.writeFile(handle, path, parked.text), signal);
+ return {
+ content: `[copied the full output (${parked.text.length} chars) to ${path} on the computer; read the parts you need with execute, e.g. sed -n or grep]`,
+ sourceScopeId: writableScopeId,
+ };
+ }
+ }
signal?.throwIfAborted();
const { handle, resource, crossScope } = await machine(sandboxId);
const mounts = crossScope ? fallbackMounts.filter((mount) => mount.mountPath === "global") : fallbackMounts;
diff --git a/src/types.ts b/src/types.ts
index 9d090639d..955e9cb17 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -137,7 +137,6 @@ export type EntryType =
| "text"
| "tool_call"
| "tool_result"
- | "soul"
| "system"
| "delivery"
| "approval_request"
diff --git a/src/wiring.ts b/src/wiring.ts
index 90ef4047e..ef42f23fa 100644
--- a/src/wiring.ts
+++ b/src/wiring.ts
@@ -324,6 +324,7 @@ import { setCustomProviders } from "./model/custom-providers.ts";
import { createCustomProviderStore, type CustomProviderStore } from "./model/custom-provider-store.ts";
import { createMemorySessionStore } from "./sessions/memory-session-store.ts";
import { createPostgresSessionStore } from "./sessions/postgres-session-store.ts";
+import { createMemoryToolOutputStore, createPostgresToolOutputStore } from "./sessions/tool-output.ts";
import type { SessionStore } from "./sessions/session-store.ts";
import { createMockHarness } from "./harness/mock-harness.ts";
import {
@@ -1363,6 +1364,20 @@ export function buildApp(
? createPostgresSessionStore(requireDbUrl("SESSION_STORE"))
: createMemorySessionStore();
memorySessions.store = sessions;
+ const toolOutput =
+ config.sessionStore === "postgres" && pgArtifactMap
+ ? createPostgresToolOutputStore({
+ pool: pgArtifactMap.pool,
+ bytesFor: (prefix) =>
+ config.snapshotStore === "s3" && config.s3Bucket
+ ? createS3DurableByteStore({
+ bucket: config.s3Bucket,
+ ...(config.s3Region ? { region: config.s3Region } : {}),
+ prefix: `${config.s3Prefix ?? ""}${prefix}`,
+ })
+ : createLocalDurableByteStore(join(config.dataDir, prefix)),
+ })
+ : createMemoryToolOutputStore({ sessions });
const runStoreKind = config.runStore;
const runSignals: RunSignalStore =
runStoreKind === "postgres"
@@ -2102,6 +2117,7 @@ export function buildApp(
sessions,
workspace,
files,
+ toolOutput,
sandbox,
sandboxResources,
swarms,
diff --git a/test/agent-tools.test.ts b/test/agent-tools.test.ts
index 1cce00707..0a511de24 100644
--- a/test/agent-tools.test.ts
+++ b/test/agent-tools.test.ts
@@ -12,8 +12,8 @@ import {
import { resumeStrategy } from "../src/core/turn-resume.ts";
import { reconstructMessagesFromHistory } from "../src/harness/replay.ts";
import { createMemoryRunSignalStore, waitForClientResult } from "../src/runs/run-signal-store.ts";
-import { filterHistoryForAudience } from "../src/resolution/context-filter.ts";
import { CommandDenied, NeedsApproval, type ToolContext } from "../src/tools/primitives.ts";
+import { createMemoryToolOutputStore } from "../src/sessions/tool-output.ts";
import type { ClientToolDeclaration, EntryType, SessionEntry } from "../src/types.ts";
import type { ComputerStatus } from "../src/sandbox/sandbox.ts";
@@ -732,6 +732,42 @@ test("a giant tool result is capped for the model, keeping the tail and matching
assert.equal(payload.stderr, undefined, "raw stderr is not stored beside the result");
});
+test("a result over the inline cap keeps its head and tail and parks the full text as tool-output/<call_id>", async () => {
+ const emitted: Emitted[] = [];
+ const outputs = createMemoryToolOutputStore({ inlineTokens: 100 });
+ const full = Array.from({ length: 2_000 }, (_, i) => `line ${i}`).join("\n");
+ const tc = {
+ ...fakeToolContext(),
+ toolOutput: outputs,
+ execute: async () => ({ stdout: full, stderr: "", code: 0, timedOut: false }),
+ };
+ const ref: ToolContextRef = { current: tc, emit: (e) => void emitted.push(e as Emitted), scopeLabel: "personal:U1" };
+ const [execute] = createAgentTools(ref);
+ const ret = (await call(execute, { command: "seq", purpose: "Run test command" })) as {
+ content: Array<{ type: string; text: string }>;
+ };
+ const seen = ret.content.map((c) => c.text).join("\n");
+ const payload = emitted.find((e) => e.type === "tool_result")!.payload as {
+ callId: string;
+ result: string;
+ output: { blob: string; chars: number; shownChars: number };
+ };
+ assert.equal(payload.result, seen, "the item stores exactly what the model saw");
+ assert.ok(seen.startsWith("line 0\nline 1"), "head survives");
+ assert.ok(seen.includes("[exit 0]"), "tail survives");
+ assert.ok(seen.includes(`tool-output/${payload.callId}`), "the marker names the parked file");
+ assert.ok(payload.output.shownChars <= 400 && seen.length < 1_000, `cut to the 100-token budget (${seen.length})`);
+ assert.equal(outputs.parked.get(payload.output.blob)!.length, payload.output.chars);
+ assert.ok(outputs.parked.get(payload.output.blob)!.includes("line 1999"));
+
+ outputs.setInlineTokens(0);
+ emitted.length = 0;
+ await call(execute, { command: "seq", purpose: "Run test command" });
+ const uncapped = emitted.find((e) => e.type === "tool_result")!.payload as { result: string; output?: unknown };
+ assert.equal(uncapped.output, undefined, "0 turns the cap off");
+ assert.ok(uncapped.result.includes("line 1000"));
+});
+
test("execute stores its output once and replays exactly what the model saw", async () => {
const emitted: Emitted[] = [];
const tc = {
@@ -1585,69 +1621,6 @@ test("pauseStampAfterToolCall stamps terminate onto sibling results once silence
assert.equal(await pauseStampAfterToolCall(ref)({}), undefined);
});
-test("a cross-scope read's tool_result keeps the SOURCE scope label so the audience filter can redact it", async () => {
- const emitted: Emitted[] = [];
- const ref: ToolContextRef = {
- current: fakeToolContext(),
- emit: (e) => {
- emitted.push(e as Emitted);
- },
- scopeLabel: "channel:C1",
- orgScopeId: "org:default-org",
- };
- const [execute, , read] = createAgentTools(ref);
-
- await call(read, { action: "read", path: "a.txt" });
- await call(read, { action: "read", path: "missing.txt" });
- await call(execute, { command: "echo hi", purpose: "Run test command" });
-
- const labelOf = (type: EntryType, match: (p: any) => boolean) =>
- emitted.find((e) => e.type === type && match(e.payload))?.scopeLabel;
- assert.equal(
- labelOf("tool_result", (p) => p.path === "a.txt"),
- "personal:U1",
- "private file content is labeled with its source scope",
- );
- assert.equal(
- labelOf("tool_call", (p) => p.path === "a.txt"),
- "channel:C1",
- "the call itself (path only) stays session-scoped",
- );
- assert.equal(
- labelOf("tool_result", (p) => p.path === "missing.txt"),
- "channel:C1",
- "a not-found read has no source scope",
- );
- assert.equal(
- labelOf("tool_result", (p) => p.tool === "execute"),
- "channel:C1",
- "non-read results stay session-scoped",
- );
-
- const history: SessionEntry[] = emitted.map((e, i) => ({
- sessionId: "s",
- seq: i + 1,
- parentSeq: null,
- type: e.type,
- payload: e.payload,
- scopeLabel: e.scopeLabel,
- createdAt: i + 1,
- }));
- const u1 = { id: "U1", type: "internal" } as const;
- const u2 = { id: "U2", type: "internal" } as const;
- const forOwner = filterHistoryForAudience(history, [u1], "channel:C1", "org:default-org");
- assert.equal(forOwner.length, history.length, "the file's owner still sees everything");
- const forJoined = filterHistoryForAudience(history, [u1, u2], "channel:C1", "org:default-org");
- assert.ok(
- !forJoined.some((e) => e.type === "tool_result" && (e.payload as { result?: string }).result === "data"),
- "the private file content is redacted once a non-entitled member is in the audience",
- );
- assert.ok(
- forJoined.some((e) => e.type === "tool_call" && (e.payload as { path?: string }).path === "a.txt"),
- "session-scoped entries are still visible",
- );
-});
-
const callWith = (tool: ReturnType<typeof createAgentTools>[number] | undefined, id: string, params: unknown) => {
assert.ok(tool);
return (tool.execute as unknown as (i: string, p: unknown) => Promise<unknown>)(id, params);
@@ -4130,7 +4103,7 @@ test("core tools carry retrySafe while client tools cannot advertise automatic r
);
});
-test("a tool result recorded after shutdown fires is marked interrupted; user Stop and plain completion are not", async () => {
+test("a tool result produced after shutdown is marked interrupted; user Stop and plain completion record it", async () => {
const record = async (abort: "shutdown" | "stop" | "none", retrySafe = true) => {
const emitted: Emitted[] = [];
const shutdown = new AbortController();
@@ -4156,9 +4129,8 @@ test("a tool result recorded after shutdown fires is marked interrupted; user St
};
const interrupted = await record("shutdown");
- const result = interrupted.find((e) => e.type === "tool_result")!.payload;
- assert.equal(result.interrupted, true);
- assert.equal(typeof result.result, "string", "the recorded text is kept alongside the mark");
+ assert.equal(interrupted.find((e) => e.type === "tool_result")?.payload.interrupted, true);
+ assert.deepEqual(interrupted.find((e) => e.type === "tool_result")?.payload.stop, { kind: "shutdown" });
assert.deepEqual(strategy(interrupted), {
kind: "retry",
call: { callId: "call-shutdown", tool: "history", input: { query: "budget" } },
@@ -4412,31 +4384,6 @@ test("native skills discovery exposes list and search with validated pagination"
}
});
-test("skill discovery records the page's source scope for history filtering", async () => {
- const emitted: Emitted[] = [];
- const ctx = fakeToolContext();
- ctx.listSkills = async () => ({
- sourceScopeId: "personal:U1",
- skills: [{ name: "private-helper", description: "Private description" }],
- nextOffset: 1,
- });
- const ref: ToolContextRef = {
- current: ctx,
- scopeLabel: "channel:C1",
- orgScopeId: "org:default-org",
- emit: (e) => {
- emitted.push(e as Emitted);
- },
- };
- const tool = createAgentTools(ref).find((t) => t.name === "skills")!;
- await callWith(tool, "discovery", { action: "list" });
- assert.equal(emitted.find((e) => e.type === "tool_result")?.scopeLabel, "personal:U1");
- const history = emitted.map((e, seq) => ({ ...e, seq, createdAt: seq, sessionId: "s" })) as SessionEntry[];
- const visible = filterHistoryForAudience(history, [{ id: "U2", type: "internal" }], "channel:C1", "org:default-org");
- assert.doesNotMatch(JSON.stringify(visible), /Private description|private-helper/);
- assert.match(JSON.stringify(history), /Private description/);
-});
-
test("client screenshots reach the model as images and remain subject to output screening", async () => {
const { store, ref } = clientToolRef();
const image = { data: "aGVsbG8=", mimeType: "image/png" as const };
diff --git a/test/agent-transcript-memory-boundary.test.ts b/test/agent-transcript-memory-boundary.test.ts
deleted file mode 100644
index 91f2eea36..000000000
--- a/test/agent-transcript-memory-boundary.test.ts
+++ /dev/null
@@ -1,277 +0,0 @@
-import "./support/auto-fake-sprites.ts";
-import { test } from "node:test";
-import assert from "node:assert/strict";
-import type { AddressInfo } from "node:net";
-import { createServer } from "../src/api/server.ts";
-import { buildApp } from "../src/wiring.ts";
-import { testConfig } from "./support/test-config.ts";
-import { mintCapabilityToken, CONTROL_PLANE_AUD } from "../src/auth/capability-token.ts";
-import { signedRequestHeaders } from "../plugins/chassis/src/source-auth-sign.ts";
-import { buildMemoryContextSnapshot } from "../src/memory/context-boundary.ts";
-import type { MemoryFact } from "../src/memory/facts.ts";
-import type { MemoryService } from "../src/memory/memory-service.ts";
-import type { Principal } from "../src/types.ts";
-
-const SECRET = "synthetic-transcript-boundary-secret";
-const HOME = "personal:alice";
-const ROOM = "group:room";
-
-async function fixture() {
- const built = buildApp(testConfig({ signingSecret: SECRET }));
- let audience: Principal[] = [
- { id: "alice", type: "internal" },
- { id: "bob", type: "internal" },
- ];
- let records: MemoryFact[] = [
- {
- id: "1",
- text: "- PRIVATE_SENTINEL",
- sensitivity: "ordinary",
- sources: [HOME],
- sourceUnknown: false,
- createdAt: 0,
- updatedAt: 0,
- },
- ];
- const memory: MemoryService = {
- facts: async () => records,
- read: async () => records.map((record) => record.text).join("\n"),
- query: async () => [],
- recall: async () => [],
- capture: async () => 0,
- };
- await built.app.upsertGroups([
- { groupId: "room", principalId: "alice" },
- { groupId: "room", principalId: "bob" },
- ]);
- await built.config.setSharingPosture("org:default-org", "open");
- await built.config.setSharingPosture(HOME, "open");
- await built.config.setSharingPosture(ROOM, "open");
- const session = await built.sessions.getOrCreateByThread("synthetic-room", "group", ROOM);
- await built.sessions.addParticipant(session.id, "alice");
- const { lease } = await built.sessions.acquireLease(session.id, "turn");
- assert.ok(lease);
- const append = (type: "user" | "assistant" | "system", payload: unknown) =>
- built.sessions.append(lease, {
- type,
- payload,
- scopeLabel: ROOM,
- });
- const snapshot = () => buildMemoryContextSnapshot({ targetScope: ROOM, audience });
- const checkpoint = async (throughSeq = -1) => {
- const current = snapshot();
- return append("system", {
- kind: "memory_context",
- snapshot: current,
- throughSeq,
- });
- };
- const server = createServer(
- {
- ...built.app,
- currentScopeMembers: async () => audience,
- isCurrentSharedScopeMember: async (id, scope) => scope === ROOM && audience.some((person) => person.id === id),
- },
- {
- signingSecret: SECRET,
- sessions: built.sessions,
- memory,
- config: built.config,
- },
- );
- await new Promise<void>((resolve) => server.listen(0, resolve));
- const base = `http://localhost:${(server.address() as AddressInfo).port}`;
- const token = await mintCapabilityToken(
- {
- actorId: "alice",
- scopeId: ROOM,
- sessionId: session.id,
- members: audience,
- liveActor: true,
- aud: CONTROL_PLANE_AUD,
- exp: Date.now() + 60_000,
- memory: { read: [HOME], write: HOME },
- },
- SECRET,
- );
- return {
- built,
- session,
- append,
- checkpoint,
- setRecords: (next: MemoryFact[]) => {
- records = next;
- },
- records: () => records,
- setAudience: (next: Principal[]) => {
- audience = next;
- },
- get: (query = "", id = session.id) =>
- fetch(`${base}/v1/conversations/${id}${query}`, { headers: { "x-agent-capability": token } }),
- list: () => fetch(`${base}/v1/conversations`, { headers: { "x-agent-capability": token } }),
- post: (suffix: string, body: unknown = {}) =>
- fetch(`${base}/v1/conversations/${session.id}${suffix}`, {
- method: "POST",
- headers: { "x-agent-capability": token, "content-type": "application/json" },
- body: JSON.stringify(body),
- }),
- memoryRead: () => fetch(`${base}/v1/memory/self`, { headers: { "x-agent-capability": token } }),
- human: () => {
- const path = `/v1/sessions/${session.id}?viewer=alice`;
- return fetch(`${base}${path}`, { headers: signedRequestHeaders(SECRET, "GET", path, "") });
- },
- close: async () => {
- await built.sessions.releaseLease(lease);
- await new Promise<void>((resolve) => server.close(() => resolve()));
- },
- };
-}
-
-for (const change of ["remove", "reclassify", "posture", "audience"] as const) {
- test(`agent transcript preserves retained context after ${change}`, async () => {
- const f = await fixture();
- try {
- await f.checkpoint();
- await f.append("assistant", { text: "PRIVATE_SENTINEL" });
- assert.equal((await f.get()).status, 200);
- if (change === "remove") f.setRecords([]);
- if (change === "reclassify") f.setRecords(f.records().map((r) => ({ ...r, sensitivity: "restricted" })));
- if (change === "posture") await f.built.config.setSharingPosture(HOME, "isolated");
- if (change === "audience")
- f.setAudience([
- { id: "alice", type: "internal" },
- { id: "eve", type: "internal" },
- ]);
- const response = await f.get();
- assert.equal(response.status, 200);
- assert.match(await response.text(), /PRIVATE_SENTINEL/);
- if (change !== "audience") {
- const fresh = await f.memoryRead();
- assert.equal(fresh.status, 200);
- assert.doesNotMatch(await fresh.text(), /PRIVATE_SENTINEL/);
- }
- const human = await f.human();
- assert.equal(human.status, 200);
- assert.match(await human.text(), /PRIVATE_SENTINEL/);
- } finally {
- await f.close();
- }
- });
-}
-
-test("agent transcripts use viewer access for old, cross-scope and project sessions", async () => {
- const f = await fixture();
- try {
- const project = await f.built.app.createProject("alice", "Research");
- assert.ok(project);
- const crossScope = await f.built.sessions.getOrCreateByThread("web:alice:old", "dm", HOME);
- const projectSession = await f.built.sessions.getOrCreateByThread("web:alice:project", "group", project.scopeId);
- const hidden = await f.built.sessions.getOrCreateByThread("web:mallory:hidden", "dm", "personal:mallory");
- for (const session of [f.session, crossScope, projectSession, hidden]) {
- const owner = session.id === hidden.id ? "mallory" : "alice";
- await f.built.sessions.addParticipant(session.id, owner);
- if (session.id === f.session.id) await f.append("assistant", { text: "readable history" });
- else {
- const { lease: active } = await f.built.sessions.acquireLease(session.id, "turn");
- assert.ok(active);
- if (session.id === projectSession.id)
- await f.built.sessions.append(active, {
- type: "system",
- scopeLabel: session.scopeId,
- payload: {
- kind: "memory_context",
- snapshot: buildMemoryContextSnapshot({ targetScope: session.scopeId, audience: [] }),
- throughSeq: -1,
- },
- });
- await f.built.sessions.append(active, {
- type: "assistant",
- scopeLabel: session.scopeId,
- payload: { text: "readable history" },
- });
- await f.built.sessions.releaseLease(active);
- }
- const response = await f.get("", session.id);
- assert.equal(response.status, owner === "alice" ? 200 : 404);
- if (owner === "alice") assert.match(await response.text(), /readable history/);
- else assert.doesNotMatch(await response.text(), /readable history/);
- }
- } finally {
- await f.close();
- }
-});
-
-test("agent transcript retains viewer-visible history across memory checkpoints", async () => {
- const f = await fixture();
- try {
- await f.checkpoint();
- const old = await f.append("assistant", { text: "PRIVATE_SENTINEL" });
- await f.built.sessions.addPin(f.session.id, { addedBy: "alice", text: "PRIVATE_SENTINEL" });
- await f.built.sessions.updateTitle(f.session.id, "PRIVATE_SENTINEL");
- f.setAudience([{ id: "alice", type: "internal" }]);
- await f.checkpoint(old.seq);
- await f.append("user", { text: "new question" });
- await f.append("assistant", { text: "SAFE_REPLY" });
- const response = await f.get();
- assert.equal(response.status, 200);
- const body = (await response.json()) as { entries: unknown[] };
- assert.match(JSON.stringify(body.entries), /SAFE_REPLY/);
- assert.match(JSON.stringify(body.entries), /PRIVATE_SENTINEL/);
- assert.doesNotMatch(JSON.stringify(body.entries), /memory_context/);
- const oldPage = await f.get(`?beforeSeq=${old.seq + 1}`);
- assert.equal(oldPage.status, 200);
- assert.match(JSON.stringify(((await oldPage.json()) as { entries: unknown[] }).entries), /PRIVATE_SENTINEL/);
- assert.match(await (await f.human()).text(), /PRIVATE_SENTINEL/);
- } finally {
- await f.close();
- }
-});
-
-test("explicit memory reads preserve the target checkpoint", async () => {
- const f = await fixture();
- try {
- await f.checkpoint();
- await f.append("assistant", { text: "SAFE_REPLY" });
- assert.equal((await f.get()).status, 200);
- assert.equal((await f.get()).status, 200);
- const memory = await f.memoryRead();
- assert.equal(memory.status, 200);
- assert.match(await memory.text(), /PRIVATE_SENTINEL/);
- assert.equal((await f.get()).status, 200);
- } finally {
- await f.close();
- }
-});
-
-test("adding an authorized fact preserves an otherwise compatible checkpoint", async () => {
- const f = await fixture();
- try {
- await f.checkpoint();
- await f.append("assistant", { text: "SAFE_REPLY" });
- f.setRecords([...f.records(), { ...f.records()[0]!, id: "2", text: "- NEW_FACT" }]);
- assert.equal((await f.get()).status, 200);
- } finally {
- await f.close();
- }
-});
-
-test("agent list and patch responses include viewer-visible titles", async () => {
- const f = await fixture();
- try {
- await f.built.sessions.updateTitle(f.session.id, "PRIVATE_TITLE");
- const listed = await f.list();
- assert.equal(listed.status, 200);
- const listing = await listed.text();
- assert.match(listing, new RegExp(f.session.id));
- assert.match(listing, /PRIVATE_TITLE/);
- const patch = await f.post("", { archived: true });
- assert.equal(patch.status, 200);
- assert.match(await patch.text(), /PRIVATE_TITLE/);
- const renamed = await f.post("", { title: "safe title" });
- assert.equal(renamed.status, 200);
- const renamedBody = await renamed.text();
- assert.match(renamedBody, /safe title/);
- } finally {
- await f.close();
- }
-});
diff --git a/test/batch-paths.test.ts b/test/batch-paths.test.ts
index 4f31ee273..633ef4074 100644
--- a/test/batch-paths.test.ts
+++ b/test/batch-paths.test.ts
@@ -1,6 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";
import { randomUUID } from "node:crypto";
+import pg from "pg";
import { createMemoryMap, createPostgresMapFactory } from "../src/persistence/durable-map.ts";
import { createMemorySessionStore } from "../src/sessions/memory-session-store.ts";
import { createPostgresSessionStore } from "../src/sessions/postgres-session-store.ts";
@@ -64,6 +65,54 @@ for (const backend of ["memory", "postgres"] as const) {
},
);
+ test(`batch appends keep order, roll back atomically and check leases: ${backend}`, { skip }, async () => {
+ const store =
+ backend === "memory" ? createMemorySessionStore() : createPostgresSessionStore(process.env.DATABASE_URL!);
+ const scope = scopeId("personal", "batch");
+ for (let i = 0; i < 2; i++) {
+ const session = await store.getOrCreateByThread(`web:batch:${randomUUID()}`, "dm", scope);
+ const { lease } = await store.acquireLease(session.id);
+ assert.ok(lease);
+ const entries = await store.appendMany(
+ lease,
+ Array.from({ length: 5 }, (_, n) => ({
+ type: "user" as const,
+ payload: { text: `message ${n}` },
+ scopeLabel: scope,
+ })),
+ );
+ assert.deepEqual(
+ entries.map((entry) => entry.seq),
+ [0, 1, 2, 3, 4],
+ );
+ assert.deepEqual((await store.getEntries(session.id)).slice(-5), entries);
+ assert.deepEqual(await store.appendMany(lease, []), []);
+ const batch = [5, 6].map((n) => ({
+ type: "user" as const,
+ payload: { text: `message ${n}` },
+ scopeLabel: scope,
+ }));
+ if (backend === "postgres") {
+ const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
+ try {
+ const constraint = `items_batch_${randomUUID().replaceAll("-", "")}`;
+ await pool.query(
+ `ALTER TABLE items ADD CONSTRAINT ${constraint} CHECK (session_id <> '${session.id}' OR seq <> 6) NOT VALID`,
+ );
+ try {
+ await assert.rejects(store.appendMany(lease, batch));
+ assert.equal((await store.getEntries(session.id)).length, 5);
+ } finally {
+ await pool.query(`ALTER TABLE items DROP CONSTRAINT ${constraint}`);
+ }
+ } finally {
+ await pool.end();
+ }
+ }
+ await store.releaseLease(lease);
+ await assert.rejects(store.appendMany(lease, batch), /lease/);
+ }
+ });
test(`session goals live on the session row behind its lease: ${backend}`, { skip }, async () => {
const store =
backend === "memory" ? createMemorySessionStore() : createPostgresSessionStore(process.env.DATABASE_URL!);
@@ -84,13 +133,17 @@ for (const backend of ["memory", "postgres"] as const) {
const { lease } = await store.acquireLease(id);
assert.ok(lease);
await store.setGoal(lease, goal(`first ${i}`));
- const notice = { type: "system" as const, payload: { kind: "goal_notice", n: i }, scopeLabel: scope };
+ const notice = {
+ type: "system" as const,
+ payload: { kind: "goal_cancelled", objective: `goal ${i}`, cancelledBy: "u" },
+ scopeLabel: scope,
+ };
await store.setGoal(lease, { ...goal(`goal ${i}`), status: "paused" }, [notice]);
await store.releaseLease(lease);
await assert.rejects(store.setGoal(lease, goal("stale lease"), [notice]), /valid session lease/);
assert.deepEqual(
(await store.getEntries(id)).map((entry) => entry.payload),
- [notice.payload],
+ [{ ...notice.payload, cause: "system" }],
"the notice lands with the goal and not with a rejected write",
);
}
diff --git a/test/context-compaction.test.ts b/test/context-compaction.test.ts
index 45d90a7a2..d0dfc172e 100644
--- a/test/context-compaction.test.ts
+++ b/test/context-compaction.test.ts
@@ -1056,6 +1056,7 @@ test("runtime handoff continues once with saved results under the original run a
type: "tool_result",
payload: {
tool: "runtime",
+ callId: `runtime-${input.runId}`,
runId: input.runId,
actorId: actor.id,
runtimeHandoff: { choice, lifetime: "task" },
@@ -1127,6 +1128,7 @@ test("a retry restores a committed runtime decision after reset crashes, without
type: "tool_result",
payload: {
tool: "runtime",
+ callId: `runtime-${input.runId}`,
runId: input.runId,
actorId: actor.id,
runtimeHandoff: { choice, lifetime: "task" },
@@ -1167,7 +1169,13 @@ test("only a cron automation receives task-runtime authority and each fire start
await input.emit({
type: "tool_result",
scopeLabel: input.scopeLabel,
- payload: { tool: "runtime", runId: input.runId, actorId: actor.id, runtimeHandoff: result.handoff },
+ payload: {
+ tool: "runtime",
+ callId: `runtime-${input.runId}`,
+ runId: input.runId,
+ actorId: actor.id,
+ runtimeHandoff: result.handoff,
+ },
});
return { reply: "", runtimeHandoff: result.handoff };
}
diff --git a/test/context-recovery.test.ts b/test/context-recovery.test.ts
index 539a8254c..92cfad487 100644
--- a/test/context-recovery.test.ts
+++ b/test/context-recovery.test.ts
@@ -6,7 +6,6 @@ import { createMemorySessionStore } from "../src/sessions/memory-session-store.t
import { estimateHistoryTokens, forModelContext } from "../src/harness/context-compaction.ts";
import { contextSummaryPayload, createContextSummaryPayload } from "../src/sessions/session-store.ts";
-import { filterHistoryForAudience } from "../src/resolution/context-filter.ts";
import { reconstructMessagesFromHistory } from "../src/harness/replay.ts";
import { countTokens } from "../src/util/tokens.ts";
@@ -200,28 +199,6 @@ test("oversized entries and orphaned parallel tool results cannot bypass the rec
}
});
-test("recent recovery cannot recover an unauthorized summary", async () => {
- const f = await fixture();
- try {
- await f.sessions.append(f.input.lease, {
- type: "system",
- payload: createContextSummaryPayload(11, "private sentinel"),
- scopeLabel: "personal:other",
- });
- await refresh(f);
- f.input.visibleHistory = filterHistoryForAudience(
- f.input.visibleHistory,
- [{ id: "test", type: "internal" }],
- "personal:test",
- "org:test",
- );
- const history = await f.compaction.compactRecent(f.input);
- assert.doesNotMatch(JSON.stringify(history), /private sentinel/);
- } finally {
- await f.sessions.releaseLease(f.input.lease);
- }
-});
-
test("recent recovery drops an oversized request inside a completed turn", async () => {
const f = await fixture();
try {
@@ -244,39 +221,3 @@ test("recent recovery drops an oversized request inside a completed turn", async
await f.sessions.releaseLease(f.input.lease);
}
});
-
-test("a foreign summary still supplies the durable exclusion floor", async () => {
- const f = await fixture();
- try {
- await f.sessions.append(f.input.lease, {
- type: "system",
- payload: createContextSummaryPayload(11, "hidden summary sentinel"),
- scopeLabel: "personal:other",
- });
- await f.sessions.append(f.input.lease, {
- type: "user",
- payload: { text: "Latest request." },
- scopeLabel: "personal:test",
- });
- await refresh(f);
- f.input.visibleHistory = filterHistoryForAudience(
- f.input.visibleHistory,
- [{ id: "test", type: "internal" }],
- "personal:test",
- "org:test",
- );
- const history = await f.compaction.compactRecent(f.input);
- assert.equal(contextSummaryPayload(history[0]!)!.throughSeq, 11);
- const reloaded = filterHistoryForAudience(
- forModelContext((await f.sessions.getContextWindow(f.input.session.id)).entries),
- [{ id: "test", type: "internal" }],
- "personal:test",
- "org:test",
- );
- assert.deepEqual(reloaded, history);
- assert.ok(estimateHistoryTokens(reloaded) <= 300);
- assert.doesNotMatch(JSON.stringify(reloaded), /old message|hidden summary sentinel/);
- } finally {
- await f.sessions.releaseLease(f.input.lease);
- }
-});
diff --git a/test/early-task-ack.test.ts b/test/early-task-ack.test.ts
index ad5ee7fbf..4641bce95 100644
--- a/test/early-task-ack.test.ts
+++ b/test/early-task-ack.test.ts
@@ -93,7 +93,7 @@ for (const action of ["read", "react", "post", "delegated-read"] as const) {
assert.equal((await built.deliveries.pending("slack")).length, 0);
await turn.emit({
type: "tool_call",
- payload: { tool: "slack", action: action === "delegated-read" ? "read" : action },
+ payload: { tool: "slack", callId: `slack-${action}`, action: action === "delegated-read" ? "read" : action },
scopeLabel: turn.scopeLabel,
});
started();
diff --git a/test/file-sharing.test.ts b/test/file-sharing.test.ts
index 22384b453..7a89c4b90 100644
--- a/test/file-sharing.test.ts
+++ b/test/file-sharing.test.ts
@@ -21,7 +21,7 @@ import {
MAX_ATTACHMENT_BYTES,
environmentNote,
inboundIssueList,
- fileEventPayload,
+ inboundIssuesNote,
inboundManifest,
materializeInbound,
withoutAlreadyIngested,
@@ -120,16 +120,11 @@ test("inboundIssueList is empty when nothing failed, and names each problem othe
assert.doesNotMatch(joined, /your own words|NOT received|⚠️|\(note:/);
});
-test("fileEventPayload tags direction and renders admin-readable text via the kind", () => {
- const inbound = fileEventPayload("in", ["x.png — too many files in one message"]);
- assert.equal(inbound.kind, "file_event");
- assert.equal(inbound.direction, "in");
- assert.match(inbound.text, /did not reach \.\/inbox\//);
- assert.match(inbound.text, /x\.png/);
- const outbound = fileEventPayload("out", ["chart.png was too large to send"]);
- assert.equal(outbound.direction, "out");
- assert.match(outbound.text, /did not go out/);
- assert.match(outbound.text, /chart\.png/);
+test("inboundIssuesNote names the files that did not reach the inbox and is empty without issues", () => {
+ assert.equal(inboundIssuesNote([]), "");
+ const text = inboundIssuesNote(["x.png — too many files in one message"]);
+ assert.match(text, /did not reach \.\/inbox\//);
+ assert.match(text, /x\.png/);
});
test("inboundManifest attributes each file to its poster when the author is known", () => {
diff --git a/test/fixtures/legacy-participant-activity-triggers.sql b/test/fixtures/legacy-participant-activity-triggers.sql
new file mode 100644
index 000000000..97263a9b6
--- /dev/null
+++ b/test/fixtures/legacy-participant-activity-triggers.sql
@@ -0,0 +1,107 @@
+ALTER TABLE sessions ADD COLUMN IF NOT EXISTS user_last_activity bigint;
+ALTER TABLE sessions ADD COLUMN IF NOT EXISTS user_last_activity_seq integer;
+ALTER TABLE sessions ADD COLUMN IF NOT EXISTS user_activity_version smallint NOT NULL DEFAULT 1;
+ALTER TABLE participants ADD COLUMN IF NOT EXISTS last_activity bigint;
+ALTER TABLE participants ADD COLUMN IF NOT EXISTS activity_version smallint NOT NULL DEFAULT 0;
+CREATE OR REPLACE FUNCTION safe_jsonb(t text)
+ RETURNS jsonb
+ LANGUAGE plpgsql
+ IMMUTABLE
+AS $function$
+ BEGIN
+ RETURN t::jsonb;
+ EXCEPTION WHEN others THEN
+ BEGIN RETURN regexp_replace(t, '(^|[^\\])(\\\\)*(\\u0000)+', '\1\2', 'g')::jsonb;
+ EXCEPTION WHEN others THEN RETURN NULL;
+ END;
+ END $function$
+;
+CREATE OR REPLACE FUNCTION session_entry_boundary_v1()
+ RETURNS trigger
+ LANGUAGE plpgsql
+AS $function$
+ BEGIN
+ PERFORM 1 FROM sessions WHERE id = NEW.session_id FOR NO KEY UPDATE;
+ IF NOT FOUND THEN RETURN NULL; END IF;
+ RETURN NEW;
+ END $function$
+;
+CREATE OR REPLACE FUNCTION participant_activity_on_insert_v3()
+ RETURNS trigger
+ LANGUAGE plpgsql
+AS $function$
+ DECLARE boundary INT;
+ DECLARE session_created BIGINT;
+ BEGIN
+ PERFORM 1 FROM participants
+ WHERE session_id = NEW.session_id AND principal_id = NEW.principal_id
+ FOR UPDATE;
+ SELECT created_at INTO session_created FROM sessions WHERE id = NEW.session_id FOR NO KEY UPDATE;
+ IF NOT FOUND THEN RETURN NULL; END IF;
+ SELECT COALESCE(MAX(seq) + 1, 0) INTO boundary FROM session_entries WHERE session_id = NEW.session_id;
+ NEW.valid_from_seq := boundary;
+ IF NEW.last_activity IS NULL THEN
+ NEW.last_activity := CASE WHEN boundary = 0 THEN session_created ELSE NEW.valid_from END;
+ END IF;
+ NEW.activity_version := 3;
+ RETURN NEW;
+ END $function$
+;
+CREATE OR REPLACE FUNCTION participant_activity_on_reopen_v3()
+ RETURNS trigger
+ LANGUAGE plpgsql
+AS $function$
+ DECLARE boundary INT;
+ DECLARE session_created BIGINT;
+ DECLARE session_user_activity BIGINT;
+ DECLARE session_user_seq INT;
+ BEGIN
+ IF (OLD.valid_to IS NULL) <> (NEW.valid_to IS NULL) THEN
+ SELECT created_at, user_last_activity, user_last_activity_seq
+ INTO session_created, session_user_activity, session_user_seq
+ FROM sessions WHERE id = NEW.session_id FOR NO KEY UPDATE;
+ IF NOT FOUND THEN RETURN NULL; END IF;
+ SELECT COALESCE(MAX(seq) + 1, 0) INTO boundary FROM session_entries WHERE session_id = NEW.session_id;
+ IF NEW.valid_to IS NULL THEN
+ NEW.valid_from_seq := boundary;
+ NEW.last_activity := CASE WHEN boundary = 0 THEN session_created ELSE NEW.valid_from END;
+ ELSE
+ NEW.valid_to_seq := boundary;
+ NEW.last_activity := CASE WHEN COALESCE(session_user_seq, -1) >= COALESCE(NEW.valid_from_seq, 0)
+ THEN COALESCE(session_user_activity, NEW.last_activity, session_created)
+ ELSE COALESCE(NEW.last_activity, session_created) END;
+ END IF;
+ NEW.activity_version := 3;
+ END IF;
+ RETURN NEW;
+ END $function$
+;
+CREATE OR REPLACE FUNCTION participant_activity_on_entry_v3()
+ RETURNS trigger
+ LANGUAGE plpgsql
+AS $function$
+ BEGIN
+ IF NEW.type = 'user' AND NOT COALESCE(safe_jsonb(NEW.payload) @> '{"overheard":true}'::jsonb, FALSE) THEN
+ UPDATE sessions
+ SET user_last_activity = NEW.created_at, user_last_activity_seq = NEW.seq
+ WHERE id = NEW.session_id;
+ IF NOT FOUND THEN RAISE EXCEPTION 'session % does not exist', NEW.session_id USING ERRCODE = 'foreign_key_violation'; END IF;
+ END IF;
+ RETURN NEW;
+ END $function$
+;
+CREATE OR REPLACE FUNCTION session_child_parent_exists()
+ RETURNS trigger
+ LANGUAGE plpgsql
+AS $function$
+ BEGIN
+ PERFORM 1 FROM sessions WHERE id = NEW.session_id FOR KEY SHARE;
+ IF NOT FOUND THEN RETURN NULL; END IF;
+ RETURN NEW;
+ END $function$
+;
+CREATE TRIGGER participant_activity_entry_v3 AFTER INSERT ON session_entries FOR EACH ROW EXECUTE FUNCTION participant_activity_on_entry_v3();
+CREATE TRIGGER session_entry_boundary_v1 BEFORE INSERT ON session_entries FOR EACH ROW EXECUTE FUNCTION session_entry_boundary_v1();
+CREATE TRIGGER session_llm_request_parent BEFORE INSERT ON session_llm_requests FOR EACH ROW EXECUTE FUNCTION session_child_parent_exists();
+CREATE TRIGGER participant_activity_reopen_v3 BEFORE UPDATE ON participants FOR EACH ROW EXECUTE FUNCTION participant_activity_on_reopen_v3();
+CREATE TRIGGER participant_activity_boundary_insert_v3 BEFORE INSERT ON participants FOR EACH ROW EXECUTE FUNCTION participant_activity_on_insert_v3();
diff --git a/test/goal-compaction.test.ts b/test/goal-compaction.test.ts
index ac8a0b2a6..3e16ae1bb 100644
--- a/test/goal-compaction.test.ts
+++ b/test/goal-compaction.test.ts
@@ -9,7 +9,7 @@ import type { ScopeId } from "../src/types.ts";
const scope = "personal:[email protected]" as ScopeId;
-test("compaction leaves the session goal on its row and writes no goal entries, ignoring legacy snapshots", async () => {
+test("compaction leaves the session goal on its row and writes no goal entries", async () => {
const sessions = createMemorySessionStore();
const session = await sessions.getOrCreateByThread("goal", "dm", scope);
const { lease } = await sessions.acquireLease(session.id);
@@ -19,11 +19,6 @@ test("compaction leaves the session goal on its row and writes no goal entries,
tokensUsed: 1234,
};
await sessions.setGoal(lease, goal);
- await sessions.append(lease, {
- type: "system",
- payload: { kind: "goal", goal: { ...goal, objective: "legacy snapshot" } },
- scopeLabel: scope,
- });
for (let i = 0; i < 10; i++) {
await sessions.append(lease, { type: "user", payload: { text: `message ${i} `.repeat(100) }, scopeLabel: scope });
}
@@ -52,7 +47,7 @@ test("compaction leaves the session goal on its row and writes no goal entries,
const goalEntries = (await sessions.getEntries(session.id)).filter(
(entry) => (entry.payload as { kind?: string }).kind === "goal",
);
- assert.equal(goalEntries.length, 1, "only the legacy snapshot; compaction does not copy goals into the log");
+ assert.equal(goalEntries.length, 0, "compaction does not copy goals into the log");
assert.deepEqual((await sessions.goals([session.id])).get(session.id), goal);
});
diff --git a/test/memory-context-boundary.test.ts b/test/memory-context-boundary.test.ts
deleted file mode 100644
index df644a765..000000000
--- a/test/memory-context-boundary.test.ts
+++ /dev/null
@@ -1,289 +0,0 @@
-import { test } from "node:test";
-import { buildMemoryContextSnapshot, nextMemoryContext } from "../src/memory/context-boundary.ts";
-import assert from "node:assert/strict";
-import { mkdtempSync } from "node:fs";
-import { tmpdir } from "node:os";
-import { join } from "node:path";
-import { createOrchestrator, type OrchestratorInput } from "../src/core/orchestrator.ts";
-import { createIdentityService } from "../src/identity/identity-service.ts";
-import { createMemoryConfigStore } from "../src/resolution/config-store.ts";
-import { createAclStore } from "../src/acl/acl-store.ts";
-import { createResolutionService } from "../src/resolution/resolution-service.ts";
-import { createMemorySessionStore } from "../src/sessions/memory-session-store.ts";
-import { createLocalWorkspaceStore } from "../src/workspace/workspace-store.ts";
-import { createMemoryService } from "../src/memory/memory-service.ts";
-import type { MemoryService } from "../src/memory/memory-service.ts";
-import { createModelGateway } from "../src/model/model-gateway.ts";
-import { createAuditLog } from "../src/audit/audit-log.ts";
-import { createRateLimiter } from "../src/ratelimit/rate-limiter.ts";
-import { createMockHarness } from "../src/harness/mock-harness.ts";
-import { createDeployStore } from "../src/deploy/deploy-store.ts";
-import { createDockerDeployProvider } from "../src/deploy/docker-deploy-provider.ts";
-import { createDeployService } from "../src/deploy/deploy-service.ts";
-import { createMemoryFileArtifactStore } from "../src/files/file-artifact-store.ts";
-import { createMemoryDurableByteStore } from "../src/files/durable-byte-store.ts";
-import type { HarnessTurnInput, Harness } from "../src/harness/harness.ts";
-import type { Sandbox } from "../src/sandbox/sandbox.ts";
-import type { Conversation, Principal, SessionEntry } from "../src/types.ts";
-
-const ORG = "default-org";
-const actor: Principal = { id: "U1", type: "internal" };
-const dm = (thread: string, text: string): OrchestratorInput => ({
- surface: "test",
- actor,
- conversation: { kind: "dm", threadRef: thread, audience: [actor] } as Conversation,
- origin: { kind: "direct" },
- text,
-});
-
-function fakeSandbox(): Sandbox {
- const unreached = () => {
- throw new Error("fakeSandbox: a conversational memory turn must not touch the sandbox");
- };
- return {
- profile: {
- backend: "fake",
- writablePersistence: "snapshot_to_workspace",
- processSessions: false,
- },
- provision: unreached as never,
- run: unreached as never,
- readFile: unreached as never,
- writeFile: unreached as never,
- writeFileBytes: unreached as never,
- readFileBytes: unreached as never,
- listDir: unreached as never,
- removeDir: unreached as never,
- teardown: unreached as never,
- };
-}
-
-function buildOrchestrator(harness: Harness, memory: MemoryService, sessions = createMemorySessionStore()) {
- const config = createMemoryConfigStore(ORG);
- const acl = createAclStore();
- const auditLog = createAuditLog();
- const workspace = createLocalWorkspaceStore(mkdtempSync(join(tmpdir(), "mca-")));
- const deploy = createDeployService({
- deployStore: createDeployStore(),
- provider: createDockerDeployProvider(),
- deployDir: join(tmpdir(), "mca-deploy"),
- auditLog,
- acl,
- });
- return createOrchestrator({
- identity: createIdentityService(),
- resolution: createResolutionService(ORG, config, acl),
- sessions,
- workspace,
- files: createMemoryFileArtifactStore(createMemoryDurableByteStore()),
- sandbox: fakeSandbox(),
- modelGateway: createModelGateway(),
- auditLog,
- rateLimiter: createRateLimiter({ maxPerWindow: 1000, windowMs: 60_000 }),
- harness,
- memory: memory ?? createMemoryService(workspace),
- memoryStrategy: {},
- deploy,
- acl,
- });
-}
-
-for (const change of ["source", "audience"] as const)
- test(`${change} change ${change === "source" ? "preserves" : "isolates"} retained memory across restart`, async () => {
- const sessions = createMemorySessionStore();
- let content = "- PRIVATE_SENTINEL";
- const memory: MemoryService = {
- read: async () => content,
- query: async () => [content],
- recall: async () => [{ text: content, score: 0.2 }],
- capture: async () => 0,
- };
- const seen: HarnessTurnInput[] = [];
- const reopened: unknown[] = [];
- let resets = 0;
- const base = createMockHarness();
- const harness: Harness = {
- ...base,
- turns: {
- ...base.turns,
- resetSession: async () => {
- resets++;
- },
- runTurn: async (turn) => {
- seen.push(turn);
- reopened.push(await turn.tools.history("PRIVATE_SENTINEL"));
- return base.turns.runTurn(turn);
- },
- },
- };
- let audience: Principal[] = [actor, { id: "U2", type: "internal" }];
- const input = (text: string): OrchestratorInput => ({
- ...dm("memory-boundary", text),
- conversation: { kind: "group", channelRef: "room", threadRef: "memory-boundary", audience },
- });
- let orch = buildOrchestrator(harness, memory, sessions);
- const first = await orch.handleTurn(input("!sysprompt"));
- assert.equal(first.status, "ok");
- assert.match(seen[0]!.environment ?? "", /PRIVATE_SENTINEL/);
- const session = (await sessions.getByThread("memory-boundary"))!;
- const lease = (await sessions.acquireLease(session.id)).lease!;
- await sessions.append(lease, {
- type: "system",
- scopeLabel: session.scopeId,
- payload: {
- kind: "context_summary",
- throughSeq: await sessions.latestEntrySeq(session.id),
- text: "PRIVATE_SENTINEL summarized",
- },
- });
- await sessions.append(lease, {
- type: "assistant",
- scopeLabel: session.scopeId,
- payload: { text: "PRIVATE_SENTINEL" },
- });
- await sessions.releaseLease(lease);
- content = "";
- if (change === "audience") audience = [...audience, { id: "U3", type: "internal" }];
- orch = buildOrchestrator(harness, memory, sessions);
- const second = await orch.handleTurn({
- ...input("safe second request"),
- priorTurns: [{ role: "assistant", text: "PRIVATE_SENTINEL" }],
- });
- assert.equal(second.status, "ok");
- assert.doesNotMatch(seen[1]!.environment ?? "", /PRIVATE_SENTINEL/);
- const retained = JSON.stringify({
- history: seen[1]!.history,
- priorTurns: seen[1]!.priorTurns,
- });
- if (change === "source") {
- assert.match(retained, /PRIVATE_SENTINEL/);
- assert.match(JSON.stringify(reopened[1]), /PRIVATE_SENTINEL/);
- assert.equal(resets, 0);
- } else {
- assert.doesNotMatch(retained, /PRIVATE_SENTINEL/);
- assert.deepEqual(reopened[1], []);
- assert.equal(resets, 1);
- }
- const third = await orch.handleTurn({ ...input("safe third request"), actor: audience[1]! });
- assert.equal(third.status, "ok");
- if (change === "audience") assert.doesNotMatch(JSON.stringify(seen[2]!.history), /PRIVATE_SENTINEL/);
- else assert.match(JSON.stringify(seen[2]!.history), /PRIVATE_SENTINEL/);
- assert.match(JSON.stringify(seen[2]!.history), /safe second request/);
- assert.equal(resets, change === "audience" ? 1 : 0);
- assert.match(JSON.stringify(await sessions.getEntries(session.id)), /PRIVATE_SENTINEL/);
- });
-
-for (const operation of ["read", "search"] as const)
- test(`a memory ${operation} outside the starting snapshot preserves context next turn`, async () => {
- const sessions = createMemorySessionStore();
- let content = "- original";
- const memory: MemoryService = {
- read: async () => content,
- query: async () => [content],
- recall: async () => [{ text: content, score: 0.2 }],
- capture: async () => 0,
- };
- const base = createMockHarness();
- const seen: HarnessTurnInput[] = [];
- const harness: Harness = {
- ...base,
- turns: {
- ...base.turns,
- runTurn: async (turn) => {
- seen.push(turn);
- if (seen.length === 1) {
- content = "- TRANSIENT_SENTINEL";
- const result =
- operation === "read" ? await turn.tools.memoryRead() : await turn.tools.memorySearch("SENTINEL");
- await turn.emit({ type: "tool_result", scopeLabel: turn.scopeLabel, payload: { text: result } });
- }
- return base.turns.runTurn(turn);
- },
- },
- };
- const orch = buildOrchestrator(harness, memory, sessions);
- assert.equal((await orch.handleTurn(dm("transient-memory", "first"))).status, "ok");
- content = "- original";
- assert.equal((await orch.handleTurn(dm("transient-memory", "second"))).status, "ok");
- assert.match(JSON.stringify(seen[1]!.history), /TRANSIENT_SENTINEL/);
- const session = (await sessions.getByThread("transient-memory"))!;
- assert.match(JSON.stringify(await sessions.getEntries(session.id)), /TRANSIENT_SENTINEL/);
- });
-
-for (const change of ["audience", "type", "scope"] as const)
- test(`an empty-memory checkpoint still isolates a changed ${change}`, () => {
- const before = buildMemoryContextSnapshot({ targetScope: "group:room", audience: [actor] });
- let audience: Principal[] = [actor];
- if (change === "audience") audience = [actor, { id: "U2", type: "internal" }];
- if (change === "type") audience = [{ ...actor, type: "guest" }];
- const after = buildMemoryContextSnapshot({
- targetScope: change === "scope" ? "group:other" : "group:room",
- audience,
- });
- const checkpoint = nextMemoryContext([], before, -1);
- const entries: SessionEntry[] = [
- {
- sessionId: "synthetic",
- parentSeq: null,
- seq: 0,
- type: "system",
- scopeLabel: "group:room",
- createdAt: 1,
- payload: checkpoint,
- },
- ];
- assert.equal(nextMemoryContext(entries, after, 5).throughSeq, 5);
- assert.equal(nextMemoryContext(entries, before, 5).throughSeq, -1);
- });
-
-test("audience identity is stable across speaker order and team membership changes", () => {
- const audience: Principal[] = [actor, { id: "U2", type: "internal" }];
- assert.deepEqual(
- buildMemoryContextSnapshot({ targetScope: "group:room", audience }),
- buildMemoryContextSnapshot({
- targetScope: "group:room",
- audience: audience.toReversed().map((p) => ({ ...p, teamIds: ["new-team"] })),
- }),
- );
-});
-
-test("a failed audience reset is retried before recording the new checkpoint", async () => {
- const sessions = createMemorySessionStore();
- const memory: MemoryService = {
- read: async () => "",
- query: async () => [],
- recall: async () => [],
- capture: async () => 0,
- };
- const base = createMockHarness();
- let resets = 0;
- const seen: HarnessTurnInput[] = [];
- const harness: Harness = {
- ...base,
- turns: {
- ...base.turns,
- resetSession: async () => {
- if (++resets === 1) throw new Error("synthetic reset failure");
- },
- runTurn: async (turn) => {
- seen.push(turn);
- return base.turns.runTurn(turn);
- },
- },
- };
- const orch = buildOrchestrator(harness, memory, sessions);
- const input: OrchestratorInput = {
- ...dm("reset-retry", "PRIVATE_SENTINEL"),
- conversation: { kind: "group", channelRef: "retry-room", threadRef: "reset-retry", audience: [actor] },
- };
- assert.equal((await orch.handleTurn(input)).status, "ok");
- input.conversation.audience = [actor, { id: "U2", type: "internal" }];
- input.text = "new audience";
- await assert.rejects(orch.handleTurn(input), /synthetic reset failure/);
- assert.equal((await orch.handleTurn(input)).status, "ok");
- assert.equal(resets, 2);
- assert.doesNotMatch(JSON.stringify(seen[1]!.history), /PRIVATE_SENTINEL/);
- assert.equal((await orch.handleTurn({ ...input, text: "stable audience" })).status, "ok");
- assert.equal(resets, 2);
- assert.match(JSON.stringify(seen[2]!.history), /new audience/);
-});
diff --git a/test/message-revisions.test.ts b/test/message-revisions.test.ts
index d2c8ab1e8..105d06282 100644
--- a/test/message-revisions.test.ts
+++ b/test/message-revisions.test.ts
@@ -332,7 +332,11 @@ test("the anchor is the last conversation entry, skipping system entries such as
await sleep(5);
await recordMessageRevisions(sessions, [edit()], NO_WAIT);
const { lease } = await sessions.acquireLease(session.id, "turn");
- await sessions.append(lease!, { type: "system", payload: { kind: "note" }, scopeLabel: SCOPE });
+ await sessions.append(lease!, {
+ type: "system",
+ payload: { kind: "wake", cause: "cron", text: "a fresh marker" },
+ scopeLabel: SCOPE,
+ });
await sessions.releaseLease(lease!);
assert.equal(await revisionAnchorAt(sessions, session.id), conversationAt);
assert.equal(await revisionAnchorAt(createMemorySessionStore(), "nope"), undefined);
diff --git a/test/orchestrator.test.ts b/test/orchestrator.test.ts
index c0838fe33..a7a01371b 100644
--- a/test/orchestrator.test.ts
+++ b/test/orchestrator.test.ts
@@ -176,35 +176,43 @@ test("the deployment turn wall-clock reaches the harness and a per-turn cap repl
assert.equal((await freshApp().app.turn(dm("!wallclock"))).reply, "wallclock:0");
});
-test("inbound file problems ride a durable file_event entry — off the reply and the model context", async () => {
+test("inbound file problems reach the model in the environment footer, not a log entry", async () => {
const { app, sessions } = freshApp();
const note = 'skipped "screenshot.png" — too many files in one message (max 10)';
const res = await app.turn(dm("what's your favorite color", { inboundNotes: [note] }));
assert.equal(res.status, "ok", res.reason);
- assert.doesNotMatch(res.reply ?? "", /too many files|skipped|screenshot\.png/);
-
- const found = await app.getSession(res.sessionId!);
- const fileEvent = found!.entries.find(
- (e) => e.type === "system" && (e.payload as { kind?: string } | null)?.kind === "file_event",
+ const entries = await sessions.getEntries(res.sessionId!);
+ assert.equal(
+ entries.some((e) => e.type === "system" && /screenshot\.png/.test(JSON.stringify(e.payload))),
+ false,
);
- assert.ok(fileEvent, "expected a durable file_event system entry");
- const payload = fileEvent!.payload as { direction: string; issues: string[]; text: string };
- assert.equal(payload.direction, "in");
- assert.ok(
- payload.issues.some((i) => i.includes("screenshot.png")),
- "the dropped file is named in the durable record",
+ const user = entries.find((e) => e.type === "user");
+ assert.match(
+ String((user!.payload as { environment?: unknown }).environment),
+ /<environment>[\s\S]*did not reach \.\/inbox\/[\s\S]*screenshot\.png[\s\S]*<\/environment>/,
);
- assert.match(payload.text, /screenshot\.png/);
const reqs = (await sessions.listLlmRequests(res.sessionId!)) as Array<{ model: string; promptEnvelope: unknown }>;
- for (const r of reqs.filter((request) => request.model !== "mock-security")) {
- assert.doesNotMatch(JSON.stringify(r.promptEnvelope), /too many files|screenshot\.png/);
- }
+ assert.ok(
+ reqs
+ .filter((request) => request.model !== "mock-security")
+ .some((r) => /screenshot\.png/.test(JSON.stringify(r.promptEnvelope))),
+ "the model sees the dropped file",
+ );
assert.equal((res as { fileNotes?: unknown }).fileNotes, undefined);
});
+test("a failed turn still records which inbound files did not arrive", async () => {
+ const { app, sessions, errors } = freshApp();
+ const note = 'skipped "screenshot.png" — too many files in one message (max 10)';
+ await assert.rejects(app.turn(dm("!boom", { inboundNotes: [note] })), /boom/);
+ const { sessionId } = (await errors.list()).find((e) => e.category === "turn")!;
+ const user = (await sessions.getEntries(sessionId!)).find((e) => e.type === "user");
+ assert.match(String((user?.payload as { environment?: unknown } | undefined)?.environment), /screenshot\.png/);
+});
+
test("a proactive-opener turn greets with no user text and records the seeding entry hidden", async () => {
const { app } = freshApp();
const res = await app.turn(dm("", { proactiveOpener: true }));
@@ -3496,9 +3504,15 @@ test("accepted approval decisions are durably recorded in the conversation", asy
(entry) => entry.type === "approval_resolved",
);
assert.equal(decisions.length, 1);
- assert.deepEqual(decisions[0]!.payload, { requestId, command, approved, ...(approved ? { scope: "once" } : {}) });
const replay = (await runs.list()).find((run) => run.request.approval?.requestId === requestId);
assert.ok(replay);
+ assert.deepEqual(decisions[0]!.payload, {
+ requestId,
+ command,
+ approved,
+ runId: replay.id,
+ ...(approved ? { scope: "once" } : {}),
+ });
const live = await app.getRun(replay.id);
const liveDecisions = live?.activity?.filter((entry) => entry.type === "approval_resolved");
assert.equal(liveDecisions?.length, 1);
@@ -4290,10 +4304,34 @@ test("narration reaches the live activity feed before its tool call", async () =
view?.activity?.map((entry) => entry.type),
["text", "tool_call", "tool_result", "text_start"],
);
- assert.deepEqual(view?.activity?.[0]?.payload, { text: "I'll check the first item." });
+ assert.deepEqual(view?.activity?.[0]?.payload, { text: "I'll check the first item.", runId: run.id });
assert.deepEqual(view?.activity?.at(-1)?.payload, { streamOffset: "I'll check the first item.\n\n".length });
});
+test("public text phases reach run activity with exact stream offsets but are not stored in session history", async () => {
+ const { app, runs } = freshApp();
+ const result = await app.turn(dm("!phased-reply"));
+ assert.equal(result.status, "ok");
+ assert.equal(result.reply, "All clear.");
+ const run = (await runs.list()).find((entry) => entry.request.text === "!phased-reply");
+ assert.ok(run);
+ const view = await app.getRun(run.id);
+ const history = await app.getSession(result.sessionId!);
+ const expected = [
+ { phase: "commentary", streamOffset: 0 },
+ { phase: "final_answer", streamOffset: "Checking.\n\n".length },
+ ];
+ assert.deepEqual(
+ view?.activity?.filter((entry) => entry.type === "text_start").map((entry) => entry.payload),
+ expected,
+ );
+ assert.deepEqual(
+ history?.entries.filter((entry) => entry.type === "text_start"),
+ [],
+ );
+ assert.equal(view?.partial, "Checking.\n\nAll clear.");
+});
+
function fixtureScreen(): SecurityScreener {
return {
provider: "fixture-screen",
diff --git a/test/pi-harness-replay-fidelity.test.ts b/test/pi-harness-replay-fidelity.test.ts
index 81013dfdc..09db23b82 100644
--- a/test/pi-harness-replay-fidelity.test.ts
+++ b/test/pi-harness-replay-fidelity.test.ts
@@ -10,6 +10,7 @@ import { createMemoryRunSignalStore, type RunSignalStore } from "../src/runs/run
import { createMemoryDurableByteStore } from "../src/files/durable-byte-store.ts";
import { createMemoryFileArtifactStore } from "../src/files/file-artifact-store.ts";
import { artifactImageLoader } from "../src/harness/replay-images.ts";
+import { createPostgresToolOutputStore, type ToolOutputStore } from "../src/sessions/tool-output.ts";
type Body = { messages?: unknown[]; input?: unknown[] };
type Reply = Array<Record<string, unknown>>;
@@ -82,6 +83,13 @@ function openaiSse(items: Reply): Response {
return new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } });
}
+function jsonb(value: unknown): unknown {
+ if (Array.isArray(value)) return value.map(jsonb);
+ if (!value || typeof value !== "object") return value;
+ const keys = Object.keys(value).sort((a, b) => a.length - b.length || Buffer.compare(Buffer.from(a), Buffer.from(b)));
+ return Object.fromEntries(keys.map((key) => [key, jsonb((value as Record<string, unknown>)[key])]));
+}
+
function withoutCacheControl(value: unknown): unknown {
if (Array.isArray(value)) return value.map(withoutCacheControl);
if (!value || typeof value !== "object") return value;
@@ -132,7 +140,7 @@ async function runTurn(
seq: seq++,
parentSeq: null,
type: e.type,
- payload: JSON.parse(JSON.stringify(e.payload ?? null)),
+ payload: jsonb(e.payload),
scopeLabel: e.scopeLabel,
createdAt: Date.now(),
};
@@ -490,3 +498,163 @@ test("a replayed image is read from the file store once, not on every turn", asy
const second = await load({ artifactId: "art-once", mimeType: "image/png" }, 10_000_000);
assert.deepEqual([first, second, opens], [{ data: PNG_A, sizeBytes: 70 }, { data: PNG_A, sizeBytes: 70 }, 1]);
});
+
+const PG_URL = process.env.DATABASE_URL;
+
+async function withPgSession(
+ fn: (ctx: {
+ emit: HarnessTurnInput["emit"];
+ history: () => Promise<SessionEntry[]>;
+ toolOutput: ToolOutputStore;
+ }) => Promise<void>,
+): Promise<void> {
+ const { randomUUID } = await import("node:crypto");
+ const { createPostgresSessionStore } = await import("../src/sessions/postgres-session-store.ts");
+ const pg = (await import("pg")).default;
+ const admin = new pg.Pool({ connectionString: PG_URL });
+ const schema = `replay_pg_${randomUUID().replaceAll("-", "")}`;
+ await admin.query(`CREATE SCHEMA ${schema}`);
+ const url = new globalThis.URL(PG_URL!);
+ url.searchParams.set("options", `-c search_path=${schema}`);
+ const store = createPostgresSessionStore(url.toString());
+ const raw = new pg.Pool({ connectionString: url.toString() });
+ try {
+ const session = await store.getOrCreateByThread(
+ "web:replay",
+ "dm",
+ "personal:tester" as SessionEntry["scopeLabel"],
+ );
+ const { lease } = await store.acquireLease(session.id);
+ assert.ok(lease);
+ await raw.query("INSERT INTO qm_settings(key,value) VALUES('tool_result_inline_tokens','256')");
+ await fn({
+ toolOutput: createPostgresToolOutputStore({
+ pool: { q: async (sql, params) => (await raw.query(sql, params)).rows },
+ bytesFor: () => createMemoryDurableByteStore(),
+ }),
+ emit: (entry) => store.append({ ...lease, turnId: "run-pg" }, entry),
+ history: async () => forModelContext(await store.getEntries(session.id)),
+ });
+ await store.releaseLease(lease);
+ } finally {
+ await raw.end();
+ await admin.query(`DROP SCHEMA ${schema} CASCADE`);
+ await admin.end();
+ }
+}
+
+const bigResult = "row ünï 🚀 ".repeat(4_000);
+const pgTools = {
+ async history() {
+ return [bigResult];
+ },
+} as unknown as ToolContext;
+
+test(
+ "after a round trip through Postgres items, the next anthropic request replays a cut result and a delivery byte-for-byte",
+ { skip: PG_URL ? false : "set DATABASE_URL" },
+ async () => {
+ await withPgSession(async ({ emit, history, toolOutput }) => {
+ const step1: Reply = [
+ { type: "thinking", thinking: "I should search history.", signature: "sig-one" },
+ { type: "text", text: "Checking." },
+ {
+ type: "tool_use",
+ id: "toolu_1",
+ name: "history",
+ input: { query: "budget", type: "dm", nested: { a: [1] } },
+ },
+ { type: "tool_use", id: "toolu_2", name: "history", input: { limit: "many" } },
+ ];
+ const step2: Reply = [
+ { type: "thinking", thinking: "Found it.", signature: "sig-two" },
+ { type: "text", text: "Done." },
+ ];
+ const tools = { ...pgTools, toolOutput } as ToolContext;
+ const first = await runTurn(
+ "where is the budget doc?",
+ [],
+ [() => anthropicSse(step1, "tool_use"), () => anthropicSse(step2, "end_turn")],
+ { emit, tools },
+ );
+ const scopeLabel = "personal:tester" as SessionEntry["scopeLabel"];
+ await emit({ type: "assistant", payload: { text: "Daily digest", via: "cron" }, scopeLabel });
+ const second = await runTurn(
+ "and Q3?",
+ await history(),
+ [() => anthropicSse([{ type: "text", text: "None." }], "end_turn")],
+ { emit, tools },
+ );
+ const before = withoutCacheControl(first.bodies[1]!.messages) as unknown[];
+ const after = withoutCacheControl(second.bodies[0]!.messages) as unknown[];
+ assert.ok(!JSON.stringify(before).includes(bigResult.trim()));
+ assert.match(JSON.stringify(before), /tool-output\/toolu_1/);
+ assert.equal(JSON.stringify(after.slice(0, before.length)), JSON.stringify(before));
+ assert.deepEqual(after.slice(before.length), [
+ {
+ role: "assistant",
+ content: [
+ { type: "thinking", thinking: "Found it.", signature: "sig-two" },
+ { type: "text", text: "Done." },
+ ],
+ },
+ { role: "assistant", content: [{ type: "text", text: "Daily digest" }] },
+ { role: "user", content: [{ type: "text", text: "and Q3?" }] },
+ ]);
+ });
+ },
+);
+
+test(
+ "after a round trip through Postgres items, the next OpenAI Responses request replays the previous turn exactly",
+ { skip: PG_URL ? false : "set DATABASE_URL" },
+ async () => {
+ await withPgSession(async ({ emit, history }) => {
+ const reasoning = (id: string, text: string) => ({
+ type: "reasoning",
+ id,
+ summary: [{ type: "summary_text", text }],
+ encrypted_content: `enc-${id}`,
+ });
+ const message = (id: string, text: string) => ({
+ type: "message",
+ id,
+ role: "assistant",
+ status: "completed",
+ content: [{ type: "output_text", text, annotations: [] }],
+ });
+ const overrides: Partial<HarnessTurnInput> = {
+ runtime: { modelId: "gpt-5.4" },
+ providerKeys: { openai: "sk-openai" },
+ emit,
+ tools: pgTools,
+ };
+ const step1: Reply = [
+ reasoning("rs_1", "Search history."),
+ {
+ type: "function_call",
+ id: "fc_1",
+ call_id: "call_1",
+ name: "history",
+ arguments: '{"type":"dm","query":"budget"}',
+ },
+ ];
+ const first = await runTurn(
+ "where is the budget doc?",
+ [],
+ [() => openaiSse(step1), () => openaiSse([reasoning("rs_2", ""), message("msg_2", "Done.")])],
+ overrides,
+ );
+ const second = await runTurn(
+ "and Q3?",
+ await history(),
+ [() => openaiSse([message("msg_3", "None.")])],
+ overrides,
+ );
+ const before = first.bodies[1]!.input!;
+ const after = second.bodies[0]!.input!;
+ assert.ok(JSON.stringify(before).includes(bigResult.trim()));
+ assert.equal(JSON.stringify(after.slice(0, before.length)), JSON.stringify(before));
+ });
+ },
+);
diff --git a/test/postgres-items-contract.test.ts b/test/postgres-items-contract.test.ts
new file mode 100644
index 000000000..439836959
--- /dev/null
+++ b/test/postgres-items-contract.test.ts
@@ -0,0 +1,279 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { randomUUID } from "node:crypto";
+import { mkdtemp, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { readFile } from "node:fs/promises";
+import { createLocalDurableByteStore } from "../src/files/durable-byte-store.ts";
+import { applyPgMigrations, createPgPool, registeredPgMigrations } from "../src/persistence/pg-pool.ts";
+import { createPostgresSessionStore } from "../src/sessions/postgres-session-store.ts";
+import { DuplicateCallError } from "../src/sessions/session-store.ts";
+import { createPostgresToolOutputStore, DEFAULT_INLINE_TOKENS } from "../src/sessions/tool-output.ts";
+import { scopeId } from "../src/types.ts";
+
+const URL = process.env.DATABASE_URL;
+const skip = URL ? false : "set DATABASE_URL (a Postgres) to run the items contract test";
+
+async function withSchema(
+ prefix: string,
+ fn: (ctx: {
+ raw: import("pg").Pool;
+ url: string;
+ store: ReturnType<typeof createPostgresSessionStore>;
+ }) => Promise<void>,
+): Promise<void> {
+ const pg = (await import("pg")).default;
+ const admin = new pg.Pool({ connectionString: URL });
+ const schema = `${prefix}_${randomUUID().replaceAll("-", "")}`;
+ await admin.query(`CREATE SCHEMA ${schema}`);
+ const url = new globalThis.URL(URL!);
+ url.searchParams.set("options", `-c search_path=${schema}`);
+ const raw = new pg.Pool({ connectionString: url.toString() });
+ try {
+ await fn({ raw, url: url.toString(), store: createPostgresSessionStore(url.toString()) });
+ } finally {
+ await raw.end();
+ await admin.query(`DROP SCHEMA ${schema} CASCADE`);
+ await admin.end();
+ }
+}
+
+test("pg items keep turn, author and time in typed columns", { skip }, async () => {
+ await withSchema("items_columns", async ({ raw, store }) => {
+ const scope = scopeId("personal", "alice");
+ const session = await store.getOrCreateByThread("web:columns", "dm", scope);
+ const { lease } = await store.acquireLease(session.id);
+ assert.ok(lease);
+ const runId = randomUUID();
+ const turn = { ...lease, turnId: runId };
+ const user = await store.append(turn, {
+ type: "user",
+ payload: { text: "hi" },
+ scopeLabel: scope,
+ author: "alice",
+ });
+ await store.append(turn, {
+ type: "user",
+ payload: { text: "also", steered: true },
+ scopeLabel: scope,
+ author: "bob",
+ });
+ await store.append(turn, {
+ type: "assistant",
+ payload: { text: "hello" },
+ scopeLabel: scope,
+ author: "ignored-on-agent-items",
+ });
+ await store.append(
+ { ...lease, turnId: "not-a-uuid" },
+ {
+ type: "tool_call",
+ payload: { tool: "execute", callId: "c1" },
+ scopeLabel: scope,
+ },
+ );
+ await store.append(lease, { type: "system", payload: { kind: "turn_failure", message: "x" }, scopeLabel: scope });
+ const stopped = await store.append(turn, {
+ type: "system",
+ payload: { kind: "turn_stopped", reason: "user", text: "Stopped" },
+ scopeLabel: scope,
+ author: "alice",
+ });
+ assert.deepEqual((await store.getEntry(session.id, stopped.seq))?.payload, {
+ kind: "turn_stopped",
+ reason: "user",
+ text: "Stopped",
+ runId,
+ });
+ await assert.rejects(
+ store.append(lease, { type: "system", payload: { kind: "goal", goal: {} }, scopeLabel: scope }),
+ /goal/,
+ );
+ await store.releaseLease(lease);
+
+ const rows = (
+ await raw.query(
+ `SELECT seq::int, kind, turn_id::text, author, error_code, (extract(epoch FROM created_at) * 1000)::bigint AS ms
+ FROM items WHERE session_id = $1 ORDER BY seq`,
+ [session.id],
+ )
+ ).rows;
+ assert.deepEqual(
+ rows.map((row) => [row.seq, row.kind, row.turn_id, row.author, row.error_code]),
+ [
+ [0, "UserMessage", runId, "alice", null],
+ [1, "SteeringMessage", runId, "bob", null],
+ [2, "AgentMessage", runId, null, null],
+ [3, "ToolCall", null, null, null],
+ [4, "Error", null, null, "turn_failure"],
+ [5, "Stop", runId, "alice", null],
+ ],
+ );
+ assert.equal(Number(rows[0].ms), user.createdAt);
+ assert.equal((await store.getEntries(session.id))[0]!.createdAt, user.createdAt);
+ await assert.rejects(
+ raw.query(
+ "INSERT INTO items(tenant_id, session_id, seq, kind, author, body, created_at) VALUES (qm_tenant_id(), $1, 9, 'AgentMessage', 'x', '{}', now())",
+ [session.id],
+ ),
+ /items_author_kind/,
+ );
+ });
+});
+
+test("pg items reject a second call with the same call id, leaving the log untouched", { skip }, async () => {
+ await withSchema("items_calls", async ({ store }) => {
+ const scope = scopeId("personal", "alice");
+ const session = await store.getOrCreateByThread("web:calls", "dm", scope);
+ const { lease } = await store.acquireLease(session.id);
+ assert.ok(lease);
+ const callId = "cron:1";
+ const call = {
+ type: "tool_call" as const,
+ payload: { tool: "deliver", callId, text: "hi" },
+ scopeLabel: scope,
+ };
+ const result = {
+ type: "tool_result" as const,
+ payload: { tool: "deliver", callId, result: "delivered" },
+ scopeLabel: scope,
+ };
+ await store.appendMany(lease, [call, result]);
+ await assert.rejects(store.appendMany(lease, [call, result]), DuplicateCallError);
+ const entries = await store.getEntries(session.id);
+ assert.equal(entries.length, 2);
+ assert.equal((entries[0]!.payload as { callId: string }).callId, callId);
+ const next = await store.append(lease, { type: "assistant", payload: { text: "after" }, scopeLabel: scope });
+ assert.equal(next.seq, 2);
+ await store.releaseLease(lease);
+ });
+});
+
+test(
+ "pg tool output: the inline cap comes from qm_settings and parked text reads back by call id",
+ { skip },
+ async () => {
+ await withSchema("items_tool_output", async ({ raw, url, store }) => {
+ const dir = await mkdtemp(join(tmpdir(), "tool-output-"));
+ const pool = createPgPool(url);
+ try {
+ const scope = scopeId("personal", "alice");
+ const session = await store.getOrCreateByThread("web:tool-output", "dm", scope);
+ let clock = Date.now();
+ const outputs = createPostgresToolOutputStore({
+ pool,
+ bytesFor: (prefix) => createLocalDurableByteStore(join(dir, prefix)),
+ now: () => clock,
+ });
+ assert.equal(await outputs.inlineTokens(), DEFAULT_INLINE_TOKENS);
+ await raw.query(
+ "INSERT INTO qm_settings(key, value) VALUES ('tool_result_inline_tokens', '0') ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value",
+ );
+ assert.equal(await outputs.inlineTokens(), DEFAULT_INLINE_TOKENS, "cached for a short while");
+ clock += 60_000;
+ assert.equal(await outputs.inlineTokens(), 0);
+
+ const full = "ünï line\n".repeat(10_000);
+ const blob = await outputs.save(full);
+ const { lease } = await store.acquireLease(session.id);
+ assert.ok(lease);
+ await store.append(lease, {
+ type: "tool_call",
+ payload: { tool: "execute", callId: "big" },
+ scopeLabel: scope,
+ });
+ await store.append(lease, {
+ type: "tool_result",
+ payload: {
+ tool: "execute",
+ callId: "big",
+ result: "head…tail",
+ output: { blob, chars: full.length, shownChars: 8 },
+ },
+ scopeLabel: scope,
+ });
+ await store.releaseLease(lease);
+ assert.deepEqual(await outputs.read(session.id, "big"), { text: full });
+ assert.equal(await outputs.read(session.id, "missing"), null);
+ assert.equal(await outputs.read("other-session", "big"), null);
+ const row = (
+ await raw.query(
+ "SELECT size, storage_key, (extract(epoch FROM expires_at - created_at) / 86400)::int AS days FROM blobs",
+ )
+ ).rows[0];
+ assert.deepEqual([Number(row.size), row.days], [Buffer.byteLength(full), 30]);
+ assert.match(row.storage_key, /^tool-output\/[0-9a-f-]{36}\/files\/[0-9a-f]{64}$/);
+ } finally {
+ await pool.close();
+ await rm(dir, { recursive: true, force: true });
+ }
+ });
+ },
+);
+
+test(
+ "pg items cutover drops legacy participant-activity triggers so participant windows stay exact",
+ { skip },
+ async () => {
+ await withSchema("items_residue", async ({ raw, url, store }) => {
+ await applyPgMigrations(
+ raw,
+ registeredPgMigrations(url).filter((migration) => migration.id < "sessions/store/0029"),
+ );
+ await raw.query(
+ await readFile(
+ new globalThis.URL("./fixtures/legacy-participant-activity-triggers.sql", import.meta.url),
+ "utf8",
+ ),
+ );
+ const scope = scopeId("personal", "alice");
+ const session = await store.getOrCreateByThread("web:residue", "dm", scope);
+ const { lease } = await store.acquireLease(session.id);
+ assert.ok(lease);
+ await store.append(lease, { type: "user", payload: { text: "one" }, scopeLabel: scope });
+ await store.releaseLease(lease);
+ await store.addParticipant(session.id, "bob", undefined, { includeHistory: true });
+ await store.addParticipant(session.id, "carol");
+ assert.deepEqual(
+ (
+ await raw.query("SELECT principal_id, valid_from_seq FROM participants WHERE session_id = $1 ORDER BY 1", [
+ session.id,
+ ])
+ ).rows.map((row) => [row.principal_id, row.valid_from_seq]),
+ [
+ ["bob", 0],
+ ["carol", 1],
+ ],
+ );
+ });
+ },
+);
+
+test("pg items hide a Slack reply receipt once the real message arrives", { skip }, async () => {
+ await withSchema("items_receipts", async ({ store }) => {
+ const scope = scopeId("personal", "alice");
+ const session = await store.getOrCreateByThread("slack:T1:C1:1.0", "dm", scope);
+ const { lease } = await store.acquireLease(session.id);
+ assert.ok(lease);
+ const receipt = {
+ type: "user" as const,
+ payload: { text: "sent from web", name: "Alice", ts: "2.0", slackReplyReceipt: true, deliveryKey: "k-1" },
+ scopeLabel: scope,
+ };
+ await store.appendMany({ ...lease, turnId: randomUUID() }, [receipt]);
+ assert.equal(await store.hasDeliveryEntry(session.id, "k-1"), true);
+ assert.equal(await store.hasDeliveryEntry(session.id, "k-2", "2.0"), true);
+ assert.equal(await store.hasDeliveryEntry(session.id, "k-2", "3.0"), false);
+ const texts = async () =>
+ (await store.getEntries(session.id, { transcript: true })).map((e) => {
+ const payload = e.payload as { text?: string; slackReplyReceipt?: boolean };
+ return [payload.text, payload.slackReplyReceipt === true];
+ });
+ assert.deepEqual(await texts(), [["sent from web", true]]);
+ await store.append(lease, { type: "user", payload: { text: "sent from web", ts: "2.0" }, scopeLabel: scope });
+ assert.deepEqual(await texts(), [["sent from web", false]]);
+ assert.equal(await store.countEntriesBefore(session.id, 10), 1);
+ assert.equal((await store.getEntries(session.id)).length, 2);
+ });
+});
diff --git a/test/postgres-items-copy.test.ts b/test/postgres-items-copy.test.ts
index 4cdfd5294..332e45a42 100644
--- a/test/postgres-items-copy.test.ts
+++ b/test/postgres-items-copy.test.ts
@@ -30,7 +30,9 @@ async function withSchema(
try {
createPostgresSessionStore(url.toString());
createPostgresDirectoryStore(url.toString());
- const migrations = registeredPgMigrations(url.toString());
+ const migrations = registeredPgMigrations(url.toString()).filter(
+ (migration) => migration.id !== "sessions/store/0036-items-cutover",
+ );
if (opts.beforeItems) {
await applyPgMigrations(
raw,
diff --git a/test/postgres-items-migration.test.ts b/test/postgres-items-migration.test.ts
new file mode 100644
index 000000000..63a9089a4
--- /dev/null
+++ b/test/postgres-items-migration.test.ts
@@ -0,0 +1,175 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { randomUUID } from "node:crypto";
+import { applyPgMigrations, registeredPgMigrations } from "../src/persistence/pg-pool.ts";
+import { ItemsCopyOperator } from "../scripts/ops/items-copy.ts";
+import { ITEM_INDEXES } from "../src/sessions/items-schema.ts";
+import { createPostgresSessionStore } from "../src/sessions/postgres-session-store.ts";
+import { scopeId, type EntryType, type SessionEntry } from "../src/types.ts";
+
+const URL = process.env.DATABASE_URL;
+const skip = URL ? false : "set DATABASE_URL (a Postgres) to run the items cutover test";
+
+test(
+ "pg items cutover: live copy, catchup, online indexes and finish hand off promoted columns to E",
+ { skip },
+ async () => {
+ const pg = (await import("pg")).default;
+ const admin = new pg.Pool({ connectionString: URL });
+ const raw = new pg.Pool({ connectionString: URL });
+ const client = new pg.Client({ connectionString: URL });
+ await client.connect();
+ try {
+ createPostgresSessionStore(URL!);
+ const migrations = registeredPgMigrations(URL!);
+ await applyPgMigrations(
+ raw,
+ migrations.filter((migration) => migration.id < "sessions/store/0035-items"),
+ );
+ const sessionId = randomUUID();
+ const scope = scopeId("personal", "alice");
+ const runId = randomUUID();
+ const legacy: [EntryType, Record<string, unknown>][] = [
+ ["user", { text: "hello", name: "Alice", principalId: "alice", runId }],
+ ["thinking", { text: "hmm", thinkingSignature: "sig" }],
+ ["text", { text: "narrating" }],
+ ["tool_call", { tool: "execute", callId: "c1", input: { command: "ls" } }],
+ ["tool_result", { tool: "execute", callId: "c1", result: "out\n", stdout: "out\n" }],
+ ["assistant", { text: "final answer" }],
+ ["system", { kind: "goal", goal: { objective: "x", status: "active" } }],
+ ["assistant", { text: "cron says hi", deliveryKey: "cron:1", via: "cron" }],
+ ["delivery", { text: "a.txt (text/plain, 3 bytes)", files: ["a.txt"] }],
+ ["user", { text: "steer", steered: true }],
+ ["system", { kind: "turn_failure", message: "boom", runId }],
+ ["system", { kind: "wake", cause: "cron", text: "cron fired" }],
+ ["soul" as EntryType, { text: "you are" }],
+ ["text_start" as EntryType, { phase: "final_answer" }],
+ ["system", { kind: "goal_cancelled", objective: "Example goal", cancelledBy: "alice" }],
+ ];
+ await raw.query(
+ "INSERT INTO sessions(id, type, scope_id, thread_ref, created_at, messages) VALUES ($1, 'dm', $2, 'web:cutover', 1, $3)",
+ [sessionId, scope, legacy.length],
+ );
+ await raw.query(
+ "INSERT INTO participants(session_id, principal_id, valid_from, valid_from_seq) VALUES ($1, 'alice', 0, 0)",
+ [sessionId],
+ );
+ const legacyEntries: SessionEntry[] = [];
+ for (const [seq, [type, payload]] of legacy.entries()) {
+ await raw.query(
+ `INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at)
+ VALUES ($1, $2, $3, $4, $5, $6, $7)`,
+ [sessionId, seq, seq ? seq - 1 : null, type, JSON.stringify(payload), scope, 1_000 + seq],
+ );
+ legacyEntries.push({
+ sessionId,
+ seq,
+ parentSeq: seq ? seq - 1 : null,
+ type,
+ payload,
+ scopeLabel: scope,
+ createdAt: 1_000 + seq,
+ });
+ }
+
+ const op = new ItemsCopyOperator(client, 3);
+ await op.prepare();
+ while (await op.copyBatch()) continue;
+ await op.completeCopy();
+ await raw.query("UPDATE session_entries SET payload=$1 WHERE session_id=$2 AND seq=5", [
+ JSON.stringify({ text: "updated final answer" }),
+ sessionId,
+ ]);
+ while (await op.catchupBatch()) continue;
+ await op.verify();
+ await assert.rejects(op.finish(true), /build the items indexes/);
+ await op.indexes();
+ assert.equal(
+ (await raw.query("SELECT count(*)::int AS n FROM pg_index WHERE indrelid='items'::regclass AND indisvalid"))
+ .rows[0].n,
+ ITEM_INDEXES.length + 1,
+ );
+ await op.finish(true);
+ await applyPgMigrations(raw, migrations);
+ await op.cleanup();
+ assert.equal((await raw.query("SELECT to_regnamespace('items_copy_ops') AS n")).rows[0].n, null);
+
+ const store = createPostgresSessionStore(URL!);
+ const copied = await store.getEntries(sessionId);
+ assert.deepEqual(copied.at(-1)?.payload, {
+ kind: "goal_cancelled",
+ cause: "system",
+ objective: "Example goal",
+ cancelledBy: "alice",
+ });
+ assert.equal((copied[0]!.payload as { text: string }).text, "hello");
+ assert.equal((copied.find((entry) => entry.seq === 5)!.payload as { text: string }).text, "updated final answer");
+ assert.equal((copied.find((entry) => entry.seq === 4)!.payload as { result: string }).result, "out\n");
+ assert.equal((copied.find((entry) => entry.seq === 3)!.payload as { callId: string }).callId, "c1");
+ assert.deepEqual(
+ copied.map((entry) => [entry.seq, entry.type, entry.createdAt]),
+ legacyEntries
+ .filter((entry) => !["soul", "text_start"].includes(entry.type))
+ .filter((entry) => (entry.payload as { kind?: string }).kind !== "goal")
+ .map((entry) => [entry.seq, entry.type, entry.createdAt]),
+ );
+ assert.deepEqual(
+ (await raw.query("SELECT seq, turn_id::text FROM items WHERE turn_id IS NOT NULL ORDER BY seq")).rows,
+ [
+ { seq: "0", turn_id: runId },
+ { seq: "10", turn_id: runId },
+ ],
+ );
+
+ const tables = await raw.query(
+ "SELECT table_name FROM information_schema.tables WHERE table_schema = current_schema() AND table_name LIKE '%session_entries'",
+ );
+ assert.deepEqual(
+ tables.rows.map((row) => row.table_name),
+ ["legacy_session_entries"],
+ );
+
+ const { lease } = await store.acquireLease(sessionId);
+ assert.ok(lease);
+ const next = await store.append(lease, {
+ type: "assistant",
+ payload: { text: "first answer after cutover zebracorn" },
+ scopeLabel: scope,
+ });
+ await store.releaseLease(lease);
+ assert.equal(next.seq, legacy.length);
+ assert.deepEqual(
+ (await store.searchEntries("alice", "zebracorn")).map((hit) => [hit.sessionId, hit.seq]),
+ [[sessionId, legacy.length]],
+ "search write-through now runs on items",
+ );
+ await raw.query(
+ `INSERT INTO legacy_session_entries(session_id, seq, type, payload, scope_label, created_at)
+ VALUES ($1, 99, 'user', '{"text":"stale aardwolf"}', $2, 1)`,
+ [sessionId, scope],
+ );
+ assert.deepEqual(await store.searchEntries("alice", "aardwolf"), [], "legacy table no longer feeds search");
+
+ await assert.rejects(
+ raw.query("UPDATE items SET body = '{}' WHERE session_id = $1", [sessionId]),
+ /UPDATE on items is forbidden/,
+ );
+ await assert.rejects(
+ raw.query("DELETE FROM items WHERE session_id = $1", [sessionId]),
+ /only allowed through its session/,
+ );
+ await assert.rejects(raw.query("TRUNCATE items"), /TRUNCATE on items is forbidden/);
+ await raw.query(
+ "INSERT INTO blobs(tenant_id, sha256, size, mime, storage_key, created_at) VALUES (qm_tenant_id(), decode(repeat('00', 32), 'hex'), 1, 'text/plain', 'k', now())",
+ );
+ await assert.rejects(raw.query("UPDATE blobs SET size = 2"), /UPDATE on blobs is forbidden/);
+
+ await store.deleteSession(sessionId);
+ assert.equal(Number((await raw.query("SELECT count(*) FROM items")).rows[0].count), 0);
+ } finally {
+ await raw.end();
+ await client.end();
+ await admin.end();
+ }
+ },
+);
diff --git a/test/postgres-items-operator.test.ts b/test/postgres-items-operator.test.ts
index 8282e9a69..d262cbbe7 100644
--- a/test/postgres-items-operator.test.ts
+++ b/test/postgres-items-operator.test.ts
@@ -24,7 +24,9 @@ test(
createPostgresDirectoryStore(url!);
await applyPgMigrations(
pool,
- registeredPgMigrations(url!).filter((m) => m.id !== "sessions/store/0035-items"),
+ registeredPgMigrations(url!).filter(
+ (m) => m.id !== "sessions/store/0035-items" && m.id !== "sessions/store/0036-items-cutover",
+ ),
);
await client.query(
"ALTER TABLE session_entries ADD CONSTRAINT operator_source_fk FOREIGN KEY(session_id) REFERENCES sessions(id) ON DELETE CASCADE",
@@ -156,29 +158,8 @@ test(
await client.query("ALTER TABLE session_entries ENABLE TRIGGER items_copy_capture");
await resumed.verify();
await assert.rejects(resumed.finish(true), /build the items indexes/);
- await assert.rejects(
- client.query("CREATE UNIQUE INDEX CONCURRENTLY items_one_call_per_kind ON items(tenant_id)"),
- /could not create unique index/,
- );
- await writer.query("SELECT pg_advisory_lock(hashtext('qm:schema-migrations'))");
- try {
- await resumed.indexes();
- await resumed.indexes();
- } finally {
- await writer.query("SELECT pg_advisory_unlock(hashtext('qm:schema-migrations'))");
- }
- assert.match(
- (await client.query("SELECT pg_get_indexdef('items_one_call_per_kind'::regclass) AS def")).rows[0].def,
- /call_id\) WHERE \(call_id IS NOT NULL\)/,
- );
- assert.equal(
- (
- await client.query(
- "SELECT count(*)::int AS n FROM pg_index WHERE indrelid='items'::regclass AND NOT indisvalid",
- )
- ).rows[0].n,
- 0,
- );
+ await resumed.indexes();
+ await resumed.indexes();
await resumed.finish(true);
assert.equal((await resumed.status()).phase, "ready");
assert.equal(
@@ -188,7 +169,7 @@ test(
await resumed.finish(true);
await applyPgMigrations(pool, registeredPgMigrations(url!));
await client.query(
- "INSERT INTO qm_schema_migrations(id,checksum) VALUES('sessions/store/0036-items-cutover','test')",
+ "INSERT INTO qm_schema_migrations(id,checksum) VALUES('sessions/store/0036-items-cutover','test') ON CONFLICT DO NOTHING",
);
await assert.rejects(resumed.catchupBatch(), /cutover is already applied/);
await resumed.cleanup();
diff --git a/test/postgres-items-parallel.test.ts b/test/postgres-items-parallel.test.ts
index ad37de648..fbf4686e1 100644
--- a/test/postgres-items-parallel.test.ts
+++ b/test/postgres-items-parallel.test.ts
@@ -26,7 +26,9 @@ test(
createPostgresDirectoryStore(url!);
await applyPgMigrations(
pool,
- registeredPgMigrations(url!).filter((m) => m.id !== "sessions/store/0035-items"),
+ registeredPgMigrations(url!).filter(
+ (m) => m.id !== "sessions/store/0035-items" && m.id !== "sessions/store/0036-items-cutover",
+ ),
);
await main.query(`INSERT INTO sessions(id,type,scope_id,thread_ref,created_at)
SELECT 's'||lpad(i::text,3,'0'),'dm','personal:test','s'||i,1 FROM generate_series(0,199) i`);
diff --git a/test/postgres-store.test.ts b/test/postgres-store.test.ts
index bb45ef004..480955d2c 100644
--- a/test/postgres-store.test.ts
+++ b/test/postgres-store.test.ts
@@ -1,5 +1,3 @@
-import { forModelContext } from "../src/harness/context-compaction.ts";
-import { memoryContextPayload, nextMemoryContext } from "../src/memory/context-boundary.ts";
import "./run-availability.ts";
import { test, before } from "node:test";
import { randomUUID } from "node:crypto";
@@ -29,7 +27,7 @@ before(async () => {
const p = new pg.Pool({ connectionString: URL });
await p.query("DROP TABLE IF EXISTS qm_schema_migrations CASCADE");
await p.query(
- "DROP TABLE IF EXISTS session_spend_days, session_spend_dirty, sessions, session_entries, participants, session_leases, session_tape, session_llm_requests, session_pins, runs, tool_calls CASCADE",
+ "DROP TABLE IF EXISTS session_spend_days, session_spend_dirty, sessions, items, participants, session_leases, session_llm_requests, session_pins, runs, tool_calls CASCADE",
);
await p.end();
});
@@ -81,11 +79,12 @@ test("pg participant presence checks seek into the visible history window", { sk
const raw = new pg.Pool({ connectionString: URL });
try {
await raw.query(
- `INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at)
- SELECT $1, n, NULL, 'user', '{"text":"window fixture"}', $2, n * 10
+ `INSERT INTO items(tenant_id, session_id, seq, kind, body, created_at)
+ SELECT qm_tenant_id(), $1, n, 'UserMessage', '{"text":"window fixture"}', to_timestamp(n * 10 / 1000.0)
FROM generate_series(0, 3999) n`,
- [session.id, session.scopeId],
+ [session.id],
);
+ await raw.query("ANALYZE items");
await store.addParticipant(session.id, owner);
const captured: { text: string; values: unknown[] }[] = [];
const execute = pg.Client.prototype.query;
@@ -113,7 +112,7 @@ test("pg participant presence checks seek into the visible history window", { sk
];
type Plan = { "Relation Name"?: string; "Rows Removed by Filter"?: number; Plans?: Plan[] };
const rejectedEntries = (plan: Plan): number =>
- (plan["Relation Name"] === "session_entries" ? (plan["Rows Removed by Filter"] ?? 0) : 0) +
+ (plan["Relation Name"] === "items" ? (plan["Rows Removed by Filter"] ?? 0) : 0) +
(plan.Plans ?? []).reduce((sum, child) => sum + rejectedEntries(child), 0);
for (const window of windows) {
await raw.query(
@@ -1356,13 +1355,13 @@ test(
)
).rows.reduce((m, r) => m.set(r.id, [r.f, r.l, r.turns]), new Map<string, unknown[]>());
try {
- for (const [seq, payload] of [
- [0, '{"text":"poisoned\\u0000tail"}'],
- [1, '{"text":"truncated'],
+ for (const [seq, body] of [
+ [0, { text: "poisonedtail" }],
+ [1, {}],
] as const) {
await raw.query(
- "INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7)",
- [junk.id, seq, seq - 1, "user", payload, scope, 1],
+ "INSERT INTO items(tenant_id, session_id, seq, kind, text, body, created_at) VALUES (qm_tenant_id(),$1,$2,'UserMessage',$3,'{}',to_timestamp(0.001))",
+ [junk.id, seq, "text" in body ? body.text : null],
);
}
await raw.query(
@@ -1400,7 +1399,7 @@ test(
assert.deepEqual(
[junkRow.firstMessage, junkRow.lastMessage],
["poisonedtail", ""],
- "a null-byte escape is dropped and malformed JSON previews as empty, never a fatal cast",
+ "a body without text previews as empty",
);
const emptyRow = all.find((r) => r.id === empty.id)!;
assert.deepEqual([emptyRow.firstMessage, emptyRow.lastMessage], ["", ""], "an entry-less legacy row is blank");
@@ -1500,7 +1499,7 @@ test("pg scopeSessionSummaries: the listing plan is an index walk with no per-ro
)) {
const explained = await raw.query(`EXPLAIN (FORMAT JSON) ${query.text}`, query.values);
const plan = JSON.stringify(explained.rows[0]["QUERY PLAN"]);
- assert.doesNotMatch(plan, /SubPlan|session_entries/, "no correlated subquery over session_entries");
+ assert.doesNotMatch(plan, /SubPlan|"Relation Name":"items"/, "no correlated subquery over items");
assert.doesNotMatch(plan, /"Node Type":"Sort"/, "the index supplies the order; nothing is sorted");
assert.match(plan, new RegExp(`"Index Name":"${index}"`), `${index} serves the listing`);
}
@@ -1644,7 +1643,9 @@ test("pg safe JSON functions are marked parallel-unsafe", { skip }, async () =>
await raw.query(`CREATE OR REPLACE FUNCTION safe_jsonb(t text) RETURNS jsonb
LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE AS $safe_jsonb$
BEGIN RETURN t::jsonb; EXCEPTION WHEN others THEN RETURN NULL; END $safe_jsonb$`);
- await raw.query("CREATE INDEX safe_jsonb_parallel_repair_test ON session_entries ((safe_jsonb(payload) ->> 'ts'))");
+ await raw.query(
+ "CREATE INDEX safe_jsonb_parallel_repair_test ON legacy_session_entries ((safe_jsonb(payload) ->> 'ts'))",
+ );
const s = createPostgresSessionStore(URL!);
await s.scanAll();
@@ -1801,7 +1802,7 @@ test(
},
);
-test("pg null-byte payloads: stripped on write, tolerated on read (no jsonb cast crash)", { skip }, async () => {
+test("pg null-byte payloads: stripped on write and counted alongside raw items", { skip }, async () => {
const s = createPostgresSessionStore(URL!);
const scope = scopeId("channel", "nul");
const a = await s.getOrCreateByThread("nulA", "channel", scope);
@@ -1820,8 +1821,8 @@ test("pg null-byte payloads: stripped on write, tolerated on read (no jsonb cast
const raw = new pg.Pool({ connectionString: URL });
try {
await raw.query(
- "INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7)",
- [a.id, 1, 0, "user", JSON.stringify({ text: "x\u0000y" }), scope, Date.now()],
+ "INSERT INTO items(tenant_id, session_id, seq, kind, body, created_at) VALUES (qm_tenant_id(),$1,$2,'UserMessage',$3,to_timestamp($4 / 1000.0))",
+ [a.id, 1, { text: "xy" }, Date.now()],
);
await raw.query("UPDATE sessions SET messages = NULL, turns = NULL, last_activity = NULL WHERE id = $1", [a.id]);
} finally {
@@ -1831,13 +1832,13 @@ test("pg null-byte payloads: stripped on write, tolerated on read (no jsonb cast
assert.equal(
attributed.reduce((n, t) => n + t.turns, 0),
2,
- "both user turns counted; the poisoned legacy row does not crash the read",
+ "both user turns counted",
);
const { lease: lease2 } = await s.acquireLease(a.id);
await s.append(lease2!, { type: "user", payload: { text: "z" }, scopeLabel: scope });
await s.releaseLease(lease2!);
const row = (await s.scopeSessionSummaries(scope, false, { limit: 500 })).find((r) => r.id === a.id)!;
- assert.equal(row.turns, 3, "append recounts NULL turns; the poisoned row is counted, not crashed");
+ assert.equal(row.turns, 3, "append recounts NULL turns");
assert.equal(row.messages, 3, "messages self-heal from the entry seq");
});
@@ -1885,8 +1886,8 @@ test(
assert.equal(legacy.lastActivity, entries[entries.length - 1]!.createdAt, "last-activity = newest entry");
await raw.query(
- "INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7)",
- [a.id, 4, 3, "user", JSON.stringify({ text: "from the old instance" }), scope, Date.now()],
+ "INSERT INTO items(tenant_id, session_id, seq, kind, body, created_at) VALUES (qm_tenant_id(),$1,$2,'UserMessage',$3,to_timestamp($4 / 1000.0))",
+ [a.id, 4, { text: "from the old instance" }, Date.now()],
);
const { lease: lease2 } = await s2.acquireLease(a.id);
await s2.append(lease2!, { type: "user", payload: { text: "back on new code" }, scopeLabel: scope });
@@ -1896,15 +1897,8 @@ test(
assert.equal(healed.turns, 3, "seq/messages mismatch triggers a full turns recount");
await raw.query(
- "INSERT INTO session_entries(session_id, seq, parent_seq, type, payload, scope_label, created_at) VALUES ($1,0,NULL,'user',$2,$3,$4), ($1,1,0,'assistant',$5,$3,$6)",
- [
- b.id,
- JSON.stringify({ text: "drained turn" }),
- scope,
- Date.now(),
- JSON.stringify({ text: "reply" }),
- Date.now() + 1,
- ],
+ "INSERT INTO items(tenant_id, session_id, seq, kind, body, created_at) VALUES (qm_tenant_id(),$1,0,'UserMessage',$2,to_timestamp($3 / 1000.0)), (qm_tenant_id(),$1,1,'AgentMessage',$4,to_timestamp($5 / 1000.0))",
+ [b.id, { text: "drained turn" }, Date.now(), { text: "reply" }, Date.now() + 1],
);
await raw.query("UPDATE sessions SET messages = 0, turns = 0 WHERE id = $1", [b.id]);
const s3 = createPostgresSessionStore(URL!);
@@ -2716,14 +2710,17 @@ test("pg search: full-text over entries with prefix match, window ACL, and type
payload: { text: "Done — pushed the memo board refresh." },
scopeLabel: scope,
});
- await s.append(lease, { type: "tool_call", payload: { text: "memo board tool noise" }, scopeLabel: scope });
+ await s.append(lease, {
+ type: "tool_call",
+ payload: { tool: "execute", callId: "noise", text: "memo board tool noise" },
+ scopeLabel: scope,
+ });
// A latecomer joins without history, then one more message lands.
await s.addParticipant(sess.id, "ULATE");
await s.append(lease, { type: "user", payload: { text: "memo board postscript" }, scopeLabel: scope });
await s.releaseLease(lease);
const hits = await s.searchEntries("USRCH", "memo boar");
- assert.ok(hits.every((hit) => hit.entryScopeLabel === scopeId("personal", "USRCH")));
assert.equal(hits.length, 3, "prefix terms match user and assistant text; tool calls are ignored");
assert.equal(hits[0]!.text, "memo board postscript", "newest first");
assert.equal(hits[2]!.author, "josh", "user hits carry the author");
@@ -2804,7 +2801,11 @@ test("pg search: message writes populate the index and tool results stay unfinda
{ seq: reply.seq, type: "assistant", text: "Rotated the deploy key.", createdAt: reply.createdAt },
]);
assert.equal(await s.searchIndexCoverage(sess.id), reply.seq);
- assert.deepEqual(await s.searchEntries("UIDX", "deploy key"), before, "search hits are unchanged by the reindex");
+ assert.deepEqual(
+ await s.searchEntries("UIDX", "deploy key"),
+ before,
+ "re-appending indexed rows leaves search hits unchanged",
+ );
await s.append(lease, { type: "user", payload: { text: "also rotate the staging deploy key" }, scopeLabel: scope });
assert.equal((await s.searchEntries("UIDX", "deploy key")).length, 3, "a new message is indexed immediately");
@@ -2879,7 +2880,7 @@ test(
const pg = (await import("pg")).default;
const raw = new pg.Pool({ connectionString: URL });
const leftovers = await raw.query(
- "SELECT (SELECT COUNT(*) FROM session_entries WHERE session_id = $1) AS e, (SELECT COUNT(*) FROM session_leases WHERE session_id = $1) AS l",
+ "SELECT (SELECT COUNT(*) FROM items WHERE session_id = $1) AS e, (SELECT COUNT(*) FROM session_leases WHERE session_id = $1) AS l",
[sess.id],
);
await raw.end();
@@ -2952,7 +2953,7 @@ test("pg deleteSessionIfEmpty: a held lease or landed entries refuse the discard
const pg = (await import("pg")).default;
const raw = new pg.Pool({ connectionString: URL });
const orphans = await raw.query(
- "SELECT (SELECT COUNT(*) FROM session_entries WHERE session_id = $1) AS e, (SELECT COUNT(*) FROM session_leases WHERE session_id = $1) AS l",
+ "SELECT (SELECT COUNT(*) FROM items WHERE session_id = $1) AS e, (SELECT COUNT(*) FROM session_leases WHERE session_id = $1) AS l",
[empty.id],
);
await raw.end();
@@ -2989,7 +2990,7 @@ test("pg search: globally limits indexed hits across sessions", { skip }, async
}
});
-test("pg search: writes are atomic and updates and deletes keep the index current", { skip }, async () => {
+test("pg search: writes are atomic, items are append-only, and deletes keep the index current", { skip }, async () => {
const store = createPostgresSessionStore(URL!);
const session = await store.getOrCreateByThread("search-atomic", "dm", scopeId("personal", "UATOMIC"));
await store.addParticipant(session.id, "UATOMIC", undefined, { includeHistory: true });
@@ -3011,71 +3012,36 @@ test("pg search: writes are atomic and updates and deletes keep the index curren
payload: { text: "original document" },
scopeLabel: session.scopeId,
});
- await pool.query("UPDATE session_entries SET payload=$3 WHERE session_id=$1 AND seq=$2", [
- session.id,
- entry.seq,
- JSON.stringify({ text: "edited document", name: "Editor" }),
- ]);
- assert.deepEqual(await store.searchEntries("UATOMIC", "original"), []);
- assert.equal((await store.searchEntries("UATOMIC", "edited"))[0]!.author, "Editor");
- await pool.query("UPDATE session_entries SET type='tool_result' WHERE session_id=$1", [session.id]);
- assert.deepEqual(await store.searchEntries("UATOMIC", "edited"), []);
- await pool.query("UPDATE session_entries SET type='user' WHERE session_id=$1", [session.id]);
- assert.equal((await store.searchEntries("UATOMIC", "edited")).length, 1);
+ await assert.rejects(
+ pool.query("UPDATE items SET body=$3 WHERE session_id=$1 AND seq=$2", [
+ session.id,
+ entry.seq,
+ { text: "edited document", name: "Editor" },
+ ]),
+ /UPDATE on items is forbidden/,
+ );
+ assert.equal((await store.searchEntries("UATOMIC", "original")).length, 1);
await pool.query("DELETE FROM session_entry_search WHERE session_id=$1", [session.id]);
assert.equal(await store.missingSearchEntries(session.id), 1);
- assert.deepEqual(await store.searchEntries("UATOMIC", "edited"), [], "search never falls back to the legacy table");
- await pool.query("UPDATE session_entries SET payload=payload WHERE session_id=$1", [session.id]);
- await pool.query("DELETE FROM session_entries WHERE session_id=$1", [session.id]);
- assert.deepEqual(await store.searchEntries("UATOMIC", "edited"), []);
- } finally {
- await pool.query("ALTER TABLE session_entry_search DROP CONSTRAINT IF EXISTS search_test_failure");
- await store.releaseLease(lease);
- await pool.end();
- }
-});
-
-test("pg search: migration fills holes below the watermark and covers legacy-only sessions", { skip }, async () => {
- const store = createPostgresSessionStore(URL!);
- const session = await store.getOrCreateByThread("search-migration", "dm", scopeId("personal", "UMIGSEARCH"));
- await store.addParticipant(session.id, "UMIGSEARCH", undefined, { includeHistory: true });
- await store.updateParticipantView(session.id, "UMIGSEARCH", { title: "My documents", archived: true });
- const pg = (await import("pg")).default;
- const pool = new pg.Pool({ connectionString: URL });
- try {
- await pool.query("DROP TRIGGER session_entries_search_write_through ON session_entries");
- await pool.query(
- "DELETE FROM qm_schema_migrations WHERE id IN ('sessions/store/0014-search-write-through-v1','sessions/store/0015-search-backfill-v1')",
- );
- for (let seq = 0; seq < 3; seq++) {
- await pool.query(
- "INSERT INTO session_entries(session_id,seq,parent_seq,type,payload,scope_label,created_at) VALUES($1,$2,NULL,'user',$3,$4,$5)",
- [session.id, seq, JSON.stringify({ text: `historical document ${seq}` }), session.scopeId, seq],
- );
- }
+ assert.deepEqual(await store.searchEntries("UATOMIC", "original"), [], "search reads only the index");
await pool.query(
- "INSERT INTO session_entry_search(session_id,seq,type,text,created_at) VALUES($1,2,'user','historical document 2',2)",
- [session.id],
+ "INSERT INTO session_entry_search(session_id,seq,type,text,created_at) VALUES($1,$2,'user',$3,$4)",
+ [session.id, entry.seq, "original document", entry.createdAt],
);
- assert.equal(await store.searchIndexCoverage(session.id), 2);
- assert.equal(await store.missingSearchEntries(session.id), 2);
- const ids = registeredPgMigrations(URL!).map((migration) => migration.id);
- assert.ok(
- ids.indexOf("sessions/store/0014-search-write-through-v1") <
- ids.indexOf("sessions/store/0015-search-backfill-v1"),
+ assert.equal((await store.searchEntries("UATOMIC", "original")).length, 1);
+ await assert.rejects(
+ pool.query("DELETE FROM items WHERE session_id=$1", [session.id]),
+ /only allowed through its session/,
);
- await migrateRegisteredPgSchemas(URL!);
- const migrated = store;
- assert.equal(await migrated.missingSearchEntries(session.id), 0);
- const hits = await migrated.searchEntries("UMIGSEARCH", "historical document");
- assert.equal(hits.length, 3);
- assert.ok(hits.every((hit) => hit.title === "My documents" && hit.archived));
- await pool.query(
- "INSERT INTO session_entries(session_id,seq,parent_seq,type,payload,scope_label,created_at) VALUES($1,3,NULL,'user',$2,$3,3)",
- [session.id, JSON.stringify({ text: "old writer document" }), session.scopeId],
+ await pool.query("DELETE FROM sessions WHERE id=$1", [session.id]);
+ assert.equal(
+ (await pool.query("SELECT count(*)::int AS n FROM session_entry_search WHERE session_id=$1", [session.id]))
+ .rows[0].n,
+ 0,
);
- assert.equal((await migrated.searchEntries("UMIGSEARCH", "old writer")).length, 1);
} finally {
+ await pool.query("ALTER TABLE session_entry_search DROP CONSTRAINT IF EXISTS search_test_failure");
+ await store.releaseLease(lease);
await pool.end();
}
});
@@ -3111,7 +3077,7 @@ test("pg transcript write failure rolls back the entry and its counters", { skip
`CREATE FUNCTION reject_transcript_test() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'transcript unavailable'; END $$`,
);
await client.query(
- `CREATE TRIGGER reject_transcript_test BEFORE INSERT ON session_entries FOR EACH ROW WHEN (NEW.session_id = '${session.id}') EXECUTE FUNCTION reject_transcript_test()`,
+ `CREATE TRIGGER reject_transcript_test BEFORE INSERT ON items FOR EACH ROW WHEN (NEW.session_id = '${session.id}') EXECUTE FUNCTION reject_transcript_test()`,
);
try {
await assert.rejects(
@@ -3123,7 +3089,7 @@ test("pg transcript write failure rolls back the entry and its counters", { skip
const counters = (await client.query("SELECT messages FROM sessions WHERE id=$1", [session.id])).rows[0];
assert.equal(counters.messages, 0);
} finally {
- await client.query("DROP TRIGGER reject_transcript_test ON session_entries");
+ await client.query("DROP TRIGGER reject_transcript_test ON items");
await client.query("DROP FUNCTION reject_transcript_test()");
await client.end();
await s.releaseLease(lease);
@@ -3225,59 +3191,6 @@ test("pg unstarted withdrawal preserves claimed and released turns atomically",
}
});
-test("pg context window preserves user memory checkpoints through compaction and restart", { skip }, async () => {
- const store = createPostgresSessionStore(URL!);
- const session = await store.getOrCreateByThread(`memory-checkpoint-${randomUUID()}`, "dm", "personal:alice");
- const { lease } = await store.acquireLease(session.id, "turn");
- assert.ok(lease);
- const append = (type: "user" | "assistant" | "system", payload: unknown) =>
- store.append(lease, { type, scopeLabel: "personal:alice", payload });
- try {
- await append("user", { text: "EXPIRED_USER" });
- await append("assistant", { text: "EXPIRED_ASSISTANT" });
- await append("system", { kind: "context_summary", throughSeq: 1, text: "EXPIRED_SUMMARY" });
- const checkpoint = nextMemoryContext(await store.getEntries(session.id), { audience: "a" }, 2);
- const checkpointEntry = await append("user", { text: "CURRENT_USER", memoryContext: checkpoint });
- const reply = await append("assistant", { text: "CURRENT_REPLY" });
- const summary = await append("system", { kind: "context_summary", throughSeq: reply.seq, text: "CURRENT_SUMMARY" });
- const restarted = createPostgresSessionStore(URL!);
- const window = await restarted.getContextWindow(session.id);
- assert.equal(window.totalEntries, 6);
- assert.deepEqual(
- window.entries.map((entry) => entry.seq),
- [checkpointEntry.seq, summary.seq],
- );
- assert.deepEqual(memoryContextPayload(window.entries[0]!), checkpoint);
- assert.deepEqual(
- forModelContext(window.entries).map((entry) => entry.seq),
- [summary.seq],
- );
- assert.doesNotMatch(JSON.stringify(forModelContext(window.entries)), /EXPIRED_|CURRENT_USER|CURRENT_REPLY/);
- const reset = nextMemoryContext(window.entries, { audience: "b" }, summary.seq);
- assert.equal(reset.throughSeq, summary.seq);
- const newUser = await append("user", { text: "AFTER_RESET", memoryContext: reset });
- const resetWindow = await restarted.getContextWindow(session.id);
- assert.deepEqual(
- forModelContext(resetWindow.entries).map((entry) => entry.seq),
- [newUser.seq],
- );
- assert.doesNotMatch(JSON.stringify(forModelContext(resetWindow.entries)), /CURRENT_SUMMARY|EXPIRED_/);
- const newSummary = await append("system", {
- kind: "context_summary",
- throughSeq: newUser.seq,
- text: "AFTER_RESET_SUMMARY",
- });
- const finalWindow = await createPostgresSessionStore(URL!).getContextWindow(session.id);
- assert.deepEqual(memoryContextPayload(finalWindow.entries[0]!), reset);
- assert.deepEqual(
- forModelContext(finalWindow.entries).map((entry) => entry.seq),
- [newSummary.seq],
- );
- } finally {
- await store.releaseLease(lease);
- }
-});
-
test(
"pg spend_usage_json returns NULL for invalid or unusual usage text instead of raising (Postgres 18 inlines SQL functions)",
{ skip },
diff --git a/test/replay.test.ts b/test/replay.test.ts
index f8e1b3bf5..aa6da8961 100644
--- a/test/replay.test.ts
+++ b/test/replay.test.ts
@@ -179,24 +179,28 @@ test("a user turn that lands right after a tool result stays adjacent — no wor
assert.deepEqual(assistantText, []);
});
-test("reconstructMessagesFromHistory replays a delivery in user voice, never the assistant's", () => {
+test("reconstructMessagesFromHistory replays a delivery as the agent's deliver call and keeps legacy file notes", () => {
const history: SessionEntry[] = [
ent("user", { text: "make a flag" }, 1),
ent("assistant", { text: "here you go" }, 2),
ent("delivery", { text: "flag.png (image/png, 100 bytes)" }, 3),
- ent("user", { text: "thanks" }, 4),
+ ent("tool_call", { tool: "deliver", callId: "cron:1", text: "flag.png is ready", via: "cron" }, 4),
+ ent("tool_result", { tool: "deliver", callId: "cron:1", result: "delivered" }, 5),
+ ent("user", { text: "thanks" }, 6),
];
const msgs = reconstructMessagesFromHistory(history);
assertValidWire(msgs);
assert.deepEqual(
msgs.map((m) => m.role),
- ["user", "assistant", "user", "user"],
+ ["user", "assistant", "user", "assistant", "toolResult", "user"],
);
- assert.deepEqual(msgs[1]!.content, [{ type: "text", text: "here you go" }]);
assert.deepEqual(msgs[2]!.content, [
{ type: "text", text: "[files delivered to the conversation: flag.png (image/png, 100 bytes)]" },
]);
- assert.deepEqual(msgs[3]!.content, [{ type: "text", text: "thanks" }]);
+ assert.deepEqual(msgs[3]!.content, [
+ { type: "toolCall", id: "cron:1", name: "deliver", arguments: { text: "flag.png is ready", via: "cron" } },
+ ]);
+ assert.deepEqual(msgs[4]!.content, [{ type: "text", text: "delivered" }]);
});
test("reconstructMessagesFromHistory opens with a fixed user frame when the agent spoke first", () => {
diff --git a/test/resume-surface-post.test.ts b/test/resume-surface-post.test.ts
new file mode 100644
index 000000000..dc5e0df1d
--- /dev/null
+++ b/test/resume-surface-post.test.ts
@@ -0,0 +1,129 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { mkdtempSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { createOrchestrator, type OrchestratorInput } from "../src/core/orchestrator.ts";
+import { createIdentityService } from "../src/identity/identity-service.ts";
+import { createMemoryConfigStore } from "../src/resolution/config-store.ts";
+import { createAclStore } from "../src/acl/acl-store.ts";
+import { createResolutionService } from "../src/resolution/resolution-service.ts";
+import { createMemorySessionStore } from "../src/sessions/memory-session-store.ts";
+import { createLocalWorkspaceStore } from "../src/workspace/workspace-store.ts";
+import { createMemoryService } from "../src/memory/memory-service.ts";
+import { createModelGateway } from "../src/model/model-gateway.ts";
+import { createAuditLog } from "../src/audit/audit-log.ts";
+import { createRateLimiter } from "../src/ratelimit/rate-limiter.ts";
+import { createMockHarness } from "../src/harness/mock-harness.ts";
+import { createDeployStore } from "../src/deploy/deploy-store.ts";
+import { createDockerDeployProvider } from "../src/deploy/docker-deploy-provider.ts";
+import { createDeployService } from "../src/deploy/deploy-service.ts";
+import { createMemoryFileArtifactStore } from "../src/files/file-artifact-store.ts";
+import { createMemoryDurableByteStore } from "../src/files/durable-byte-store.ts";
+import type { Harness } from "../src/harness/harness.ts";
+import { createDeliveryStore } from "../src/delivery/delivery-store.ts";
+import type { DeliveryStore } from "../src/delivery/delivery-store.ts";
+import type { Sandbox } from "../src/sandbox/sandbox.ts";
+import type { Conversation, Principal } from "../src/types.ts";
+
+const ORG = "default-org";
+const actor: Principal = { id: "U1", type: "internal" };
+
+function fakeSandbox(): Sandbox {
+ const unreached = () => {
+ throw new Error("fakeSandbox: a resumed reply must not touch the sandbox");
+ };
+ return {
+ profile: {
+ backend: "fake",
+ writablePersistence: "snapshot_to_workspace",
+ processSessions: false,
+ },
+ provision: unreached as never,
+ run: unreached as never,
+ readFile: unreached as never,
+ writeFile: unreached as never,
+ writeFileBytes: unreached as never,
+ readFileBytes: unreached as never,
+ listDir: unreached as never,
+ removeDir: unreached as never,
+ teardown: unreached as never,
+ };
+}
+
+function buildOrchestrator(
+ harness: Harness,
+ sessions: ReturnType<typeof createMemorySessionStore>,
+ deliveries: DeliveryStore,
+) {
+ const config = createMemoryConfigStore(ORG);
+ const acl = createAclStore();
+ const auditLog = createAuditLog();
+ const workspace = createLocalWorkspaceStore(mkdtempSync(join(tmpdir(), "rsp-")));
+ const deploy = createDeployService({
+ deployStore: createDeployStore(),
+ provider: createDockerDeployProvider(),
+ deployDir: join(tmpdir(), "rsp-deploy"),
+ auditLog,
+ acl,
+ });
+ return createOrchestrator({
+ identity: createIdentityService(),
+ resolution: createResolutionService(ORG, config, acl),
+ sessions,
+ workspace,
+ files: createMemoryFileArtifactStore(createMemoryDurableByteStore()),
+ sandbox: fakeSandbox(),
+ modelGateway: createModelGateway(),
+ auditLog,
+ rateLimiter: createRateLimiter({ maxPerWindow: 1000, windowMs: 60_000 }),
+ harness,
+ memory: createMemoryService(workspace),
+ deliveries,
+ memoryStrategy: {},
+ deploy,
+ acl,
+ });
+}
+
+test("a resumed turn whose dead attempt already posted does not post its closing text again", async () => {
+ const sessions = createMemorySessionStore();
+ const deliveries = createDeliveryStore();
+ const base = createMockHarness();
+ const harness: Harness = {
+ ...base,
+ turns: {
+ ...base.turns,
+ runTurn: async (turn) => {
+ await turn.emit({ type: "assistant", payload: { text: "Done, it printed ok." }, scopeLabel: turn.scopeLabel });
+ return { reply: "Done, it printed ok." };
+ },
+ },
+ };
+ const threadRef = "ch:C1:100.1";
+ const session = await sessions.getOrCreateByThread(threadRef, "channel", "channel:C1");
+ const { lease } = await sessions.acquireLease(session.id);
+ assert.ok(lease);
+ for (const [type, payload] of [
+ ["user", { text: "run it and tell me the output", runId: "run-1" }],
+ ["tool_call", { tool: "slack", action: "post", callId: "p1", text: "Output: ok" }],
+ ["tool_result", { tool: "slack", action: "post", callId: "p1", ok: true, result: "[sent]" }],
+ ] as const)
+ await sessions.append(lease, { type, payload, scopeLabel: "channel:C1" });
+ await sessions.releaseLease(lease);
+ const orch = buildOrchestrator(harness, sessions, deliveries);
+ const result = await orch.handleTurn({
+ surface: "slack",
+ actor,
+ conversation: { kind: "channel", threadRef, channelRef: "C1", audience: [actor] } as Conversation,
+ deliveryTarget: "slack:C1:100.1",
+ origin: { kind: "direct" },
+ text: "run it and tell me the output",
+ surfaceTools: true,
+ addressed: true,
+ attempt: 2,
+ runId: "run-1",
+ } as OrchestratorInput);
+ assert.notEqual(result.status, "error");
+ assert.deepEqual(await deliveries.pending("slack"), []);
+});
diff --git a/test/scope-reach.test.ts b/test/scope-reach.test.ts
index 0b982edd0..ffcca615a 100644
--- a/test/scope-reach.test.ts
+++ b/test/scope-reach.test.ts
@@ -16,8 +16,7 @@ import {
} from "../src/tools/primitives.ts";
import { createDirectoryStore } from "../src/directory/directory-store.ts";
import { resolveReachableChannel } from "../src/resolution/scope-reach.ts";
-import { filterHistoryForAudience } from "../src/resolution/context-filter.ts";
-import { scopeId, type Principal, type TurnRequest, type WorkspaceLayer } from "../src/types.ts";
+import { scopeId, type TurnRequest, type WorkspaceLayer } from "../src/types.ts";
import type { Sandbox, SandboxHandle } from "../src/sandbox/sandbox.ts";
import type { AuditEvent, AuditLog } from "../src/audit/audit-log.ts";
import { testConfig } from "./support/test-config.ts";
@@ -398,22 +397,6 @@ test("reach teardown keeps (does not destroy) a room with its own computer", asy
);
});
-test("Trap 1 e2e: the reach tool_result is labeled the session scope and survives the audience filter", async () => {
- const built = await roomApp();
- const res = await built.app.turn(dm("!reach #project-alpha echo hello"));
- assert.equal(res.reply, "hello");
- const entries = await built.sessions.getEntries(res.sessionId!);
- const toolResults = entries.filter((e) => e.type === "tool_result");
- assert.ok(toolResults.length >= 1, "a reach tool_result was recorded");
- for (const e of toolResults) assert.equal(e.scopeLabel, scopeId("personal", "U1"));
- const audience: Principal[] = [{ id: "U1", type: "internal" }];
- const kept = filterHistoryForAudience(entries, audience, scopeId("personal", "U1"), scopeId("org", "default-org"));
- assert.ok(
- kept.some((e) => e.type === "tool_result"),
- "the reach result is not dropped from the DM's own next turn",
- );
-});
-
test("DM reach to a private channel the human isn't in is denied", async () => {
const built = freshApp({ reachExecEnabled: true });
await built.directory.replace([
diff --git a/test/screening-release.test.ts b/test/screening-release.test.ts
index 68e6d46aa..c5aebcee0 100644
--- a/test/screening-release.test.ts
+++ b/test/screening-release.test.ts
@@ -4,13 +4,11 @@ import assert from "node:assert/strict";
import { createMockHarness } from "../src/harness/mock-harness.ts";
import { createAgentTools, type ToolContextRef } from "../src/harness/agent-tools.ts";
import type { HarnessTurnInput } from "../src/harness/harness.ts";
-import { filterHistoryForAudience } from "../src/resolution/context-filter.ts";
import type { TurnRequest } from "../src/types.ts";
import { testConfig } from "./support/test-config.ts";
let executions = 0;
let observedRelease = "";
-let failRelease = false;
mock.module("../src/harness/mock-harness.ts", {
namedExports: {
createMockHarness() {
@@ -24,7 +22,6 @@ mock.module("../src/harness/mock-harness.ts", {
(entry.payload as { securityReleaseRequestId?: string }).securityReleaseRequestId,
);
observedRelease = String((held?.payload as { text?: string } | undefined)?.text ?? "Missing released output");
- if (failRelease) throw new Error("fixture continuation failure");
const reply = "Released fixture output.";
await turn.emit({ type: "user", payload: { text: turn.input }, scopeLabel: turn.scopeLabel });
await turn.emit({ type: "assistant", payload: { text: reply }, scopeLabel: turn.scopeLabel });
@@ -57,7 +54,7 @@ mock.module("../src/harness/mock-harness.ts", {
args: unknown,
) => Promise<{ content: Array<{ text?: string }> }>;
const result = await execute(
- "fixture-call",
+ `fixture-call-${++callSeq}`,
sharedRead
? { action: "read", path: "private.txt" }
: { command: "printf SCREENING_FIXTURE_BLOCK", purpose: "Check output screening" },
@@ -70,6 +67,7 @@ mock.module("../src/harness/mock-harness.ts", {
},
},
});
+let callSeq = 0;
const { buildApp } = await import("../src/wiring.ts");
function request(threadRef = "fixture-thread"): TurnRequest {
@@ -146,43 +144,3 @@ for (const failure of ["error", "timeout"] as const) {
assert.ok(events.some((event) => event.action === "security_posture.tool_result_failed_open"));
});
}
-
-test("released shared content retains scope and survives a failed continuation", async () => {
- const built = buildApp(testConfig({ securityPosture: "auto" }), {
- securityScreener: {
- provider: "fixture-screen",
- async classify() {
- return { verdict: { decision: "strict" }, score: 1, threshold: 0.5 };
- },
- },
- });
- const req: TurnRequest = {
- ...request(),
- text: "run fixture shared",
- conversation: { kind: "channel", threadRef: "shared-fixture", channelRef: "C", audience: [{ externalId: "U1" }] },
- };
- const blocked = await built.app.turn(req);
- const approval = { requestId: blocked.pendingApprovals![0]!.requestId, approved: true };
- failRelease = true;
- try {
- await built.app.turn({ ...req, approval }).catch(() => undefined);
- } finally {
- failRelease = false;
- }
- const entries = await built.sessions.getEntries(blocked.sessionId!);
- const released = entries.filter(
- (entry) =>
- entry.type === "user" &&
- (entry.payload as { securityReleaseRequestId?: string }).securityReleaseRequestId === approval.requestId,
- );
- assert.equal(released.length, 1);
- assert.equal(released[0]!.scopeLabel, "personal:U1");
- assert.match(JSON.stringify(released[0]!.payload), /SCREENING_FIXTURE_BLOCK/);
- assert.equal(
- filterHistoryForAudience(released, [{ id: "U2", type: "internal" }], "channel:C", "org:default-org").length,
- 0,
- );
- const replay = await built.app.turn({ ...req, approval });
- assert.equal(replay.status, "refused");
- assert.match(replay.reason ?? "", /expired/);
-});
diff --git a/test/semantic-memory-http.test.ts b/test/semantic-memory-http.test.ts
index 4c0c3ff6c..7ae1f8b63 100644
--- a/test/semantic-memory-http.test.ts
+++ b/test/semantic-memory-http.test.ts
@@ -82,7 +82,7 @@ async function seedVectors(
}
}
-pgTest("HTTP turn injects semantic matches; grep remains independent and authorized", async (t) => {
+pgTest("HTTP turn injects semantic matches; grep remains independent", async (t) => {
const databaseFixture = await isolatedDatabase(t);
const databaseUrl = databaseFixture.url;
const inputs: string[][] = [];
@@ -153,15 +153,6 @@ pgTest("HTTP turn injects semantic matches; grep remains independent and authori
conversation: { kind: "dm" as const, threadRef: "semantic-http" },
text: "!sysprompt\nhow should I verify a UI change?",
};
- const seeded = await built.sessions.getOrCreateByThread("semantic-http", "dm", "personal:U1");
- const { lease } = await built.sessions.acquireLease(seeded.id);
- assert.ok(lease);
- await built.sessions.append(lease, {
- type: "user",
- scopeLabel: "personal:U2",
- payload: { text: "FOREIGN_HISTORY_SENTINEL" },
- });
- await built.sessions.releaseLease(lease);
const response = await post("/v1/turns", request);
const result = (await response.json()) as { status: string; reply: string };
assert.equal(response.status, 200);
@@ -189,7 +180,6 @@ pgTest("HTTP turn injects semantic matches; grep remains independent and authori
const forbidden = await post("/v1/memory/search", { query: "confidential" }, { "x-agent-capability": cap });
assert.deepEqual(((await forbidden.json()) as { results: unknown[] }).results, []);
assert.ok(inputs.every((batch) => batch.every((text) => !text.includes(otherFact))));
- assert.ok(inputs.every((batch) => batch.every((text) => !text.includes("FOREIGN_HISTORY_SENTINEL"))));
const personalFact = (await built.memory.facts!("personal:U1")).find((row) => row.text === fact)!;
await built.memory.edit!("personal:U1", [
diff --git a/test/session-append-many.test.ts b/test/session-append-many.test.ts
index ca4d21edc..775a6bc10 100644
--- a/test/session-append-many.test.ts
+++ b/test/session-append-many.test.ts
@@ -44,20 +44,20 @@ for (const backend of ["memory", "postgres"] as const) {
assert.equal(Number(rows[0].turns), 2);
const constraint = `batch_failure_${randomUUID().replaceAll("-", "")}`;
await pool.query(
- `ALTER TABLE session_entries ADD CONSTRAINT ${constraint} CHECK (session_id <> '${session.id}' OR seq <> 5)`,
+ `ALTER TABLE items ADD CONSTRAINT ${constraint} CHECK (session_id <> '${session.id}' OR seq <> 5)`,
);
try {
await assert.rejects(
store.appendMany(lease, [
{ type: "user", payload: { text: "Rolled back" }, scopeLabel: scope },
- { type: "assistant", payload: { text: "Fails insert" }, scopeLabel: scope },
+ { type: "assistant", payload: { text: "Fails item insert" }, scopeLabel: scope },
]),
);
assert.equal((await store.getEntries(session.id)).length, 4);
const after = await pool.query("SELECT messages, turns FROM sessions WHERE id = $1", [session.id]);
assert.deepEqual(after.rows, rows);
} finally {
- await pool.query(`ALTER TABLE session_entries DROP CONSTRAINT ${constraint}`);
+ await pool.query(`ALTER TABLE items DROP CONSTRAINT ${constraint}`);
}
}
await store.releaseLease(lease);
diff --git a/test/session-goal-cancel.test.ts b/test/session-goal-cancel.test.ts
index 23d127246..311b131b5 100644
--- a/test/session-goal-cancel.test.ts
+++ b/test/session-goal-cancel.test.ts
@@ -67,6 +67,7 @@ for (const status of ["active", "paused"] as const) {
const entries = await built.sessions.getEntries(session.id);
assert.deepEqual(entries.at(-1)?.payload, {
kind: "goal_cancelled",
+ cause: "system",
objective: "old outreach goal",
cancelledBy: "U1",
});
diff --git a/test/session-mail-approval.test.ts b/test/session-mail-approval.test.ts
index 1132f56bf..e358cb894 100644
--- a/test/session-mail-approval.test.ts
+++ b/test/session-mail-approval.test.ts
@@ -54,6 +54,8 @@ const ORG = "default-org";
const actor: Principal = { id: "U1", type: "internal" };
const conversation: Conversation = { kind: "dm", threadRef: "web:U1:mail", audience: [actor] };
+let callSeq = 0;
+
async function scenario() {
const sessions = createMemorySessionStore();
const { runs } = createMemoryRunStore();
@@ -87,7 +89,7 @@ async function scenario() {
};
const tool = createAgentTools(ref).find((tool) => tool.name === "subagents")!;
const execute = tool.execute as unknown as (id: string, input: unknown) => Promise<{ content: unknown[] }>;
- const result = await execute("wait-call", { action: "wait", timeoutMs: 0 });
+ const result = await execute(`wait-call-${++callSeq}`, { action: "wait", timeoutMs: 0 });
const reply = JSON.stringify(result.content);
await turn.emit({ type: "assistant", payload: { text: reply }, scopeLabel: turn.scopeLabel });
return { reply, modelCalls: 1 };
diff --git a/test/session-memory-title.test.ts b/test/session-memory-title.test.ts
index e30b785b5..15957cc7a 100644
--- a/test/session-memory-title.test.ts
+++ b/test/session-memory-title.test.ts
@@ -95,7 +95,7 @@ test("participant can read child history and title across memory checkpoints", a
assert.equal(listed.children[0]?.lastSaid, "PRIVATE_SENTINEL");
});
-test("reset source title does not enter fresh child task", async () => {
+test("explicit source title changes reach fresh child tasks without copying history", async () => {
const r = await rig();
await r.sessions.updateTitle(r.room.id, "PRIVATE_SENTINEL");
const { lease } = await r.sessions.acquireLease(r.room.id);
@@ -106,20 +106,20 @@ test("reset source title does not enter fresh child task", async () => {
payload: { text: "PRIVATE_SENTINEL" },
});
await r.sessions.append(lease, {
- type: "system",
+ type: "user",
scopeLabel: scope,
payload: {
- kind: "memory_context",
- fingerprint: "new",
- snapshot: { audience: "safe-audience" },
- throughSeq: old.seq,
+ text: "new audience turn",
+ memoryContext: { kind: "memory_context", snapshot: { audience: "safe-audience" }, throughSeq: old.seq },
},
});
await r.sessions.releaseLease(lease);
+ await r.sessions.updateTitle(r.room.id, "Current parent");
const opened = await r.syscallsFor((await r.sessions.get(r.room.id))!).open({ task: "safe task", name: "safe name" });
assert.ok(opened.ok);
const child = (await r.sessions.get(opened.sessionId))!;
const run = (await r.runs.inFlightForThread(child.threadRef))[0]!;
+ assert.match(run.request.text, /Current parent/);
assert.doesNotMatch(run.request.text, /PRIVATE_SENTINEL/);
});
test("participant sees child title in session write response", async () => {
diff --git a/test/session-search-tool.test.ts b/test/session-search-tool.test.ts
index 5a0aed977..ac5ec9aea 100644
--- a/test/session-search-tool.test.ts
+++ b/test/session-search-tool.test.ts
@@ -16,7 +16,7 @@ import { scopeId, type Principal } from "../src/types.ts";
const alice: Principal = { id: "[email protected]", type: "internal" };
const bob: Principal = { id: "[email protected]", type: "internal" };
-test("session search reuses indexed search with audience intersection, tenure windows and bounded references", async () => {
+test("session search reuses indexed search with participant intersection, tenure windows and bounded references", async () => {
const { app, sessions } = buildApp(
testConfig({ dataDir: mkdtempSync(join(tmpdir(), "session-search-tool-")), publicWebUrl: "https://qm.example" }),
);
@@ -37,11 +37,6 @@ test("session search reuses indexed search with audience intersection, tenure wi
payload: { text: "memo shared" },
scopeLabel: source.scopeId,
});
- await sessions.append(lease, {
- type: "assistant",
- payload: { text: "memo private-only" },
- scopeLabel: scopeId("personal", alice.id),
- });
await sessions.releaseLease(lease);
const { runs } = createMemoryRunStore();
let members: Principal[] | undefined;
@@ -80,43 +75,9 @@ test("session search reuses indexed search with audience intersection, tenure wi
[[source.id, shared.seq, "memo shared", `https://qm.example/s/${source.id}`]],
);
assert.equal((await tools.search!({ query: "memo shared", limit: 1 })).hits.length, 1);
- assert.deepEqual((await app.search("private-only", [bob])).hits, []);
- assert.deepEqual(await app.searchSessions(bob.id, "private-only"), []);
members = [alice];
- assert.equal((await tools.search!({ query: "memo" })).hits.length, 3);
+ assert.equal((await tools.search!({ query: "memo" })).hits.length, 2);
assert.equal((await tools.search!({ query: "memo", limit: 1 })).hits.length, 1);
members = [bob, { id: "[email protected]", type: "internal" }];
assert.deepEqual((await tools.search!({ query: "memo" })).hits, []);
});
-
-test("shared session search resolves team labels against current membership", async () => {
- const { createSessionMethods } = await import("../src/api/app-sessions.ts");
- const { createMemorySessionStore } = await import("../src/sessions/memory-session-store.ts");
- const { createIdentityService } = await import("../src/identity/identity-service.ts");
- const sessions = createMemorySessionStore();
- const source = await sessions.getOrCreateByThread("web:team-search", "group", scopeId("group", "team-search"));
- await sessions.addParticipant(source.id, alice.id, undefined, { includeHistory: true });
- await sessions.addParticipant(source.id, bob.id, undefined, { includeHistory: true });
- const { lease } = await sessions.acquireLease(source.id);
- assert.ok(lease);
- await sessions.append(lease, {
- type: "assistant",
- payload: { text: "engineering memo" },
- scopeLabel: scopeId("team", "eng"),
- });
- await sessions.releaseLease(lease);
- let member = alice.id;
- const methods = createSessionMethods(
- {
- sessions,
- identity: createIdentityService(),
- teams: { isMember: async (id, team) => id === member && team === "eng" },
- } as import("../src/api/app-types.ts").AppDeps,
- { managedProjectMembership: async () => undefined } as unknown as import("../src/api/app-helpers.ts").AppHelpers,
- );
- assert.equal((await methods.searchSessions(alice.id, "engineering")).length, 1);
- assert.deepEqual(await methods.searchSessions(bob.id, "engineering"), []);
- member = bob.id;
- assert.deepEqual(await methods.searchSessions(alice.id, "engineering"), []);
- assert.equal((await methods.searchSessions(bob.id, "engineering")).length, 1);
-});
diff --git a/test/session-search.test.ts b/test/session-search.test.ts
index babb4bd16..a04a485df 100644
--- a/test/session-search.test.ts
+++ b/test/session-search.test.ts
@@ -80,7 +80,7 @@ test("memory store: search ignores non-conversational entry types", async () =>
const { lease } = await store.acquireLease(s.id);
await store.append(lease!, {
type: "tool_call",
- payload: { text: "grep zanzibar" },
+ payload: { tool: "execute", callId: "c1", text: "grep zanzibar" },
scopeLabel: scopeId("personal", "U1"),
});
await store.append(lease!, {
@@ -106,7 +106,7 @@ test("app.searchSessions decorates hits with session metadata and enforces visib
const { lease } = await sessions.acquireLease(id);
await sessions.append(lease!, {
type: "tool_result",
- payload: { text: "zanzibar weather outputonly" },
+ payload: { tool: "execute", callId: "c1", text: "zanzibar weather outputonly" },
scopeLabel: scopeId("personal", "U1"),
});
await sessions.releaseLease(lease!);
diff --git a/test/session-sharing.test.ts b/test/session-sharing.test.ts
index 7389f512a..c3d3aa176 100644
--- a/test/session-sharing.test.ts
+++ b/test/session-sharing.test.ts
@@ -56,6 +56,16 @@ test("shared transcript allowlists visible message fields and published replies"
);
});
+test("a delivery into the conversation is shared as the assistant's message", () => {
+ assert.deepEqual(
+ sharedMessages([entry("user", { text: "remind me" }, 1), entry("assistant", { text: "Standup now" }, 2)]),
+ [
+ { role: "user", text: "remind me" },
+ { role: "assistant", text: "Standup now" },
+ ],
+ );
+});
+
test("work phases and approval decisions remain outside the shared message snapshot", () => {
assert.deepEqual(
sharedMessages([
@@ -254,14 +264,13 @@ test("attachment projection includes only user and delivered attachments", () =>
entry("tool_result", { tool: "attach", files: [{ artifactId: "old", name: "a" }] }, 4),
entry("tool_result", { tool: "attach", files: [{ artifactId: "new", name: "a" }] }, 5),
entry("assistant", { text: "Here" }, 6),
- entry("delivery", { files: [{ artifactId: "new" }] }, 7),
entry("tool_call", { action: "post", callId: "p", text: "Posted" }, 8),
entry("tool_result", { callId: "p", files: [{ artifactId: "posted" }] }, 9),
entry("assistant", { text: "private final" }, 10),
]);
assert.deepEqual(messages, [
{ role: "user", text: "", attachmentIds: ["user"] },
- { role: "assistant", text: "Here", attachmentIds: ["new"] },
+ { role: "assistant", text: "Here" },
{ role: "assistant", text: "Posted", attachmentIds: ["posted"] },
]);
});
diff --git a/test/session-store.test.ts b/test/session-store.test.ts
index 095518470..4baf85271 100644
--- a/test/session-store.test.ts
+++ b/test/session-store.test.ts
@@ -806,7 +806,7 @@ for (const [name, make] of backends) {
await store.append(lease, { type: "user", payload: { text: "hi" }, scopeLabel: scope });
const lastUser = await store.append(lease, { type: "user", payload: { text: "still here" }, scopeLabel: scope });
await store.append(lease, { type: "assistant", payload: { text: "yo" }, scopeLabel: scope });
- await store.append(lease, { type: "tool_call", payload: {}, scopeLabel: scope });
+ await store.append(lease, { type: "tool_call", payload: { tool: "execute", callId: "c1" }, scopeLabel: scope });
await store.releaseLease(lease);
const row = (await store.listByParticipant("U1")).find((x) => x.id === s.id)!;
@@ -838,7 +838,7 @@ for (const [name, make] of backends) {
const { lease } = await store.acquireLease(s.id);
assert.ok(lease);
- await store.append(lease, { type: "system", payload: { text: "note" }, scopeLabel: scope });
+ await store.append(lease, { type: "system", payload: { kind: "wake", text: "note" }, scopeLabel: scope });
await store.releaseLease(lease);
assert.equal(
diff --git a/test/session-syscalls.test.ts b/test/session-syscalls.test.ts
index ba9abbce0..83bbd8160 100644
--- a/test/session-syscalls.test.ts
+++ b/test/session-syscalls.test.ts
@@ -403,41 +403,6 @@ test("session reads hide history outside the viewer's participant tenure", async
assert.doesNotMatch(view.rendered, /hidden-before-membership/);
});
-test("shared session reads require every audience member to see each entry", async () => {
- const r = await rig();
- const other: Principal = { id: "U2", type: "internal" };
- const sharedScope = scopeId("group", "G1");
- const shared = await r.sessions.getOrCreateByThread("group:G1", "group", sharedScope);
- await r.sessions.addParticipant(shared.id, actor.id);
- await r.sessions.addParticipant(shared.id, other.id);
- const { lease } = await r.sessions.acquireLease(shared.id);
- assert.ok(lease);
- await r.sessions.append(lease, { type: "assistant", payload: { text: "private-only" }, scopeLabel: scope });
- await r.sessions.append(lease, { type: "assistant", payload: { text: "shared-visible" }, scopeLabel: sharedScope });
- await r.sessions.releaseLease(lease);
- const factory = createSessionSyscalls({
- mailbox: r.mailbox,
- sessions: r.sessions,
- runs: r.runs,
- signals: r.signals,
- maxAttempts: 3,
- });
- const tool = factory.forTurn({
- session: shared,
- scopeId: sharedScope,
- orgScopeId: scopeId("org", "example"),
- request: {
- surface: "web",
- actor,
- conversation: { kind: "group", threadRef: shared.threadRef, audience: [actor, other] },
- },
- });
- const view = await tool.read({ target: shared.id });
- assert.ok(view.ok && view.mode === "transcript");
- assert.doesNotMatch(view.rendered, /private-only/);
- assert.match(view.rendered, /shared-visible/);
-});
-
test("child runs retain the managed roster and read-only floor", async () => {
const r = await rig();
const tool = createSessionSyscalls({
@@ -1860,38 +1825,6 @@ test("sessions list shows only sidebar sessions the whole audience can see and c
});
for (const out of [await fromChild.list!(), await fromChild.start!({ fork: true })])
assert.equal(out.ok, true, JSON.stringify(out));
- const unshareable =
- "this conversation's earlier history was shared with a different audience, so it can't be forked from here.";
- const checkpoint = async (audience: string, throughSeq: number) => {
- const { lease } = await sessions.acquireLease(room.id);
- assert.ok(lease);
- await sessions.append(lease, {
- type: "user",
- scopeLabel: scope,
- payload: { text: "hi", memoryContext: { kind: "memory_context", snapshot: { audience }, throughSeq } },
- });
- await sessions.releaseLease(lease);
- };
- assert.deepEqual(await forTurn(true, { memory: "before" }).start!({ fork: true }), {
- ok: false,
- message: unshareable,
- });
- await checkpoint("before", 0);
- assert.deepEqual(await forTurn(true, { memory: "before" }).start!({ fork: true }), {
- ok: false,
- message: unshareable,
- });
- await checkpoint("before", -1);
- const sameAudience = forTurn(true, { memory: "before" });
- assert.equal((await sameAudience.start!({ fork: true })).ok, true);
- const listedTitles = await sameAudience.list!();
- assert.ok(listedTitles.ok);
- assert.ok(listedTitles.sessions.some((s) => s.title === "room"));
- const newAudience = forTurn(true, { memory: "after" });
- assert.deepEqual(await newAudience.start!({ fork: true }), { ok: false, message: unshareable });
- const hidden = await newAudience.list!();
- assert.ok(hidden.ok);
- assert.ok(hidden.sessions.some((s) => s.title === "room"));
const slack = forTurn(true, { surface: "slack" });
for (const out of [
await slack.list!(),
@@ -1903,7 +1836,7 @@ test("sessions list shows only sidebar sessions the whole audience can see and c
const out = await blocked.start!({ fork: true });
assert.equal(out.ok, false);
}
- assert.equal(started.length, 4);
+ assert.equal(started.length, 3);
});
test("stopping a session tree tells every stopped run who stopped it", async () => {
diff --git a/test/skills-callable.test.ts b/test/skills-callable.test.ts
index 66a8c9fd0..755ef8fdc 100644
--- a/test/skills-callable.test.ts
+++ b/test/skills-callable.test.ts
@@ -205,7 +205,7 @@ test("shortlist uses visible history, resets with compaction, and leaves the sys
assert.ok(lease);
await built.sessions.append(lease, {
type: "tool_call",
- payload: { tool: "skills", action: "read", name: "helper-19" },
+ payload: { tool: "skills", callId: "skills-1", action: "read", name: "helper-19" },
scopeLabel: "personal:U1",
});
await built.sessions.releaseLease(lease);
diff --git a/test/tool-output-readback.test.ts b/test/tool-output-readback.test.ts
new file mode 100644
index 000000000..27649ddb7
--- /dev/null
+++ b/test/tool-output-readback.test.ts
@@ -0,0 +1,44 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { createToolContext } from "../src/tools/primitives.ts";
+import type { Sandbox } from "../src/sandbox/sandbox.ts";
+import type { ToolOutputStore } from "../src/sessions/tool-output.ts";
+import { scopeId } from "../src/types.ts";
+
+function readBack(parked: Awaited<ReturnType<ToolOutputStore["read"]>>) {
+ const written: Array<[string, string]> = [];
+ const tc = createToolContext({
+ sandbox: {
+ readFile: async () => null,
+ writeFile: async (_h: unknown, path: string, data: string) => void written.push([path, data]),
+ } as unknown as Sandbox,
+ provision: async () => ({ id: "h", rootDir: "/workspace" }),
+ layers: [{ scopeId: scopeId("personal", "U1"), mountPath: "", mode: "rw" }],
+ commandPolicy: () => ({ mode: "denylist", rules: [] }),
+ authorizeCommand: () => false,
+ grantedHandles: [],
+ workspace: {} as never,
+ deploy: {} as never,
+ acl: {} as never,
+ createdBy: "U1",
+ sessionId: "s1",
+ toolOutput: { read: async () => parked } as unknown as ToolOutputStore,
+ });
+ return { tc, written };
+}
+
+test("reading a parked tool output copies the full text onto the computer for execute to slice", async () => {
+ const full = Array.from({ length: 30_000 }, (_, i) => `row ${i + 1}`).join("\n");
+ const { tc, written } = readBack({ text: full });
+ const read = await tc.read("tool-output/toolu_1");
+ assert.deepEqual(written, [["tool-output/toolu_1", full]]);
+ assert.match(String(read.content), /copied the full output \(\d+ chars\) to tool-output\/toolu_1 on the computer/);
+ assert.ok(String(read.content).length < 300);
+});
+
+test("an expired parked output says so and writes nothing", async () => {
+ const { tc, written } = readBack({ expired: true });
+ const read = await tc.read("tool-output/toolu_1");
+ assert.deepEqual(written, []);
+ assert.match(String(read.content), /expired after 30 days/);
+});
diff --git a/test/tool-output.test.ts b/test/tool-output.test.ts
new file mode 100644
index 000000000..c59590ee9
--- /dev/null
+++ b/test/tool-output.test.ts
@@ -0,0 +1,55 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import {
+ cutToolOutput,
+ createMemoryToolOutputStore,
+ DEFAULT_INLINE_TOKENS,
+ inlineTokensFrom,
+ toolOutputCallId,
+} from "../src/sessions/tool-output.ts";
+import type { SessionEntry } from "../src/types.ts";
+
+test("the inline cap setting: unset means 4096 tokens, 0 means off, junk falls back", () => {
+ assert.equal(inlineTokensFrom(undefined), DEFAULT_INLINE_TOKENS);
+ assert.equal(inlineTokensFrom(null), DEFAULT_INLINE_TOKENS);
+ assert.equal(inlineTokensFrom(0), 0);
+ assert.equal(inlineTokensFrom("2048"), 2048);
+ assert.equal(inlineTokensFrom(-1), DEFAULT_INLINE_TOKENS);
+ assert.equal(inlineTokensFrom({}), DEFAULT_INLINE_TOKENS);
+});
+
+test("cutToolOutput keeps text within the budget untouched and halves the rest between head and tail", () => {
+ assert.equal(cutToolOutput("short", 10, "c1"), null);
+ assert.equal(cutToolOutput("x".repeat(1_000), 0, "c1"), null);
+ const text = "H".repeat(500) + "T".repeat(500);
+ const cut = cutToolOutput(text, 50, "c1")!;
+ assert.equal(cut.shownChars, 200);
+ assert.ok(cut.text.startsWith("H".repeat(100)));
+ assert.ok(cut.text.endsWith("T".repeat(100)));
+ assert.match(cut.text, /tool-output\/c1/);
+});
+
+test("tool-output paths name one call", () => {
+ assert.equal(toolOutputCallId("tool-output/toolu_1"), "toolu_1");
+ assert.equal(toolOutputCallId("tool-output/"), null);
+ assert.equal(toolOutputCallId("tool-output/a/b"), null);
+ assert.equal(toolOutputCallId("notes/tool-output/a"), null);
+});
+
+test("a parked result reads back only through its own session's tool result", async () => {
+ const entries: SessionEntry[] = [];
+ const store = createMemoryToolOutputStore({ sessions: { getEntries: async (id) => (id === "s1" ? entries : []) } });
+ const blob = await store.save("the whole thing");
+ entries.push({
+ sessionId: "s1",
+ seq: 0,
+ parentSeq: null,
+ type: "tool_result",
+ payload: { tool: "execute", callId: "c1", result: "the…", output: { blob, chars: 15, shownChars: 4 } },
+ scopeLabel: "personal:u",
+ createdAt: 1,
+ });
+ assert.deepEqual(await store.read("s1", "c1"), { text: "the whole thing" });
+ assert.equal(await store.read("s2", "c1"), null);
+ assert.equal(await store.read("s1", "c2"), null);
+});
diff --git a/test/turn-delivery-dedup.test.ts b/test/turn-delivery-dedup.test.ts
index aa3744f2d..b4ef23994 100644
--- a/test/turn-delivery-dedup.test.ts
+++ b/test/turn-delivery-dedup.test.ts
@@ -115,8 +115,9 @@ test("completedSurfaceEnqueues counts only completed surface enqueue calls after
entry(5, "tool_call", { tool: "slack", action: "reach", callId: "c" }),
entry(6, "tool_result", { tool: "slack", action: "reach", callId: "c", ok: true }),
entry(7, "tool_call", { tool: "slack", action: "post", callId: "d" }),
+ entry(8, "tool_result", { tool: "slack", callId: "d", interrupted: true, isError: true }),
];
- assert.equal(completedSurfaceEnqueues(entries, 0, "slack"), 2, "the dangling post at seq 7 is not counted");
+ assert.equal(completedSurfaceEnqueues(entries, 0, "slack"), 2, "the interrupted post at seq 7 is not counted");
assert.equal(completedSurfaceEnqueues(entries, 5, "slack"), 0, "entries at or before sinceSeq are excluded");
const noCallIds = [
diff --git a/test/turn-resume.test.ts b/test/turn-resume.test.ts
index 4f5e035f1..13ecc7c39 100644
--- a/test/turn-resume.test.ts
+++ b/test/turn-resume.test.ts
@@ -17,7 +17,7 @@ const assistant = (text: string, seq: number) => ent("assistant", { text }, seq)
const toolCall = (seq: number) => ent("tool_call", { tool: "execute", callId: `c${seq}`, command: "make build" }, seq);
const toolResult = (seq: number) => ent("tool_result", { callId: `c${seq - 1}`, result: "ok" }, seq);
const steer = (text: string, seq: number) => ent("user", { text, ts: String(seq), steered: true, runId: "run-1" }, seq);
-const delivered = (text: string, seq: number) => ent("assistant", { text, deliveryKey: `run:other-${seq}` }, seq);
+const delivered = (text: string, seq: number) => ent("assistant", { text, runId: "other-turn" }, seq);
test("findTrailingPartialTurn finds the dead attempt's user entry and counts its partial work", () => {
const entries = [
@@ -69,7 +69,6 @@ test("platform rails and render-only rows are NOT work — only tool rows replay
ent("system", { kind: "file_event", problem: "expired upload" }, 2),
ent("thinking", { thinking: "let me plan", thinkingSignature: "SIG" }, 3),
ent("text", { text: "on it" }, 4),
- ent("delivery", { text: "ack" }, 5),
];
const found = findTrailingPartialTurn(entries, "run-1");
assert.deepEqual(found, { userSeq: 1, workEntries: 0 });
@@ -171,8 +170,8 @@ test("turnAtSeq reads through a mid-turn steer, which is the person adding to th
});
});
-test("turnAtSeq ignores an assistant entry that is another conversation's delivery", () => {
- const entries = [user("mine", 10), toolCall(11), delivered("Nightly report", 12)];
+test("turnAtSeq ignores another conversation's delivery", () => {
+ const entries = [ent("user", { text: "mine", runId: "my-turn" }, 10), toolCall(11), delivered("Nightly report", 12)];
assert.deepEqual(turnAtSeq(entries, 10), { userSeq: 10, workEntries: 1 });
});
@@ -296,3 +295,9 @@ test("resumeStrategy continues silently whenever every call is answered", () =>
"retry",
);
});
+
+test("an unrelated message does not close an interrupted turn or supply its answer", () => {
+ const entries = [ent("user", { text: "mine", runId: "my-turn" }, 10), toolCall(11), delivered("Nightly report", 12)];
+ assert.deepEqual(findTrailingPartialTurn(entries, "my-turn"), { userSeq: 10, workEntries: 1 });
+ assert.deepEqual(turnAtSeq(entries, 10), { userSeq: 10, workEntries: 1 });
+});
diff --git a/test/web-transcript-delivery.test.ts b/test/web-transcript-delivery.test.ts
index d72ad5260..5924c2b99 100644
--- a/test/web-transcript-delivery.test.ts
+++ b/test/web-transcript-delivery.test.ts
@@ -6,6 +6,7 @@ import { createMemoryRunStore } from "../src/runs/memory-run-store.ts";
import { withWebTranscriptDeliveries } from "../src/delivery/web-transcript-delivery.ts";
import { wireRunResultDeliveries } from "../src/delivery/run-result-delivery.ts";
import { createTranscriptSource } from "../src/sessions/transcript-source.ts";
+import { reconstructMessagesFromHistory } from "../src/harness/replay.ts";
import { sleep } from "../src/util/async.ts";
import type { DeliveryStore } from "../src/delivery/delivery-store.ts";
import type { SessionStore } from "../src/sessions/session-store.ts";
@@ -61,13 +62,23 @@ test("enqueue stays a plain insert; the drain writes the text durably before the
assert.equal(drained.length, 1, "the delivery is visible once its text is durable");
const entries = await sessions.getEntries(session.id);
- assert.equal(entries.length, 1);
- assert.equal(entries[0]!.type, "assistant");
- assert.deepEqual(entries[0]!.payload, {
- text: "Reminder: standup in 10 minutes",
- deliveryKey: "agent:main:cron:c1:100",
- via: "cron",
- });
+ assert.deepEqual(
+ entries.map((e) => [e.type, e.payload]),
+ [
+ [
+ "assistant",
+ {
+ text: "Reminder: standup in 10 minutes",
+ via: "cron",
+ runId: drained[0]!.id,
+ },
+ ],
+ ],
+ );
+ const messages = reconstructMessagesFromHistory(entries);
+ assert.equal(messages.at(-1)!.role, "assistant");
+ assert.match(JSON.stringify(messages), /Reminder: standup in 10 minutes/);
+ assert.doesNotMatch(JSON.stringify(messages), /toolCall|toolResult/);
});
test("the recorded entry renders through the transcript source", async () => {
@@ -83,8 +94,10 @@ test("the recorded entry renders through the transcript source", async () => {
await deliveries.pending("web");
const projected = (await createTranscriptSource(sessions).forRender(session.id)).entries;
- assert.equal(projected.length, 1);
- assert.equal(projected[0]!.type, "assistant");
+ assert.deepEqual(
+ projected.map((e) => e.type),
+ ["assistant"],
+ );
assert.equal((projected[0]!.payload as { text?: string }).text, "cron reply body");
});
@@ -103,7 +116,7 @@ test("repeated drains and a restarted decorator never duplicate the transcript e
const restarted = withWebTranscriptDeliveries(inner, sessions);
await restarted.pending("web");
- assert.equal((await sessions.getEntries(session.id)).length, 1, "one entry across drains and restarts");
+ assert.equal((await sessions.getEntries(session.id)).length, 1, "one delivery across drains and restarts");
});
test("a failed transcript write converges on retry: one entry", async () => {
@@ -111,12 +124,12 @@ test("a failed transcript write converges on retry: one entry", async () => {
let failNextAppend = true;
const flaky = {
...sessions,
- append: async (...args: Parameters<SessionStore["append"]>) => {
+ appendMany: async (...args: Parameters<SessionStore["appendMany"]>) => {
if (failNextAppend) {
failNextAppend = false;
throw new Error("transient append failure");
}
- return sessions.append(...args);
+ return sessions.appendMany(...args);
},
};
const inner = createDeliveryStore();
@@ -336,7 +349,7 @@ test("another run's failure record never suppresses this run's note", async () =
const { lease } = await sessions.acquireLease(session.id, "turn");
await sessions.append(lease!, {
type: "system",
- payload: { kind: "turn_failure", message: "earlier run, delivered note", deliveryKey: "run:other" },
+ payload: { kind: "turn_failure", message: "earlier run, delivered note", runId: "earlier-run" },
scopeLabel: SCOPE,
});
await sessions.append(lease!, {
@@ -387,7 +400,7 @@ test("a recovered attachments-only reply is nudged, never silently settled away"
assert.equal((await sessions.getEntries(session.id)).length, 0);
});
-test("a recovered reply the turn never recorded is written into the transcript, in the agent's plain voice", async () => {
+test("a recovered reply the turn never recorded is written into the transcript as the agent's delivery", async () => {
const { sessions, inner, deliveries } = wired();
const session = await webSession(sessions);
await terminalRun(deliveries, inner, { status: "ok", sessionId: session.id, reply: "recovered reply" });
@@ -397,7 +410,28 @@ test("a recovered reply the turn never recorded is written into the transcript,
const entries = await sessions.getEntries(session.id);
assert.equal(entries.length, 1);
assert.equal(entries[0]!.type, "assistant");
- assert.deepEqual(entries[0]!.payload, { text: "recovered reply", deliveryKey: drained[0]!.idempotencyKey });
+ assert.equal((entries[0]!.payload as { text: string }).text, "recovered reply");
+ const model = JSON.stringify(reconstructMessagesFromHistory(entries));
+ assert.match(model, /recovered reply/);
+ assert.doesNotMatch(model, /"via"/, "the turn's own reply carries no delivery badge");
+});
+
+test("a delivery marks the web conversation unread for its participant", async () => {
+ const { sessions, deliveries } = wired();
+ const session = await webSession(sessions);
+ await sessions.addParticipant(session.id, "[email protected]");
+ await sessions.updateParticipantView(session.id, "[email protected]", { readAt: 1 });
+ assert.notEqual((await sessions.getForParticipant(session.id, "[email protected]"))!.unread, true);
+
+ await deliveries.enqueue({
+ destination: webDestination(),
+ text: "Reminder: standup in 10 minutes",
+ idempotencyKey: "agent:main:cron:c1:900",
+ provenance: cronProvenance(),
+ });
+ await deliveries.pending("web");
+
+ assert.equal((await sessions.getForParticipant(session.id, "[email protected]"))!.unread, true);
});
test("Slack send receipts survive a restart, retry a busy session, and collapse against the Slack event", async () => {