Files
PaperClipAI/server/src/services/summary-slots.ts
T
Nicky LeachandPaperclip 2dd9811e8b Own agent hiring and termination in a lifecycle module (#15631)
## Thinking Path

> - Paperclip manages AI agents and their work.
> - Agent creation, approval, pause, and termination change whether an
agent can work.
> - These changes currently use separate service and database paths.
> - Some agents need setup after their record exists and before their
harness can run.
> - This PR adds one module that owns those state changes and their
transactions.
> - The module records progress and retries incomplete work after a
server restart.

## Linked Issues or Issue Description

**Problem or motivation**

A hire can become active before its execution environment is ready.
Direct agent state writes also bypass one common lifecycle boundary.
Termination does not have a durable step for resource cleanup.

**Proposed solution**

Use agent lifecycle commands for hiring, approval, pause, resume, and
termination. Keep proposed hires pending until approval. Prepare each
approved hire, test its saved harness configuration, and then permit
work. Wait for required plugin work before completing a step.

**Alternatives considered**

A general database commit hook would expose transaction details to
callers. A timer alone would delay each new hire. This module owns each
command transaction and starts its worker after commit. A periodic scan
recovers work after a process stops.

**Roadmap alignment**

This change adds the lifecycle boundary for the existing agent and
plugin systems. It does not add a user interface or a provider
implementation. It replaces the lifecycle delivery approach in #15391.
Related proposals include #11475 and #13099; this change does not add
their external executor protocols.

## What Changed

- Add durable approval, preparation, verification, pause, resume, stop,
and cleanup states.
- Keep agent creation, lifecycle state, configuration writes, and agent
record deletion behind `modules/agent-lifecycle`. Keep validation,
credential rules, reads, onboarding, invitations, general approvals, and
company data deletion in their services.
- Commit a hire before the remaining onboarding or invitation work.
Serialize requests with the existing dedicated database connection
helper. Reuse the hire after a failed attempt.
- Keep accepted work in the run queue during setup. Require execution
and cleanup evidence before completing pause or termination.
- Retain the original credential owner when approving migrated hires.
- Start lifecycle work after each command commits. Use database leases
and revision checks for retries and late results.
- Add a generic required plugin method and capability. Store the
required plugin IDs for each agent. Track host and plugin completion
separately.
- Keep harness verification and credential rules in services. Supply
their results to lifecycle through explicit ports. Reject failed logins
and incomplete probes.
- Remove reverse imports from lifecycle adapters into services. Share
record queries and validation below both layers.
- Use one hire decision path for agent IDs and approval IDs. Resolve the
approval and agent change in one transaction. Expose policy
reconciliation on the command object.
- Save configuration and verification invalidation through one module
operation. Ordinary commands own their transaction. Restrict the shared
transaction integration to its listed callers. Reject identity, company,
accounting, timestamp, and unknown fields.
- Require completed termination before deleting agent records. The
company deletion service calls the module through a separate deletion
interface.
- Save an approved secret before applying its agent binding. A failed
binding leaves the secret approved. A retry reuses it.
- Remove old permission and metadata export files. Move hire
configuration rules out of lifecycle persistence.
- Report failed worker steps and scans with operation context and known
error codes. Omit provider messages, stacks, and configuration values.
Document ordinary application use and extension rules.
- Recover expired native owners only after execution stops. Retry
cleanup of failed retained leases. Wait for setup in the live smoke
script.
- Keep manual, budget, and company pause reasons separate. Guard
execution status writes with the ready state.
- Add a migration, boundary checks, regression tests, and a lifecycle
guide. Keep the current UI.
- Limit periodic policy checks to 100 eligible agents per minute.
Continue from the last agent ID. Exclude pending hires and agents in
termination or a final state. Keep explicit company and budget updates
immediate.

## Verification

- Latest head `ccc3c86b0` aligns the shared status update schema with
the PATCH route. All 15 shared tests, 104 server route/OpenAPI tests, 2
CLI tests, and shared/server/CLI TypeScript checks pass. The Greptile
comment is fixed. CI is running; no new Apex review was requested for
this small fix.

- Rebase `75708ac94` rebases onto master at `2eb5187e4`. The backup test
uses the shared 90-second timeout from master; the older timeout commit
was removed. The affected backup test passes.
- Agent budget edits now pass the target agent ID through enforcement.
All 50 affected tests, server TypeScript, and 11 boundary tests pass.
Both configuration update paths have regression coverage. CI is running
on the latest head. No new Apex review was requested for these small
fixes.

- Rebase `9b568666a` preserves the run-control extraction and
delivery-work startup changes from master. All 559 affected tests,
server TypeScript, and 11 boundary tests pass. CI and Apex review are
running on the final head.
- The policy scan fix passes 56 lifecycle and diagnostic tests, 24
policy and caller tests, and 11 boundary tests. A regression test checks
the page limit, interval, excluded states, and cursor wraparound.

- Rebase `fd4698750` passes all 611 tests in eight affected lifecycle,
route, and heartbeat suites. Server TypeScript and all 11 module
boundary tests pass. The company deletion rollback and concurrent hire
tests are included.

- Follow-up `00aac7646` fixes the failed decision deletion test and the
public update schema. All 126 affected server tests, 8 shared tests, 2
CLI tests, and 11 boundary tests pass. Shared, server, CLI, and UI
TypeScript checks pass.

- Server TypeScript passes: `pnpm --filter @paperclipai/server exec tsc
--noEmit`.
- All 11 boundary tests pass: `node --test
scripts/check-module-boundaries.test.mjs`. The repository boundary scan
also passes.
- All 68 lifecycle, failure diagnostic, and harness verification tests
pass. Tests reject forbidden fields through the root command, combined
transition command, and restricted transaction integration. They also
check invalid fields returned by configuration preparation.
- Failure tests cover host work, plugin work, claims, renewals,
deferrals, and scans. Log tests check known error codes and exclusion of
private error contents.
- All 355 tests in 10 existing caller suites pass. These tests cover
secret approval, pairing, pending hires, credentials, company import,
built-in agents, managed agents, instructions, and skills.
- The branch is rebased onto `origin/master` at `2eb5187e4`. The queue
guards and cancellation lock fix use the extracted heartbeat queue.
Agent cancellation uses the extracted run-control file. The source diff
scan found no secrets or private URLs.
- Earlier checks cover approval retries, rollback, late verification
results, policy holds, deletion rollback, concurrent company deletion,
and queue admission during setup.
- Before the rebase, CI passed on `00aac7646` and Greptile Apex rated it
5/5 with no open comments. CI and a new Apex review are pending for the
rebased commit. The branch has no merge conflicts.
- The full local build and native Dot suite were not run. The host has
no Cargo. No live provider test was run.

## Risks

- Background policy recovery takes more than one minute when more than
100 agents are eligible. Explicit company and budget updates still
reconcile immediately.

- The restricted configuration integration keeps an agent reference and
its dependent records in one transaction. It cannot change a budget or
invoke a lifecycle transition. A periodic scan starts pending setup
after that transaction commits.
- Secret approval and binding use separate transactions. A failed
binding can leave an approved secret with no agent binding.
- New approved hires remain unavailable until setup and verification
complete. Existing clients see `paused` during these steps. This PR does
not change the UI.
- Migration `0320_daily_onslaught.sql` maps existing statuses and pause
reasons. It does not create external resources or claim that an existing
harness passed verification.
- Use a coordinated server update for this migration. Older server
versions can write legacy status without the new lifecycle checks.
- A failed onboarding or invitation request can leave a committed hire.
A retry reuses that hire and completes the remaining records.
- Deletion requires completed termination. Company deletion can require
another request while cleanup is pending.
- Required plugins must make repeated calls safe and reject older
revisions. A disabled or removed required plugin blocks completion until
it is restored.
- A new hire can run a second harness test after the existing UI test.
Some adapters can charge for this test.
- No live provider test was run. Provider implementation and resource
backfill remain separate work.

## Model Used

OpenAI GPT-6 through Codex, with reasoning, code tools, and local test
execution.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have described the issue in this PR with the feature request
field labels
- [x] I have not referenced internal or instance-local issues or links
- [x] My branch name describes the change
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation
- [x] I have considered and documented risks
- [x] Targeted local tests pass
- [x] All Paperclip CI gates are green on the new commit
- [x] Greptile Apex is 5/5 with no open comments on the new commit
- [x] I will address review comments before requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-10-09 09:05:12 -07:00

764 lines
27 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { and, desc, eq, gte, inArray, isNull } from "drizzle-orm";
import type { Db } from "@paperclipai/db";
import {
documentRevisions,
documents,
executionWorkspaces,
issues,
projectWorkspaces,
projects,
summarySlots,
} from "@paperclipai/db";
import {
type GenerateSummarySlotResponse,
type GetSummarySlotResponse,
type IssueStatus,
type ListSummarySlotRevisionsResponse,
type SummarySlot,
type SummarySlotDocument,
type SummarySlotIssueRef,
type SummarySlotRevision,
type SummarySlotScopeKind,
type SummarySlotScopeSelector,
summarySlotScopeSelectorSchema,
type WriteSummarySlotResponse,
} from "@paperclipai/shared";
import { conflict, forbidden, notFound, unprocessable } from "../errors.js";
import { readBuiltInAgentMarker } from "../lib/built-in-agent-metadata.js";
import { builtInAgentService } from "./built-in-agents.js";
import { agentService } from "./agents.js";
import { issueService } from "./issues.js";
/** Built-in agent key for the Summarizer bundle (see PAP-13920). */
export const SUMMARIZER_BUILT_IN_KEY = "summarizer";
/** Generation issues in these statuses are no longer active and can be superseded. */
const TERMINAL_ISSUE_STATUSES = new Set<IssueStatus>(["done", "cancelled"]);
const DEFAULT_SUMMARY_FORMAT = "markdown";
const SUMMARY_SLOT_REVISION_LIMIT = 20;
const SUMMARY_SNAPSHOT_GROUP_LIMIT = 12;
const SUMMARY_SNAPSHOT_INITIAL_LOOKBACK_MS = 7 * 24 * 60 * 60 * 1_000;
export interface SummarySlotSelectorInput {
companyId: string;
scopeKind: string;
slotKey: string;
scopeId?: string | null;
}
export interface SummaryGenerateActor {
agentId?: string | null;
userId?: string | null;
runId?: string | null;
}
export interface SummaryWriteActor {
agentId?: string | null;
runId?: string | null;
}
type ResolvedSelector = SummarySlotScopeSelector & {
companyId: string;
scopeId: string | null;
};
type SummarySlotRow = typeof summarySlots.$inferSelect;
function mapSlot(row: SummarySlotRow): SummarySlot {
return {
id: row.id,
companyId: row.companyId,
scopeKind: row.scopeKind,
scopeId: row.scopeId ?? null,
slotKey: row.slotKey,
documentId: row.documentId ?? null,
status: row.status,
failureReason: row.failureReason ?? null,
generatingIssueId: row.generatingIssueId ?? null,
lastGeneratedAt: row.lastGeneratedAt ?? null,
lastGeneratedByAgentId: row.lastGeneratedByAgentId ?? null,
lastModel: row.lastModel ?? null,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
function mapDocument(row: typeof documents.$inferSelect): SummarySlotDocument {
return {
id: row.id,
companyId: row.companyId,
title: row.title ?? null,
format: row.format as SummarySlotDocument["format"],
body: row.latestBody,
latestRevisionId: row.latestRevisionId ?? null,
latestRevisionNumber: row.latestRevisionNumber,
createdByAgentId: row.createdByAgentId ?? null,
createdByUserId: row.createdByUserId ?? null,
updatedByAgentId: row.updatedByAgentId ?? null,
updatedByUserId: row.updatedByUserId ?? null,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
function mapRevision(row: typeof documentRevisions.$inferSelect): SummarySlotRevision {
return {
id: row.id,
companyId: row.companyId,
documentId: row.documentId,
revisionNumber: row.revisionNumber,
title: row.title ?? null,
format: row.format as SummarySlotRevision["format"],
body: row.body,
changeSummary: row.changeSummary ?? null,
createdByAgentId: row.createdByAgentId ?? null,
createdByUserId: row.createdByUserId ?? null,
createdByRunId: row.createdByRunId ?? null,
createdAt: row.createdAt,
};
}
function scopeLabel(scopeKind: SummarySlotScopeKind): string {
switch (scopeKind) {
case "project":
return "project";
case "project_workspace":
return "project workspace";
case "execution_workspace":
return "execution workspace";
case "workspaces_overview":
return "workspaces overview";
default:
return "target";
}
}
export function summarySlotService(db: Db) {
const builtIns = builtInAgentService(db);
const agents = agentService(db);
const issuesSvc = issueService(db);
function resolveSelector(input: SummarySlotSelectorInput): ResolvedSelector {
const parsed = summarySlotScopeSelectorSchema.safeParse({
scopeKind: input.scopeKind,
slotKey: input.slotKey,
scopeId: input.scopeId ?? undefined,
});
if (!parsed.success) {
throw unprocessable("Invalid summary slot selector", parsed.error.issues);
}
return {
...parsed.data,
companyId: input.companyId,
scopeId: parsed.data.scopeId ?? null,
};
}
/** Enforce that the scope target exists inside the company boundary. */
async function assertTargetVisible(sel: ResolvedSelector): Promise<void> {
if (sel.scopeKind === "workspaces_overview") return;
if (!sel.scopeId) {
// Guaranteed by the selector schema, but keep the invariant explicit.
throw unprocessable(`${sel.scopeKind} summary slots require scopeId`);
}
if (sel.scopeKind === "project") {
const row = await db
.select({ id: projects.id })
.from(projects)
.where(and(eq(projects.id, sel.scopeId), eq(projects.companyId, sel.companyId)))
.then((rows) => rows[0] ?? null);
if (!row) throw notFound("Summary target not found");
return;
}
if (sel.scopeKind === "project_workspace") {
const row = await db
.select({ id: projectWorkspaces.id })
.from(projectWorkspaces)
.where(and(eq(projectWorkspaces.id, sel.scopeId), eq(projectWorkspaces.companyId, sel.companyId)))
.then((rows) => rows[0] ?? null);
if (!row) throw notFound("Summary target not found");
return;
}
if (sel.scopeKind === "execution_workspace") {
const row = await db
.select({ id: executionWorkspaces.id })
.from(executionWorkspaces)
.where(and(eq(executionWorkspaces.id, sel.scopeId), eq(executionWorkspaces.companyId, sel.companyId)))
.then((rows) => rows[0] ?? null);
if (!row) throw notFound("Summary target not found");
}
}
function findSlotRow(sel: ResolvedSelector) {
return db
.select()
.from(summarySlots)
.where(
and(
eq(summarySlots.companyId, sel.companyId),
eq(summarySlots.scopeKind, sel.scopeKind),
eq(summarySlots.slotKey, sel.slotKey),
sel.scopeId === null ? isNull(summarySlots.scopeId) : eq(summarySlots.scopeId, sel.scopeId),
),
)
.then((rows) => rows[0] ?? null);
}
async function loadDocument(companyId: string, documentId: string | null) {
if (!documentId) return null;
return db
.select()
.from(documents)
.where(and(eq(documents.id, documentId), eq(documents.companyId, companyId)))
.then((rows) => rows[0] ?? null);
}
async function loadIssueRef(companyId: string, issueId: string | null): Promise<{
ref: SummarySlotIssueRef | null;
row: typeof issues.$inferSelect | null;
}> {
if (!issueId) return { ref: null, row: null };
const row = await db
.select()
.from(issues)
.where(and(eq(issues.id, issueId), eq(issues.companyId, companyId)))
.then((rows) => rows[0] ?? null);
if (!row) return { ref: null, row: null };
return {
row,
ref: {
id: row.id,
identifier: row.identifier ?? null,
title: row.title,
status: row.status as IssueStatus,
assigneeAgentId: row.assigneeAgentId ?? null,
},
};
}
function isIssueActive(row: typeof issues.$inferSelect | null): boolean {
return !!row && !TERMINAL_ISSUE_STATUSES.has(row.status as IssueStatus);
}
async function getSlot(input: SummarySlotSelectorInput): Promise<GetSummarySlotResponse> {
const sel = resolveSelector(input);
await assertTargetVisible(sel);
const slotRow = await findSlotRow(sel);
if (!slotRow) return { slot: null, document: null, generatingIssue: null };
const [documentRow, issueRef] = await Promise.all([
loadDocument(sel.companyId, slotRow.documentId ?? null),
loadIssueRef(sel.companyId, slotRow.generatingIssueId ?? null),
]);
return {
slot: mapSlot(slotRow),
document: documentRow ? mapDocument(documentRow) : null,
generatingIssue: issueRef.ref,
};
}
async function listRevisions(input: SummarySlotSelectorInput): Promise<ListSummarySlotRevisionsResponse> {
const sel = resolveSelector(input);
await assertTargetVisible(sel);
const slotRow = await findSlotRow(sel);
if (!slotRow || !slotRow.documentId) {
return { slot: slotRow ? mapSlot(slotRow) : null, revisions: [] };
}
const revisions = await db
.select()
.from(documentRevisions)
.where(
and(
eq(documentRevisions.documentId, slotRow.documentId),
eq(documentRevisions.companyId, sel.companyId),
),
)
.orderBy(desc(documentRevisions.revisionNumber))
.limit(SUMMARY_SLOT_REVISION_LIMIT);
return { slot: mapSlot(slotRow), revisions: revisions.map(mapRevision) };
}
async function upsertSlot(
sel: ResolvedSelector,
patch: Partial<typeof summarySlots.$inferInsert>,
): Promise<SummarySlotRow> {
const now = new Date();
const [slot] = await db
.insert(summarySlots)
.values({
companyId: sel.companyId,
scopeKind: sel.scopeKind,
scopeId: sel.scopeId,
slotKey: sel.slotKey,
status: "idle",
createdAt: now,
updatedAt: now,
...patch,
})
.onConflictDoUpdate({
target: [
summarySlots.companyId,
summarySlots.scopeKind,
summarySlots.scopeId,
summarySlots.slotKey,
],
set: { ...patch, updatedAt: now },
})
.returning();
return slot;
}
async function resolveGenerationTargetProject(sel: ResolvedSelector): Promise<{
projectId: string | null;
projectWorkspaceId: string | null;
executionWorkspaceId: string | null;
}> {
if (sel.scopeKind === "project") {
return { projectId: sel.scopeId, projectWorkspaceId: null, executionWorkspaceId: null };
}
if (sel.scopeKind === "project_workspace" && sel.scopeId) {
const row = await db
.select({ projectId: projectWorkspaces.projectId })
.from(projectWorkspaces)
.where(and(eq(projectWorkspaces.id, sel.scopeId), eq(projectWorkspaces.companyId, sel.companyId)))
.then((rows) => rows[0] ?? null);
return {
projectId: row?.projectId ?? null,
projectWorkspaceId: sel.scopeId,
executionWorkspaceId: null,
};
}
if (sel.scopeKind === "execution_workspace" && sel.scopeId) {
const row = await db
.select({
projectId: executionWorkspaces.projectId,
projectWorkspaceId: executionWorkspaces.projectWorkspaceId,
})
.from(executionWorkspaces)
.where(and(eq(executionWorkspaces.id, sel.scopeId), eq(executionWorkspaces.companyId, sel.companyId)))
.then((rows) => rows[0] ?? null);
return {
projectId: row?.projectId ?? null,
projectWorkspaceId: row?.projectWorkspaceId ?? null,
executionWorkspaceId: sel.scopeId,
};
}
return { projectId: null, projectWorkspaceId: null, executionWorkspaceId: null };
}
function scopeIssueConditions(sel: ResolvedSelector) {
if (sel.scopeKind === "project") return [eq(issues.projectId, sel.scopeId!)];
if (sel.scopeKind === "project_workspace") return [eq(issues.projectWorkspaceId, sel.scopeId!)];
if (sel.scopeKind === "execution_workspace") return [eq(issues.executionWorkspaceId, sel.scopeId!)];
return [];
}
async function buildScopeSnapshot(sel: ResolvedSelector, previousGeneratedAt: Date | null): Promise<string> {
const commonConditions = [
eq(issues.companyId, sel.companyId),
isNull(issues.hiddenAt),
...scopeIssueConditions(sel),
];
const recentlyDoneSince = previousGeneratedAt
?? new Date(Date.now() - SUMMARY_SNAPSHOT_INITIAL_LOOKBACK_MS);
const selectFields = {
identifier: issues.identifier,
title: issues.title,
status: issues.status,
priority: issues.priority,
updatedAt: issues.updatedAt,
};
const [blocked, inReview, inProgress, recentlyDone] = await Promise.all([
db.select(selectFields).from(issues)
.where(and(...commonConditions, eq(issues.status, "blocked")))
.orderBy(desc(issues.updatedAt)).limit(SUMMARY_SNAPSHOT_GROUP_LIMIT),
db.select(selectFields).from(issues)
.where(and(...commonConditions, eq(issues.status, "in_review")))
.orderBy(desc(issues.updatedAt)).limit(SUMMARY_SNAPSHOT_GROUP_LIMIT),
db.select(selectFields).from(issues)
.where(and(...commonConditions, eq(issues.status, "in_progress")))
.orderBy(desc(issues.updatedAt)).limit(SUMMARY_SNAPSHOT_GROUP_LIMIT),
db.select(selectFields).from(issues)
.where(and(
...commonConditions,
inArray(issues.status, ["done"]),
gte(issues.updatedAt, recentlyDoneSince),
))
.orderBy(desc(issues.updatedAt)).limit(SUMMARY_SNAPSHOT_GROUP_LIMIT),
]);
const formatGroup = (
heading: string,
rows: Array<typeof blocked[number]>,
) => [
`### ${heading}`,
...(rows.length > 0
? rows.map((row) => {
const identifier = row.identifier ?? "Unnumbered issue";
const companyPrefix = row.identifier?.split("-", 1)[0];
const issueLink = companyPrefix
? `[${identifier}](/${companyPrefix}/issues/${identifier})`
: identifier;
return `- ${issueLink} — ${row.title} (${row.priority}; updated ${row.updatedAt.toISOString()})`;
})
: ["- None."]),
];
return [
"## Prebuilt scope snapshot",
"",
`Snapshot generated at ${new Date().toISOString()}. Recently done means updated since ${recentlyDoneSince.toISOString()}.`,
"Use this bounded, company-scoped snapshot as the issue source of truth for this run. Do not call issue-list endpoints.",
"",
...formatGroup("Blocked", blocked),
"",
...formatGroup("In review", inReview),
"",
...formatGroup("In progress", inProgress),
"",
...formatGroup("Recently done", recentlyDone),
].join("\n");
}
function generationIssueDescription(
sel: ResolvedSelector,
scopeSnapshot: string,
generationIssueId: string | null = null,
): string {
const target = sel.scopeId ? `\`${sel.scopeId}\`` : "the workspaces overview";
const summarySlotPath = `/api/companies/${encodeURIComponent(sel.companyId)}/summary-slots/${encodeURIComponent(sel.scopeKind)}/${encodeURIComponent(sel.slotKey)}`;
const scopeQuery = sel.scopeId ? `?scopeId=${encodeURIComponent(sel.scopeId)}` : "";
return [
`Generate the ${scopeLabel(sel.scopeKind)} summary for ${target}.`,
"",
"Call `/summarize-status`. Its API quick reference has the full request shapes; use these resolved routes for this generation:",
"",
`- Read current slot: \`GET ${summarySlotPath}${scopeQuery}\``,
`- Write revision: \`PUT ${summarySlotPath}\``,
"",
"Use this write payload:",
"",
"```json",
JSON.stringify(
{
scopeKind: sel.scopeKind,
scopeId: sel.scopeId,
slotKey: sel.slotKey,
generationIssueId,
},
null,
2,
),
"```",
"",
"Write one short, colloquial Markdown summary that opens with the 1–3 specific, concrete, actionable items the reader should do right now to unblock this work — each saying what to do and why it's the thing holding up progress, with an inline link — followed by a brief plain-prose status of where things stand. Use your judgment: read whatever issues you need to understand the state, then focus on what's most important. Write for a reader who has not memorized issue ids or threads. If genuinely nothing needs the reader, say so plainly in one line and name the next thing worth watching. Never a trailing list of issue links or any link dump. Not a task list.",
"The current-slot response includes the latest document body and `latestRevisionId`; use those directly.",
"Follow the skill's streaming protocol: emit the first plain-text `STATUS:` line immediately — named from the first task in the snapshot, before any analysis — keep emitting `STATUS:` lines as you think, and emit the sentinel-wrapped summary draft before the authoritative summary-slot write.",
"Pass the `generationIssueId` from the payload, the previous revision id when present, and the model actually used to the summary-slot write API.",
"",
scopeSnapshot,
"",
"Close this task with a short comment once the summary revision is written.",
].join("\n");
}
function generationIssueTitle(sel: ResolvedSelector, createdAt = new Date()): string {
const timestamp = createdAt.toISOString().replace("T", " ").replace(/:\d{2}\.\d{3}Z$/, " UTC");
return `Summarize ${scopeLabel(sel.scopeKind)} on ${timestamp}`;
}
async function generate(
input: SummarySlotSelectorInput,
actor: SummaryGenerateActor,
): Promise<GenerateSummarySlotResponse> {
const sel = resolveSelector(input);
await assertTargetVisible(sel);
const builtIn = await builtIns.get(sel.companyId, SUMMARIZER_BUILT_IN_KEY);
if (builtIn.status !== "ready" || !builtIn.agentId) {
throw unprocessable("Summarizer built-in agent is not configured", {
code: "summarizer_not_configured",
status: builtIn.status,
});
}
const summarizerAgentId = builtIn.agentId;
// Dedupe: if a generation is already active, return the in-flight state.
const existing = await findSlotRow(sel);
if (existing && existing.status === "generating" && existing.generatingIssueId) {
const active = await loadIssueRef(sel.companyId, existing.generatingIssueId);
if (isIssueActive(active.row)) {
return {
slot: mapSlot(existing),
generatingIssue: active.ref!,
alreadyGenerating: true,
};
}
}
const { projectId, projectWorkspaceId, executionWorkspaceId } = await resolveGenerationTargetProject(sel);
const scopeSnapshot = await buildScopeSnapshot(sel, existing?.lastGeneratedAt ?? null);
const createdAt = new Date();
const generationVersion = existing?.generatingIssueId ?? existing?.updatedAt.toISOString() ?? "initial";
let issueDeduplicated = false;
const created = await issuesSvc.create(sel.companyId, {
projectId,
projectWorkspaceId,
executionWorkspaceId,
title: generationIssueTitle(sel, createdAt),
description: generationIssueDescription(sel, scopeSnapshot),
status: "todo",
priority: "medium",
assigneeAgentId: summarizerAgentId,
createdByAgentId: actor.agentId ?? null,
createdByUserId: actor.userId ?? null,
hiddenAt: createdAt,
idempotencyKey: [
"summary-slot-generation",
sel.scopeKind,
sel.scopeId ?? "global",
sel.slotKey,
generationVersion,
].join(":"),
onDeduplicated: (reason) => {
issueDeduplicated = reason === "idempotency_key";
},
});
const generationIssue = (
await issuesSvc.update(created.id, {
description: generationIssueDescription(sel, scopeSnapshot, created.id),
})
) ?? created;
const slotRow = await upsertSlot(sel, {
status: "generating",
failureReason: null,
generatingIssueId: generationIssue.id,
});
return {
slot: mapSlot(slotRow),
generatingIssue: {
id: generationIssue.id,
identifier: generationIssue.identifier ?? null,
title: generationIssue.title,
status: generationIssue.status as IssueStatus,
assigneeAgentId: generationIssue.assigneeAgentId ?? null,
},
alreadyGenerating: issueDeduplicated,
};
}
async function assertSummarizerWriter(
sel: ResolvedSelector,
slotRow: SummarySlotRow | null,
input: { generationIssueId?: string | null },
actor: SummaryWriteActor,
): Promise<void> {
if (!actor.agentId) {
throw forbidden("Only the Summarizer built-in agent may write summaries");
}
const agent = await agents.getById(actor.agentId);
if (!agent || agent.companyId !== sel.companyId) {
throw forbidden("Only the Summarizer built-in agent may write summaries");
}
const marker = readBuiltInAgentMarker(agent.metadata);
if (marker?.key !== SUMMARIZER_BUILT_IN_KEY) {
throw forbidden("Only the Summarizer built-in agent may write summaries");
}
// The write must originate from the linked, in-flight generation task.
const generationIssueId = input.generationIssueId ?? null;
if (!generationIssueId) {
throw forbidden("Summary writes must identify the active generation task");
}
if (!slotRow?.generatingIssueId || slotRow.generatingIssueId !== generationIssueId) {
throw forbidden("Summary write does not match the active generation task");
}
const issueRef = await loadIssueRef(sel.companyId, generationIssueId);
if (!issueRef.row) {
throw forbidden("Linked generation task not found");
}
const payloadMatch = issueRef.row.description?.match(/```json\n([\s\S]*?)\n```/);
let payload: Record<string, unknown> | null = null;
try {
payload = payloadMatch ? (JSON.parse(payloadMatch[1]) as Record<string, unknown>) : null;
} catch {
payload = null;
}
if (
payload?.generationIssueId !== generationIssueId ||
payload.scopeKind !== sel.scopeKind ||
(payload.scopeId ?? null) !== sel.scopeId ||
payload.slotKey !== sel.slotKey
) {
throw forbidden("Generation task does not target this summary slot");
}
if (issueRef.row.assigneeAgentId !== actor.agentId) {
throw forbidden("Generation task is not assigned to this agent");
}
const runId = actor.runId ?? null;
const runMatches =
!!runId && (issueRef.row.checkoutRunId === runId || issueRef.row.executionRunId === runId);
if (!runMatches) {
throw forbidden("Summary write must run from the linked generation task");
}
}
async function write(
input: SummarySlotSelectorInput & {
markdown: string;
title?: string | null;
changeSummary?: string | null;
baseRevisionId?: string | null;
generationIssueId?: string | null;
model?: string | null;
},
actor: SummaryWriteActor,
): Promise<WriteSummarySlotResponse> {
const sel = resolveSelector(input);
await assertTargetVisible(sel);
const slotRow = await findSlotRow(sel);
await assertSummarizerWriter(sel, slotRow, input, actor);
const now = new Date();
const result = await db.transaction(async (tx) => {
const currentSlot = slotRow
? await tx
.select()
.from(summarySlots)
.where(eq(summarySlots.id, slotRow.id))
.then((rows) => rows[0] ?? null)
: null;
if (!currentSlot || currentSlot.generatingIssueId !== input.generationIssueId) {
throw conflict("Summary generation was superseded by a newer task");
}
let documentRow: typeof documents.$inferSelect;
let revisionRow: typeof documentRevisions.$inferSelect;
const existingDocument = currentSlot?.documentId
? await tx
.select()
.from(documents)
.where(and(eq(documents.id, currentSlot.documentId), eq(documents.companyId, sel.companyId)))
.then((rows) => rows[0] ?? null)
: null;
if (existingDocument) {
if (input.baseRevisionId && input.baseRevisionId !== existingDocument.latestRevisionId) {
throw conflict("Summary was updated by someone else", {
currentRevisionId: existingDocument.latestRevisionId,
});
}
const nextRevisionNumber = existingDocument.latestRevisionNumber + 1;
[revisionRow] = await tx
.insert(documentRevisions)
.values({
companyId: sel.companyId,
documentId: existingDocument.id,
revisionNumber: nextRevisionNumber,
title: input.title ?? null,
format: DEFAULT_SUMMARY_FORMAT,
body: input.markdown,
changeSummary: input.changeSummary ?? null,
createdByAgentId: actor.agentId ?? null,
createdByRunId: actor.runId ?? null,
createdAt: now,
})
.returning();
[documentRow] = await tx
.update(documents)
.set({
title: input.title ?? null,
format: DEFAULT_SUMMARY_FORMAT,
latestBody: input.markdown,
latestRevisionId: revisionRow.id,
latestRevisionNumber: nextRevisionNumber,
updatedByAgentId: actor.agentId ?? null,
updatedAt: now,
})
.where(eq(documents.id, existingDocument.id))
.returning();
} else {
[documentRow] = await tx
.insert(documents)
.values({
companyId: sel.companyId,
title: input.title ?? null,
format: DEFAULT_SUMMARY_FORMAT,
latestBody: input.markdown,
latestRevisionId: null,
latestRevisionNumber: 1,
createdByAgentId: actor.agentId ?? null,
updatedByAgentId: actor.agentId ?? null,
createdAt: now,
updatedAt: now,
})
.returning();
[revisionRow] = await tx
.insert(documentRevisions)
.values({
companyId: sel.companyId,
documentId: documentRow.id,
revisionNumber: 1,
title: input.title ?? null,
format: DEFAULT_SUMMARY_FORMAT,
body: input.markdown,
changeSummary: input.changeSummary ?? null,
createdByAgentId: actor.agentId ?? null,
createdByRunId: actor.runId ?? null,
createdAt: now,
})
.returning();
[documentRow] = await tx
.update(documents)
.set({ latestRevisionId: revisionRow.id })
.where(eq(documents.id, documentRow.id))
.returning();
}
const slotPatch = {
documentId: documentRow.id,
status: "idle" as const,
failureReason: null,
generatingIssueId: null,
lastGeneratedAt: now,
lastGeneratedByAgentId: actor.agentId ?? null,
lastModel: input.model ?? null,
updatedAt: now,
};
const [nextSlot] = await tx
.update(summarySlots)
.set(slotPatch)
.where(
and(
eq(summarySlots.id, currentSlot.id),
eq(summarySlots.generatingIssueId, input.generationIssueId!),
),
)
.returning();
if (!nextSlot) {
throw conflict("Summary generation was superseded by a newer task");
}
return { slot: nextSlot, document: documentRow, revision: revisionRow };
});
return {
slot: mapSlot(result.slot),
document: mapDocument(result.document),
revision: mapRevision(result.revision),
};
}
return {
getSlot,
listRevisions,
generate,
write,
};
}