agent/php/Actions/Credits/AwardCredits.php
Snider 83df8ad71a fix(agent): address CodeRabbit + SonarCloud findings on PR #6
20+ CHANGES_REQUESTED dispositions across PHP MCP services, Go pkg/agentic,
hermes_runner_mcp Python server, plugin shell scripts.

Highlights:
- DatabaseSchema.php: identifier quoting
- AwardCredits.php: task row locking order
- CreditTransaction.php: fail-fast row decoding
- OpenApiGenerator.php: YAML parse handling + uri query params
- CaptureDispatchResultJob.php: AgentProfile namespace fix
- CreditsController.php: missing workspace_id fail-closed
- QueryAuditService.php: prose query false positives + unbounded aggregation
- McpHealthService.php: proc_close after timeout + env var resolution
- CreditLedger.php + FleetOverview.php: workspace agent + dispatch target validation
- McpAgentServerCommand.php: quota burn on failed tool calls
- McpMetricsService.php: N-day window consistency
- hermes_runner_mcp: API key off command line + invalid method+id + run_id encoding
- CircuitBreaker.php: extracted CircuitOpenException class with autoload-correct placement
- pkg/agentic + brain + flow: SonarCloud sendMessage/fetchLoopRepoRefs/commitWorkspace/Connect annotations
- shell scripts: removed [[ usage for portability

43 files modified, 1 new (CircuitOpenException.php).

Verification: gofmt -w + php -l + python3 -m py_compile + bash -n all clean.
Touched-package go test passes (pkg/lib/flow, pkg/lib).
Full go test ./... blocked by pre-existing dappco.re module graph drift, out of scope.

Parked for separate work:
- Mantis #1062: go.mod local replace removal (cross-repo architectural)
- Mantis #1063: Sonar residual line-length / duplication quality-gate cluster

Closes findings on https://github.com/dAppCore/agent/pull/6

Co-authored-by: Codex <noreply@openai.com>
2026-04-27 13:39:24 +01:00

99 lines
3.2 KiB
PHP

<?php
// SPDX-License-Identifier: EUPL-1.2
declare(strict_types=1);
namespace Core\Mod\Agentic\Actions\Credits;
use Core\Actions\Action;
use Core\Mod\Agentic\Models\CreditEntry;
use Core\Mod\Agentic\Models\FleetNode;
use Core\Mod\Agentic\Models\FleetTask;
use Illuminate\Support\Facades\DB;
class AwardCredits
{
use Action;
/**
* @throws \InvalidArgumentException
*/
public function handle(
int $workspaceId,
string $agentId,
string $taskType,
int $amount,
?int $fleetNodeId = null,
?string $description = null,
?int $fleetTaskId = null,
): CreditEntry {
if ($agentId === '' || $taskType === '' || $amount === 0) {
throw new \InvalidArgumentException('agent_id, task_type, and non-zero amount are required');
}
return DB::transaction(function () use (
$workspaceId,
$agentId,
$taskType,
$amount,
$fleetNodeId,
$description,
$fleetTaskId,
): CreditEntry {
$node = $fleetNodeId !== null
? FleetNode::query()->where('workspace_id', $workspaceId)->lockForUpdate()->find($fleetNodeId)
: FleetNode::query()
->where('workspace_id', $workspaceId)
->where('agent_id', $agentId)
->lockForUpdate()
->first();
if (! $node instanceof FleetNode) {
throw new \InvalidArgumentException('Fleet node not found');
}
if ($fleetTaskId !== null) {
$fleetTask = FleetTask::query()
->where('workspace_id', $workspaceId)
->lockForUpdate()
->find($fleetTaskId);
if (! $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 the node being credited');
}
$existing = CreditEntry::query()
->where('workspace_id', $workspaceId)
->where('fleet_node_id', $node->id)
->where('fleet_task_id', $fleetTaskId)
->first();
if ($existing instanceof CreditEntry) {
return $existing;
}
}
$previousBalance = (int) CreditEntry::query()
->where('workspace_id', $workspaceId)
->where('agent_id', $node->agent_id)
->latest('id')
->value('balance_after');
return CreditEntry::query()->create([
'workspace_id' => $workspaceId,
'fleet_node_id' => $node->id,
'fleet_task_id' => $fleetTaskId,
'agent_id' => $node->agent_id,
'task_type' => $taskType,
'amount' => $amount,
'balance_after' => $previousBalance + $amount,
'description' => $description,
]);
});
}
}