tandem-server 0.7.2

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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright (c) 2026 Frumu LTD
// Licensed under the Business Source License 1.1

use std::path::Path;

use anyhow::Context;

use super::*;

pub(super) fn encrypt_legacy_local_line(
    crypto: &ProtectedFileCrypto,
    plaintext: &str,
    context: &ProtectedRecordContext,
) -> anyhow::Result<String> {
    anyhow::ensure!(
        !crypto.provider.is_plaintext() && !crypto.provider.is_hosted(),
        "legacy local line encryption requires a local-key provider"
    );
    Ok(crypto
        .provider
        .encrypt_field_scoped(
            plaintext,
            &context.key_scope,
            &context.policy_decision_id,
            &context.audit_id,
        )?
        .0)
}

pub(crate) async fn migrate_jsonl_records_file(
    path: &Path,
    expected_legacy_lines: &[String],
    migrated_records: &[(String, ProtectedRecordContext)],
    store: &ProtectedStoreContext,
    additional_anchor: Option<crate::audit_integrity::ExternalAnchorUpdate<'_>>,
) -> anyhow::Result<()> {
    let lock = path_lock_for(path).await;
    let _guard = lock.lock().await;
    fs::create_dir_all(path.parent().unwrap_or_else(|| Path::new("."))).await?;
    let crypto = crypto();
    let _process_guard = ProcessWriteLock::acquire(path).await?;
    let prior = decrypt_jsonl_state(&crypto, path, store).await?;
    anyhow::ensure!(
        !prior.authenticated
            && prior.generation == 0
            && prior.lines.as_slice() == expected_legacy_lines,
        "protected JSONL legacy rows changed before migration"
    );
    anyhow::ensure!(
        !migrated_records.is_empty(),
        "protected JSONL migration requires at least one output row"
    );

    if crypto.provider.is_plaintext() {
        return rewrite_plaintext_legacy_jsonl(path, migrated_records, store, additional_anchor)
            .await;
    }

    let authority = crate::audit_integrity::integrity_authority()?;
    commit_legacy_jsonl_migration(
        &crypto,
        path,
        migrated_records,
        store,
        authority.as_ref(),
        additional_anchor,
    )
    .await
}

pub(super) async fn migrate_legacy_jsonl_and_append(
    crypto: &ProtectedFileCrypto,
    path: &Path,
    legacy_lines: &[String],
    plaintext: &str,
    context: &ProtectedRecordContext,
    store: &ProtectedStoreContext,
    authority: &crate::audit_integrity::AuditIntegrityKeyring,
    additional_anchor: Option<crate::audit_integrity::ExternalAnchorUpdate<'_>>,
) -> anyhow::Result<()> {
    anyhow::ensure!(
        !legacy_lines.is_empty(),
        "legacy protected JSONL migration requires existing rows"
    );
    let mut records = legacy_lines
        .iter()
        .map(|line| (line.clone(), store.manifest.clone()))
        .collect::<Vec<_>>();
    records.push((plaintext.to_string(), context.clone()));
    commit_legacy_jsonl_migration(
        crypto,
        path,
        &records,
        store,
        Some(authority),
        additional_anchor,
    )
    .await
}

async fn rewrite_plaintext_legacy_jsonl(
    path: &Path,
    migrated_records: &[(String, ProtectedRecordContext)],
    store: &ProtectedStoreContext,
    additional_anchor: Option<crate::audit_integrity::ExternalAnchorUpdate<'_>>,
) -> anyhow::Result<()> {
    ensure_legacy_store_not_anchored(path, store).await?;
    ensure_integrity_sidecars_absent(path, "plaintext protected JSONL migration").await?;
    let previous_data = read_optional_file(path)
        .await?
        .context("legacy protected JSONL data disappeared during migration")?;
    let mut stored = String::new();
    for (record, _) in migrated_records {
        stored.push_str(record);
        stored.push('\n');
    }
    let additional_anchor_snapshot = snapshot_additional_anchor(additional_anchor, path).await?;

    let write_result = async {
        atomic_replace(path, stored.as_bytes())
            .await
            .context("write migrated plaintext protected JSONL data")?;
        write_additional_anchor(additional_anchor, additional_anchor_snapshot.as_ref(), path).await
    }
    .await;
    if let Err(error) = write_result {
        let anchor_rollback = rollback_additional_anchor(
            additional_anchor,
            additional_anchor_snapshot.as_ref(),
            path,
        )
        .await;
        let data_rollback = restore_file(path, Some(&previous_data))
            .await
            .context("restore plaintext protected JSONL after failed migration");
        return finish_failed_transaction(error, [anchor_rollback, data_rollback]);
    }
    Ok(())
}

async fn commit_legacy_jsonl_migration(
    crypto: &ProtectedFileCrypto,
    path: &Path,
    migrated_records: &[(String, ProtectedRecordContext)],
    store: &ProtectedStoreContext,
    authority: Option<&crate::audit_integrity::AuditIntegrityKeyring>,
    additional_anchor: Option<crate::audit_integrity::ExternalAnchorUpdate<'_>>,
) -> anyhow::Result<()> {
    let previous_data = read_optional_file(path)
        .await?
        .context("legacy protected JSONL data disappeared during migration")?;
    let head_path = integrity_head_path(path);
    let state_path = initialized_state_path(path);
    let previous_head = read_optional_file(&head_path).await?;
    let previous_state = read_optional_file(&state_path).await?;
    anyhow::ensure!(
        previous_head.is_none() && previous_state.is_none(),
        "legacy protected JSONL rows conflict with integrity state"
    );

    let mut stored = String::new();
    let mut previous_digest = None;
    for (index, (record, record_context)) in migrated_records.iter().enumerate() {
        let mut frame = AuthenticatedJsonlFrame {
            version: AUTHENTICATED_STORE_VERSION,
            store_id: store.store_id.clone(),
            sequence: index as u64 + 1,
            previous_digest,
            context: record_context.clone(),
            stored_record: crypto.encrypt_record(record, record_context)?,
            integrity_key_id: authority.map(|keys| keys.active_key_id().to_string()),
            digest: String::new(),
        };
        frame.digest = sign_jsonl_frame_digest(&frame, authority)?;
        previous_digest = Some(frame.digest.clone());
        let outer = crypto.encrypt_record(&serde_json::to_string(&frame)?, &store.manifest)?;
        stored.push_str(AUTHENTICATED_JSONL_PREFIX);
        stored.push_str(&outer);
        stored.push('\n');
    }

    let head = AuthenticatedStoreHead {
        version: AUTHENTICATED_STORE_VERSION,
        store_id: store.store_id.clone(),
        generation: migrated_records.len() as u64,
        digest: previous_digest.context("migrated protected JSONL head is missing")?,
        integrity_key_id: authority.map(|keys| keys.active_key_id().to_string()),
    };
    let previous_cached_head = cached_head(path).await;
    let encoded_head = encode_authenticated_head(crypto, &head, store)?;
    let encoded_state = encode_authenticated_state(crypto, &authenticated_state(&head), store)?;
    let store_anchor_snapshot = snapshot_protected_store_anchor(path, store, &head, None).await?;
    let additional_anchor_snapshot = snapshot_additional_anchor(additional_anchor, path).await?;
    validate_cached_head(path, &head).await?;

    let write_result = async {
        atomic_replace(&state_path, encoded_state.as_bytes())
            .await
            .context("write migrated protected JSONL initialized state")?;
        atomic_replace(&head_path, encoded_head.as_bytes())
            .await
            .context("write migrated protected JSONL integrity head")?;
        atomic_replace(path, stored.as_bytes())
            .await
            .context("write migrated protected JSONL data")?;
        write_protected_store_anchor(path, store, &head, store_anchor_snapshot.as_ref()).await?;
        write_additional_anchor(additional_anchor, additional_anchor_snapshot.as_ref(), path).await
    }
    .await;
    if let Err(error) = write_result {
        let additional_rollback = rollback_additional_anchor(
            additional_anchor,
            additional_anchor_snapshot.as_ref(),
            path,
        )
        .await;
        let store_anchor_rollback = rollback_protected_store_anchor(
            path,
            store,
            &head,
            None,
            store_anchor_snapshot.as_ref(),
        )
        .await;
        let file_rollback = restore_failed_collection_write(
            path,
            Some(&previous_data),
            &head_path,
            previous_head.as_deref(),
            &state_path,
            previous_state.as_deref(),
            &head,
            previous_cached_head.as_ref(),
        )
        .await;
        return finish_failed_transaction(
            error,
            [additional_rollback, store_anchor_rollback, file_rollback],
        );
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use tandem_memory::MemoryCryptoProvider;

    use super::*;
    use crate::encrypted_file_store::with_test_crypto_provider;

    fn migration_fixture(
        name: &str,
    ) -> (
        tempfile::TempDir,
        std::path::PathBuf,
        std::path::PathBuf,
        ProtectedStoreContext,
        ProtectedRecordContext,
        ProtectedRecordContext,
    ) {
        let root = tempfile::tempdir().expect("temporary root");
        let path = root.path().join("state").join(format!("{name}.jsonl"));
        let anchors = root.path().join("anchors");
        let store = super::super::tests::store_context(name);
        let first = super::super::tests::record_context("first");
        let second = super::super::tests::record_context("second");
        (root, path, anchors, store, first, second)
    }

    #[tokio::test]
    async fn configured_authority_migrates_legacy_rows_before_append() {
        let (_root, path, anchors, store, first_context, second_context) =
            migration_fixture("legacy-migration");
        let provider = MemoryCryptoProvider::local_key([0x63; 32]);

        with_test_crypto_provider(provider, None, async {
            fs::create_dir_all(path.parent().expect("state parent"))
                .await
                .expect("state directory");
            let legacy =
                encrypt_legacy_local_line(&crypto(), "first", &first_context).expect("legacy row");
            fs::write(&path, format!("{legacy}\n"))
                .await
                .expect("legacy store");
            let authority = crate::audit_integrity::test_keyring(
                "active",
                "migration-audit-integrity-secret-32-bytes",
                &[],
            );

            crate::audit_integrity::with_test_keyring(
                Some(authority),
                crate::audit_integrity::with_test_anchor_dir(anchors, async {
                    append_jsonl_record_file(&path, "second", &second_context, &store, true)
                        .await
                        .expect("migrate and append");

                    let encoded = fs::read_to_string(&path).await.expect("migrated store");
                    assert_eq!(encoded.lines().count(), 2);
                    assert!(encoded
                        .lines()
                        .all(|line| line.starts_with(AUTHENTICATED_JSONL_PREFIX)));
                    assert!(integrity_head_path(&path).exists());
                    assert!(initialized_state_path(&path).exists());
                    assert_eq!(
                        read_jsonl_records_file(&path, &store)
                            .await
                            .expect("read migrated store"),
                        vec!["first".to_string(), "second".to_string()]
                    );

                    let second = encoded.lines().nth(1).expect("second frame");
                    fs::write(&path, format!("{second}\n"))
                        .await
                        .expect("delete migrated legacy row");
                    forget_cached_head_for_test(&path).await;
                    assert!(read_jsonl_records_file(&path, &store).await.is_err());
                }),
            )
            .await;
        })
        .await;
    }

    #[tokio::test]
    async fn anchor_publication_failure_restores_legacy_store_and_anchor_absence() {
        let (_root, path, anchors, store, first_context, second_context) =
            migration_fixture("anchor-rollback");
        with_test_crypto_provider(MemoryCryptoProvider::local_key([0x64; 32]), None, async {
            fs::create_dir_all(path.parent().expect("state parent"))
                .await
                .expect("state directory");
            let legacy =
                encrypt_legacy_local_line(&crypto(), "first", &first_context).expect("legacy row");
            let original = format!("{legacy}\n");
            fs::write(&path, &original).await.expect("legacy store");
            let authority = crate::audit_integrity::test_keyring(
                "active",
                "rollback-audit-integrity-secret-32-bytes",
                &[],
            );

            crate::audit_integrity::with_test_keyring(
                Some(authority),
                crate::audit_integrity::with_test_anchor_dir(anchors, async {
                    let error = crate::audit_integrity::with_test_anchor_write_failure(
                        append_jsonl_record_file(&path, "second", &second_context, &store, true),
                    )
                    .await
                    .expect_err("injected anchor failure must fail migration");
                    assert!(format!("{error:#}").contains("injected external anchor failure"));
                    assert_eq!(fs::read_to_string(&path).await.unwrap(), original);
                    assert!(!integrity_head_path(&path).exists());
                    assert!(!initialized_state_path(&path).exists());
                    assert!(cached_head(&path).await.is_none());
                    assert_eq!(
                        read_jsonl_records_file(&path, &store).await.unwrap(),
                        vec!["first".to_string()]
                    );
                    let identity =
                        protected_store_anchor_identity(&path, &store).expect("anchor identity");
                    crate::audit_integrity::ensure_external_anchor_absent(
                        "protected-store",
                        &identity,
                        &path,
                    )
                    .await
                    .expect("failed migration must not retain its anchor");
                }),
            )
            .await;
        })
        .await;
    }

    #[tokio::test]
    async fn companion_anchor_failure_restores_sequenced_legacy_migration() {
        let (_root, path, anchors, store, first_context, second_context) =
            migration_fixture("companion-anchor-rollback");
        with_test_crypto_provider(MemoryCryptoProvider::local_key([0x66; 32]), None, async {
            fs::create_dir_all(path.parent().expect("state parent"))
                .await
                .expect("state directory");
            let legacy = encrypt_legacy_local_line(&crypto(), r#"{"seq":1}"#, &first_context)
                .expect("legacy sequenced row");
            let original = format!("{legacy}\n");
            fs::write(&path, &original).await.expect("legacy store");
            let authority = crate::audit_integrity::test_keyring(
                "active",
                "companion-anchor-rollback-secret-32-bytes",
                &[],
            );

            crate::audit_integrity::with_test_keyring(
                Some(authority),
                crate::audit_integrity::with_test_anchor_dir(anchors, async {
                    let logical_identity = format!(
                        "protected-audit:{}",
                        crate::audit_integrity::canonical_state_file_identity(&path)
                            .expect("canonical logical identity")
                    );
                    let update = crate::audit_integrity::ExternalAnchorUpdate {
                        scope: "protected-audit-ledger",
                        identity: &logical_identity,
                        generation: 2,
                        digest: "logical-sequence-two-digest",
                        previous: None,
                    };
                    crate::audit_integrity::with_test_anchor_write_failure_after(
                        2,
                        append_jsonl_record_file_with_anchor(
                            &path,
                            r#"{"seq":2}"#,
                            &second_context,
                            &store,
                            true,
                            Some(update),
                        ),
                    )
                    .await
                    .expect_err("companion anchor publication must fail");

                    assert_eq!(fs::read_to_string(&path).await.unwrap(), original);
                    assert!(!integrity_head_path(&path).exists());
                    assert!(!initialized_state_path(&path).exists());
                    assert!(cached_head(&path).await.is_none());
                    assert_eq!(
                        read_jsonl_records_file(&path, &store).await.unwrap(),
                        vec![r#"{"seq":1}"#.to_string()]
                    );
                    let physical_identity =
                        protected_store_anchor_identity(&path, &store).expect("physical identity");
                    crate::audit_integrity::ensure_external_anchor_absent(
                        "protected-store",
                        &physical_identity,
                        &path,
                    )
                    .await
                    .expect("physical anchor rolled back");
                    crate::audit_integrity::ensure_external_anchor_absent(
                        update.scope,
                        update.identity,
                        &path,
                    )
                    .await
                    .expect("companion anchor rolled back");
                }),
            )
            .await;
        })
        .await;
    }
}