Files
PaperClipAI/packages/plugins
Jason e7ecb66d42 fix(server): store JSON null for plugin state values (#22409)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Plugins keep key/value state through the plugin SDK `state.set(key,
value)`, and the server stores it in `plugin_state.value_json`
> - The SDK type allows any JSON value, including `null`, but the column
is `jsonb NOT NULL`
> - `plugin-state-store.ts` passes JS `null` to Drizzle, and Drizzle
writes SQL NULL for it instead of JSON `null`
> - So `state.set(key, null)` fails with `null value in column
"value_json" ... violates not-null constraint`, on insert and on
overwrite
> - This pull request writes the JSON literal `'null'::jsonb` for a null
value on both the insert and the conflict-update paths, and rejects
`undefined` with a clear 400 error
> - The benefit is that plugins can store and clear values with `null`
as the SDK contract says

## Linked Issues or Issue Description

Fixes: #22150

## What Changed

- `server/src/services/plugin-state-store.ts`: a `null` value is written
as `'null'::jsonb` for insert and for upsert update. Other values keep
the current parameterized encoding. A top-level `undefined` value now
gets a 400 `badRequest` and does not change the stored row.
- `server/src/__tests__/plugin-state-store-postgres.test.ts`: new tests
on real embedded Postgres. They cover a null insert, value → null →
value, `get` and `list`, instance and company upserts,
plugin/company/namespace isolation, and `undefined` rejection. They also
check `jsonb_typeof(value_json) = 'null'`, so the row holds JSON null
and not SQL NULL.

## Verification

- Red: before the fix, the Postgres tests fail with `PostgresError: null
value in column "value_json" of relation "plugin_state" violates
not-null constraint` (6 failed).
- Green: `pnpm exec vitest run
server/src/__tests__/plugin-state-store-postgres.test.ts` gives `6
passed (6)`.
- `pnpm --filter @paperclipai/server typecheck` passes.

## Risks

- Low risk. The SQL uses only a fixed `'null'::jsonb` literal. Scope
predicates do not change. No schema or migration changes.
- `undefined` now gets a 400 error. Before, it failed with the same
database constraint error, so no working call changes.
- Not in scope: a stored JSON string `"null"` is read back as `null` by
the current driver/Drizzle decoding. This occurs without this change. I
can open a separate issue for it.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Devin (Cognition AI) autonomous coding agent, with tool use and code
execution (shell, pnpm, vitest, embedded Postgres, git). The agent wrote
the change and the tests. A human (the PR author) reviewed 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
- [ ] 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 04:44:09 -08:00
..