harn-cli 0.10.86

CLI for the Harn programming language — run, test, REPL, format, and lint
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
//! Plan-level capability tests: what the solver decides is required, before
//! any edit is emitted.

use super::*;

#[test]
fn capability_apply_preserves_mcp_tool_harness_and_narrows_private_helpers() {
    let temp = tempfile::TempDir::new().unwrap();
    let script = temp.path().join("assistant.mcp.harn");
    let original = "pub fn search(harness: Harness) {\n  return harness.net.get(\"https://example.com\")\n}\n\nfn timestamp(harness: Harness) {\n  return harness.clock.now_ms()\n}\n";
    fs::write(&script, original).unwrap();

    super::apply_repairs_with_options_at(
        temp.path(),
        RepairSafety::SurfaceChanging,
        false,
        super::FixOptions::capability_migrations(),
    )
    .unwrap();

    let updated = fs::read_to_string(&script).unwrap();
    assert!(
        updated.contains("pub fn search(harness: Harness)"),
        "the MCP runtime supplies a root Harness to public tools:\n{updated}"
    );
    assert!(
        updated.contains("fn timestamp(clock: HarnessClock)"),
        "ordinary private helpers should still attenuate:\n{updated}"
    );
}

/// A capability reached only through `${...}` is still reached.
///
/// `visit::walk_program` stops at `Node::InterpolatedString`: the lexer keeps a
/// hole as unparsed source text, so it has no AST children. The whole-program
/// solver computed required capabilities with that walk, so a handle used only
/// inside a hole looked unused. The repair then deleted it from the parameter
/// type and every call site while the interpolation kept calling it. `harn fix
/// --apply` runs unattended in the fleet bump workflow, so it rewrote working
/// helpers into code that does not compile and shipped that as a bump PR
/// (harn-cloud#1469).
///
/// The fixture needs THREE capabilities. With two, no attenuation repair is
/// proposed at all and the test passes vacuously — which is exactly how an
/// earlier version of it passed against the unfixed solver.
///
/// The assertion is EQUIVALENCE, not merely "did not delete": the same use
/// inside and outside a hole must plan the same repair. "Did not delete" alone
/// would also hold if the analysis simply gave up on any file containing a hole.
#[test]
fn capability_plan_counts_uses_inside_string_interpolation() {
    let plan_edits = |name: &str, body: &str| {
        let temp = tempfile::TempDir::new().unwrap();
        let script = temp.path().join(name);
        fs::write(
            &script,
            format!(
                "fn with_temp_dir(harness: {{fs: HarnessFs, process: HarnessProcess, random: HarnessRandom}}, body) {{\n\
                 {body}\
                 \x20 harness.fs.mkdir(dir)\n\
                 \x20 const outcome = try {{\n\
                 \x20   body(dir)\n\
                 \x20 }}\n\
                 \x20 harness.process.run({{program: \"rm\", args: [\"-rf\", dir]}})\n\
                 \x20 return unwrap(outcome)\n\
                 }}\n\n\
                 pipeline main(harness: Harness, task) {{\n\
                 \x20 return with_temp_dir(\n\
                 \x20   {{fs: harness.fs, process: harness.process, random: harness.random}},\n\
                 \x20   {{ dir -> dir }},\n\
                 \x20 )\n\
                 }}\n"
            ),
        )
        .unwrap();
        // `--apply` is a different entry from `build_plan`; only this one
        // reaches the repair that broke. Apply for real and read the file back,
        // so the assertion is on emitted code rather than on a plan.
        super::apply_repairs_with_options_at(
            temp.path(),
            RepairSafety::SurfaceChanging,
            false,
            super::FixOptions::capability_migrations(),
        )
        .unwrap();
        fs::read_to_string(&script).unwrap()
    };

    // Identical programs; `random` is reached through a hole in one and through
    // a plain statement in the other.
    let through_hole = plan_edits(
        "hole.harn",
        "  const dir = \".tmp-${harness.random.uuid_v7()}\"\n",
    );
    let through_statement = plan_edits(
        "statement.harn",
        "  const id = harness.random.uuid_v7()\n  const dir = \".tmp-${id}\"\n",
    );

    let signature = |source: &str| {
        source
            .lines()
            .find(|line| line.starts_with("fn with_temp_dir"))
            .unwrap_or_default()
            .to_string()
    };

    assert!(
        !through_hole.contains("harness.random")
            || signature(&through_hole).contains("HarnessRandom")
            || signature(&through_hole).contains("harness: Harness"),
        "the body still calls `harness.random`, so the repair must not have removed it \
         from the signature:\n{through_hole}"
    );
    assert_eq!(
        signature(&through_hole),
        signature(&through_statement),
        "a capability reached through `${{...}}` must be repaired the same as one \
         reached through a statement"
    );
}

#[test]
fn capability_plan_repairs_imported_helpers_without_type_diagnostics() {
    let temp = tempfile::TempDir::new().unwrap();
    let script = temp.path().join("main.harn");
    fs::write(
        &script,
        "import { agent_capture_events } from \"std/agent/events\"\nimport { agent_parse_tool_calls } from \"std/agent/primitives\"\nimport { agent_session_finalize, agent_session_messages, agent_reminder_providers_fire } from \"std/agent/state\"\n\nfn custom_agent(harness: Harness) -> HarnessAgent {\n  return harness.agent\n}\n\npipeline main(harness: Harness, task) {\n  const session = \"session\"\n  const messages = agent_session_messages(session)\n  agent_session_finalize(session, \"done\")\n  agent_session_finalize(custom_agent(harness), session, \"already explicit\")\n  const captured = agent_capture_events(session, fn() { nil })\n  const parsed = agent_parse_tool_calls(\"<tool_call>x({})</tool_call>\", [], \"text\")\n  const report = agent_reminder_providers_fire(session, \"session_idle\", {}, {})\n  return {messages: messages, captured: captured, parsed: parsed, report: report}\n}\n",
    )
    .unwrap();
    let files = vec![script.clone()];
    let graph = commands::check::build_module_graph(&files);

    let repairs = whole_program_capabilities::plan(
        &files,
        &graph,
        &[],
        &BTreeSet::new(),
        &Default::default(),
        &mut Vec::new(),
    )
    .unwrap();

    assert_eq!(
        repairs
            .iter()
            .flat_map(|repair| &repair.edits)
            .filter(|edit| {
                edit.span.start == edit.span.end && edit.replacement == "harness.agent, "
            })
            .count(),
        5,
        "every imported Agent helper must derive its prefix from the module signature: {repairs:#?}"
    );

    let mut updated = fs::read_to_string(&script).unwrap();
    let mut edits = repairs
        .iter()
        .flat_map(|repair| repair.edits.iter().cloned())
        .collect::<Vec<_>>();
    edits.sort_by_key(|edit| std::cmp::Reverse((edit.span.start, edit.span.end)));
    for edit in edits {
        updated.replace_range(edit.span.start..edit.span.end, &edit.replacement);
    }
    fs::write(&script, updated).unwrap();
    let repaired_graph = commands::check::build_module_graph(&files);
    let fixed_point = whole_program_capabilities::plan(
        &files,
        &repaired_graph,
        &[],
        &BTreeSet::new(),
        &Default::default(),
        &mut Vec::new(),
    )
    .unwrap();
    assert!(
        fixed_point.is_empty(),
        "already-migrated imported calls must be a planner fixed point: {fixed_point:#?}"
    );
}

#[test]
fn capability_plan_preserves_explicit_imported_capability_expressions() {
    let temp = tempfile::TempDir::new().unwrap();
    let script = temp.path().join("main.harn");
    fs::write(
        &script,
        "import { terminal_width } from \"std/tui\"\n\nfn custom_term(term: HarnessTerm) -> HarnessTerm {\n  return term\n}\n\npipeline main(harness: Harness, task) {\n  const term = custom_term(harness.term)\n  return [terminal_width(custom_term(harness.term)), terminal_width(term)]\n}\n",
    )
    .unwrap();
    let files = vec![script];
    let graph = commands::check::build_module_graph(&files);

    let repairs = whole_program_capabilities::plan(
        &files,
        &graph,
        &[],
        &BTreeSet::new(),
        &Default::default(),
        &mut Vec::new(),
    )
    .unwrap();

    assert!(
        repairs.is_empty(),
        "explicit capability expressions must not be shifted into optional ordinary parameters: {repairs:#?}"
    );
}

#[test]
fn capability_plan_preserves_an_unknown_capability_identifier_when_ordinary_args_are_missing() {
    let temp = tempfile::TempDir::new().unwrap();
    let script = temp.path().join("main.harn");
    fs::write(
        &script,
        "import { agent_session_messages } from \"std/agent/state\"\n\nfn custom_agent(harness: Harness) -> HarnessAgent {\n  return harness.agent\n}\n\npipeline main(harness: Harness, task) {\n  const agent = custom_agent(harness)\n  return agent_session_messages(agent)\n}\n",
    )
    .unwrap();
    let files = vec![script];
    let graph = commands::check::build_module_graph(&files);

    let repairs = whole_program_capabilities::plan(
        &files,
        &graph,
        &[],
        &BTreeSet::new(),
        &Default::default(),
        &mut Vec::new(),
    )
    .unwrap();

    assert!(
        !repairs
            .iter()
            .flat_map(|repair| &repair.edits)
            .any(|edit| edit.replacement == "harness.agent, "),
        "an unknown identifier may already be the carrier; adding one would shift it into the missing session slot: {repairs:#?}"
    );
}

#[test]
fn capability_plan_uses_inferred_capability_types_to_repair_a_different_missing_carrier() {
    let temp = tempfile::TempDir::new().unwrap();
    let library = temp.path().join("library.harn");
    let entrypoint = temp.path().join("main.harn");
    fs::write(
        &library,
        "pub fn run_with_root(harness: Harness, agent: HarnessAgent) {\n  return {root: harness, agent: agent}\n}\n",
    )
    .unwrap();
    fs::write(
        &entrypoint,
        "import { run_with_root } from \"./library\"\n\nfn custom_agent(harness: Harness) -> HarnessAgent {\n  return harness.agent\n}\n\npipeline main(harness: Harness, task) {\n  const agent = custom_agent(harness)\n  return run_with_root(agent)\n}\n",
    )
    .unwrap();
    let files = vec![entrypoint, library];
    let graph = commands::check::build_module_graph(&files);

    let repairs = whole_program_capabilities::plan(
        &files,
        &graph,
        &[],
        &BTreeSet::new(),
        &Default::default(),
        &mut Vec::new(),
    )
    .unwrap();

    assert!(
        repairs
            .iter()
            .flat_map(|repair| &repair.edits)
            .any(|edit| edit.replacement == "harness, "),
        "an inferred Agent cannot occupy a root Harness slot, so the missing root carrier is observable: {repairs:#?}"
    );
}

#[test]
fn capability_plan_disambiguates_shadowed_inferred_bindings_by_declaration() {
    let temp = tempfile::TempDir::new().unwrap();
    let script = temp.path().join("main.harn");
    fs::write(
        &script,
        "import { agent_session_messages } from \"std/agent/state\"\n\nfn custom_agent(harness: Harness) -> HarnessAgent {\n  return harness.agent\n}\n\npipeline main(harness: Harness, task) {\n  const value = custom_agent(harness)\n  const before = agent_session_messages(value, \"outer-before\")\n  const nested = [1].map({ _ ->\n    const value = \"nested-session\"\n    return agent_session_messages(value)\n  })\n  const after = agent_session_messages(value, \"outer-after\")\n  return {before: before, nested: nested, after: after}\n}\n",
    )
    .unwrap();
    let files = vec![script];
    let graph = commands::check::build_module_graph(&files);

    let repairs = whole_program_capabilities::plan(
        &files,
        &graph,
        &[],
        &BTreeSet::new(),
        &Default::default(),
        &mut Vec::new(),
    )
    .unwrap();

    assert_eq!(
        repairs
            .iter()
            .flat_map(|repair| &repair.edits)
            .filter(|edit| edit.replacement == "harness.agent, ")
            .count(),
        1,
        "only the string-valued shadow should receive the missing Agent carrier: {repairs:#?}"
    );
}

#[test]
fn capability_plan_completes_a_partial_imported_capability_prefix() {
    let temp = tempfile::TempDir::new().unwrap();
    let script = temp.path().join("main.harn");
    fs::write(
        &script,
        "import { which } from \"std/os\"\n\npipeline main(harness: Harness, task) {\n  return which(harness.tools, \"git\")\n}\n",
    )
    .unwrap();
    let files = vec![script.clone()];
    let graph = commands::check::build_module_graph(&files);

    let repairs = whole_program_capabilities::plan(
        &files,
        &graph,
        &[],
        &BTreeSet::new(),
        &Default::default(),
        &mut Vec::new(),
    )
    .unwrap();
    let edits = repairs
        .iter()
        .flat_map(|repair| &repair.edits)
        .filter(|edit| edit.replacement == ", harness.system")
        .collect::<Vec<_>>();
    assert_eq!(
        edits.len(),
        1,
        "only the missing capability suffix should be inserted: {repairs:#?}"
    );
    let on_disk = fs::read_to_string(&script).unwrap();
    #[expect(clippy::string_slice, reason = "test input is ASCII")]
    let inserted_at = &on_disk[edits[0].span.start..edits[0].span.end];
    assert_eq!(inserted_at, "");

    let mut updated = fs::read_to_string(&script).unwrap();
    let mut all_edits = repairs
        .iter()
        .flat_map(|repair| repair.edits.iter().cloned())
        .collect::<Vec<_>>();
    all_edits.sort_by_key(|edit| std::cmp::Reverse((edit.span.start, edit.span.end)));
    for edit in all_edits {
        updated.replace_range(edit.span.start..edit.span.end, &edit.replacement);
    }
    assert_eq!(
        call_argument_paths(&updated, "which")[0],
        [
            Some("harness.tools".to_string()),
            Some("harness.system".to_string()),
            None,
        ]
    );
    fs::write(&script, updated).unwrap();
    let repaired_graph = commands::check::build_module_graph(&files);
    assert!(
        whole_program_capabilities::plan(
            &files,
            &repaired_graph,
            &[],
            &BTreeSet::new(),
            &Default::default(),
            &mut Vec::new(),
        )
        .unwrap()
        .is_empty(),
        "a completed imported prefix must be a planner fixed point"
    );
}

#[test]
fn capability_plan_resolves_private_imported_capability_aliases() {
    let temp = tempfile::TempDir::new().unwrap();
    let library = temp.path().join("library.harn");
    let entrypoint = temp.path().join("main.harn");
    fs::write(
        &library,
        "type AgentHandle = HarnessAgent\n\npub fn imported_helper(agent: AgentHandle, session: string) {\n  return agent.snapshot(session)\n}\n",
    )
    .unwrap();
    fs::write(
        &entrypoint,
        "import { imported_helper } from \"./library\"\n\npipeline main(harness: Harness, task) {\n  return imported_helper(\"session\")\n}\n",
    )
    .unwrap();
    let files = vec![entrypoint, library];
    let graph = commands::check::build_module_graph(&files);

    let repairs = whole_program_capabilities::plan(
        &files,
        &graph,
        &[],
        &BTreeSet::new(),
        &Default::default(),
        &mut Vec::new(),
    )
    .unwrap();

    assert!(
        repairs.iter().flat_map(|repair| &repair.edits).any(|edit| {
            edit.span.start == edit.span.end && edit.replacement == "harness.agent, "
        }),
        "a private signature alias must resolve through the module graph: {repairs:#?}"
    );
}