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
use std::fs;
use std::io::Write;
use std::process::Command;

use tempfile::TempDir;

fn bock_bin() -> Command {
    Command::new(env!("CARGO_BIN_EXE_bock"))
}

fn create_project(source: &str) -> TempDir {
    let dir = TempDir::new().unwrap();
    let file_path = dir.path().join("main.bock");
    let mut f = fs::File::create(&file_path).unwrap();
    f.write_all(source.as_bytes()).unwrap();
    f.flush().unwrap();
    dir
}

const SIMPLE_SOURCE: &str = "fn add(a: Int, b: Int) -> Int { a + b }\n";

#[test]
fn build_js_produces_output_files() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--target", "js", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected exit 0, got {}\nstdout: {stdout}\nstderr: {stderr}",
        output.status,
    );

    // Check that build/js directory was created with files
    let build_dir = dir.path().join("build/js");
    assert!(build_dir.exists(), "build/js directory should exist");

    // Should have at least one .js file
    let js_files: Vec<_> = fs::read_dir(&build_dir)
        .unwrap()
        .filter_map(|e| e.ok())
        .filter(|e| {
            e.path()
                .extension()
                .and_then(|ext| ext.to_str())
                .is_some_and(|ext| ext == "js")
        })
        .collect();
    assert!(!js_files.is_empty(), "should have generated .js files");
}

#[test]
fn build_ts_produces_output_files() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--target", "ts", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected exit 0, got {}\nstdout: {stdout}\nstderr: {stderr}",
        output.status,
    );

    let build_dir = dir.path().join("build/ts");
    assert!(build_dir.exists(), "build/ts directory should exist");
}

#[test]
fn build_python_produces_output_files() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--target", "python", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected exit 0, got {}\nstdout: {stdout}\nstderr: {stderr}",
        output.status,
    );

    let build_dir = dir.path().join("build/python");
    assert!(build_dir.exists(), "build/python directory should exist");
}

#[test]
fn build_rust_target() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--target", "rust", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected exit 0, got {}\nstdout: {stdout}\nstderr: {stderr}",
        output.status,
    );

    let build_dir = dir.path().join("build/rust");
    assert!(build_dir.exists(), "build/rust directory should exist");
}

#[test]
fn build_go_target() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--target", "go", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected exit 0, got {}\nstdout: {stdout}\nstderr: {stderr}",
        output.status,
    );

    let build_dir = dir.path().join("build/go");
    assert!(build_dir.exists(), "build/go directory should exist");
}

#[test]
fn build_all_targets() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--all-targets", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected exit 0, got {}\nstdout: {stdout}\nstderr: {stderr}",
        output.status,
    );

    // Should have directories for all targets
    for target in &["js", "ts", "python", "rust", "go"] {
        let build_dir = dir.path().join(format!("build/{target}"));
        assert!(build_dir.exists(), "build/{target} directory should exist");
    }
}

#[test]
fn build_release_uses_release_dir() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--target", "js", "--release", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected exit 0, got {}\nstdout: {stdout}\nstderr: {stderr}",
        output.status,
    );

    let build_dir = dir.path().join("build/release/js");
    assert!(
        build_dir.exists(),
        "build/release/js directory should exist for release builds"
    );
}

#[test]
fn build_unknown_target_fails() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--target", "java"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "expected non-zero exit for unknown target"
    );
}

#[test]
fn build_default_target_is_js() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected exit 0, got {}\nstdout: {stdout}\nstderr: {stderr}",
        output.status,
    );

    let build_dir = dir.path().join("build/js");
    assert!(build_dir.exists(), "default target should be js");
}

#[test]
fn build_syntax_error_fails() {
    let dir = create_project("fn { broken\n");
    let output = bock_bin()
        .args(["build", "--target", "js", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "expected non-zero exit for syntax errors"
    );
}

#[test]
fn build_no_bock_files_fails() {
    let dir = TempDir::new().unwrap();
    let output = bock_bin()
        .args(["build", "--target", "js"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "expected non-zero exit when no .bock files found"
    );
}

#[test]
fn build_shows_timing_output() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--target", "js", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("ms"),
        "build output should include timing info, got: {stdout}"
    );
}

#[test]
fn build_deterministic_flag_accepted() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args([
            "build",
            "--target",
            "js",
            "--deterministic",
            "--source-only",
        ])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected exit 0 with --deterministic, got {}\nstdout: {stdout}\nstderr: {stderr}",
        output.status,
    );
}

#[test]
fn build_multifile_dependency_ordering() {
    let dir = TempDir::new().unwrap();

    fs::write(
        dir.path().join("models.bock"),
        "module Models\n\npublic record User {\n    id: Int\n    name: String\n}\n",
    )
    .unwrap();
    fs::write(
        dir.path().join("main.bock"),
        "module Main\n\nuse Models.{User}\n\nfn greet() -> Int { 42 }\n",
    )
    .unwrap();

    let output = bock_bin()
        .args(["build", "--target", "js", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "expected exit 0 for multi-file build, got {}\nstdout: {stdout}\nstderr: {stderr}",
        output.status,
    );
    assert!(
        stdout.contains("2 source file"),
        "should report 2 source files, got: {stdout}",
    );
}

#[test]
fn build_circular_dependency_fails() {
    let dir = TempDir::new().unwrap();

    fs::write(
        dir.path().join("a.bock"),
        "module A\n\nuse B.{foo}\n\nfn bar() -> Int { 1 }\n",
    )
    .unwrap();
    fs::write(
        dir.path().join("b.bock"),
        "module B\n\nuse A.{bar}\n\nfn foo() -> Int { 2 }\n",
    )
    .unwrap();

    let output = bock_bin()
        .args(["build", "--target", "js", "--source-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "expected non-zero exit for circular dependency",
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("circular"),
        "stderr should mention circular dependency: {stderr}",
    );
}

// ── Strictness governance (§17.6) ───────────────────────────────────────────

fn seed_unpinned_build_manifest(dir: &std::path::Path) {
    let path = dir
        .join(".bock")
        .join("decisions")
        .join("build")
        .join("main.bock.json");
    fs::create_dir_all(path.parent().unwrap()).unwrap();
    let entry = r#"[{
        "id": "abc12345deadbeef",
        "module": "main.bock",
        "target": "js",
        "decision_type": "codegen",
        "choice": "generated JS",
        "alternatives": [],
        "reasoning": "JS async pattern",
        "model_id": "stub:stub",
        "confidence": 0.9,
        "pinned": false,
        "pin_reason": null,
        "timestamp": "2026-04-22T10:00:00Z"
    }]"#;
    fs::write(path, entry).unwrap();
}

#[test]
fn build_strict_with_unpinned_decisions_errors_out() {
    let dir = create_project(SIMPLE_SOURCE);
    seed_unpinned_build_manifest(dir.path());

    let output = bock_bin()
        .args(["build", "--target", "js", "--source-only", "--strict"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "expected non-zero exit when production build has unpinned decisions",
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("unpinned decision")
            && stderr.contains("production mode")
            && stderr.contains("bock override"),
        "stderr should name the governance failure: {stderr}",
    );
}

#[test]
fn build_pin_all_pins_every_build_decision() {
    let dir = create_project(SIMPLE_SOURCE);
    seed_unpinned_build_manifest(dir.path());

    let output = bock_bin()
        .args(["build", "--target", "js", "--source-only", "--pin-all"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "pin-all build should succeed in development mode: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("pin-all pinned 1 decision"),
        "stdout should report pinned count: {stdout}"
    );

    // Manifest entry is now pinned.
    let manifest_path = dir.path().join(".bock/decisions/build/main.bock.json");
    let content = fs::read_to_string(&manifest_path).unwrap();
    assert!(content.contains("\"pinned\": true"));
    assert!(content.contains("pin_reason"));
    assert!(content.contains("bulk-pinned"));
}

#[test]
fn development_then_strict_workflow_succeeds_after_pin_all() {
    // 1. Seed unpinned state, then pin-all in development.
    let dir = create_project(SIMPLE_SOURCE);
    seed_unpinned_build_manifest(dir.path());

    let pin = bock_bin()
        .args(["build", "--target", "js", "--source-only", "--pin-all"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(pin.status.success());

    // 2. Now a strict (production) build passes the governance gate.
    let strict = bock_bin()
        .args(["build", "--target", "js", "--source-only", "--strict"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(
        strict.status.success(),
        "strict build should succeed after pin-all: {}",
        String::from_utf8_lossy(&strict.stderr)
    );
}

#[test]
fn build_no_ai_flag_is_alias_for_deterministic() {
    let dir = create_project(SIMPLE_SOURCE);
    let output = bock_bin()
        .args(["build", "--target", "js", "--source-only", "--no-ai"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "--no-ai should be accepted as alias: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

/// A runnable entry program so every target emits a real per-module tree (rust
/// needs a `main` for `src/main.rs`; go needs a `func main`). Uses `println`
/// (→ `console.log` for js/ts) rather than `print` (→ `process.stdout.write`,
/// which `tsc --noEmit` rejects without `@types/node`), so the project-mode
/// build's per-file ts validation passes — matching what the conformance
/// fixtures use.
const MAIN_SOURCE: &str = "fn main() { println(\"hi\") }\n";

/// The project-mode run-affordance manifest each target's scaffolder emits, or
/// `None` for targets that need no manifest to run (python — PEP 420).
fn target_manifest(target: &str) -> Option<&'static str> {
    match target {
        "js" | "ts" => Some("package.json"),
        "rust" => Some("Cargo.toml"),
        "go" => Some("go.mod"),
        "python" => None,
        other => panic!("unknown target {other}"),
    }
}

/// DV18 (spec §20.6.2): **source mode** (`--source-only`) emits "no manifests,
/// scaffolding, or entry-point wiring." Before S6a, codegen emitted the
/// run-affordance manifests in *all* modes, so `--source-only` wrongly carried
/// them. S6a moves manifest emission into the project-mode scaffolder, so source
/// mode is now bare per-module source. This test makes the fix checkable: for
/// every target, `bock build --source-only` must NOT emit the manifest.
#[test]
fn source_mode_is_bare_no_manifest() {
    for target in &["js", "ts", "python", "rust", "go"] {
        let dir = create_project(MAIN_SOURCE);
        let output = bock_bin()
            .args(["build", "--target", target, "--source-only"])
            .current_dir(dir.path())
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "source-only build failed for {target}: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        let build_dir = dir.path().join(format!("build/{target}"));
        if let Some(manifest) = target_manifest(target) {
            assert!(
                !build_dir.join(manifest).exists(),
                "DV18: source mode (--source-only) for `{target}` wrongly emitted \
                 the manifest `{manifest}` — source mode must be bare source"
            );
        }
    }
}

/// The dual of the DV18 test: **project mode** (the default — no
/// `--source-only`) must emit the run-affordance manifest via the scaffolder, so
/// the output is runnable in the target toolchain (§20.6.2). The build may warn
/// that a toolchain is absent (compilation skipped), but the manifest is written
/// before any toolchain invocation, so its presence does not depend on the
/// toolchain being installed.
#[test]
fn project_mode_emits_manifest() {
    for target in &["js", "ts", "rust", "go"] {
        let dir = create_project(MAIN_SOURCE);
        let output = bock_bin()
            .args(["build", "--target", target])
            .current_dir(dir.path())
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "project-mode build failed for {target}: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        let build_dir = dir.path().join(format!("build/{target}"));
        let manifest = target_manifest(target).expect("js/ts/rust/go have manifests");
        assert!(
            build_dir.join(manifest).exists(),
            "project mode for `{target}` must emit the scaffolder manifest \
             `{manifest}` so the output is runnable"
        );
    }
}