mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-15 10:30:37 +09:00
[codex] Improve agent runtime recovery and governance (#4086)
## Thinking Path > - Paperclip orchestrates AI agents for zero-human companies. > - The heartbeat runtime, agent import path, and agent configuration defaults determine whether work is dispatched safely and predictably. > - Several accumulated fixes all touched agent execution recovery, wake routing, import behavior, and runtime concurrency defaults. > - Those changes need to land together so the heartbeat service and agent creation defaults stay internally consistent. > - This pull request groups the runtime/governance changes from the split branch into one standalone branch. > - The benefit is safer recovery for stranded runs, bounded high-volume reads, imported-agent approval correctness, skill-template support, and a clearer default concurrency policy. ## What Changed - Fixed stranded continuation recovery so successful automatic retries are requeued instead of incorrectly blocking the issue. - Bounded high-volume issue/log reads across issue, heartbeat, agent, project, and workspace paths. - Fixed imported-agent approval and instruction-path permission handling. - Quarantined seeded worktree execution state during worktree provisioning. - Queued approval follow-up wakes and hardened SQL_ASCII heartbeat output handling. - Added reusable agent instruction templates for hiring flows. - Set the default max concurrent agent runs to five and updated related UI/tests/docs. ## Verification - `pnpm install --frozen-lockfile` - `pnpm exec vitest run server/src/__tests__/company-portability.test.ts server/src/__tests__/heartbeat-process-recovery.test.ts server/src/__tests__/heartbeat-comment-wake-batching.test.ts server/src/__tests__/heartbeat-list.test.ts server/src/__tests__/issues-service.test.ts server/src/__tests__/agent-permissions-routes.test.ts packages/adapter-utils/src/server-utils.test.ts ui/src/lib/new-agent-runtime-config.test.ts` - Split integration check: merged this branch first, followed by the other [PAP-1614](/PAP/issues/PAP-1614) branches, with no merge conflicts. - Confirmed this branch does not include `pnpm-lock.yaml`. ## Risks - Medium risk: touches heartbeat recovery, queueing, and issue list bounds in central runtime paths. - Imported-agent and concurrency default behavior changes may affect existing automation that assumes one-at-a-time default runs. - No database migrations are included. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI Codex, GPT-5.4 tool-enabled coding model, agentic code-editing/runtime with local shell and GitHub CLI access; exact context window and reasoning mode are not exposed by the Paperclip harness. ## 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 run tests locally and they pass - [x] I have added or updated tests where applicable - [x] If this change affects the UI, I have included before/after screenshots - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
parent
057fee4836
commit
16b2b84d84
38 changed files with 1569 additions and 240 deletions
|
|
@ -38,6 +38,9 @@ import { getDefaultCompanyGoal } from "./goals.js";
|
|||
|
||||
const ALL_ISSUE_STATUSES = ["backlog", "todo", "in_progress", "in_review", "blocked", "done", "cancelled"];
|
||||
const MAX_ISSUE_COMMENT_PAGE_LIMIT = 500;
|
||||
export const ISSUE_LIST_DEFAULT_LIMIT = 500;
|
||||
export const ISSUE_LIST_MAX_LIMIT = 1000;
|
||||
const ISSUE_LIST_RELATED_QUERY_CHUNK_SIZE = 500;
|
||||
export const MAX_CHILD_ISSUES_CREATED_BY_HELPER = 25;
|
||||
const MAX_CHILD_COMPLETION_SUMMARIES = 20;
|
||||
const CHILD_COMPLETION_SUMMARY_BODY_MAX_CHARS = 500;
|
||||
|
|
@ -106,6 +109,10 @@ type IssueUserCommentStats = {
|
|||
myLastCommentAt: Date | null;
|
||||
lastExternalCommentAt: Date | null;
|
||||
};
|
||||
type IssueReadStat = {
|
||||
issueId: string;
|
||||
myLastReadAt: Date | null;
|
||||
};
|
||||
type IssueLastActivityStat = {
|
||||
issueId: string;
|
||||
latestCommentAt: Date | null;
|
||||
|
|
@ -158,6 +165,18 @@ function escapeLikePattern(value: string): string {
|
|||
return value.replace(/[\\%_]/g, "\\$&");
|
||||
}
|
||||
|
||||
export function clampIssueListLimit(limit: number): number {
|
||||
return Math.min(ISSUE_LIST_MAX_LIMIT, Math.max(1, Math.floor(limit)));
|
||||
}
|
||||
|
||||
function chunkList<T>(values: T[], size: number): T[][] {
|
||||
const chunks: T[][] = [];
|
||||
for (let index = 0; index < values.length; index += size) {
|
||||
chunks.push(values.slice(index, index + size));
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
|
||||
function truncateInlineSummary(value: string | null | undefined, maxChars = CHILD_COMPLETION_SUMMARY_BODY_MAX_CHARS) {
|
||||
const normalized = value?.trim();
|
||||
if (!normalized) return null;
|
||||
|
|
@ -494,20 +513,22 @@ function latestIssueActivityAt(...values: Array<Date | string | null | undefined
|
|||
async function labelMapForIssues(dbOrTx: any, issueIds: string[]): Promise<Map<string, IssueLabelRow[]>> {
|
||||
const map = new Map<string, IssueLabelRow[]>();
|
||||
if (issueIds.length === 0) return map;
|
||||
const rows = await dbOrTx
|
||||
.select({
|
||||
issueId: issueLabels.issueId,
|
||||
label: labels,
|
||||
})
|
||||
.from(issueLabels)
|
||||
.innerJoin(labels, eq(issueLabels.labelId, labels.id))
|
||||
.where(inArray(issueLabels.issueId, issueIds))
|
||||
.orderBy(asc(labels.name), asc(labels.id));
|
||||
for (const issueIdChunk of chunkList(issueIds, ISSUE_LIST_RELATED_QUERY_CHUNK_SIZE)) {
|
||||
const rows = await dbOrTx
|
||||
.select({
|
||||
issueId: issueLabels.issueId,
|
||||
label: labels,
|
||||
})
|
||||
.from(issueLabels)
|
||||
.innerJoin(labels, eq(issueLabels.labelId, labels.id))
|
||||
.where(inArray(issueLabels.issueId, issueIdChunk))
|
||||
.orderBy(asc(labels.name), asc(labels.id));
|
||||
|
||||
for (const row of rows) {
|
||||
const existing = map.get(row.issueId);
|
||||
if (existing) existing.push(row.label);
|
||||
else map.set(row.issueId, [row.label]);
|
||||
for (const row of rows) {
|
||||
const existing = map.get(row.issueId);
|
||||
if (existing) existing.push(row.label);
|
||||
else map.set(row.issueId, [row.label]);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
|
@ -537,27 +558,29 @@ async function activeRunMapForIssues(
|
|||
.filter((id): id is string => id != null);
|
||||
if (runIds.length === 0) return map;
|
||||
|
||||
const rows = await dbOrTx
|
||||
.select({
|
||||
id: heartbeatRuns.id,
|
||||
status: heartbeatRuns.status,
|
||||
agentId: heartbeatRuns.agentId,
|
||||
invocationSource: heartbeatRuns.invocationSource,
|
||||
triggerDetail: heartbeatRuns.triggerDetail,
|
||||
startedAt: heartbeatRuns.startedAt,
|
||||
finishedAt: heartbeatRuns.finishedAt,
|
||||
createdAt: heartbeatRuns.createdAt,
|
||||
})
|
||||
.from(heartbeatRuns)
|
||||
.where(
|
||||
and(
|
||||
inArray(heartbeatRuns.id, runIds),
|
||||
inArray(heartbeatRuns.status, ACTIVE_RUN_STATUSES),
|
||||
),
|
||||
);
|
||||
for (const runIdChunk of chunkList([...new Set(runIds)], ISSUE_LIST_RELATED_QUERY_CHUNK_SIZE)) {
|
||||
const rows = await dbOrTx
|
||||
.select({
|
||||
id: heartbeatRuns.id,
|
||||
status: heartbeatRuns.status,
|
||||
agentId: heartbeatRuns.agentId,
|
||||
invocationSource: heartbeatRuns.invocationSource,
|
||||
triggerDetail: heartbeatRuns.triggerDetail,
|
||||
startedAt: heartbeatRuns.startedAt,
|
||||
finishedAt: heartbeatRuns.finishedAt,
|
||||
createdAt: heartbeatRuns.createdAt,
|
||||
})
|
||||
.from(heartbeatRuns)
|
||||
.where(
|
||||
and(
|
||||
inArray(heartbeatRuns.id, runIdChunk),
|
||||
inArray(heartbeatRuns.status, ACTIVE_RUN_STATUSES),
|
||||
),
|
||||
);
|
||||
|
||||
for (const row of rows) {
|
||||
map.set(row.id, row);
|
||||
for (const row of rows) {
|
||||
map.set(row.id, row);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
|
@ -617,6 +640,131 @@ function withActiveRuns(
|
|||
}));
|
||||
}
|
||||
|
||||
async function userCommentStatsForIssues(
|
||||
dbOrTx: any,
|
||||
companyId: string,
|
||||
userId: string,
|
||||
issueIds: string[],
|
||||
): Promise<IssueUserCommentStats[]> {
|
||||
const stats: IssueUserCommentStats[] = [];
|
||||
for (const issueIdChunk of chunkList(issueIds, ISSUE_LIST_RELATED_QUERY_CHUNK_SIZE)) {
|
||||
const rows = await dbOrTx
|
||||
.select({
|
||||
issueId: issueComments.issueId,
|
||||
myLastCommentAt: sql<Date | null>`
|
||||
MAX(CASE WHEN ${issueComments.authorUserId} = ${userId} THEN ${issueComments.createdAt} END)
|
||||
`,
|
||||
lastExternalCommentAt: sql<Date | null>`
|
||||
MAX(
|
||||
CASE
|
||||
WHEN ${issueComments.authorUserId} IS NULL OR ${issueComments.authorUserId} <> ${userId}
|
||||
THEN ${issueComments.createdAt}
|
||||
END
|
||||
)
|
||||
`,
|
||||
})
|
||||
.from(issueComments)
|
||||
.where(
|
||||
and(
|
||||
eq(issueComments.companyId, companyId),
|
||||
inArray(issueComments.issueId, issueIdChunk),
|
||||
),
|
||||
)
|
||||
.groupBy(issueComments.issueId);
|
||||
stats.push(...rows);
|
||||
}
|
||||
return stats;
|
||||
}
|
||||
|
||||
async function userReadStatsForIssues(
|
||||
dbOrTx: any,
|
||||
companyId: string,
|
||||
userId: string,
|
||||
issueIds: string[],
|
||||
): Promise<IssueReadStat[]> {
|
||||
const stats: IssueReadStat[] = [];
|
||||
for (const issueIdChunk of chunkList(issueIds, ISSUE_LIST_RELATED_QUERY_CHUNK_SIZE)) {
|
||||
const rows = await dbOrTx
|
||||
.select({
|
||||
issueId: issueReadStates.issueId,
|
||||
myLastReadAt: issueReadStates.lastReadAt,
|
||||
})
|
||||
.from(issueReadStates)
|
||||
.where(
|
||||
and(
|
||||
eq(issueReadStates.companyId, companyId),
|
||||
eq(issueReadStates.userId, userId),
|
||||
inArray(issueReadStates.issueId, issueIdChunk),
|
||||
),
|
||||
);
|
||||
stats.push(...rows);
|
||||
}
|
||||
return stats;
|
||||
}
|
||||
|
||||
async function lastActivityStatsForIssues(
|
||||
dbOrTx: any,
|
||||
companyId: string,
|
||||
issueIds: string[],
|
||||
): Promise<IssueLastActivityStat[]> {
|
||||
const byIssueId = new Map<string, IssueLastActivityStat>();
|
||||
for (const issueIdChunk of chunkList(issueIds, ISSUE_LIST_RELATED_QUERY_CHUNK_SIZE)) {
|
||||
const [commentRows, logRows] = await Promise.all([
|
||||
dbOrTx
|
||||
.select({
|
||||
issueId: issueComments.issueId,
|
||||
latestCommentAt: sql<Date | null>`MAX(${issueComments.createdAt})`,
|
||||
})
|
||||
.from(issueComments)
|
||||
.where(
|
||||
and(
|
||||
eq(issueComments.companyId, companyId),
|
||||
inArray(issueComments.issueId, issueIdChunk),
|
||||
),
|
||||
)
|
||||
.groupBy(issueComments.issueId),
|
||||
dbOrTx
|
||||
.select({
|
||||
issueId: activityLog.entityId,
|
||||
latestLogAt: sql<Date | null>`MAX(${activityLog.createdAt})`,
|
||||
})
|
||||
.from(activityLog)
|
||||
.where(
|
||||
and(
|
||||
eq(activityLog.companyId, companyId),
|
||||
eq(activityLog.entityType, "issue"),
|
||||
inArray(activityLog.entityId, issueIdChunk),
|
||||
sql`${activityLog.action} NOT IN (${sql.join(
|
||||
ISSUE_LOCAL_INBOX_ACTIVITY_ACTIONS.map((action) => sql`${action}`),
|
||||
sql`, `,
|
||||
)})`,
|
||||
),
|
||||
)
|
||||
.groupBy(activityLog.entityId),
|
||||
]);
|
||||
|
||||
for (const row of commentRows) {
|
||||
byIssueId.set(row.issueId, {
|
||||
issueId: row.issueId,
|
||||
latestCommentAt: row.latestCommentAt,
|
||||
latestLogAt: null,
|
||||
});
|
||||
}
|
||||
for (const row of logRows) {
|
||||
const existing = byIssueId.get(row.issueId);
|
||||
if (existing) existing.latestLogAt = row.latestLogAt;
|
||||
else {
|
||||
byIssueId.set(row.issueId, {
|
||||
issueId: row.issueId,
|
||||
latestCommentAt: null,
|
||||
latestLogAt: row.latestLogAt,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return [...byIssueId.values()];
|
||||
}
|
||||
|
||||
export function issueService(db: Db) {
|
||||
const instanceSettings = instanceSettingsService(db);
|
||||
|
||||
|
|
@ -1105,99 +1253,12 @@ export function issueService(db: Db) {
|
|||
const issueIds = withRuns.map((row) => row.id);
|
||||
const [statsRows, readRows, lastActivityRows] = await Promise.all([
|
||||
contextUserId
|
||||
? db
|
||||
.select({
|
||||
issueId: issueComments.issueId,
|
||||
myLastCommentAt: sql<Date | null>`
|
||||
MAX(CASE WHEN ${issueComments.authorUserId} = ${contextUserId} THEN ${issueComments.createdAt} END)
|
||||
`,
|
||||
lastExternalCommentAt: sql<Date | null>`
|
||||
MAX(
|
||||
CASE
|
||||
WHEN ${issueComments.authorUserId} IS NULL OR ${issueComments.authorUserId} <> ${contextUserId}
|
||||
THEN ${issueComments.createdAt}
|
||||
END
|
||||
)
|
||||
`,
|
||||
})
|
||||
.from(issueComments)
|
||||
.where(
|
||||
and(
|
||||
eq(issueComments.companyId, companyId),
|
||||
inArray(issueComments.issueId, issueIds),
|
||||
),
|
||||
)
|
||||
.groupBy(issueComments.issueId)
|
||||
? userCommentStatsForIssues(db, companyId, contextUserId, issueIds)
|
||||
: Promise.resolve([]),
|
||||
contextUserId
|
||||
? db
|
||||
.select({
|
||||
issueId: issueReadStates.issueId,
|
||||
myLastReadAt: issueReadStates.lastReadAt,
|
||||
})
|
||||
.from(issueReadStates)
|
||||
.where(
|
||||
and(
|
||||
eq(issueReadStates.companyId, companyId),
|
||||
eq(issueReadStates.userId, contextUserId),
|
||||
inArray(issueReadStates.issueId, issueIds),
|
||||
),
|
||||
)
|
||||
? userReadStatsForIssues(db, companyId, contextUserId, issueIds)
|
||||
: Promise.resolve([]),
|
||||
Promise.all([
|
||||
db
|
||||
.select({
|
||||
issueId: issueComments.issueId,
|
||||
latestCommentAt: sql<Date | null>`MAX(${issueComments.createdAt})`,
|
||||
})
|
||||
.from(issueComments)
|
||||
.where(
|
||||
and(
|
||||
eq(issueComments.companyId, companyId),
|
||||
inArray(issueComments.issueId, issueIds),
|
||||
),
|
||||
)
|
||||
.groupBy(issueComments.issueId),
|
||||
db
|
||||
.select({
|
||||
issueId: activityLog.entityId,
|
||||
latestLogAt: sql<Date | null>`MAX(${activityLog.createdAt})`,
|
||||
})
|
||||
.from(activityLog)
|
||||
.where(
|
||||
and(
|
||||
eq(activityLog.companyId, companyId),
|
||||
eq(activityLog.entityType, "issue"),
|
||||
inArray(activityLog.entityId, issueIds),
|
||||
sql`${activityLog.action} NOT IN (${sql.join(
|
||||
ISSUE_LOCAL_INBOX_ACTIVITY_ACTIONS.map((action) => sql`${action}`),
|
||||
sql`, `,
|
||||
)})`,
|
||||
),
|
||||
)
|
||||
.groupBy(activityLog.entityId),
|
||||
]).then(([commentRows, logRows]) => {
|
||||
const byIssueId = new Map<string, IssueLastActivityStat>();
|
||||
for (const row of commentRows) {
|
||||
byIssueId.set(row.issueId, {
|
||||
issueId: row.issueId,
|
||||
latestCommentAt: row.latestCommentAt,
|
||||
latestLogAt: null,
|
||||
});
|
||||
}
|
||||
for (const row of logRows) {
|
||||
const existing = byIssueId.get(row.issueId);
|
||||
if (existing) existing.latestLogAt = row.latestLogAt;
|
||||
else {
|
||||
byIssueId.set(row.issueId, {
|
||||
issueId: row.issueId,
|
||||
latestCommentAt: null,
|
||||
latestLogAt: row.latestLogAt,
|
||||
});
|
||||
}
|
||||
}
|
||||
return [...byIssueId.values()];
|
||||
}),
|
||||
lastActivityStatsForIssues(db, companyId, issueIds),
|
||||
]);
|
||||
const statsByIssueId = new Map(statsRows.map((row) => [row.issueId, row]));
|
||||
const lastActivityByIssueId = new Map(lastActivityRows.map((row) => [row.issueId, row]));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue