gobby-wiki 0.3.0

Gobby wiki CLI shell
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
use super::*;
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::time::Duration;

use crate::WikiError;
use crate::research::{AcceptedNoteDraft, ResearchStopReason};
use crate::research_loop::helpers::validate_source_reference;
use crate::session::AcceptedResearchNote;

#[derive(Default)]
struct FakeModel {
    decisions: VecDeque<ModelDecision>,
}

impl FakeModel {
    fn new(decisions: Vec<ModelDecision>) -> Self {
        Self {
            decisions: decisions.into(),
        }
    }
}

impl ResearchModel for FakeModel {
    fn next_action(
        &mut self,
        _request: ModelRequest<'_>,
    ) -> Result<ModelDecision, ResearchModelError> {
        self.decisions
            .pop_front()
            .ok_or_else(|| ResearchModelError::InvalidResponse("no decision".to_string()))
    }
}

struct BudgetModel;

impl ResearchModel for BudgetModel {
    fn next_action(
        &mut self,
        _request: ModelRequest<'_>,
    ) -> Result<ModelDecision, ResearchModelError> {
        Err(ResearchModelError::BudgetExceeded)
    }
}

struct FakeSearch;

impl WikiSearch for FakeSearch {
    fn search(&mut self, query: &str, _limit: usize) -> Result<ResearchObservation, WikiError> {
        Ok(
            ResearchObservation::new("search", format!("searched {query}"))
                .with_sources(vec!["raw/source.md".to_string()]),
        )
    }
}

struct FakeAsk;

impl WikiAsk for FakeAsk {
    fn ask(&mut self, query: &str) -> Result<ResearchObservation, WikiError> {
        Ok(ResearchObservation::new("ask", format!("asked {query}")))
    }
}

struct FakeRead;

impl WikiRead for FakeRead {
    fn read(&mut self, path: &Path) -> Result<ResearchObservation, WikiError> {
        Ok(ResearchObservation::new("read", path.display().to_string())
            .with_sources(vec![path.display().to_string()]))
    }
}

struct FakeIngest;

impl SourceIngestor for FakeIngest {
    fn ingest_url(&mut self, url: &str) -> Result<ResearchObservation, WikiError> {
        Ok(ResearchObservation::new("ingest_url", url).with_sources(vec![url.to_string()]))
    }

    fn ingest_file(&mut self, path: &Path) -> Result<ResearchObservation, WikiError> {
        Ok(
            ResearchObservation::new("ingest_file", path.display().to_string())
                .with_sources(vec![path.display().to_string()]),
        )
    }
}

#[derive(Default)]
struct FakeWriter {
    notes: Vec<AcceptedNoteDraft>,
    conflict: bool,
}

impl ResearchNoteWriter for FakeWriter {
    fn write_note(&mut self, note: &AcceptedNoteDraft) -> Result<NoteWriteOutcome, WikiError> {
        self.notes.push(note.clone());
        Ok(NoteWriteOutcome {
            note: AcceptedResearchNote {
                title: note.title.clone(),
                path: PathBuf::from(format!("raw/research/{}.md", note.title)),
            },
            created: !self.conflict,
            write_conflict: self.conflict,
        })
    }
}

fn test_deps<'a>(
    model: &'a mut dyn ResearchModel,
    ask: &'a mut dyn WikiAsk,
    search: &'a mut dyn WikiSearch,
    read: &'a mut dyn WikiRead,
    ingest: &'a mut dyn SourceIngestor,
    note_writer: &'a mut dyn ResearchNoteWriter,
) -> ResearchLoopDeps<'a> {
    ResearchLoopDepsBuilder::default()
        .model(model)
        .ask(ask)
        .search(search)
        .read(read)
        .ingest(ingest)
        .note_writer(note_writer)
        .build()
        .expect("all research loop dependencies are set")
}

#[test]
fn research_loop_deps_builder_reports_missing_required_fields() {
    let missing_model = match ResearchLoopDepsBuilder::default().build() {
        Err(field) => field,
        Ok(_) => panic!("builder should require model"),
    };
    assert_eq!(missing_model, ResearchLoopDepsBuildError::Model);

    let mut model = FakeModel::default();
    let mut ask = FakeAsk;
    let mut search = FakeSearch;
    let mut read = FakeRead;
    let mut ingest = FakeIngest;
    let missing_writer = match ResearchLoopDepsBuilder::default()
        .model(&mut model)
        .ask(&mut ask)
        .search(&mut search)
        .read(&mut read)
        .ingest(&mut ingest)
        .build()
    {
        Err(field) => field,
        Ok(_) => panic!("builder should require note_writer"),
    };
    assert_eq!(missing_writer, ResearchLoopDepsBuildError::NoteWriter);
}

#[test]
fn model_budget_error_stops_as_budget_exhausted() {
    let mut model = BudgetModel;
    let mut ask = FakeAsk;
    let mut search = FakeSearch;
    let mut read = FakeRead;
    let mut ingest = FakeIngest;
    let mut writer = FakeWriter::default();
    let mut loop_ = ResearchLoop::new(
        Path::new("/tmp/wiki"),
        config(),
        test_deps(
            &mut model,
            &mut ask,
            &mut search,
            &mut read,
            &mut ingest,
            &mut writer,
        ),
    );

    let result = loop_
        .run(ResearchLoopInput {
            question: "What is exhausted?",
            source_constraints: &[],
            initial_notes: &[],
        })
        .expect("loop runs");

    assert_eq!(result.stop_reason, ResearchStopReason::BudgetExhausted);
}

fn config() -> ResearchLoopConfig {
    ResearchLoopConfig {
        max_steps: 12,
        max_tokens: 24_000,
        max_sources: 8,
        max_wall_time: Duration::from_secs(900),
        max_note_bytes: 24_000,
    }
}

#[test]
fn model_planned_note_is_written_after_source_is_observed() {
    let root = tempfile::tempdir().expect("wiki root");
    let raw_dir = root.path().join("raw");
    std::fs::create_dir_all(&raw_dir).expect("raw dir");
    std::fs::write(raw_dir.join("source.md"), "source").expect("source file");

    let mut model = FakeModel::new(vec![
        ModelDecision {
            action: ResearchAction::Search {
                query: "events".to_string(),
            },
            tokens_used: 10,
        },
        ModelDecision {
            action: ResearchAction::AcceptNote {
                title: "Event notes".to_string(),
                body: "Events are persisted.".to_string(),
                sources: vec!["raw/source.md".to_string()],
            },
            tokens_used: 12,
        },
        ModelDecision {
            action: ResearchAction::Finish {
                reason: Some("done".to_string()),
            },
            tokens_used: 2,
        },
    ]);

    let mut ask = FakeAsk;
    let mut search = FakeSearch;
    let mut read = FakeRead;
    let mut ingest = FakeIngest;
    let mut writer = FakeWriter::default();
    let mut loop_ = ResearchLoop::new(
        root.path(),
        config(),
        test_deps(
            &mut model,
            &mut ask,
            &mut search,
            &mut read,
            &mut ingest,
            &mut writer,
        ),
    );
    let result = loop_
        .run(ResearchLoopInput {
            question: "How are events persisted?",
            source_constraints: &[],
            initial_notes: &[],
        })
        .expect("loop runs");
    assert_eq!(result.stop_reason, ResearchStopReason::Finish);
    assert_eq!(writer.notes.len(), 1);
    assert_eq!(writer.notes[0].sources, vec!["raw/source.md".to_string()]);
}

#[test]
fn write_conflict_stops_the_run_without_recording_the_note() {
    let root = tempfile::tempdir().expect("wiki root");
    let raw_dir = root.path().join("raw");
    std::fs::create_dir_all(&raw_dir).expect("raw dir");
    std::fs::write(raw_dir.join("source.md"), "source").expect("source file");

    let mut model = FakeModel::new(vec![
        ModelDecision {
            action: ResearchAction::Search {
                query: "events".to_string(),
            },
            tokens_used: 10,
        },
        ModelDecision {
            action: ResearchAction::AcceptNote {
                title: "Event notes".to_string(),
                body: "Events are persisted.".to_string(),
                sources: vec!["raw/source.md".to_string()],
            },
            tokens_used: 12,
        },
    ]);

    let mut ask = FakeAsk;
    let mut search = FakeSearch;
    let mut read = FakeRead;
    let mut ingest = FakeIngest;
    let mut writer = FakeWriter {
        conflict: true,
        ..Default::default()
    };
    let mut loop_ = ResearchLoop::new(
        root.path(),
        config(),
        test_deps(
            &mut model,
            &mut ask,
            &mut search,
            &mut read,
            &mut ingest,
            &mut writer,
        ),
    );
    let result = loop_
        .run(ResearchLoopInput {
            question: "How are events persisted?",
            source_constraints: &[],
            initial_notes: &[],
        })
        .expect("loop runs");
    assert_eq!(result.stop_reason, ResearchStopReason::WriteConflict);
    assert!(result.write_conflict);
    // The run halts without recording or overwriting the note.
    assert!(result.accepted_notes.is_empty());
    assert!(result.changed_paths.is_empty());
}

#[test]
fn accepted_note_without_observed_source_is_blocked() {
    let mut model = FakeModel::new(vec![ModelDecision {
        action: ResearchAction::AcceptNote {
            title: "Unsupported".to_string(),
            body: "A claim with no source.".to_string(),
            sources: vec!["raw/missing.md".to_string()],
        },
        tokens_used: 7,
    }]);

    let mut ask = FakeAsk;
    let mut search = FakeSearch;
    let mut read = FakeRead;
    let mut ingest = FakeIngest;
    let mut writer = FakeWriter::default();
    let mut loop_ = ResearchLoop::new(
        Path::new("/tmp/wiki"),
        config(),
        test_deps(
            &mut model,
            &mut ask,
            &mut search,
            &mut read,
            &mut ingest,
            &mut writer,
        ),
    );
    let result = loop_
        .run(ResearchLoopInput {
            question: "What is unsupported?",
            source_constraints: &[],
            initial_notes: &[],
        })
        .expect("loop runs");
    assert_eq!(result.stop_reason, ResearchStopReason::SourceBlocked);
    assert!(writer.notes.is_empty());
    assert_eq!(result.gaps.len(), 1);
}

#[test]
fn parses_fenced_json_action() {
    let action =
        parse_model_action("```json\n{\"action\":\"finish\",\"reason\":\"enough evidence\"}\n```")
            .expect("action parsed");
    assert_eq!(
        action,
        ResearchAction::Finish {
            reason: Some("enough evidence".to_string())
        }
    );
}

#[test]
fn file_url_source_references_use_path_scope_validation() {
    let root = tempfile::tempdir().expect("wiki root");
    let inside = root.path().join("raw/source.md");
    std::fs::create_dir_all(inside.parent().expect("inside parent")).expect("inside raw dir");
    std::fs::write(&inside, "source").expect("inside source");
    let inside_url = url::Url::from_file_path(&inside).expect("inside file URL");
    assert!(validate_source_reference(root.path(), inside_url.as_str()).is_ok());

    let outside = tempfile::tempdir().expect("outside root");
    std::fs::create_dir_all(outside.path().join("raw")).expect("outside raw dir");
    std::fs::write(outside.path().join("raw/source.md"), "source").expect("outside source");
    let outside_url =
        url::Url::from_file_path(outside.path().join("raw/source.md")).expect("outside file URL");
    let error = validate_source_reference(root.path(), outside_url.as_str())
        .expect_err("outside file URL rejected");

    assert!(error.contains("outside the wiki scope"));
}

#[cfg(unix)]
#[test]
fn source_reference_rejects_relative_symlink_escape() {
    let root = tempfile::tempdir().expect("wiki root");
    let outside = tempfile::tempdir().expect("outside root");
    let outside_source = outside.path().join("source.md");
    std::fs::write(&outside_source, "source").expect("outside source");

    let raw_dir = root.path().join("raw");
    std::fs::create_dir_all(&raw_dir).expect("raw dir");
    std::os::unix::fs::symlink(&outside_source, raw_dir.join("source.md")).expect("source symlink");

    let error = validate_source_reference(root.path(), "raw/source.md")
        .expect_err("symlink escape rejected");

    assert!(error.contains("relative source path must stay inside the wiki scope"));
}