Add count, newest-first and lean run reads to customer-success inspection (#22781)

<!-- Write all pull request text in Simplified Technical English
(ASD-STE100): short sentences, one instruction per sentence, simple
approved vocabulary, and the active voice. -->

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Paperclip Cloud can inspect a tenant stack read-only through the
customer-success inspection API
> - That API can only list rows in id order. It has no totals, and each
run row holds large JSON columns
> - A customer-success agent must page every run of a stack to find the
latest run or the failed-run total
> - That is too slow and too large for a refresh across many stacks
> - This pull request adds a `count` operation, a creation-time `order`
for lists, and a lean `summary` view for runs
> - The benefit is one small read per metric, with the same read-only,
company-scoped rules

## Linked Issues or Issue Description

**Subsystem affected**

Server: the customer-success inspection API (`/api/customer-success/v1`)
and its shared query contract.

**Problem or motivation**

Customer-success inspection reads cannot answer summary questions. To
get the latest run, the run count or the failed-run count, a reader must
page through every run. Each run row can be hundreds of KB, because it
holds the result, usage, context snapshot and runner profile.

**Proposed solution**

Add read-only summary reads to the inspection contract: a `count` that
groups by status, a newest-first or oldest-first `order` for `list`, and
a `summary` view of runs without the large JSON columns.

**Alternatives considered**

Paging all runs with a smaller page size. That still reads every large
row. A new aggregated endpoint outside the reader catalog. That would
skip the existing catalog, permit and transaction rules.

## What Changed

- `packages/shared/src/customer-success.ts`: add the `count` operation,
`order` (`newest`/`oldest`, `list` only), `view` (`summary`, `runs`
list/get only), `InspectionCount`, and `inspectionReadPath()`. The
parser rejects these fields where they do not apply.
- `server/src/services/customer-success-inspection.ts`: `count` returns
`{total, byStatus}` (grouped by the reader's `status` column, or
`byStatus: null`). `order` sorts by `createdAt`, then id. `view:
"summary"` selects the `RUN_SUMMARY_COLUMNS` allowlist and adds
`durationMs`. All reads stay in the same read-only, repeatable-read
transaction and keep the existing run redaction.
- `server/src/routes/customer-success.ts`: serve the same handler on
`/read` and the new `/read-v2`. `/read` refuses queries that use the new
features. A tenant without `/read-v2` answers 404, so Cloud fails closed
with `tenant_inspection_version_unsupported`.
- `server/src/__tests__/customer-success.test.ts`: tests for counts,
company scope, newest-first paging, the summary allowlist and duration,
parser bounds, and the path split.
- `server/src/__tests__/customer-success-cloud.test.ts`: the registry
double now has the batch reads (`getStacks`, `getAccounts`) that the
current Cloud broker uses. The test also runs a count and a newest-first
summary read end to end.
- `server/src/__tests__/openapi-routes.test.ts`: list `/read-v2` with
the other inspection protocol routes.
- `doc/CUSTOMER-SUCCESS-INSPECTION.md`: document the summary reads and
the path split.

## Verification

- `cd server && pnpm exec vitest run
src/__tests__/customer-success.test.ts
src/__tests__/openapi-routes.test.ts
src/__tests__/customer-success-run-liveness.test.ts`: 78 tests pass.
- Red check: the new count/order/summary test fails against the previous
service (`Unsupported inspection operation`).
- Coordinated qualification with a local build of the Cloud broker
branch: `PAPERCLIP_INSPECTION_CLOUD_DIST=… pnpm exec vitest run
src/__tests__/customer-success-cloud.test.ts` passes, including the
count and newest-first summary reads over `/read-v2`.

## Risks

- Low risk. The new features are read-only and use the existing catalog,
company boundary, permit check and read-only transaction.
- The summary view is an allowlist. A new run column stays out of the
summary until someone adds it.
- Mixed versions are safe in both orders. An older Cloud never sends the
new fields. A newer Cloud sends them only to `/read-v2`, and an older
tenant answers 404 there.
- No migration.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use
(shell, file edits) and extended thinking.

## 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
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-11-24 11:41:16 -06:00
1 parent 11cfe0910d
commit 4daeab5c76
8 files changed
+355 -15

No files matched your search

+42 -2
View File
@@ -65,10 +65,28 @@ export const INSPECTION_RESOURCES = [
"threads",
] as const;
export type InspectionResource = (typeof INSPECTION_RESOURCES)[number];
export const INSPECTION_ORDERS = ["newest", "oldest"] as const;
export type InspectionOrder = (typeof INSPECTION_ORDERS)[number];
export const INSPECTION_VIEWS = ["summary"] as const;
export type InspectionView = (typeof INSPECTION_VIEWS)[number];
/** The tenant path for queries that use only the original (version 1) features. */
export const INSPECTION_READ_PATH = "/read";
/**
* The tenant path for queries that use `count`, `order` or `view`. A tenant
* that predates them answers 404 here, so Cloud fails closed with
* `tenant_inspection_version_unsupported` instead of misreading the query.
*/
export const INSPECTION_EXTENDED_READ_PATH = "/read-v2";
export interface InspectionCount {
total: number;
/** Counts grouped by the resource's `status` column, or null when it has none. */
byStatus: Record<string, number> | null;
}
export interface InspectionQuery {
operation:
| "list"
| "get"
| "count"
| "instructions.file"
| "skills.file"
| "runs.log"
@@ -85,6 +103,10 @@ export interface InspectionQuery {
limit?: number;
offset?: number;
range?: { start: number; end: number };
/** `list` only: order by creation time instead of by id. */
order?: InspectionOrder;
/** `list`/`get` on `runs` only: leave out the large JSON columns. */
view?: InspectionView;
context?: {
projectId?: string;
workspaceId?: string;
@@ -129,6 +151,11 @@ export function canonicalJson(value: unknown): string {
}
throw new Error("Invalid JSON value");
}
export function inspectionReadPath(query: InspectionQuery): string {
return query.operation === "count" || query.order !== undefined || query.view !== undefined
? INSPECTION_EXTENDED_READ_PATH
: INSPECTION_READ_PATH;
}
export function parseInspectionQuery(value: unknown): InspectionQuery {
if (!value || typeof value !== "object" || Array.isArray(value))
throw new Error("Invalid inspection query");
@@ -144,6 +171,8 @@ export function parseInspectionQuery(value: unknown): InspectionQuery {
"limit",
"offset",
"range",
"order",
"view",
"context",
]);
if (Object.keys(q).some((k) => !allowed.has(k)))
@@ -152,6 +181,7 @@ export function parseInspectionQuery(value: unknown): InspectionQuery {
![
"list",
"get",
"count",
"instructions.file",
"skills.file",
"runs.log",
@@ -169,11 +199,21 @@ export function parseInspectionQuery(value: unknown): InspectionQuery {
if (q.resource !== undefined && !INSPECTION_RESOURCES.includes(q.resource))
throw new Error("Unsupported inspection resource");
if (q.operation === "get" && !q.resourceId) throw new Error("resourceId required");
if (["get", "list"].includes(q.operation) && !q.resource) throw new Error("resource required");
if (["get", "list", "count"].includes(q.operation) && !q.resource)
throw new Error("resource required");
if (!(q.operation === "list" && q.resource === "companies") && !q.companyId)
throw new Error("companyId required");
if (!["get", "list"].includes(q.operation) && !q.resourceId)
if (!["get", "list", "count"].includes(q.operation) && !q.resourceId)
throw new Error("resourceId required");
if (q.operation === "count" && q.resourceId !== undefined)
throw new Error("count does not take resourceId");
if (q.order !== undefined && (q.operation !== "list" || !INSPECTION_ORDERS.includes(q.order)))
throw new Error("Invalid order");
if (
q.view !== undefined &&
(!["list", "get"].includes(q.operation) || q.resource !== "runs" || !INSPECTION_VIEWS.includes(q.view))
)
throw new Error("Invalid view");
if (
q.path !== undefined &&
(typeof q.path !== "string" || q.path.length > 1024 || q.path.includes("\0"))