bock 1.0.0

The Bock language compiler and CLI — feature-declarative, target-agnostic programming language with AI-native transpilation
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
use std::path::PathBuf;
use std::process::ExitCode;

use clap::{Parser, Subcommand};

mod build;
mod cache_cmd;
mod check;
mod decision_io;
mod doc;
mod fmt;
mod inspect;
mod new;
#[path = "override.rs"]
mod override_cmd;
mod pin;
mod pkg;
mod promote;
mod repl;
mod run;
mod stdlib;
mod test;

/// The Bock programming language compiler and toolchain.
#[derive(Parser)]
#[command(name = "bock", version, about, long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

/// All top-level subcommands.
#[derive(Subcommand)]
enum Command {
    /// Scaffold a new Bock project.
    New {
        /// Project name.
        name: String,
    },
    /// Transpile and compile a Bock project.
    Build {
        /// Target language (e.g., js, ts, python, rust, go).
        #[arg(short, long)]
        target: Option<String>,

        /// Build for all configured targets.
        #[arg(long)]
        all_targets: bool,

        /// Enable release optimizations.
        #[arg(long)]
        release: bool,

        /// Emit generated code without invoking the target compiler.
        #[arg(long)]
        source_only: bool,

        /// Use only rule-based codegen (skip AI-assisted generation).
        /// Alias: `--no-ai`.
        #[arg(long, alias = "no-ai")]
        deterministic: bool,

        /// Force production strictness for this build regardless of
        /// the project's configured default. Fails if any build-scope
        /// decision in the manifest is unpinned.
        #[arg(long)]
        strict: bool,

        /// After a successful build, pin every build-scope decision in
        /// `.bock/decisions/build/`. Intended workflow: run in
        /// development with `--pin-all`, commit the pins, then ship
        /// with production strictness.
        #[arg(long)]
        pin_all: bool,

        /// Emit source map files alongside generated code (default: on).
        #[arg(long, default_value_t = true, overrides_with = "no_source_map")]
        source_map: bool,

        /// Suppress source map file output.
        #[arg(long)]
        no_source_map: bool,
    },
    /// Execute a Bock program (interpreter by default).
    Run {
        /// Path to the entry file.
        file: Option<String>,

        /// Re-run on file changes (not yet implemented).
        #[arg(long)]
        watch: bool,

        /// Arguments passed to the Bock program (after `--`).
        #[arg(last = true)]
        program_args: Vec<String>,
    },
    /// Type-check and lint without building.
    Check {
        /// Paths to .bock files to check. If omitted, checks all .bock files in the current directory.
        files: Vec<PathBuf>,

        /// Restrict the check to specific aspects. Valid aspects: types, context.
        /// Accepts a comma-separated list (--only=types,context) and may be
        /// repeated (--only=types --only=context); omitting it runs the full check.
        #[arg(long, value_name = "ASPECT")]
        only: Vec<String>,

        /// Brief output: compact, one-line diagnostics with no source-context snippets.
        #[arg(long)]
        brief: bool,

        /// Force production strictness for the check. Without this flag the
        /// check runs at development strictness (completeness gaps are
        /// warnings); with it, completeness gaps become errors and a
        /// non-zero exit. Mirrors `bock build --strict`.
        #[arg(long)]
        strict: bool,
    },
    /// Run tests.
    Test {
        /// Optional test filter pattern.
        #[arg(long)]
        filter: Option<String>,

        /// Paths to .bock files to test. If omitted, discovers all .bock files recursively.
        files: Vec<PathBuf>,
    },
    /// Format Bock source files.
    Fmt {
        /// Check formatting without modifying files.
        #[arg(long)]
        check: bool,
    },
    /// Start an interactive REPL session.
    Repl,
    /// Browse AI decisions, rule cache, and AI response cache.
    ///
    /// With no subcommand, lists build-scope decisions. Use `--runtime`
    /// for runtime decisions, `--all` for a unified view.
    Inspect {
        #[command(subcommand)]
        command: Option<InspectCommand>,

        /// Show runtime-scope decisions only (alias for `inspect decisions --runtime`).
        #[arg(long, conflicts_with = "all")]
        runtime: bool,

        /// Show both build and runtime decisions with prefixed ids.
        #[arg(long)]
        all: bool,

        /// Only list decisions that are not yet pinned.
        #[arg(long)]
        unpinned: bool,

        /// Filter by module path substring.
        #[arg(long)]
        module: Option<String>,

        /// Filter by decision type (e.g. `codegen`, `repair`, `adaptive_recovery`).
        #[arg(long = "type")]
        type_filter: Option<String>,

        /// Emit machine-readable JSON instead of the human table.
        #[arg(long)]
        json: bool,
    },
    /// Pin AI decisions in the manifest so they replay deterministically.
    Pin {
        /// The decision identifier (prefixed `build:id` / `runtime:id`
        /// or bare). Omitted when using a bulk flag.
        decision: Option<String>,

        /// Pin every unpinned decision whose module path contains the
        /// given substring.
        #[arg(long = "all-in", conflicts_with_all = ["all_build", "all_runtime"])]
        all_in: Option<String>,

        /// Pin every unpinned build-scope decision.
        #[arg(long = "all-build", conflicts_with_all = ["all_in", "all_runtime"])]
        all_build: bool,

        /// Pin every unpinned runtime-scope decision.
        #[arg(long = "all-runtime", conflicts_with_all = ["all_in", "all_build"])]
        all_runtime: bool,

        /// Free-form reason recorded on pin metadata.
        #[arg(long)]
        reason: Option<String>,
    },
    /// Clear pin metadata from a single decision.
    Unpin {
        /// The decision identifier (prefixed or bare).
        decision: String,
    },
    /// Override or promote an AI decision in the manifest.
    ///
    /// With a bare id: pin the named decision in place.
    /// With a second positional argument or `--from-file`: replace the
    /// decision's `choice` string and auto-pin.
    /// With `--promote`: copy a pinned runtime decision into the build
    /// manifest so subsequent production builds replay it
    /// deterministically.
    Override {
        /// The decision identifier to operate on.
        decision: Option<String>,

        /// Replace the decision's `choice` with this inline value.
        new_choice: Option<String>,

        /// Read the replacement choice from a file instead of the
        /// positional argument.
        #[arg(long = "from-file", conflicts_with = "new_choice")]
        from_file: Option<PathBuf>,

        /// Treat `decision` as a runtime-scope id. Required when
        /// pinning a runtime decision without promoting it.
        #[arg(long)]
        runtime: bool,

        /// Promote a pinned runtime decision to the build manifest
        /// (§10.8 promotion path). The runtime entry is marked
        /// superseded for audit but kept on disk.
        #[arg(long)]
        promote: bool,

        /// Free-form reason to record alongside the pin (e.g. a code
        /// review ticket or reviewer handle).
        #[arg(long)]
        reason: Option<String>,
    },
    /// Manage on-disk AI, decision, and rule caches.
    Cache {
        #[command(subcommand)]
        command: CacheCliCommand,
    },
    /// Analyze a project at the next strictness level (sketch → development
    /// → production). Reports issues that would fail at the next level; with
    /// `--apply`, automatically fixes simple cases and bumps the project's
    /// configured strictness default.
    Promote {
        /// Automatically apply safe fixes and update `bock.project` after a
        /// clean check. Without this flag, `bock promote` only reports.
        #[arg(long)]
        apply: bool,

        /// Report issues without modifying anything (the default).
        #[arg(long)]
        check: bool,
    },
    /// Package manager commands.
    Pkg {
        #[command(subcommand)]
        command: Option<PkgCommand>,
    },
    /// Query or interact with AI models.
    Model {
        #[command(subcommand)]
        command: Option<ModelCommand>,
    },
    /// Generate documentation.
    Doc {
        /// Path to document (file or directory, defaults to cwd).
        path: Option<String>,

        /// Output directory for generated docs (defaults to `<path>/docs`).
        #[arg(long)]
        output: Option<String>,

        /// Output format: `markdown` (default) or `html`.
        #[arg(long, default_value = "markdown")]
        format: String,
    },
    /// Start the Bock language server over stdio.
    Lsp {
        /// Use stdio transport (default; provided for LSP convention).
        ///
        /// `--stdio` is the universal LSP convention. We accept it for
        /// compatibility with LSP clients (VS Code, neovim, emacs
        /// lsp-mode, etc.) but stdio is already the default and only
        /// transport at v1.
        #[arg(long)]
        stdio: bool,
    },
}

/// `bock inspect` subcommands.
#[derive(Subcommand)]
enum InspectCommand {
    /// List decisions matching the given filters (default subcommand).
    Decisions {
        /// Show runtime-scope decisions only.
        #[arg(long, conflicts_with = "all")]
        runtime: bool,

        /// Show both build and runtime decisions with prefixed ids.
        #[arg(long)]
        all: bool,

        /// Only list decisions that are not yet pinned.
        #[arg(long)]
        unpinned: bool,

        /// Filter by module path substring.
        #[arg(long)]
        module: Option<String>,

        /// Filter by decision type (e.g. `codegen`, `adaptive_recovery`).
        #[arg(long = "type")]
        type_filter: Option<String>,

        /// Emit machine-readable JSON instead of the human table.
        #[arg(long)]
        json: bool,
    },
    /// Show one decision in detail. Accepts prefixed or bare ids.
    Decision {
        /// The decision identifier.
        id: String,

        /// Emit JSON instead of the human view.
        #[arg(long)]
        json: bool,
    },
    /// Summarise the on-disk AI response cache.
    Cache {
        /// Always show the on-disk size even when there are no entries.
        #[arg(long)]
        size: bool,
    },
    /// List learned codegen rules.
    Rules {
        /// Show rules for a single target only.
        #[arg(long)]
        target: Option<String>,
    },
    /// Dump the lowered AIR tree for a single source file.
    ///
    /// Runs the compiler frontend (lex, parse, name resolution, AIR
    /// lowering) on one file and prints the resulting tree — indented and
    /// human-readable by default, or as a stable machine-readable JSON tree
    /// with `--json` (the shape editor tooling consumes). Exits non-zero on
    /// any frontend error; in `--json` mode errors emit a JSON error object.
    Air {
        /// The `.bock` file to lower.
        file: String,

        /// Emit the machine-readable JSON tree instead of the human view.
        #[arg(long)]
        json: bool,
    },
}

/// `bock cache` subcommands.
#[derive(Subcommand)]
enum CacheCliCommand {
    /// Print summary statistics for the AI cache, decision manifests,
    /// and rule cache.
    Stats,
    /// Clear a cache. By default wipes the AI response cache.
    Clear {
        /// Clear decision manifests instead of the AI cache.
        #[arg(long)]
        decisions: bool,

        /// Combined with `--decisions`, restrict the clear to the
        /// runtime manifest.
        #[arg(long)]
        runtime: bool,

        /// Combined with `--decisions`, restrict the clear to the
        /// build manifest.
        #[arg(long)]
        build: bool,

        /// Clear the local rule cache instead of the AI cache.
        #[arg(long, conflicts_with_all = ["decisions", "runtime", "build"])]
        rules: bool,
    },
}

/// Package manager subcommands.
#[derive(Subcommand)]
enum PkgCommand {
    /// Initialize a new package.
    Init,
    /// Add a dependency (downloads from the registry and updates the lockfile).
    Add {
        /// Package name.
        name: String,
        /// Version requirement (e.g. "^1.0"). Defaults to latest.
        #[arg(short, long)]
        version: Option<String>,
        /// Do not hit the network; only use cached tarballs.
        #[arg(long)]
        offline: bool,
        /// Registry URL to fetch from (overrides bock.project default).
        #[arg(long)]
        registry: Option<String>,
    },
    /// Remove a dependency.
    Remove {
        /// Package name.
        name: String,
    },
    /// Show the dependency tree.
    Tree,
    /// List dependencies.
    List,
    /// Manage the on-disk tarball cache.
    Cache {
        #[command(subcommand)]
        command: PkgCacheCommand,
    },
}

/// `bock pkg cache` subcommands.
#[derive(Subcommand)]
enum PkgCacheCommand {
    /// Remove every tarball from `.bock/cache/`.
    Clear,
}

/// Model subcommands.
#[derive(Subcommand)]
enum ModelCommand {
    /// Show current model configuration.
    Show,
    /// Set model configuration.
    Set {
        /// Configuration key.
        key: String,
        /// Configuration value.
        value: String,
    },
}

#[tokio::main]
async fn main() -> anyhow::Result<ExitCode> {
    let cli = Cli::parse();

    // The process exit code is decided in exactly one place: this binding,
    // seeded to success and overridden only by commands (currently `check`)
    // whose pass/fail outcome must map to a non-zero exit. Subcommand handlers
    // return their outcome rather than calling `process::exit`, keeping the
    // exit contract centralized and the handlers testable.
    let mut exit_code = ExitCode::SUCCESS;

    match cli.command {
        Command::New { name } => new::run(&name)?,
        Command::Build {
            target,
            all_targets,
            release,
            source_only,
            deterministic,
            strict,
            pin_all,
            source_map,
            no_source_map,
        } => {
            let targets = build::parse_targets(target, all_targets)?;
            let options = build::BuildOptions {
                targets,
                release,
                source_only,
                deterministic,
                strict,
                pin_all,
                source_map: source_map && !no_source_map,
            };
            build::run(&options)?;
        }
        Command::Run {
            file,
            watch,
            program_args,
        } => {
            if watch {
                println!("bock run --watch: not yet implemented");
            } else {
                run::run(file, program_args).await?;
            }
        }
        Command::Check {
            files,
            only,
            brief,
            strict,
        } => match check::AspectSelection::from_raw(&only) {
            Ok(aspects) => {
                let options = check::CheckOptions {
                    aspects,
                    brief,
                    strict,
                };
                if !check::run(files, &options)?.is_clean() {
                    exit_code = ExitCode::FAILURE;
                }
            }
            Err(unknown) => {
                eprintln!(
                    "error: unknown check aspect '{unknown}'. Valid aspects: {}",
                    check::Aspect::valid_list()
                );
                exit_code = ExitCode::FAILURE;
            }
        },
        Command::Test { filter, files } => test::run(filter, files).await?,
        Command::Fmt { check } => fmt::run(check)?,
        Command::Repl => repl::run().await?,
        Command::Inspect {
            command,
            runtime,
            all,
            unpinned,
            module,
            type_filter,
            json,
        } => {
            let cmd = command.unwrap_or(InspectCommand::Decisions {
                runtime,
                all,
                unpinned,
                module,
                type_filter,
                json,
            });
            // `inspect air` carries an exit-code contract (0 = lowered
            // cleanly, 1 = frontend error), mirroring `bock check`; the
            // other inspect subcommands always report `Clean`.
            if !run_inspect(cmd)?.is_clean() {
                exit_code = ExitCode::FAILURE;
            }
        }
        Command::Pin {
            decision,
            all_in,
            all_build,
            all_runtime,
            reason,
        } => {
            let options = pin::PinOptions {
                id: decision,
                all_in,
                all_build,
                all_runtime,
                reason,
            };
            pin::run_pin(&options)?;
        }
        Command::Unpin { decision } => pin::run_unpin(&decision)?,
        Command::Override {
            decision,
            new_choice,
            from_file,
            runtime,
            promote,
            reason,
        } => {
            let options = override_cmd::OverrideOptions {
                decision,
                new_choice,
                from_file,
                runtime,
                promote,
                reason,
            };
            override_cmd::run(&options)?;
        }
        Command::Cache { command } => match command {
            CacheCliCommand::Stats => cache_cmd::run_stats()?,
            CacheCliCommand::Clear {
                decisions,
                runtime,
                build,
                rules,
            } => {
                let opts = cache_cmd::ClearOptions {
                    decisions,
                    runtime,
                    build,
                    rules,
                };
                cache_cmd::run_clear(&opts)?;
            }
        },
        Command::Promote { apply, check } => {
            let options = promote::PromoteOptions {
                apply: apply && !check,
            };
            promote::run(&options)?;
        }
        Command::Pkg { command } => pkg::run(command)?,
        Command::Model { .. } => stub("model"),
        Command::Doc {
            path,
            output,
            format,
        } => doc::run(path, output, &format)?,
        Command::Lsp { stdio: _ } => bock_lsp::run_stdio().await,
    }

    Ok(exit_code)
}

fn stub(name: &str) {
    println!("bock {name}: not yet implemented");
}

/// Dispatch one `bock inspect` subcommand.
///
/// Returns the pass/fail outcome so `main` can map it to the process exit
/// code: `inspect air` reports `Failed` on frontend errors; every other
/// subcommand reports `Clean` (their genuine failures surface as `Err`).
fn run_inspect(cmd: InspectCommand) -> anyhow::Result<check::CheckOutcome> {
    match cmd {
        InspectCommand::Decisions {
            runtime,
            all,
            unpinned,
            module,
            type_filter,
            json,
        } => {
            let scope = if all {
                inspect::ScopeFilter::All
            } else if runtime {
                inspect::ScopeFilter::Runtime
            } else {
                inspect::ScopeFilter::Build
            };
            let options = inspect::InspectDecisionsOptions {
                scope,
                unpinned_only: unpinned,
                module_filter: module,
                type_filter,
                json,
            };
            inspect::run_decisions(&options).map(|()| check::CheckOutcome::Clean)
        }
        InspectCommand::Decision { id, json } => {
            inspect::run_decision(&id, json).map(|()| check::CheckOutcome::Clean)
        }
        InspectCommand::Cache { size } => {
            inspect::run_cache(size).map(|()| check::CheckOutcome::Clean)
        }
        InspectCommand::Rules { target } => {
            inspect::run_rules(target.as_deref()).map(|()| check::CheckOutcome::Clean)
        }
        InspectCommand::Air { file, json } => inspect::run_air(std::path::Path::new(&file), json),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use clap::Parser;

    #[test]
    fn lsp_accepts_stdio_flag() {
        let cli = Cli::try_parse_from(["bock", "lsp", "--stdio"])
            .expect("`bock lsp --stdio` should parse cleanly");
        assert!(matches!(cli.command, Command::Lsp { stdio: true }));
    }

    #[test]
    fn lsp_works_without_stdio_flag() {
        let cli = Cli::try_parse_from(["bock", "lsp"])
            .expect("`bock lsp` (no flag) should parse cleanly");
        assert!(matches!(cli.command, Command::Lsp { stdio: false }));
    }

    #[test]
    fn check_accepts_strict_flag() {
        let cli = Cli::try_parse_from(["bock", "check", "--strict", "main.bock"])
            .expect("`bock check --strict` should parse cleanly");
        assert!(matches!(cli.command, Command::Check { strict: true, .. }));
    }

    #[test]
    fn check_strict_defaults_false() {
        let cli = Cli::try_parse_from(["bock", "check", "main.bock"])
            .expect("`bock check` should parse cleanly");
        assert!(matches!(cli.command, Command::Check { strict: false, .. }));
    }
}