Files
PaperClipAI/server/src/services
921d51d877 fix(server): allow plugin managed-agent instructions on a pending-approval agent (#16951)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Plugins can declare managed agents. The server creates them in
`server/src/services/plugin-managed-agents.ts`.
> - A company can require board approval for new agents. A new agent
then starts as `pending_approval`. A `hire_agent` approval must appear
in the board inbox.
> - A managed agent can declare `instructions`. The host writes those
files and updates the agent `adapterConfig`.
> - That update runs on a `pending_approval` agent. The agent update
path refuses it with HTTP 409 (`pending_approval_agent_config_frozen`).
> - The error stops `createManagedAgent` before it creates the approval
and the plugin binding. The agent is stuck. The board has nothing to
approve.
> - This pull request passes `allowPendingApprovalConfigUpdate: true`
for that one update. Other instruction and skill materialization paths
already do this.
> - The benefit is that a managed agent with declared instructions gets
its approval and its binding when the company requires board approval.

## Linked Issues or Issue Description

Refs #11635. That pull request fixed the same root cause. Its author is
@StartupMill. A triage bot closed it as stale on 2026-10-14. It was
never merged. The defect is still present on `master` at cde944e. This
pull request has a smaller scope. See "What Changed" for the difference.

No separate issue exists. This section describes the bug.

**What happened?**

A plugin declares a managed agent that has an `instructions` block. The
company has `requireBoardApprovalForNewAgents` turned on. The host
creates the agent as `pending_approval`. Then it materializes the
declared instructions and updates the agent `adapterConfig`. The update
fails with HTTP 409: "Pending approval agent configuration cannot be
changed before board approval".

The error stops `createManagedAgent` before the `hire_agent` approval
and the plugin binding exist. The agent stays `pending_approval`. No
approval exists for a board user to click. The `adapterConfig` has no
instruction bundle keys. A second reconcile relinks the agent by its
managed marker. It does not create the approval.

**Expected behavior**

The instructions are written. The `hire_agent` approval is created with
the final `adapterConfig` in its payload. The binding is saved. The
board can approve the agent.

**Steps to reproduce**

1. Turn on "require board approval for new agents" for a company.
2. Install a plugin with the `agents.managed` capability and a managed
agent declaration that has `instructions` (`entryFile` and `content`).
The LLM Wiki plugin Wiki Maintainer is one example.
3. Run `managedReconcile` for that agent key (for example, through the
plugin bootstrap).
4. The call fails with 409. The agent list shows the agent as
`pending_approval`. The approvals list has no matching `hire_agent`
entry.

**Paperclip version or commit**

`master` at cde944e.

## What Changed

- `materializeDeclaredInstructions` in
`server/src/services/plugin-managed-agents.ts` passes
`allowPendingApprovalConfigUpdate: true` to `agentSvc.update`. The
approval payload is built from the updated agent, so it carries the
final `adapterConfig`.
- New test in `server/src/__tests__/plugin-managed-agents.test.ts`:
"materializes declared instructions and creates the hire approval when
board approval is required". It checks the agent status, the instruction
file on disk, the approval payload, and that a second reconcile adds no
second approval.

Difference from #11635: that pull request also added a backfill. The
backfill creates a missing hire approval for managed agents that are
already stuck. This pull request does not include it, on purpose.
Greptile found that the backfill is a lookup followed by an insert, so
two overlapping reconciles can create two open approvals for one agent.
The fix for that needs a change in the approvals service or schema. That
is a separate change. I can do it as a follow-up if the maintainers want
it. Also, `master` changed since #11635, so the backfill would need a
rewrite.

## Verification

- `cd server && npx vitest run
src/__tests__/plugin-managed-agents.test.ts --maxWorkers=1` - 23 of 23
tests pass.
- With the one-line fix removed, the new test fails with the 409 message
above.
- Not run: the rest of the server test suite and a repo-wide typecheck.

## Risks

- Low risk. The flag lifts the freeze only for the plugin's own
instruction materialization. This is the same flag that the other
instruction and skill materialization paths already pass. Config changes
on pending agents from any other path stay refused.
- Agents that are already stuck are not repaired by this change. The
Wiki plugin has to create them again, or a follow-up has to add the
backfill.
- No schema change and no migration.

## Model Used

Claude Opus 5.5 (Anthropic) via Claude Code, with tool use (shell, file
edits, test execution). A human reviewed the diagnosis and the diff.

## 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 (no
docs describe this internal flow)
- [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

Co-authored-by: tomRumi <tomRumi@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 00:01:27 -08:00
..
…
…
…
…
…
…