Skip to content

Consume activity workflow continuation v2 (server#1181) - #501

Merged
rmcdaniel merged 4 commits into
mainfrom
general/sdk-php-500-continuation-v2
Nov 26, 2026
Merged

rmcdaniel merged 4 commits into
mainfrom
general/sdk-php-500-continuation-v2

Conversation

@rmcdaniel

@rmcdaniel rmcdaniel commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Part of #500. This is the sdk-php consumer of activity workflow continuation v2 (durable-workflow/server#1181, contract). The issue stays open after merge: it is done only when this ships in the same window as the Server release that advertises v2 and the #1181 benchmark passes.

What

With enableActivityContinuation on, the Worker prefers v2 when the Server advertises a well-formed v2 member and keeps v1 as the fallback. The option stays off by default.

  • Negotiation. v2 needs every contract condition: supported_versions contains 2, v2.version === 2, the exact complete_endpoint, requires_worker_capabilities within what the Worker registers (sticky_execution, sticky_history_suffix), negotiated sticky history suffixes, and the option. The v2 member is parsed on its own, so a malformed member disables v2 only. Worker interceptors keep the ordinary path, as in v1. A 422 unsupported_continuation_version turns v2 off until the Worker registers again.
  • Seeding the cache. A workflow task completion with a cached boundary sends continuation_tail: {"version": 2}. The tail is appended only when it continues the cache exactly (StickyWorkflowCache::appendTail, the same rule as suffixes). Otherwise the entry is evicted. An omitted tail, or a missing one from an older Server, keeps the old boundary. A closed run evicts. A 422 invalid_continuation_tail is resent once without the tail and evicts the entry. The run's workflow type and input are kept with the cache entry for the speculative replay.
  • Sending v2. It is sent only when the cache starts with WorkflowStarted, is contiguous from sequence 1, and ends in this attempt's ActivityStarted. That last check covers a lease a replayed carried poll handed back. The Worker must also have reserved a workflow poll slot, and the replay of the cache plus one synthetic ActivityCompleted (no ID, no recorded_at, as v1 builds it) must pass the v1 checks. The request carries history_through_sequence, history_last_event_id, the sticky_cache claim (boundary equal to through), the commands, workflow_poll and an activity_poll. If any precondition fails, the Worker sends the ordinary completion. It does not fall back to v1.
  • Accepted. The tail keeps the cache exact. The carried activity runs in the same tick, and its own v2 request is speculated from that tail. The chain is capped at 16 activities per tick, so queries and workflow polls still get a turn.
  • Declines. The Worker processes the carried workflow poll answer. It evicts for cache_identity_mismatch and run_not_open and keeps the cache for every other reason.
  • Refusals. 4xx refusals, 500 invalid_continuation_receipt and 500 history_invariant_violated send the ordinary completion with the same carried workflow poll. The two 500s also evict and log an error. 409 stale_attempt and conflicting_completion go to the existing stale and duplicate handling and are never retried.
  • Unknown outcome. Any other 5xx, or a transport failure, resends the identical v2 request through the existing completion retry schedule (retryStorageAdmission gains an uncertainOutcome hook). It is never replaced by an ordinary completion. If the retries run out, the Worker logs worker.outcome_unpublished and leaves the attempt to the Server.
  • tail_unavailable on a retry. The Worker does not speculate on the leased activity, because the cache does not end in its ActivityStarted. That activity completes the ordinary way.

v1 is unchanged. With the option off, or without the v2 member, the request and answer paths are byte-for-byte the old ones.

Docs (docs/portal/build/workers.md), CHANGELOG.md [Unreleased] and tests/Integration/README.md are updated. The guide says continuations are unqualified and may cost throughput until the #1181 benchmark passes.

Tests

  • tests/WorkerActivityContinuationV2Test.php (67 tests) uses mocked HTTP to cover:
    • the accepted chain with no history reads;
    • every malformed-v2 and worker-condition case, and that v1 still works;
    • each unusable workflow-completion tail: the budgets, tail_unavailable, a missing tail, another boundary, a gap, a last_sequence mismatch, another version, malformed events;
    • a tail ending in another attempt's ActivityStarted, and the invalid_continuation_tail resend;
    • all 12 decline reasons with keep or evict, plus a declined carried workflow task that replays as a sticky hit;
    • all 10 refusal statuses and reasons, stale_attempt and conflicting_completion;
    • five unknown-outcome kinds, each resent identically, and retries that run out;
    • tail_unavailable on a retry, the accepted-tail edge cases, the chain bound, and StickyWorkflowCache tail and context behaviour.
  • tests/Integration/ActivityContinuationV2Test.php runs against a real native Server. It covers an accepted 3-step chain with no history reads and each activity recorded once. It also covers a lost answer resent identically and answered from the receipt (already_completed, same decision), a 422 refusal falling back to the ordinary completion with the same workflow poll, a decline caused by a signal, and a 300 KB result whose tail is omitted. Locally it passed 4 of 4 runs against an image built from server main 8b71fd1 (Dockerfile.native).
  • Replay corpus: tests/fixtures/replay-regressions/activity-continuation-v2-tail-chain.json runs a new golden.activity-continuation-v2 scenario (tests/Support/ActivityContinuationV2Scenario.php) through the official runner. On this branch the two activities complete with v2 continuations and no history read. The base revision fails the scenario, and the trusted runner rejects the new workflow type at both source revisions, as the corpus rule requires for a new runner consumer. validate-regression-corpus.py --base-ref origin/main passes locally.
  • Integration gating. The cases are opt-in (DURABLE_WORKFLOW_CONTINUATION_V2_QUALIFICATION=1 plus DURABLE_WORKFLOW_RUNTIME_URL), following the repo's pattern for candidate-Server qualification. They also skip unless cluster info advertises the v2 member. They skipped cleanly against published durableworkflow/server:3.29.0, so no published Server can make them fail. Ordinary CI runs them as skipped.
  • I ran these locally on PHP 8.3: the new tests, WorkerActivityContinuationTest, every Worker*Test, StickyHistorySuffixTest, ApiReferenceMetadataTest, ClientContractTest and the completion-limit tests, 3,928 tests in all. One timing test, WorkerActivityLeaseMaintenanceTest::testASlowInitialRenewalKeepsTheClaimUnderTheConservativePollGrant, failed once under load and passed 3 of 3 times on its own with this change. PHPStan is clean, and so is the public-boundary check. CI runs the full matrix.

Notes for review

  • Contract gaps: the contract gives no Worker action for standalone_activity (the cache is kept, a no-op in practice), 422 invalid_continuation_tail (resent without the tail, entry evicted), or a missing continuation_tail on an accepted v2 answer (evicted). Each choice is the conservative one.
  • With the v2 tail request, the Server's carried activity claim is restricted to activities the completion created (contract open question 2). That only happens with the opt-in.

🤖 Generated with Claude Code

Native Server implements activity workflow continuation v2 (server#1181).
With enableActivityContinuation on, the Worker now prefers v2 when the
Server advertises a well-formed v2 member and keeps v1 as the fallback.

- Negotiation reads the v2 member on its own, so a malformed member
  disables v2 only. v2 also needs sticky history suffixes and no worker
  interceptors. A 422 unsupported_continuation_version turns v2 off until
  the Worker registers again.
- A workflow task completion with a cached boundary asks for
  continuation_tail. A tail is appended only when it continues the cache
  exactly; otherwise the entry is evicted. An omitted or missing tail keeps
  the old boundary. 422 invalid_continuation_tail resends without the tail.
- The activity completion sends v2 only when the cache starts with
  WorkflowStarted, is contiguous and ends in this attempt's ActivityStarted,
  and the replay with the result passes the v1 checks. It carries an
  activity poll; an accepted answer's tail keeps the cache exact and its
  leased activity runs in the same tick, up to 16 per tick.
- Declines process the carried workflow poll and evict only for
  cache_identity_mismatch and run_not_open. Refusals send the ordinary
  completion with the same poll. Unknown outcomes resend the identical
  request and are never replaced by an ordinary completion.

The option stays off by default. Docs say continuations are unqualified
and may cost throughput until the server#1181 benchmark passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rmcdaniel and others added 3 commits November 26, 2026 12:38
The regression corpus requires a new fixture when sticky-cache and
workflow-completion code changes. One opted-in worker runs two sequential
activities against a v2 Server: the first workflow task's tail seeds the
cache, and both activities complete with v2 continuations speculated from
the cache, with no history read. The second continuation's commands come
from the first accepted answer's tail and complete the run with 14.

On the base revision the worker has no v2 consumer, completes the
workflow task without asking for a tail and the scenario fails. The
trusted runner has no implementation for the new workflow type at either
source revision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ReplayRegressionCorpusTest replays every fixture through
tests/Support/ReplayRegressionFixture.php, which mirrors the official
runner's scenario dispatch. Add the golden.activity-continuation-v2 case
there too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

@rmcdaniel

Copy link
Copy Markdown
Member Author

Independent review: PASS at head 636999f

I checked this against the v2 contract on server main, including "Resolved during implementation", and against the server source (continuation_v2.rs, continuation_tail.rs, and the capability in continuation.rs).

What I verified:

  • Negotiation. v2 is selected only when all of these hold: supported_versions contains the integer 2; v2 is an object with version === 2 and the exact complete_endpoint; requires_worker_capabilities is a subset of sticky_execution and sticky_history_suffix, which the Worker always registers and which portableWorkerAffinity() puts in the manifest; the registration negotiated suffix v1; and enableActivityContinuation is on, as it is off by default. activityContinuationV2Contract parses the v2 member separately from the v1 descriptor, so a malformed member nulls only v2. When v2 is not selected, the request bodies are unchanged: a null continuation_tail is filtered out, the continuation body keeps its field order, taskDelivery->carried(null) is a no-op, and the activity loop runs once.
  • Cache seeding. continuation_tail is sent only when the sticky claim carries history_last_sequence. appendTail requires the cached last sequence to equal after_sequence, a cache that starts with WorkflowStarted, and the same continues rule that suffixes use. Anything else evicts. An omitted or missing tail on a workflow completion keeps the boundary. A missing tail on an accepted answer evicts.
  • Speculation and request. The worker preconditions hold. The cache must be contiguous from sequence 1 and end in this attempt's ActivityStarted, checked by activity_attempt_id, which the server writes into that event's payload. The synthetic ActivityCompleted has no ID and no recorded_at, as in v1. The member keys match continuation_v2::parse exactly: version, history_through_sequence, history_last_event_id (capped at 64 bytes, a stricter limit than the server's 64 characters), the sticky_cache claim whose history_last_sequence equals through, and the commands. activity_poll is a top-level field.
  • Declines and refusals. Declines evict only for cache_identity_mismatch and run_not_open; every other decline keeps the cache, and the carried workflow poll answer goes through the existing carrier. Refusals (4xx, plus 500 invalid_continuation_receipt and history_invariant_violated) send the ordinary completion with the same workflow_poll. The two corrupt-state 500s evict and log at error level. unsupported_continuation_version turns v2 off until the next registration. 409 stale_attempt and conflicting_completion take the existing path, the same one v1 uses.
  • Unknown outcome. Any other 5xx, or a transport failure (status 0), resends the identical v2 request. The closure captures the same continuation array, poll requests and claim, and retryStorageAdmission now treats these as an untyped outage. The request is never replaced by an ordinary completion. When retries run out, the Worker logs outcome_unpublished and leaves the attempt to the Server. tail_unavailable keeps the old boundary, so the leased activity completes the ordinary way.
  • Concurrency and memory. The PHP worker runs tasks sequentially, and completion pipelining uses cooperative fibers. Speculation works on a copy-on-write snapshot of the cached history. appendTail re-checks the boundary at the moment it appends, so any interleaving ends in a skip or an eviction, never a wrong history. Context is stored on the entry and evicted with it. The chain is capped at 16 activities per tick.
  • Docs. The guide and the CHANGELOG say the feature is off by default, unqualified, and may cost throughput until the server#1181 benchmark passes.
  • Tests. I ran WorkerActivityContinuationV2Test, WorkerActivityContinuationTest and StickyHistorySuffixTest locally on PHP 8.3: 90 tests, all OK. I then broke the code four ways, and each mutation failed tests: dropping the attempt-ID check (2 failures), removing the evicting declines (2), classifying 5xx as a refusal (2), and removing the tail continuity check (3). The integration cases are opt-in through DURABLE_WORKFLOW_CONTINUATION_V2_QUALIFICATION=1 and also skip unless cluster info advertises v2. That is the right gate while v2 is only on server main. The release-backed run the issue requires remains open on Consume activity workflow continuation v2 (server #1181) #500, which this PR correctly does not close.
  • CI. At this head, 33 checks pass and 1 is skipped (Connected cooperative cancellation, which is conditional). The branch is up to date with main.

Two non-blocking follow-ups. Both affect performance only, within the opt-in path, and neither affects correctness:

  1. src/Worker/StickyWorkflowCache.php, appendTail. continues([]) returns false, so an empty tail evicts the entry. An empty tail is last_sequence == after_sequence with no events, and a workflow task completion with no commands produces one. With v2 on, the run's next workflow task then needs a cold replay. Suggested fix: accept $events === [] && $lastSequence === $afterSequence as a no-op continuation.
  2. src/Worker/StickyWorkflowCache.php, appendTail. It updates touched_at but not expires_at. An accepted v2 answer is the run's workflow task, and its nested completion renews the server-side sticky claim, but the worker's entry still expires ttl_seconds after the last remember(). Once a chain of accepted steps runs longer than the TTL, the cache is missed and the next workflow task is a forced cold replay. Suggested fix: refresh expires_at in appendTail, as remember() does.

@rmcdaniel
rmcdaniel merged commit c9cc155 into main Nov 26, 2026
61 of 63 checks passed
@rmcdaniel
rmcdaniel deleted the general/sdk-php-500-continuation-v2 branch November 26, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant