mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 04:29:01 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Each task can have a task workspace, and the API lets agents and board users inspect and change it > - The routes `GET` and `PUT /api/issues/:id/workspace` and `POST /api/issues/:id/workspace/repositories` are in their own router > - The issues router changes an issue identifier (for example `ABC-12`) to the issue UUID, but that resolver does not apply to this router > - So these routes send the identifier into a UUID comparison, Postgres refuses it, and the API returns 500 > - This pull request makes these routes accept an issue identifier, and return 404 for a value that is not an identifier and not a UUID > - The benefit is that the workspace routes act like the other issue routes, and a bad value gives a clear 4xx, not a 500 ## Linked Issues or Issue Description I searched the open and closed issues and pull requests for this problem. I did not find one. Related issues show the same class of error on other routes: #20356, #13504, #17332. They do not touch these routes. **What happened?** `GET /api/issues/ABC-12/workspace` and `PUT /api/issues/ABC-12/workspace` return `500 {"error":"Internal server error"}`. The server log shows `DrizzleQueryError` caused by `PostgresError: invalid input syntax for type uuid: "ABC-12"`. The same request with the issue UUID returns 200. Other issue routes, for example `GET /api/issues/ABC-12` and `GET /api/issues/ABC-12/comments`, accept the identifier. A value that is not an identifier and not a UUID (for example `not-a-task`) also returns 500. **Expected behavior** The workspace routes accept an issue identifier, as the other issue routes do. The agent skill reference (`skills/paperclip/references/issue-workspaces.md`) writes these routes with `:issueId`, so agents send identifiers. A value that does not identify an issue returns a 4xx. **Steps to reproduce** 1. Create an issue. Note its identifier (for example `ABC-12`). 2. Send `GET /api/issues/ABC-12/workspace` as a board user or as an agent. 3. See `500 Internal server error` and the UUID syntax error in the server log. 4. Send the same request with the issue UUID. See 200. **Paperclip version or commit** Seen on release `2026.1106.0`. The route code is the same on `master` at `4f33bb357`. **Deployment mode** Authenticated, private exposure. Installed from npm. Embedded Postgres. **Agent adapter(s) involved** Not adapter-specific (core bug). An agent found it with the Claude Code adapter. ## What Changed - `server/src/routes/task-workspaces.ts`: the shared `task()` lookup now uses `normalizeIssueIdentifier` and `isUuidLike` from `@paperclipai/shared`. An identifier matches `issues.identifier`. A UUID matches `issues.id`. Any other value returns `404 Task not found` before the query. - The lookup keeps the same read condition (`issueReadSqlCondition`) and the same access checks. An identifier that the actor cannot read gets the same 404 as a UUID that the actor cannot read. - `server/src/__tests__/task-workspace-binding.test.ts`: one new test. It reads and selects a workspace with the identifier (in lower case for the read), and checks 404 for `not-a-task` and for an unknown identifier. - No documentation change. The skill reference already writes these routes with `:issueId`. ## Verification - Red before the fix: `npx vitest run src/__tests__/task-workspace-binding.test.ts -t "resolves an issue identifier"` failed with `expected 500 to be 200`. - Green after the fix: the same command passes. - The three test files that mount these routes pass: `task-workspace-binding.test.ts`, `heartbeat-repository-catalog-recovery.test.ts` and `workspace-preparation-history.test.ts` (117 tests). - `pnpm --filter @paperclipai/server typecheck` passes. - `pnpm verify --base upstream/master` keeps 2,498 test files, because `server/src/app.ts` imports the route. I stopped the local run after 75 minutes, in the first of its 10 jobs. In that part, four files failed: `file-resources-git-scan-load`, `git-restore-point-expiration`, `git-workspace-expiration` and `issue-recovery-assignments`. None of them imports the changed route or `app.ts`. On the base revision, the same four files also stall on this machine: a test database connection stays idle in a transaction, and the cleanup waits for its lock. - The CI checks on this pull request run the full suite. All checks pass. - Manual check on a release `2026.1106.0` instance before the fix: identifier 500, UUID 200, `not-a-task` 500. ## Risks - Low risk. The change touches one lookup in one route file. - A UUID still matches by id, with the same checks. `isUuidLike` accepts UUID versions 1 to 5. Issue ids come from `gen_random_uuid()` (version 4). - A value that is not a UUID and not an identifier now returns 404 instead of 500. No client can depend on the old 500. ## Model Used - Anthropic Claude Opus 5.5 (`claude-opus-5-5`). Capabilities: extended reasoning, tool use and code execution. The harness does not show the context window size. ## 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 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
55 lines
3.7 KiB
TypeScript
55 lines
3.7 KiB
TypeScript
import { and, eq } from "drizzle-orm";
|
|
import { issues, type Db } from "@paperclipai/db";
|
|
import { Router, type Request } from "express";
|
|
import { isUuidLike, normalizeIssueIdentifier, prepareWorkspaceRepositorySchema, selectTaskWorkspaceSchema } from "@paperclipai/shared";
|
|
import { accessService } from "../services/access.js";
|
|
import { executionWorkspaceService } from "../services/execution-workspaces.js";
|
|
import { issueReadSqlCondition } from "../services/authorization.js";
|
|
import { projectToolContext } from "../services/project-tool-context.js";
|
|
import { logActivity } from "../services/activity-log.js";
|
|
import { forbidden, notFound } from "../errors.js";
|
|
import { assertCompanyAccess, getActorInfo, hasCompanyAccess } from "./authz.js";
|
|
import { validate } from "../middleware/validate.js";
|
|
|
|
export function taskWorkspaceRoutes(db: Db) {
|
|
const router = Router();
|
|
const workspaces = executionWorkspaceService(db);
|
|
async function task(req: Request, write = false) {
|
|
const ref = String(req.params.id).trim();
|
|
const identifier = normalizeIssueIdentifier(ref);
|
|
if (!identifier && !isUuidLike(ref)) throw notFound("Task not found");
|
|
const match = identifier ? eq(issues.identifier, identifier) : eq(issues.id, ref);
|
|
const [issue] = await db.select().from(issues).where(and(match, await issueReadSqlCondition(db, req.actor)));
|
|
if (!issue) throw notFound("Task not found");
|
|
if (!hasCompanyAccess(req, issue.companyId)) throw notFound("Task not found");
|
|
assertCompanyAccess(req, issue.companyId);
|
|
const decision = await accessService(db).decide({ actor: req.actor, action: write ? "issue:mutate" : "issue:read", resource: {
|
|
type: "issue", companyId: issue.companyId, issueId: issue.id,
|
|
status: issue.status, assigneeAgentId: issue.assigneeAgentId, assigneeUserId: issue.assigneeUserId,
|
|
} });
|
|
if (!decision.allowed) throw forbidden("Task workspace is outside this actor's authorization boundary");
|
|
if (write && req.actor.type === "agent") {
|
|
const context = await projectToolContext(db, req.actor, true, "Workspace");
|
|
if (context.issue.id !== issue.id) throw forbidden("Workspace selection is scoped to the current task");
|
|
}
|
|
return issue;
|
|
}
|
|
router.get("/issues/:id/workspace", async (req, res) => {
|
|
const issue = await task(req);
|
|
const view = await workspaces.inspectTaskWorkspace(issue.companyId, issue.id, req.actor);
|
|
res.json({ ...view, repositories: issue.executionWorkspaceId ? await workspaces.listTaskRepositories(issue.companyId, issue.executionWorkspaceId) : [], capabilities: { prepareRepository: view.workspace?.providerType === "adapter_managed" ? "unsupported" : "next_normal_admission", selectWorkspace: "next_normal_admission" }, fileLocations: { taskFiles: "workspace", personalInstructionsAndMemory: "AGENT_HOME", providerHome: "runtime_managed" } });
|
|
});
|
|
router.put("/issues/:id/workspace", validate(selectTaskWorkspaceSchema), async (req, res) => {
|
|
const issue = await task(req, true);
|
|
const result = await workspaces.selectTaskWorkspace({ companyId: issue.companyId, issueId: issue.id, actor: req.actor, ...req.body });
|
|
const actor = getActorInfo(req);
|
|
await logActivity(db, { companyId: issue.companyId, ...actor, action: "issue.workspace.selection_requested", entityType: "issue", entityId: issue.id, details: { selection: req.body.selection, applies: "next_normal_admission" } });
|
|
res.json(result);
|
|
});
|
|
router.post("/issues/:id/workspace/repositories", validate(prepareWorkspaceRepositorySchema), async (req, res) => {
|
|
const issue = await task(req, true);
|
|
res.json(await workspaces.requestTaskRepository({ companyId: issue.companyId, issueId: issue.id, actor: req.actor, request: req.body }));
|
|
});
|
|
return router;
|
|
}
|