Files
PaperClipAI/doc
DottaandPaperclip 56bc470ab2 perf(server): project continuation history result fields (#17930)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - A continuation carries completed tool receipts so the agent can
avoid repeat actions.
> - It also checks saved conversation and workspace recovery fields
before choosing a prior terminal run.
> - The history query transfers every result field for every matching
run.
> - Native output and unused receipt request fields can make this read
large.
> - This pull request selects the fields those consumers use and keeps
each completed receipt result in full.
> - The benefit is less database transfer with the same history,
recovery decisions, and completed-action evidence.

## Linked Issues or Issue Description

**What existing behavior does this improve?**

The history read in `buildExecutionContinuation`.

**Current behavior**

The query returns complete `result_json` values for all matching runs.
Consumers read completed receipt state, operation ID and result, plus
conversation and workspace recovery controls. Large native output and
receipt request fields cross the connection without being used.

**Proposed behavior**

Select only those result fields in PostgreSQL. Keep typed policy and
recovery values. Keep all eligible completed receipts and their complete
result values. Retain missing versus null receipt results, run and
receipt order, company/task/agent scope, and the source-run exception.
Keep the full retained history without a new limit.

**Reason and benefit**

The synthetic wide-output fixture removes 99.879% of this query's
DataRow bytes. A required 2 MiB receipt-result control saves only
0.0074%, because that complete result is needed. Actual savings depend
on result contents; the wide-output result is not a forecast for all
deployments.

**Breaking changes**

None intended. No schema, API, migration, workflow, history limit, or
receipt truncation changes.

**Additional context**

Related: #17481 projects a separate recovery read, and #17896 narrows
explicit continuation admission. This PR covers the continuation history
query and has no dependency on the other byte reduction PRs.

## What Changed

- Project completed tool receipts to their state, operation ID and
complete result. Omit fields that the continuation does not read.
- Project typed conversation/workspace controls and the recovery schema.
Keep the shared policy helper unchanged.
- Add real PostgreSQL comparisons for complete continuation envelopes,
ordering, scope, legacy JSON, recovery flags, and receipt result
preservation.
- Add external isolated database support to the existing continuation
tests and document a reproducible query benchmark.

## Verification

- Pinned baseline: `3cd3af5d75f200185ed4d9703a3145594a489b13`.
- PostgreSQL 17.11 with synthetic local data. The harness captures SQL
from the real baseline and treatment services. Imported helpers remain
unchanged.
- Command: `pnpm --filter @paperclipai/server exec tsx
../scripts/benchmark-continuation-history.ts
--baseline-ref=3cd3af5d75f200185ed4d9703a3145594a489b13`. Set
`PAPERCLIP_CONTINUATION_HISTORY_BENCHMARK_DATABASE_URL` to a disposable
database ending in `_test` to use an external server. The default uses
disposable embedded PostgreSQL.
- The baseline was measured before the production edit with
`--baseline-only`. The treatment compares complete envelopes, every
retained scalar column, completed receipts, and shared policy decisions.
The wide unused-output fixture must remove more than 90% of DataRow
bytes or the benchmark fails.
- Wide unused-output fixture: 25 runs with independent incompressible
192 KiB native output and 32 KiB unused receipt request fields. DataRow
bytes: 5,744,950 to 6,925 (99.879% less).
- Required-result control: one complete 2 MiB receipt result. DataRow
bytes: 2,097,556 to 2,097,401 (0.0074% less). The whole result is
checked for equality.
- Exact byte boundary: PostgreSQL text-format DataRow frames, including
field length prefixes. Excludes RowDescription, command metadata, TLS,
TCP, and other service queries.
- Seven measured query round trips per variant follow two warmups and
alternate order. Final-head wide-output median: 32.86 to 4.41 ms (ranges
26.24–35.54 and 3.62–5.21 ms). Required-result median: 17.01 to 20.60 ms
(ranges 16.30–32.89 and 18.85–22.48 ms). The required-result control
shows projection overhead and overlapping timing ranges; it has no
latency improvement claim. These are local measurements, not deployed
latency results.
-
`PAPERCLIP_EXECUTION_CONTINUATION_TEST_DATABASE_URL=<isolated_test_database>
pnpm exec vitest run server/src/services/execution-continuation.test.ts
server/src/services/execution-continuation-history-read.test.ts
--maxWorkers=1`: 55 tests pass in PostgreSQL 17.
- Cases cover numeric receipt key order, empty operation IDs, malformed
receipt entries, missing versus null results, complete large results,
malformed root JSON, all closed workspace restore failure codes, invalid
recovery schemas, cancelled admission rows, and
company/task/agent/source-run boundaries.
- `pnpm -r typecheck` and `pnpm build` pass on final head
`d665d9f5c47b237c81b9cc80fdec1d39ad90754f`. The final-head focused
PostgreSQL run passes all 55 tests. The final benchmark records that
SHA, an empty source diff, and passing equivalence/byte assertions. The
first typecheck found optional array accesses in new test assertions;
explicit required-array assertions fixed them. Local full `pnpm
test:run` is not claimed because the host embedded PostgreSQL runtime is
unavailable; the full test matrix runs in CI. Exact-head Greptile review
completed success at 5/5 with no actionable findings or unresolved
threads. The full final-head CI matrix passes. All 58 current-head
checks are terminal: 54 success, two neutral, and two optional Storybook
skips. One original server shard ended with a canceled test step and no
assertion failure; its single targeted retry passed. No product change
was needed for that interruption.

## Risks

- Future continuation consumers must add any newly required result field
to this projection. Existing shared policy semantics stay in the helper.
- Completed receipt results can still be large and can dominate
transfer. This PR preserves them without a cap. The receipt-heavy
control adds JSON projection work for only 155 bytes of savings, so it
can be slower despite equal output.
- JSON malformed-value and property-presence rules affect action
evidence and recovery. Real PostgreSQL comparisons check those decisions
and complete output.
- Production transfer and latency improvements need a fresh deployed
capture. Nothing was merged or deployed by this work.

## Model Used

OpenAI GPT-6 through Codex. The exact runtime model ID and context
window are not exposed by this session. The work used repository tools,
code execution, and real PostgreSQL validation.

## 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

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-10-27 16:30:37 -05:00
..
…
…
…
…