Files
PaperClipAI/packages/plugins
Tambria Kemp 66b97fb490 fix(plugin-sdk): do not stamp a finished invocation on a worker-to-host call (#17797)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Plugins run in a worker process. The host issues an invocation for
each request or event it delivers, and the SDK stamps that invocation id
on every worker-to-host call so the host can resolve the call's company.
> - Node propagates the ambient AsyncLocalStorage context into a timer
callback. A `setTimeout` or `setInterval` armed inside a handler
therefore still sees the invocation after the handler returned.
> - The SDK stamped it anyway. The host looked the id up, missed it, and
refused the call as "a missing, expired, or unknown invocation scope".
> - The host resolves a call that carries no id by the company the call
names. So a call that claimed nothing would have been allowed. A call
that claimed a dead invocation was not.
> - The symptom is quiet. The plugin's feature still works, but every
host call from its timer fails, and the work arrives late or not at all.
> - This pull request stops the stamp at the moment the handler returns.
The worker is the only side that knows when that happened.
> - The benefit is that a plugin author does not have to discover this
trap. Scheduled and deferred work in a plugin now behaves as the
specification describes.

## Linked Issues or Issue Description

Fixes: #17532

Related issues, each a different cause behind the same error string.
This change does not fix them:

- Refs #15765 — the host-side mirror. There the call carries **no** id,
and the `activeInvocations.size > 0` rule refuses it. Here the call
carries a **dead** id, and the lookup refuses it. Each fix leaves the
other bug standing.
- Refs #8633 — a polling loop started in `setup()`, which has no
invocation to inherit.
- Refs #9310 — `runJob` carries no scope.
- Refs #7897 — an SDK version that never echoes the id.

Related pull requests. I read all three before writing this one:

- Refs #10580 — the opt-in form of this fix. It adds
`ctx.runtime.runProactively(cb)` so an author can detach deliberately.
The two compose: that API lets an author drop the stamp *inside* a live
handler, and this change drops it only after the handler returned. This
one needs no plugin change, which matters because the trap is invisible
to the author.
- Refs #15925 — retires an event invocation scope when the worker
handlers settle. It is complementary, and it makes this fix more
necessary. Today a notification registers its scope with the maximum RPC
timeout and clears it only if the send throws, so a timer armed in an
event handler is admitted for up to 15 minutes. #15925 closes that
window. The dead-stamp refusal then appears on the event path too, and
this change is what prevents it. The two also work together in code:
this change marks the invocation finished before the notification
settles, so the settlement notification that #15925 sends goes out
unstamped and carries its id in the parameters, as it already does.
- Refs #8159 — a 5 second grace period after a scoped RPC settles. It
answers a different question. That window covers a nested request
already in flight when the response lands. It does not cover a timer
that fires seconds or minutes later. It also carries an unrelated
transaction change in `server/src/services/environments.ts`, and it has
conflicted since June.

## What Changed

- `packages/plugins/sdk/src/worker-rpc-host.ts`: a `WeakSet` of finished
invocation contexts, plus `liveInvocation()` at the two stamp sites and
`runInInvocationScope()` at the two scope-entry sites.
- The guard is keyed on the invocation context object, not on the id
string. Each delivery deserializes its own object. A second delivery
that carries the same id keeps its own stamp while its own handler runs.
- Both scope-entry sites are guarded. A host-to-worker notification also
runs its handler inside the invocation scope, and an event handler is a
normal place to arm a timer.
- `doc/plugins/PLUGIN_SPEC.md` gains §12.3.1. The contract said that a
call with no invocation is attributed by the company it names. It never
said which calls have no invocation, and not being able to tell is the
whole trap.
- No host change. No manifest change. No new dependency.

## Verification

```bash
cd packages/plugins/sdk
pnpm vitest run                 # 178 passed, 14 files
pnpm tsc --noEmit -p tsconfig.json   # clean
```

Red before green, which is the part worth checking:

- With the three new tests in place and `src/worker-rpc-host.ts`
restored to `master`, **two of the three fail**. The in-handler call
keeps its stamp and the timer's call keeps it too, which is the bug.
- The third test passes either way by design. It asserts that a live
invocation still stamps, and it is there to prove the guard does not
over-reach.
- The tests drive a real worker over a pair of pipes. The host side
answers every worker-to-host request and records the id on each
`state.get` request and each `execute.log` notification. One case enters
through a request, one through an event notification, and one runs two
deliveries that share an id.

## Risks

Low.

- The behaviour change is narrow. A worker-to-host call made after its
handler returned now goes out with no invocation id, where before it
went out with a finished one.
- The host already handles a call with no id. It resolves the company
the call names against the plugin's proactive company scopes, and it
refuses the call when that company is not configured. So a call that was
refused for a missing scope can now succeed, but only where the operator
already authorized that company. Where the operator did not, the call is
still refused. The line that refuses it changes; the answer does not.
- A call made inside a handler is unaffected. The stamp is dropped only
after the handler settles.
- The `WeakSet` holds no strong reference, so a finished invocation
context is still collected.

## Model Used

Claude Opus 5 (`claude-opus-5`), with extended thinking and tool use.
The model wrote the change and the tests, ran the verification above,
and read the three related pull requests before positioning this one. A
human reviewed all of it.

## 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
- [x] I will address all Greptile and reviewer comments before
requesting merge
2026-12-05 02:27:22 -08:00
..