Commit Graph
2 Commits
Author SHA1 Message Date
Dotta ba63f4f8b2 perf(test): split chat-channels monolith into 16 suites and shrink PR chat shards 5 -> 2 (#22838)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - `server/src/__tests__/chat-channels.integration.test.ts` had grown
into an 86,498-LOC single-file monolith (~1,815s total CPU across 5
serialized line-sharded PR runners) that forced special-case line
sharding in `scripts/run-vitest-stable.mjs`, prevented `general-chat`
from participating in file-level test selection, and pinned 5 PR CI
shards even for single-provider chat edits.
> - This pull request splits the monolith into 16 provider- and
domain-scoped integration suites plus 4 `<1,000`-LOC shared harness
helpers (`harness.ts`, `providers.ts`, `github-harness.ts`,
`retry-fixtures.ts`), optimizes redundant setup/polling overhead
(`-41.8%` net LOC, `-70.9%` CPU-min: `1,815.3s -> 528.4s` with every
original assertion and matrix case preserved), switches `general-chat`
to file-level duration-balanced LPT sharding (`265.1s / 263.3s` across 2
shards), wires `general-chat` into `test-selection` and `local-verify`,
and shrinks the PR matrix from 5 chat shards to 2 (`general_tests`: `30
-> 27`).
> - Every provider (GitHub, Discord, Slack, Teams, Telegram) and shared
core behavior retains full assertion coverage while ordinary chat edits
now select only affected provider suites instead of all 5 shards.

## Linked Issues or Issue Description

Refs #22804

**What existing behavior does this improve?**
Test execution time, maintainability, and incremental test selection for
`server/src/__tests__/chat-channels/**`,
`scripts/run-vitest-stable.mjs`, and `.github/workflows/pr-trusted.yml`.

**Subsystem affected**
server/ — chat-channels integration tests & CI test runner/matrix

**Current behavior**
`server/src/__tests__/chat-channels.integration.test.ts` is an
86,498-LOC monolith requiring custom source-line sharding across 5 PR CI
jobs (`chat (1/5)`..`chat (5/5)`) and bypassed by file-level test
selection.

**Proposed behavior**
Replace the monolith with 16 focused provider/domain suites + 4 shared
harness modules, use standard file-level duration-balanced LPT sharding
with `scripts/chat-shard-durations.json`, include `general-chat` in
incremental test selection (`select-tests.mjs`,
`test-selection-plan.mjs`, `local-verify/plan.mjs`), and run
`general-chat` on 2 PR shards (`chat (1/2)`, `chat (2/2)`).

## What Changed

- **Split & optimized `chat-channels.integration.test.ts` (`86,498 LOC`
→ 16 provider/domain suites + 4 `<1,000`-LOC harness modules, `-37,314`
net LOC / `-70.9%` CPU-min):**
- Shared harnesses: `harness.ts` (957 LOC), `providers.ts` (857 LOC),
`github-harness.ts` (817 LOC), `retry-fixtures.ts` (905 LOC).
- 16 integration suites covering Board/Core (1), Discord (2), GitHub
(4), Slack (4), Teams (2), and Telegram (3), totaling `528.4s` CPU (down
from `1,815.3s`), preserving all 20 stopped-workspace-cleanup and 28
response-maintenance GitHub matrix cases.
- **File-level LPT sharding & test selection for `general-chat`:**
- Updated `scripts/run-vitest-stable.mjs` (947 LOC),
`scripts/chat-shard-durations.json`,
`scripts/ci/refresh-chat-shard-durations.mjs`,
`scripts/ci/select-tests.mjs`, `scripts/ci/test-selection-manifest.mjs`,
`scripts/ci/test-selection-plan.mjs`, `scripts/local-verify/plan.mjs`,
and `scripts/general-server-shard-durations.json`.
- Updated `maps/agent-access-map/chat-channels.md`,
`maps/feature-map/{chat-channels,connection-setup,recovery}.md`,
`server/scripts/benchmark-response-presentation.mjs` (including
`../../services/native-runtime/...` relative baseline import), and
`server/scripts/test-slack-sync-protocol.mjs`.
- **Shrank PR `general-chat` matrix from 5 shards to 2 (`general_tests`:
`30 -> 27`):**
- Updated `.github/ci-task-matrix.json`,
`.github/workflows/pr-trusted.yml` fallback matrix, and
`.github/scripts/tests/ci-plan.test.mjs` (`30 -> 27`).
  - Kept `release-verify.yml` at 3 chat shards (LPT-balanced).

## Verification

- `node --test .github/scripts/tests/ci-plan.test.mjs
scripts/__tests__/run-vitest-stable-shard.test.mjs
scripts/__tests__/run-vitest-stable-pr-mode.test.mjs
scripts/__tests__/local-verify.test.mjs
scripts/__tests__/release-verify-workflow.test.mjs
scripts/ci/test-selection*.test.mjs` — **197/197 PASS**
- `node scripts/check-maps.mjs` — **PASS** (35 areas, 192 capability IDs
aligned)
- `node scripts/check-test-budget.mjs --base origin/master` — **PASS**
(105 nightly suites validated, 17 test files checked)
- `node server/scripts/typecheck-layers.mjs` — **0 errors** across all 7
layers
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/chat-channels/github-review-workflow.integration.test.ts`
— **89/89 PASS** (`171.27s`)
- Live shard execution (`node scripts/run-vitest-stable.mjs --mode
general --group general-chat --shard-index 0 --shard-count 2`) — **8/8
files, 455/455 tests PASS in 104.0s**

## Risks

- Low risk: all 16 split suites preserve full PostgreSQL-backed
integration assertions across every chat provider, `general-chat` file
sharding gracefully handles arbitrary shard counts (including
`release-verify.yml`'s 3 shards and over-sharded empty shards), and full
CI validates all chat suites live before merge.

## Model Used

- Google Gemini (Antigravity agent swarm) with extended reasoning, tool
use, and local test execution (`ui-prune-2`, `vitest-runner`,
`verifier`, `ci-pipeline`).

## 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
2026-11-24 17:47:05 -06:00
DottaandPaperclip bf7f77d8cd fix: avoid response recovery self-contention (#18715)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The server must present accepted agent responses as comments under
current chat authority.
> - The recovery worker can check four tasks at the same time.
> - Different tasks can share a chat endpoint or external principal.
Their authorization transactions then compete for endpoint or identity
locks.
> - A failed transaction leaves the accepted response pending and
repeats its source queries on a later retry.
> - This pull request groups checks that share a task, endpoint or
principal before dispatch. It keeps every transactional authority check
and lock.
> - The benefit is less failed recovery work and prompt, exactly-once
presentation of valid accepted responses.

## Linked Issues or Issue Description

Refs #17714 for a separate publication-commit lock-order fix. Refs
#18686 for separate run-list JSON work. Neither changes this recovery
admission path.

**What happened?**

A response-presentation worker reported `could not obtain lock on row in
relation "chat_endpoints"`. Four real PostgreSQL recovery fixtures
reproduced three such failures in one pass with no external ingress. The
worker grouped by task, while authorization locks shared endpoints and
principals. Review also exposed two valid Slack endpoints in one
workspace that share one principal and compete for its try-advisory
lock.

**Expected behavior**

Recovery checks that share an endpoint or principal should retain task
order without competing with one another. Unrelated endpoints should
make progress. The server must still reject stale authority and retry
legitimate external contention.

**Steps to reproduce**

Run the shared-endpoint regression or the portable comparison helper
linked below. The fixture creates four accepted historical responses for
distinct tasks and principals on one actual endpoint.

## What Changed

- Read optional endpoint and principal scheduling hints through a
company-scoped join to the canonical wake action. The join does not
change eligibility or grant authority.
- Extend existing bounded check groups to merge intersecting resource
keys. Preserve task order, transitive conflicts, independent-group
progress, shutdown, drain and pending scopes.
- Add real PostgreSQL controls for shared endpoints, a principal shared
across two endpoints, an observed blocked transaction with independent
endpoint progress, external locks, exact selected output, and endpoint
revocation despite a forged hint.
- Add a portable helper that materializes the pinned baseline with `git
show`, runs the existing real fixture, retains results, and restores
temporary files.

## Verification

- Final principal-extension head
`57047441b28765ed1cf49b8924efc69f5c03dcb4`: 34 focused scheduler/chat
controls passed on Node 24.22.2 / pnpm 9.15.4. Independent exact-head
source review is green. Full local `pnpm -r typecheck` and `pnpm build`
pass on this head. Fresh Greptile and Paperclip reviews are both 5/5
with no findings. All 59 final-head contexts are complete: 55 success, 3
skipped, and the neutral Paperclip review at 5/5. There are no failures
or pending checks.
- Earlier endpoint-only head `eea437e7edbe7eb41eb880d4d8fd6d99a9c247da`:
full local typecheck/build and all remote CI passed. The redundant local
full suite was interrupted for this review fix after 956 passing chat
cases; it is not a full local pass.
- Final shared-principal counts-only comparison completed the same two
exact responses: product calls 165 → 147, rows 114 → 100, outer
contention failures 1 → 0. SQL query errors stayed zero; advisory-lock
refusal is not a failed SQL query.
- The earlier endpoint-only study completed five alternating pairs / ten
measured runs: median calls 443 → 293, rows 314 → 200, query errors 6 →
0 for four exact responses. Its instrumented shared-host CPU data
predates the principal extension and is not a final-head or hosted CPU
claim. Retry clocks were accelerated; wall time is not hosted latency.
- [Raw samples, final principal work counts, portable commands and
failure
accounting](https://github.com/paperclipai/paperclip/blob/57047441b28765ed1cf49b8924efc69f5c03dcb4/doc/performance/2026-11-03-response-presentation-contention.md).
- Initial server4 CI reported three provider calls instead of two in the
unchanged Anthropic identity fixture. The exact local case passed; the
full 88-case file passed with observations showing the two expected
identity-route calls. The third CI call's origin remains unknown. One
bounded rerun of the failed job and its dependent CI aggregate passed.
No assertion or source change was made for that failure.
- The causal benchmark baseline worker/query is
`82c033ed127d07862e6765aa4b69a3cf339bde81`. The PR base is `36df8933f0`.
They are not claims about the currently serving revision.

## Risks

- Connected resource groups can reduce parallelism when tasks link
several endpoints. This preserves the existing task ordering contract.
- Missing or stale endpoint/principal hints retain current transactional
checks. Other workers and ingress can still cause legitimate NOWAIT
retries.
- There is no schema, API, permission or provider-transport change.
Endpoint revocation and company isolation remain enforced by their
existing owners.

## Model Used

OpenAI GPT-6.1 Sol (`gpt-6.1-sol`), with tool use and code execution,
produced and reviewed this change. The runtime does not expose 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

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-03 06:04:25 -06:00