Implementation:A5c ai Babysitter canonicalize args
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Cryptography, Canonicalization, Policy_Enforcement |
| Last Updated | 2026-10-23 00:00 GMT |
Overview
Concrete tool for canonical argument serialization and hashing used in proof-based policy enforcement gates.
Description
The canonicalize-args.ts module provides the single shared canonicalizer for argument serialization in the policy enforcement system. It guarantees:
- TOTAL: Defined for every JSON-representable value; never throws on well-formed input.
- LOSS-PRESERVING: Object keys are deep-sorted, but array order, string bytes, and value types are preserved. No lowercase, trim, or unicode-fold.
- FAIL-CLOSED: Non-finite numbers (NaN, ±Infinity) throw instead of silently coercing to null.
Four exported functions:
- canonicalizeArgs(value) — Canonical JSON string with deep-sorted keys.
- argsHash(value) — SHA-256 of canonicalizeArgs.
- commandHash(command) — SHA-256 of canonicalizeArgv joined by |.
- canonicalizeArgv(command) — POSIX-ish tokenizer respecting quotes; throws on unterminated quotes.
Usage
- Policy enforcement Step 5: Recompute hashes at the execution gate to verify command authorization (TOCTOU prevention).
Code Reference
Source Location
- Repository: Babysitter
- File: packages/adapters/policy/src/canonicalize-args.ts:L34-98
Signature
function canonicalizeArgs(value: unknown): string;
function argsHash(value: unknown): string;
// = sha256(canonicalizeArgs(value))
function commandHash(command: string | string[]): string;
// = sha256(canonicalizeArgv(command).join('|'))
function canonicalizeArgv(command: string): string[];
// Tokenize shell command into canonical argv array
Import
import { canonicalizeArgs, argsHash, commandHash, canonicalizeArgv } from "@a5c-ai/policy-adapter";
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| value | unknown | Yes (args) | Any JSON-representable value |
| command | string[] | Yes (cmd) │ Shell command line or argv array |
Outputs
| Name | Type | Description |
|---|---|---|
| canonicalizeArgs | string │ Canonical JSON string | |
| argsHash | string | SHA-256 hex digest |
| commandHash | string | SHA-256 hex digest |
| canonicalizeArgv | string[] │ Token array |
Usage Examples
Canonicalize and Hash Arguments
import { canonicalizeArgs, argsHash, commandHash } from "@a5c-ai/policy-adapter";
// Args are order-independent after canonicalization
const a = argsHash({ repo: "origin", ref: "main" });
const b = argsHash({ ref: "main", repo: "origin" });
console.log(a === b); // true — deep-sorted keys
// Command hash for shell execution
const hash = commandHash("git push origin main");
console.log("Command hash:", hash);
Fail-Closed on Non-Representable
try {
canonicalizeArgs({ value: NaN });
} catch (e) {
console.error(e.message); // "canonicalizeArgs: non-finite number is not JSON-representable"
}
Related Pages
Implements Principle
Requires Environment
Uses Heuristics
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment