-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
135 lines (118 loc) · 4.12 KB
/
main.php
File metadata and controls
135 lines (118 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
/*
* leases — sandboxed on-call agent. Lease-gated shell, reasoning streamed.
*
* RFC §15.4 (permission challenge), §15.5 (lease lifecycle), §11.4
* (kind: thought streams).
*/
require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/agent.php';
use Arcp\Client\ARCPClient;
use Arcp\Clock\SystemClock;
use Arcp\Envelope\Envelope;
use Arcp\Errors\ARCPException;
use Arcp\Errors\ErrorCode;
use Arcp\Errors\PermissionDeniedException;
use Arcp\Ids\MessageId;
use Arcp\Ids\StreamId;
use Arcp\Messages\Streaming\StreamChunk;
use Arcp\Messages\Streaming\StreamKind;
use Arcp\Messages\Streaming\StreamOpen;
use function Arcp\Samples\Leases\llmLoop;
use Arcp\Samples\Leases\LLMStep;
const READ_BINARIES = ['/usr/bin/journalctl', '/usr/bin/cat', '/usr/bin/ss', '/usr/bin/ps'];
const WRITE_BINARIES = ['/usr/bin/systemctl', '/usr/bin/kill'];
const READ_LEASE_SECONDS = 30 * 60;
const WRITE_LEASE_SECONDS = 60;
/**
* @param list<string> $argv
*
* @return array{0: string, 1: string, 2: string, 3: int}
*/
function classify(array $argv, string $host): array
{
if ($argv === []) {
throw new PermissionDeniedException('binary', '?', 'empty argv');
}
$binary = $argv[0];
if (in_array($binary, READ_BINARIES, true)) {
return ['host.read', "host:{$host}", 'read', READ_LEASE_SECONDS];
}
if (in_array($binary, WRITE_BINARIES, true)) {
$target = $binary === '/usr/bin/systemctl' ? ($argv[2] ?? '?') : ($argv[1] ?? '?');
return ['host.write', "host:{$host}/{$binary}/{$target}", 'write', WRITE_LEASE_SECONDS];
}
throw new PermissionDeniedException('binary', $binary, "binary not allowed: {$binary}");
}
function acquireLease(
ARCPClient $client,
string $permission,
string $resource,
string $operation,
int $seconds,
string $reason,
): string {
// Sends `permission.request`; awaits `lease.granted` / `permission.deny`.
// Implemented in production via $client->requestPermission(...).
throw new \RuntimeException('not implemented');
}
/**
* @param list<string> $argv
*/
function runCommand(ARCPClient $client, array $argv, string $reason, string $host): string
{
[$permission, $resource, $operation, $seconds] = classify($argv, $host);
$lease = acquireLease($client, $permission, $resource, $operation, $seconds, $reason);
// The lease is the only guard. Spawn the subprocess elsewhere.
return '<would run ' . implode(' ', $argv) . " under lease {$lease}>";
}
function emitThought(ARCPClient $client, StreamId $streamId, int $sequence, string $text): void
{
$env = new Envelope(
id: MessageId::random(),
payload: new StreamChunk(sequence: $sequence, role: 'assistant_thought', content: $text),
timestamp: new SystemClock()->now(),
sessionId: $client->session->sessionId,
streamId: $streamId,
);
$client->session->transport->send($env);
}
function main(): void
{
/** @var ARCPClient $client */
$client = elided(); // transport, identity (constrained), auth elided
$streamId = StreamId::random();
$client->session->transport->send(new Envelope(
id: MessageId::random(),
payload: new StreamOpen(StreamKind::Thought),
timestamp: new SystemClock()->now(),
sessionId: $client->session->sessionId,
streamId: $streamId,
));
$seq = 0;
foreach (llmLoop('api-gateway pod is OOMing every 4 minutes') as $step) {
/** @var LLMStep $step */
emitThought($client, $streamId, $seq++, $step->thought);
if ($step->toolCall !== null) {
try {
runCommand($client, $step->toolCall->argv, $step->toolCall->reason, 'edge-pod-04');
} catch (ARCPException $e) {
if ($e->code() === ErrorCode::PermissionDenied) {
continue; // feeds back into the next prompt
}
throw $e;
}
}
if ($step->final !== null) {
echo $step->final, "\n";
break;
}
}
$client->close();
}
function elided(): ARCPClient
{
throw new \RuntimeException('not implemented');
}
main();