agent/php/Services/ShapeClassifier.php
Snider 060f5abb66 feat(agent/php): ProfileSelector + ShapeClassifier services per Phase 3 (#826)
ShapeClassifier::classify($ticket) returns 'A'|'B'|'C' per policy v1:
- (severity=critical OR priority=urgent) → A
- has tag in ['security','crypto','core'] → A
- (severity=major OR priority=high) → B
- everything else → C

ProfileSelector::pickFor($ticket) walks AgentProfile::active(), matches
capability tags case-insensitively against ticket.tags:
- Class A: cheapest matching profile (cost_class alphabetic order)
- Class B: any active profile with quota_headroom_pct >= 25
- Class C: deterministic round-robin via last_dispatched_at

Pest Unit tests cover Good (matching profile picked), Bad (no match → null),
Ugly (all profiles disabled → null), plus class A/B headroom gating + class C
round-robin determinism.

Codex note: php -l clean; pest skipped — no vendor/ at this repo root
(downstream lab/lthn.ai owns composer install).

Closes tasks.lthn.sh/view.php?id=826

Co-authored-by: Codex <noreply@openai.com>
2026-04-26 00:48:37 +01:00

113 lines
2.5 KiB
PHP

<?php
// SPDX-License-Identifier: EUPL-1.2
declare(strict_types=1);
namespace Core\Mod\Agentic\Services;
use ArrayAccess;
class ShapeClassifier
{
public const CLASS_A = 'A';
public const CLASS_B = 'B';
public const CLASS_C = 'C';
/**
* @var list<string>
*/
private const ESCALATION_CAPABILITIES = [
'security',
'crypto',
'core',
];
/**
* @param array<string, mixed>|object $ticket
* @return 'A'|'B'|'C'
*/
public function classify(mixed $ticket): string
{
$severity = $this->stringValue($ticket, 'severity');
$priority = $this->stringValue($ticket, 'priority');
if ($severity === 'critical' || $priority === 'urgent') {
return self::CLASS_A;
}
if (array_intersect($this->normaliseTags($this->value($ticket, 'tags')), self::ESCALATION_CAPABILITIES) !== []) {
return self::CLASS_A;
}
if ($severity === 'major' || $priority === 'high') {
return self::CLASS_B;
}
return self::CLASS_C;
}
/**
* @param array<string, mixed>|object $ticket
*/
private function stringValue(mixed $ticket, string $key): string
{
$value = $this->value($ticket, $key);
return is_string($value)
? strtolower(trim($value))
: '';
}
/**
* @param array<string, mixed>|object $ticket
*/
private function value(mixed $ticket, string $key): mixed
{
if (is_array($ticket)) {
return $ticket[$key] ?? null;
}
if ($ticket instanceof ArrayAccess && $ticket->offsetExists($key)) {
return $ticket[$key];
}
if (is_object($ticket) && property_exists($ticket, $key)) {
return $ticket->{$key};
}
return null;
}
/**
* @return list<string>
*/
private function normaliseTags(mixed $tags): array
{
if (! is_array($tags)) {
return [];
}
$normalised = [];
foreach ($tags as $key => $value) {
if (is_string($key) && $value) {
$tag = strtolower(trim($key));
if ($tag !== '') {
$normalised[] = $tag;
}
}
if (is_string($value)) {
$tag = strtolower(trim($value));
if ($tag !== '') {
$normalised[] = $tag;
}
}
}
return array_values(array_unique($normalised));
}
}