Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions inc/Engine/AI/DataMachineHandlerCompletionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ public function recordToolResult( string $tool_name, ?array $tool_def, array $to
* @inheritDoc
*/
public function recordNaturalCompletion( array $messages, string $assistant_text, array $runtime_context, int $turn_count ): WP_Agent_Conversation_Completion_Decision {
$remaining_handlers = $this->remainingConfiguredHandlers();
if ( ! empty( $remaining_handlers ) ) {
return WP_Agent_Conversation_Completion_Decision::incomplete(
'AIConversationLoop: Configured handler completion missing, nudging continuation',
array(
'turn_count' => $turn_count,
'remaining_handlers' => $remaining_handlers,
'configured_handlers' => $this->configured_handlers,
'continuation_message' => DataMachineCompletionAssertions::buildNudge( array( 'handler_slugs' => $remaining_handlers ), $messages ),
)
);
}

$evaluation = $this->assertions->evaluate( $runtime_context, $assistant_text );
if ( $evaluation['complete'] ) {
return WP_Agent_Conversation_Completion_Decision::complete(
Expand All @@ -137,6 +150,19 @@ public function recordNaturalCompletion( array $messages, string $assistant_text
);
}

/**
* Return configured handler slugs that have not completed yet.
*
* @return array<int,string>
*/
private function remainingConfiguredHandlers(): array {
if ( empty( $this->configured_handlers ) ) {
return array();
}

return array_values( array_diff( $this->configured_handlers, array_unique( $this->executed_handler_slugs ) ) );
}

/**
* Keep handler-style tools from ending a run before generic completion
* assertions are satisfied.
Expand Down
10 changes: 10 additions & 0 deletions tests/agent-conversation-runtime-policy-smoke.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ function runtime_policy_first_event_payload( RuntimePolicySmokeEventSink $sink,
assert_runtime_policy( $second_decision->isComplete(), 'handler policy completes after all configured handlers fire' );
assert_runtime_policy( array( 'wordpress_publish', 'pinterest_publish' ) === array_values( $second_decision->context()['executed_handlers'] ?? array() ), 'handler policy reports executed handlers' );

$natural_before_handler_policy = new DataMachineHandlerCompletionPolicy( array( 'wiki_upsert' ) );
$natural_before_handler_decision = $natural_before_handler_policy->recordNaturalCompletion(
array( array( 'role' => 'user', 'content' => 'write the wiki article' ) ),
'I summarized the source.',
array( 'mode' => 'pipeline' ),
1
);
assert_runtime_policy( ! $natural_before_handler_decision->isComplete(), 'handler policy rejects natural completion before configured handlers fire' );
assert_runtime_policy( array( 'wiki_upsert' ) === ( $natural_before_handler_decision->context()['remaining_handlers'] ?? null ), 'handler policy reports remaining handlers on natural completion' );

// 2. Injected completion/transcript collaborators steer the loop without leaking into provider payloads.
$natural_dispatch_count = 0;
WpAiClientTestDouble::reset();
Expand Down
Loading