mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-22 00:30:19 +02:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Agent Chat turns discussion into plans, tasks, reviews, and hires. > - These workflows need reliable tool results and task context on the native runner. > - Live Claude and Codex tests exposed lost retry requests, invalid project inputs, and a child startup crash. > - Recovery also exposed a misleading retry action and missing child task context. > - This pull request fixes those paths and adds regression coverage. > - Agents can continue the original request and operators can inspect a stopped run. ## Linked Issues or Issue Description **What happened?** A failed Agent Chat retry could lose the user's question. Project creation accepted unsupported icons in its tool schema. Codex could stop when a helper's MCP startup event arrived before its thread lineage. A stopped task offered Retry even when the server required execution reconciliation. Resumed agents could miss existing delegated tasks. Hiring and review instructions did not describe the native runner's available tools and source requirements. **Expected behavior** Retries retain the selected request. Tool schemas match the API. Child startup information does not gain authority over the parent or stop it. Recovery actions match the server's requirements. Task context exposes existing child work. Handoffs contain the material the assignee needs. **Steps to reproduce** 1. Enable experimental Agent Chat in an isolated development instance. 2. Configure native Codex and ACPX Claude agents on Paperclip Runner. 3. Ask for a plan, revise it, approve task creation, and request a hire and status report. 4. Retry a failed chat turn and check that it answers the original request. 5. Start a Codex helper before its thread lineage arrives. 6. Resume a delegated task and inspect its existing children and saved output. **Paperclip version or commit** The live failures were found at `f2c5e54dc`. This branch is rebased onto `86b7ee992`. **Deployment mode** Isolated local development instance with native Codex and ACPX Claude. No database migration or default permission change. Related work: Refs #13284 for Agent Chat. Refs #13438 for the server-side API receipt fix, which this branch preserves. The transport also accepts the earlier HTTP receipt format. Refs #13655 for the current Codex continuation and helper lineage handling, which this branch also preserves. ## What Changed - Preserve failed Agent Chat wake-comment IDs and session generation from the authorized source run. Reject pre-reset retries. - Wrap API receipts with the correct semantic call identity. Test current and earlier receipt formats through real HTTP and runnerd. - Classify early child MCP startup notifications as information. Keep foreign completion and result events rejected. - Constrain project icons on both tool surfaces and regenerate the protocol contracts. - Include bounded, company-scoped visible direct child tasks in task context. Filter hidden tasks before applying the limit. - Replace the rejected Retry action with Inspect run for native continuation reconciliation. - Update hiring, review handoff, status reporting, and development guidance. ## Verification - Live tests covered Claude and Codex questions, plan revisions, approval, task creation, hiring, status, chat reset, failures, and recovery. - The recovered task produced its saved checklist and example. A later follow-up read the existing child tasks and document without creating more work. - Full build, repository type checks, token gates, 142 focused tests, 188 runner TypeScript tests, and the Rust notification/descendant regressions passed after rebase. The separate local full-suite run was stopped after the complete CI suite passed. - Review fixes passed the updated route, tool-authority, and icon regression tests plus server type checking. - Required commands: `pnpm build`, `pnpm -r typecheck`, `PAPERCLIP_IN_WORKTREE=false pnpm test:run`, and `pnpm check:token-gates`. - At `4ce8047b0`, all 55 applicable GitHub checks pass (two Storybook checks are intentionally skipped), including the complete general/serialized test matrix, runner tests, browser tests, build, type checks, Docker checks, and canary dry run. - Fresh Greptile review is 5/5 on `4ce8047b0`; all three findings were fixed with regressions and there are no unresolved review threads. - Two initial CI service-startup timeouts passed unchanged in local reproductions and in the latest CI run. ## Risks - The new event classification is limited to MCP startup information. It does not authorize foreign task completion, results, or tool requests. - Task context returns at most 100 direct child tasks and reports truncation. It excludes hidden tasks and other companies. This improves delegation context but does not enforce semantic duplicate detection. - Native reconciliation still requires an operator to inspect and record prior outcomes. The new link does not replace the recovery API. - API tools remain opt-in. Claude permission choices remain explicit. No default permission, schema, or workflow changes. ## Model Used OpenAI GPT-6 in Codex, with reasoning, repository editing, code execution, API tools, and browser testing. The exact deployment identifier and context-window size are not exposed in this session. Live acceptance agents used OpenAI `gpt-5.6-sol` and Anthropic `claude-sonnet-4-6`. ## 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 either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
550 lines
24 KiB
TypeScript
550 lines
24 KiB
TypeScript
import {
|
|
persistActivity,
|
|
publishActivity,
|
|
type ActivityPublication,
|
|
} from "./activity-log.js";
|
|
import type { NativeStatusDecision } from "./native-runtime/status-arbiter.js";
|
|
import { and, desc, eq, isNull, sql } from "drizzle-orm";
|
|
import {
|
|
agentTaskSessions,
|
|
agentWakeupRequests,
|
|
heartbeatRuns,
|
|
issueComments,
|
|
issueTreeHolds,
|
|
issueThreadInteractions,
|
|
issues,
|
|
type Db,
|
|
} from "@paperclipai/db";
|
|
|
|
import { sanitizeQuarantinedCommentForHigherTrust } from "./source-trust.js";
|
|
|
|
export type ConversationIdentity = {
|
|
conversationAgentId?: string | null;
|
|
conversationUserId?: string | null;
|
|
conversationState?: string | null;
|
|
status?: string;
|
|
};
|
|
export function isConversation(
|
|
issue: ConversationIdentity | null | undefined,
|
|
): boolean {
|
|
return Boolean(issue?.conversationAgentId && issue.conversationUserId);
|
|
}
|
|
export function isWaitingConversation(
|
|
issue: ConversationIdentity | null | undefined,
|
|
): boolean {
|
|
return (
|
|
isConversation(issue) &&
|
|
issue?.conversationState === "waiting" &&
|
|
issue.status === "in_review"
|
|
);
|
|
}
|
|
|
|
/** Recovery for an older turn must not replace a reset or an answered chat. */
|
|
export function isSupersededConversationRun(
|
|
issue: ConversationIdentity & {
|
|
conversationSessionGeneration?: number;
|
|
executionRunId?: string | null;
|
|
},
|
|
run: { id: string; contextSnapshot: Record<string, unknown> | null },
|
|
): boolean {
|
|
if (!isConversation(issue)) return false;
|
|
const generation = run.contextSnapshot?.conversationSessionGeneration;
|
|
return (
|
|
(typeof generation === "number" &&
|
|
typeof issue.conversationSessionGeneration === "number" &&
|
|
generation !== issue.conversationSessionGeneration) ||
|
|
(typeof generation === "number" &&
|
|
isWaitingConversation(issue) &&
|
|
issue.executionRunId !== run.id)
|
|
);
|
|
}
|
|
/** Execution tasks may link to a conversation, but never drive its turns.
|
|
* Apply before enqueue, including while a reply is still running: waiting until
|
|
* finalization is too late to prevent a deferred dependency follow-up.
|
|
*/
|
|
export function isConversationExecutionWake(
|
|
issue: ConversationIdentity | null | undefined,
|
|
reason: string | null | undefined,
|
|
): boolean {
|
|
return isConversation(issue) && (
|
|
reason === "issue_blockers_resolved" ||
|
|
reason === "issue_children_completed" ||
|
|
reason === "issue_unblock_requested"
|
|
);
|
|
}
|
|
|
|
export function isConversationReset(body: string): boolean {
|
|
return body.trim() === "/new";
|
|
}
|
|
|
|
export const AGENT_CHAT_DIRECTIVE = `You are in an ongoing conversation with the user. Help them clarify the outcome they want. Ask focused questions when missing information materially affects the task; when the request is already clear, do not require a ritual confirmation.
|
|
|
|
Research, clarify, and develop full plans here using the conversation's plan document. Revise the draft as the discussion develops. Planning alone does not create execution tasks. Put implementation and substantial execution into separate tasks.
|
|
|
|
When the user asks to approve a plan before handoff, publish the plan and create a revision-bound approval card before ending the turn. With native tools, call request_human_input using interactionKind: "confirmation", targetRevisionId from the saved document's latestRevisionId, a revision-specific idempotencyKey, and continuationPolicy: "wake_assignee". Set payload.target to { type: "issue_document", key: "plan", revisionId: latestRevisionId }. Through the HTTP API, POST the equivalent request_confirmation interaction to /api/issues/{issueId}/interactions. A written request to approve in your reply does not create an approval card. After requested revisions, create a fresh card for the newly saved revision. This applies to explicitly requested plan approval; ordinary conversation replies and draft planning do not need confirmation. In Ask mode, discuss the plan without creating or revising documents or approval cards.
|
|
|
|
Before handing off work, inspect available projects and repositories. Every task you create from this chat must belong to a suitable project. Reuse an appropriate existing project; otherwise use create_project. Consider all relevant available repositories and pass repositoryIds for one or multiple repositories when the work spans them. For existing GitHub repositories you can access that are absent from the catalog, pass their HTTPS repositoryUrls; this registers them with the project without creating remote GitHub repositories. You may combine known IDs and URLs and attach multiple repositories. The direct HTTP equivalent is POST /api/companies/{companyId}/projects with name, repositoryIds and/or repositoryUrls arrays, and an idempotencyKey. Include all selected repositories in that creation; do not combine these arrays with workspace. Never invent repository IDs or substitute inaccessible repositories. Ask when the choice is materially ambiguous or required access is missing. Non-code projects may need no repository.
|
|
|
|
Create ordinary assigned tasks, never subtasks of this conversation. Give each task a clear outcome, context, acceptance criteria, project, and appropriate assignee. Use create_task with initialPlan to copy the relevant plan into the new task before execution starts. If using the HTTP API directly, POST /api/companies/{companyId}/issues with projectId, assigneeAgentId, status: "todo", initialPlan containing the relevant plan Markdown, and an idempotencyKey; omit parentId. Putting a plan in description does not create the task's plan document. Verify the new task's plan document before claiming the handoff is complete. Preserve the original plan here. Carry forward only the remaining execution steps, not completed planning, approval, project creation, or task creation steps. Include the source conversation ID, approved plan revision ID, and accepted interaction ID so the worker can verify the recorded approval. State the approved scope and what is already done; do not claim the new plan document has its own approval. The worker should execute that authorized scope, and ask again only if scope changes or another applicable gate requires it. When splitting work, include the relevant part of the plan in each task. Create and link each task before claiming it exists.
|
|
|
|
For review or follow-up tasks, include the existing source materials the assignee needs, not just task identifiers or a rubric. Read the source task's saved documents and comments, then copy the relevant existing content into the handoff or attach resources the recipient can actually read. A new task's tools may only expose its own documents and history. Supplying existing source material is coordination, not creating the assigned deliverable.
|
|
|
|
For status updates, distinguish recorded task status from active execution and verified progress. Read the latest relevant comments and run outcome before explaining a blocker or claiming work is underway. Use the advertised API tools for another task's details when needed. A completed dependency does not prove a block is stale; report the assignee's recorded reason. Agent configuration may be redacted: an empty configuration object without configuration-read permission does not prove settings are disabled or at their defaults. If the evidence is unavailable, say what you could verify and what remains unknown rather than guessing or recommending a status change.
|
|
|
|
Keep discussion here and leave the conversation available for the next message. Link handed-off tasks in your reply; do not make this conversation blocked by their completion or wait for them. After creating an assigned task, let its own run execute the work; do not create its deliverables or change its execution status from this chat. Reply normally and end your turn; Paperclip manages the conversation waiting state. Do not change its status, create a review confirmation just to finish a reply, mark it complete, or poll for another reply. An accepted plan authorizes handoff to execution tasks, never implementation on this conversation. Honor normal approvals. Ask mode is non-mutating. Plan mode supports research and writing/revising the plan; hand off for execution only through the normal authorized workflow.`;
|
|
|
|
/** A reset keeps history visible, but parked input from a stopped session cannot become a new turn. */
|
|
export function currentConversationCommentCondition() {
|
|
return sql`not exists (
|
|
select 1 from ${issues} conversation_issue
|
|
join ${issueComments} conversation_boundary
|
|
on conversation_boundary.id = conversation_issue.conversation_boundary_comment_id
|
|
and conversation_boundary.company_id = conversation_issue.company_id
|
|
and conversation_boundary.issue_id = conversation_issue.id
|
|
where conversation_issue.id = ${issueComments.issueId}
|
|
and conversation_issue.company_id = ${issueComments.companyId}
|
|
and conversation_issue.conversation_agent_id is not null
|
|
and (${issueComments.createdAt}, ${issueComments.id}) < (conversation_boundary.created_at, conversation_boundary.id)
|
|
)`;
|
|
}
|
|
|
|
/** Runs under the normal issue execution lock, before any provider session is read. */
|
|
export async function prepareConversationTurn(
|
|
db: Db,
|
|
run: typeof heartbeatRuns.$inferSelect,
|
|
) {
|
|
const context = { ...(run.contextSnapshot ?? {}) };
|
|
const issueId = typeof context.issueId === "string" ? context.issueId : null;
|
|
if (!issueId) return { context, reset: false, conversation: false };
|
|
let publication: ActivityPublication | null = null;
|
|
const result = await db.transaction(async (tx) => {
|
|
const [issue] = await tx
|
|
.select()
|
|
.from(issues)
|
|
.where(and(eq(issues.id, issueId), eq(issues.companyId, run.companyId)))
|
|
.for("update");
|
|
if (!isConversation(issue))
|
|
return { context, reset: false, conversation: false };
|
|
const commentId =
|
|
typeof context.wakeCommentId === "string"
|
|
? context.wakeCommentId
|
|
: typeof context.commentId === "string"
|
|
? context.commentId
|
|
: null;
|
|
const [comment] = commentId
|
|
? await tx
|
|
.select()
|
|
.from(issueComments)
|
|
.where(
|
|
and(
|
|
eq(issueComments.id, commentId),
|
|
eq(issueComments.issueId, issueId),
|
|
eq(issueComments.companyId, run.companyId),
|
|
),
|
|
)
|
|
: [];
|
|
const reset = Boolean(
|
|
comment && comment.authorUserId && isConversationReset(comment.body),
|
|
);
|
|
let generation = issue.conversationSessionGeneration;
|
|
if (
|
|
typeof context.conversationSessionGeneration === "number" &&
|
|
context.conversationSessionGeneration !== generation
|
|
) {
|
|
throw new Error(
|
|
"Conversation session changed; this older turn cannot resume",
|
|
);
|
|
}
|
|
// The boundary lives on the command comment. A crash/retry reuses it instead of resetting twice.
|
|
if (reset && comment && comment.conversationSessionGeneration == null) {
|
|
generation += 1;
|
|
await tx
|
|
.update(issues)
|
|
.set({
|
|
conversationSessionGeneration: generation,
|
|
conversationBoundaryCommentId: comment.id,
|
|
updatedAt: new Date(),
|
|
})
|
|
.where(eq(issues.id, issue.id));
|
|
await tx
|
|
.update(issueComments)
|
|
.set({ conversationSessionGeneration: generation })
|
|
.where(eq(issueComments.id, comment.id));
|
|
// Questions from the previous session must not keep occupying the
|
|
// composer or wake the old topic, even if they survive normal comments.
|
|
const expiredQuestions = await tx.update(issueThreadInteractions).set({
|
|
status: "expired", resolvedAt: new Date(), updatedAt: new Date(),
|
|
resolvedByUserId: comment.authorUserId,
|
|
result: { version: 1, outcome: "withdrawn", reason: "New conversation session", answers: [], summaryMarkdown: null },
|
|
}).where(and(eq(issueThreadInteractions.companyId, issue.companyId),
|
|
eq(issueThreadInteractions.issueId, issue.id), eq(issueThreadInteractions.status, "pending"),
|
|
eq(issueThreadInteractions.kind, "ask_user_questions"))).returning({ id: issueThreadInteractions.id });
|
|
publication = (
|
|
await persistActivity(tx as unknown as Db, {
|
|
companyId: issue.companyId,
|
|
actorType: "system",
|
|
actorId: "conversation",
|
|
action: "issue.conversation_session_started",
|
|
entityType: "issue",
|
|
entityId: issue.id,
|
|
runId: run.id,
|
|
details: { generation, boundaryCommentId: comment.id, expiredInteractionIds: expiredQuestions.map((row) => row.id) },
|
|
})
|
|
).publication;
|
|
// Deliberately do not touch agentRuntimeState or sessions belonging to other tasks.
|
|
await tx
|
|
.delete(agentTaskSessions)
|
|
.where(
|
|
and(
|
|
eq(agentTaskSessions.companyId, issue.companyId),
|
|
eq(agentTaskSessions.agentId, issue.conversationAgentId!),
|
|
eq(agentTaskSessions.taskKey, issue.id),
|
|
),
|
|
);
|
|
}
|
|
await tx
|
|
.update(issues)
|
|
.set({
|
|
conversationState: "active",
|
|
status: "in_progress",
|
|
updatedAt: new Date(),
|
|
})
|
|
.where(eq(issues.id, issue.id));
|
|
const next = {
|
|
...context,
|
|
conversationSessionGeneration: generation,
|
|
conversationMode: true,
|
|
};
|
|
await tx
|
|
.update(heartbeatRuns)
|
|
.set({ contextSnapshot: next })
|
|
.where(eq(heartbeatRuns.id, run.id));
|
|
return { context: next, reset, conversation: true };
|
|
});
|
|
if (publication) publishActivity(publication);
|
|
return result;
|
|
}
|
|
|
|
/** Finalizers only park a turn with a durable response, interaction, or processed /new. */
|
|
export async function settleConversationTurn(
|
|
db: Db,
|
|
run: typeof heartbeatRuns.$inferSelect,
|
|
) {
|
|
if (run.status !== "succeeded") return false;
|
|
const context = run.contextSnapshot ?? {};
|
|
const issueId = typeof context.issueId === "string" ? context.issueId : null;
|
|
if (!issueId) return false;
|
|
let publication: ActivityPublication | null = null;
|
|
const settled = await db.transaction(async (tx) => {
|
|
const [issue] = await tx
|
|
.select()
|
|
.from(issues)
|
|
.where(and(eq(issues.id, issueId), eq(issues.companyId, run.companyId)))
|
|
.for("update");
|
|
if (
|
|
!isConversation(issue) ||
|
|
(issue.executionRunId && issue.executionRunId !== run.id)
|
|
)
|
|
return false;
|
|
const [response] = await tx
|
|
.select({ id: issueComments.id })
|
|
.from(issueComments)
|
|
.where(
|
|
and(
|
|
eq(issueComments.issueId, issueId),
|
|
eq(issueComments.createdByRunId, run.id),
|
|
eq(issueComments.authorAgentId, issue.conversationAgentId!),
|
|
isNull(issueComments.deletedAt),
|
|
),
|
|
)
|
|
.limit(1);
|
|
// Native question/plan waits use the durable interaction as the reply;
|
|
// their terminal prose is deliberately not materialized as a comment.
|
|
const [interaction] = !response && context.conversationReset !== true
|
|
? await tx.select({ id: issueThreadInteractions.id })
|
|
.from(issueThreadInteractions)
|
|
.where(and(
|
|
eq(issueThreadInteractions.companyId, run.companyId),
|
|
eq(issueThreadInteractions.issueId, issueId),
|
|
eq(issueThreadInteractions.sourceRunId, run.id),
|
|
eq(issueThreadInteractions.createdByAgentId, issue.conversationAgentId!),
|
|
eq(issueThreadInteractions.status, "pending"),
|
|
))
|
|
.limit(1)
|
|
: [];
|
|
if (!response && !interaction && context.conversationReset !== true) return false;
|
|
if (
|
|
context.conversationSessionGeneration !==
|
|
issue.conversationSessionGeneration
|
|
)
|
|
return false;
|
|
// Messages arriving during the reply remain actionable, including the
|
|
// crash window between their comment commit and wake enqueue.
|
|
const wakeId =
|
|
typeof context.wakeCommentId === "string"
|
|
? context.wakeCommentId
|
|
: context.commentId;
|
|
const [wake] =
|
|
typeof wakeId === "string"
|
|
? await tx
|
|
.select()
|
|
.from(issueComments)
|
|
.where(eq(issueComments.id, wakeId))
|
|
: [];
|
|
const [pending] = wake
|
|
? await tx
|
|
.select({ id: issueComments.id })
|
|
.from(issueComments)
|
|
.where(
|
|
and(
|
|
eq(issueComments.issueId, issueId),
|
|
isNull(issueComments.deletedAt),
|
|
sql`${issueComments.authorUserId} is not null`,
|
|
sql`(${issueComments.createdAt}, ${issueComments.id}) > (select cursor.created_at, cursor.id from issue_comments cursor where cursor.id = ${wake.id}::uuid)`,
|
|
),
|
|
)
|
|
.limit(1)
|
|
: [];
|
|
const status = pending ? "in_progress" : "in_review";
|
|
const conversationState = pending ? "active" : "waiting";
|
|
if (
|
|
issue.status === status &&
|
|
issue.conversationState === conversationState
|
|
)
|
|
return true;
|
|
await tx
|
|
.update(issues)
|
|
.set({
|
|
status,
|
|
conversationState,
|
|
statusVersion: sql`${issues.statusVersion} + 1`,
|
|
completedAt: null,
|
|
cancelledAt: null,
|
|
updatedAt: new Date(),
|
|
})
|
|
.where(
|
|
and(
|
|
eq(issues.id, issueId),
|
|
sql`(${issues.executionRunId} is null or ${issues.executionRunId} = ${run.id})`,
|
|
),
|
|
);
|
|
publication = (
|
|
await persistActivity(tx as unknown as Db, {
|
|
companyId: issue.companyId,
|
|
actorType: "system",
|
|
actorId: "conversation",
|
|
action: "issue.updated",
|
|
entityType: "issue",
|
|
entityId: issue.id,
|
|
runId: run.id,
|
|
details: {
|
|
status,
|
|
conversationState,
|
|
conversationSessionGeneration: issue.conversationSessionGeneration,
|
|
},
|
|
})
|
|
).publication;
|
|
return true;
|
|
});
|
|
if (publication) publishActivity(publication);
|
|
return settled;
|
|
}
|
|
|
|
/** Fresh provider context uses only messages in this session, up to this turn. */
|
|
export async function conversationReplay(
|
|
db: Db,
|
|
companyId: string,
|
|
issueId: string,
|
|
wakeCommentId: string | null,
|
|
) {
|
|
const [issue] = await db
|
|
.select()
|
|
.from(issues)
|
|
.where(and(eq(issues.id, issueId), eq(issues.companyId, companyId)));
|
|
if (!isConversation(issue)) return "";
|
|
const [boundary] = issue.conversationBoundaryCommentId
|
|
? await db
|
|
.select()
|
|
.from(issueComments)
|
|
.where(eq(issueComments.id, issue.conversationBoundaryCommentId))
|
|
: [];
|
|
const [wake] = wakeCommentId
|
|
? await db
|
|
.select()
|
|
.from(issueComments)
|
|
.where(
|
|
and(
|
|
eq(issueComments.id, wakeCommentId),
|
|
eq(issueComments.issueId, issueId),
|
|
),
|
|
)
|
|
: [];
|
|
const rows = await db
|
|
.select()
|
|
.from(issueComments)
|
|
.where(
|
|
and(
|
|
eq(issueComments.companyId, companyId),
|
|
eq(issueComments.issueId, issueId),
|
|
isNull(issueComments.deletedAt),
|
|
boundary
|
|
? sql`(${issueComments.createdAt}, ${issueComments.id}) > (select cursor.created_at, cursor.id from issue_comments cursor where cursor.id = ${boundary.id}::uuid)`
|
|
: undefined,
|
|
wake
|
|
? sql`(${issueComments.createdAt}, ${issueComments.id}) < (select cursor.created_at, cursor.id from issue_comments cursor where cursor.id = ${wake.id}::uuid)`
|
|
: undefined,
|
|
),
|
|
)
|
|
.orderBy(desc(issueComments.createdAt), desc(issueComments.id))
|
|
.limit(40);
|
|
return rows
|
|
.reverse()
|
|
.map((row) =>
|
|
JSON.stringify({
|
|
author: row.authorAgentId ? "agent" : "user",
|
|
body: sanitizeQuarantinedCommentForHigherTrust(row).body.slice(0, 8000),
|
|
}),
|
|
)
|
|
.join("\n");
|
|
}
|
|
|
|
/** Comment rows form a durable outbox for the narrow commit-to-enqueue crash window. */
|
|
export async function undeliveredConversationComments(
|
|
db: Db,
|
|
companyId: string,
|
|
issueId: string,
|
|
) {
|
|
return db
|
|
.select()
|
|
.from(issueComments)
|
|
.where(
|
|
and(
|
|
eq(issueComments.companyId, companyId),
|
|
eq(issueComments.issueId, issueId),
|
|
isNull(issueComments.deletedAt),
|
|
sql`${issueComments.clientRequestId} is not null`,
|
|
sql`not exists (select 1 from ${agentWakeupRequests} where ${agentWakeupRequests.companyId} = ${companyId}
|
|
and ${agentWakeupRequests.idempotencyKey} = 'conversation-comment:' || ${issueComments.id}::text)`,
|
|
),
|
|
)
|
|
.orderBy(issueComments.createdAt, issueComments.id)
|
|
.limit(100);
|
|
}
|
|
|
|
/** A user's /new resumes this chat without replaying the stopped turn. */
|
|
export async function resumeConversationForReset(db: Db, comment: typeof issueComments.$inferSelect) {
|
|
if (!comment.authorUserId || !isConversationReset(comment.body)) return;
|
|
const publications: ActivityPublication[] = [];
|
|
await db.transaction(async (tx) => {
|
|
const [issue] = await tx.select().from(issues).where(and(
|
|
eq(issues.id, comment.issueId), eq(issues.companyId, comment.companyId),
|
|
)).for("update");
|
|
if (!isConversation(issue) || comment.conversationSessionGeneration != null) return;
|
|
const released = await tx.update(issueTreeHolds).set({
|
|
status: "released", releasedAt: new Date(), updatedAt: new Date(),
|
|
releasedByActorType: "user", releasedByUserId: comment.authorUserId,
|
|
releaseReason: "Resumed by /new", releaseMetadata: { commentId: comment.id, wakeAgents: false },
|
|
}).where(and(eq(issueTreeHolds.companyId, issue.companyId),
|
|
eq(issueTreeHolds.rootIssueId, issue.id), eq(issueTreeHolds.mode, "pause"),
|
|
eq(issueTreeHolds.status, "active"))).returning();
|
|
for (const hold of released) {
|
|
publications.push((await persistActivity(tx as unknown as Db, {
|
|
companyId: issue.companyId, actorType: "user", actorId: comment.authorUserId!,
|
|
action: "issue.tree_hold_released", entityType: "issue", entityId: issue.id,
|
|
details: { holdId: hold.id, mode: "pause", reason: "Resumed by /new", commentId: comment.id },
|
|
})).publication);
|
|
}
|
|
});
|
|
for (const publication of publications) publishActivity(publication);
|
|
}
|
|
|
|
/** Serialize durable outbox delivery across API servers; the normal wake queue owns execution. */
|
|
export async function deliverConversationComments(
|
|
db: Db,
|
|
issue: { id: string; companyId: string; conversationAgentId: string | null },
|
|
enqueue: (
|
|
agentId: string,
|
|
options: {
|
|
source: "on_demand";
|
|
triggerDetail: "manual";
|
|
reason: string;
|
|
idempotencyKey: string;
|
|
requestedByActorType: "user";
|
|
requestedByActorId: string | null;
|
|
payload: Record<string, unknown>;
|
|
contextSnapshot: Record<string, unknown>;
|
|
},
|
|
) => Promise<unknown>,
|
|
) {
|
|
if (!issue.conversationAgentId) return;
|
|
for (;;) {
|
|
const delivered = await db.transaction(async (tx) => {
|
|
// Contenders release their connection while waiting so concurrent sends
|
|
// cannot exhaust the pool needed by normal wake admission.
|
|
const locks = await tx.execute(
|
|
sql`select pg_try_advisory_xact_lock(hashtextextended(${"conversation-delivery:" + issue.id}, 0)) as acquired`,
|
|
);
|
|
if (!locks[0]?.acquired) return false;
|
|
for (const comment of await undeliveredConversationComments(
|
|
tx as unknown as Db,
|
|
issue.companyId,
|
|
issue.id,
|
|
)) {
|
|
await resumeConversationForReset(db, comment);
|
|
await enqueue(issue.conversationAgentId!, {
|
|
source: "on_demand",
|
|
triggerDetail: "manual",
|
|
reason: "issue_commented",
|
|
idempotencyKey: `conversation-comment:${comment.id}`,
|
|
requestedByActorType: "user",
|
|
requestedByActorId: comment.authorUserId,
|
|
payload: { issueId: issue.id, commentId: comment.id },
|
|
contextSnapshot: {
|
|
issueId: issue.id,
|
|
taskKey: issue.id,
|
|
commentId: comment.id,
|
|
wakeCommentId: comment.id,
|
|
wakeCommentIds: [comment.id],
|
|
source: "issue.comment",
|
|
},
|
|
});
|
|
}
|
|
return true;
|
|
});
|
|
if (delivered) return;
|
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
}
|
|
}
|
|
|
|
/** Conversation turns do not need execution-task completion evidence or a continuation. */
|
|
export function conversationNativeDecision(input: {
|
|
conversation: boolean;
|
|
terminalState: unknown;
|
|
workspaceFinalizeStatus: string;
|
|
hasGovernanceGate: boolean;
|
|
priorStatus: NativeStatusDecision["toStatus"];
|
|
decision: NativeStatusDecision;
|
|
}): NativeStatusDecision {
|
|
if (
|
|
!input.conversation ||
|
|
input.terminalState !== "succeeded" ||
|
|
input.workspaceFinalizeStatus !== "succeeded" ||
|
|
input.hasGovernanceGate ||
|
|
input.decision.statusAction === "blocked" ||
|
|
input.decision.effects.some(
|
|
(effect) =>
|
|
effect.kind === "schedule_retry" ||
|
|
effect.kind === "record_finalization_error",
|
|
)
|
|
)
|
|
return input.decision;
|
|
return {
|
|
...input.decision,
|
|
statusAction: "preserve",
|
|
toStatus: input.priorStatus,
|
|
reasonCode: "conversation_turn_finished",
|
|
unblockDescriptor: null,
|
|
effects: [],
|
|
};
|
|
}
|