diff --git a/api/core/workflow_copilot/contract_gen.py b/api/core/workflow_copilot/contract_gen.py index 7a7a09e7c5f..7552b84978a 100644 --- a/api/core/workflow_copilot/contract_gen.py +++ b/api/core/workflow_copilot/contract_gen.py @@ -4,7 +4,7 @@ from ``models.py`` and ``SessionView`` from ``services.workflow_copilot.service` Regenerate after any change to those sources:: - uv run --project api python -m core.workflow_copilot.contract_gen + uv run --directory api python -m core.workflow_copilot.contract_gen This writes the two checked-in derived files: @@ -45,7 +45,7 @@ _JSON_SCHEMA_DIALECT = "https://json-schema.org/draft/2020-12/schema" _TS_HEADER = ( "// AUTO-GENERATED by api/core/workflow_copilot/contract_gen.py — do not edit by hand.", - "// Regenerate: uv run --project api python -m core.workflow_copilot.contract_gen", + "// Regenerate: uv run --directory api python -m core.workflow_copilot.contract_gen", ) diff --git a/api/core/workflow_copilot/models.py b/api/core/workflow_copilot/models.py index d7b7dc213e3..ec64573d5e1 100644 --- a/api/core/workflow_copilot/models.py +++ b/api/core/workflow_copilot/models.py @@ -134,7 +134,9 @@ class TestInput: @dataclass(kw_only=True) class ConversationItem: seq: int = 0 - kind: str = "" # run-context | assistant-turn | user | notice | diagnosis | change-set | verify-result + kind: str = "" # user | decision | notice | run_context | preflight_context | assistant_turn | plan + # | form | challenge | resource_select | checkpoint | change_set | test_result | error | summary + # | publish | build_learning payload: dict[str, Any] = field(default_factory=dict) at_version: int = 0 diff --git a/api/services/workflow_copilot/service.py b/api/services/workflow_copilot/service.py index 1996cb7a5e0..75051862d50 100644 --- a/api/services/workflow_copilot/service.py +++ b/api/services/workflow_copilot/service.py @@ -113,7 +113,7 @@ def _phase_for(state: PcState) -> Phase: _ACTIONS_FOR: dict[PcState, list[UiAction]] = { PcState.FIX_AWAIT_APPROVAL: [ UiAction(id="approve_plan", label="Approve fix", kind=ActionKind.PRIMARY), - UiAction(id="revert", label="Revert", kind=ActionKind.DESTRUCTIVE), + UiAction(id="reject_repair", label="Reject", kind=ActionKind.DESTRUCTIVE), ], PcState.FIX_AWAIT_VERIFY: [ UiAction(id="run_validation", label="Run validation", kind=ActionKind.PRIMARY), @@ -148,6 +148,9 @@ _ACTION_ID_TO_KIND: dict[str, str] = { } +_CLIENT_ONLY_ACTIONS = frozenset({"view_changes"}) + + def resolve_action_kind(raw: str) -> str: """Map a new FE action_id to the engine handler kind; pass through anything already a handler kind (legacy {kind:...} back-compat). @@ -279,6 +282,11 @@ class WorkflowCopilotService: s, _fc = self._repo.get_session(session_id) if s.owner_account_id != actor.account_id: raise NotFoundError("session not found") + if action.kind in _CLIENT_ONLY_ACTIONS: + # Client-side-only actions (e.g. view_changes toggles a card locally) never + # reach the engine — dispatching would hit handle_await_decision's keep_draft + # default and silently terminate the session. Return the current view unchanged. + return self.get_session_view(session_id, actor) if action.base_version != s.version: raise ConflictError(f"stale base_version {action.base_version} for session {session_id}") self.dispatch(session_id, action, actor) diff --git a/api/tests/unit_tests/core/workflow_copilot/test_contract.py b/api/tests/unit_tests/core/workflow_copilot/test_contract.py index ea2dcb53fa6..40e7871415a 100644 --- a/api/tests/unit_tests/core/workflow_copilot/test_contract.py +++ b/api/tests/unit_tests/core/workflow_copilot/test_contract.py @@ -234,7 +234,7 @@ def test_schema_in_lockstep(): root = next(p for p in Path(__file__).parents if (p / "web").is_dir() and (p / "api").is_dir()) checked_json = (root / "api/core/workflow_copilot/contract_schema.json").read_text() checked_ts = (root / "web/app/components/workflow-copilot/contract/types.ts").read_text() - regen_hint = "run: uv run --project api python -m core.workflow_copilot.contract_gen" + regen_hint = "run: uv run --directory api python -m core.workflow_copilot.contract_gen" assert json.dumps(schema, indent=2, ensure_ascii=False) + "\n" == checked_json, regen_hint assert ts == checked_ts, regen_hint diff --git a/api/tests/unit_tests/services/workflow_copilot/test_service.py b/api/tests/unit_tests/services/workflow_copilot/test_service.py index d19f1c6e4a0..a0be009eb94 100644 --- a/api/tests/unit_tests/services/workflow_copilot/test_service.py +++ b/api/tests/unit_tests/services/workflow_copilot/test_service.py @@ -27,6 +27,7 @@ from core.workflow_copilot.models import ( ) from core.workflow_copilot.state import PcState from models.base import Base +from services.workflow_copilot import service as service_module from services.workflow_copilot.repository import SqlCopilotRepository from services.workflow_copilot.service import WorkflowCopilotService, resolve_action_kind @@ -337,6 +338,58 @@ def test_resolve_action_kind_passes_through_legacy_and_unmapped_kinds() -> None: assert resolve_action_kind("keep_draft") == "keep_draft" +def test_waiting_state_actions_resolve_to_handled_kinds() -> None: + """For EACH Fix/Checklist waiting state in ``_ACTIONS_FOR``, every + surfaced non-client-only action id must resolve (via + ``resolve_action_kind``) to a handler kind that state's handler in + ``handlers_fix.py`` actually branches on -- otherwise the button is dead + (falls through to the state's default no-op/branch) rather than doing + what its label promises. + + Regression for the FIX_AWAIT_APPROVAL ``revert`` dead button: + ``resolve_action_kind("revert") == "undo"``, but + ``handle_await_approval`` only branches on ``approve_repair`` / + ``reject_repair`` -- ``undo`` fell through to the gate's no-op branch, + so the reject path was unreachable from the FE.""" + handled_kinds: dict[PcState, set[str]] = { + PcState.FIX_AWAIT_APPROVAL: {"approve_repair", "reject_repair"}, + PcState.FIX_AWAIT_VERIFY: {"run_verify", "undo"}, + PcState.FIX_AWAIT_TESTDATA: {"provide_testdata"}, + # excludes the client-only view_changes, which never reaches the handler. + PcState.FIX_AWAIT_DECISION: {"publish", "undo"}, + PcState.CHECKLIST_AWAIT_RECHECK: {"recheck"}, + } + for state, handled in handled_kinds.items(): + actions = service_module._ACTIONS_FOR[state] + resolved = { + resolve_action_kind(a.id) for a in actions if a.id not in service_module._CLIENT_ONLY_ACTIONS + } + assert resolved <= handled, f"{state}: resolved kinds {resolved} not handled by its handler ({handled})" + + +def test_submit_action_view_changes_is_a_noop_not_keep_draft( + service: WorkflowCopilotService, repo: SqlCopilotRepository, enqueued: list[tuple] +) -> None: + """``view_changes`` is a client-side-only card toggle (forces + ``change_set.full_diff_open`` in the FE) that is never mapped in + ``_ACTION_ID_TO_KIND``, so ``resolve_action_kind`` passes it through + unchanged. If it ever reached ``dispatch`` at ``fix.await_decision``, + ``handle_await_decision``'s DEFAULT branch (``keep_draft`` -> + ``PcState.SUCCESS``, terminal) would silently end the session. The + ``_CLIENT_ONLY_ACTIONS`` guard in ``submit_action`` must intercept it + before the CAS check/dispatch and return the view unchanged.""" + s = _seed_session_at(repo, PcState.FIX_AWAIT_DECISION) + actor = _actor() + + view = service.submit_action(s.id, actor, Action(kind="view_changes", base_version=s.version)) + + assert view.state == "fix.await_decision" + assert view.state != "success" + assert view.version == s.version # no CAS/dispatch advance happened + assert view.run_status != "complete" + assert enqueued == [] # never reached enqueue_fn -- proves it's a no-op, not a dispatched keep_draft + + def test_dispatch_releases_lock_when_enqueue_fails(repo: SqlCopilotRepository) -> None: lock = FakeSessionLock() diff --git a/web/app/components/workflow-copilot/contract/types.ts b/web/app/components/workflow-copilot/contract/types.ts index f2e141f4d6c..75a336e736f 100644 --- a/web/app/components/workflow-copilot/contract/types.ts +++ b/web/app/components/workflow-copilot/contract/types.ts @@ -1,5 +1,5 @@ // AUTO-GENERATED by api/core/workflow_copilot/contract_gen.py — do not edit by hand. -// Regenerate: uv run --project api python -m core.workflow_copilot.contract_gen +// Regenerate: uv run --directory api python -m core.workflow_copilot.contract_gen export type Phase = 'understand' | 'clarify' | 'resources' | 'plan' | 'modify' | 'test' | 'review' | 'publish' | 'complete'; export type RunStatus = 'thinking' | 'executing' | 'waiting_input' | 'waiting_confirmation' | 'paused' | 'failed' | 'complete'; export type ActionKind = 'primary' | 'secondary' | 'destructive' | 'automatic';