reviewer-protocol 0.0.1 → 1.0.0-beta.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,194 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * The protocol's major version. Optional fields and failure kinds are added without raising
4
+ * it: every schema here ignores unknown fields and reads an unknown failure kind as `unknown`,
5
+ * so an older host keeps accepting a newer reviewer.
6
+ */
7
+ export declare const REVIEWER_PROTOCOL_VERSION = 1;
8
+ /**
9
+ * Why a turn failed, classified by the host's vocabulary: few vendors classify their own
10
+ * failures, and those that do disagree on the names. Timeouts, crashes and cancellations are
11
+ * not kinds: the host observes them itself, since no vendor reports them reliably.
12
+ */
13
+ export declare const FAILURE_KINDS: readonly ['authentication', 'quota', 'rate_limit', 'overloaded', 'context_overflow', 'max_output', 'refusal', 'model_not_found', 'transport', 'harness_error', 'unknown'];
14
+ export type FailureKind = (typeof FAILURE_KINDS)[number];
15
+ export declare const failureKindSchema: z.ZodCatch<z.ZodEnum<{
16
+ authentication: "authentication";
17
+ context_overflow: "context_overflow";
18
+ harness_error: "harness_error";
19
+ max_output: "max_output";
20
+ model_not_found: "model_not_found";
21
+ overloaded: "overloaded";
22
+ quota: "quota";
23
+ rate_limit: "rate_limit";
24
+ refusal: "refusal";
25
+ transport: "transport";
26
+ unknown: "unknown";
27
+ }>>;
28
+ export declare const failureSchema: z.ZodObject<{
29
+ kind: z.ZodCatch<z.ZodEnum<{
30
+ authentication: "authentication";
31
+ context_overflow: "context_overflow";
32
+ harness_error: "harness_error";
33
+ max_output: "max_output";
34
+ model_not_found: "model_not_found";
35
+ overloaded: "overloaded";
36
+ quota: "quota";
37
+ rate_limit: "rate_limit";
38
+ refusal: "refusal";
39
+ transport: "transport";
40
+ unknown: "unknown";
41
+ }>>;
42
+ message: z.ZodString;
43
+ retryAt: z.ZodOptional<z.ZodNumber>;
44
+ }, z.core.$strip>;
45
+ export type Failure = z.infer<typeof failureSchema>;
46
+ export declare const sessionEventSchema: z.ZodObject<{
47
+ type: z.ZodLiteral<"session">;
48
+ id: z.ZodString;
49
+ harness: z.ZodOptional<z.ZodObject<{
50
+ name: z.ZodString;
51
+ version: z.ZodOptional<z.ZodString>;
52
+ }, z.core.$strip>>;
53
+ model: z.ZodOptional<z.ZodString>;
54
+ }, z.core.$strip>;
55
+ export type SessionEvent = z.infer<typeof sessionEventSchema>;
56
+ export declare const PROGRESS_KINDS: readonly ['message', 'thought', 'tool_start', 'tool_end', 'model_call'];
57
+ export type ProgressKind = (typeof PROGRESS_KINDS)[number];
58
+ export declare const progressEventSchema: z.ZodObject<{
59
+ type: z.ZodLiteral<"progress">;
60
+ kind: z.ZodEnum<{
61
+ message: "message";
62
+ model_call: "model_call";
63
+ thought: "thought";
64
+ tool_end: "tool_end";
65
+ tool_start: "tool_start";
66
+ }>;
67
+ text: z.ZodOptional<z.ZodString>;
68
+ }, z.core.$strip>;
69
+ export type ProgressEvent = z.infer<typeof progressEventSchema>;
70
+ /**
71
+ * Tokens spent since the previous usage event, in the host's unified meaning: `input`
72
+ * excludes cache reads and writes, `output` includes reasoning, and `reasoning` is the part
73
+ * of `output` spent on reasoning. Tokens are authoritative; `costUsd` is a reference value,
74
+ * since few harnesses report it and some estimate it.
75
+ */
76
+ export declare const usageEventSchema: z.ZodObject<{
77
+ type: z.ZodLiteral<"usage">;
78
+ key: z.ZodString;
79
+ model: z.ZodOptional<z.ZodString>;
80
+ input: z.ZodNumber;
81
+ output: z.ZodNumber;
82
+ cacheRead: z.ZodNumber;
83
+ cacheWrite: z.ZodOptional<z.ZodNumber>;
84
+ reasoning: z.ZodOptional<z.ZodNumber>;
85
+ costUsd: z.ZodOptional<z.ZodNumber>;
86
+ servedBy: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
87
+ }, z.core.$strip>;
88
+ export type UsageEvent = z.infer<typeof usageEventSchema>;
89
+ export declare const LOG_LEVELS: readonly ['debug', 'info', 'warn', 'error'];
90
+ export declare const logEventSchema: z.ZodObject<{
91
+ type: z.ZodLiteral<"log">;
92
+ level: z.ZodEnum<{
93
+ debug: "debug";
94
+ error: "error";
95
+ info: "info";
96
+ warn: "warn";
97
+ }>;
98
+ text: z.ZodString;
99
+ }, z.core.$strip>;
100
+ export type LogEvent = z.infer<typeof logEventSchema>;
101
+ export declare const turnEndEventSchema: z.ZodObject<{
102
+ type: z.ZodLiteral<"turn_end">;
103
+ status: z.ZodEnum<{
104
+ completed: "completed";
105
+ failed: "failed";
106
+ }>;
107
+ stopReason: z.ZodOptional<z.ZodString>;
108
+ text: z.ZodOptional<z.ZodString>;
109
+ failure: z.ZodOptional<z.ZodObject<{
110
+ kind: z.ZodCatch<z.ZodEnum<{
111
+ authentication: "authentication";
112
+ context_overflow: "context_overflow";
113
+ harness_error: "harness_error";
114
+ max_output: "max_output";
115
+ model_not_found: "model_not_found";
116
+ overloaded: "overloaded";
117
+ quota: "quota";
118
+ rate_limit: "rate_limit";
119
+ refusal: "refusal";
120
+ transport: "transport";
121
+ unknown: "unknown";
122
+ }>>;
123
+ message: z.ZodString;
124
+ retryAt: z.ZodOptional<z.ZodNumber>;
125
+ }, z.core.$strip>>;
126
+ usageComplete: z.ZodBoolean;
127
+ }, z.core.$strip>;
128
+ export type TurnEndEvent = z.infer<typeof turnEndEventSchema>;
129
+ export declare const reviewerEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
130
+ type: z.ZodLiteral<"session">;
131
+ id: z.ZodString;
132
+ harness: z.ZodOptional<z.ZodObject<{
133
+ name: z.ZodString;
134
+ version: z.ZodOptional<z.ZodString>;
135
+ }, z.core.$strip>>;
136
+ model: z.ZodOptional<z.ZodString>;
137
+ }, z.core.$strip>, z.ZodObject<{
138
+ type: z.ZodLiteral<"progress">;
139
+ kind: z.ZodEnum<{
140
+ message: "message";
141
+ model_call: "model_call";
142
+ thought: "thought";
143
+ tool_end: "tool_end";
144
+ tool_start: "tool_start";
145
+ }>;
146
+ text: z.ZodOptional<z.ZodString>;
147
+ }, z.core.$strip>, z.ZodObject<{
148
+ type: z.ZodLiteral<"usage">;
149
+ key: z.ZodString;
150
+ model: z.ZodOptional<z.ZodString>;
151
+ input: z.ZodNumber;
152
+ output: z.ZodNumber;
153
+ cacheRead: z.ZodNumber;
154
+ cacheWrite: z.ZodOptional<z.ZodNumber>;
155
+ reasoning: z.ZodOptional<z.ZodNumber>;
156
+ costUsd: z.ZodOptional<z.ZodNumber>;
157
+ servedBy: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
158
+ }, z.core.$strip>, z.ZodObject<{
159
+ type: z.ZodLiteral<"log">;
160
+ level: z.ZodEnum<{
161
+ debug: "debug";
162
+ error: "error";
163
+ info: "info";
164
+ warn: "warn";
165
+ }>;
166
+ text: z.ZodString;
167
+ }, z.core.$strip>, z.ZodObject<{
168
+ type: z.ZodLiteral<"turn_end">;
169
+ status: z.ZodEnum<{
170
+ completed: "completed";
171
+ failed: "failed";
172
+ }>;
173
+ stopReason: z.ZodOptional<z.ZodString>;
174
+ text: z.ZodOptional<z.ZodString>;
175
+ failure: z.ZodOptional<z.ZodObject<{
176
+ kind: z.ZodCatch<z.ZodEnum<{
177
+ authentication: "authentication";
178
+ context_overflow: "context_overflow";
179
+ harness_error: "harness_error";
180
+ max_output: "max_output";
181
+ model_not_found: "model_not_found";
182
+ overloaded: "overloaded";
183
+ quota: "quota";
184
+ rate_limit: "rate_limit";
185
+ refusal: "refusal";
186
+ transport: "transport";
187
+ unknown: "unknown";
188
+ }>>;
189
+ message: z.ZodString;
190
+ retryAt: z.ZodOptional<z.ZodNumber>;
191
+ }, z.core.$strip>>;
192
+ usageComplete: z.ZodBoolean;
193
+ }, z.core.$strip>], "type">;
194
+ export type ReviewerEvent = z.infer<typeof reviewerEventSchema>;
package/dist/events.js ADDED
@@ -0,0 +1,95 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * The protocol's major version. Optional fields and failure kinds are added without raising
4
+ * it: every schema here ignores unknown fields and reads an unknown failure kind as `unknown`,
5
+ * so an older host keeps accepting a newer reviewer.
6
+ */
7
+ export const REVIEWER_PROTOCOL_VERSION = 1;
8
+ /**
9
+ * Why a turn failed, classified by the host's vocabulary: few vendors classify their own
10
+ * failures, and those that do disagree on the names. Timeouts, crashes and cancellations are
11
+ * not kinds: the host observes them itself, since no vendor reports them reliably.
12
+ */
13
+ export const FAILURE_KINDS = [
14
+ 'authentication',
15
+ 'quota',
16
+ 'rate_limit',
17
+ 'overloaded',
18
+ 'context_overflow',
19
+ 'max_output',
20
+ 'refusal',
21
+ 'model_not_found',
22
+ 'transport',
23
+ 'harness_error',
24
+ 'unknown',
25
+ ];
26
+ // oxlint-disable-next-line unicorn/prefer-top-level-await -- zod's `catch` supplies a fallback value; it is no promise chain.
27
+ export const failureKindSchema = z.enum(FAILURE_KINDS).catch('unknown');
28
+ export const failureSchema = z.object({
29
+ kind: failureKindSchema,
30
+ message: z.string(),
31
+ /** When the vendor says the request may be retried (epoch milliseconds). */
32
+ retryAt: z.number().int().nonnegative().optional(),
33
+ });
34
+ export const sessionEventSchema = z.object({
35
+ type: z.literal('session'),
36
+ /** The harness's own session id: what a later turn passes to continue this session. */
37
+ id: z.string().min(1),
38
+ harness: z.object({ name: z.string().min(1), version: z.string().optional() }).optional(),
39
+ model: z.string().optional(),
40
+ });
41
+ export const PROGRESS_KINDS = ['message', 'thought', 'tool_start', 'tool_end', 'model_call'];
42
+ export const progressEventSchema = z.object({
43
+ type: z.literal('progress'),
44
+ kind: z.enum(PROGRESS_KINDS),
45
+ text: z.string().optional(),
46
+ });
47
+ const tokenCountSchema = z.number().int().nonnegative();
48
+ /**
49
+ * Tokens spent since the previous usage event, in the host's unified meaning: `input`
50
+ * excludes cache reads and writes, `output` includes reasoning, and `reasoning` is the part
51
+ * of `output` spent on reasoning. Tokens are authoritative; `costUsd` is a reference value,
52
+ * since few harnesses report it and some estimate it.
53
+ */
54
+ export const usageEventSchema = z.object({
55
+ type: z.literal('usage'),
56
+ /** Identifies this report within its session, so a report delivered twice counts once. */
57
+ key: z.string().min(1),
58
+ model: z.string().optional(),
59
+ input: tokenCountSchema,
60
+ output: tokenCountSchema,
61
+ cacheRead: tokenCountSchema,
62
+ cacheWrite: tokenCountSchema.optional(),
63
+ reasoning: tokenCountSchema.optional(),
64
+ costUsd: z.number().nonnegative().optional(),
65
+ /**
66
+ * The model calls since the previous usage event that each upstream provider served, by the name the endpoint
67
+ * gave it: a gateway such as OpenRouter routes the same model to several providers, which may serve it differently.
68
+ * Absent when no endpoint named one.
69
+ */
70
+ servedBy: z.record(z.string().min(1), tokenCountSchema).optional(),
71
+ });
72
+ export const LOG_LEVELS = ['debug', 'info', 'warn', 'error'];
73
+ export const logEventSchema = z.object({
74
+ type: z.literal('log'),
75
+ level: z.enum(LOG_LEVELS),
76
+ text: z.string(),
77
+ });
78
+ export const turnEndEventSchema = z.object({
79
+ type: z.literal('turn_end'),
80
+ status: z.enum(['completed', 'failed']),
81
+ /** The harness's own stop reason, kept for diagnosis. */
82
+ stopReason: z.string().optional(),
83
+ /** The final assistant text of the turn. */
84
+ text: z.string().optional(),
85
+ failure: failureSchema.optional(),
86
+ /** False when some of the turn's usage is known to be unreported. */
87
+ usageComplete: z.boolean(),
88
+ });
89
+ export const reviewerEventSchema = z.discriminatedUnion('type', [
90
+ sessionEventSchema,
91
+ progressEventSchema,
92
+ usageEventSchema,
93
+ logEventSchema,
94
+ turnEndEventSchema,
95
+ ]);
@@ -0,0 +1,9 @@
1
+ export * from './acp.ts';
2
+ export * from './canonicalJson.ts';
3
+ export * from './events.ts';
4
+ export * from './jsonPointer.ts';
5
+ export * from './launch.ts';
6
+ export * from './manifest.ts';
7
+ export * from './mapping.ts';
8
+ export * from './pattern.ts';
9
+ export * from './template.ts';
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export * from './acp.js';
2
+ export * from './canonicalJson.js';
3
+ export * from './events.js';
4
+ export * from './jsonPointer.js';
5
+ export * from './launch.js';
6
+ export * from './manifest.js';
7
+ export * from './mapping.js';
8
+ export * from './pattern.js';
9
+ export * from './template.js';
@@ -0,0 +1,5 @@
1
+ import { z } from 'zod';
2
+ /** An RFC 6901 JSON Pointer: the empty string (the whole document) or `/`-prefixed reference tokens. */
3
+ export declare const jsonPointerSchema: z.ZodString;
4
+ /** The value the pointer names, or undefined when the document has none there. */
5
+ export declare function resolvePointer(document: unknown, pointer: string): unknown;
@@ -0,0 +1,24 @@
1
+ import { z } from 'zod';
2
+ /** An RFC 6901 JSON Pointer: the empty string (the whole document) or `/`-prefixed reference tokens. */
3
+ export const jsonPointerSchema = z.string().regex(/^(?:\/(?:[^~/]|~[01])*)*$/, 'not a JSON Pointer (RFC 6901)');
4
+ /** The value the pointer names, or undefined when the document has none there. */
5
+ export function resolvePointer(document, pointer) {
6
+ if (pointer === '')
7
+ return document;
8
+ let current = document;
9
+ for (const token of pointer.slice(1).split('/')) {
10
+ const key = token.replaceAll('~1', '/').replaceAll('~0', '~');
11
+ if (Array.isArray(current)) {
12
+ if (!/^(?:0|[1-9]\d*)$/.test(key))
13
+ return undefined;
14
+ current = current[Number(key)];
15
+ }
16
+ else if (current !== null && typeof current === 'object' && Object.hasOwn(current, key)) {
17
+ current = current[key];
18
+ }
19
+ else {
20
+ return undefined;
21
+ }
22
+ }
23
+ return current;
24
+ }
@@ -0,0 +1,44 @@
1
+ import type { Manifest, ManifestRole } from './manifest.ts';
2
+ import type { TemplateValues } from './template.ts';
3
+ /** Everything the host does to launch the harness: what a user is shown before trusting a manifest. */
4
+ export interface LaunchPlan {
5
+ command: string;
6
+ args: string[];
7
+ /** Text written to standard input, which is then closed. */
8
+ stdin?: string;
9
+ /** No variable is in both. */
10
+ env: {
11
+ set: Record<string, string>;
12
+ unset: string[];
13
+ };
14
+ /** Files written before the launch. */
15
+ files: {
16
+ path: string;
17
+ content: string;
18
+ }[];
19
+ /**
20
+ * The JSON file the harness's usage is read from once the process exits. The host removes it
21
+ * before the launch: a process that writes none would otherwise report the file an earlier
22
+ * process of the session left.
23
+ */
24
+ usageFilePath?: string;
25
+ /** What a sandboxed launch lets through; present only when the launch is sandboxed. */
26
+ sandbox?: {
27
+ passEnv: string[];
28
+ domains: string[];
29
+ };
30
+ }
31
+ export interface LaunchOptions {
32
+ role: ManifestRole;
33
+ values: TemplateValues;
34
+ platform: 'darwin' | 'linux';
35
+ /** Whether the host runs the harness in its sandbox: the manifest's `sandbox` then applies, and the launch throws without one. */
36
+ sandboxed?: boolean;
37
+ }
38
+ /**
39
+ * Resolves the manifest's templates into the launch of one process: the one that continues
40
+ * the session `values.sessionId` when the manifest can continue sessions and the id is
41
+ * given, else the one that starts a session. An ACP harness starts one server per session
42
+ * and takes its prompts over the protocol, so its plan carries no prompt.
43
+ */
44
+ export declare function resolveLaunch(manifest: Manifest, options: LaunchOptions): LaunchPlan;
package/dist/launch.js ADDED
@@ -0,0 +1,56 @@
1
+ import { renderArgs, renderTemplate } from './template.js';
2
+ /**
3
+ * Resolves the manifest's templates into the launch of one process: the one that continues
4
+ * the session `values.sessionId` when the manifest can continue sessions and the id is
5
+ * given, else the one that starts a session. An ACP harness starts one server per session
6
+ * and takes its prompts over the protocol, so its plan carries no prompt.
7
+ */
8
+ export function resolveLaunch(manifest, options) {
9
+ const { role, values, platform } = options;
10
+ const override = role === 'resolver' ? manifest.resolver : undefined;
11
+ const { transport } = manifest;
12
+ const resumes = transport.type === 'jsonl' && transport.resumeArgs !== undefined && values.sessionId !== undefined;
13
+ const baseArgs = resumes
14
+ ? (override?.resumeArgs ?? (transport.type === 'jsonl' ? transport.resumeArgs : undefined) ?? [])
15
+ : (override?.args ?? transport.args);
16
+ // The override's instruction for a variable replaces the manifest's opposite one.
17
+ const overrideSet = override?.env?.set ?? {};
18
+ const overrideUnset = override?.env?.unset ?? [];
19
+ const sandbox = options.sandboxed === true ? manifest.sandbox : undefined;
20
+ if (options.sandboxed === true && sandbox === undefined) {
21
+ throw new Error(`The manifest "${manifest.id}" declares no "sandbox", so its harness cannot run in a sandbox.`);
22
+ }
23
+ const set = {
24
+ ...Object.fromEntries(Object.entries(manifest.env?.set ?? {}).filter(([name]) => !overrideUnset.includes(name))),
25
+ ...overrideSet,
26
+ ...sandbox?.setEnv,
27
+ };
28
+ return {
29
+ command: manifest.command,
30
+ args: renderArgs([...baseArgs, ...(transport.platformArgs?.[platform] ?? [])], values),
31
+ ...(transport.type === 'jsonl' && transport.stdin !== undefined
32
+ ? { stdin: renderTemplate(transport.stdin, values) }
33
+ : {}),
34
+ env: {
35
+ set: Object.fromEntries(Object.entries(set).map(([name, value]) => [name, renderTemplate(value, values)])),
36
+ unset: [
37
+ ...new Set([
38
+ ...(manifest.env?.unset ?? []).filter((name) => !Object.hasOwn(overrideSet, name)),
39
+ ...overrideUnset,
40
+ ]),
41
+ ].filter((name) => !Object.hasOwn(sandbox?.setEnv ?? {}, name) &&
42
+ !(sandbox?.passEnv ?? []).includes(name) &&
43
+ !(sandbox?.signInEnv ?? []).includes(name)),
44
+ },
45
+ files: (manifest.files ?? []).map((file) => ({
46
+ path: renderTemplate(file.path, values),
47
+ content: renderTemplate(file.content, values),
48
+ })),
49
+ ...(transport.type === 'jsonl' && transport.usageFile !== undefined
50
+ ? { usageFilePath: renderTemplate(transport.usageFile.path, values) }
51
+ : {}),
52
+ ...(sandbox === undefined
53
+ ? {}
54
+ : { sandbox: { passEnv: [...(sandbox.passEnv ?? []), ...(sandbox.signInEnv ?? [])], domains: sandbox.domains } }),
55
+ };
56
+ }