Skip to content

refactor: fold tool-call and nudge accumulators into turn state (#2803 slice 3)#2820

Merged
chubes4 merged 1 commit into
mainfrom
fold-byref-into-turnstate
Jun 30, 2026
Merged

refactor: fold tool-call and nudge accumulators into turn state (#2803 slice 3)#2820
chubes4 merged 1 commit into
mainfrom
fold-byref-into-turnstate

Conversation

@chubes4

@chubes4 chubes4 commented Jun 28, 2026

Copy link
Copy Markdown
Member

Summary

Third scoped slice of #2803. Eliminates the remaining by-reference parameter smuggling in conversation-loop.php by folding the per-turn mutable accumulators onto the existing DataMachineProviderTurnState object. This is a state-plumbing refactor only — behavior is byte-for-byte identical across every conversation path.

Slices 1+2 (merged via #2808 and #2815) introduced DataMachineProviderTurnState and absorbed latest_messages, latest_turn_count, and last_request_metadata. This slice absorbs the last three.

Fields added to DataMachineProviderTurnState

  • public array $last_tool_calls = array(); — tool calls from the latest dispatched turn (DM-flavored shape)
  • public array $all_tool_calls = array(); — all tool calls accumulated across the run
  • public array $completion_nudges = array(); — completion nudge diagnostics accumulated across the run (DM-only)

Declared as public typed properties with array init, matching the existing field style on the object.

By-ref params removed

datamachine_build_provider_turn_adapter() signature, before → after:

  • Before: 12 params, 3 by-ref (&$last_tool_calls, &$all_tool_calls, &$completion_nudges) — on top of the 3 by-ref already removed in slices 1+2, this is the 6 → 0 by-ref target from the issue.
  • After: 10 params, 0 by-ref. All per-turn mutable state now flows through the single $turn_state object.

Dropped from both the function signature and the returned closure's use(...) list.

Call sites updated

  1. Factory call (datamachine_run_conversation, ~L180) — removed the 3 by-ref args.
  2. Closure writes (~L1073) — $last_tool_calls = $tool_calls$turn_state->last_tool_calls = $tool_calls; $all_tool_calls[] = ...$turn_state->all_tool_calls[] = ....
  3. Nudge recorder call (~L1163) — datamachine_record_completion_nudge( $turn_state->completion_nudges, ... ). The helper keeps its own by-ref param and now mutates the object property directly (valid PHP — public array property passed by reference).
  4. Failure-path error result (catch block, ~L278) — reads $turn_state->last_tool_calls / $turn_state->all_tool_calls.
  5. Result normalizer call (~L353) — reads $turn_state->last_tool_calls, $turn_state->all_tool_calls, $turn_state->completion_nudges.
  6. Init block (~L86) — removed the three local array() initializers (now object-owned).

Per-path parity

Every former by-ref read now reads the same value from $turn_state at the same point in execution:

  • completed / completion_policy_stop — normalizer reads accumulators after the loop returns; values populated by the closure during the loop, unchanged ordering.
  • budget_exceeded / runtime_tool_pending / interrupted — same normalizer read path.
  • failed (pre-substrate RuntimeException) — catch block reads last_tool_calls/all_tool_calls from $turn_state, populated by the closure up to the point of failure, identical to the prior by-ref snapshot.
  • nudge diagnostics — appended via the recorder mutating $turn_state->completion_nudges, read by the normalizer at the same point with the same accumulated sequence.

Verification

  • php -l clean on both touched files.
  • vendor/bin/phpcbf + vendor/bin/phpcsfully green on both files, including the Generic.Formatting.MultipleStatementAlignment equals-alignment sniff.
  • Relevant smoke tests pass individually: agent-conversation-runner-request, agent-conversation-result, agent-conversation-runtime-policy, ai-message-envelope, ai-loop-event-sink, provider-turn-tool-extraction-contract.
  • Full smoke suite: PASS=233 FAIL=62 — identical to baseline (changes stashed). The 62 failures are pre-existing environmental issues when the suite is run as a batch loop (missing WP test bootstrap/autoload), unrelated to this change and present on main. Zero regressions introduced.
  • grep confirms &$last_tool_calls, &$all_tool_calls, &$completion_nudges are gone from the adapter; the only remaining occurrence is the receiving by-ref param inside the datamachine_record_completion_nudge helper (which now receives $turn_state->completion_nudges).

Scope

Slice 3 of #2803 — references #2803, does not close it. Remaining: the broader 64-function file split into cohesive modules.

…slice 3)

Eliminate the remaining by-reference accumulator params smuggled through
datamachine_build_provider_turn_adapter() by folding last_tool_calls,
all_tool_calls, and completion_nudges onto DataMachineProviderTurnState.

The adapter factory drops its last three by-ref params (6 -> 0 by-ref):
slices 1+2 already absorbed latest_messages, latest_turn_count, and
last_request_metadata; this slice absorbs the tool-call and nudge
accumulators so all per-turn mutable state flows through one object.

Reads in the failure-path error result and the result normalizer now
source the same values from $turn_state at the same points, keeping
behavior byte-for-byte identical across every conversation path.
@homeboy-ci

homeboy-ci Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Homeboy Results — data-machine

Lint

lint — passed

ℹ️ Full options: homeboy docs commands/lint
Deep dive: homeboy lint data-machine --changed-since e5becf8

Artifacts and drill-down
  • CI results artifact: homeboy-ci-results-data-machine-lint-quality-Linux-node24 contains immediate command JSON for this action invocation.
  • Observation artifact: homeboy-observations-data-machine-lint-quality-Linux-node24 contains exported Homeboy run history for deeper queries.
  • Drill-down: download the observation artifact, then run homeboy runs import <dir>, homeboy runs list, and homeboy runs findings <run-id>.
  • Artifacts are attached to the workflow run: https://github.com/Extra-Chill/data-machine/actions/runs/28339861732

Test

test — passed

  • 6 passed

ℹ️ Auto-fix lint issues: homeboy refactor data-machine --from lint --write
ℹ️ Collect coverage: homeboy test data-machine --coverage
ℹ️ Save test baseline: homeboy test data-machine --baseline
ℹ️ Pass args to test runner: homeboy test -- [args]
ℹ️ Full options: homeboy docs commands/test
Deep dive: homeboy test data-machine --changed-since e5becf8

Artifacts and drill-down
  • CI results artifact: homeboy-ci-results-data-machine-test-quality-Linux-node24 contains immediate command JSON for this action invocation.
  • Observation artifact: homeboy-observations-data-machine-test-quality-Linux-node24 contains exported Homeboy run history for deeper queries.
  • Drill-down: download the observation artifact, then run homeboy runs import <dir>, homeboy runs list, and homeboy runs findings <run-id>.
  • Artifacts are attached to the workflow run: https://github.com/Extra-Chill/data-machine/actions/runs/28339861732

Audit

audit — passed

  • audit — 10 finding(s)
  • Total: 10 finding(s)

Deep dive: homeboy audit data-machine --changed-since e5becf8

Artifacts and drill-down
  • CI results artifact: homeboy-ci-results-data-machine-audit-quality-Linux-node24 contains immediate command JSON for this action invocation.
  • Observation artifact: homeboy-observations-data-machine-audit-quality-Linux-node24 contains exported Homeboy run history for deeper queries.
  • Drill-down: download the observation artifact, then run homeboy runs import <dir>, homeboy runs list, and homeboy runs findings <run-id>.
  • Artifacts are attached to the workflow run: https://github.com/Extra-Chill/data-machine/actions/runs/28339861732
Tooling versions
  • Homeboy CLI: homeboy 0.269.3+e2ca18f6b37e+03270118
  • Extension: wordpress from https://github.com/Extra-Chill/homeboy-extensions
  • Extension revision: e124cf1f
  • Action: unknown@unknown

@chubes4 chubes4 merged commit 9adedf9 into main Jun 30, 2026
5 checks passed
@chubes4 chubes4 deleted the fold-byref-into-turnstate branch June 30, 2026 14:50
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