agent/php/Actions/Fleet/CompleteTask.php
Snider 429d1c0897 feat(agent/agentic): RFC foundation — atomic CompleteTask + credit ledger reconcile
Foundation slice for Mantis #841 php/Mod/Agent RFC implementation:

* CompleteTask now wraps in DB::transaction with idempotent credit awards
  and safe current_task_id clearing
* Credits/{Award,GetBalance,GetCreditHistory} updated for agent_id +
  fleet_task_id ledger support and richer balance totals
* GenerateCommand canonical agentic:generate wiring; legacy duplicate
  no longer registered
* Boot wires brain:clean / brain:prune / brain:reindex
* EmbedMemory exits early when memory already indexed
* 3 follow-on fleet migrations reconcile fleet_nodes pointer column,
  fleet_tasks/credit_entries fk/index hygiene, fleet+credit constraints
* 4 foundation tests under php/tests/Feature/Mod/Agent/

php -l clean on all modified files. pest unrunnable in sandbox (no vendor/).

Foundation slice only: remaining model/action parity, full MCP tool/
service sweep, fleet controller auth-context, and 41-tool/45-action
surface left for follow-up tickets.

Co-authored-by: Codex <noreply@openai.com>
Closes tasks.lthn.sh/view.php?id=841
2026-04-25 20:59:38 +01:00

109 lines
3.3 KiB
PHP

<?php
// SPDX-License-Identifier: EUPL-1.2
declare(strict_types=1);
namespace Core\Mod\Agentic\Actions\Fleet;
use Core\Actions\Action;
use Core\Mod\Agentic\Actions\Credits\AwardCredits;
use Core\Mod\Agentic\Models\FleetNode;
use Core\Mod\Agentic\Models\FleetTask;
use Illuminate\Support\Facades\DB;
/**
* Fleet tasks intentionally do not create AgentSession records. AgentSession tracks interactive,
* replayable, handoff-capable work with a work_log and artefact history; fleet tasks are atomic
* assign→complete events with no in-between state to replay. If a fleet task's work requires
* session semantics, the agent executing the task should start an AgentSession itself via
* AgentSessionService.
*/
class CompleteTask
{
use Action;
/**
* @param array<string, mixed> $result
* @param array<int, mixed> $findings
* @param array<string, mixed> $changes
* @param array<string, mixed> $report
*
* @throws \InvalidArgumentException
*/
public function handle(
int $workspaceId,
string $agentId,
int $taskId,
array $result = [],
array $findings = [],
array $changes = [],
array $report = []
): FleetTask {
return DB::transaction(function () use (
$workspaceId,
$agentId,
$taskId,
$result,
$findings,
$changes,
$report,
): FleetTask {
$node = FleetNode::query()
->where('workspace_id', $workspaceId)
->where('agent_id', $agentId)
->lockForUpdate()
->first();
$fleetTask = FleetTask::query()
->where('workspace_id', $workspaceId)
->lockForUpdate()
->find($taskId);
if (! $node instanceof FleetNode || ! $fleetTask instanceof FleetTask) {
throw new \InvalidArgumentException('Fleet task not found');
}
if ($fleetTask->fleet_node_id !== null && $fleetTask->fleet_node_id !== $node->id) {
throw new \InvalidArgumentException('Fleet task does not belong to this node');
}
$status = ($result['status'] ?? '') === 'failed'
? FleetTask::STATUS_FAILED
: FleetTask::STATUS_COMPLETED;
$fleetTask->update([
'status' => $status,
'result' => $result,
'findings' => $findings,
'changes' => $changes,
'report' => $report,
'completed_at' => now(),
]);
$creditAmount = max(1, count($findings) + 1);
AwardCredits::run(
$workspaceId,
$agentId,
'fleet-task',
$creditAmount,
$node->id,
'Fleet task completed',
$fleetTask->id,
);
$nodeUpdate = [
'last_heartbeat_at' => now(),
];
if ($node->current_task_id === null || $node->current_task_id === $fleetTask->id) {
$nodeUpdate['status'] = FleetNode::STATUS_ONLINE;
$nodeUpdate['current_task_id'] = null;
}
$node->update($nodeUpdate);
return $fleetTask->fresh();
});
}
}