agent-contracts-runtime 0.0.3 → 0.2.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.
@@ -0,0 +1,153 @@
1
+ import { z } from 'zod';
2
+
3
+ /**
4
+ * Generator configuration — loads and validates agent-runtime.config.yaml
5
+ */
6
+
7
+ declare const RuntimeConfigSchema: z.ZodObject<{
8
+ dsl: z.ZodDefault<z.ZodString>;
9
+ generated_dir: z.ZodDefault<z.ZodString>;
10
+ bindings: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
11
+ active_guardrail_policy: z.ZodOptional<z.ZodString>;
12
+ templates_dir: z.ZodOptional<z.ZodString>;
13
+ }, "strip", z.ZodTypeAny, {
14
+ dsl: string;
15
+ generated_dir: string;
16
+ bindings: string[];
17
+ active_guardrail_policy?: string | undefined;
18
+ templates_dir?: string | undefined;
19
+ }, {
20
+ dsl?: string | undefined;
21
+ generated_dir?: string | undefined;
22
+ bindings?: string[] | undefined;
23
+ active_guardrail_policy?: string | undefined;
24
+ templates_dir?: string | undefined;
25
+ }>;
26
+ type RuntimeConfig = z.infer<typeof RuntimeConfigSchema>;
27
+ interface ResolvedConfig extends RuntimeConfig {
28
+ configDir: string;
29
+ dslPath: string;
30
+ generatedDir: string;
31
+ bindingPaths: string[];
32
+ templatesDir: string | undefined;
33
+ }
34
+ declare function loadConfig(configPath: string): Promise<ResolvedConfig>;
35
+
36
+ /**
37
+ * Build template contexts from resolved DSL data
38
+ */
39
+ interface AgentEntry {
40
+ id: string;
41
+ varName: string;
42
+ role_name: string;
43
+ purpose: string;
44
+ mode: string;
45
+ dispatch_only: boolean;
46
+ can_read_artifacts: string[];
47
+ can_write_artifacts: string[];
48
+ can_execute_tools: string[];
49
+ can_invoke_agents: string[];
50
+ can_return_handoffs: string[];
51
+ responsibilities: string[];
52
+ constraints: string[];
53
+ rules: Array<{
54
+ id: string;
55
+ description: string;
56
+ severity: string;
57
+ }>;
58
+ escalation_criteria: Array<{
59
+ condition: string;
60
+ action: string;
61
+ }>;
62
+ }
63
+ interface TaskEntry {
64
+ id: string;
65
+ varName: string;
66
+ description: string;
67
+ target_agent: string;
68
+ allowed_from_agents: string[];
69
+ workflow: string;
70
+ invocation_handoff: string;
71
+ result_handoff: string;
72
+ input_artifacts: string[];
73
+ responsibilities: string[];
74
+ completion_criteria: string[];
75
+ optional: boolean;
76
+ }
77
+ interface WorkflowStepEntry {
78
+ type: string;
79
+ task?: string;
80
+ from_agent?: string;
81
+ description?: string;
82
+ optional?: boolean;
83
+ max_retries?: number;
84
+ gate_kind?: string;
85
+ }
86
+ interface WorkflowEntry {
87
+ id: string;
88
+ varName: string;
89
+ description: string;
90
+ trigger: string;
91
+ entry_conditions: string[];
92
+ steps: WorkflowStepEntry[];
93
+ }
94
+ interface HandoffEntry {
95
+ id: string;
96
+ varName: string;
97
+ pascalName: string;
98
+ schemaName: string;
99
+ description: string;
100
+ schema: Record<string, unknown>;
101
+ }
102
+ interface ContractContext {
103
+ agentEntries: AgentEntry[];
104
+ taskEntries: TaskEntry[];
105
+ workflowEntries: WorkflowEntry[];
106
+ handoffEntries: HandoffEntry[];
107
+ }
108
+ declare function buildContractContext(dsl: any): ContractContext;
109
+ interface GuardrailCheckEntry {
110
+ guardrail_id: string;
111
+ description: string;
112
+ severity: string;
113
+ action: string;
114
+ matcher_type: string;
115
+ pattern: string;
116
+ message: string;
117
+ file_glob?: string;
118
+ exclude_glob?: string;
119
+ }
120
+ interface GuardrailContext {
121
+ commandChecks: GuardrailCheckEntry[];
122
+ fileChecks: GuardrailCheckEntry[];
123
+ contentChecks: GuardrailCheckEntry[];
124
+ allChecks: GuardrailCheckEntry[];
125
+ hasGuardrails: boolean;
126
+ }
127
+ /**
128
+ * Build guardrail context from DSL guardrails, binding guardrail_impl, and active policy.
129
+ * Mirrors the agent-contracts resolveChecks pattern.
130
+ */
131
+ declare function buildGuardrailContext(dsl: any, binding: any, policyName?: string): GuardrailContext;
132
+
133
+ /**
134
+ * Generator — orchestrates DSL-to-TypeScript code generation
135
+ */
136
+ interface GenerateOptions {
137
+ clean?: boolean;
138
+ check?: boolean;
139
+ templates?: string;
140
+ configPath?: string;
141
+ }
142
+ interface GenerateResult {
143
+ filesGenerated: string[];
144
+ dslHash: string;
145
+ generationInputHash: string;
146
+ cleaned: boolean;
147
+ checkMode: boolean;
148
+ hasDifferences: boolean;
149
+ }
150
+ declare function generate(options?: GenerateOptions): Promise<GenerateResult>;
151
+ declare function checkFreshness(configPath?: string): Promise<boolean>;
152
+
153
+ export { type GenerateOptions, type GenerateResult, type ResolvedConfig, buildContractContext, buildGuardrailContext, checkFreshness, generate, loadConfig };
@@ -0,0 +1,15 @@
1
+ import {
2
+ buildContractContext,
3
+ buildGuardrailContext,
4
+ checkFreshness,
5
+ generate,
6
+ loadConfig
7
+ } from "../chunk-DNTCPVJN.js";
8
+ export {
9
+ buildContractContext,
10
+ buildGuardrailContext,
11
+ checkFreshness,
12
+ generate,
13
+ loadConfig
14
+ };
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-contracts-runtime",
3
- "version": "0.0.3",
3
+ "version": "0.2.0",
4
4
  "description": "Runtime bridge for executing agent-contracts workflows on Agent SDKs",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -28,6 +28,10 @@
28
28
  "./adapters/mock": {
29
29
  "types": "./dist/adapters/mock.d.ts",
30
30
  "import": "./dist/adapters/mock.js"
31
+ },
32
+ "./generator": {
33
+ "types": "./dist/generator/index.d.ts",
34
+ "import": "./dist/generator/index.js"
31
35
  }
32
36
  },
33
37
  "files": [
@@ -80,6 +84,7 @@
80
84
  "dependencies": {
81
85
  "commander": "^12.1.0",
82
86
  "dotenv": "^17.4.2",
87
+ "handlebars": "^4.7.9",
83
88
  "yaml": "^2.8.3",
84
89
  "zod": "^3.24.4"
85
90
  },
@@ -0,0 +1,54 @@
1
+ export interface AgentRule {
2
+ readonly id: string;
3
+ readonly description: string;
4
+ readonly severity: "mandatory" | "recommended" | "optional";
5
+ }
6
+
7
+ export interface EscalationCriterion {
8
+ readonly condition: string;
9
+ readonly action: "stop_and_report" | "report_to_architect" | "wait_for_approval";
10
+ }
11
+
12
+ export interface AgentContract {
13
+ readonly id: string;
14
+ readonly role_name: string;
15
+ readonly purpose: string;
16
+ readonly mode: "read-write" | "read-only";
17
+ readonly dispatch_only: boolean;
18
+ readonly can_read_artifacts: readonly string[];
19
+ readonly can_write_artifacts: readonly string[];
20
+ readonly can_execute_tools: readonly string[];
21
+ readonly can_invoke_agents: readonly string[];
22
+ readonly can_return_handoffs: readonly string[];
23
+ readonly responsibilities: readonly string[];
24
+ readonly constraints: readonly string[];
25
+ readonly rules: readonly AgentRule[];
26
+ readonly escalation_criteria: readonly EscalationCriterion[];
27
+ }
28
+
29
+ {{#each agentEntries}}
30
+ export const {{varName}}: AgentContract = {
31
+ id: "{{id}}",
32
+ role_name: "{{role_name}}",
33
+ purpose: {{{json purpose}}},
34
+ mode: "{{mode}}",
35
+ dispatch_only: {{dispatch_only}},
36
+ can_read_artifacts: {{{json can_read_artifacts}}},
37
+ can_write_artifacts: {{{json can_write_artifacts}}},
38
+ can_execute_tools: {{{json can_execute_tools}}},
39
+ can_invoke_agents: {{{json can_invoke_agents}}},
40
+ can_return_handoffs: {{{json can_return_handoffs}}},
41
+ responsibilities: {{{json responsibilities}}},
42
+ constraints: {{{json constraints}}},
43
+ rules: {{{json rules}}},
44
+ escalation_criteria: {{{json escalation_criteria}}},
45
+ } as const;
46
+
47
+ {{/each}}
48
+ export const agentRegistry: Record<string, AgentContract> = {
49
+ {{#each agentEntries}}
50
+ "{{id}}": {{varName}},
51
+ {{/each}}
52
+ } as const;
53
+
54
+ export type AgentId = keyof typeof agentRegistry;
@@ -0,0 +1,4 @@
1
+ export * as agents from "./agents.js";
2
+ export * as handoffs from "./handoffs.js";
3
+ export * as tasks from "./tasks.js";
4
+ export * as workflows from "./workflows.js";
@@ -0,0 +1,132 @@
1
+ export type GuardrailAction = "block" | "warn" | "shadow" | "info";
2
+
3
+ export interface GuardrailCheckResult {
4
+ guardrail_id: string;
5
+ passed: boolean;
6
+ action: GuardrailAction;
7
+ message: string;
8
+ }
9
+
10
+ {{#if (notEmpty commandChecks)}}
11
+ const COMMAND_CHECKS = [
12
+ {{#each commandChecks}}
13
+ {
14
+ guardrail_id: "{{guardrail_id}}",
15
+ pattern: new RegExp({{{json pattern}}}),
16
+ action: "{{action}}" as GuardrailAction,
17
+ message: {{{json message}}},
18
+ },
19
+ {{/each}}
20
+ ] as const;
21
+
22
+ export function checkCommand(command: string): GuardrailCheckResult[] {
23
+ const results: GuardrailCheckResult[] = [];
24
+ for (const check of COMMAND_CHECKS) {
25
+ const passed = !check.pattern.test(command);
26
+ results.push({
27
+ guardrail_id: check.guardrail_id,
28
+ passed,
29
+ action: check.action,
30
+ message: passed ? "" : check.message,
31
+ });
32
+ }
33
+ return results;
34
+ }
35
+ {{else}}
36
+ export function checkCommand(_command: string): GuardrailCheckResult[] {
37
+ return [];
38
+ }
39
+ {{/if}}
40
+
41
+ {{#if (notEmpty fileChecks)}}
42
+ const FILE_CHECKS = [
43
+ {{#each fileChecks}}
44
+ {
45
+ guardrail_id: "{{guardrail_id}}",
46
+ pattern: {{{json pattern}}},
47
+ action: "{{action}}" as GuardrailAction,
48
+ message: {{{json message}}},
49
+ {{#if exclude_glob}}
50
+ exclude: {{{json exclude_glob}}},
51
+ {{/if}}
52
+ },
53
+ {{/each}}
54
+ ] as const;
55
+
56
+ function matchGlob(pattern: string, path: string): boolean {
57
+ const regex = new RegExp(
58
+ "^" +
59
+ pattern
60
+ .replace(/\*\*/g, "{{GLOBSTAR}}")
61
+ .replace(/\*/g, "[^/]*")
62
+ .replace(/\{\{GLOBSTAR\}\}/g, ".*")
63
+ .replace(/\?/g, "[^/]") +
64
+ "$",
65
+ );
66
+ return regex.test(path);
67
+ }
68
+
69
+ export function checkFilePath(filePath: string): GuardrailCheckResult[] {
70
+ const results: GuardrailCheckResult[] = [];
71
+ for (const check of FILE_CHECKS) {
72
+ if ("exclude" in check && check.exclude && matchGlob(check.exclude, filePath)) {
73
+ continue;
74
+ }
75
+ const matched = matchGlob(check.pattern, filePath);
76
+ results.push({
77
+ guardrail_id: check.guardrail_id,
78
+ passed: !matched,
79
+ action: check.action,
80
+ message: matched ? check.message : "",
81
+ });
82
+ }
83
+ return results;
84
+ }
85
+ {{else}}
86
+ export function checkFilePath(_filePath: string): GuardrailCheckResult[] {
87
+ return [];
88
+ }
89
+ {{/if}}
90
+
91
+ {{#if (notEmpty contentChecks)}}
92
+ const CONTENT_CHECKS = [
93
+ {{#each contentChecks}}
94
+ {
95
+ guardrail_id: "{{guardrail_id}}",
96
+ pattern: new RegExp({{{json pattern}}}),
97
+ action: "{{action}}" as GuardrailAction,
98
+ message: {{{json message}}},
99
+ {{#if file_glob}}
100
+ file_glob: {{{json file_glob}}},
101
+ {{/if}}
102
+ {{#if exclude_glob}}
103
+ exclude_glob: {{{json exclude_glob}}},
104
+ {{/if}}
105
+ },
106
+ {{/each}}
107
+ ] as const;
108
+
109
+ export function checkContent(filePath: string, content: string): GuardrailCheckResult[] {
110
+ const results: GuardrailCheckResult[] = [];
111
+ for (const check of CONTENT_CHECKS) {
112
+ if ("file_glob" in check && check.file_glob && !matchGlob(check.file_glob, filePath)) {
113
+ continue;
114
+ }
115
+ if ("exclude_glob" in check && check.exclude_glob && matchGlob(check.exclude_glob, filePath)) {
116
+ continue;
117
+ }
118
+ const matched = check.pattern.test(content);
119
+ results.push({
120
+ guardrail_id: check.guardrail_id,
121
+ passed: !matched,
122
+ action: check.action,
123
+ message: matched ? check.message : "",
124
+ });
125
+ }
126
+ return results;
127
+ }
128
+ {{else}}
129
+ export function checkContent(_filePath: string, _content: string): GuardrailCheckResult[] {
130
+ return [];
131
+ }
132
+ {{/if}}
@@ -0,0 +1,19 @@
1
+ import { z } from "zod";
2
+
3
+ {{#each handoffEntries}}
4
+ // ---------------------------------------------------------------------------
5
+ // {{id}}
6
+ // ---------------------------------------------------------------------------
7
+
8
+ export const {{schemaName}} = {{{indent (zodType schema) 0}}};
9
+
10
+ export type {{pascalName}} = z.infer<typeof {{schemaName}}>;
11
+
12
+ {{/each}}
13
+ export const handoffSchemas = {
14
+ {{#each handoffEntries}}
15
+ "{{id}}": {{schemaName}},
16
+ {{/each}}
17
+ } as const;
18
+
19
+ export type HandoffTypeId = keyof typeof handoffSchemas;
@@ -0,0 +1,31 @@
1
+ import { checkCommand, checkFilePath, checkContent, type GuardrailCheckResult } from "./guardrails.js";
2
+
3
+ export { type GuardrailCheckResult } from "./guardrails.js";
4
+
5
+ export interface HookContext {
6
+ command?: string;
7
+ filePath?: string;
8
+ content?: string;
9
+ }
10
+
11
+ export function runGuardrailChecks(ctx: HookContext): GuardrailCheckResult[] {
12
+ const results: GuardrailCheckResult[] = [];
13
+
14
+ if (ctx.command) {
15
+ results.push(...checkCommand(ctx.command));
16
+ }
17
+
18
+ if (ctx.filePath) {
19
+ results.push(...checkFilePath(ctx.filePath));
20
+ }
21
+
22
+ if (ctx.filePath && ctx.content) {
23
+ results.push(...checkContent(ctx.filePath, ctx.content));
24
+ }
25
+
26
+ return results;
27
+ }
28
+
29
+ export function hasBlockingViolation(results: GuardrailCheckResult[]): boolean {
30
+ return results.some((r) => !r.passed && r.action === "block");
31
+ }
@@ -0,0 +1,38 @@
1
+ import type { AgentId } from "./agents.js";
2
+ import type { HandoffTypeId } from "./handoffs.js";
3
+
4
+ export interface TaskContract {
5
+ readonly id: string;
6
+ readonly description: string;
7
+ readonly target_agent: AgentId;
8
+ readonly allowed_from_agents: readonly AgentId[];
9
+ readonly workflow: string;
10
+ readonly invocation_handoff: HandoffTypeId;
11
+ readonly result_handoff: HandoffTypeId;
12
+ readonly input_artifacts: readonly string[];
13
+ readonly responsibilities: readonly string[];
14
+ readonly completion_criteria: readonly string[];
15
+ readonly optional: boolean;
16
+ }
17
+
18
+ {{#each taskEntries}}
19
+ export const {{varName}}: TaskContract = {
20
+ id: "{{id}}",
21
+ description: {{{json description}}},
22
+ target_agent: "{{target_agent}}",
23
+ allowed_from_agents: {{{json allowed_from_agents}}},
24
+ workflow: "{{workflow}}",
25
+ invocation_handoff: "{{invocation_handoff}}",
26
+ result_handoff: "{{result_handoff}}",
27
+ input_artifacts: {{{json input_artifacts}}},
28
+ responsibilities: {{{json responsibilities}}},
29
+ completion_criteria: {{{json completion_criteria}}},
30
+ optional: {{optional}},
31
+ };
32
+
33
+ {{/each}}
34
+ export const taskRegistry: Record<string, TaskContract> = {
35
+ {{#each taskEntries}}
36
+ "{{id}}": {{varName}},
37
+ {{/each}}
38
+ };
@@ -0,0 +1,60 @@
1
+ export interface DelegateStep {
2
+ readonly type: "delegate";
3
+ readonly task: string;
4
+ readonly from_agent: string;
5
+ readonly description: string;
6
+ readonly optional: boolean;
7
+ readonly max_retries: number;
8
+ }
9
+
10
+ export interface GateStep {
11
+ readonly type: "gate";
12
+ readonly gate_kind: string;
13
+ readonly description: string;
14
+ }
15
+
16
+ export type WorkflowStep = DelegateStep | GateStep;
17
+
18
+ export interface WorkflowContract {
19
+ readonly id: string;
20
+ readonly description: string;
21
+ readonly trigger: string;
22
+ readonly entry_conditions: readonly string[];
23
+ readonly steps: readonly WorkflowStep[];
24
+ }
25
+
26
+ {{#each workflowEntries}}
27
+ export const {{varName}}: WorkflowContract = {
28
+ id: "{{id}}",
29
+ description: {{{json description}}},
30
+ trigger: {{{json trigger}}},
31
+ entry_conditions: {{{json entry_conditions}}},
32
+ steps: [
33
+ {{#each steps}}
34
+ {{#if (eq type "delegate")}}
35
+ {
36
+ type: "delegate",
37
+ task: "{{task}}",
38
+ from_agent: "{{from_agent}}",
39
+ description: {{{json description}}},
40
+ optional: {{optional}},
41
+ max_retries: {{max_retries}},
42
+ },
43
+ {{/if}}
44
+ {{#if (eq type "gate")}}
45
+ {
46
+ type: "gate",
47
+ gate_kind: "{{gate_kind}}",
48
+ description: {{{json description}}},
49
+ },
50
+ {{/if}}
51
+ {{/each}}
52
+ ],
53
+ };
54
+
55
+ {{/each}}
56
+ export const workflowRegistry: Record<string, WorkflowContract> = {
57
+ {{#each workflowEntries}}
58
+ "{{id}}": {{varName}},
59
+ {{/each}}
60
+ };