-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalamedes_reference_consumer.ts
More file actions
297 lines (270 loc) · 7.77 KB
/
Copy pathpalamedes_reference_consumer.ts
File metadata and controls
297 lines (270 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
type PlanEnvelope = {
ok: boolean;
tool_name: string;
result_type: "plan";
plan: {
schema_version: string;
version: string;
goal: string;
success_metric: string;
deadline: string;
[key: string]: unknown;
};
fingerprint: string;
contract_version: string;
implementation_version: string;
};
type CycleEnvelope = {
ok: boolean;
result_type: "cycle";
plan: PlanEnvelope["plan"];
fingerprint: string;
contract_version: string;
implementation_version: string;
qa: {
result: string;
score: number;
};
health: {
status: string;
};
};
type ContractsEnvelope = {
ok: boolean;
result_type: "contracts";
contract_version: string;
contracts: {
host_action_contract?: {
capability_names?: string[];
[key: string]: unknown;
};
[key: string]: unknown;
};
[key: string]: unknown;
};
type ToolCatalogEnvelope = {
ok: boolean;
result_type: "tool_catalog";
catalog: {
authoritative: boolean;
execute_endpoint: string;
tool_count: number;
};
tools: Array<{
name: string;
kind: "read" | "mutation";
execute_via: {
generic: string;
legacy_wrapper: string;
};
[key: string]: unknown;
}>;
[key: string]: unknown;
};
type ToolExecuteEnvelope = {
tool: string;
input: Record<string, unknown>;
result: Record<string, unknown>;
};
type ErrorEnvelope = {
error: string;
type: string;
error_code: string;
retryable: boolean;
operation?: string;
step?: string;
current_fingerprint?: string;
[key: string]: unknown;
};
class PalamedesHttpError extends Error {
status: number;
payload: ErrorEnvelope;
constructor(status: number, payload: ErrorEnvelope) {
super(String(payload.error ?? `http_${status}`));
this.status = status;
this.payload = payload;
}
}
class PalamedesTsConsumer {
private readonly baseUrl: string;
constructor(baseUrl = "http://127.0.0.1:8787") {
this.baseUrl = baseUrl.replace(/\/$/, "");
}
private async request<T>(
path: string,
init?: RequestInit,
): Promise<{ payload: T; response: Response }> {
const response = await fetch(`${this.baseUrl}${path}`, init);
const payload = (await response.json()) as T;
if (!response.ok) {
throw new PalamedesHttpError(response.status, payload as ErrorEnvelope);
}
return { payload, response };
}
async getPlan(): Promise<{ envelope: PlanEnvelope; etag: string }> {
const { payload, response } = await this.request<PlanEnvelope>("/plan");
return { envelope: payload, etag: response.headers.get("etag") ?? "" };
}
async getCycle(limit = 5): Promise<CycleEnvelope> {
const { payload } = await this.request<CycleEnvelope>(`/cycle?limit=${limit}`);
return payload;
}
async getContracts(): Promise<ContractsEnvelope> {
const { payload } = await this.request<ContractsEnvelope>("/contracts");
return payload;
}
async getTools(): Promise<ToolCatalogEnvelope> {
const { payload } = await this.request<ToolCatalogEnvelope>("/tools");
return payload;
}
async updatePlan(payload: Record<string, unknown>, etag: string): Promise<PlanEnvelope> {
const { payload: result } = await this.request<PlanEnvelope>("/plan", {
method: "POST",
headers: {
"Content-Type": "application/json",
"If-Match": etag,
},
body: JSON.stringify(payload),
});
return result;
}
async executeTool(
tool: string,
input: Record<string, unknown>,
etag = "",
): Promise<ToolExecuteEnvelope> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (etag) {
headers["If-Match"] = etag;
}
const { payload } = await this.request<ToolExecuteEnvelope>("/tools/execute", {
method: "POST",
headers,
body: JSON.stringify({ tool, input }),
});
return payload;
}
async expectConflict(
payload: Record<string, unknown>,
etag: string,
): Promise<ErrorEnvelope> {
try {
await this.updatePlan(payload, etag);
} catch (error) {
if (error instanceof PalamedesHttpError) {
return error.payload;
}
throw error;
}
throw new Error("expected conflict but update succeeded");
}
async runSmoke(): Promise<Record<string, unknown>> {
const checks: Array<Record<string, unknown>> = [];
const { envelope: before, etag } = await this.getPlan();
checks.push({
name: "plan_envelope",
ok: before.result_type === "plan" && Boolean(before.fingerprint),
contract_version: before.contract_version,
});
const updated = await this.updatePlan(
{
goal: "TS reference consumer updated goal",
success_metric: "Reach 2 retained pilots",
deadline: "2026-05-01",
},
etag,
);
checks.push({
name: "etag_write",
ok: updated.plan.goal === "TS reference consumer updated goal" && updated.fingerprint !== before.fingerprint,
fingerprint_changed: updated.fingerprint !== before.fingerprint,
});
const cycle = await this.getCycle(3);
checks.push({
name: "cycle_snapshot",
ok: cycle.result_type === "cycle" && cycle.plan.goal === updated.plan.goal,
qa_result: cycle.qa.result,
health_status: cycle.health.status,
});
const conflict = await this.expectConflict({ goal: "stale write from ts" }, etag);
checks.push({
name: "stale_conflict",
ok:
conflict.error_code === "plan_fingerprint_mismatch" &&
conflict.retryable === true &&
Boolean(conflict.current_fingerprint),
error_code: conflict.error_code,
retryable: conflict.retryable,
operation: conflict.operation ?? "",
step: conflict.step ?? "",
});
const contracts = await this.getContracts();
const capabilityNames = contracts.contracts.host_action_contract?.capability_names ?? [];
checks.push({
name: "contracts_catalog",
ok: contracts.result_type === "contracts" && capabilityNames.includes("plan.write"),
capability_count: capabilityNames.length,
});
const tools = await this.getTools();
checks.push({
name: "tool_catalog",
ok:
tools.result_type === "tool_catalog" &&
tools.catalog.authoritative === true &&
tools.catalog.execute_endpoint === "/tools/execute" &&
tools.tools.some((tool) => tool.name === "request_review"),
tool_count: tools.catalog.tool_count,
});
return {
ok: checks.every((item) => Boolean(item.ok)),
runtime: "node_typescript_strip",
consumer: "palamedes_reference_consumer",
base_url: this.baseUrl,
checks,
final_goal: cycle.plan.goal,
contract_version: before.contract_version,
implementation_version: before.implementation_version,
};
}
}
function readFlag(name: string, fallback: string): string {
const args = process.argv.slice(2);
const index = args.indexOf(name);
if (index >= 0 && index + 1 < args.length) {
return args[index + 1] ?? fallback;
}
return fallback;
}
async function main(): Promise<void> {
const baseUrl = readFlag("--base-url", "http://127.0.0.1:8787");
const mode = readFlag("--mode", "smoke");
const client = new PalamedesTsConsumer(baseUrl);
if (mode !== "smoke") {
throw new Error(`unsupported mode: ${mode}`);
}
const report = await client.runSmoke();
console.log(JSON.stringify(report, null, 2));
}
void main().catch((error: unknown) => {
if (error instanceof PalamedesHttpError) {
console.error(
JSON.stringify(
{
ok: false,
runtime: "node_typescript_strip",
consumer: "palamedes_reference_consumer",
status: error.status,
payload: error.payload,
},
null,
2,
),
);
process.exitCode = 1;
return;
}
console.error(error);
process.exitCode = 1;
});