conpub 0.1.3

Agent-first CLI for publishing local knowledge files to Confluence Cloud
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
use crate::domain::*;
use crate::infrastructure::*;
use crate::support::*;
use serde_json::json;
use std::collections::HashMap;
use std::path::PathBuf;

/// Read-only aggregate of the bound source and its publish state: every
/// document fingerprinted next to what was last published. Shared by
/// `status` and `plan`; takes no lock, stages nothing, and performs no
/// remote calls. The write path (`publish`/`sync`) keeps its own
/// `prepare_publish_set`, which layers subset selection, the hard
/// missing-asset refusal, and staging on top of the same inputs.
pub(super) struct PublishStateView {
    pub(super) resolved: ResolvedConfig,
    pub(super) stage_root: PathBuf,
    pub(super) state_path: PathBuf,
    pub(super) documents: Vec<Document>,
    pub(super) snapshots: Vec<DocumentSnapshot>,
    pub(super) state: SyncState,
}

pub(super) fn load_publish_state_view() -> AppResult<PublishStateView> {
    let resolved = resolve_config()?;
    let root = PathBuf::from(&resolved.root);
    let source = PathBuf::from(&resolved.source_abs);
    let documents = list_documents(&root, &source)?;
    validate_directory_index_conflicts(&resolved, &documents)?;
    validate_unique_slugs(&documents)?;
    let hierarchy = build_hierarchy(&resolved, &documents, &documents)?;
    let snapshots = snapshot_hierarchy(&root, &hierarchy)?;
    let stage_root = publish_stage_root(&resolved)?;
    let identity = sync_state_identity(&resolved);
    let state_path = sync_state_path(&stage_root);
    let state = load_sync_state(&state_path, &identity)?;

    Ok(PublishStateView {
        resolved,
        stage_root,
        state_path,
        documents,
        snapshots,
        state,
    })
}

pub(crate) fn cmd_plan() -> AppResult<serde_json::Value> {
    let view = load_publish_state_view()?;
    let root = PathBuf::from(&view.resolved.root);
    let missing_assets: HashMap<String, Vec<String>> =
        missing_staged_asset_references(&root, &view.documents)?
            .into_iter()
            .collect();

    let (mut sync_items, _) =
        build_sync_plan(&view.snapshots, &view.state, true, &view.resolved.source);
    join_sync_items_with_typub_status(&view.stage_root, &mut sync_items)?;

    // A document that references assets the shared `_assets/` staging cannot
    // provide is blocked regardless of its publish-state verdict: publishing
    // it would fail locally, so no state-derived action applies.
    let items = sync_items
        .into_iter()
        .map(|item| match missing_assets.get(&item.path) {
            Some(missing) => PlanItem {
                path: item.path,
                title: item.title,
                action: "blocked".to_string(),
                reason: format!(
                    "references assets not present in _assets/: {}",
                    missing.join(", ")
                ),
                confluence_url: item.url,
            },
            None => PlanItem {
                path: item.path,
                title: item.title,
                action: item.action,
                reason: item.reason.unwrap_or_default(),
                confluence_url: item.url,
            },
        })
        .collect::<Vec<_>>();

    let publishable = items
        .iter()
        .filter(|item| item.action == "create" || item.action == "update")
        .count();

    Ok(ok(json!({
        "target": view.resolved.target,
        "state_file": display_path(&view.state_path),
        "count": items.len(),
        "publishable": publishable,
        "summary": plan_counts(&items),
        "items": items,
    })))
}

fn plan_counts(items: &[PlanItem]) -> serde_json::Value {
    let mut counts: HashMap<&str, usize> = HashMap::new();
    for item in items {
        *counts.entry(item.action.as_str()).or_default() += 1;
    }

    json!({
        "create": counts.get("create").copied().unwrap_or(0),
        "update": counts.get("update").copied().unwrap_or(0),
        "unchanged": counts.get("unchanged").copied().unwrap_or(0),
        "deleted": counts.get("deleted").copied().unwrap_or(0),
        "blocked": counts.get("blocked").copied().unwrap_or(0),
    })
}

pub(crate) fn cmd_publish(
    yes: bool,
    dry_run: bool,
    delay_ms: u64,
    concurrency: usize,
) -> AppResult<serde_json::Value> {
    if !dry_run && !yes {
        return Err(AppError::new(
            "CONFIRMATION_REQUIRED",
            "run `conpub publish --yes` to allow remote writes",
        ));
    }
    validate_publish_pacing(concurrency)?;

    let (prepared, _) = prepare_publish_set(&[])?;

    if dry_run {
        stage_dry_run_snapshots(&prepared)?;
        let items = dry_run_publish_items(&prepared.snapshots);

        return Ok(ok(json!({
            "dry_run": true,
            "target": &prepared.resolved.target,
            "stage_root": display_path(&prepared.stage_root),
            "typub_status_db": display_path(&typub_status_db_path(&prepared.stage_root)),
            "count": items.len(),
            "pacing": publish_pacing_json(delay_ms, concurrency),
            "items": items,
        })));
    }

    let _state_lock = lock_sync_state(&prepared.stage_root)?;
    let state = load_sync_state(&prepared.state_path, &prepared.identity)?;
    let items = publish_prepared_snapshots(&prepared, &prepared.snapshots, &state, delay_ms)?;
    let failed = items.iter().filter(|item| item.error.is_some()).count();
    update_sync_state_from_publish_results(
        &prepared.state_path,
        &prepared.identity,
        &prepared.snapshots,
        &items,
    )?;

    Ok(ok(json!({
        "dry_run": false,
        "target": &prepared.resolved.target,
        "stage_root": display_path(&prepared.stage_root),
        "typub_status_db": display_path(&typub_status_db_path(&prepared.stage_root)),
        "count": items.len(),
        "failed": failed,
        "pacing": publish_pacing_json(delay_ms, concurrency),
        "items": items,
    })))
}

pub(crate) fn cmd_sync(
    paths: Vec<String>,
    yes: bool,
    dry_run: bool,
    delay_ms: u64,
    concurrency: usize,
    archive_deleted: bool,
) -> AppResult<serde_json::Value> {
    if !dry_run && !yes {
        return Err(AppError::new(
            "CONFIRMATION_REQUIRED",
            "run `conpub sync --dry-run` to inspect changes or `conpub sync --yes` to allow remote writes",
        ));
    }
    validate_publish_pacing(concurrency)?;

    let (prepared, subset) = prepare_publish_set(&paths)?;
    let _state_lock = if dry_run {
        None
    } else {
        Some(lock_sync_state(&prepared.stage_root)?)
    };
    let mut state = load_sync_state(&prepared.state_path, &prepared.identity)?;
    let (mut items, publish_snapshots) = build_sync_plan(
        &prepared.snapshots,
        &state,
        !subset,
        &prepared.resolved.source,
    );
    join_sync_items_with_typub_status(&prepared.stage_root, &mut items)?;
    mark_superseded_deleted(&mut items);
    apply_archive_deleted_plan(&mut items, archive_deleted);

    if dry_run {
        return Ok(ok(json!({
            "dry_run": true,
            "subset": subset,
            "archive_deleted": archive_deleted,
            "target": &prepared.resolved.target,
            "stage_root": display_path(&prepared.stage_root),
            "state_file": display_path(&prepared.state_path),
            "typub_status_db": display_path(&typub_status_db_path(&prepared.stage_root)),
            "count": items.len(),
            "publishable": publish_snapshots.len(),
            "pacing": publish_pacing_json(delay_ms, concurrency),
            "summary": sync_counts(&items),
            "items": items,
        })));
    }

    if !publish_snapshots.is_empty() {
        let publish_results =
            publish_prepared_snapshots(&prepared, &publish_snapshots, &state, delay_ms)?;
        merge_publish_results_into_sync_items(&mut items, &publish_results);
        update_sync_state_from_sync_results(&mut state, &prepared.snapshots, &items);
    }

    // Re-mark after publish results land: a create that adopted an existing
    // page by title only now carries its page id, and the matching deleted
    // entry must never reach the archive call below.
    mark_superseded_deleted(&mut items);
    remove_superseded_deleted_from_state(&mut state, &items);

    if archive_deleted {
        let archive_submitted = runtime_archive_deleted(&prepared.resolved, &mut items)?;
        if archive_submitted {
            remove_archived_deleted_from_state(&mut state, &items);
        }
    }
    write_sync_state(&prepared.state_path, &state)?;

    let failed = items.iter().filter(|item| item.status == "failed").count();

    Ok(ok(json!({
        "dry_run": false,
        "subset": subset,
        "archive_deleted": archive_deleted,
        "target": &prepared.resolved.target,
        "stage_root": display_path(&prepared.stage_root),
        "state_file": display_path(&prepared.state_path),
        "typub_status_db": display_path(&typub_status_db_path(&prepared.stage_root)),
        "count": items.len(),
        "publishable": publish_snapshots.len(),
        "failed": failed,
        "pacing": publish_pacing_json(delay_ms, concurrency),
        "summary": sync_counts(&items),
        "items": items,
    })))
}

/// Reconcile deleted state entries. Default is bookkeeping-only: entries are
/// dropped from the local state and the remote pages are left in place.
/// `--archive` archives the pages first; `--delete` deletes them permanently.
/// Superseded entries (page id owned by a live document after an adoption)
/// are always dropped without any remote action, whatever flags are set.
pub(crate) fn cmd_prune(yes: bool, archive: bool, delete: bool) -> AppResult<serde_json::Value> {
    if archive && delete {
        return Err(AppError::new(
            "PRUNE_CONFLICTING_FLAGS",
            "--archive and --delete are mutually exclusive",
        ));
    }

    let (prepared, _subset) = prepare_publish_set(&[])?;
    let _state_lock = if yes {
        Some(lock_sync_state(&prepared.stage_root)?)
    } else {
        None
    };
    let mut state = load_sync_state(&prepared.state_path, &prepared.identity)?;
    let (mut items, _) =
        build_sync_plan(&prepared.snapshots, &state, true, &prepared.resolved.source);
    join_sync_items_with_typub_status(&prepared.stage_root, &mut items)?;
    mark_superseded_deleted(&mut items);
    let mut items = items
        .into_iter()
        .filter(|item| item.action == "deleted")
        .collect::<Vec<_>>();

    for item in &mut items {
        if item.status == "superseded" {
            continue;
        }
        let (status, reason) = match (&item.platform_id, archive, delete) {
            (Some(_), true, _) => (
                "pending_archive",
                "would archive the Confluence page, then drop the state entry",
            ),
            (Some(_), _, true) => (
                "pending_delete",
                "would delete the Confluence page permanently, then drop the state entry",
            ),
            (Some(_), false, false) => (
                "pending_prune",
                "would drop the state entry; the Confluence page is left in place",
            ),
            (None, ..) => (
                "pending_prune",
                "no Confluence page id is known; would drop the state entry",
            ),
        };
        item.status = status.to_string();
        item.reason = Some(reason.to_string());
    }

    if !yes {
        return Ok(ok(json!({
            "dry_run": true,
            "archive": archive,
            "delete": delete,
            "target": &prepared.resolved.target,
            "state_file": display_path(&prepared.state_path),
            "count": items.len(),
            "items": items,
        })));
    }

    if archive {
        runtime_archive_deleted(&prepared.resolved, &mut items)?;
    } else if delete {
        runtime_delete_deleted(&prepared.resolved, &mut items)?;
    }

    let mut pruned = 0_usize;
    for item in &mut items {
        let drop_entry = matches!(
            item.status.as_str(),
            "superseded" | "archived" | "deleted_remote" | "pending_prune"
        );
        if drop_entry {
            state.documents.remove(&item.path);
            pruned += 1;
            if item.status == "pending_prune" {
                item.status = "pruned".to_string();
                item.reason = Some("state entry dropped".to_string());
            }
        }
    }
    write_sync_state(&prepared.state_path, &state)?;

    let failed = items.iter().filter(|item| item.status == "failed").count();

    Ok(ok(json!({
        "dry_run": false,
        "archive": archive,
        "delete": delete,
        "target": &prepared.resolved.target,
        "state_file": display_path(&prepared.state_path),
        "count": items.len(),
        "pruned": pruned,
        "failed": failed,
        "items": items,
    })))
}

struct PreparedPublishSet {
    resolved: ResolvedConfig,
    stage_root: PathBuf,
    identity: SyncStateIdentity,
    state_path: PathBuf,
    snapshots: Vec<DocumentSnapshot>,
}

fn prepare_publish_set(paths: &[String]) -> AppResult<(PreparedPublishSet, bool)> {
    let resolved = resolve_config()?;
    let root = PathBuf::from(&resolved.root);
    let source = PathBuf::from(&resolved.source_abs);
    let subset = !paths.is_empty();
    let all_documents = list_documents(&root, &source)?;
    validate_directory_index_conflicts(&resolved, &all_documents)?;
    validate_unique_slugs(&all_documents)?;

    let selected_documents = if subset {
        resolve_document_subset(&root, &source, paths)?
    } else {
        all_documents.clone()
    };
    let hierarchy = build_hierarchy(&resolved, &selected_documents, &all_documents)?;

    // Refuse the publish set locally when any document to be published
    // references an asset the shared `_assets/` staging cannot provide —
    // otherwise the failure surfaces half-way remote, after page creation.
    let publish_documents: Vec<Document> = hierarchy
        .iter()
        .map(|entry| entry.document.clone())
        .collect();
    let missing = missing_staged_asset_references(&root, &publish_documents)?;
    if !missing.is_empty() {
        let detail = missing
            .iter()
            .map(|(path, refs)| format!("{path}: {}", refs.join(", ")))
            .collect::<Vec<_>>()
            .join("; ");
        return Err(AppError::new(
            "ASSET_MISSING",
            format!(
                "documents reference assets not present in _assets/ (place files under <root>/_assets/ and reference them as assets/<name>): {detail}"
            ),
        ));
    }

    let stage_root = publish_stage_root(&resolved)?;
    let identity = sync_state_identity(&resolved);
    let state_path = sync_state_path(&stage_root);
    let snapshots = snapshot_hierarchy(&root, &hierarchy)?;

    Ok((
        PreparedPublishSet {
            resolved,
            stage_root,
            identity,
            state_path,
            snapshots,
        },
        subset,
    ))
}

fn publish_prepared_snapshots(
    prepared: &PreparedPublishSet,
    snapshots: &[DocumentSnapshot],
    state: &SyncState,
    delay_ms: u64,
) -> AppResult<Vec<PublishItemResult>> {
    let typub_config = build_typub_config(&prepared.resolved, &prepared.stage_root)?;
    let runtime = build_async_runtime()?;
    runtime.block_on(publish_snapshots_with_hierarchy(
        &typub_config,
        &prepared.resolved,
        &prepared.stage_root,
        snapshots,
        state,
        delay_ms,
    ))
}

fn stage_dry_run_snapshots(prepared: &PreparedPublishSet) -> AppResult<()> {
    for snapshot in &prepared.snapshots {
        stage_document_files(&prepared.resolved, &snapshot.document, &prepared.stage_root)?;
    }

    Ok(())
}

fn dry_run_publish_items(snapshots: &[DocumentSnapshot]) -> Vec<PublishItemResult> {
    snapshots
        .iter()
        .map(|item| PublishItemResult {
            path: item.document.path.clone(),
            title: item.document.title.clone(),
            slug: item.slug.clone(),
            parent_path: item.parent_path.clone(),
            parent_id: None,
            status: "dry_run".to_string(),
            url: None,
            platform_id: None,
            error: None,
        })
        .collect()
}