apoc 0.9.1

Agentic process control for LLMs
Documentation
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use std::collections::BTreeMap;
use std::io::{BufRead, BufReader, Read, Write};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::thread;

use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use thiserror::Error;

pub const EXTENSION_PROTOCOL_VERSION: u64 = 1;
pub const DEFAULT_EXTENSION_HOST_NAME: &str = "com.douglance.apoc";
pub const DEFAULT_EXTENSION_ID: &str = "apoc-agent-probe";
pub const MAX_NATIVE_MESSAGE_BYTES: usize = 768 * 1024;
pub const MAX_CHUNK_COUNT: usize = 4096;
pub const MAX_PENDING_ASSEMBLIES: usize = 64;

#[derive(Debug, Error)]
pub enum HostError {
    #[error("failed to encode JSON: {0}")]
    JsonEncode(serde_json::Error),
    #[error("failed to decode JSON: {0}")]
    JsonDecode(serde_json::Error),
    #[error("invalid native message size: {0}")]
    InvalidNativeMessageSize(usize),
    #[error("maxBytes must be a positive integer")]
    InvalidMaxBytes,
    #[error("maxBytes is too small for an extension chunk envelope")]
    MaxBytesTooSmall,
    #[error("extension message requires too many chunks: {0}")]
    TooManyChunks(usize),
    #[error("invalid extension chunk")]
    InvalidChunk,
    #[error("invalid extension chunk encoding")]
    InvalidChunkEncoding,
    #[error("invalid extension chunk utf-8")]
    InvalidChunkUtf8,
    #[error("inconsistent extension chunk total")]
    InconsistentChunkTotal,
    #[error("too many pending extension chunk assemblies")]
    TooManyPendingAssemblies,
    #[error("conflicting duplicate extension chunk")]
    ConflictingDuplicateChunk,
    #[error("daemon connection error: {0}")]
    DaemonConnect(String),
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostOptions {
    pub socket_path: PathBuf,
    pub extension_origin: Option<String>,
    pub host_name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ChunkEnvelope {
    pub v: u64,
    #[serde(rename = "type")]
    pub kind: String,
    pub id: String,
    pub index: usize,
    pub total: usize,
    pub data: String,
}

#[derive(Debug, Default)]
pub struct ChunkAssembler {
    assemblies: BTreeMap<String, Assembly>,
}

#[derive(Debug)]
struct Assembly {
    total: usize,
    parts: Vec<Option<String>>,
}

impl ChunkAssembler {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn accept(&mut self, value: Value) -> Result<Option<Value>, HostError> {
        if !is_chunk_candidate(&value) {
            return Ok(Some(value));
        }
        let chunk = parse_chunk(value)?;
        match self.assemblies.get(&chunk.id) {
            Some(existing) if existing.total != chunk.total => {
                return Err(HostError::InconsistentChunkTotal);
            }
            None if self.assemblies.len() >= MAX_PENDING_ASSEMBLIES => {
                return Err(HostError::TooManyPendingAssemblies);
            }
            _ => {}
        }

        let assembly = self
            .assemblies
            .entry(chunk.id.clone())
            .or_insert_with(|| Assembly {
                total: chunk.total,
                parts: vec![None; chunk.total],
            });
        if let Some(existing) = &assembly.parts[chunk.index]
            && existing != &chunk.data
        {
            return Err(HostError::ConflictingDuplicateChunk);
        }
        assembly.parts[chunk.index] = Some(chunk.data);
        if assembly.parts.iter().filter(|part| part.is_some()).count() != assembly.total {
            return Ok(None);
        }

        let assembly = self.assemblies.remove(&chunk.id).expect("assembly exists");
        let mut bytes = Vec::new();
        for part in assembly.parts {
            let part = part.ok_or(HostError::InvalidChunk)?;
            let decoded = BASE64
                .decode(part.as_bytes())
                .map_err(|_| HostError::InvalidChunkEncoding)?;
            bytes.extend_from_slice(&decoded);
        }
        let json = String::from_utf8(bytes).map_err(|_| HostError::InvalidChunkUtf8)?;
        serde_json::from_str(&json)
            .map(Some)
            .map_err(HostError::JsonDecode)
    }
}

pub fn split_native_message(
    id: &str,
    value: &Value,
    max_bytes: usize,
) -> Result<Vec<Value>, HostError> {
    if max_bytes == 0 {
        return Err(HostError::InvalidMaxBytes);
    }
    let bytes = serde_json::to_vec(value).map_err(HostError::JsonEncode)?;
    if bytes.len() <= max_bytes {
        return Ok(vec![value.clone()]);
    }

    let mut low = 1;
    let mut high = bytes.len().saturating_sub(1).min(max_bytes);
    let mut result = None;
    while low <= high {
        let chunk_bytes = (low + high) / 2;
        let candidate = make_chunks(id, &bytes, chunk_bytes)?;
        let fits = candidate
            .iter()
            .all(|chunk| serde_json::to_vec(chunk).is_ok_and(|encoded| encoded.len() <= max_bytes));
        if fits {
            result = Some(candidate);
            low = chunk_bytes + 1;
        } else {
            high = chunk_bytes.saturating_sub(1);
        }
    }
    result.ok_or(HostError::MaxBytesTooSmall)
}

pub fn encode_native_frame(value: &Value) -> Result<Vec<u8>, HostError> {
    let body = serde_json::to_vec(value).map_err(HostError::JsonEncode)?;
    if body.len() > MAX_NATIVE_MESSAGE_BYTES {
        return Err(HostError::InvalidNativeMessageSize(body.len()));
    }
    let mut frame = Vec::with_capacity(4 + body.len());
    frame.extend_from_slice(&(body.len() as u32).to_le_bytes());
    frame.extend_from_slice(&body);
    Ok(frame)
}

pub fn decode_native_frames(buffer: &mut Vec<u8>) -> Result<Vec<Value>, HostError> {
    let mut values = Vec::new();
    loop {
        if buffer.len() < 4 {
            return Ok(values);
        }
        let size = u32::from_le_bytes([buffer[0], buffer[1], buffer[2], buffer[3]]) as usize;
        if size > MAX_NATIVE_MESSAGE_BYTES {
            return Err(HostError::InvalidNativeMessageSize(size));
        }
        if buffer.len() < 4 + size {
            return Ok(values);
        }
        let body = buffer[4..4 + size].to_vec();
        buffer.drain(..4 + size);
        values.push(serde_json::from_slice(&body).map_err(HostError::JsonDecode)?);
    }
}

pub fn default_socket_path() -> PathBuf {
    if let Some(path) = std::env::var_os("APOC_EXTENSION_SOCKET") {
        return PathBuf::from(path);
    }
    runtime_dir().join("apoc-extension.sock")
}

pub fn parse_options(args: &[String]) -> HostOptions {
    let mut socket_path = default_socket_path();
    let mut host_name = std::env::var("APOC_EXTENSION_HOST_NAME")
        .unwrap_or_else(|_| DEFAULT_EXTENSION_HOST_NAME.to_string());
    let mut index = 0;
    while index < args.len() {
        match args[index].as_str() {
            "--socket" if index + 1 < args.len() => {
                socket_path = PathBuf::from(&args[index + 1]);
                index += 2;
            }
            "--host-name" if index + 1 < args.len() => {
                host_name = args[index + 1].clone();
                index += 2;
            }
            _ => index += 1,
        }
    }
    let extension_origin = args
        .iter()
        .find(|arg| arg.starts_with("chrome-extension://"))
        .cloned();
    HostOptions {
        socket_path,
        extension_origin,
        host_name,
    }
}

pub fn bridge_connect_message(options: &HostOptions) -> Value {
    let mut object = Map::new();
    object.insert(
        "cmd".to_string(),
        Value::String("ext.bridge.connect".to_string()),
    );
    object.insert(
        "hostName".to_string(),
        Value::String(options.host_name.clone()),
    );
    if let Some(origin) = &options.extension_origin {
        object.insert("extensionOrigin".to_string(), Value::String(origin.clone()));
    }
    Value::Object(object)
}

pub fn is_extension_host_envelope(value: &Value) -> bool {
    let Some(object) = value.as_object() else {
        return false;
    };
    if object.get("v").and_then(Value::as_u64) != Some(EXTENSION_PROTOCOL_VERSION) {
        return false;
    }
    match object.get("type").and_then(Value::as_str) {
        Some("hello") => {
            object.get("extensionId").and_then(Value::as_str).is_some()
                && object.get("version").and_then(Value::as_str).is_some()
        }
        Some("response") => {
            object.get("id").and_then(Value::as_str).is_some()
                && object.get("ok").and_then(Value::as_bool).is_some()
        }
        Some("event") => object.get("event").and_then(Value::as_object).is_some(),
        _ => false,
    }
}

pub fn is_extension_daemon_envelope(value: &Value) -> bool {
    let Some(object) = value.as_object() else {
        return false;
    };
    if object.get("v").and_then(Value::as_u64) != Some(EXTENSION_PROTOCOL_VERSION) {
        return false;
    }
    match object.get("type").and_then(Value::as_str) {
        Some("request") => {
            object.get("id").and_then(Value::as_str).is_some()
                && object.get("request").and_then(Value::as_object).is_some()
        }
        Some("ack") => object.get("id").is_none_or(Value::is_string),
        _ => false,
    }
}

pub fn run_stdio_host(args: &[String]) -> Result<(), HostError> {
    let options = parse_options(args);
    let daemon = match UnixStream::connect(&options.socket_path) {
        Ok(stream) => stream,
        Err(error) => {
            let event = lifecycle_event(format!("daemon connection error: {error}"));
            write_native_value(&mut std::io::stdout(), &event)?;
            return Err(HostError::DaemonConnect(error.to_string()));
        }
    };
    let daemon_writer = Arc::new(Mutex::new(daemon.try_clone()?));
    send_daemon_value(&daemon_writer, &bridge_connect_message(&options))?;

    let stdout = Arc::new(Mutex::new(std::io::stdout()));
    let daemon_stdout = Arc::clone(&stdout);
    thread::spawn(move || {
        let mut assembler = ChunkAssembler::new();
        let mut reader = BufReader::new(daemon);
        let mut line = String::new();
        loop {
            line.clear();
            match reader.read_line(&mut line) {
                Ok(0) | Err(_) => break,
                Ok(_) => {
                    if line.trim().is_empty() {
                        continue;
                    }
                    let Ok(parsed) = serde_json::from_str::<Value>(&line) else {
                        continue;
                    };
                    let Ok(Some(assembled)) = assembler.accept(parsed) else {
                        continue;
                    };
                    if is_extension_daemon_envelope(&assembled) {
                        let mut output = daemon_stdout.lock().expect("stdout lock poisoned");
                        let _ = write_native_value(&mut *output, &assembled);
                    }
                }
            }
        }
    });

    let mut assembler = ChunkAssembler::new();
    let mut buffer = Vec::new();
    let mut chunk = [0_u8; 8192];
    let mut input = std::io::stdin();
    loop {
        let bytes_read = input.read(&mut chunk)?;
        if bytes_read == 0 {
            break;
        }
        buffer.extend_from_slice(&chunk[..bytes_read]);
        for value in decode_native_frames(&mut buffer)? {
            let Some(assembled) = assembler.accept(value)? else {
                continue;
            };
            if is_extension_host_envelope(&assembled) {
                send_daemon_value(&daemon_writer, &assembled)?;
            }
        }
    }
    Ok(())
}

fn runtime_dir() -> PathBuf {
    if let Some(path) = std::env::var_os("APOC_RUNTIME_DIR") {
        return PathBuf::from(path);
    }
    std::env::temp_dir().join(format!("apoc-{}", unsafe { libc::geteuid() }))
}

fn is_chunk_candidate(value: &Value) -> bool {
    value
        .as_object()
        .and_then(|object| object.get("type"))
        .and_then(Value::as_str)
        == Some("chunk")
}

fn parse_chunk(value: Value) -> Result<ChunkEnvelope, HostError> {
    let chunk: ChunkEnvelope =
        serde_json::from_value(value).map_err(|_| HostError::InvalidChunk)?;
    if chunk.v != EXTENSION_PROTOCOL_VERSION
        || chunk.kind != "chunk"
        || chunk.id.is_empty()
        || chunk.total == 0
        || chunk.total > MAX_CHUNK_COUNT
        || chunk.index >= chunk.total
    {
        return Err(HostError::InvalidChunk);
    }
    Ok(chunk)
}

fn make_chunks(id: &str, bytes: &[u8], chunk_bytes: usize) -> Result<Vec<Value>, HostError> {
    if chunk_bytes == 0 {
        return Err(HostError::InvalidMaxBytes);
    }
    let total = bytes.len().div_ceil(chunk_bytes);
    if total > MAX_CHUNK_COUNT {
        return Err(HostError::TooManyChunks(total));
    }
    let mut chunks = Vec::with_capacity(total);
    for index in 0..total {
        let start = index * chunk_bytes;
        let end = (start + chunk_bytes).min(bytes.len());
        chunks.push(
            serde_json::to_value(ChunkEnvelope {
                v: EXTENSION_PROTOCOL_VERSION,
                kind: "chunk".to_string(),
                id: id.to_string(),
                index,
                total,
                data: BASE64.encode(&bytes[start..end]),
            })
            .map_err(HostError::JsonEncode)?,
        );
    }
    Ok(chunks)
}

fn send_daemon_value(writer: &Arc<Mutex<UnixStream>>, value: &Value) -> Result<(), HostError> {
    let messages = split_native_message("daemon", value, MAX_NATIVE_MESSAGE_BYTES)?;
    let mut stream = writer.lock().expect("daemon writer lock poisoned");
    for message in messages {
        serde_json::to_writer(&mut *stream, &message).map_err(HostError::JsonEncode)?;
        stream.write_all(b"\n")?;
    }
    stream.flush()?;
    Ok(())
}

fn write_native_value(writer: &mut dyn Write, value: &Value) -> Result<(), HostError> {
    for message in split_native_message("native", value, MAX_NATIVE_MESSAGE_BYTES)? {
        let frame = encode_native_frame(&message)?;
        writer.write_all(&frame)?;
    }
    writer.flush()?;
    Ok(())
}

fn lifecycle_event(message: String) -> Value {
    serde_json::json!({
        "v": EXTENSION_PROTOCOL_VERSION,
        "type": "event",
        "event": {
            "ts": now_ms(),
            "kind": "lifecycle",
            "message": message,
        }
    })
}

fn now_ms() -> u128 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|duration| duration.as_millis())
        .unwrap_or(0)
}