relay-knowledge 1.1.14

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
use std::{
    fs,
    path::{Path, PathBuf},
    process::Command,
    sync::Arc,
    time::{SystemTime, UNIX_EPOCH},
};

use super::*;
use crate::{
    api::{CodeRepositoryRegisterRequest, InterfaceKind},
    application::RuntimeConfiguration,
    domain::{CodeIndexMode, CodeIndexRequest, CodeRepositorySelector},
    env::{EnvironmentConfig, PlatformKind},
    storage::SqliteGraphStore,
};

#[test]
fn parses_repo_set_commands_and_validation_errors() {
    assert_eq!(
        parse_repo_set(&[
            "create".to_owned(),
            "workspace".to_owned(),
            "--description".to_owned(),
            "all repos".to_owned(),
        ])
        .expect("create should parse"),
        RepoSetCommand::Create {
            alias: "workspace".to_owned(),
            description: Some("all repos".to_owned()),
        }
    );
    assert_eq!(
        parse_repo_set(&[
            "add".to_owned(),
            "workspace".to_owned(),
            "core".to_owned(),
            "--ref".to_owned(),
            "main".to_owned(),
            "--path".to_owned(),
            "src".to_owned(),
            "--language".to_owned(),
            "rust".to_owned(),
            "--priority".to_owned(),
            "4".to_owned(),
        ])
        .expect("add should parse"),
        RepoSetCommand::Add {
            set_alias: "workspace".to_owned(),
            repository_alias: "core".to_owned(),
            ref_selector: "main".to_owned(),
            path_filters: vec!["src".to_owned()],
            language_filters: vec!["rust".to_owned()],
            priority: 4,
        }
    );
    assert_eq!(
        parse_query_kind("sbom").expect("sbom query kind should parse"),
        CodeQueryKind::Sbom
    );
    assert_eq!(
        parse_repo_set(&[
            "remove".to_owned(),
            "workspace".to_owned(),
            "core".to_owned(),
        ])
        .expect("remove should parse"),
        RepoSetCommand::Remove {
            set_alias: "workspace".to_owned(),
            repository_alias: "core".to_owned(),
        }
    );
    assert_eq!(
        parse_repo_set(&[
            "query".to_owned(),
            "workspace".to_owned(),
            "--query".to_owned(),
            "RetryPolicy".to_owned(),
            "builder".to_owned(),
            "--kind".to_owned(),
            "references".to_owned(),
            "--limit".to_owned(),
            "7".to_owned(),
            "--path".to_owned(),
            "src".to_owned(),
            "--language".to_owned(),
            "rust".to_owned(),
            "--freshness".to_owned(),
            "wait-until-fresh".to_owned(),
        ])
        .expect("query should parse"),
        RepoSetCommand::Query {
            set_alias: "workspace".to_owned(),
            query: "RetryPolicy builder".to_owned(),
            kind: CodeQueryKind::References,
            limit: 7,
            path_filters: vec!["src".to_owned()],
            language_filters: vec!["rust".to_owned()],
            freshness: FreshnessPolicy::WaitUntilFresh,
            exclude_generated: false,
        }
    );
    assert!(matches!(
        parse_repo_set(&[
            "query".to_owned(),
            "workspace".to_owned(),
            "--query".to_owned(),
            "RetryPolicy".to_owned(),
            "--exclude-generated".to_owned(),
        ])
        .expect("query should parse generated exclusion"),
        RepoSetCommand::Query {
            exclude_generated: true,
            ..
        }
    ));
    assert_eq!(
        parse_repo_set(&[
            "query".to_owned(),
            "workspace".to_owned(),
            "serve".to_owned()
        ])
        .expect("positional query should parse"),
        RepoSetCommand::Query {
            set_alias: "workspace".to_owned(),
            query: "serve".to_owned(),
            kind: CodeQueryKind::Hybrid,
            limit: 10,
            path_filters: Vec::new(),
            language_filters: Vec::new(),
            freshness: FreshnessPolicy::AllowStale,
            exclude_generated: false,
        }
    );
    assert_eq!(
        parse_repo_set(&["status".to_owned(), "workspace".to_owned()])
            .expect("status should parse"),
        RepoSetCommand::Status {
            set_alias: "workspace".to_owned(),
        }
    );
    assert_eq!(
        parse_repo_set(&[
            "refresh".to_owned(),
            "workspace".to_owned(),
            "--async".to_owned(),
        ])
        .expect("refresh should parse"),
        RepoSetCommand::Refresh {
            set_alias: "workspace".to_owned(),
            async_task: true,
        }
    );
    assert_eq!(
        parse_repo_set(&[
            "refresh-worker".to_owned(),
            "--task-id".to_owned(),
            "task-1".to_owned(),
        ])
        .expect("worker should parse"),
        RepoSetCommand::RefreshWorker {
            task_id: Some("task-1".to_owned()),
        }
    );

    assert_eq!(
        parse_repo_set(&[]).expect_err("empty command should fail"),
        CliError::UnexpectedArgument("repo-set".to_owned())
    );
    assert_eq!(
        parse_repo_set(&["unknown".to_owned()]).expect_err("unknown command should fail"),
        CliError::UnexpectedArgument("unknown".to_owned())
    );
    assert_eq!(
        parse_repo_set(&["add".to_owned(), "workspace".to_owned(), "core".to_owned()])
            .expect_err("missing ref should fail"),
        CliError::MissingValue("--ref")
    );
    assert_eq!(
        parse_repo_set(&["remove".to_owned(), "workspace".to_owned()])
            .expect_err("missing remove alias should fail"),
        CliError::MissingValue("<repo-alias>")
    );
    assert_eq!(
        parse_repo_set(&[
            "query".to_owned(),
            "workspace".to_owned(),
            "--kind".to_owned(),
            "unknown".to_owned(),
            "--query".to_owned(),
            "x".to_owned(),
        ])
        .expect_err("unknown query kind should fail"),
        CliError::InvalidCodeQueryKind("unknown".to_owned())
    );
    assert_eq!(
        parse_repo_set(&[
            "query".to_owned(),
            "workspace".to_owned(),
            "--query".to_owned(),
            "x".to_owned(),
            "--limit".to_owned(),
            "many".to_owned(),
        ])
        .expect_err("invalid limit should fail"),
        CliError::InvalidLimit("many".to_owned())
    );
}

#[tokio::test]
async fn runs_repo_set_commands_against_shared_service() {
    let app_repo = FixtureRepo::create("repo-set-cli-app");
    app_repo.write(
        "src/client.rs",
        r#"
use service::serve;

pub fn client() {
    serve();
}
"#,
    );
    app_repo.git(["add", "."]);
    app_repo.git(["commit", "-m", "app"]);
    let service_repo = FixtureRepo::create("repo-set-cli-service");
    service_repo.write(
        "src/service.rs",
        r#"
pub fn serve() -> u32 {
    2
}
"#,
    );
    service_repo.git(["add", "."]);
    service_repo.git(["commit", "-m", "service"]);
    let service = service_with_memory_store().await;

    register_and_index(&service, &app_repo, "app").await;
    register_and_index(&service, &service_repo, "svc").await;

    let created = run_repo_set(
        &service,
        RepoSetCommand::Create {
            alias: "workspace".to_owned(),
            description: Some("cli set".to_owned()),
        },
        context("create"),
        OutputFormat::Json,
    )
    .await
    .expect("set create should run");
    assert_eq!(json_value(&created)["repository_set"]["alias"], "workspace");

    for alias in ["app", "svc"] {
        let added = run_repo_set(
            &service,
            RepoSetCommand::Add {
                set_alias: "workspace".to_owned(),
                repository_alias: alias.to_owned(),
                ref_selector: "HEAD".to_owned(),
                path_filters: Vec::new(),
                language_filters: Vec::new(),
                priority: if alias == "app" { 10 } else { 0 },
            },
            context(&format!("add-{alias}")),
            OutputFormat::Json,
        )
        .await
        .expect("set add should run");
        assert_eq!(json_value(&added)["member"]["repository_alias"], alias);
    }

    let status = run_repo_set(
        &service,
        RepoSetCommand::Status {
            set_alias: "workspace".to_owned(),
        },
        context("status"),
        OutputFormat::Json,
    )
    .await
    .expect("status should run");
    assert_eq!(
        json_value(&status)["status"]["members"]
            .as_array()
            .unwrap()
            .len(),
        2
    );

    let graph_only = run_repo_set(
        &service,
        RepoSetCommand::Query {
            set_alias: "workspace".to_owned(),
            query: "serve".to_owned(),
            kind: CodeQueryKind::Definition,
            limit: 5,
            path_filters: Vec::new(),
            language_filters: Vec::new(),
            freshness: FreshnessPolicy::GraphOnly,
            exclude_generated: false,
        },
        context("query-graph-only"),
        OutputFormat::Json,
    )
    .await
    .expect("graph-only query should return diagnostics");
    let graph_only = json_value(&graph_only);
    assert_eq!(graph_only["results"].as_array().unwrap().len(), 0);
    assert_eq!(
        graph_only["degraded_reason"],
        "graph_only freshness policy selected"
    );

    let stale_overlay = run_repo_set(
        &service,
        RepoSetCommand::Query {
            set_alias: "workspace".to_owned(),
            query: "serve".to_owned(),
            kind: CodeQueryKind::Definition,
            limit: 5,
            path_filters: Vec::new(),
            language_filters: Vec::new(),
            freshness: FreshnessPolicy::WaitUntilFresh,
            exclude_generated: false,
        },
        context("query-wait-before-refresh"),
        OutputFormat::Json,
    )
    .await
    .expect_err("wait-until-fresh should reject stale overlay");
    assert!(
        stale_overlay
            .to_string()
            .contains("overlay is stale; run repo-set refresh")
    );

    let queried = run_repo_set(
        &service,
        RepoSetCommand::Query {
            set_alias: "workspace".to_owned(),
            query: "serve".to_owned(),
            kind: CodeQueryKind::Definition,
            limit: 5,
            path_filters: Vec::new(),
            language_filters: Vec::new(),
            freshness: FreshnessPolicy::AllowStale,
            exclude_generated: false,
        },
        context("query"),
        OutputFormat::Json,
    )
    .await
    .expect("query should run");
    assert!(
        json_value(&queried)["results"]
            .as_array()
            .expect("results should be an array")
            .iter()
            .any(|hit| hit["member"]["repository_alias"] == "svc")
    );

    let refreshed = run_repo_set(
        &service,
        RepoSetCommand::Refresh {
            set_alias: "workspace".to_owned(),
            async_task: false,
        },
        context("refresh"),
        OutputFormat::Json,
    )
    .await
    .expect("refresh should run");
    assert_eq!(json_value(&refreshed)["summary"]["resolved_edge_count"], 1);

    let queued = run_repo_set(
        &service,
        RepoSetCommand::Refresh {
            set_alias: "workspace".to_owned(),
            async_task: true,
        },
        context("refresh-async"),
        OutputFormat::Json,
    )
    .await
    .expect("async refresh should queue");
    assert_eq!(json_value(&queued)["task"]["state"], "queued");

    let completed = run_repo_set(
        &service,
        RepoSetCommand::RefreshWorker { task_id: None },
        context("refresh-worker"),
        OutputFormat::Json,
    )
    .await
    .expect("refresh worker should run");
    assert_eq!(json_value(&completed)["state"], "succeeded");

    let removed = run_repo_set(
        &service,
        RepoSetCommand::Remove {
            set_alias: "workspace".to_owned(),
            repository_alias: "app".to_owned(),
        },
        context("remove-app"),
        OutputFormat::Json,
    )
    .await
    .expect("set remove should run");
    let removed = json_value(&removed);
    assert_eq!(removed["member"]["repository_alias"], "app");
    assert_eq!(
        removed["status"]["members"]
            .as_array()
            .expect("members should be an array")
            .len(),
        1
    );
    assert_eq!(removed["status"]["overlay"]["state"], "missing");

    run_repo_set(
        &service,
        RepoSetCommand::Create {
            alias: "empty-workspace".to_owned(),
            description: None,
        },
        context("create-empty"),
        OutputFormat::Json,
    )
    .await
    .expect("empty set should create");
    let queued_empty = run_repo_set(
        &service,
        RepoSetCommand::Refresh {
            set_alias: "empty-workspace".to_owned(),
            async_task: true,
        },
        context("refresh-empty-async"),
        OutputFormat::Json,
    )
    .await
    .expect("empty async refresh should queue");
    let task_id = json_value(&queued_empty)["task"]["task_id"]
        .as_str()
        .expect("queued task should expose id")
        .to_owned();
    let failed_empty = run_repo_set(
        &service,
        RepoSetCommand::RefreshWorker {
            task_id: Some(task_id),
        },
        context("refresh-empty-worker"),
        OutputFormat::Json,
    )
    .await
    .expect_err("empty set worker should fail and persist retry");
    assert!(failed_empty.to_string().contains("has no members"));

    let idle = run_repo_set(
        &service,
        RepoSetCommand::RefreshWorker { task_id: None },
        context("refresh-worker-idle"),
        OutputFormat::Json,
    )
    .await
    .expect("idle refresh worker should run");
    assert!(idle.is_empty());
}

fn json_value(output: &str) -> serde_json::Value {
    serde_json::from_str(output.trim()).expect("json output should parse")
}

fn context(name: &str) -> RequestContext {
    RequestContext::with_ids(
        InterfaceKind::Cli,
        format!("req-{name}"),
        format!("trace-{name}"),
    )
}

async fn service_with_memory_store() -> RelayKnowledgeService {
    let environment = EnvironmentConfig::from_pairs(
        PlatformKind::Unix,
        [
            ("HOME", "/home/alice"),
            ("TMPDIR", "/tmp"),
            ("RELAY_KNOWLEDGE_HOME", "/srv/relay"),
        ],
    )
    .expect("environment should parse");
    let runtime = RuntimeConfiguration::from_environment(&environment)
        .await
        .expect("runtime should compose");
    let store = Arc::new(SqliteGraphStore::open_in_memory().expect("store should open"));

    RelayKnowledgeService::with_store(runtime, store)
}

async fn register_and_index(service: &RelayKnowledgeService, repo: &FixtureRepo, alias: &str) {
    service
        .register_code_repository(
            CodeRepositoryRegisterRequest {
                root_path: repo.path.display().to_string(),
                alias: alias.to_owned(),
                path_filters: Vec::new(),
                language_filters: Vec::new(),
            },
            context(&format!("register-{alias}")),
        )
        .await
        .expect("repository should register");
    service
        .index_code_repository(
            CodeIndexRequest {
                repository: CodeRepositorySelector::new(alias, "HEAD", Vec::new(), Vec::new())
                    .expect("selector should validate"),
                mode: CodeIndexMode::Full,
                workspace_detection: Default::default(),
                freshness_policy: FreshnessPolicy::WaitUntilFresh,
                reuse_historical: false,
            },
            context(&format!("index-{alias}")),
        )
        .await
        .expect("repository should index");
}

struct FixtureRepo {
    path: PathBuf,
}

impl FixtureRepo {
    fn create(name: &str) -> Self {
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("clock should be after epoch")
            .as_nanos();
        let path = std::env::temp_dir().join(format!("relay-knowledge-{name}-{nanos}"));
        fs::create_dir_all(path.join("src")).expect("repo directory should be created");
        let repo = Self { path };
        repo.git(["init"]);
        repo.git(["config", "user.email", "relay@example.invalid"]);
        repo.git(["config", "user.name", "Relay Test"]);
        repo
    }

    fn write(&self, relative: &str, content: &str) {
        let path = self.path.join(relative);
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).expect("parent directory should exist");
        }
        fs::write(path, content).expect("fixture file should be written");
    }

    fn git<const N: usize>(&self, args: [&str; N]) {
        let output = git_command(&self.path, args)
            .output()
            .expect("git should run");
        assert!(
            output.status.success(),
            "git failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }
}

impl Drop for FixtureRepo {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.path);
    }
}

fn git_command<const N: usize>(path: &Path, args: [&str; N]) -> Command {
    let mut command = Command::new("git");
    command.current_dir(path).args(args);
    command
}