pagesift 1.6.1

A single-binary, Firecrawl-compatible web scraping engine that turns pages into clean Markdown.
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
//! `pagesift graph inspect` subcommand: inspect crawl output artifacts or persisted DataGraph stores.

use std::path::Path;

use anyhow::{Context, bail};
use clap::Subcommand;
use pagesift_graph::{GraphOptions, GraphSummary, PageGraphDelta, StreamingGraphBuilder};

use crate::*;

#[derive(Subcommand)]
pub(crate) enum GraphCommand {
    /// Inspect DataGraph artifacts in a crawl `--out` directory, graph JSON/JSONL file, or data store directory
    Inspect {
        /// Path to `--out` directory, `graph_summary.json` / `graph.jsonl` file, or store data directory
        path: PathBuf,
        /// Filter version history by URL or normalized resource key (for store or delta inspection)
        #[arg(long)]
        url: Option<String>,
        /// Filter recorded conflicts or claims by entity subject key (e.g. sku:PRO-1)
        #[arg(long)]
        subject: Option<String>,
        /// Filter historical store state at or before Unix ms or RFC-3339 timestamp
        #[arg(long)]
        as_of: Option<String>,
        /// Output raw inspection result as JSON
        #[arg(long)]
        json: bool,
    },
}

fn parse_as_of_cli(raw: Option<&str>) -> Result<Option<u64>> {
    let Some(s) = raw.map(str::trim).filter(|v| !v.is_empty()) else {
        return Ok(None);
    };
    if let Ok(ms) = s.parse::<u64>() {
        return Ok(Some(ms));
    }
    pagesift_graph::parse_timestamp_ms(s)
        .map(Some)
        .with_context(|| format!("invalid --as-of timestamp {s:?}; expected Unix ms or RFC-3339"))
}

pub(crate) fn run(action: GraphCommand, _cfg: pagesift::config::Config) -> Result<ExitCode> {
    match action {
        GraphCommand::Inspect {
            path,
            url,
            subject,
            as_of,
            json,
        } => inspect(
            &path,
            url.as_deref(),
            subject.as_deref(),
            as_of.as_deref(),
            json,
        ),
    }
}

fn inspect(
    path: &Path,
    url: Option<&str>,
    subject: Option<&str>,
    as_of: Option<&str>,
    as_json: bool,
) -> Result<ExitCode> {
    let as_of_ms = parse_as_of_cli(as_of)?;
    if !path.exists() {
        bail!("path does not exist: {}", path.display());
    }

    if path.is_file() {
        let summary = if path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
            read_jsonl_summary(path)?
        } else {
            let bytes = std::fs::read(path)
                .with_context(|| format!("reading graph summary {}", path.display()))?;
            serde_json::from_slice::<GraphSummary>(&bytes)
                .with_context(|| format!("parsing GraphSummary from {}", path.display()))?
        };
        return emit_summary(&summary, url, subject, as_json);
    }

    let summary_file = path.join("graph_summary.json");
    let deltas_file = path.join("graph.jsonl");
    if summary_file.exists() {
        let bytes = std::fs::read(&summary_file)
            .with_context(|| format!("reading {}", summary_file.display()))?;
        let summary = serde_json::from_slice::<GraphSummary>(&bytes)
            .with_context(|| format!("parsing {}", summary_file.display()))?;
        return emit_summary(&summary, url, subject, as_json);
    }
    if deltas_file.exists() {
        let summary = read_jsonl_summary(&deltas_file)?;
        return emit_summary(&summary, url, subject, as_json);
    }

    let store = pagesift_store::Store::open_dir(path)
        .with_context(|| format!("opening store at {}", path.display()))?;
    inspect_store(&store, url, subject, as_of_ms, as_json)
}

fn read_jsonl_summary(path: &Path) -> Result<GraphSummary> {
    let text = std::fs::read_to_string(path)
        .with_context(|| format!("reading graph deltas {}", path.display()))?;
    let mut builder = StreamingGraphBuilder::new(GraphOptions::enabled());
    for (idx, line) in text.lines().enumerate() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        let delta: PageGraphDelta = serde_json::from_str(trimmed)
            .with_context(|| format!("parsing PageGraphDelta at line {}", idx + 1))?;
        builder.ingest_delta(delta);
    }
    Ok(builder.finalize())
}

fn emit_summary(
    summary: &GraphSummary,
    url: Option<&str>,
    subject: Option<&str>,
    as_json: bool,
) -> Result<ExitCode> {
    let filtered_conflicts: Vec<_> = summary
        .conflicts
        .iter()
        .filter(|c| subject.is_none_or(|s| c.subject_key == s))
        .cloned()
        .collect();
    if let Some(target) = url {
        let key = if target.contains("://") {
            pagesift_graph::normalize_resource_key(target)
        } else {
            target.to_string()
        };
        let nodes: Vec<_> = summary
            .nodes
            .iter()
            .filter(|n| n.resource_key == key)
            .collect();
        let edges: Vec<_> = summary
            .versions
            .iter()
            .filter(|e| e.resource_key == key)
            .collect();
        if as_json {
            println!(
                "{}",
                serde_json::to_string_pretty(&serde_json::json!({
                    "resourceKey": key,
                    "nodes": nodes,
                    "versions": edges,
                    "conflicts": filtered_conflicts,
                }))?
            );
        } else {
            println!("resource_key: {key}");
            println!(
                "nodes: {}, version_edges: {}, conflicts: {}",
                nodes.len(),
                edges.len(),
                filtered_conflicts.len()
            );
            for n in nodes {
                println!(
                    "  {} status={} ts={} title={:?}",
                    n.final_url, n.status_code, n.observed_at_ms, n.title
                );
            }
        }
        return Ok(ExitCode::SUCCESS);
    }
    if as_json {
        if subject.is_some() {
            println!(
                "{}",
                serde_json::to_string_pretty(&serde_json::json!({
                    "subject": subject,
                    "conflicts": filtered_conflicts,
                }))?
            );
        } else {
            println!("{}", serde_json::to_string_pretty(summary)?);
        }
        return Ok(ExitCode::SUCCESS);
    }
    let st = &summary.stats;
    println!(
        "nodes: {}  blocks: {}  citations: {}  provenance: {}  versions: {}  evidence: {}  claims: {}  fact_clusters: {}  conflicts: {}  canonical_clusters: {}",
        st.nodes,
        st.blocks,
        st.citations,
        st.provenance_edges,
        st.version_edges,
        st.evidence_spans,
        st.claims,
        st.fact_clusters,
        st.conflicts,
        st.canonical_clusters
    );
    for c in &filtered_conflicts {
        println!(
            "conflict [{}] {} .{}: {} variants (preferred={:?})",
            c.conflict_id,
            c.subject_key,
            c.field_path,
            c.variants.len(),
            c.preferred_hint
        );
    }
    Ok(ExitCode::SUCCESS)
}

fn inspect_store(
    store: &pagesift_store::Store,
    url: Option<&str>,
    subject: Option<&str>,
    as_of_ms: Option<u64>,
    as_json: bool,
) -> Result<ExitCode> {
    let gs = store.graph();
    let max_limit = pagesift_store::DEFAULT_GRAPH_CONFLICTS_LIMIT;
    let (conflicts, _, _, conflicts_unavail) =
        gs.list_conflicts_as_of_checked(subject, as_of_ms, max_limit, 0)?;
    if let Some(target) = url {
        let hist = gs.history_as_of(
            target,
            as_of_ms,
            pagesift_store::DEFAULT_GRAPH_HISTORY_LIMIT,
        )?;
        let res = gs.claims_for_resource_as_of_checked(target, as_of_ms)?;
        let history_unavailable = conflicts_unavail || res.history_unavailable;
        let mut claims = res.claims;
        if let Some(sk) = subject {
            claims.retain(|c| c.claim.subject_key == sk);
        }
        if as_json {
            println!(
                "{}",
                serde_json::to_string_pretty(&serde_json::json!({
                    "resourceKey": hist.resource_key,
                    "asOfMs": as_of_ms,
                    "versions": hist.versions,
                    "edges": hist.edges,
                    "claims": claims,
                    "conflicts": conflicts,
                    "historyUnavailable": history_unavailable,
                }))?
            );
        } else {
            println!("resource_key: {}", hist.resource_key);
            println!(
                "versions: {}, edges: {}, claims: {}, conflicts: {}, history_unavailable: {}",
                hist.versions.len(),
                hist.edges.len(),
                claims.len(),
                conflicts.len(),
                history_unavailable
            );
            for v in &hist.versions {
                println!(
                    "  {} doc={} status={} ts={}",
                    v.url, v.doc_id, v.status_code, v.observed_at_ms
                );
            }
        }
        return Ok(ExitCode::SUCCESS);
    }
    let (claims, _, _, claims_unavail) =
        gs.list_claims_as_of_checked(subject, as_of_ms, max_limit, 0)?;
    let history_unavailable = conflicts_unavail || claims_unavail;
    if as_json {
        println!(
            "{}",
            serde_json::to_string_pretty(&serde_json::json!({
                "subject": subject,
                "asOfMs": as_of_ms,
                "totalClaims": claims.len(),
                "claims": claims,
                "totalConflicts": conflicts.len(),
                "conflicts": conflicts,
                "historyUnavailable": history_unavailable,
            }))?
        );
    } else {
        println!(
            "claims: {}, conflicts: {}, history_unavailable: {}",
            claims.len(),
            conflicts.len(),
            history_unavailable
        );
        for c in &conflicts {
            println!(
                "conflict [{}] {} .{}: {} variants",
                c.conflict_id,
                c.subject_key,
                c.field_path,
                c.variants.len()
            );
        }
    }
    Ok(ExitCode::SUCCESS)
}

#[cfg(test)]
mod tests {
    use super::*;
    use pagesift_graph::{DocumentInput, SourceKind};

    fn doc(url: &str, hash: &str, ts: u64) -> DocumentInput {
        DocumentInput {
            requested_url: url.into(),
            final_url: url.into(),
            canonical_url: None,
            canonical_from_header: false,
            source_kind: SourceKind::Html,
            observed_at_ms: ts,
            status_code: 200,
            engine: "http".into(),
            content_hash: Some(hash.into()),
            raw_hash: None,
            text_hash: None,
            etag: None,
            last_modified: None,
            title: Some("Home".into()),
            markdown: "# Home".into(),
            redirect_chain: Vec::new(),
            links: Vec::new(),
            alternates: Vec::new(),
            duplicate_of: None,
            attachments: Vec::new(),
            structured_json_ld: Vec::new(),
            extracted_fields: None,
            raw_traces: Vec::new(),
            chunk_spans: Vec::new(),
            previous_version: None,
            version_diff_stats: None,
            changed_block_ids: Vec::new(),
        }
    }

    #[test]
    fn inspect_reads_directory_jsonl_and_summary() {
        let dir = tempfile::tempdir().unwrap();
        let mut builder = StreamingGraphBuilder::new(GraphOptions::enabled());
        let d1 = builder.ingest(doc("https://example.com/", "h1", 100));
        let d2 = builder.ingest(doc("https://example.com/about", "h2", 200));
        let summary = builder.finalize();

        let jsonl = format!(
            "{}\n{}\n",
            serde_json::to_string(&d1).unwrap(),
            serde_json::to_string(&d2).unwrap(),
        );
        let jsonl_path = dir.path().join("graph.jsonl");
        std::fs::write(&jsonl_path, jsonl).unwrap();

        let from_jsonl = read_jsonl_summary(&jsonl_path).unwrap();
        assert_eq!(from_jsonl.nodes.len(), 2);

        let sum_path = dir.path().join("graph_summary.json");
        std::fs::write(&sum_path, serde_json::to_vec(&summary).unwrap()).unwrap();
        assert_eq!(
            inspect(dir.path(), Some("https://example.com/"), None, None, true).unwrap(),
            ExitCode::SUCCESS
        );
        assert_eq!(
            inspect(&jsonl_path, None, None, None, false).unwrap(),
            ExitCode::SUCCESS
        );
    }

    #[test]
    fn inspect_reads_store_dir_as_of() {
        let dir = tempfile::tempdir().unwrap();
        let store = pagesift_store::Store::open_dir(dir.path()).unwrap();
        let gs = store.graph();
        let mut builder = StreamingGraphBuilder::new(GraphOptions::enabled());
        let d1 = builder.ingest(doc("https://example.com/", "h1", 100));
        gs.commit_version(pagesift_store::VersionCommitRequest {
            expected_prev_doc_id: None,
            alias_keys: &[],
            record: &pagesift_graph::VersionRecord {
                resource_key: d1.node.resource_key.clone(),
                doc_id: d1.node.doc_id.clone(),
                url: d1.node.final_url.clone(),
                canonical_url: None,
                observed_at_ms: 100,
                content_hash: Some("h1".into()),
                text_hash: None,
                status_code: 200,
                etag: None,
                last_modified: None,
            },
            edge: None,
            snapshot_bytes: Some(b"# Home"),
            snapshot_unavailable_reason: None,
        })
        .unwrap();
        drop(gs);
        drop(store);

        assert_eq!(
            inspect(
                dir.path(),
                Some("https://example.com/"),
                None,
                Some("100"),
                true
            )
            .unwrap(),
            ExitCode::SUCCESS
        );
        assert_eq!(
            inspect(dir.path(), None, Some("subject-1"), Some("100"), false).unwrap(),
            ExitCode::SUCCESS
        );
    }
}