agent-contracts-runtime 0.1.0 → 0.3.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,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
+ };