harn-cli 0.10.15

CLI for the Harn programming language — run, test, REPL, format, and lint
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
use harn_serve::adapters::acp::{
    ACP_SCHEMA_COMPATIBILITY, HARN_AGENT_EVENT_KINDS, HARN_AGENT_EVENT_METHOD,
    HARN_CONTENT_EXTENSION_FIELDS, HARN_PROVIDER_CATALOG_METHOD, HARN_SESSION_UPDATE_EXTENSIONS,
    HARN_TOOL_LIFECYCLE_EXTENSION_FIELDS,
};
use harn_serve::{A2A_PROTOCOL_VERSION, MCP_PROTOCOL_VERSION};
use harn_vm::llm::receipts::{
    tool_call_receipt_schema, TOOL_CALL_RECEIPT_EXECUTORS, TOOL_CALL_RECEIPT_SCHEMA_ARTIFACT,
    TOOL_CALL_RECEIPT_SCHEMA_VERSION, TOOL_CALL_RECEIPT_STATUSES,
};
use serde_json::json;

use super::constants::*;
use super::support::*;
use super::values::*;

pub(super) fn generate_tool_call_receipt_schema() -> Result<String, String> {
    serde_json::to_string_pretty(&tool_call_receipt_schema())
        .map_err(|error| format!("failed to serialize tool-call receipt schema: {error}"))
}

/// Render the protocol-artifact manifest the running Harn would write.
///
/// Re-exposed via `harn package artifacts manifest` so downstream automation
/// (downstream hosts, cloud platforms) can compare against vendored copies without
/// shelling out to `dump-protocol-artifacts`.
pub(crate) fn manifest_json() -> Result<String, String> {
    generate_manifest()
}

pub(super) fn generate_manifest() -> Result<String, String> {
    generate_manifest_for_version(env!("CARGO_PKG_VERSION"))
}

pub(super) fn generate_manifest_for_version(artifact_version: &str) -> Result<String, String> {
    let mut schemas = SCHEMA_COPIES
        .iter()
        .map(|schema| {
            Ok(json!({
                "protocol": schema.protocol,
                "source": schema.source,
                "artifact": schema.artifact,
                "provenance": schema_provenance(schema.source)?,
            }))
        })
        .collect::<Result<Vec<_>, String>>()?;
    let receipt_schema = tool_call_receipt_schema();
    schemas.push(json!({
        "protocol": "harn",
        "source": "crates/harn-vm/src/llm/receipts.rs",
        "artifact": TOOL_CALL_RECEIPT_SCHEMA_ARTIFACT,
        "provenance": receipt_schema
            .get("x-harn-provenance")
            .cloned()
            .unwrap_or(serde_json::Value::Null),
    }));

    serde_json::to_string_pretty(&json!({
        "schemaVersion": 1,
        "artifactVersion": artifact_version,
        "generatedBy": "harn dump-protocol-artifacts",
        "checkCommand": "make check-protocol-artifacts",
        "bindings": {
            "typescript": {
                "artifact": "harn-protocol.ts",
                "stability": "stable",
            },
            "swift": {
                "artifact": "HarnProtocol.swift",
                "stability": "stable",
            },
            "rust": {
                "artifact": "harn-protocol.rs",
                "vendorPath": "protocol/src/generated.rs",
                "stability": "stable",
            },
            "python": {
                "artifact": "python/harn_protocol.py",
                "minimumPythonVersion": "3.9",
                "stability": "stable",
            },
            "go": {
                "artifact": "go/harnprotocol/harnprotocol.go",
                "modulePath": "github.com/burin-labs/harn/spec/protocol-artifacts/go/harnprotocol",
                "stability": "stable",
            },
        },
        "schemas": schemas,
        "acp": {
            "schemaCompatibility": ACP_SCHEMA_COMPATIBILITY,
            "agentMethods": ACP_AGENT_METHODS,
            "dispatchedMethods": ACP_DISPATCHED_METHODS,
            "transportControlMethods": ACP_TRANSPORT_CONTROL_METHODS,
            "handledMethods": concat_unique_wire_values(&[
                ACP_TRANSPORT_CONTROL_METHODS,
                ACP_DISPATCHED_METHODS,
            ]),
            "deprecatedAgentMethods": deprecated_wire_values(ACP_DEPRECATED_AGENT_METHODS),
            "clientMethods": ACP_CLIENT_METHODS,
            "agentNotifications": ACP_AGENT_NOTIFICATIONS,
            "sessionUpdateVariants": all_acp_session_updates(),
            "harnSessionUpdateExtensions": HARN_SESSION_UPDATE_EXTENSIONS,
            "harnSessionTimelineMethods": HARN_SESSION_TIMELINE_METHODS,
            "harnAgentEventMethod": HARN_AGENT_EVENT_METHOD,
            "harnProviderCatalogMethod": HARN_PROVIDER_CATALOG_METHOD,
            "harnAgentEventKinds": HARN_AGENT_EVENT_KINDS,
            "toolLifecycleExtensionFields": HARN_TOOL_LIFECYCLE_EXTENSION_FIELDS,
            "contentExtensionFields": HARN_CONTENT_EXTENSION_FIELDS,
            "contentBlockTypes": ACP_CONTENT_BLOCK_TYPES,
            "toolKinds": tool_kind_values(),
            "toolCallStatuses": tool_call_status_values(),
            "toolCallErrorCategories": tool_call_error_category_values(),
            "toolMutationStatuses": tool_mutation_status_values(),
            "toolExecutorSimpleValues": ACP_TOOL_EXECUTOR_SIMPLE_VALUES,
            "workerStatuses": worker_status_values(),
        },
        "a2a": {
            "protocolVersion": A2A_PROTOCOL_VERSION,
            "methods": A2A_METHODS,
            "taskStates": A2A_TASK_STATES,
            "taskEventTypes": A2A_TASK_EVENT_TYPES,
        },
        "mcp": {
            "protocolVersion": MCP_PROTOCOL_VERSION,
            "stableProtocolVersion": MCP_PROTOCOL_VERSION,
            "draftProtocolVersion": MCP_DRAFT_PROTOCOL_VERSION,
            "legacy20250618ProtocolVersion": MCP_LEGACY_2025_06_18_PROTOCOL_VERSION,
            "final2026ProtocolVersion": MCP_FINAL_2026_PROTOCOL_VERSION,
            "protocolVersions": MCP_PROTOCOL_VERSIONS,
            "jsonSchemaDialect": MCP_JSON_SCHEMA_2020_12_DIALECT,
            "methods": MCP_METHODS,
            "requiredRequestMetadataKeys": MCP_REQUIRED_METADATA_KEYS,
            "metadataKeys": MCP_METADATA_KEYS,
            "standardHttpHeaders": MCP_STANDARD_HTTP_HEADERS,
            "cacheResultFields": MCP_CACHE_RESULT_FIELDS,
            "cacheScopes": MCP_CACHE_SCOPES,
            "resultTypes": MCP_RESULT_TYPES,
            "inputRequiredResultType": MCP_INPUT_REQUIRED_RESULT_TYPE,
            "unsupportedProtocolVersionError": {
                "code": MCP_UNSUPPORTED_PROTOCOL_VERSION_ERROR_CODE,
                "message": MCP_UNSUPPORTED_PROTOCOL_VERSION_ERROR_MESSAGE,
            },
            "oauthClientRegistrationModes": MCP_OAUTH_CLIENT_REGISTRATION_MODES,
            "oauthAuthModes": MCP_OAUTH_AUTH_MODES,
            "oauthApplicationTypes": MCP_OAUTH_APPLICATION_TYPES,
            "loggingLevels": MCP_LOGGING_LEVELS,
        },
        "receipts": {
            "toolCallReceiptSchemaVersion": TOOL_CALL_RECEIPT_SCHEMA_VERSION,
            "toolCallReceiptSchema": TOOL_CALL_RECEIPT_SCHEMA_ARTIFACT,
            "toolCallReceiptStatuses": TOOL_CALL_RECEIPT_STATUSES,
            "toolCallReceiptExecutors": TOOL_CALL_RECEIPT_EXECUTORS,
        },
    }))
    .map_err(|error| format!("failed to serialize manifest: {error}"))
}

pub(super) fn deprecated_wire_values(values: &[DeprecatedWireValue]) -> serde_json::Value {
    let mut map = serde_json::Map::new();
    for value in values {
        map.insert(
            value.value.to_string(),
            json!({
                "replacement": value.replacement,
                "removal": "after one release",
            }),
        );
    }
    serde_json::Value::Object(map)
}

pub(super) fn generate_readme() -> String {
    format!(
        "# Harn Protocol Artifacts\n\n\
         <!-- GENERATED by `harn dump-protocol-artifacts` -- do not edit by hand. -->\n\n\
         This directory is the checked-in Harn protocol contract for downstream hosts.\n\
         It publishes JSON Schema profiles plus TypeScript, Swift, Rust, Python, and\n\
         Go bindings generated from Harn's adapter vocabulary. Hosts should consume or\n\
         vendor these artifacts directly instead of maintaining hand-written mirrors\n\
         of Harn wire enums, JSON-RPC envelopes, or extension fields.\n\n\
         Regenerate with:\n\n\
         ```sh\n\
         make gen-protocol-artifacts\n\
         ```\n\n\
         Verify drift with:\n\n\
         ```sh\n\
         make check-protocol-artifacts\n\
         ```\n\n\
         Files:\n\n\
         - `manifest.json`: deterministic summary of protocol versions, advertised Harn\n\
           extension fields, and generated binding vocabulary.\n\
         - `schemas/acp-session-update.schema.json`: Harn's ACP session-update schema\n\
           profile (`{ACP_SCHEMA_COMPATIBILITY}`).\n\
         - `schemas/a2a-0.3.0.schema.json`: Harn's A2A schema profile (`{A2A_PROTOCOL_VERSION}`).\n\
         - `schemas/mcp-2025-11-25.schema.json`: Harn's MCP schema profile (`{MCP_PROTOCOL_VERSION}`).\n\
         - `schemas/mcp-draft-2026-v1.schema.json`: Harn's opt-in MCP RC schema\n\
           profile (`{MCP_DRAFT_PROTOCOL_VERSION}`), pinned beside the stable profile until the\n\
           final `2026-07-28` specification lands.\n\
         - `schemas/tool-call-receipt.schema.json`: Harn's typed, privacy-preserving\n\
           `ToolCallReceipt` schema for audited tool calls.\n\
         - `harn-protocol.ts`: TypeScript definitions for ACP session updates,\n\
           tool lifecycle metadata, A2A task events, and MCP metadata.\n\
         - `HarnProtocol.swift`: Swift definitions for the same host-facing surface.\n\
         - `harn-protocol.rs`: dependency-free Rust module of ACP method-name,\n\
           session-update discriminator, content-extension key, and protocol\n\
           version `pub const`s. The only binding that publishes the complete\n\
           dispatched ACP method surface; an IDE host vendors it as\n\
           `protocol/src/generated.rs`.\n\
         - `python/harn_protocol.py`: Python dataclasses, enums, and constants for\n\
           the same host-facing surface (Python 3.9+, stdlib-only).\n\
         - `go/harnprotocol/harnprotocol.go`: Go package with structs, typed string\n\
           aliases, and constants mirroring the Python and Swift bindings.\n\
         - `fixtures/round_trip.json`: representative JSON envelopes used by\n\
           `make check-bindings` to exercise Python and Go round-trips.\n\
         - `fixtures/mcp-rc/`: hand-authored MCP DRAFT-2026-v1 wire fixtures\n\
           (modern success, unsupported-version retry, cache hints,\n\
           input-required, header mismatch, no-session HTTP, recursive\n\
           `$defs` tool schema, legacy 2025-11-25 compat) replayed by\n\
           `make mcp-rc-conformance` and republished here for downstream host\n\
           and cloud test suites.\n\n\
         Compatibility rule: additive enum values and optional fields are minor-version\n\
         compatible; removing or renaming a wire value requires a Harn minor-version\n\
         migration note and a regenerated artifact diff.\n",
    )
}

#[cfg(test)]
pub(super) fn generate_round_trip_fixture() -> Result<String, String> {
    generate_round_trip_fixture_for_version(env!("CARGO_PKG_VERSION"))
}

pub(super) fn generate_round_trip_fixture_for_version(
    artifact_version: &str,
) -> Result<String, String> {
    let tool_call = json!({
        "sessionUpdate": "tool_call",
        "toolCallId": "call-001",
        "title": "Read README.md",
        "kind": "read",
        "status": "in_progress",
        "rawInput": { "path": "README.md" },
        "_meta": {
            "harn": {
                "executor": "harn_builtin",
                "durationMs": 12.5,
                "executionDurationMs": 11.2,
                "parsing": false,
            }
        }
    });
    let session_update = json!({
        "jsonrpc": "2.0",
        "method": "session/update",
        "params": {
            "sessionId": "sess-42",
            "update": tool_call,
        }
    });
    let agent_event = json!({
        "jsonrpc": "2.0",
        "method": HARN_AGENT_EVENT_METHOD,
        "params": {
            "sessionId": "sess-42",
            "kind": "composition_child_call",
            "runId": "cmp-42",
            "toolCallId": "tool-cmp-42",
            "toolName": "tool.write_file",
            "operationIndex": 0,
            "requestedSideEffectLevel": "workspace_write",
            "annotations": {
                "kind": "edit",
                "side_effect_level": "workspace_write",
            },
            "policyContext": {
                "ceiling": "workspace_write",
                "approval": "denied",
            },
            "rawInput": {
                "path": "src/lib.rs",
                "content": "...",
            },
        }
    });
    let tool_call_receipt = json!({
        "schema_version": TOOL_CALL_RECEIPT_SCHEMA_VERSION,
        "session_id": "sess-42",
        "run_id": "run-42",
        "tool_call_id": "call-001",
        "tool_name": "read_file",
        "iteration": 1,
        "turn_index": 0,
        "emit_order": 0,
        "reason": "Read README.md for context",
        "kind": "read",
        "executor": "harn",
        "status": "ok",
        "error_category": null,
        "duration_ms": 12,
        "args_hash": "0".repeat(64),
        "result_hash": "1".repeat(64),
        "audit": {
            "summary": "Read README.md for context",
            "layers": [{"name": "with_required_reason", "status": "ok"}],
        },
        "emitted_at": "2026-05-16T00:00:00Z",
        "model": "mock",
        "provider": "mock",
    });
    let request = json!({
        "jsonrpc": "2.0",
        "id": 17,
        "method": "session/prompt",
        "params": {"sessionId": "sess-42", "prompt": [{"type": "text", "text": "hi"}]},
    });
    let response = json!({
        "jsonrpc": "2.0",
        "id": 17,
        "result": {"ok": true},
    });
    let error_response = json!({
        "jsonrpc": "2.0",
        "id": "abc",
        "error": {"code": -32601, "message": "method not found"},
    });
    let a2a_task = json!({
        "id": "task-1",
        "contextId": "ctx-7",
        "status": {
            "state": "working",
            "message": {"id": "msg-1", "role": "agent", "parts": [{"type": "text", "text": "ok"}]}
        }
    });
    let mcp_tool = json!({
        "name": "echo",
        "title": "Echo",
        "description": "Echoes input",
        "inputSchema": {
            "$schema": MCP_JSON_SCHEMA_2020_12_DIALECT,
            "type": "object",
            "properties": {"x": {"type": "string"}}
        }
    });
    let mcp_discover_result = json!({
        "resultType": "complete",
        "supportedVersions": MCP_PROTOCOL_VERSIONS,
        "capabilities": {"tools": {}},
        "serverInfo": {"name": "harn", "version": artifact_version},
        "instructions": "Use the stable MCP version unless the RC is explicitly enabled.",
    });
    let mcp_input_required_result = json!({
        "resultType": MCP_INPUT_REQUIRED_RESULT_TYPE,
        "requestState": "opaque-state",
        "inputRequests": {
            "confirm": {
                "method": "elicitation/create",
                "params": {
                    "message": "Confirm the action",
                    "mode": "form",
                    "requestedSchema": {
                        "$schema": MCP_JSON_SCHEMA_2020_12_DIALECT,
                        "type": "object",
                        "properties": {"confirmed": {"type": "boolean"}},
                        "required": ["confirmed"],
                    }
                }
            }
        }
    });
    let mcp_unsupported_version_error = json!({
        "jsonrpc": "2.0",
        "id": 19,
        "error": {
            "code": MCP_UNSUPPORTED_PROTOCOL_VERSION_ERROR_CODE,
            "message": MCP_UNSUPPORTED_PROTOCOL_VERSION_ERROR_MESSAGE,
            "data": {
                "requested": "DRAFT-2026-v0",
                "supported": MCP_PROTOCOL_VERSIONS,
            }
        }
    });

    let fixture = json!({
        "artifactVersion": artifact_version,
        "harnAgentEventMethod": HARN_AGENT_EVENT_METHOD,
        "harnProviderCatalogMethod": HARN_PROVIDER_CATALOG_METHOD,
        "envelopes": {
            "request": request,
            "response": response,
            "errorResponse": error_response,
            "sessionUpdateNotification": session_update,
            "agentEventNotification": agent_event,
        },
        "a2aTask": a2a_task,
        "mcpTool": mcp_tool,
        "mcpDiscoverResult": mcp_discover_result,
        "mcpInputRequiredResult": mcp_input_required_result,
        "mcpUnsupportedProtocolVersionError": mcp_unsupported_version_error,
        "toolCallReceipt": tool_call_receipt,
    });

    serde_json::to_string_pretty(&fixture)
        .map_err(|error| format!("failed to serialize round-trip fixture: {error}"))
}