tandem-server 0.6.8

HTTP server for Tandem engine APIs
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
#[tokio::test]
async fn context_tree_endpoints_are_tenant_scoped() {
    let state = test_state().await;
    let app = app_router(state.clone());

    // Seed a context node + L2 layer for tenant org-a directly through the
    // memory manager (nodes have no HTTP creation surface).
    if let Some(parent) = state.memory_db_path.parent() {
        tokio::fs::create_dir_all(parent).await.expect("memory dir");
    }
    let manager = tandem_memory::MemoryManager::new(&state.memory_db_path)
        .await
        .expect("memory manager");
    let scope_a = tandem_memory::types::MemoryTenantScope {
        org_id: "org-a".to_string(),
        workspace_id: "workspace-a".to_string(),
        deployment_id: None,
    };
    let node_id = manager
        .store_content_with_layers(
            "tandem://resources/proj-a/secret.md",
            "tenant A secret context",
            None,
            &scope_a,
        )
        .await
        .expect("seed node");

    let resolve_request = |org: &str, workspace: &str| {
        Request::builder()
            .method("POST")
            .uri("/memory/context/resolve")
            .header("content-type", "application/json")
            .header("x-tandem-org-id", org)
            .header("x-tandem-workspace-id", workspace)
            .header("x-tandem-actor-id", "actor-a")
            .body(Body::from(
                json!({ "uri": "tandem://resources/proj-a/secret.md" }).to_string(),
            ))
            .expect("request")
    };

    // The owning tenant resolves the node.
    let resp = app
        .clone()
        .oneshot(resolve_request("org-a", "workspace-a"))
        .await
        .expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
    let payload: Value = serde_json::from_slice(&body).expect("json");
    assert_eq!(
        payload.pointer("/node/id").and_then(Value::as_str),
        Some(node_id.as_str())
    );

    // A different tenant gets the same shape as a nonexistent URI: node null.
    let resp = app
        .clone()
        .oneshot(resolve_request("org-b", "workspace-b"))
        .await
        .expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
    let payload: Value = serde_json::from_slice(&body).expect("json");
    assert!(payload.get("node").is_some_and(Value::is_null));

    // Tree traversal: owner sees the child; foreign tenant sees an empty tree.
    let tree_request = |org: &str, workspace: &str| {
        Request::builder()
            .method("GET")
            .uri("/memory/context/tree?uri=tandem%3A%2F%2Fresources%2Fproj-a&max_depth=3")
            .header("x-tandem-org-id", org)
            .header("x-tandem-workspace-id", workspace)
            .header("x-tandem-actor-id", "actor-a")
            .body(Body::empty())
            .expect("request")
    };
    let resp = app
        .clone()
        .oneshot(tree_request("org-a", "workspace-a"))
        .await
        .expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
    let payload: Value = serde_json::from_slice(&body).expect("json");
    assert_eq!(
        payload
            .pointer("/tree")
            .and_then(Value::as_array)
            .map(Vec::len),
        Some(1)
    );
    let resp = app
        .clone()
        .oneshot(tree_request("org-b", "workspace-b"))
        .await
        .expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
    let body = to_bytes(resp.into_body(), usize::MAX).await.expect("body");
    let payload: Value = serde_json::from_slice(&body).expect("json");
    assert_eq!(
        payload
            .pointer("/tree")
            .and_then(Value::as_array)
            .map(Vec::len),
        Some(0)
    );

    // Layer generation on a foreign node id behaves exactly like a missing
    // node id: 200 no-op, and the owner's layer set is untouched.
    let resp = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/memory/context/layers/generate")
                .header("content-type", "application/json")
                .header("x-tandem-org-id", "org-b")
                .header("x-tandem-workspace-id", "workspace-b")
                .header("x-tandem-actor-id", "actor-a")
                .body(Body::from(json!({ "node_id": node_id }).to_string()))
                .expect("request"),
        )
        .await
        .expect("response");
    assert_eq!(resp.status(), StatusCode::OK);
    let scope_b = tandem_memory::types::MemoryTenantScope {
        org_id: "org-b".to_string(),
        workspace_id: "workspace-b".to_string(),
        deployment_id: None,
    };
    assert!(manager
        .get_context_layer(&node_id, tandem_memory::types::LayerType::L0, &scope_b)
        .await
        .expect("layer lookup")
        .is_none());
    assert!(manager
        .get_context_layer(&node_id, tandem_memory::types::LayerType::L0, &scope_a)
        .await
        .expect("layer lookup")
        .is_none());
    assert!(manager
        .get_context_layer(&node_id, tandem_memory::types::LayerType::L2, &scope_a)
        .await
        .expect("layer lookup")
        .is_some());
}

fn verified_org_unit_context(
    tenant_context: tandem_types::TenantContext,
    actor: &str,
    org_units: Vec<String>,
) -> tandem_types::VerifiedTenantContext {
    let principal = tandem_types::RequestPrincipal::authenticated_user(actor, "tandem-web");
    let authority_chain = tandem_types::AuthorityChain::from_request(principal);
    let strict_projection = tandem_types::StrictTenantContext::new(
        tenant_context.clone(),
        tandem_types::PrincipalRef::human_user(actor),
        authority_chain.clone(),
        tandem_types::ResourceScope::root(tandem_types::ResourceRef::new(
            tenant_context.org_id.clone(),
            tenant_context.workspace_id.clone(),
            tandem_types::ResourceKind::Workspace,
            tenant_context.workspace_id.clone(),
        )),
        tandem_types::AssertionMetadata::new(
            "tandem-web",
            "tandem-runtime",
            1_000,
            9_999_999_999_999,
            format!("org-unit-assertion-{actor}"),
        ),
    );
    tandem_types::VerifiedTenantContext {
        tenant_context,
        human_actor: tandem_types::HumanActor::tandem_user(actor),
        authority_chain,
        roles: Vec::new(),
        org_units,
        capabilities: Vec::new(),
        policy_version: None,
        strict_projection: Some(strict_projection),
        issuer: "tandem-web".to_string(),
        audience: "tandem-runtime".to_string(),
        issued_at_ms: 1_000,
        expires_at_ms: 9_999_999_999_999,
        assertion_id: format!("org-unit-assertion-{actor}"),
        assertion_key_id: None,
    }
}

fn org_unit_put_request(run_id: &str, owner_org_unit_id: &str) -> tandem_memory::MemoryPutRequest {
    tandem_memory::MemoryPutRequest {
        private: false,
        run_id: run_id.to_string(),
        partition: tandem_memory::MemoryPartition {
            org_id: "acme".to_string(),
            workspace_id: "north".to_string(),
            project_id: "proj-a".to_string(),
            tier: tandem_memory::GovernedMemoryTier::Session,
        },
        kind: tandem_memory::MemoryContentKind::Fact,
        content: "engineering department runbook detail".to_string(),
        artifact_refs: Vec::new(),
        classification: tandem_memory::MemoryClassification::Internal,
        authority_job_context: None,
        metadata: Some(json!({ "owner_org_unit_id": owner_org_unit_id })),
    }
}

fn org_unit_capability(run_id: &str, subject: &str) -> tandem_memory::MemoryCapabilityToken {
    tandem_memory::MemoryCapabilityToken {
        run_id: run_id.to_string(),
        subject: subject.to_string(),
        org_id: "acme".to_string(),
        workspace_id: "north".to_string(),
        project_id: "proj-a".to_string(),
        memory: tandem_memory::MemoryCapabilities::default(),
        expires_at: 9_999_999_999_999,
    }
}

#[tokio::test]
async fn memory_put_org_unit_stamp_requires_membership() {
    let state = test_state().await;
    let tenant_context =
        tandem_types::TenantContext::explicit_user_workspace("acme", "north", None, "user-a");

    // A verified writer who is not a member of the stamped unit is rejected.
    let outsider = verified_org_unit_context(
        tenant_context.clone(),
        "user-a",
        vec!["ou-sales".to_string()],
    );
    let denied = super::super::skills_memory::memory_put_impl_with_verified(
        &state,
        &tenant_context,
        Some(&outsider),
        org_unit_put_request("org-unit-put-denied", "ou-eng"),
        Some(org_unit_capability("org-unit-put-denied", "user-a")),
    )
    .await;
    assert_eq!(denied.expect_err("non-member stamp"), StatusCode::FORBIDDEN);

    // A member of the unit stores the record.
    let member = verified_org_unit_context(
        tenant_context.clone(),
        "user-a",
        vec!["ou-eng".to_string(), "ou-sales".to_string()],
    );
    let stored = super::super::skills_memory::memory_put_impl_with_verified(
        &state,
        &tenant_context,
        Some(&member),
        org_unit_put_request("org-unit-put-allowed", "ou-eng"),
        Some(org_unit_capability("org-unit-put-allowed", "user-a")),
    )
    .await
    .expect("member stamp stores");
    assert!(stored.stored);
}

#[tokio::test]
async fn governed_read_filter_threads_org_units_from_verified_context() {
    let tenant_context =
        tandem_types::TenantContext::explicit_user_workspace("acme", "north", None, "user-a");
    let member = verified_org_unit_context(
        tenant_context.clone(),
        "user-a",
        vec!["ou-eng".to_string()],
    );
    let filter = crate::memory::read_policy::governed_memory_read_filter(
        tandem_types::RuntimeAuthMode::EnterpriseRequired,
        Some(&member),
        false,
        2_000,
    )
    .expect("governed filter");
    assert_eq!(
        filter.caller_org_units,
        Some(std::collections::BTreeSet::from(["ou-eng".to_string()]))
    );

    // An org-unit-restricted record is readable by the member and hidden from a
    // same-tenant principal in a different unit.
    let restricted_metadata = json!({ "owner_org_unit_id": "ou-eng" });
    let record = tandem_memory::types::GlobalMemoryRecord {
        id: "record-ou".to_string(),
        user_id: "user-a".to_string(),
        source_type: "fact".to_string(),
        content: "department fact".to_string(),
        content_hash: "hash".to_string(),
        run_id: "run-ou".to_string(),
        session_id: None,
        message_id: None,
        tool_name: None,
        project_tag: Some("proj-a".to_string()),
        channel_tag: None,
        host_tag: None,
        metadata: Some(restricted_metadata),
        provenance: None,
        redaction_status: "passed".to_string(),
        redaction_count: 0,
        visibility: "private".to_string(),
        demoted: false,
        score_boost: 0.0,
        created_at_ms: 1_000,
        updated_at_ms: 1_000,
        expires_at_ms: None,
    };
    assert!(filter.allows_global_record(&record));

    let other_unit = verified_org_unit_context(
        tenant_context.clone(),
        "user-b",
        vec!["ou-sales".to_string()],
    );
    let other_filter = crate::memory::read_policy::governed_memory_read_filter(
        tandem_types::RuntimeAuthMode::EnterpriseRequired,
        Some(&other_unit),
        false,
        2_000,
    )
    .expect("governed filter");
    let decision = other_filter.decision_for_global_record(&record);
    assert!(!decision.allowed);
    assert_eq!(decision.reason.as_deref(), Some("org_unit_scope_mismatch"));
}

#[tokio::test]
async fn memory_put_rejects_non_storage_backed_tiers() {
    let state = test_state().await;
    let app = app_router(state.clone());

    // Team/Curated exist only in the governance contract; writes fail closed
    // until a backing store lands (TAN-607).
    for tier in ["team", "curated"] {
        let req = Request::builder()
            .method("POST")
            .uri("/memory/put")
            .header("content-type", "application/json")
            .body(Body::from(
                json!({
                    "run_id": format!("run-{tier}"),
                    "partition": {
                        "org_id": "org-1",
                        "workspace_id": "ws-1",
                        "project_id": "proj-1",
                        "tier": tier
                    },
                    "kind": "note",
                    "content": "should fail: tier has no backing store",
                    "classification": "internal",
                    "capability": {
                        "run_id": format!("run-{tier}"),
                        "subject": "user-1",
                        "org_id": "org-1",
                        "workspace_id": "ws-1",
                        "project_id": "proj-1",
                        "memory": {
                            "read_tiers": ["session", "project"],
                            "write_tiers": ["session", "project", tier],
                            "promote_targets": [],
                            "require_review_for_promote": false,
                            "allow_auto_use_tiers": []
                        },
                        "expires_at": 9999999999999u64
                    }
                })
                .to_string(),
            ))
            .expect("request");
        let resp = app.clone().oneshot(req).await.expect("response");
        assert_eq!(
            resp.status(),
            StatusCode::FORBIDDEN,
            "tier {tier} must be rejected"
        );
    }
}

#[tokio::test]
async fn memory_promote_rejects_non_storage_backed_tiers() {
    let state = test_state().await;
    let tenant_context =
        tandem_types::TenantContext::explicit_user_workspace("org-1", "ws-1", None, "user-1");

    for tier in [
        tandem_memory::GovernedMemoryTier::Team,
        tandem_memory::GovernedMemoryTier::Curated,
    ] {
        let capability = tandem_memory::MemoryCapabilityToken {
            run_id: "run-promote-tier".to_string(),
            subject: "user-1".to_string(),
            org_id: "org-1".to_string(),
            workspace_id: "ws-1".to_string(),
            project_id: "proj-1".to_string(),
            memory: tandem_memory::MemoryCapabilities {
                promote_targets: vec![tier],
                ..Default::default()
            },
            expires_at: 9_999_999_999_999,
        };
        let denied = super::super::skills_memory::memory_promote_impl(
            &state,
            &tenant_context,
            tandem_memory::MemoryPromoteRequest {
                run_id: "run-promote-tier".to_string(),
                source_memory_id: "missing-source".to_string(),
                from_tier: tandem_memory::GovernedMemoryTier::Session,
                to_tier: tier,
                partition: tandem_memory::MemoryPartition {
                    org_id: "org-1".to_string(),
                    workspace_id: "ws-1".to_string(),
                    project_id: "proj-1".to_string(),
                    tier,
                },
                reason: "promote into unbacked tier".to_string(),
                review: tandem_memory::PromotionReview {
                    required: false,
                    reviewer_id: None,
                    approval_id: None,
                },
                authority_job_context: None,
                source_outcome: None,
            },
            Some(capability),
        )
        .await;
        assert_eq!(
            denied.expect_err("unbacked tier promotion is rejected"),
            StatusCode::FORBIDDEN
        );
    }
}