Files
PaperClipAI/server/src/services
DottaandPaperclip 0791da0cc5 fix(server): synchronize routine history during native status commit (#18033)
<!-- entire-trail-link-start -->
https://entire.com/gh/paperclipai/-/trails/173/changes/paperclip/173
<!-- entire-trail-link-end -->

## Thinking Path

> - Paperclip manages work assigned to AI agents.
> - A routine stores each invocation and its execution task.
> - Native completion commits task status without using the public issue
route.
> - That route synchronizes routine history, but the native status
transaction did not.
> - A successful task could remain listed as an unfinished routine
invocation on a local instance.
> - This pull request uses the existing database projection in the
native status transaction.
> - Task status and routine history now commit or roll back together.

## Linked Issues or Issue Description

**What happened?**

A real routine execution completed successfully. Its task had status
`done` and retained its routine origin and invocation identity. Public
routine history still returned `issue_created`, with `completedAt: null`
and a linked task whose status was `done`. The existing five-minute
reconciliation runs only on cloud-managed instances. It does not close
this path for a local instance.

**Expected behavior**

An authoritative native task status change must update the matching
routine invocation through the existing routine status rules. It must
not update another task, routine or company.

**Steps to reproduce**

1. Create a routine on a local instance and run it through the public
routine API.
2. Let its native execution finish and commit the task as done.
3. Read the routine run history. Before this change, the invocation can
remain `issue_created` while its linked task is done.

**Paperclip version or commit**

The retained failure used merged production `39e811c9c5`. The missing
hook is also present on this branch's base, `8049fbf51d`.

Related: #16633 contains the existing cloud-only reconciliation. A
bounded search found no duplicate native synchronization fix.

## What Changed

- Synchronize routine history inside the native status transaction after
the task status projection.
- Require matching company, invocation and linked task before
synchronization. Preserve supported custom display origins.
- Add database-backed tests for terminal and active states, replay,
invalid custody, rollback and a real plugin-managed routine dispatch
with a custom origin.
- Keep the routine status projection in a database-only helper. Both the
routine service and native status committer use it without loading
heartbeat execution from the committer.

## Verification

- Own-source server TypeScript check passed after the helper extraction.
It includes the new test source and production dependencies. Local
package exports point at another checkout, so the check used explicit
paths to this checkout's source and disabled declaration emit.
- Existing pure `status-arbiter.test.ts`: 20 tests passed. `git diff
--check` passed.
- Initial required CI run 40711603938 built and typechecked
successfully. It executed all ten new database tests: nine passed; the
public-dispatch fixture failed because it assumed status version zero.
The fixture now reads the saved version and decision binding.
- That CI run also exposed a heartbeat mock dependency cycle. The
database-only helper removes the
committer-to-routine-service-to-heartbeat import. Required CI on the new
head must confirm both repairs.
- Initial browser shard 3/8 failed during cleanup: legacy run
cancellation waited for a heartbeat row lock. Its cause is under
investigation; the failure remains recorded.
- A local runtime-execution check failed during module import from stale
package exports, before any tests or database setup ran. It is not
passing evidence. No local database, provider or live eval was run.
- Latest-head required CI run 40717315212 executed all ten new database
tests successfully in server shard 5/12 (2,163 passed; three unrelated
skips). The heartbeat runtime suite executed 23 tests successfully in
server shard 9/12 (1,704 passed).
- Latest-head browser shard 3/8 passed all 33 tests. This does not
establish the cause of the preserved earlier cleanup lock timeout.
- Required CI run 40717315212 completed successfully on head
`2d77f4302b36303741abad07266bbe9e376ba6a5`. All 48 workflow jobs are
green after one failed-jobs-only infrastructure retry. The first attempt
had seven explicit runner shutdowns and three lost-runner communication
failures. The exact chat shard host was independently confirmed as an
AWS Spot termination. Original logs and annotations remain retained.
- Greptile completed at 5/5 on this exact head, with no outstanding
findings. All review threads are resolved. The independent follow-up
review found no issues.
- No live qualification or corrected eval pass is claimed. The PR
remains open and unmerged, with auto-merge off.

## Risks

- Matching routine executions perform the existing bounded status
synchronization queries inside the status transaction. A database
failure rolls back both projections.
- Inconsistent invocation or linked-task metadata now prevents
synchronization. No unrelated invocation is inferred or repaired.
- The existing scheduled repair remains unchanged. This PR does not
change accounting, provider behavior or eval budgets.

## Model Used

- OpenAI `gpt-6.1-sol`, with reasoning and tool-assisted source
inspection and coding. The session did not supply a 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 references)
- [x] My branch name describes the change and contains no internal
ticket id or instance-derived details
- [x] I have run relevant credential-free tests locally and they pass;
database coverage is pending CI
- [x] I have added or updated tests where applicable
- [x] I have checked documentation; the existing routine status contract
is unchanged
- [x] I have considered and documented 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-28 11:09:49 -05:00
..
…
…
…