Skip to content

Playbooks: each run keeps its own copy #795

Open

Description

@bryantderosier

Item 1 of 9 in the Playbooks v2: one owner, flexible staffing milestone. It goes first, since most of the others build on it.

The problem

A run only remembers which file its playbook came from (definition_path) and re-reads that file on every tool call. So if I edit or delete a playbook while it's running, the agent following it sees the change halfway through. A lot of code exists only because of that: missing-file and invalid-file handling, reselecting when the current step disappears, a file watcher, and the rule that I can't delete a playbook while a run uses it.

Where this sits

This is section 7 ("Each run keeps its own copy") and section 11 ("Upgrade") of the Playbooks proposal (26 October). Changing an active run's procedure on purpose is #787. This issue only makes the copy the run's source of truth.

What we already have

  • The run row. fromRow is at apps/server/src/j5/playbooks/PlaybookStore.ts:86-105. The table is 018_AgentLedPlaybooks.ts:6-14, plus crew_instance_id from 029. The contract's PlaybookRun exposes definitionPath (packages/contracts/src/j5/playbook.ts:92-103). There's no copy column and no cancel reason; cancelling only sets status (PlaybookStore.ts:226, :691, :868).

  • Start. It reads the file at PlaybookStore.ts:587, outside the transaction, and inserts at :611-612. Retries replay the recorded path (:422-446).

  • Live reads on the run path:

    • view :288
    • recordFinished :638, for analytics
    • mutate :671
    • progressRows :751-757
    • liveStep :797

    The library's own reads stay: definition :216, discover :383, readPath :455, export :525, rename :551.

  • Missing and invalid files:

    • "Cannot read the live playbook…" (:199, :212)
    • the empty-steps fallback view (:292-303)
    • current throwing the issue (:740-745)
    • the relay going pending on an unreadable file (PlaybookCrewRelay.ts:137-141)
    • Fleet's issue branch (PlaybookCrewRelay.ts:351-361)
  • Reselect:

    • step_missing (PlaybookStore.ts:276-281) and the reselect branch (:695-698)
    • the tool (mcp.ts:107-113, :223-225)
    • the instructions (packages/provider-core/src/j5/playbookInstructions.ts:13, :16)
    • preapproval (apps/server/src/j5/a2a/mcp/j5ToolPreapproval.ts:53, :68)
    • the tool-name lists in registration.test.ts:290 and handlers.test.ts:246
  • The file watcher. PlaybookDefinitionWatch.ts watches the folders holding active runs' files and calls definitionsChanged (PlaybookStore.ts:895-910). It's wired in runtimeLayer.ts:5.

  • The delete guard. PlaybookStore.ts:498-505, pinned by playbookRpc.test.ts:445.

  • How clients show a file problem. PlaybookProgress.issue (playbook.ts:124) feeds presentPlaybook, which shows "Needs attention" and "Step unavailable" (packages/client-runtime/src/j5/playbooks.ts:289-324, sorting at :390-397). On web that's PlaybookStages.tsx:50,80,159, PlaybookRunsSection.tsx:82,145, and fleet.logic.ts:51-53. On mobile it's PlaybookComposerControl.tsx:79,88,157.

  • Migrations. They're registered by hand in apps/server/src/j5/a2a/Migrations.ts:57-104. The latest is 047, so this one is 048, and Migrations.test.ts hard-codes the latest id.

    • The 019 trigger deletes movement receipts when a run leaves active (019_PlaybookRunMaintenance.ts:15-20), so the upgrade's cancel will fire it. That's fine.
    • neutraliseCopiedDatabase.ts:123-134 already cancels runs in bulk and is a good pattern to copy.
  • Tests that pin live edits. Rewrite these rather than keep them:

    • PlaybookStore.test.ts: 158, 215, 390, 443, 510, 949
    • mcp.test.ts:356
    • playbookRpc.test.ts: 289, 354, 391, 484
    • PlaybookCrewRelay.test.ts: 550, 593
    • PlaybookDefinitionWatch.test.ts
    • the step_missing fixtures in client-runtime, PlaybookStages, fleet.logic, and fleetRead tests

Proposal

  • Start parses the playbook once and saves the parsed copy on the run (title, description, and each step's id, title, prompt, and persona). It does this in the same transaction as the insert.
  • Every run read uses the copy: current step, movement, progress, Fleet, and finish analytics. Only the library (list, read, save, rename, export, delete) touches files.
  • Migration 048 adds the copy column and a cancel reason, and cancels the runs that are still active.

Acceptance criteria

  • Starting a run saves a copy of the playbook on the run. Every later tool call for that run reads the copy, never the file.
  • Editing a saved playbook changes future runs only. An active run's steps and prompts stay as they were when it started.
  • Deleting a playbook while a run of it is active succeeds, and the run carries on from its copy. The delete guard and its test are gone.
  • Migration 048 adds the copy column and a cancel reason. It marks every active run cancelled with the reason "started before runs kept their own copy; start it again." Completed and cancelled runs are untouched.
  • The cancel reason reaches clients on the run, and run history shows it.
  • Past runs without a copy show their name, status, and last step, without the step list, on web and mobile.
  • The live re-read code is deleted:
    • the missing-file and invalid-file paths
    • step_missing, and the issue field on progress
    • PlaybookDefinitionWatch
    • "Needs attention" and "Step unavailable" on web and mobile
  • j5_playbook_reselect is removed from the toolkit, the preapproval lists, and the instructions, since a step can no longer vanish from under a run. Playbooks: revise an active run's procedure #787 covers a revision that removes the current step.
  • A start retry returns the same run with its copy.
  • A Crew following a playbook still starts and moves as today. The Crew's start check (PlaybookCrewRelay.ts:292) compares the run's recorded path, and Crew proposals keep reading the file to plan seats until Playbooks: the owner hands each step to someone #788 removes that.
  • Focused tests cover:
    • an edit during a run, which leaves it unchanged;
    • a delete during a run, which leaves it running;
    • the migration cancelling active runs with the reason and leaving completed and cancelled runs alone;
    • run history for a run without a copy;
    • a start retry.
  • docs/j5/product/features/playbooks.md AC8, AC9, the AC12 clause about blocking deletion, and the "A live single-agent procedure" scenario are rewritten. docs/j5/user/playbooks.md no longer says edits reach active runs.

Out of scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions