apoc 0.4.0

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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Native invocation-history recording and workspace scoping.

use std::future::Future;
use std::pin::Pin;
use std::process::Command;
use std::sync::Arc;

use incurs::command::{CommandBuilder, CommandDef, TypedContext, TypedResult};
use incurs::errors::ValidationError;
use incurs::middleware::MiddlewareFn;
use incurs::schema::{FieldMeta, FieldType, IncurSchema};
use schemars::JsonSchema;
use serde::Serialize;
use serde_json::{Value, json};

use crate::model::{
    InvocationRecord, InvocationScopeKind, InvocationSource, InvocationStatus, InvocationSurface,
    now_ms,
};
use crate::process::controller;
use crate::store::Store;

#[derive(Clone)]
struct ActiveInvocation {
    id: String,
    command_path: String,
    store: Arc<Store>,
}

tokio::task_local! {
    static ACTIVE_INVOCATION: ActiveInvocation;
}

struct PurposeOptions<Options> {
    purpose: String,
    options: Options,
}

impl<Options: IncurSchema> IncurSchema for PurposeOptions<Options> {
    fn fields() -> Vec<FieldMeta> {
        let mut fields = Options::fields();
        if let Some(field) = fields.iter_mut().find(|field| field.name == "purpose") {
            field.required = true;
            field.description = Some(
                "Brief user-facing summary of what this apoc call is doing. Never include secrets.",
            );
        } else {
            fields.push(purpose_field());
        }
        fields
    }

    fn from_raw(raw: &std::collections::BTreeMap<String, Value>) -> Result<Self, ValidationError> {
        let purpose = raw
            .get("purpose")
            .and_then(Value::as_str)
            .ok_or_else(|| purpose_error("Purpose is required"))?;
        let purpose = purpose.trim();
        if purpose.is_empty() {
            return Err(purpose_error("Purpose cannot be empty"));
        }
        if purpose.chars().any(char::is_control) {
            return Err(purpose_error(
                "Purpose must be one line without control characters",
            ));
        }
        if purpose.chars().count() > 120 {
            return Err(purpose_error("Purpose must be 120 characters or fewer"));
        }
        let mut normalized = raw.clone();
        normalized.insert("purpose".to_string(), Value::String(purpose.to_string()));
        Ok(Self {
            purpose: purpose.to_string(),
            options: Options::from_raw(&normalized)?,
        })
    }
}

fn purpose_field() -> FieldMeta {
    FieldMeta {
        name: "purpose",
        cli_name: "purpose".to_string(),
        description: Some(
            "Brief user-facing summary of what this apoc call is doing. Never include secrets.",
        ),
        field_type: FieldType::String,
        required: true,
        default: None,
        alias: None,
        deprecated: false,
        env_name: None,
    }
}

fn purpose_error(message: &str) -> ValidationError {
    ValidationError {
        message: message.to_string(),
        field_errors: Vec::new(),
        cause: None,
    }
}

/// Returns the validated purpose attached to the current typed command context.
pub(crate) fn invocation_purpose(globals: &Value) -> &str {
    globals
        .get("purpose")
        .and_then(Value::as_str)
        .expect("tracked apoc commands always carry a validated purpose")
}

/// Resolved durable scope for one invocation working directory.
pub(crate) struct InvocationScope {
    /// Canonical working directory, when available.
    pub(crate) cwd: Option<String>,
    /// Scope classification.
    pub(crate) kind: InvocationScopeKind,
    /// Exact worktree or directory key.
    pub(crate) key: Option<String>,
}

/// Returns the invocation currently executing inside the typed command graph.
pub(crate) fn active_invocation_id() -> Option<String> {
    ACTIVE_INVOCATION.try_with(|active| active.id.clone()).ok()
}

/// Returns the invocation store currently executing inside the typed command graph.
pub(crate) fn active_store() -> Option<Arc<Store>> {
    ACTIVE_INVOCATION
        .try_with(|active| Arc::clone(&active.store))
        .ok()
}

/// Resolves a local history scope from an invocation working directory.
pub(crate) fn invocation_scope(cwd: Option<&str>) -> InvocationScope {
    let Some(cwd) = cwd else {
        return InvocationScope {
            cwd: None,
            kind: InvocationScopeKind::Unknown,
            key: None,
        };
    };
    let cwd = std::fs::canonicalize(cwd)
        .ok()
        .unwrap_or_else(|| std::path::PathBuf::from(cwd));
    let cwd = cwd.to_string_lossy().into_owned();
    let worktree = Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .current_dir(&cwd)
        .output()
        .ok()
        .filter(|output| output.status.success())
        .and_then(|output| String::from_utf8(output.stdout).ok())
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
        .and_then(|value| {
            std::fs::canonicalize(value)
                .ok()
                .map(|path| path.to_string_lossy().into_owned())
        });
    match worktree {
        Some(worktree) => InvocationScope {
            cwd: Some(cwd),
            kind: InvocationScopeKind::GitWorktree,
            key: Some(worktree),
        },
        None => InvocationScope {
            cwd: Some(cwd.clone()),
            kind: InvocationScopeKind::Cwd,
            key: Some(cwd),
        },
    }
}

/// Records every command that enters the shared CLI, MCP, or Code Mode graph.
pub(crate) fn invocation_middleware(store: Option<Arc<Store>>) -> MiddlewareFn {
    Arc::new(move |context, next| {
        let configured_store = store.clone();
        Box::pin(async move {
            let store = configured_store.or_else(|| Store::open_default().ok().map(Arc::new));
            let Some(store) = store else {
                next().await;
                return;
            };
            let controller = controller(context.agent);
            let parent_id = active_invocation_id();
            let scope = invocation_scope(controller.cwd.as_deref());
            let surface = match context
                .request
                .as_ref()
                .map(|request| request.method.as_str())
            {
                Some("code") => InvocationSurface::Code,
                Some(_) => InvocationSurface::Mcp,
                None => InvocationSurface::Cli,
            };
            let at = now_ms();
            let invocation = InvocationRecord {
                id: controller.invocation_id,
                parent_id,
                execution_id: controller.execution_id,
                source: InvocationSource::Native,
                source_id: None,
                source_order: None,
                source_path: None,
                accepted_sequence: 0,
                surface,
                actor: Some(controller.actor),
                agent: controller.agent,
                command_path: context.command.clone(),
                purpose: None,
                normalized_input: serde_json::json!({}),
                search_text: context.command.clone(),
                pattern_signature: String::new(),
                variable_paths: Vec::new(),
                cwd: scope.cwd,
                scope_kind: scope.kind,
                scope_key: scope.key,
                started_at: at,
                completed_at: None,
                updated_at: at,
                status: InvocationStatus::Running,
                exit_code: None,
                error_code: None,
                error: None,
            };
            if let Err(error) = store.start_invocation(&invocation) {
                tracing::warn!(error = %error, "could not record apoc invocation");
                next().await;
                return;
            }
            let active = ActiveInvocation {
                id: invocation.id.clone(),
                command_path: invocation.command_path,
                store: Arc::clone(&store),
            };
            ACTIVE_INVOCATION.scope(active, next()).await;
            if store
                .get_invocation(&invocation.id)
                .ok()
                .flatten()
                .is_some_and(|record| record.status == InvocationStatus::Running)
            {
                let _ = store.finish_invocation(
                    &invocation.id,
                    InvocationStatus::Incomplete,
                    Some(now_ms()),
                    None,
                    None,
                    None,
                );
            }
        })
    })
}

/// Builds one typed command whose normalized input and outcome update history.
pub(crate) fn tracked_typed<Args, Options, Env, Output, Handler, HandlerFuture>(
    name: impl Into<String>,
    handler: Handler,
) -> CommandBuilder
where
    Args: incurs::schema::IncurSchema + Serialize + Send + Sync + 'static,
    Options: incurs::schema::IncurSchema + Serialize + Send + Sync + 'static,
    Env: incurs::schema::IncurSchema + Serialize + Send + Sync + 'static,
    Output: JsonSchema + Serialize + Send + Sync + 'static,
    Handler: Fn(TypedContext<Args, Options, Env>) -> HandlerFuture + Send + Sync + 'static,
    HandlerFuture: Future<Output = TypedResult<Output>> + Send + 'static,
{
    let handler = Arc::new(handler);
    CommandDef::typed::<Args, PurposeOptions<Options>, Env, Output, _, _>(name, move |context| {
        let handler = Arc::clone(&handler);
        let TypedContext {
            agent,
            args,
            display_name,
            env,
            mut globals,
            options,
            request,
            format,
            format_explicit,
            name,
            vars,
            version,
        } = context;
        let PurposeOptions { purpose, options } = options;
        let mut serialized_options = serde_json::to_value(&options).unwrap_or(Value::Null);
        if let Some(object) = serialized_options.as_object_mut() {
            object.remove("purpose");
        }
        let input = redact_history_input(serde_json::json!({
            "purpose": &purpose,
            "args": &args,
            "options": serialized_options,
        }));
        if !globals.is_object() {
            globals = json!({});
        }
        globals
            .as_object_mut()
            .expect("globals was normalized to an object")
            .insert("purpose".to_string(), Value::String(purpose.clone()));
        let context = TypedContext {
            agent,
            args,
            display_name,
            env,
            globals,
            options,
            request,
            format,
            format_explicit,
            name,
            vars,
            version,
        };
        Box::pin(async move {
            let active = ACTIVE_INVOCATION.try_with(Clone::clone).ok();
            if let Some(active) = &active {
                let search_text = format!(
                    "{} {}",
                    active.command_path,
                    serde_json::to_string(&input).unwrap_or_default()
                );
                let _ = active.store.update_invocation_input(
                    &active.id,
                    Some(purpose),
                    input,
                    search_text,
                );
            }
            let result = handler(context).await;
            if let Some(active) = active {
                finish(&active, &result);
            }
            result
        }) as Pin<Box<dyn Future<Output = TypedResult<Output>> + Send>>
    })
}

/// Removes secret material before typed input reaches durable history, search, or patterns.
///
/// Secret-bearing command fields are deliberately represented only by their destination
/// names. Provider names, provider references, versions, and literal values remain in memory.
fn redact_history_input(mut value: Value) -> Value {
    let mut secret_material = Vec::new();
    collect_secret_material(&value, None, &mut secret_material);
    secret_material.sort_by_key(|value| std::cmp::Reverse(value.len()));
    secret_material.dedup();
    let redacted_purpose = value.get("purpose").and_then(Value::as_str).map(|purpose| {
        secret_material
            .iter()
            .filter(|secret| !secret.is_empty())
            .fold(purpose.to_string(), |value, secret| {
                value.replace(secret, "[REDACTED]")
            })
    });
    if let Some(redacted) = redacted_purpose {
        value["purpose"] = Value::String(redacted);
    }
    redact_history_value(&mut value);
    value
}

fn collect_secret_material(value: &Value, key: Option<&str>, material: &mut Vec<String>) {
    match value {
        Value::Array(values) => {
            for value in values {
                collect_secret_material(value, key, material);
            }
        }
        Value::Object(object) => {
            if key.is_some_and(is_secret_field) {
                for value in object.values() {
                    collect_secret_reference(value, material);
                }
            }
            for (key, value) in object {
                collect_secret_material(value, Some(key), material);
            }
        }
        Value::String(value) if key.is_some_and(is_secret_field) => {
            collect_secret_reference(&Value::String(value.clone()), material);
        }
        _ => {}
    }
}

fn collect_secret_reference(value: &Value, material: &mut Vec<String>) {
    match value {
        Value::String(value) => {
            let reference = value
                .split_once('=')
                .map_or(value.as_str(), |(_, value)| value);
            material.push(reference.to_string());
            if let Some((_, reference)) = reference.split_once(':') {
                material.push(reference.to_string());
            }
        }
        Value::Array(values) => {
            for value in values {
                collect_secret_reference(value, material);
            }
        }
        Value::Object(object) => {
            let provider = object.get("provider").and_then(Value::as_str);
            let reference = object.get("reference").and_then(Value::as_str);
            if let Some(reference) = reference {
                material.push(reference.to_string());
                if let Some(provider) = provider {
                    material.push(format!("{provider}:{reference}"));
                }
            } else {
                for value in object.values() {
                    collect_secret_reference(value, material);
                }
            }
        }
        _ => {}
    }
}

fn redact_history_value(value: &mut Value) {
    match value {
        Value::Array(values) => {
            for value in values {
                redact_history_value(value);
            }
        }
        Value::Object(object) => {
            for (key, value) in object {
                if key == "reference" {
                    *value = json!({ "redacted": true });
                } else if is_secret_field(key) {
                    *value = redacted_secret_entries(value);
                } else {
                    redact_history_value(value);
                }
            }
        }
        _ => {}
    }
}

fn is_secret_field(key: &str) -> bool {
    matches!(
        key,
        "secret_env"
            | "secretEnv"
            | "secret_header"
            | "secretHeader"
            | "secret_headers"
            | "secretHeaders"
            | "secrets"
    )
}

fn redacted_secret_entries(value: &Value) -> Value {
    let mut names = Vec::new();
    collect_secret_names(value, &mut names);
    names.sort();
    names.dedup();
    Value::Array(
        names
            .into_iter()
            .map(|name| json!({ "name": name, "redacted": true }))
            .collect(),
    )
}

fn collect_secret_names(value: &Value, names: &mut Vec<String>) {
    match value {
        Value::Null => {}
        Value::String(value) => {
            let name = value
                .split_once('=')
                .map_or(value.as_str(), |(name, _)| name);
            if !name.is_empty() {
                names.push(name.to_string());
            }
        }
        Value::Array(values) => {
            for value in values {
                collect_secret_names(value, names);
            }
        }
        Value::Object(object) => {
            names.extend(object.keys().filter(|name| !name.is_empty()).cloned());
        }
        _ => {}
    }
}

fn finish<Output: Serialize>(active: &ActiveInvocation, result: &TypedResult<Output>) {
    let (status, exit_code, error_code, error) = match result {
        TypedResult::Ok {
            data, exit_code, ..
        } => {
            link_execution(active, data);
            (
                if exit_code.is_some_and(|code| code != 0) {
                    InvocationStatus::Failed
                } else {
                    InvocationStatus::Succeeded
                },
                *exit_code,
                exit_code
                    .filter(|code| *code != 0)
                    .map(|_| "NONZERO_EXIT".to_string()),
                None,
            )
        }
        TypedResult::Error {
            code,
            message,
            exit_code,
            ..
        } => (
            InvocationStatus::Failed,
            *exit_code,
            Some(code.clone()),
            Some(message.clone()),
        ),
    };
    if let Err(error) = active.store.finish_invocation(
        &active.id,
        status,
        Some(now_ms()),
        exit_code,
        error_code,
        error,
    ) {
        tracing::warn!(error = %error, "could not finish apoc invocation history");
    }
}

fn link_execution<Output: Serialize>(active: &ActiveInvocation, output: &Output) {
    let Ok(output) = serde_json::to_value(output) else {
        return;
    };
    let execution_id = output
        .get("execution")
        .and_then(|value| value.get("id"))
        .or_else(|| output.get("id"))
        .and_then(serde_json::Value::as_str);
    let Some(execution_id) = execution_id else {
        return;
    };
    let _ = active
        .store
        .link_invocation_execution(&active.id, execution_id);
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::redact_history_input;

    #[test]
    fn history_input_keeps_only_secret_destination_names() {
        let redacted = redact_history_input(json!({
            "args": {},
            "options": {
                "secret_env": ["OPENAI_API_KEY=env:APOC_HISTORY_SENTINEL"],
                "secret_headers": {
                    "Authorization": {
                        "provider": "env",
                        "reference": "APOC_PROVIDER_SENTINEL",
                        "version": null
                    }
                },
                "nested": {
                    "secrets": {
                        "TOKEN": {
                            "provider": "env",
                            "reference": "APOC_NESTED_SENTINEL"
                        }
                    }
                }
            }
        }));

        let encoded = serde_json::to_string(&redacted).expect("serialize redacted input");
        assert!(encoded.contains("OPENAI_API_KEY"));
        assert!(encoded.contains("Authorization"));
        assert!(encoded.contains("TOKEN"));
        assert!(!encoded.contains("env:APOC_HISTORY_SENTINEL"));
        assert!(!encoded.contains("APOC_PROVIDER_SENTINEL"));
        assert!(!encoded.contains("APOC_NESTED_SENTINEL"));
        assert!(!encoded.contains("provider"));
        assert!(!encoded.contains("reference"));
    }

    #[test]
    fn history_input_preserves_non_secret_environment_values() {
        let input = json!({
            "options": {
                "env": ["RUST_LOG=debug"],
                "secret_env": null
            }
        });

        let redacted = redact_history_input(input);
        assert_eq!(redacted["options"]["env"], json!(["RUST_LOG=debug"]));
        assert_eq!(redacted["options"]["secret_env"], json!([]));
    }

    #[test]
    fn purpose_redacts_recognized_secret_references_before_history() {
        let redacted = redact_history_input(json!({
            "purpose": "Checking env:APOC_PURPOSE_SENTINEL without exposing APOC_PURPOSE_SENTINEL.",
            "args": {},
            "options": {
                "secret_env": ["TOKEN=env:APOC_PURPOSE_SENTINEL"]
            }
        }));

        let encoded = serde_json::to_string(&redacted).unwrap();
        assert!(!encoded.contains("APOC_PURPOSE_SENTINEL"));
        assert!(encoded.contains("[REDACTED]"));
        assert!(encoded.contains("TOKEN"));
    }
}