relay-knowledge 1.1.17

Graph-database-based knowledge graph project.
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
use std::time::{SystemTime, UNIX_EPOCH};

use crate::{
    domain::{
        CodeIndexMode, CodeIndexPublicationFence, CodeIndexResourceBudget, CodeIndexSession,
        CodeIndexSnapshot, CodeRepositoryRegistration, code_snapshot_scope_id,
    },
    storage::{
        CodeIndexFinalizationStep, CodeIndexPublicationStore as _, CodeIndexTaskClaimRequest,
        CodeIndexTaskSeed, CodeIndexTaskStore as _, PartitionedSqliteKnowledgeStore,
        RepositoryCatalogStore as _, SoftwareProjectionStore as _,
    },
};

use super::super::test_support::{partitioned_store, partitioned_store_with_paths};
use super::clear_workspace;

const REPOSITORY_ID: &str = "repo-partitioned-rebind";
const ALIAS: &str = "partitioned-rebind";
const BASE_COMMIT: &str = "base-commit";

#[tokio::test]
async fn clearing_an_unpublished_workspace_requires_a_partitioned_fence() {
    let store = partitioned_store("clear-workspace");

    clear_workspace(
        &store,
        "repo-missing".to_owned(),
        "scope-missing".to_owned(),
    )
    .await
    .expect_err("partitioned workspace clearing must require a task fence");
}

#[tokio::test]
async fn staged_route_survives_reopen_and_resumes_the_same_fenced_checkpoint() {
    let (store, control_path, paths) = partitioned_store_with_paths("checkpoint-route-reopen");
    store
        .upsert_code_repository(
            CodeRepositoryRegistration::new(
                REPOSITORY_ID,
                ALIAS,
                "/tmp/partitioned-checkpoint-route",
                Vec::new(),
                Vec::new(),
            )
            .expect("registration should validate"),
        )
        .await
        .expect("repository should register");
    let source_scope = code_snapshot_scope_id(REPOSITORY_ID, "resume-tree", &[], &[]);
    store
        .queue_code_index_task(CodeIndexTaskSeed {
            repository_id: REPOSITORY_ID.to_owned(),
            alias: ALIAS.to_owned(),
            ref_selector: "HEAD".to_owned(),
            resolved_commit_sha: "resume-commit".to_owned(),
            tree_hash: "resume-tree".to_owned(),
            source_scope: source_scope.clone(),
            path_filters: Vec::new(),
            language_filters: Vec::new(),
            mode: CodeIndexMode::Full,
            input_fingerprint: format!("checkpoint-route-{source_scope}"),
            resource_budget: CodeIndexResourceBudget::default(),
            payload_json: "{}".to_owned(),
            now_ms: now_millis(),
        })
        .await
        .expect("full task should queue");
    let running = claim_task(&store, "resume-worker", now_millis(), 60_000).await;
    let publication_fence = fence(&running, "resume-worker");
    let session = CodeIndexSession {
        repository_id: REPOSITORY_ID.to_owned(),
        source_scope: source_scope.clone(),
        base_resolved_commit_sha: None,
        resolved_commit_sha: "resume-commit".to_owned(),
        tree_hash: "resume-tree".to_owned(),
        path_filters: Vec::new(),
        language_filters: Vec::new(),
        full_replace: true,
        total_path_count: 0,
        changed_path_count: 0,
        skipped_unchanged_count: 0,
        deleted_paths: Vec::new(),
        changed_paths: Vec::new(),
        tombstones: Vec::new(),
        workspaces: Vec::new(),
        resource_budget: CodeIndexResourceBudget::default(),
    };
    let checkpoint = store
        .begin_code_index_session_with_fence(session.clone(), publication_fence.clone())
        .await
        .expect("checkpoint and its pre-staged route should commit");

    drop(store);
    let reopened = PartitionedSqliteKnowledgeStore::open(control_path.clone(), paths.clone())
        .expect("partitioned store should reopen after the simulated crash");
    let recovered = reopened
        .code_index_checkpoint(source_scope)
        .await
        .expect("pre-staged route should locate the shard checkpoint")
        .expect("checkpoint should survive reopen");
    assert_eq!(recovered, checkpoint);

    let resumed = reopened
        .begin_code_index_session_at_checkpoint_with_fence(
            session,
            Some(recovered.clone()),
            publication_fence,
        )
        .await
        .expect("the same task and fence should resume its exact checkpoint");
    assert_eq!(resumed, recovered);
}

#[tokio::test]
async fn prepared_worktree_rebind_survives_reopen_and_publishes_direct_snapshot() {
    let (store, control_path, paths) = partitioned_store_with_paths("worktree-rebind-reopen");
    let (pending_scope, real_scope, snapshot) = worktree_fixture(&store).await;
    let running = claim_task(&store, "worker-reopen", now_millis(), 60_000).await;
    assert_eq!(running.source_scope, pending_scope);
    let publication_fence = fence(&running, "worker-reopen");

    store
        .catalog
        .prepare_snapshot_target(&snapshot, publication_fence.clone())
        .await
        .expect("control WAL should durably prepare the real target");
    let prepared = store
        .active_code_index_task(REPOSITORY_ID.to_owned())
        .await
        .expect("prepared task should load")
        .expect("prepared task should remain active");
    assert_eq!(prepared.source_scope, real_scope);

    drop(store);
    let reopened = PartitionedSqliteKnowledgeStore::open(control_path.clone(), paths.clone())
        .expect("partitioned store should reopen after the simulated crash");
    let recovered = reopened
        .active_code_index_task(REPOSITORY_ID.to_owned())
        .await
        .expect("reopened task should load")
        .expect("durable handoff should remain active");
    assert_eq!(recovered.source_scope, real_scope);

    let summary = reopened
        .apply_code_index_snapshot_with_fence(snapshot, publication_fence)
        .await
        .expect("a bounded worktree overlay should publish without a clean-full fallback");
    assert_eq!(summary.source_scope, real_scope);
    assert!(
        reopened
            .catalog
            .staged_scope_owned_by_task(
                REPOSITORY_ID.to_owned(),
                real_scope.clone(),
                running.task_id.clone(),
            )
            .await
            .expect("staged route should remain inspectable")
    );
    assert!(
        reopened
            .code_index_checkpoint(real_scope.clone())
            .await
            .expect("routed target checkpoint should query")
            .is_none(),
        "the direct worktree protocol must not fabricate a durable clone checkpoint"
    );

    drop(reopened);
    let reopened = PartitionedSqliteKnowledgeStore::open(control_path, paths)
        .expect("partitioned store should reopen after direct shard publication");
    assert!(
        reopened
            .catalog
            .staged_scope_owned_by_task(REPOSITORY_ID.to_owned(), real_scope, running.task_id,)
            .await
            .expect("staged direct route should survive response loss and reopen")
    );
}

#[tokio::test]
async fn stale_generation_cannot_prepare_partitioned_worktree_rebind() {
    let store = partitioned_store("worktree-rebind-stale-generation");
    let (pending_scope, real_scope, snapshot) = worktree_fixture(&store).await;
    let old = claim_task(&store, "worker-old", now_millis(), 60_000).await;
    expire_task_lease(&store, &old.task_id).await;
    let current = store
        .claim_code_index_task(CodeIndexTaskClaimRequest {
            task_id: Some(old.task_id.clone()),
            lease_owner: "worker-current".to_owned(),
            lease_duration_ms: 60_000,
            max_attempts: 3,
            now_ms: now_millis(),
        })
        .await
        .expect("expired attempt takeover should run")
        .expect("expired attempt should be reclaimed");
    assert!(current.publication_generation > old.publication_generation);

    let error = store
        .catalog
        .prepare_snapshot_target(&snapshot, fence(&old, "worker-old"))
        .await
        .expect_err("stale generation must not prepare the control handoff");
    assert!(
        matches!(error, crate::storage::StorageError::InvalidInput(message) if message.contains("no longer active"))
    );
    let active = store
        .active_code_index_task(REPOSITORY_ID.to_owned())
        .await
        .expect("current task should load")
        .expect("current task should remain active");
    assert_eq!(active.source_scope, pending_scope);

    store
        .catalog
        .prepare_snapshot_target(&snapshot, fence(&current, "worker-current"))
        .await
        .expect("current generation should prepare the handoff");
    let prepared = store
        .active_code_index_task(REPOSITORY_ID.to_owned())
        .await
        .expect("prepared task should load")
        .expect("prepared task should remain active");
    assert_eq!(prepared.source_scope, real_scope);
}

#[tokio::test]
async fn fenced_finalization_advances_one_durable_staged_shard_checkpoint_per_call() {
    let store = partitioned_store("single-step-finalization");
    store
        .upsert_code_repository(
            CodeRepositoryRegistration::new(
                REPOSITORY_ID,
                ALIAS,
                "/tmp/partitioned-single-step",
                Vec::new(),
                Vec::new(),
            )
            .expect("registration should validate"),
        )
        .await
        .expect("repository should register");
    let tree_hash = "single-step-tree";
    let source_scope = code_snapshot_scope_id(REPOSITORY_ID, tree_hash, &[], &[]);
    store
        .queue_code_index_task(CodeIndexTaskSeed {
            repository_id: REPOSITORY_ID.to_owned(),
            alias: ALIAS.to_owned(),
            ref_selector: "HEAD".to_owned(),
            resolved_commit_sha: "single-step-commit".to_owned(),
            tree_hash: tree_hash.to_owned(),
            source_scope: source_scope.clone(),
            path_filters: Vec::new(),
            language_filters: Vec::new(),
            mode: CodeIndexMode::Full,
            input_fingerprint: format!("single-step-{source_scope}"),
            resource_budget: CodeIndexResourceBudget::default(),
            payload_json: "{}".to_owned(),
            now_ms: now_millis(),
        })
        .await
        .expect("single-step task should queue");
    let running = claim_task(&store, "worker-single-step", now_millis(), 60_000).await;
    let publication_fence = fence(&running, "worker-single-step");
    let session = CodeIndexSession {
        repository_id: REPOSITORY_ID.to_owned(),
        source_scope: source_scope.clone(),
        base_resolved_commit_sha: None,
        resolved_commit_sha: "single-step-commit".to_owned(),
        tree_hash: tree_hash.to_owned(),
        path_filters: Vec::new(),
        language_filters: Vec::new(),
        full_replace: true,
        total_path_count: 0,
        changed_path_count: 0,
        skipped_unchanged_count: 0,
        deleted_paths: Vec::new(),
        changed_paths: Vec::new(),
        tombstones: Vec::new(),
        workspaces: Vec::new(),
        resource_budget: CodeIndexResourceBudget::default(),
    };
    store
        .begin_code_index_session_with_fence(session.clone(), publication_fence.clone())
        .await
        .expect("single-step session should begin in the staged shard");
    let shard = store
        .catalog
        .checkpoint_repository_store(REPOSITORY_ID.to_owned())
        .await
        .expect("staged shard route should load")
        .expect("staged shard should exist");

    let first = store
        .advance_code_index_session_with_fence(session.clone(), publication_fence.clone())
        .await
        .expect("first finalization quantum should advance");
    let CodeIndexFinalizationStep::Pending {
        checkpoint_state: first_state,
    } = first
    else {
        panic!("first finalization quantum must remain pending");
    };
    assert_eq!(first_state, "finalizing:build_query_indexes:v3:0");
    assert_eq!(
        shard
            .code_index_checkpoint(source_scope.clone())
            .await
            .expect("first durable checkpoint should load")
            .expect("first durable checkpoint should exist")
            .state,
        first_state
    );

    let second = store
        .advance_code_index_session_with_fence(session, publication_fence)
        .await
        .expect("second finalization quantum should advance");
    let CodeIndexFinalizationStep::Pending {
        checkpoint_state: second_state,
    } = second
    else {
        panic!("second finalization quantum must remain pending");
    };
    assert_eq!(second_state, "finalizing:build_query_indexes:v3:2");
    assert_eq!(
        shard
            .code_index_checkpoint(source_scope)
            .await
            .expect("second durable checkpoint should load")
            .expect("second durable checkpoint should exist")
            .state,
        second_state
    );
}

#[tokio::test]
async fn fenced_full_scope_routes_to_its_staged_shard_before_catalog_activation() {
    let store = partitioned_store("fenced-full-software-barrier");
    store
        .upsert_code_repository(
            CodeRepositoryRegistration::new(
                REPOSITORY_ID,
                ALIAS,
                "/tmp/partitioned-fenced-full",
                Vec::new(),
                Vec::new(),
            )
            .expect("registration should validate"),
        )
        .await
        .expect("repository should register");
    let tree_hash = "fenced-full-tree";
    let source_scope = code_snapshot_scope_id(REPOSITORY_ID, tree_hash, &[], &[]);
    store
        .queue_code_index_task(CodeIndexTaskSeed {
            repository_id: REPOSITORY_ID.to_owned(),
            alias: ALIAS.to_owned(),
            ref_selector: "HEAD".to_owned(),
            resolved_commit_sha: "fenced-full-commit".to_owned(),
            tree_hash: tree_hash.to_owned(),
            source_scope: source_scope.clone(),
            path_filters: Vec::new(),
            language_filters: Vec::new(),
            mode: CodeIndexMode::Full,
            input_fingerprint: format!("fenced-full-{source_scope}"),
            resource_budget: CodeIndexResourceBudget::default(),
            payload_json: "{}".to_owned(),
            now_ms: now_millis(),
        })
        .await
        .expect("full task should queue");
    let running = claim_task(&store, "worker-full", now_millis(), 60_000).await;
    let publication_fence = fence(&running, "worker-full");

    store
        .apply_code_index_snapshot_with_fence(
            empty_snapshot(
                source_scope.clone(),
                None,
                "fenced-full-commit",
                tree_hash,
                true,
            ),
            publication_fence.clone(),
        )
        .await
        .expect("full code scope should stage in its repository shard");
    assert_eq!(
        store
            .catalog
            .repository_for_scope(source_scope.clone())
            .await
            .expect("staged catalog route should query")
            .as_deref(),
        Some(REPOSITORY_ID),
        "checkpoint and retry routing must retain the staged shard owner"
    );
    assert!(
        store
            .catalog
            .active_repository_for_scope(source_scope.clone())
            .await
            .expect("active catalog route should query")
            .is_none(),
        "the new scope must remain hidden from ordinary reads before projection"
    );

    crate::storage::stage_empty_business_projection_with_fence_for_test(
        &store,
        running.repository_id.clone(),
        source_scope.clone(),
        running.resolved_commit_sha.clone(),
        publication_fence.clone(),
    )
    .await
    .expect("business projection should route by fenced repository ownership");
    let projection = store
        .refresh_software_global_projection_with_fence(source_scope.clone(), publication_fence)
        .await
        .expect("software projection should route by fenced repository ownership");

    assert!(!projection.status.stale);
    assert_eq!(
        store
            .catalog
            .repository_for_scope(source_scope.clone())
            .await
            .expect("published catalog route should query")
            .as_deref(),
        Some(REPOSITORY_ID)
    );
    assert_eq!(
        store
            .catalog
            .active_repository_for_scope(source_scope.clone())
            .await
            .expect("published route should query")
            .as_deref(),
        Some(REPOSITORY_ID)
    );
    let status = store
        .code_repository_status(REPOSITORY_ID.to_owned())
        .await
        .expect("repository status should query")
        .expect("repository status should exist");
    assert!(!status.stale);
    assert_eq!(
        status.last_indexed_scope_id.as_deref(),
        Some(source_scope.as_str())
    );
}

async fn worktree_fixture(
    store: &PartitionedSqliteKnowledgeStore,
) -> (String, String, CodeIndexSnapshot) {
    store
        .upsert_code_repository(
            CodeRepositoryRegistration::new(
                REPOSITORY_ID,
                ALIAS,
                "/tmp/partitioned-rebind",
                Vec::new(),
                Vec::new(),
            )
            .expect("registration should validate"),
        )
        .await
        .expect("repository should register");
    let base_scope = code_snapshot_scope_id(REPOSITORY_ID, "base-tree", &[], &[]);
    super::seed_snapshot_for_test(
        store,
        empty_snapshot(base_scope, None, BASE_COMMIT, "base-tree", true),
    )
    .await
    .expect("base scope should publish");

    let pending_tree = format!("worktree:pending:{BASE_COMMIT}");
    let pending_scope = code_snapshot_scope_id(REPOSITORY_ID, &pending_tree, &[], &[]);
    store
        .queue_code_index_task(CodeIndexTaskSeed {
            repository_id: REPOSITORY_ID.to_owned(),
            alias: ALIAS.to_owned(),
            ref_selector: BASE_COMMIT.to_owned(),
            resolved_commit_sha: pending_tree.clone(),
            tree_hash: pending_tree,
            source_scope: pending_scope.clone(),
            path_filters: Vec::new(),
            language_filters: Vec::new(),
            mode: CodeIndexMode::WorktreeOverlay,
            input_fingerprint: format!("partitioned-rebind-{pending_scope}"),
            resource_budget: CodeIndexResourceBudget::default(),
            payload_json: "{}".to_owned(),
            now_ms: now_millis(),
        })
        .await
        .expect("worktree task should queue");

    let overlay_hash = "0123456789abcdef";
    let real_tree = format!("worktree:{overlay_hash}");
    let real_scope = code_snapshot_scope_id(REPOSITORY_ID, &real_tree, &[], &[]);
    let snapshot = empty_snapshot(
        real_scope.clone(),
        Some(BASE_COMMIT.to_owned()),
        &format!("worktree:{BASE_COMMIT}:{overlay_hash}"),
        &real_tree,
        false,
    );
    (pending_scope, real_scope, snapshot)
}

fn empty_snapshot(
    source_scope: String,
    base_resolved_commit_sha: Option<String>,
    resolved_commit_sha: &str,
    tree_hash: &str,
    full_replace: bool,
) -> CodeIndexSnapshot {
    CodeIndexSnapshot {
        repository_id: REPOSITORY_ID.to_owned(),
        source_scope,
        base_resolved_commit_sha,
        resolved_commit_sha: resolved_commit_sha.to_owned(),
        tree_hash: tree_hash.to_owned(),
        path_filters: Vec::new(),
        language_filters: Vec::new(),
        full_replace,
        changed_path_count: 0,
        skipped_unchanged_count: 0,
        deleted_paths: Vec::new(),
        tombstones: Vec::new(),
        files: Vec::new(),
        symbols: Vec::new(),
        references: Vec::new(),
        imports: Vec::new(),
        calls: Vec::new(),
        dependencies: Vec::new(),
        feature_flags: Vec::new(),
        framework_nodes: Vec::new(),
        framework_edges: Vec::new(),
        routes: Vec::new(),
        chunks: Vec::new(),
        workspaces: Vec::new(),
        diagnostics: Vec::new(),
    }
}

async fn claim_task(
    store: &PartitionedSqliteKnowledgeStore,
    lease_owner: &str,
    now_ms: u64,
    lease_duration_ms: u64,
) -> crate::domain::CodeIndexTaskRecord {
    store
        .claim_code_index_task(CodeIndexTaskClaimRequest {
            task_id: None,
            lease_owner: lease_owner.to_owned(),
            lease_duration_ms,
            max_attempts: 3,
            now_ms,
        })
        .await
        .expect("task claim should run")
        .expect("worktree task should claim")
}

async fn expire_task_lease(store: &PartitionedSqliteKnowledgeStore, task_id: &str) {
    let task_id = task_id.to_owned();
    let changed = store
        .control
        .run(move |connection| {
            Ok(connection.execute(
                "UPDATE code_repository_index_tasks
                 SET lease_expires_at_ms = 0
                 WHERE task_id = ?1 AND state = 'running'",
                [task_id],
            )?)
        })
        .await
        .expect("task lease fixture should expire");
    assert_eq!(changed, 1, "one running task lease should expire");
}

fn fence(
    task: &crate::domain::CodeIndexTaskRecord,
    lease_owner: &str,
) -> CodeIndexPublicationFence {
    CodeIndexPublicationFence {
        repository_id: task.repository_id.clone(),
        task_id: task.task_id.clone(),
        lease_owner: lease_owner.to_owned(),
        attempt_count: task.attempt_count,
        generation: task.publication_generation,
    }
}

fn now_millis() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("clock should follow epoch")
        .as_millis()
        .try_into()
        .unwrap_or(u64::MAX)
}