@hasna/contracts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,17 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ Copyright 2026 Hasna, Inc.
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,269 @@
1
+ # @hasna/contracts
2
+
3
+ Shared schemas, TypeScript types, validators, fixtures, and CLI checks for Hasna
4
+ open-source agent infrastructure.
5
+
6
+ `@hasna/contracts` is not a database, daemon, scheduler, or orchestrator. It is
7
+ the shared language used by the `open-*` packages when they exchange refs, runs,
8
+ decisions, costs, context packs, validation plans, trajectories, and proof
9
+ bundles.
10
+
11
+ ## Purpose
12
+
13
+ Agents lose time and tokens when every package invents its own shape for
14
+ evidence, actors, runs, costs, decisions, and status. This package defines the
15
+ boring contracts those packages can validate at CLI, MCP, SDK, API, event, and
16
+ file boundaries.
17
+
18
+ The goal is to make integration work deterministic:
19
+
20
+ - Producers emit a small object with a known `schema` id.
21
+ - Consumers validate the object before spending model context on it.
22
+ - Failed validation returns concrete field errors instead of vague agent prose.
23
+ - Work products can link task ids, run ids, evidence ids, and proof ids without
24
+ custom glue scripts.
25
+
26
+ ## Install
27
+
28
+ ```bash
29
+ bun add @hasna/contracts
30
+ ```
31
+
32
+ ## CLI
33
+
34
+ The CLI is Bun-based. Use the package `bin` entries (`contracts` or
35
+ `contracts-cli`) from Bun/npm scripts; do not import `@hasna/contracts/cli` as a
36
+ Node library.
37
+
38
+ List known schema ids:
39
+
40
+ ```bash
41
+ contracts schemas
42
+ contracts schemas --json
43
+ ```
44
+
45
+ Validate a file using its embedded `schema` field:
46
+
47
+ ```bash
48
+ contracts validate examples/evidence-ref.valid.json
49
+ ```
50
+
51
+ Validate against an explicit schema id:
52
+
53
+ ```bash
54
+ contracts validate --schema hasna.evidence_ref.v1 examples/evidence-ref.valid.json
55
+ ```
56
+
57
+ Check package fixtures. Files ending in `.valid.json` must pass, and files
58
+ ending in `.invalid.json` must fail for schema reasons. Empty fixture sets,
59
+ unknown schemas, and malformed JSON are harness failures.
60
+
61
+ ```bash
62
+ contracts conformance examples
63
+ contracts conformance --json examples
64
+ ```
65
+
66
+ ## TypeScript
67
+
68
+ ```ts
69
+ import {
70
+ SCHEMA_IDS,
71
+ parseEmbeddedContract,
72
+ parseContract,
73
+ validateEmbeddedContract,
74
+ validateContract,
75
+ type EvidenceRef,
76
+ type EvidenceRefInput
77
+ } from "@hasna/contracts";
78
+
79
+ const draft: EvidenceRefInput = {
80
+ schema: SCHEMA_IDS.evidenceRef,
81
+ id: "ev_tests",
82
+ createdAt: "2026-06-27T10:00:00.000Z",
83
+ kind: "command_output",
84
+ uri: "artifact://runs/run_123/tests.txt"
85
+ };
86
+
87
+ const result = validateContract(SCHEMA_IDS.evidenceRef, draft);
88
+ if (!result.success) {
89
+ return { ok: false, issues: result.error.issues };
90
+ }
91
+
92
+ const evidence: EvidenceRef = parseContract(SCHEMA_IDS.evidenceRef, draft);
93
+ const embedded = validateEmbeddedContract(draft);
94
+ const parsedBySchemaField = parseEmbeddedContract(draft);
95
+ ```
96
+
97
+ Use `validateContract` at boundaries when you want to return structured issues.
98
+ Use `parseContract` when invalid input should throw `ContractValidationError`.
99
+ Use `validateEmbeddedContract` or `parseEmbeddedContract` when the producer sends
100
+ a top-level object and the consumer should dispatch from its `schema` field.
101
+ Input aliases such as `EvidenceRefInput` describe producer payloads before Zod
102
+ defaults are applied; output aliases such as `EvidenceRef` describe parsed data.
103
+
104
+ ## Contract Catalog
105
+
106
+ - `hasna.actor_ref.v1`: agent, human, service, model, workflow, or system actor.
107
+ - `hasna.resource_ref.v1`: portable reference to a task, repo, file, run,
108
+ session, loop, knowledge item, report, proof bundle, or other shared resource.
109
+ - `hasna.evidence_ref.v1`: pointer to files, command output, screenshots, logs,
110
+ diffs, reports, artifacts, URLs, videos, HAR captures, test results, metrics,
111
+ traces, or other evidence.
112
+ - `hasna.work_run.v1`: normalized run receipt for agent, command, workflow, loop,
113
+ eval, test, deploy, or review work.
114
+ - `hasna.decision_envelope.v1`: decision record with selected/skipped
115
+ resources, rationale, actor, costs, obligations, redactions, and evidence.
116
+ - `hasna.cost_estimate.v1`: money and token estimates with provider, model,
117
+ account, basis, and resource references.
118
+ - `hasna.capability_card.v1`: machine-readable description of a package command,
119
+ MCP tool, API operation, workflow, or agent skill.
120
+ - `hasna.context_pack.v1`: bounded context bundle with objective, resources,
121
+ evidence, constraints, and token budget.
122
+ - `hasna.agent_trajectory.v1`: compact trace of agent steps, tool calls,
123
+ decisions, blockers, and final outcome.
124
+ - `hasna.validation_plan.v1`: deterministic checks a package or agent should run.
125
+ - `hasna.proof_bundle.v1`: reviewable validation result that ties a subject to
126
+ checks, evidence, verifier, and verdict.
127
+
128
+ Every top-level contract includes a literal `schema` field. Consumers should
129
+ reject objects whose embedded schema does not match the validator being used.
130
+ Top-level `EvidenceRef` documents are dereferenceable and require a URI; nested
131
+ evidence pointers may be compact `{ id }` links when the enclosing bundle or
132
+ store can resolve them.
133
+
134
+ Capability cards use compact kinds: package commands, MCP tools, and API
135
+ operations map to `tool`; workflow runners map to `service` or `lane`; agent
136
+ skills map to `agent`; model routes map to `model`; connectors map to
137
+ `connector`.
138
+
139
+ Common resource-kind mappings:
140
+
141
+ | Repo domain | Preferred resource kinds |
142
+ | --- | --- |
143
+ | `open-todos` | `task`, `project`, `verification`, `proof_bundle` |
144
+ | `open-loops` | `loop`, `workflow`, `run`, `artifact` |
145
+ | `open-actions` | `action`, `tool`, `event`; decisions are emitted as `DecisionEnvelope` contracts, not resource kinds |
146
+ | `open-sessions` | `session`, `run`, `machine`, `artifact` |
147
+ | `open-context` | `context_pack`, `file`, `url`, `knowledge` |
148
+ | `open-knowledge` / `open-mementos` | `knowledge`, `memento`, `context_pack` |
149
+ | `open-files` | `file`, `artifact`, `url` |
150
+ | `open-evals` | `eval`, `verification`, `report`, `proof_bundle` |
151
+ | `open-economy` | `cost`, `budget`; budget choices are emitted as `DecisionEnvelope` contracts, not resource kinds |
152
+ | `open-monitor` | `alert`, `incident`, `machine`, `report` |
153
+
154
+ ## Package Boundaries
155
+
156
+ `@hasna/contracts` owns schemas, types, validators, examples, and conformance
157
+ helpers. Owning packages still own storage and behavior.
158
+
159
+ - `open-todos` owns tasks, task plans, locks, comments, and task evidence.
160
+ - `open-loops` owns loop and workflow execution.
161
+ - `open-actions` owns executable action manifests.
162
+ - `open-sessions` owns transcript and trajectory ingestion.
163
+ - `open-context` owns context-pack construction and retrieval.
164
+ - `open-knowledge` owns durable knowledge records and promotion workflows.
165
+ - `open-files` owns artifact storage, file indexing, and dereference logic.
166
+ - `open-mementos` owns memory lifecycle and recall.
167
+ - `open-reports` owns rendered reports and proof presentation.
168
+ - `open-evals` owns evaluation execution and scored validation results.
169
+ - `open-economy` owns budget, cost, and usage policy decisions.
170
+ - `open-monitor` owns fleet health classification and alerting.
171
+
172
+ ## Downstream Integration Recipes
173
+
174
+ Adopt contracts as optional compact boundary output first. Prefer `--contract`
175
+ CLI flags, MCP response variants, or SDK adapter functions rather than replacing
176
+ native domain objects immediately.
177
+
178
+ - `open-files`: emit `ResourceRef`, `EvidenceRef`, and `ContextPack` for file
179
+ records, versions, signed URLs, source manifests, and evidence assets.
180
+ - `open-todos`: expose task refs as `ResourceRef`; verification evidence as
181
+ `ProofBundle`; task execution receipts as `WorkRun`; review gates as
182
+ `ValidationPlan`.
183
+ - `open-loops`: emit loop/workflow runs as `WorkRun`, audit traces as
184
+ `AgentTrajectory`, logs/artifacts as `EvidenceRef`, and verifier output as
185
+ `ProofBundle`.
186
+ - `open-sessions`: convert messages/tool calls to `AgentTrajectory`, token
187
+ usage to `CostEstimate`, and transcript paths to `EvidenceRef`.
188
+ - `open-context`: serialize built context as `ContextPack` with citations as
189
+ `EvidenceRef` and source chunks as `ResourceRef`.
190
+ - `open-knowledge`: return retrieval results as `ContextPack`; write policy and
191
+ promotion decisions as `DecisionEnvelope`.
192
+ - `open-mementos`: expose memories as `ResourceRef` kind `memento` or
193
+ `knowledge`; reflection runs as `WorkRun`.
194
+ - `open-evals`: map cases/assertions to `ValidationPlan`, runs/results to
195
+ `ProofBundle`, reports to `EvidenceRef`, and judge/baseline choices to
196
+ `DecisionEnvelope`.
197
+ - `open-economy`: own `CostEstimate` production and emit budget decisions as
198
+ `DecisionEnvelope`.
199
+ - `open-monitor`: output doctor checks, fleet triage, and alerts as
200
+ `ValidationPlan`, `ProofBundle`, `EvidenceRef`, `alert`, and `incident`
201
+ resources.
202
+ - `open-actions`: keep domain action manifests, but expose shared `ActorRef`,
203
+ `EvidenceRef`, `CapabilityCard`, `DecisionEnvelope`, and `WorkRun` adapter
204
+ views.
205
+ - `open-reports`: consume `ProofBundle`, `WorkRun`, `ContextPack`,
206
+ `CostEstimate`, and `EvidenceRef` to render compact Markdown/JSON/HTML proof
207
+ reports.
208
+
209
+ ## Enforcement Model
210
+
211
+ Validate at every boundary where another package, agent, process, or machine can
212
+ consume the object:
213
+
214
+ - CLI: fail with non-zero exit and structured JSON when `--json` is requested.
215
+ - SDK/API: reject invalid input before writing to storage or launching work.
216
+ - MCP/tool responses: validate outbound payloads before returning to the model.
217
+ - Events/webhooks: validate before publish and before handler execution.
218
+ - Files/artifacts: validate before using persisted JSON as evidence.
219
+ - CI/release: run `bun run verify:release` before publishing.
220
+
221
+ Invalid contracts should fail early at the boundary, not after another agent has
222
+ spent context trying to repair ambiguous data.
223
+
224
+ ## Versioning
225
+
226
+ Schema ids are immutable, and current wire schemas are strict about unknown
227
+ fields. Breaking changes create a new id, such as `hasna.proof_bundle.v2`.
228
+ Additive fields may extend the current version only after a coordinated
229
+ validator rollout reaches consumers that will receive those fields. If older
230
+ validators can still consume the object, keep emitting only the old field set.
231
+ If producers need to emit the new field before that rollout is complete, create
232
+ a new schema id instead.
233
+
234
+ Recommended rollout for a new major schema:
235
+
236
+ 1. Add the new schema and examples in this package.
237
+ 2. Keep the old schema exported while consumers migrate.
238
+ 3. Teach producers to emit the new schema behind a feature flag or option.
239
+ 4. Teach consumers to accept both versions when practical.
240
+ 5. Remove old-version production only after downstream repos have release notes
241
+ and migration tasks.
242
+
243
+ During dual-version windows, consumers should inspect the embedded `schema` via
244
+ `validateEmbeddedContract`, route accepted ids to version-specific adapters, and
245
+ reject unknown schema ids before writing data or launching work.
246
+
247
+ ## Examples
248
+
249
+ The `examples/` directory contains one valid fixture for every known schema plus
250
+ targeted invalid fixtures for important invariants. Keep fixtures small and
251
+ portable. Prefer `artifact://`, `repo://`, `task://`, or package-owned ids over
252
+ machine-local paths.
253
+
254
+ The conformance command intentionally treats unknown schemas, malformed JSON,
255
+ and empty fixture sets as harness failures. Invalid fixtures must fail because
256
+ the schema rejected them, not because the fixture cannot be parsed.
257
+
258
+ ## Verification
259
+
260
+ ```bash
261
+ bun run typecheck
262
+ bun test
263
+ bun run build
264
+ bun run smoke:dist
265
+ bun run verify:release
266
+ ```
267
+
268
+ `verify:release` runs typecheck, tests, build, and a smoke test against the
269
+ packaged CLI entrypoint in `dist/cli/index.js`.
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env bun
2
+ import { Command } from "commander";
3
+ export declare function createContractsProgram(): Command;
4
+ export declare function main(argv?: string[]): void;