supercode-harness 0.4.11

The optional native Supercode agent and tool harness
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
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use serde_json::{json, Value};
use supercode_harness::mcp;
use supercode_harness::server::{RpcEngine, RuntimeSubmitError};
use supercode_harness::tools::{ToolContext, ToolRegistry};
use supercode_harness::{
    Agent, ChatMessage, ChatRequest, Config, FrontendElicitationAction, FrontendResponse,
    FrontendRuntimeError, HarnessId, HarnessSessionService, HttpFrontendRuntime, Provider,
    SdkErrorCode, SdkOperation, SdkRequest, SdkRuntime, SdkService, Usage,
};

fn repo_root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../..")
        .canonicalize()
        .unwrap()
}

fn fixture_locator() -> Value {
    json!({
        "harness": HarnessId::PI,
        "session_id": "1e6f2a3b-0000-4000-8000-000000000001",
        "storage": {
            "kind": "file",
            "path": repo_root().join("crates/harness/tests/fixtures/pi_session.jsonl"),
        },
    })
}

async fn json_sdk(
    service: &mut HarnessSessionService,
    operation: SdkOperation,
    params: Value,
) -> Value {
    let method = operation.method().unwrap();
    service
        .handle_async(json!({"jsonrpc":"2.0", "id":1, "method":method, "params":params}))
        .await
}

async fn mcp_sdk(registry: &ToolRegistry, operation: &str, params: Value) -> Value {
    let response = mcp::handle_request(
        registry,
        &ToolContext::new(repo_root()),
        &json!({
            "jsonrpc":"2.0",
            "id":1,
            "method":"tools/call",
            "params": {
                "name":"supercode_sdk",
                "arguments":{"operation":operation, "params":params},
            },
        }),
    )
    .await
    .unwrap();
    if response["result"]["isError"] == true {
        return response;
    }
    serde_json::from_str(response["result"]["content"][0]["text"].as_str().unwrap()).unwrap()
}

#[tokio::test]
async fn persisted_session_surfaces_share_identity_export_and_named_errors() {
    let locator = fixture_locator();
    let mut direct = HarnessSessionService::new();
    let mut json_api = HarnessSessionService::new();
    let mut registry = ToolRegistry::new();
    mcp::register_sdk_tool(&mut registry);

    let direct_load = direct
        .execute(SdkRequest {
            operation: SdkOperation::Load,
            params: json!({"locator":locator.clone()}),
        })
        .await
        .unwrap();
    let api_load = json_sdk(
        &mut json_api,
        SdkOperation::Load,
        json!({"locator":locator.clone()}),
    )
    .await["result"]
        .clone();
    let mcp_load = mcp_sdk(&registry, "load", json!({"locator":locator.clone()})).await;

    let load_rows = [
        ("rust-sdk", direct_load),
        ("service-json-rpc", api_load),
        ("mcp-tool", mcp_load),
    ];
    for (surface, value) in &load_rows {
        assert_eq!(
            value["session"]["session_id"], "1e6f2a3b-0000-4000-8000-000000000001",
            "stable identity drifted on {surface}"
        );
    }
    for (_, value) in &load_rows[1..] {
        assert_eq!(value, &load_rows[0].1);
    }

    let mut exports = Vec::new();
    for (surface, mut service) in [
        ("rust-sdk", HarnessSessionService::new()),
        ("service-json-rpc", HarnessSessionService::new()),
    ] {
        let value = if surface == "rust-sdk" {
            service
                .execute(SdkRequest {
                    operation: SdkOperation::Export,
                    params: json!({"locator":locator.clone(), "target_harness":"pi"}),
                })
                .await
                .unwrap()
        } else {
            json_sdk(
                &mut service,
                SdkOperation::Export,
                json!({"locator":locator.clone(), "target_harness":"pi"}),
            )
            .await["result"]
                .clone()
        };
        exports.push((
            surface,
            value["artifact"]["content"].as_str().unwrap().to_string(),
        ));
    }
    exports.push((
        "mcp-tool",
        mcp_sdk(
            &registry,
            "export",
            json!({"locator":locator, "target_harness":"pi"}),
        )
        .await["artifact"]["content"]
            .as_str()
            .unwrap()
            .to_string(),
    ));
    for (surface, content) in &exports[1..] {
        assert_eq!(content, &exports[0].1, "export drifted on {surface}");
    }

    let direct_error = HarnessSessionService::new()
        .execute(SdkRequest {
            operation: SdkOperation::Steer,
            params: json!({}),
        })
        .await
        .unwrap_err();
    assert_eq!(direct_error.code(), SdkErrorCode::UnsupportedAction);
    let json_error = json_sdk(
        &mut HarnessSessionService::new(),
        SdkOperation::Steer,
        json!({}),
    )
    .await;
    assert_eq!(json_error["error"]["name"], "unsupported_action");
    let mcp_error = mcp_sdk(&registry, "steer", json!({})).await;
    assert_eq!(mcp_error["result"]["isError"], true);
    assert_eq!(
        mcp_error["result"]["structuredContent"]["error"]["name"],
        "unsupported_action"
    );
    assert_eq!(
        mcp_error["result"]["structuredContent"]["error"]["operation"],
        "steer"
    );
}

struct BlockingProvider {
    entered: tokio::sync::Notify,
    release: tokio::sync::Notify,
}

impl BlockingProvider {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            entered: tokio::sync::Notify::new(),
            release: tokio::sync::Notify::new(),
        })
    }
}

struct SharedProvider(Arc<BlockingProvider>);

#[async_trait]
impl Provider for SharedProvider {
    async fn complete(
        &self,
        _request: &ChatRequest,
        on_delta: &(dyn for<'a> Fn(&'a str) + Send + Sync),
    ) -> supercode_harness::Result<(ChatMessage, Usage)> {
        self.0.entered.notify_one();
        self.0.release.notified().await;
        on_delta("same reply");
        Ok((ChatMessage::assistant("same reply"), Usage::default()))
    }
}

struct LiveObservation {
    session_id: String,
    events: Vec<(u64, String, Value)>,
    history: Vec<ChatMessage>,
    persisted: String,
}

async fn exercise_live(
    runtime: Arc<dyn SdkRuntime>,
    provider: Arc<BlockingProvider>,
    persisted: Arc<Mutex<String>>,
) -> LiveObservation {
    let mut attachment = runtime.attach(50).await.unwrap();
    let session_id = attachment.descriptor.session_id.clone();
    let active = {
        let runtime = runtime.clone();
        tokio::spawn(async move { runtime.submit("same prompt".into()).await })
    };
    provider.entered.notified().await;
    assert!(matches!(
        runtime.submit("busy prompt".into()).await,
        Err(FrontendRuntimeError::Submit(RuntimeSubmitError::Busy))
    ));
    assert!(matches!(
        runtime
            .respond(FrontendResponse::Other {
                request_id: 99,
                action: FrontendElicitationAction::Cancel,
                content: None,
            })
            .await,
        Err(FrontendRuntimeError::UnsupportedAction("respond"))
    ));
    provider.release.notify_waiters();
    assert_eq!(active.await.unwrap().unwrap(), "same reply");

    let mut events = Vec::new();
    loop {
        let event =
            tokio::time::timeout(std::time::Duration::from_secs(2), attachment.next_event())
                .await
                .unwrap()
                .unwrap();
        let terminal = event.kind == "turn_succeeded";
        events.push((event.sequence, event.kind, event.payload));
        if terminal {
            break;
        }
    }
    let history = runtime.attach(50).await.unwrap().history;
    let persisted = persisted.lock().unwrap().clone();
    LiveObservation {
        session_id,
        events,
        history,
        persisted,
    }
}

async fn exercise_attached_mcp(
    runtime: Arc<RpcEngine>,
    provider: Arc<BlockingProvider>,
    persisted: Arc<Mutex<String>>,
) -> LiveObservation {
    let mut registry = ToolRegistry::new();
    registry.register(mcp::SdkMcpTool::attached(runtime.clone()).await.unwrap());
    let registry = Arc::new(registry);

    let active = {
        let registry = registry.clone();
        tokio::spawn(
            async move { mcp_sdk(&registry, "input", json!({"prompt":"same prompt"})).await },
        )
    };
    provider.entered.notified().await;
    let busy = mcp_sdk(&registry, "input", json!({"prompt":"busy prompt"})).await;
    assert_eq!(busy["result"]["structuredContent"]["error"]["name"], "busy");
    assert_eq!(
        busy["result"]["structuredContent"]["error"]["operation"],
        "input"
    );
    let unsupported = mcp_sdk(
        &registry,
        "respond",
        json!({"response":{
            "kind":"other", "request_id":99, "action":"cancel", "content":null
        }}),
    )
    .await;
    assert_eq!(
        unsupported["result"]["structuredContent"]["error"]["name"],
        "unsupported_action"
    );
    assert_eq!(
        unsupported["result"]["structuredContent"]["error"]["operation"],
        "respond"
    );
    provider.release.notify_waiters();
    let completed = active.await.unwrap();
    assert_eq!(completed["session_id"], "stable-sdk-session");
    assert_eq!(completed["reply"], "same reply");

    let mut events = Vec::new();
    loop {
        let value = mcp_sdk(&registry, "events", json!({})).await;
        assert_eq!(value["session_id"], "stable-sdk-session");
        let event = &value["event"];
        let terminal = event["kind"] == "turn_succeeded";
        events.push((
            event["sequence"].as_u64().unwrap(),
            event["kind"].as_str().unwrap().to_string(),
            event["payload"].clone(),
        ));
        if terminal {
            break;
        }
    }
    LiveObservation {
        session_id: "stable-sdk-session".into(),
        events,
        history: runtime.attach(50).await.unwrap().history,
        persisted: persisted.lock().unwrap().clone(),
    }
}

fn scripted_engine(
    provider: Arc<BlockingProvider>,
    persisted: Arc<Mutex<String>>,
    cwd: &Path,
) -> Arc<RpcEngine> {
    let agent = Agent::with_provider(
        Config::builder()
            .cwd(cwd)
            .system_prompt("sdk conformance")
            .build(),
        Box::new(SharedProvider(provider)),
    );
    RpcEngine::new_named(
        agent,
        "stable-sdk-session",
        Some(Box::new(move |agent: &supercode_harness::SdkAgent| {
            *persisted.lock().unwrap() = serde_json::to_string(agent.history()).unwrap();
        })),
    )
}

#[tokio::test]
async fn local_and_http_live_surfaces_share_events_busy_and_persistence() {
    let fixture: Value = serde_json::from_str(include_str!(
        "../../../sdk/frontend/test/fixtures/conformance.json"
    ))
    .unwrap();
    assert_eq!(fixture["session_id"], "stable-sdk-session");
    assert_eq!(fixture["system"], "sdk conformance");
    assert_eq!(fixture["prompt"], "same prompt");
    assert_eq!(fixture["competing_prompt"], "busy prompt");
    assert_eq!(fixture["reply"], "same reply");
    let cwd =
        std::env::temp_dir().join(format!("supercode-sdk-conformance-{}", std::process::id()));
    std::fs::create_dir_all(&cwd).unwrap();

    let local_provider = BlockingProvider::new();
    let local_persisted = Arc::new(Mutex::new(String::new()));
    let local_engine = scripted_engine(local_provider.clone(), local_persisted.clone(), &cwd);
    let local = exercise_live(local_engine.clone(), local_provider, local_persisted).await;

    let http_provider = BlockingProvider::new();
    let http_persisted = Arc::new(Mutex::new(String::new()));
    let http_engine = scripted_engine(http_provider.clone(), http_persisted.clone(), &cwd);
    let token: Arc<str> = "sdk-conformance-token".into();
    let address =
        supercode_harness::server::run_http(http_engine.clone(), "127.0.0.1:0", token.clone())
            .await
            .unwrap();
    let http_runtime = HttpFrontendRuntime::connect(format!("http://{address}"), token.to_string())
        .await
        .unwrap();
    let http = exercise_live(http_runtime, http_provider, http_persisted).await;

    let mcp_provider = BlockingProvider::new();
    let mcp_persisted = Arc::new(Mutex::new(String::new()));
    let mcp_engine = scripted_engine(mcp_provider.clone(), mcp_persisted.clone(), &cwd);
    let mcp = exercise_attached_mcp(mcp_engine.clone(), mcp_provider, mcp_persisted).await;

    for (surface, observed) in [("local", &local), ("http", &http), ("mcp", &mcp)] {
        assert_eq!(observed.session_id, "stable-sdk-session", "{surface}");
        assert!(!observed.persisted.is_empty(), "{surface} did not persist");
        assert_eq!(
            observed
                .events
                .iter()
                .map(|(_, kind, _)| kind.as_str())
                .collect::<Vec<_>>(),
            fixture["event_kinds"]
                .as_array()
                .unwrap()
                .iter()
                .map(|value| value.as_str().unwrap())
                .collect::<Vec<_>>(),
            "{surface}"
        );
    }
    assert_eq!(local.events, http.events);
    assert_eq!(
        serde_json::to_value(&local.history).unwrap(),
        serde_json::to_value(&http.history).unwrap(),
        "local internal history may retain durable metadata that ChatMessage's public wire projection deliberately omits"
    );
    assert_eq!(local.persisted, http.persisted);
    assert_eq!(local.events, mcp.events);
    assert_eq!(
        serde_json::to_value(&local.history).unwrap(),
        serde_json::to_value(&mcp.history).unwrap(),
        "local and MCP public history projections must remain identical"
    );
    assert_eq!(local.persisted, mcp.persisted);

    local_engine.shutdown().await;
    http_engine.shutdown().await;
    mcp_engine.shutdown().await;
    std::fs::remove_dir_all(cwd).ok();
}

#[tokio::test]
async fn simultaneous_http_inputs_have_one_controller_and_one_named_lease_error() {
    let cwd = std::env::temp_dir().join(format!("supercode-sdk-input-race-{}", std::process::id()));
    std::fs::create_dir_all(&cwd).unwrap();
    let provider = BlockingProvider::new();
    let persisted = Arc::new(Mutex::new(String::new()));
    let engine = scripted_engine(provider.clone(), persisted, &cwd);
    let token: Arc<str> = "sdk-input-race-token".into();
    let address = supercode_harness::server::run_http(engine.clone(), "127.0.0.1:0", token.clone())
        .await
        .unwrap();
    let first = HttpFrontendRuntime::connect(format!("http://{address}"), token.to_string())
        .await
        .unwrap();
    let second = HttpFrontendRuntime::connect(format!("http://{address}"), token.to_string())
        .await
        .unwrap();
    let mut attachment = first.attach(50).await.unwrap();
    let barrier = Arc::new(tokio::sync::Barrier::new(3));

    let first_input = {
        let barrier = barrier.clone();
        tokio::spawn(async move {
            barrier.wait().await;
            SdkRuntime::send_input(first, "simultaneous first".into()).await
        })
    };
    let second_input = {
        let barrier = barrier.clone();
        tokio::spawn(async move {
            barrier.wait().await;
            SdkRuntime::send_input(second, "simultaneous second".into()).await
        })
    };
    barrier.wait().await;
    let results = [first_input.await.unwrap(), second_input.await.unwrap()];
    assert_eq!(results.iter().filter(|result| result.is_ok()).count(), 1);
    assert_eq!(
        results
            .iter()
            .filter(|result| matches!(result, Err(FrontendRuntimeError::ControllerRequired { .. })))
            .count(),
        1
    );

    provider.entered.notified().await;
    provider.release.notify_waiters();
    loop {
        let event = attachment.next_event().await.unwrap();
        if event.kind == "turn_succeeded" {
            break;
        }
    }
    engine.shutdown().await;
    std::fs::remove_dir_all(cwd).ok();
}