Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
import { Router, type Request, type Response } from "express";
|
2026-02-16 13:31:58 -06:00
|
|
|
import type { Db } from "@paperclip/db";
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
import {
|
|
|
|
|
addIssueCommentSchema,
|
|
|
|
|
checkoutIssueSchema,
|
|
|
|
|
createIssueSchema,
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
linkIssueApprovalSchema,
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
updateIssueSchema,
|
|
|
|
|
} from "@paperclip/shared";
|
2026-02-16 13:31:58 -06:00
|
|
|
import { validate } from "../middleware/validate.js";
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
import {
|
|
|
|
|
agentService,
|
|
|
|
|
heartbeatService,
|
|
|
|
|
issueApprovalService,
|
|
|
|
|
issueService,
|
|
|
|
|
logActivity,
|
|
|
|
|
} from "../services/index.js";
|
2026-02-17 12:24:43 -06:00
|
|
|
import { logger } from "../middleware/logger.js";
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
import { assertCompanyAccess, getActorInfo } from "./authz.js";
|
2026-02-16 13:31:58 -06:00
|
|
|
|
|
|
|
|
export function issueRoutes(db: Db) {
|
|
|
|
|
const router = Router();
|
|
|
|
|
const svc = issueService(db);
|
2026-02-17 12:24:43 -06:00
|
|
|
const heartbeat = heartbeatService(db);
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
const agentsSvc = agentService(db);
|
|
|
|
|
const issueApprovalsSvc = issueApprovalService(db);
|
|
|
|
|
|
|
|
|
|
async function assertCanManageIssueApprovalLinks(req: Request, res: Response, companyId: string) {
|
|
|
|
|
assertCompanyAccess(req, companyId);
|
|
|
|
|
if (req.actor.type === "board") return true;
|
|
|
|
|
if (!req.actor.agentId) {
|
|
|
|
|
res.status(403).json({ error: "Agent authentication required" });
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const actorAgent = await agentsSvc.getById(req.actor.agentId);
|
|
|
|
|
if (!actorAgent || actorAgent.companyId !== companyId) {
|
|
|
|
|
res.status(403).json({ error: "Forbidden" });
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (actorAgent.role === "ceo" || Boolean(actorAgent.permissions?.canCreateAgents)) return true;
|
|
|
|
|
res.status(403).json({ error: "Missing permission to link approvals" });
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-02-16 13:31:58 -06:00
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
router.get("/companies/:companyId/issues", async (req, res) => {
|
|
|
|
|
const companyId = req.params.companyId as string;
|
|
|
|
|
assertCompanyAccess(req, companyId);
|
|
|
|
|
const result = await svc.list(companyId, {
|
|
|
|
|
status: req.query.status as string | undefined,
|
|
|
|
|
assigneeAgentId: req.query.assigneeAgentId as string | undefined,
|
|
|
|
|
projectId: req.query.projectId as string | undefined,
|
|
|
|
|
});
|
2026-02-16 13:31:58 -06:00
|
|
|
res.json(result);
|
|
|
|
|
});
|
|
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
router.get("/issues/:id", async (req, res) => {
|
2026-02-16 13:31:58 -06:00
|
|
|
const id = req.params.id as string;
|
2026-02-19 09:09:40 -06:00
|
|
|
const isIdentifier = /^[A-Z]+-\d+$/i.test(id);
|
|
|
|
|
const issue = isIdentifier ? await svc.getByIdentifier(id) : await svc.getById(id);
|
2026-02-16 13:31:58 -06:00
|
|
|
if (!issue) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
assertCompanyAccess(req, issue.companyId);
|
2026-02-19 09:09:40 -06:00
|
|
|
const ancestors = await svc.getAncestors(issue.id);
|
2026-02-17 20:07:14 -06:00
|
|
|
res.json({ ...issue, ancestors });
|
2026-02-16 13:31:58 -06:00
|
|
|
});
|
|
|
|
|
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
router.get("/issues/:id/approvals", async (req, res) => {
|
|
|
|
|
const id = req.params.id as string;
|
|
|
|
|
const issue = await svc.getById(id);
|
|
|
|
|
if (!issue) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
assertCompanyAccess(req, issue.companyId);
|
|
|
|
|
const approvals = await issueApprovalsSvc.listApprovalsForIssue(id);
|
|
|
|
|
res.json(approvals);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post("/issues/:id/approvals", validate(linkIssueApprovalSchema), async (req, res) => {
|
|
|
|
|
const id = req.params.id as string;
|
|
|
|
|
const issue = await svc.getById(id);
|
|
|
|
|
if (!issue) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!(await assertCanManageIssueApprovalLinks(req, res, issue.companyId))) return;
|
|
|
|
|
|
|
|
|
|
const actor = getActorInfo(req);
|
|
|
|
|
await issueApprovalsSvc.link(id, req.body.approvalId, {
|
|
|
|
|
agentId: actor.agentId,
|
|
|
|
|
userId: actor.actorType === "user" ? actor.actorId : null,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await logActivity(db, {
|
|
|
|
|
companyId: issue.companyId,
|
|
|
|
|
actorType: actor.actorType,
|
|
|
|
|
actorId: actor.actorId,
|
|
|
|
|
agentId: actor.agentId,
|
|
|
|
|
runId: actor.runId,
|
|
|
|
|
action: "issue.approval_linked",
|
|
|
|
|
entityType: "issue",
|
|
|
|
|
entityId: issue.id,
|
|
|
|
|
details: { approvalId: req.body.approvalId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const approvals = await issueApprovalsSvc.listApprovalsForIssue(id);
|
|
|
|
|
res.status(201).json(approvals);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.delete("/issues/:id/approvals/:approvalId", async (req, res) => {
|
|
|
|
|
const id = req.params.id as string;
|
|
|
|
|
const approvalId = req.params.approvalId as string;
|
|
|
|
|
const issue = await svc.getById(id);
|
|
|
|
|
if (!issue) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!(await assertCanManageIssueApprovalLinks(req, res, issue.companyId))) return;
|
|
|
|
|
|
|
|
|
|
await issueApprovalsSvc.unlink(id, approvalId);
|
|
|
|
|
|
|
|
|
|
const actor = getActorInfo(req);
|
|
|
|
|
await logActivity(db, {
|
|
|
|
|
companyId: issue.companyId,
|
|
|
|
|
actorType: actor.actorType,
|
|
|
|
|
actorId: actor.actorId,
|
|
|
|
|
agentId: actor.agentId,
|
|
|
|
|
runId: actor.runId,
|
|
|
|
|
action: "issue.approval_unlinked",
|
|
|
|
|
entityType: "issue",
|
|
|
|
|
entityId: issue.id,
|
|
|
|
|
details: { approvalId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.json({ ok: true });
|
|
|
|
|
});
|
|
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
router.post("/companies/:companyId/issues", validate(createIssueSchema), async (req, res) => {
|
|
|
|
|
const companyId = req.params.companyId as string;
|
|
|
|
|
assertCompanyAccess(req, companyId);
|
|
|
|
|
|
|
|
|
|
const actor = getActorInfo(req);
|
|
|
|
|
const issue = await svc.create(companyId, {
|
|
|
|
|
...req.body,
|
|
|
|
|
createdByAgentId: actor.agentId,
|
|
|
|
|
createdByUserId: actor.actorType === "user" ? actor.actorId : null,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await logActivity(db, {
|
|
|
|
|
companyId,
|
|
|
|
|
actorType: actor.actorType,
|
|
|
|
|
actorId: actor.actorId,
|
|
|
|
|
agentId: actor.agentId,
|
2026-02-19 09:09:40 -06:00
|
|
|
runId: actor.runId,
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
action: "issue.created",
|
|
|
|
|
entityType: "issue",
|
|
|
|
|
entityId: issue.id,
|
|
|
|
|
details: { title: issue.title },
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-17 12:24:43 -06:00
|
|
|
if (issue.assigneeAgentId) {
|
|
|
|
|
void heartbeat
|
|
|
|
|
.wakeup(issue.assigneeAgentId, {
|
|
|
|
|
source: "assignment",
|
|
|
|
|
triggerDetail: "system",
|
|
|
|
|
reason: "issue_assigned",
|
|
|
|
|
payload: { issueId: issue.id, mutation: "create" },
|
|
|
|
|
requestedByActorType: actor.actorType,
|
|
|
|
|
requestedByActorId: actor.actorId,
|
|
|
|
|
contextSnapshot: { issueId: issue.id, source: "issue.create" },
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => logger.warn({ err, issueId: issue.id }, "failed to wake assignee on issue create"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 13:31:58 -06:00
|
|
|
res.status(201).json(issue);
|
|
|
|
|
});
|
|
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
router.patch("/issues/:id", validate(updateIssueSchema), async (req, res) => {
|
2026-02-16 13:31:58 -06:00
|
|
|
const id = req.params.id as string;
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
const existing = await svc.getById(id);
|
|
|
|
|
if (!existing) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
assertCompanyAccess(req, existing.companyId);
|
|
|
|
|
|
2026-02-17 20:46:12 -06:00
|
|
|
const { comment: commentBody, ...updateFields } = req.body;
|
|
|
|
|
const issue = await svc.update(id, updateFields);
|
2026-02-16 13:31:58 -06:00
|
|
|
if (!issue) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
// Build activity details with previous values for changed fields
|
|
|
|
|
const previous: Record<string, unknown> = {};
|
|
|
|
|
for (const key of Object.keys(updateFields)) {
|
|
|
|
|
if (key in existing && (existing as Record<string, unknown>)[key] !== (updateFields as Record<string, unknown>)[key]) {
|
|
|
|
|
previous[key] = (existing as Record<string, unknown>)[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
const actor = getActorInfo(req);
|
|
|
|
|
await logActivity(db, {
|
|
|
|
|
companyId: issue.companyId,
|
|
|
|
|
actorType: actor.actorType,
|
|
|
|
|
actorId: actor.actorId,
|
|
|
|
|
agentId: actor.agentId,
|
2026-02-19 09:09:40 -06:00
|
|
|
runId: actor.runId,
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
action: "issue.updated",
|
|
|
|
|
entityType: "issue",
|
|
|
|
|
entityId: issue.id,
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
details: { ...updateFields, _previous: Object.keys(previous).length > 0 ? previous : undefined },
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
});
|
|
|
|
|
|
2026-02-17 20:46:12 -06:00
|
|
|
let comment = null;
|
|
|
|
|
if (commentBody) {
|
|
|
|
|
comment = await svc.addComment(id, commentBody, {
|
|
|
|
|
agentId: actor.agentId ?? undefined,
|
|
|
|
|
userId: actor.actorType === "user" ? actor.actorId : undefined,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await logActivity(db, {
|
|
|
|
|
companyId: issue.companyId,
|
|
|
|
|
actorType: actor.actorType,
|
|
|
|
|
actorId: actor.actorId,
|
|
|
|
|
agentId: actor.agentId,
|
2026-02-19 09:09:40 -06:00
|
|
|
runId: actor.runId,
|
2026-02-17 20:46:12 -06:00
|
|
|
action: "issue.comment_added",
|
|
|
|
|
entityType: "issue",
|
|
|
|
|
entityId: issue.id,
|
|
|
|
|
details: { commentId: comment.id },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// @-mention wakeups
|
|
|
|
|
svc.findMentionedAgents(issue.companyId, commentBody).then((ids) => {
|
|
|
|
|
for (const mentionedId of ids) {
|
|
|
|
|
heartbeat.wakeup(mentionedId, {
|
|
|
|
|
source: "automation",
|
|
|
|
|
triggerDetail: "system",
|
|
|
|
|
reason: `Mentioned in comment on issue ${id}`,
|
|
|
|
|
payload: { issueId: id, commentId: comment!.id },
|
|
|
|
|
requestedByActorType: actor.actorType,
|
|
|
|
|
requestedByActorId: actor.actorId,
|
|
|
|
|
contextSnapshot: { issueId: id, commentId: comment!.id, source: "comment.mention" },
|
|
|
|
|
}).catch((err) => logger.warn({ err, agentId: mentionedId }, "failed to wake mentioned agent"));
|
|
|
|
|
}
|
|
|
|
|
}).catch((err) => logger.warn({ err, issueId: id }, "failed to resolve @-mentions"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 12:24:43 -06:00
|
|
|
const assigneeChanged =
|
|
|
|
|
req.body.assigneeAgentId !== undefined && req.body.assigneeAgentId !== existing.assigneeAgentId;
|
2026-02-19 09:09:40 -06:00
|
|
|
const reopened =
|
|
|
|
|
(existing.status === "done" || existing.status === "cancelled") &&
|
|
|
|
|
issue.status !== "done" && issue.status !== "cancelled";
|
|
|
|
|
|
|
|
|
|
if ((assigneeChanged || reopened) && issue.assigneeAgentId) {
|
2026-02-17 12:24:43 -06:00
|
|
|
void heartbeat
|
|
|
|
|
.wakeup(issue.assigneeAgentId, {
|
2026-02-19 09:09:40 -06:00
|
|
|
source: reopened ? "automation" : "assignment",
|
2026-02-17 12:24:43 -06:00
|
|
|
triggerDetail: "system",
|
2026-02-19 09:09:40 -06:00
|
|
|
reason: reopened ? "issue_reopened" : "issue_assigned",
|
2026-02-17 12:24:43 -06:00
|
|
|
payload: { issueId: issue.id, mutation: "update" },
|
|
|
|
|
requestedByActorType: actor.actorType,
|
|
|
|
|
requestedByActorId: actor.actorId,
|
2026-02-19 09:09:40 -06:00
|
|
|
contextSnapshot: { issueId: issue.id, source: reopened ? "issue.reopen" : "issue.update" },
|
2026-02-17 12:24:43 -06:00
|
|
|
})
|
|
|
|
|
.catch((err) => logger.warn({ err, issueId: issue.id }, "failed to wake assignee on issue update"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 20:46:12 -06:00
|
|
|
res.json({ ...issue, comment });
|
2026-02-16 13:31:58 -06:00
|
|
|
});
|
|
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
router.delete("/issues/:id", async (req, res) => {
|
2026-02-16 13:31:58 -06:00
|
|
|
const id = req.params.id as string;
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
const existing = await svc.getById(id);
|
|
|
|
|
if (!existing) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
assertCompanyAccess(req, existing.companyId);
|
|
|
|
|
|
2026-02-16 13:31:58 -06:00
|
|
|
const issue = await svc.remove(id);
|
|
|
|
|
if (!issue) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
|
|
|
|
|
const actor = getActorInfo(req);
|
|
|
|
|
await logActivity(db, {
|
|
|
|
|
companyId: issue.companyId,
|
|
|
|
|
actorType: actor.actorType,
|
|
|
|
|
actorId: actor.actorId,
|
|
|
|
|
agentId: actor.agentId,
|
2026-02-19 09:09:40 -06:00
|
|
|
runId: actor.runId,
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
action: "issue.deleted",
|
|
|
|
|
entityType: "issue",
|
|
|
|
|
entityId: issue.id,
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-16 13:31:58 -06:00
|
|
|
res.json(issue);
|
|
|
|
|
});
|
|
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
router.post("/issues/:id/checkout", validate(checkoutIssueSchema), async (req, res) => {
|
|
|
|
|
const id = req.params.id as string;
|
|
|
|
|
const issue = await svc.getById(id);
|
|
|
|
|
if (!issue) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
assertCompanyAccess(req, issue.companyId);
|
|
|
|
|
|
|
|
|
|
if (req.actor.type === "agent" && req.actor.agentId !== req.body.agentId) {
|
|
|
|
|
res.status(403).json({ error: "Agent can only checkout as itself" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updated = await svc.checkout(id, req.body.agentId, req.body.expectedStatuses);
|
|
|
|
|
const actor = getActorInfo(req);
|
|
|
|
|
|
|
|
|
|
await logActivity(db, {
|
|
|
|
|
companyId: issue.companyId,
|
|
|
|
|
actorType: actor.actorType,
|
|
|
|
|
actorId: actor.actorId,
|
|
|
|
|
agentId: actor.agentId,
|
2026-02-19 09:09:40 -06:00
|
|
|
runId: actor.runId,
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
action: "issue.checked_out",
|
|
|
|
|
entityType: "issue",
|
|
|
|
|
entityId: issue.id,
|
|
|
|
|
details: { agentId: req.body.agentId },
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-17 12:24:43 -06:00
|
|
|
void heartbeat
|
|
|
|
|
.wakeup(req.body.agentId, {
|
|
|
|
|
source: "assignment",
|
|
|
|
|
triggerDetail: "system",
|
|
|
|
|
reason: "issue_checked_out",
|
|
|
|
|
payload: { issueId: issue.id, mutation: "checkout" },
|
|
|
|
|
requestedByActorType: actor.actorType,
|
|
|
|
|
requestedByActorId: actor.actorId,
|
|
|
|
|
contextSnapshot: { issueId: issue.id, source: "issue.checkout" },
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => logger.warn({ err, issueId: issue.id }, "failed to wake assignee on issue checkout"));
|
|
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
res.json(updated);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post("/issues/:id/release", async (req, res) => {
|
|
|
|
|
const id = req.params.id as string;
|
|
|
|
|
const existing = await svc.getById(id);
|
|
|
|
|
if (!existing) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
assertCompanyAccess(req, existing.companyId);
|
|
|
|
|
|
|
|
|
|
const released = await svc.release(id, req.actor.type === "agent" ? req.actor.agentId : undefined);
|
|
|
|
|
if (!released) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const actor = getActorInfo(req);
|
|
|
|
|
await logActivity(db, {
|
|
|
|
|
companyId: released.companyId,
|
|
|
|
|
actorType: actor.actorType,
|
|
|
|
|
actorId: actor.actorId,
|
|
|
|
|
agentId: actor.agentId,
|
2026-02-19 09:09:40 -06:00
|
|
|
runId: actor.runId,
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
action: "issue.released",
|
|
|
|
|
entityType: "issue",
|
|
|
|
|
entityId: released.id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.json(released);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get("/issues/:id/comments", async (req, res) => {
|
|
|
|
|
const id = req.params.id as string;
|
|
|
|
|
const issue = await svc.getById(id);
|
|
|
|
|
if (!issue) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
assertCompanyAccess(req, issue.companyId);
|
|
|
|
|
const comments = await svc.listComments(id);
|
|
|
|
|
res.json(comments);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post("/issues/:id/comments", validate(addIssueCommentSchema), async (req, res) => {
|
|
|
|
|
const id = req.params.id as string;
|
|
|
|
|
const issue = await svc.getById(id);
|
|
|
|
|
if (!issue) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
assertCompanyAccess(req, issue.companyId);
|
|
|
|
|
|
|
|
|
|
const actor = getActorInfo(req);
|
2026-02-19 09:09:40 -06:00
|
|
|
const reopenRequested = req.body.reopen === true;
|
|
|
|
|
const isClosed = issue.status === "done" || issue.status === "cancelled";
|
|
|
|
|
let reopened = false;
|
|
|
|
|
let reopenFromStatus: string | null = null;
|
|
|
|
|
let currentIssue = issue;
|
|
|
|
|
|
|
|
|
|
if (reopenRequested && isClosed) {
|
|
|
|
|
const reopenedIssue = await svc.update(id, { status: "todo" });
|
|
|
|
|
if (!reopenedIssue) {
|
|
|
|
|
res.status(404).json({ error: "Issue not found" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
reopened = true;
|
|
|
|
|
reopenFromStatus = issue.status;
|
|
|
|
|
currentIssue = reopenedIssue;
|
|
|
|
|
|
|
|
|
|
await logActivity(db, {
|
|
|
|
|
companyId: currentIssue.companyId,
|
|
|
|
|
actorType: actor.actorType,
|
|
|
|
|
actorId: actor.actorId,
|
|
|
|
|
agentId: actor.agentId,
|
|
|
|
|
runId: actor.runId,
|
|
|
|
|
action: "issue.updated",
|
|
|
|
|
entityType: "issue",
|
|
|
|
|
entityId: currentIssue.id,
|
|
|
|
|
details: {
|
|
|
|
|
status: "todo",
|
|
|
|
|
reopened: true,
|
|
|
|
|
reopenedFrom: reopenFromStatus,
|
|
|
|
|
source: "comment",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
const comment = await svc.addComment(id, req.body.body, {
|
|
|
|
|
agentId: actor.agentId ?? undefined,
|
|
|
|
|
userId: actor.actorType === "user" ? actor.actorId : undefined,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await logActivity(db, {
|
2026-02-19 09:09:40 -06:00
|
|
|
companyId: currentIssue.companyId,
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
actorType: actor.actorType,
|
|
|
|
|
actorId: actor.actorId,
|
|
|
|
|
agentId: actor.agentId,
|
2026-02-19 09:09:40 -06:00
|
|
|
runId: actor.runId,
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
action: "issue.comment_added",
|
|
|
|
|
entityType: "issue",
|
2026-02-19 09:09:40 -06:00
|
|
|
entityId: currentIssue.id,
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
details: { commentId: comment.id },
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-17 20:07:14 -06:00
|
|
|
// @-mention wakeups
|
|
|
|
|
svc.findMentionedAgents(issue.companyId, req.body.body).then((ids) => {
|
|
|
|
|
for (const mentionedId of ids) {
|
|
|
|
|
heartbeat.wakeup(mentionedId, {
|
|
|
|
|
source: "automation",
|
|
|
|
|
triggerDetail: "system",
|
|
|
|
|
reason: `Mentioned in comment on issue ${id}`,
|
|
|
|
|
payload: { issueId: id, commentId: comment.id },
|
|
|
|
|
requestedByActorType: actor.actorType,
|
|
|
|
|
requestedByActorId: actor.actorId,
|
|
|
|
|
contextSnapshot: { issueId: id, commentId: comment.id, source: "comment.mention" },
|
|
|
|
|
}).catch((err) => logger.warn({ err, agentId: mentionedId }, "failed to wake mentioned agent"));
|
|
|
|
|
}
|
|
|
|
|
}).catch((err) => logger.warn({ err, issueId: id }, "failed to resolve @-mentions"));
|
|
|
|
|
|
2026-02-19 09:09:40 -06:00
|
|
|
if (reopened && currentIssue.assigneeAgentId) {
|
|
|
|
|
void heartbeat
|
|
|
|
|
.wakeup(currentIssue.assigneeAgentId, {
|
|
|
|
|
source: "automation",
|
|
|
|
|
triggerDetail: "system",
|
|
|
|
|
reason: "issue_reopened_via_comment",
|
|
|
|
|
payload: {
|
|
|
|
|
issueId: currentIssue.id,
|
|
|
|
|
commentId: comment.id,
|
|
|
|
|
reopenedFrom: reopenFromStatus,
|
|
|
|
|
mutation: "comment",
|
|
|
|
|
},
|
|
|
|
|
requestedByActorType: actor.actorType,
|
|
|
|
|
requestedByActorId: actor.actorId,
|
|
|
|
|
contextSnapshot: {
|
|
|
|
|
issueId: currentIssue.id,
|
|
|
|
|
taskId: currentIssue.id,
|
|
|
|
|
commentId: comment.id,
|
|
|
|
|
source: "issue.comment.reopen",
|
|
|
|
|
wakeReason: "issue_reopened_via_comment",
|
|
|
|
|
reopenedFrom: reopenFromStatus,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => logger.warn({ err, issueId: currentIssue.id }, "failed to wake assignee on issue reopen comment"));
|
Implement agent hiring, approval workflows, config revisions, LLM reflection, and sidebar badges
Agent management: hire endpoint with permission gates and pending_approval status,
config revision tracking with rollback, agent duplicate route, permission CRUD.
Block pending_approval agents from auth, heartbeat, and assignments.
Approvals: revision request/resubmit flow, approval comments CRUD, issue-approval
linking, auto-wake agents on approval decisions with context snapshot.
Costs: per-agent breakdown, period filtering (month/week/day/all), cost by agent
list endpoint.
Adapters: agentConfigurationDoc on all adapters, /llms/agent-configuration.txt
reflection routes. Inject PAPERCLIP_APPROVAL_ID, PAPERCLIP_APPROVAL_STATUS,
PAPERCLIP_LINKED_ISSUE_IDS into adapter environments.
Sidebar badges endpoint for pending approval/inbox counts. Dashboard and company
settings extensions.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:41 -06:00
|
|
|
} else if (currentIssue.assigneeAgentId) {
|
|
|
|
|
void heartbeat
|
|
|
|
|
.wakeup(currentIssue.assigneeAgentId, {
|
|
|
|
|
source: "automation",
|
|
|
|
|
triggerDetail: "system",
|
|
|
|
|
reason: "issue_commented",
|
|
|
|
|
payload: {
|
|
|
|
|
issueId: currentIssue.id,
|
|
|
|
|
commentId: comment.id,
|
|
|
|
|
mutation: "comment",
|
|
|
|
|
},
|
|
|
|
|
requestedByActorType: actor.actorType,
|
|
|
|
|
requestedByActorId: actor.actorId,
|
|
|
|
|
contextSnapshot: {
|
|
|
|
|
issueId: currentIssue.id,
|
|
|
|
|
taskId: currentIssue.id,
|
|
|
|
|
commentId: comment.id,
|
|
|
|
|
source: "issue.comment",
|
|
|
|
|
wakeReason: "issue_commented",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => logger.warn({ err, issueId: currentIssue.id }, "failed to wake assignee on issue comment"));
|
2026-02-19 09:09:40 -06:00
|
|
|
}
|
|
|
|
|
|
Add server routes for companies, approvals, costs, and dashboard
New routes: companies, approvals, costs, dashboard, authz. New
services: companies, approvals, costs, dashboard, heartbeat,
activity-log. Add auth middleware and structured error handling.
Expand existing agent and issue routes with richer CRUD operations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 09:07:27 -06:00
|
|
|
res.status(201).json(comment);
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-16 13:31:58 -06:00
|
|
|
return router;
|
|
|
|
|
}
|