govctl 0.8.4

Project governance CLI for RFC, ADR, and Work Item management
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! New command implementation - create artifacts.

use crate::NewTarget;
use crate::config::{Config, IdStrategy};
use crate::diagnostic::{Diagnostic, DiagnosticCode};
use crate::model::{
    AdrContent, AdrMeta, AdrSpec, AdrStatus, ChangelogEntry, ClauseKind, ClauseSpec, ClauseStatus,
    ClauseWire, RfcPhase, RfcSpec, RfcStatus, RfcWire, SectionSpec, WorkItemContent, WorkItemMeta,
    WorkItemSpec, WorkItemStatus, WorkItemVerification,
};
use crate::schema::ARTIFACT_SCHEMA_TEMPLATES;
use crate::schema::{ArtifactSchema, with_schema_header};
use crate::ui;
use crate::write::{WriteOp, create_dir_all, today, write_file};
use slug::slugify;

fn schema_version_for_init() -> u32 {
    std::env::var("GOVCTL_SCHEMA_VERSION")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(crate::cmd::migrate::CURRENT_SCHEMA_VERSION)
}
use std::path::PathBuf;

/// Skill templates: (relative_path, content) pairs.
/// Source of truth: .claude/skills/; embedded at compile time.
/// Per ADR-0028, all workflow commands are now skills for cross-platform compatibility.
const SKILL_TEMPLATES: &[(&str, &str)] = &[
    (
        "skills/discuss/SKILL.md",
        include_str!("../../.claude/skills/discuss/SKILL.md"),
    ),
    (
        "skills/gov/SKILL.md",
        include_str!("../../.claude/skills/gov/SKILL.md"),
    ),
    (
        "skills/quick/SKILL.md",
        include_str!("../../.claude/skills/quick/SKILL.md"),
    ),
    (
        "skills/spec/SKILL.md",
        include_str!("../../.claude/skills/spec/SKILL.md"),
    ),
    (
        "skills/rfc-writer/SKILL.md",
        include_str!("../../.claude/skills/rfc-writer/SKILL.md"),
    ),
    (
        "skills/adr-writer/SKILL.md",
        include_str!("../../.claude/skills/adr-writer/SKILL.md"),
    ),
    (
        "skills/wi-writer/SKILL.md",
        include_str!("../../.claude/skills/wi-writer/SKILL.md"),
    ),
    (
        "skills/guard-writer/SKILL.md",
        include_str!("../../.claude/skills/guard-writer/SKILL.md"),
    ),
    (
        "skills/commit/SKILL.md",
        include_str!("../../.claude/skills/commit/SKILL.md"),
    ),
    (
        "skills/migrate/SKILL.md",
        include_str!("../../.claude/skills/migrate/SKILL.md"),
    ),
    (
        "skills/decision-analysis/SKILL.md",
        include_str!("../../.claude/skills/decision-analysis/SKILL.md"),
    ),
];

/// Agent templates: (relative_path, content) pairs.
/// Source of truth: .claude/agents/; embedded at compile time.
const AGENT_TEMPLATES: &[(&str, &str)] = &[
    (
        "agents/rfc-reviewer.md",
        include_str!("../../.claude/agents/rfc-reviewer.md"),
    ),
    (
        "agents/adr-reviewer.md",
        include_str!("../../.claude/agents/adr-reviewer.md"),
    ),
    (
        "agents/wi-reviewer.md",
        include_str!("../../.claude/agents/wi-reviewer.md"),
    ),
    (
        "agents/compliance-checker.md",
        include_str!("../../.claude/agents/compliance-checker.md"),
    ),
];

/// Initialize govctl project
pub fn init_project(config: &Config, force: bool, op: WriteOp) -> anyhow::Result<Vec<Diagnostic>> {
    let config_path = config.gov_root.join("config.toml");

    if config_path.exists() && !force && !op.is_preview() {
        return Err(Diagnostic::new(
            DiagnosticCode::E0501ConfigInvalid,
            format!(
                "{} already exists (use -f to overwrite)",
                config_path.display()
            ),
            config_path.display().to_string(),
        )
        .into());
    }

    let dirs: Vec<_> = vec![
        config.gov_root.clone(),
        config.rfc_dir(),
        config.schema_dir(),
        config.rfc_output(),
        config.adr_dir(),
        config.work_dir(),
        config.guard_dir(),
        config.templates_dir(),
    ];

    for dir in &dirs {
        create_dir_all(dir, op, Some(&config.display_path(dir)))?;
        if !op.is_preview() {
            ui::created_path(&config.display_path(dir));
        }
    }

    // Write config after gov_root exists
    write_file(
        &config_path,
        &Config::default_toml(schema_version_for_init()),
        op,
        Some(&config.display_path(&config_path)),
    )?;
    if !op.is_preview() {
        ui::created_path(&config.display_path(&config_path));
    }

    // Install bundled artifact JSON Schemas under gov/schema/.
    let schema_dir = config.schema_dir();
    for template in ARTIFACT_SCHEMA_TEMPLATES {
        let path = schema_dir.join(template.filename);
        let display_path = config.display_path(&path);
        write_file(&path, template.content, op, Some(&display_path))?;
        if !op.is_preview() {
            ui::created_path(&display_path);
        }
    }

    // Ensure .gitignore contains .govctl.lock
    ensure_gitignore_lock_entry(op)?;

    if !op.is_preview() {
        ui::success("Project initialized");
        ui::hint(
            "To install agent skills locally: govctl init-skills\n  \
             Or install the govctl plugin:    /plugin install govctl@govctl",
        );
    }
    Ok(vec![])
}

// Codex agent templates generated by build.rs from .claude/agents/*.md
include!(concat!(env!("OUT_DIR"), "/agent_codex_templates.rs"));

/// Install agent skills and agents into the project's agent directory. [[ADR-0035]]
pub fn sync_skills(
    config: &Config,
    force: bool,
    format: &crate::SkillFormat,
    dir_override: Option<&std::path::Path>,
    op: WriteOp,
) -> anyhow::Result<Vec<Diagnostic>> {
    // Resolution: --dir flag > config agent_dir > format-implied default
    let format_default = match format {
        crate::SkillFormat::Claude => std::path::Path::new(".claude"),
        crate::SkillFormat::Codex => std::path::Path::new(".codex"),
    };
    let agent_dir_owned;
    let agent_dir = if let Some(dir) = dir_override {
        if dir.is_relative() {
            agent_dir_owned = config
                .gov_root
                .parent()
                .unwrap_or(std::path::Path::new("."))
                .join(dir);
            &agent_dir_owned
        } else {
            agent_dir_owned = dir.to_path_buf();
            &agent_dir_owned
        }
    } else if config.paths.agent_dir != crate::config::default_agent_dir() {
        // Config has an explicit agent_dir
        &config.paths.agent_dir
    } else {
        // Use format-implied default
        agent_dir_owned = config
            .gov_root
            .parent()
            .unwrap_or(std::path::Path::new("."))
            .join(format_default);
        &agent_dir_owned
    };

    let agent_templates: &[(&str, &str)] = match format {
        crate::SkillFormat::Claude => AGENT_TEMPLATES,
        crate::SkillFormat::Codex => AGENT_TEMPLATES_CODEX,
    };

    let mut synced = 0;
    let mut skipped = 0;

    for (rel_path, template) in SKILL_TEMPLATES.iter().chain(agent_templates.iter()) {
        let path = agent_dir.join(rel_path);
        let display_path = config.display_path(&path);

        // Create parent directory if needed
        if let Some(parent) = path.parent() {
            let display_parent = config.display_path(parent);
            create_dir_all(parent, op, Some(&display_parent))?;
        }

        // Check if file exists and skip if not forcing
        if path.exists() && !force && !op.is_preview() {
            skipped += 1;
            if !op.is_preview() {
                ui::sub_info(format!(
                    "Skipped {} (already exists, use -f to overwrite)",
                    path.display()
                ));
            }
            continue;
        }

        // Write template
        write_file(&path, template, op, Some(&display_path))?;

        if !op.is_preview() {
            if path.exists() && force {
                ui::info(format!("Updated {}", display_path.display()));
            } else {
                ui::created_path(&display_path);
            }
        }
        synced += 1;
    }

    if !op.is_preview() {
        if synced > 0 {
            ui::success(format!("Synced {} asset(s)", synced));
        }
        if skipped > 0 {
            ui::info(format!(
                "{} asset(s) skipped (use -f to overwrite)",
                skipped
            ));
        }
        if synced == 0 && skipped == 0 {
            ui::info("No assets to sync");
        }
    }

    Ok(vec![])
}

/// Create a new artifact
pub fn create(config: &Config, target: &NewTarget, op: WriteOp) -> anyhow::Result<Vec<Diagnostic>> {
    match target {
        NewTarget::Rfc { title, id } => create_rfc(config, title, id.as_deref(), op),
        NewTarget::Clause {
            clause_id,
            title,
            section,
            kind,
        } => create_clause(config, clause_id, title, section, *kind, op),
        NewTarget::Adr { title } => create_adr(config, title, op),
        NewTarget::Work { title, active } => create_work_item(config, title, *active, op),
    }
}

/// Create a new RFC
fn create_rfc(
    config: &Config,
    title: &str,
    manual_id: Option<&str>,
    op: WriteOp,
) -> anyhow::Result<Vec<Diagnostic>> {
    let rfcs_dir = config.rfc_dir();

    // Determine RFC ID: use manual if provided, otherwise auto-generate
    let rfc_id = match manual_id {
        Some(id) => {
            // Validate format
            if !id.starts_with("RFC-") {
                return Err(Diagnostic::new(
                    DiagnosticCode::E0110RfcInvalidId,
                    format!("RFC ID must start with 'RFC-' (got: {id})"),
                    id,
                )
                .into());
            }
            // Check for collision (skip in preview mode)
            if !op.is_preview() && rfcs_dir.join(id).exists() {
                return Err(Diagnostic::new(
                    DiagnosticCode::E0109RfcAlreadyExists,
                    format!("RFC already exists: {id}"),
                    id,
                )
                .into());
            }
            id.to_string()
        }
        None => {
            // Auto-generate: find max RFC number and increment
            let max_num = std::fs::read_dir(&rfcs_dir)
                .into_iter()
                .flatten()
                .flatten()
                .filter_map(|entry| {
                    let name = entry.file_name();
                    let name_str = name.to_string_lossy();
                    name_str
                        .strip_prefix("RFC-")
                        .and_then(|s| s.parse::<u32>().ok())
                })
                .max()
                .unwrap_or(0);

            format!("RFC-{:04}", max_num + 1)
        }
    };

    let rfc_dir = rfcs_dir.join(&rfc_id);
    let clauses_dir = rfc_dir.join("clauses");

    // Final collision check (skip in preview mode)
    if !op.is_preview() && rfc_dir.exists() {
        return Err(Diagnostic::new(
            DiagnosticCode::E0109RfcAlreadyExists,
            format!("RFC already exists: {}", rfc_dir.display()),
            rfc_dir.display().to_string(),
        )
        .into());
    }

    // Create directories
    let display_clauses_dir = config.display_path(&clauses_dir);
    create_dir_all(&clauses_dir, op, Some(&display_clauses_dir))?;

    // Create rfc.toml
    let rfc = RfcSpec {
        rfc_id: rfc_id.to_string(),
        title: title.to_string(),
        version: "0.1.0".to_string(),
        status: RfcStatus::Draft,
        phase: RfcPhase::Spec,
        owners: vec![config.project.default_owner.clone()],
        created: today(),
        updated: None,
        supersedes: None,
        refs: vec![],
        tags: vec![],
        sections: vec![
            SectionSpec {
                title: "Summary".to_string(),
                clauses: vec![],
            },
            SectionSpec {
                title: "Specification".to_string(),
                clauses: vec![],
            },
        ],
        changelog: vec![ChangelogEntry {
            version: "0.1.0".to_string(),
            date: today(),
            notes: Some("Initial draft".to_string()),
            added: vec![],
            changed: vec![],
            deprecated: vec![],
            removed: vec![],
            fixed: vec![],
            security: vec![],
        }],
        signature: None, // Will be set on first bump per [[ADR-0016]]
    };

    let rfc_toml = rfc_dir.join("rfc.toml");
    let wire: RfcWire = rfc.into();
    let body = toml::to_string_pretty(&wire)?;
    let content = with_schema_header(ArtifactSchema::Rfc, &body);
    let display_rfc_toml = config.display_path(&rfc_toml);
    write_file(&rfc_toml, &content, op, Some(&display_rfc_toml))?;

    if !op.is_preview() {
        ui::created("RFC", &config.display_path(&rfc_toml));
        ui::sub_info(format!(
            "Clauses dir: {}",
            config.display_path(&clauses_dir).display()
        ));
    }

    Ok(vec![])
}

/// Create a new clause
fn create_clause(
    config: &Config,
    clause_id: &str,
    title: &str,
    section: &str,
    kind: ClauseKind,
    op: WriteOp,
) -> anyhow::Result<Vec<Diagnostic>> {
    // Parse clause_id (RFC-0001:C-NAME)
    let parts: Vec<&str> = clause_id.split(':').collect();
    if parts.len() != 2 {
        return Err(Diagnostic::new(
            DiagnosticCode::E0210ClauseInvalidIdFormat,
            "Invalid clause ID format. Expected RFC-NNNN:C-NAME",
            clause_id,
        )
        .into());
    }

    let rfc_id = parts[0];
    let clause_name = parts[1];

    let rfc_path = config.rfc_dir().join(rfc_id).join("rfc.toml");
    if !rfc_path.exists() {
        return Err(Diagnostic::new(
            DiagnosticCode::E0102RfcNotFound,
            format!("RFC not found: {rfc_id}"),
            rfc_id,
        )
        .into());
    }

    let mut rfc = crate::write::read_rfc(config, &rfc_path)?;

    // Create clause
    let clause = ClauseSpec {
        clause_id: clause_name.to_string(),
        title: title.to_string(),
        kind,
        status: ClauseStatus::Active,
        text: "TODO: Add clause text here.".to_string(),
        anchors: vec![],
        superseded_by: None,
        since: None, // Will be set by rfc bump
        tags: vec![],
    };

    let clause_path = config
        .rfc_dir()
        .join(rfc_id)
        .join("clauses")
        .join(format!("{clause_name}.toml"));

    let wire: ClauseWire = clause.into();
    let body = toml::to_string_pretty(&wire)?;
    let content = with_schema_header(ArtifactSchema::Clause, &body);
    let display_clause_path = config.display_path(&clause_path);
    write_file(&clause_path, &content, op, Some(&display_clause_path))?;

    // Update RFC to include clause in section
    let clause_rel_path = format!("clauses/{clause_name}.toml");

    // Find or create section
    if let Some(sec) = rfc.sections.iter_mut().find(|s| s.title == section) {
        if !sec.clauses.contains(&clause_rel_path) {
            sec.clauses.push(clause_rel_path.clone());
        }
    } else {
        rfc.sections.push(SectionSpec {
            title: section.to_string(),
            clauses: vec![clause_rel_path.clone()],
        });
    }

    let wire: RfcWire = rfc.into();
    let body = toml::to_string_pretty(&wire)?;
    let rfc_content = with_schema_header(ArtifactSchema::Rfc, &body);
    let display_rfc_path = config.display_path(&rfc_path);
    write_file(&rfc_path, &rfc_content, op, Some(&display_rfc_path))?;

    if !op.is_preview() {
        ui::created("clause", &config.display_path(&clause_path));
        ui::sub_info(format!(
            "Added to section '{}', path: {}",
            section, clause_rel_path
        ));
    }

    Ok(vec![])
}

/// Create a new ADR
fn create_adr(config: &Config, title: &str, op: WriteOp) -> anyhow::Result<Vec<Diagnostic>> {
    // Find next ADR number
    let adr_dir = config.adr_dir();
    let display_adr_dir = config.display_path(&adr_dir);
    create_dir_all(&adr_dir, op, Some(&display_adr_dir))?;

    let mut max_num = 0u32;
    if let Ok(entries) = std::fs::read_dir(&adr_dir) {
        for entry in entries.flatten() {
            let name = entry.file_name();
            let name_str = name.to_string_lossy();
            if name_str.starts_with("ADR-")
                && let Some(num_str) = name_str
                    .strip_prefix("ADR-")
                    .and_then(|s| s.split('-').next())
                && let Ok(num) = num_str.parse::<u32>()
            {
                max_num = max_num.max(num);
            }
        }
    }

    let next_num = max_num + 1;
    let adr_id = format!("ADR-{next_num:04}");
    let slug = slugify(title);
    let filename = format!("{adr_id}-{slug}.toml");
    let adr_path = adr_dir.join(&filename);

    // Create ADR spec
    let spec = AdrSpec {
        govctl: AdrMeta {
            schema: 1,
            id: adr_id.clone(),
            title: title.to_string(),
            status: AdrStatus::Proposed,
            date: today(),
            superseded_by: None,
            refs: vec![],
            tags: vec![],
        },
        content: AdrContent {
            context: "Describe the context and problem statement.\nWhat is the issue that we're seeing that is motivating this decision?".to_string(),
            decision: "Describe the decision that was made.\nWhat is the change that we're proposing and/or doing?".to_string(),
            consequences: "Describe the resulting context after applying the decision.\nWhat becomes easier or more difficult to do because of this change?".to_string(),
            alternatives: vec![],
        },
    };

    let body = toml::to_string_pretty(&spec)?;
    let content = with_schema_header(ArtifactSchema::Adr, &body);
    let display_adr_path = config.display_path(&adr_path);
    write_file(&adr_path, &content, op, Some(&display_adr_path))?;

    if !op.is_preview() {
        ui::created("ADR", &config.display_path(&adr_path));
    }

    Ok(vec![])
}

/// Create a new work item
fn create_work_item(
    config: &Config,
    title: &str,
    active: bool,
    op: WriteOp,
) -> anyhow::Result<Vec<Diagnostic>> {
    let work_dir = config.work_dir();
    let display_work_dir = config.display_path(&work_dir);
    create_dir_all(&work_dir, op, Some(&display_work_dir))?;

    let date = today();
    let slug = slugify(title);

    // Generate work item ID based on configured strategy [[ADR-0020]]
    let work_id = match config.work_item.id_strategy {
        IdStrategy::Sequential => {
            // Original behavior: WI-YYYY-MM-DD-NNN
            let id_prefix = format!("WI-{date}-");
            let max_seq = find_max_sequence(&work_dir, &id_prefix);
            format!("WI-{date}-{:03}", max_seq + 1)
        }
        IdStrategy::AuthorHash => {
            // Author-namespaced: WI-YYYY-MM-DD-{hash4}-NNN
            let author_hash = IdStrategy::get_author_hash().unwrap_or_else(|| {
                // Fallback to random if git email not configured
                IdStrategy::generate_random_suffix()
            });
            let id_prefix = format!("WI-{date}-{author_hash}-");
            let max_seq = find_max_sequence(&work_dir, &id_prefix);
            format!("WI-{date}-{author_hash}-{:03}", max_seq + 1)
        }
        IdStrategy::Random => {
            // Random suffix: WI-YYYY-MM-DD-{rand4}
            let random_suffix = IdStrategy::generate_random_suffix();
            format!("WI-{date}-{random_suffix}")
        }
    };

    // Find unique filename (loop until no collision)
    let mut filename = format!("{date}-{slug}.toml");
    let mut work_path = work_dir.join(&filename);
    let mut suffix = 1u32;

    while !op.is_preview() && work_path.exists() {
        filename = format!("{date}-{slug}-{suffix:03}.toml");
        work_path = work_dir.join(&filename);
        suffix += 1;
    }

    // Create work item spec
    let (status, started) = if active {
        (WorkItemStatus::Active, Some(date.clone()))
    } else {
        (WorkItemStatus::Queue, None)
    };

    let spec = WorkItemSpec {
        govctl: WorkItemMeta {
            schema: 1,
            id: work_id.clone(),
            title: title.to_string(),
            status,
            created: Some(date.clone()),
            started,
            completed: None,
            refs: vec![],
            tags: vec![],
        },
        content: WorkItemContent {
            description:
                "Describe the work to be done.\nWhat is the goal? What are the acceptance criteria?"
                    .to_string(),
            journal: vec![],
            acceptance_criteria: vec![],
            notes: vec![],
        },
        verification: WorkItemVerification::default(),
    };

    let body = toml::to_string_pretty(&spec)?;
    let content = with_schema_header(ArtifactSchema::WorkItem, &body);
    let display_work_path = config.display_path(&work_path);
    write_file(&work_path, &content, op, Some(&display_work_path))?;

    if !op.is_preview() {
        let display_path = config.display_path(&work_path);
        ui::created("work item", &display_path);
        ui::sub_info(format!("ID: {work_id}"));
    }

    Ok(vec![])
}

/// Find the maximum sequence number for work items with a given ID prefix.
///
/// Scans all TOML files in `work_dir`, parses their `id` field, and returns
/// the highest sequence number found for IDs starting with `id_prefix`.
fn find_max_sequence(work_dir: &std::path::Path, id_prefix: &str) -> u32 {
    std::fs::read_dir(work_dir)
        .into_iter()
        .flatten()
        .flatten()
        .filter_map(|entry| {
            let path = entry.path();
            (path.extension()? == "toml").then_some(path)
        })
        .filter_map(|path| std::fs::read_to_string(&path).ok())
        .filter_map(|content| {
            content
                .lines()
                .find(|line| line.starts_with("id = \""))
                .and_then(|line| line.strip_prefix("id = \""))
                .and_then(|s| s.strip_suffix('"'))
                .and_then(|id| id.strip_prefix(id_prefix))
                .and_then(|seq_str| seq_str.parse::<u32>().ok())
        })
        .max()
        .unwrap_or(0)
}

/// Ensure .gitignore contains .govctl.lock entry
fn ensure_gitignore_lock_entry(op: WriteOp) -> anyhow::Result<()> {
    const LOCK_ENTRY: &str = ".govctl.lock";
    let gitignore_path = PathBuf::from(".gitignore");

    if gitignore_path.exists() {
        let content = std::fs::read_to_string(&gitignore_path)?;
        // Check if already present
        if content.lines().any(|line| line.trim() == LOCK_ENTRY) {
            return Ok(());
        }
        // Append to existing .gitignore
        let new_content = if content.ends_with('\n') {
            format!("{}{}\n", content, LOCK_ENTRY)
        } else {
            format!("{}\n{}\n", content, LOCK_ENTRY)
        };
        write_file(&gitignore_path, &new_content, op, None)?;
        if !op.is_preview() {
            ui::info(format!("Added '{}' to .gitignore", LOCK_ENTRY));
        }
    } else {
        // Create new .gitignore
        let content = format!("# govctl lock file\n{}\n", LOCK_ENTRY);
        write_file(&gitignore_path, &content, op, None)?;
        if !op.is_preview() {
            ui::created_path(&gitignore_path);
        }
    }
    Ok(())
}