mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-16 10:50:38 +09:00
Improve operator workflow QoL (#5291)
## Thinking Path > - Paperclip is a control plane operators use repeatedly to supervise agent companies. > - Common operator workflows depend on fast scanning of inboxes, issue sidebars, workspaces, cost totals, and runtime services. > - Several small UI and service gaps made those workflows slower or less clear. > - This pull request groups the operator-facing QoL changes that can stand alone from recovery and adapter work. > - The benefit is a denser, clearer board experience for issue triage and workspace operation. ## What Changed - Added inbox assignee/project grouping and issue list token/runtime totals. - Improved issue properties with removable blocker chips and workspace task links. - Improved execution workspace layout, runtime controls, issues tab default, and stopped-port reuse behavior. - Added mobile markdown/routine dialog fixes, page title company names, sidebar polish, and dashboard run task label cleanup. ## Verification - `pnpm install --frozen-lockfile` - `pnpm exec vitest run ui/src/lib/inbox.test.ts ui/src/components/IssueProperties.test.tsx ui/src/components/WorkspaceRuntimeControls.test.tsx server/src/__tests__/workspace-runtime.test.ts server/src/__tests__/costs-service.test.ts` ## Risks - Medium UI risk because this touches several operator surfaces. The branch is intentionally grouped around workflow/QoL files and keeps the file count below the Greptile limit. ## Model Used - OpenAI GPT-5 Codex via Paperclip `codex_local` adapter, with shell/git/GitHub CLI tool use. ## 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
11ffd6f2c5
commit
424e81d087
47 changed files with 1739 additions and 250 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { and, desc, eq, gte, isNotNull, isNull, lt, lte, sql } from "drizzle-orm";
|
||||
import { alias } from "drizzle-orm/pg-core";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import { activityLog, agents, companies, costEvents, issues, projects } from "@paperclipai/db";
|
||||
import { activityLog, agents, companies, costEvents, heartbeatRuns, issues, projects } from "@paperclipai/db";
|
||||
import { notFound, unprocessable } from "../errors.js";
|
||||
import { budgetService, type BudgetServiceHooks } from "./budgets.js";
|
||||
|
||||
|
|
@ -135,18 +135,53 @@ export function costService(db: Db, budgetHooks: BudgetServiceHooks = {}) {
|
|||
};
|
||||
},
|
||||
|
||||
issueTreeSummary: async (companyId: string, issueId: string) => {
|
||||
issueTreeSummary: async (
|
||||
companyId: string,
|
||||
issueId: string,
|
||||
options: { excludeRoot?: boolean } = {},
|
||||
) => {
|
||||
// Callers must resolve and authorize a visible root issue before invoking this.
|
||||
// The route does that so zero counts are not mistaken for a missing root.
|
||||
const childIssues = alias(issues, "child");
|
||||
const issueTreeCondition = sql<boolean>`
|
||||
${issues.id} IN (
|
||||
WITH RECURSIVE issue_tree(id) AS (
|
||||
|
||||
// The seed of the recursive CTE: when excludeRoot is true, start from
|
||||
// the direct children so the root issue itself is not counted.
|
||||
const cteSeed = options.excludeRoot
|
||||
? sql`
|
||||
SELECT ${issues.id}
|
||||
FROM ${issues}
|
||||
WHERE ${issues.companyId} = ${companyId}
|
||||
AND ${issues.parentId} = ${issueId}
|
||||
AND ${issues.hiddenAt} IS NULL
|
||||
`
|
||||
: sql`
|
||||
SELECT ${issues.id}
|
||||
FROM ${issues}
|
||||
WHERE ${issues.companyId} = ${companyId}
|
||||
AND ${issues.id} = ${issueId}
|
||||
AND ${issues.hiddenAt} IS NULL
|
||||
`;
|
||||
|
||||
const cteSeedText = options.excludeRoot
|
||||
? sql`
|
||||
SELECT (${issues.id})::text AS id
|
||||
FROM ${issues}
|
||||
WHERE ${issues.companyId} = ${companyId}
|
||||
AND ${issues.parentId} = ${issueId}
|
||||
AND ${issues.hiddenAt} IS NULL
|
||||
`
|
||||
: sql`
|
||||
SELECT (${issues.id})::text AS id
|
||||
FROM ${issues}
|
||||
WHERE ${issues.companyId} = ${companyId}
|
||||
AND ${issues.id} = ${issueId}
|
||||
AND ${issues.hiddenAt} IS NULL
|
||||
`;
|
||||
|
||||
const issueTreeCondition = sql<boolean>`
|
||||
${issues.id} IN (
|
||||
WITH RECURSIVE issue_tree(id) AS (
|
||||
${cteSeed}
|
||||
UNION ALL
|
||||
SELECT ${childIssues.id}
|
||||
FROM ${issues} ${childIssues}
|
||||
|
|
@ -158,38 +193,80 @@ export function costService(db: Db, budgetHooks: BudgetServiceHooks = {}) {
|
|||
)
|
||||
`;
|
||||
|
||||
const [row] = await db
|
||||
.select({
|
||||
issueCount: sql<number>`count(distinct ${issues.id})::int`,
|
||||
costCents: sumAsNumber(costEvents.costCents),
|
||||
inputTokens: sumAsNumber(costEvents.inputTokens),
|
||||
cachedInputTokens: sumAsNumber(costEvents.cachedInputTokens),
|
||||
outputTokens: sumAsNumber(costEvents.outputTokens),
|
||||
})
|
||||
.from(issues)
|
||||
.leftJoin(
|
||||
costEvents,
|
||||
and(
|
||||
eq(costEvents.companyId, companyId),
|
||||
eq(costEvents.issueId, issues.id),
|
||||
),
|
||||
const runSummarySql = sql`
|
||||
WITH RECURSIVE issue_tree(id) AS (
|
||||
${cteSeedText}
|
||||
UNION ALL
|
||||
SELECT (${childIssues.id})::text
|
||||
FROM ${issues} ${childIssues}
|
||||
JOIN issue_tree ON (${childIssues.parentId})::text = issue_tree.id
|
||||
WHERE ${childIssues.companyId} = ${companyId}
|
||||
AND ${childIssues.hiddenAt} IS NULL
|
||||
)
|
||||
.where(
|
||||
and(
|
||||
eq(issues.companyId, companyId),
|
||||
isNull(issues.hiddenAt),
|
||||
issueTreeCondition,
|
||||
SELECT
|
||||
count(distinct ${heartbeatRuns.id})::int AS "runCount",
|
||||
coalesce(sum(extract(epoch from (coalesce(${heartbeatRuns.finishedAt}, now()) - ${heartbeatRuns.startedAt})) * 1000), 0)::double precision AS "runtimeMs"
|
||||
FROM ${heartbeatRuns}
|
||||
WHERE ${heartbeatRuns.companyId} = ${companyId}
|
||||
AND ${heartbeatRuns.startedAt} IS NOT NULL
|
||||
AND (
|
||||
${heartbeatRuns.contextSnapshot} ->> 'issueId' IN (SELECT id FROM issue_tree)
|
||||
OR EXISTS (
|
||||
SELECT 1
|
||||
FROM ${activityLog}
|
||||
JOIN issue_tree ON ${activityLog.entityId} = issue_tree.id
|
||||
WHERE ${activityLog.companyId} = ${companyId}
|
||||
AND ${activityLog.entityType} = 'issue'
|
||||
AND ${activityLog.runId} = ${heartbeatRuns.id}
|
||||
)
|
||||
)
|
||||
`;
|
||||
|
||||
// Run cost-event aggregation and run-duration aggregation in parallel.
|
||||
// They're separate queries because cost_events fan out per-event and
|
||||
// joining heartbeat_runs through them would double-count run durations.
|
||||
const [costRowResult, runRowResult] = await Promise.all([
|
||||
db
|
||||
.select({
|
||||
issueCount: sql<number>`count(distinct ${issues.id})::int`,
|
||||
costCents: sumAsNumber(costEvents.costCents),
|
||||
inputTokens: sumAsNumber(costEvents.inputTokens),
|
||||
cachedInputTokens: sumAsNumber(costEvents.cachedInputTokens),
|
||||
outputTokens: sumAsNumber(costEvents.outputTokens),
|
||||
})
|
||||
.from(issues)
|
||||
.leftJoin(
|
||||
costEvents,
|
||||
and(
|
||||
eq(costEvents.companyId, companyId),
|
||||
eq(costEvents.issueId, issues.id),
|
||||
),
|
||||
)
|
||||
.where(
|
||||
and(
|
||||
eq(issues.companyId, companyId),
|
||||
isNull(issues.hiddenAt),
|
||||
issueTreeCondition,
|
||||
),
|
||||
),
|
||||
);
|
||||
db.execute(runSummarySql),
|
||||
]);
|
||||
|
||||
const costRow = costRowResult[0];
|
||||
const runRow = Array.isArray(runRowResult)
|
||||
? (runRowResult[0] as { runCount?: number | string | null; runtimeMs?: number | string | null } | undefined)
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
issueId,
|
||||
issueCount: Number(row?.issueCount ?? 0),
|
||||
issueCount: Number(costRow?.issueCount ?? 0),
|
||||
includeDescendants: true,
|
||||
costCents: Number(row?.costCents ?? 0),
|
||||
inputTokens: Number(row?.inputTokens ?? 0),
|
||||
cachedInputTokens: Number(row?.cachedInputTokens ?? 0),
|
||||
outputTokens: Number(row?.outputTokens ?? 0),
|
||||
costCents: Number(costRow?.costCents ?? 0),
|
||||
inputTokens: Number(costRow?.inputTokens ?? 0),
|
||||
cachedInputTokens: Number(costRow?.cachedInputTokens ?? 0),
|
||||
outputTokens: Number(costRow?.outputTokens ?? 0),
|
||||
runCount: Number(runRow?.runCount ?? 0),
|
||||
runtimeMs: Number(runRow?.runtimeMs ?? 0),
|
||||
};
|
||||
},
|
||||
|
||||
|
|
|
|||
|
|
@ -108,6 +108,11 @@ interface RuntimeServiceRecord extends RuntimeServiceRef {
|
|||
processGroupId: number | null;
|
||||
}
|
||||
|
||||
type StoppedRuntimeServiceReuseCandidate = {
|
||||
id: string;
|
||||
port: number | null;
|
||||
};
|
||||
|
||||
const runtimeServicesById = new Map<string, RuntimeServiceRecord>();
|
||||
const runtimeServicesByReuseKey = new Map<string, string>();
|
||||
const runtimeServiceLeasesByRun = new Map<string, string[]>();
|
||||
|
|
@ -1815,6 +1820,33 @@ async function persistRuntimeServiceRecord(db: Db | undefined, record: RuntimeSe
|
|||
});
|
||||
}
|
||||
|
||||
async function findStoppedRuntimeServiceReuseCandidate(input: {
|
||||
db?: Db;
|
||||
companyId: string;
|
||||
reuseKey: string | null;
|
||||
}): Promise<StoppedRuntimeServiceReuseCandidate | null> {
|
||||
if (!input.db || !input.reuseKey) return null;
|
||||
const row = await input.db
|
||||
.select({
|
||||
id: workspaceRuntimeServices.id,
|
||||
port: workspaceRuntimeServices.port,
|
||||
})
|
||||
.from(workspaceRuntimeServices)
|
||||
.where(
|
||||
and(
|
||||
eq(workspaceRuntimeServices.companyId, input.companyId),
|
||||
eq(workspaceRuntimeServices.reuseKey, input.reuseKey),
|
||||
eq(workspaceRuntimeServices.provider, "local_process"),
|
||||
eq(workspaceRuntimeServices.status, "stopped"),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(workspaceRuntimeServices.updatedAt))
|
||||
.limit(1)
|
||||
.then((rows) => rows[0] ?? null);
|
||||
|
||||
return row ?? null;
|
||||
}
|
||||
|
||||
function clearIdleTimer(record: RuntimeServiceRecord) {
|
||||
if (!record.idleTimer) return;
|
||||
clearTimeout(record.idleTimer);
|
||||
|
|
@ -1927,9 +1959,20 @@ async function startLocalRuntimeService(input: {
|
|||
const serviceIdentityFingerprint = input.reuseKey ?? envFingerprint;
|
||||
const explicitPort = identity.explicitPort;
|
||||
const identityPort = identity.identityPort;
|
||||
const stoppedReuseCandidate = await findStoppedRuntimeServiceReuseCandidate({
|
||||
db: input.db,
|
||||
companyId: input.agent.companyId,
|
||||
reuseKey: input.reuseKey,
|
||||
});
|
||||
const reusableStoppedPort =
|
||||
asString(portConfig.type, "") === "auto" && stoppedReuseCandidate?.port
|
||||
? (await readLocalServicePortOwner(stoppedReuseCandidate.port))
|
||||
? null
|
||||
: stoppedReuseCandidate.port
|
||||
: null;
|
||||
const port =
|
||||
asString(portConfig.type, "") === "auto"
|
||||
? await allocatePort()
|
||||
? (reusableStoppedPort ?? await allocatePort())
|
||||
: explicitPort > 0
|
||||
? explicitPort
|
||||
: null;
|
||||
|
|
@ -2073,7 +2116,7 @@ async function startLocalRuntimeService(input: {
|
|||
}
|
||||
|
||||
const record: RuntimeServiceRecord = {
|
||||
id: randomUUID(),
|
||||
id: stoppedReuseCandidate?.id ?? randomUUID(),
|
||||
companyId: input.agent.companyId,
|
||||
projectId: input.workspace.projectId,
|
||||
projectWorkspaceId: input.workspace.workspaceId,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue