← Back to Pull requests

fix(backend): resume only the schedules archiving paused when re-hiring an expert #17267

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 22, 2026 19:30 UTC
Author

Why / What / How

Why: Letting an expert go pauses its running schedules, and hiring it again resumed every paused schedule attributed to it, including the ones the user had paused themselves. The user then saw runs, and possibly charges, they had switched off. Presets and routines already tell the two kinds of pause apart (AgentPreset.deactivatedByExpertArchive, ExpertRoutine.pausedByExpertArchive). Workflow schedules had nowhere to keep that mark: a schedule is an APScheduler job, not a Prisma row, and a pause is stored only as next_run_time = NULL. The experts journey in #17266 reproduces the bug end to end on dev; the steps are in the issue's latest comment.

What: Re-hiring now resumes only the schedules that letting the expert go paused. A schedule the user paused before letting the expert go, or while it was away, stays paused.

How: The mark lives on the job itself, so no migration is needed. GraphExecutionJobArgs gains paused_by_expert_archive (default False), which is stored in the job's kwargs.

  • Archiving. pause_execution_schedule(..., for_expert_archive=True) pauses a running workflow schedule and marks it in one modify_job write. It never marks a schedule that is already paused, because that pause was someone else's.

  • Any other pause clears the mark, even on a schedule that is already paused. An explicit pause is the user's word on that schedule, and it outranks the archive's.

  • Re-hiring. resume_execution_schedule(..., only_if_paused_by_expert_archive=True) resumes only a marked schedule and clears the mark. Every resume clears it.

  • Locking. Pause and resume read the job and write it back under APScheduler's jobstore lock, so a concurrent pause can't be overwritten. A test forces that interleaving.

  • Edits. An edit keeps the mark: changing a schedule's timing or inputs doesn't decide whether it runs. Re-hiring brings it back with the user's edit.

  • Create retries. A create sent again under its key ignores the mark when it compares, as it already ignores next_run_time: both are pause state, not part of what the create asked for.
  • Schedules archived before this change carry no mark, so re-hiring leaves them paused. The archive used to pause them with the same call a user's pause makes, so nothing tells "paused by letting the expert go" apart from "paused by the user". Resuming them would repeat this bug for anyone who had paused one on purpose. Leaving them paused costs one resume: they show as Paused on the expert's schedules, and the expert resumes them from chat (resume_schedule). The same holds during the rollout. An old scheduler ignores the new arguments, because the RPC body model drops unknown fields, and an old API pod doesn't send them. So mixed versions never resume more than they do today, and a schedule an old pod pauses carries no mark, which leaves it paused on a later re-hire.

    The approach follows #15101 by goingforstudying-ctrl. That PR was closed unmerged; this is a new implementation on current dev. It also lets a user's pause take over a schedule the archive already paused, holds the jobstore lock across each read and write, and keeps the mark out of the create-retry comparison.

    Fixes #14577

    Changes 🏗️

  • backend/executor/scheduler.py: the paused_by_expert_archive mark; pause_execution_schedule(for_expert_archive=...) and resume_execution_schedule(only_if_paused_by_expert_archive=...), both under the jobstore lock; the create-retry comparison leaves the mark out.

  • backend/api/features/experts/scheduling.py: detach_expert_triggers marks the schedules it pauses; reattach_expert_triggers resumes only marked ones.

  • frontend/src/app/api/openapi.json: regenerated; GraphExecutionJobInfo gains the field.

  • New backend/executor/scheduler_archive_mark_test.py: a real APScheduler on a SQL job store. Covers who sets and clears the mark, a job stored before the mark existed, an edit, a create retry, and a forced race between a user's pause and the archive flow.

  • New backend/api/features/experts/rehire_schedules_test.py: DB-backed, against the scheduler service and its job store, through the real archive and hire flows. Covers the issue's steps (paused from the expert's chat, two round trips), a pause while the expert is away (directly, and after a resume), and a schedule archived before the mark existed.

  • backend/api/features/experts/experts_db_test.py: the mocked detach and reattach tests now expect the new arguments.
  • No data/.py changes and no new queries. The scheduler's pause and resume still check the job's owner (_authorized_job, by user id) before they read or write it.

    The journey's re-hire check in #17266 is still pinned with a runtime test.fail(). #17266 hasn't merged yet; once both have landed, that pin comes off.

    Agents and large language models used

  • Claude Code with Claude Opus 5.5
  • Checklist 📋

    For code changes:


  • I have clearly listed my changes in the PR description

  • I have made a test plan

  • I have tested my changes according to the test plan:

  • - [x] The 4 new DB tests pass on this branch. They ran against the scheduler service and its job store, on a private Postgres, Redis cluster and RabbitMQ stack with the server fixture.
    - [x] Revert check. The same 4 tests fail on dev 4b4a8354a2 (fix reverted, tests kept), on the bug itself. Three fail with "the user's paused schedule resumed"; the pre-mark case fails with its paused schedule running again. The 11 new scheduler tests fail on dev too, since the arguments don't exist there.
    - [x] Mutation checks. Seven one-change mutants of the scheduler: no lock in pause, no lock in resume, the create retry comparing the mark, no take-over by a user's pause, the archive marking an already-paused schedule, a resume keeping the mark, and re-hire ignoring the mark. Each fails at least one of the new scheduler tests. An archive that forgets to mark fails 3 of the 4 DB tests; the pre-mark case passes either way.
    - [x] Neighbours on this branch, 229 passed. executor/scheduler_test.py, scheduler_cadence_test.py and scheduler_idempotent_create_test.py; the experts scheduling_test.py and workflow_removal_test.py; copilot/tools/manage_schedules_test.py; the schedules routes_test.py; the external v2 schedules_test.py; and the three detach/reattach tests in experts_db_test.py.
    - [x] backend/util/architecture
    _test.py and backend/blocks/test/test_block.py: 4451 passed, 84 skipped.
    - [x] Whole-backend pyright: 0 errors. ruff check (0.17.4), isort 9.0.2 and black change nothing.
    - [x] export-api-schema plus prettier: the openapi.json diff is the one new field. Whole-frontend tsc --noEmit, next lint, oxlint and prettier --check . pass.

    For frontend changes (design system, see docs/engineering/design-system.md):

    Not applicable: the only frontend change is the generated openapi.json.

    For configuration changes:

    None.

    🤖 Generated with Claude Code

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: coderabbitai[bot] Review duration: 9m
    All Vouch review confidence checks passed
    Residual risk 0.35 = Change Risk (0.52) × (1 − Review Confidence (0.32)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 58% · Reviewer familiarity: 35%
    31%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #17267"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 31.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 31.9% · Lower = rushed or shallow review
    31.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 35.4% · Moderate risk
    35.4%