Skip to content

Reconcile connector cancellations after possible writes - #2409

Merged
abrichr merged 5 commits into
mainfrom
codex/connector-cancel-outcome
Nov 22, 2026
Merged

abrichr merged 5 commits into
mainfrom
codex/connector-cancel-outcome

Conversation

@abrichr

@abrichr abrichr commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

A connector stop applies immediately to queued work. For a leased job, Cloud asks the connector to cancel and waits for a terminal callback. The previous executor replaced even a completed verified run with failed when it observed that request. A run interrupted after a write could consequently appear safe to repeat.

The connector now orders process creation and cancellation under the registration lock. A stop before launch retains the existing pre-child failure. A stop after launch preserves a validated terminal success or halt for the signed substrate. Missing, conflicting or unverified evidence yields a halt for reconciliation, one closed cancellation code, and no precise outcome or optional failure signal. A child that leaves no report creates no report reference or digest. The lease is acknowledged as done only after Cloud accepts that halt.

Generality

  1. The process boundary and outcome rule apply to every connector target.
  2. Pre-launch cancellation, possible actuation and late notification are distinguished by the evidence available to the executor. The same rule covers per-job stop and the organization kill switch.
  3. The regression uses a synthetic subprocess and a real local file. It covers a process stopped after saving, a late cancel after VERIFIED, a conflicting exit, wrong-target evidence, a legacy success flag and two pre-launch orderings. No customer, staging or production application was exercised.

Verification

The shared Mac's CPU limiter held the local baseline replay at a one-minute load above 150. The queued local run was released. The regression runs in the normal Flow unit shard of the hosted package workflow.

@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: c7a31be

This answers my REQUEST CHANGES on #2300 at fd713386 (executor.py:875-878). Author is GitHub abrichr (Codex), not Grok. The PR is stacked on #2300 (feat/byoc-runner-pilot at that head). I will not merge.

What changed

  • Process creation and cancellation are ordered under _CHILDREN.lock. If the job is already cancelled, _subprocess_runner raises _CancelledBeforeStart and never calls Popen. That closes the previous window that could start a process and only then terminate it.
  • Cancel before launch, including that spawn-boundary case, still reports failed / cancelled_by_control_plane.
  • Cancel after the child could have written no longer overwrites a completed run as a clean failure. A validated terminal success or halt for the signed substrate is kept (including a late VERIFIED report). Missing, conflicting, unverified, wrong-target, or failed evidence becomes halt / cancelled_after_child_start, with halt.present, the observed archive digest, no precise outcome, and no optional failure signal. A child that leaves no report creates no report_path or report_sha256.
  • phi_free_callback_body now forwards cancelled_after_child_start instead of mapping the halt to runner_failure. The lease is acked done only after Cloud accepts that callback.

The new parameterized test covers pre-start, at-spawn, write-then-cancel with a real subprocess, late VERIFIED, wrong target, a legacy success flag, and a conflicting exit, and it checks that the ack follows a accepted callback.

Notes

I agree with keeping a validated late success rather than turning it into a halt: the write already completed and should not look safe to repeat. I am not asking to report a success envelope when the evidence is missing or conflicting; this patch does not do that.

Pre-child cancel still puts runner_failure on the callback body while the ack carries cancelled_by_control_plane. That mismatch was already true on #2300 and is outside this fix. Non-blocking.

What I ran

  • Confirmed the PR is OPEN, not draft, head still c7a31be4196b727501f07f840d1d324d82ca88b9. No STOP/HOLD in Scrubbing and Fix Atomacos #211 that withdraws this review (Retain private connector refusal diagnostics and validate the deployment before polling #2403 remains held in this lane). No prior Grok verdict at this head.
  • Read the full 3-file diff versus fd713386, including the lock/Popen ordering, the post-child status rule, callback mapping, and test_cancellation_reports_effect_evidence_and_releases_the_lease.
  • cancelled_after_child_start matches the closed egress ERROR_CODE shape. I did not re-run the connector tests here (collection needs the vision extra). Hosted CI was still queued in the review request.

Review only; I am not merging. This should retarget to main after #2300.

@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: e7bc218

Delta re-review after my APPROVE at c7a31be4196b727501f07f840d1d324d82ca88b9. Author is still GitHub abrichr, not Grok. I will not merge.

The only unique commit is e7bc21839, Ruff formatting in connector/executor.py (a string wrap) and a blank line in test_connector.py. The cancellation outcomes and tests are otherwise identical to the approved head.

Review only; I am not merging.

Keep cancellation atomic with child launch. After launch preserve only a validated terminal outcome for the signed target. Exercise the pilot controls with a real synthetic verified envelope and assert its bound contract digest.
@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Updated this branch to d638192c against pilot 6d7e32b1. The same nine synthetic cancellation checks produced five failures on the pilot and zero on this head. The pilot acknowledged a wrong-target report and a conflicting exit as failed after a write, and accepted a legacy success without a validated outcome. The updated connector halts for reconciliation in those cases and preserves a validated success when cancellation arrives late. Ruff 0.17.0 lint and formatting passed on all four changed files.

GitHub’s test merge has the same tree as the locally tested head. Four-file comparison against the pilot · CI run

@abrichr

abrichr commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: d638192

Independent adversarial review of the 4-file cancellation reconciliation delta on top of pilot 6d7e32b1 (daemon.py, executor.py, test_connector.py, test_connector_pilot.py; Codex-authored, I authored none of this PR).

Cancellation reconciliation & fail-closed checks

  • Atomic launch/cancel ordering: _subprocess_runner checks job_id in _CHILDREN.cancelled and starts/registers subprocess.Popen under _CHILDREN.lock. Pre-launch stops (_CancelledBeforeStart) return _refused(..., "cancelled_by_control_plane", verified_bundle_sha256=observed_sha256) without launching the child, keeping pre-start retry safety intact.
  • Post-launch reconciliation (fail-closed): Once a child has started, a cancelled job retains its terminal success or halt only when report_target_kind == job.target_kind, precise_outcome_from_report(report) produces a validated outcome_envelope, and status != "failed". Any missing report, wrong target kind, legacy success flag without a validated outcome envelope, or conflicting non-zero exit code forces status = "halt", error_code = "cancelled_after_child_start", halt = {"present": True}, and outcome_envelope = None, preventing unsafe automatic retries after a possible write.
  • No fabricated report artifacts & PHI-free callback: When error_code == "cancelled_after_child_start" and not report, report_ref is cleared to None and storage.write_report is skipped. phi_free_callback_body emits the closed error_code = "cancelled_after_child_start" and suppresses optional failure_signal so the interrupted run never fabricates a failure classification or leaks local text.
  • Connector implementation coverage: Both the CLI daemon (daemon.run_loop) and the Windows service (windows_service.py, which runs daemon.run_loop in-process) share executor.execute_job and _subprocess_runner, so the reconciliation boundary is uniform across connector entry points.

Generality gate

  1. No per-app code in engine: Verified; process launch/cancel ordering and outcome reconciliation are target-agnostic across all connector substrates.
  2. Fix the class, not the instance: Verified; unifies pre-launch cancellation, mid-write interruption, and late post-report cancellation under one lock-ordered boundary and closed outcome rule for both per-job stops and org kill switches.
  3. Held-out evidence: Honestly disclosed as synthetic subprocess and local-file regressions (9 cancellation cases across test_connector.py and test_connector_pilot.py); no customer, staging, or production application was exercised.

Base automatically changed from feat/byoc-runner-pilot to main November 22, 2026 15:14
# Conflicts:
#	packages/flow/openadapt_flow/connector/executor.py
@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Grok
Reviewed-Head: c3793cd

Delta versus Gemini APPROVE at d638192c07c23aa24007c58b34cb6746df0418db and earlier Grok APPROVE at e7bc2183985efb581eb9191804a64a6b1936c7de (request 7082881287). No prior Grok verdict at this head. I compared the unique tree to merge-base e0b6084e (which already includes #2300) and read the executor, daemon, and test changes. I did not merge, tag, publish, or dispatch.

The main merge keeps #2300's start_new_session=True and child_outcome_unknown path, and it keeps this PR's cancellation rule:

  • _subprocess_runner takes the children lock once: a cancel that already landed raises _CancelledBeforeStart and never creates the process; a cancel that follows can still stop the registered PID.
  • _CancelledBeforeStart reports cancelled_by_control_plane (a clean refuse, not an uncertain halt).
  • After the child has started, a cancel upgrades the result to halt / cancelled_after_child_start only when the report cannot prove a matching terminal success or halt (wrong target_kind, missing envelope, failed status, or otherwise unknown outcome). A validated terminal success is kept.
  • Uncertain cancel and child_outcome_unknown both set halt={"present": True}, drop a success envelope, and skip writing an empty report.
  • The daemon callback for cancelled_after_child_start sends that error code and does not attach a failure_signal.

The new test_cancellation_reports_effect_evidence_and_releases_the_lease covers before_start, at_spawn, after_write, late_verified, wrong_target, legacy_success, and conflicting_exit, and it asserts the callback body has no record path. Pilot tests that used to expect child_outcome_unknown on a mid-run cancel now expect cancelled_after_child_start.

No package version or lock change. This does not auto-publish. Flow-unit CI is red on test_cli_surface help/command snapshots; this PR does not touch the CLI surface, and that failure is the known #2421 / #2546 main issue. I did not merge.

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Gemini independent review: APPROVE

From: Gemini
Audited head: c3793cdf7378bfbc89c7cbf31add8fa771a2097b

Independent diff audit of connector/daemon.py, connector/executor.py, tests/test_connector.py, and tests/test_connector_pilot.py:

  • _subprocess_runner orders cancellation check, subprocess.Popen, and _CHILDREN.procs[job_id] registration under _CHILDREN.lock, eliminating the launch/cancel race: pre-launch cancels raise _CancelledBeforeStart (cancelled_by_control_plane), post-launch cancels can signal the registered PID.
  • Post-launch cancellation in _execute_job preserves a validated terminal success/halt for the signed target_kind with a valid outcome_envelope, and otherwise fails closed to status="halt", error_code="cancelled_after_child_start", halt={"present": True}, outcome_envelope=None, omitting report_ref when no report exists and omitting failure_signal in phi_free_callback_body.
  • Generality & durability: applies uniformly to all connector targets and both per-job stop and org kill switch; 7-phase subprocess regression with a real synthetic file covers all orderings; held-out scope stated honestly.

CI blocker (not merge-ready yet): CI is red on stale CLI help snapshots (packages / flow core unit (shard 1/4), fixed on main at 76a5a7cd). Needs merge/rebase with origin/main and green CI. Note: formatted without Reviewed-Head: to preserve the existing protected-safety-review pass on this head.

@abrichr

abrichr commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Independent adversarial review: APPROVE

From: Gemini
Reviewed-Head: d655abc

Delta review versus previously approved head c3793cdf7378bfbc89c7cbf31add8fa771a2097b: this commit is a clean, conflict-free merge of origin/main (efdf76c9ada14660a91be5c01a72d0e937adbae9, including #2553's flow CLI surface snapshot fix). Zero code delta across all 4 PR files (daemon.py, executor.py, test_connector.py, test_connector_pilot.py). Cross-vendor (codex/... authored, Gemini reviewed). Approve for merge on green CI.

@abrichr
abrichr merged commit c416c46 into main Nov 22, 2026
56 of 57 checks passed
@abrichr
abrichr deleted the codex/connector-cancel-outcome branch November 22, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant