@ftrdotdev/pi-delegates 0.0.0-stage → 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/CHANGELOG.md +77 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +57 -0
- package/README.md +448 -2
- package/ROADMAP.md +158 -0
- package/TESTING.md +1061 -0
- package/agents.ts +8 -0
- package/examples/README.md +114 -0
- package/examples/gated.ts +28 -0
- package/index.ts +9 -0
- package/package.json +93 -4
- package/skills/pi-delegates/SKILL.md +147 -0
- package/skills/pi-delegates/references/control.md +159 -0
- package/skills/pi-delegates/references/definitions.md +163 -0
- package/skills/pi-delegates/references/gates.md +175 -0
- package/skills/pi-delegates/references/launch.md +156 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +161 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1298 -0
- package/src/domain/orchestration/control.ts +436 -0
- package/src/domain/orchestration/fanout.test.ts +419 -0
- package/src/domain/orchestration/fanout.ts +334 -0
- package/src/domain/orchestration/gates.test.ts +686 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +173 -0
- package/src/domain/orchestration/launch.ts +581 -0
- package/src/domain/orchestration/requests.test.ts +700 -0
- package/src/domain/orchestration/requests.ts +769 -0
- package/src/domain/orchestration/review.test.ts +214 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +262 -0
- package/src/domain/orchestration/run-wait.ts +256 -0
- package/src/domain/orchestration/workflow-section.test.ts +62 -0
- package/src/domain/orchestration/workflow-section.ts +99 -0
- package/src/domain/orchestration/workflow.test.ts +1425 -0
- package/src/domain/orchestration/workflow.ts +705 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +527 -0
- package/src/domain/shapes.test.ts +165 -0
- package/src/domain/skills.test.ts +466 -0
- package/src/domain/skills.ts +97 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +201 -0
- package/src/domain/toolkit.ts +379 -0
- package/src/domain/view.test.ts +739 -0
- package/src/domain/view.ts +459 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +481 -0
- package/src/extension/commands.ts +227 -0
- package/src/extension/entry.test.ts +1118 -0
- package/src/extension/entry.ts +342 -0
- package/src/extension/gate-alerts.test.ts +235 -0
- package/src/extension/gate-alerts.ts +143 -0
- package/src/extension/guard.test.ts +48 -0
- package/src/extension/guard.ts +31 -0
- package/src/extension/host-signals.test.ts +20 -0
- package/src/extension/host-signals.ts +13 -0
- package/src/extension/inspector-command.test.ts +1191 -0
- package/src/extension/inspector-command.ts +638 -0
- package/src/extension/prompt.test.ts +110 -0
- package/src/extension/prompt.ts +50 -0
- package/src/extension/request-alerts.test.ts +183 -0
- package/src/extension/request-alerts.ts +181 -0
- package/src/extension/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/shortcut.test.ts +64 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +993 -0
- package/src/extension/tools.ts +505 -0
- package/src/extension/version.test.ts +123 -0
- package/src/extension/version.ts +97 -0
- package/src/harness-policy.test.ts +74 -0
- package/src/package-manifest.test.ts +157 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +387 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +361 -0
- package/src/presentation/inspector.test.ts +679 -0
- package/src/presentation/inspector.ts +440 -0
- package/src/presentation/overlay.test.ts +555 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +329 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +530 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/strip.test.ts +196 -0
- package/src/presentation/strip.ts +74 -0
- package/src/presentation/wording.test.ts +238 -0
- package/src/presentation/wording.ts +222 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +134 -0
- package/src/runtime/config.test.ts +44 -0
- package/src/runtime/config.ts +87 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +35 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +140 -0
- package/src/runtime/log.test.ts +128 -0
- package/src/runtime/log.ts +68 -0
- package/src/runtime/mcp.test.ts +1453 -0
- package/src/runtime/mcp.ts +938 -0
- package/src/runtime/models.test.ts +145 -0
- package/src/runtime/models.ts +259 -0
- package/src/runtime/observe.test.ts +50 -0
- package/src/runtime/observe.ts +150 -0
- package/src/runtime/session.test.ts +359 -0
- package/src/runtime/session.ts +425 -0
- package/src/runtime/timers.test.ts +31 -0
- package/src/runtime/timers.ts +10 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { mkdtemp, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { assert, it } from "@effect/vitest";
|
|
6
|
+
import type { Report } from "@earendil-works/pi-durable";
|
|
7
|
+
import {
|
|
8
|
+
fileLogSink,
|
|
9
|
+
logReport,
|
|
10
|
+
reasonOf,
|
|
11
|
+
truncate,
|
|
12
|
+
type LogFields,
|
|
13
|
+
type LogLevel,
|
|
14
|
+
type LogSink,
|
|
15
|
+
} from "./log.ts";
|
|
16
|
+
|
|
17
|
+
const scratch = () => mkdtemp(join(tmpdir(), "delegates-log-"));
|
|
18
|
+
|
|
19
|
+
it("the file sink appends one JSON object per line", async () => {
|
|
20
|
+
const path = join(await scratch(), "delegates.log");
|
|
21
|
+
const sink = fileLogSink(path);
|
|
22
|
+
sink.write("info", "a.one", { taskId: 7 });
|
|
23
|
+
sink.write("warn", "b.two");
|
|
24
|
+
|
|
25
|
+
const lines = readFileSync(path, "utf8").trim().split("\n");
|
|
26
|
+
assert.strictEqual(lines.length, 2);
|
|
27
|
+
const first = JSON.parse(lines[0] ?? "") as Record<string, unknown>;
|
|
28
|
+
assert.strictEqual(first.level, "info");
|
|
29
|
+
assert.strictEqual(first.code, "a.one");
|
|
30
|
+
assert.strictEqual(first.taskId, 7);
|
|
31
|
+
assert.strictEqual(typeof first.ts, "number");
|
|
32
|
+
assert.strictEqual((JSON.parse(lines[1] ?? "") as Record<string, unknown>).code, "b.two");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("the file sink never throws when its path cannot be written", async () => {
|
|
36
|
+
const blocker = join(await scratch(), "blocker");
|
|
37
|
+
await writeFile(blocker, "not a directory");
|
|
38
|
+
const sink = fileLogSink(join(blocker, "delegates.log"));
|
|
39
|
+
assert.doesNotThrow(() => sink.write("error", "broken.write"));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("truncate keeps at most its maximum and reasonOf reads a message", () => {
|
|
43
|
+
assert.strictEqual(truncate("x".repeat(5000), 200).length, 200);
|
|
44
|
+
assert.strictEqual(truncate("short", 200), "short");
|
|
45
|
+
assert.strictEqual(reasonOf(new Error("boom")), "boom");
|
|
46
|
+
assert.strictEqual(reasonOf("text"), "text");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const reportTask = { id: 7 as never, kind: "delegates.run", conversationId: 3 as never };
|
|
50
|
+
const reportError = new Error("boom");
|
|
51
|
+
|
|
52
|
+
const reportFixtures: ReadonlyArray<Report> = [
|
|
53
|
+
{ kind: "storage_failed", error: reportError },
|
|
54
|
+
{ kind: "task_faulted", task: reportTask, reason: "threw", error: reportError },
|
|
55
|
+
{
|
|
56
|
+
kind: "task_interrupted",
|
|
57
|
+
task: reportTask,
|
|
58
|
+
reason: "interrupted",
|
|
59
|
+
restarts: 2,
|
|
60
|
+
error: reportError,
|
|
61
|
+
},
|
|
62
|
+
{ kind: "migration_failed", task: reportTask, error: reportError },
|
|
63
|
+
{ kind: "task_outdated", task: reportTask, reason: "missing_task", error: reportError },
|
|
64
|
+
{ kind: "task_error", task: reportTask, error: reportError },
|
|
65
|
+
{ kind: "extension_error", task: reportTask, error: reportError },
|
|
66
|
+
{ kind: "host_error", callback: "now", task: reportTask, error: reportError },
|
|
67
|
+
{ kind: "listener_error", error: reportError },
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
const tasksInReports = new Set([
|
|
71
|
+
"task_faulted",
|
|
72
|
+
"task_interrupted",
|
|
73
|
+
"migration_failed",
|
|
74
|
+
"task_outdated",
|
|
75
|
+
"task_error",
|
|
76
|
+
"extension_error",
|
|
77
|
+
"host_error",
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
const recordingSink = () => {
|
|
81
|
+
const entries: { level: LogLevel; code: string; fields: LogFields }[] = [];
|
|
82
|
+
const sink: LogSink = {
|
|
83
|
+
write: (level, code, fields = {}) => {
|
|
84
|
+
entries.push({ level, code, fields });
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
return { sink, entries };
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
it("the report mapper logs every kind without task content", () => {
|
|
91
|
+
const { sink, entries } = recordingSink();
|
|
92
|
+
for (const report of reportFixtures) logReport(sink, report);
|
|
93
|
+
|
|
94
|
+
assert.deepStrictEqual(
|
|
95
|
+
entries.map((entry) => entry.code),
|
|
96
|
+
reportFixtures.map((report) => `report.${report.kind}`),
|
|
97
|
+
);
|
|
98
|
+
for (const [index, entry] of entries.entries()) {
|
|
99
|
+
const kind = reportFixtures[index]!.kind;
|
|
100
|
+
assert.strictEqual(entry.level, errorLevels.has(kind) ? "error" : "warn");
|
|
101
|
+
assert.strictEqual(entry.fields.error, "Error");
|
|
102
|
+
assert.strictEqual(entry.fields.detail, "boom");
|
|
103
|
+
if (tasksInReports.has(kind)) assert.strictEqual(entry.fields.taskId, 7);
|
|
104
|
+
for (const forbidden of ["task", "answer", "prompt", "command"]) {
|
|
105
|
+
assert.ok(!(forbidden in entry.fields), `${kind} logged ${forbidden}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const errorLevels = new Set([
|
|
111
|
+
"storage_failed",
|
|
112
|
+
"task_faulted",
|
|
113
|
+
"task_interrupted",
|
|
114
|
+
"migration_failed",
|
|
115
|
+
"task_outdated",
|
|
116
|
+
"task_error",
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
it("an error message is truncated, not dropped", () => {
|
|
120
|
+
const { sink, entries } = recordingSink();
|
|
121
|
+
logReport(sink, { kind: "task_error", task: reportTask, error: new Error("x".repeat(5000)) });
|
|
122
|
+
|
|
123
|
+
const entry = entries[0];
|
|
124
|
+
assert.ok(entry !== undefined);
|
|
125
|
+
assert.strictEqual(typeof entry.fields.detail, "string");
|
|
126
|
+
assert.strictEqual((entry.fields.detail as string).length, 200);
|
|
127
|
+
assert.deepStrictEqual(Object.keys(entry.fields).sort(), ["detail", "error", "taskId"]);
|
|
128
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Report } from "@earendil-works/pi-durable";
|
|
2
|
+
import { appendFileSync } from "node:fs";
|
|
3
|
+
|
|
4
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
5
|
+
export type LogFields = Record<string, string | number | boolean | null | undefined>;
|
|
6
|
+
|
|
7
|
+
export interface LogSink {
|
|
8
|
+
write(level: LogLevel, code: string, fields?: LogFields): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const nullLogSink: LogSink = { write: () => {} };
|
|
12
|
+
|
|
13
|
+
/** `detail` is display-only: it is never parsed and never classifies state. */
|
|
14
|
+
export const DETAIL_LIMIT = 200;
|
|
15
|
+
|
|
16
|
+
export const truncate = (text: string, max: number): string =>
|
|
17
|
+
text.length <= max ? text : text.slice(0, max);
|
|
18
|
+
|
|
19
|
+
export const reasonOf = (error: unknown): string =>
|
|
20
|
+
error instanceof Error ? error.message : String(error);
|
|
21
|
+
|
|
22
|
+
export const errorName = (error: unknown): string =>
|
|
23
|
+
error instanceof Error ? error.name : typeof error;
|
|
24
|
+
|
|
25
|
+
/** The two fields every failure entry carries: the name and the truncated message. */
|
|
26
|
+
export const errorFields = (error: unknown): LogFields => ({
|
|
27
|
+
error: errorName(error),
|
|
28
|
+
detail: truncate(reasonOf(error), DETAIL_LIMIT),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const reportLevels: Readonly<Record<Report["kind"], LogLevel>> = {
|
|
32
|
+
storage_failed: "error",
|
|
33
|
+
task_faulted: "error",
|
|
34
|
+
task_interrupted: "error",
|
|
35
|
+
migration_failed: "error",
|
|
36
|
+
task_outdated: "error",
|
|
37
|
+
task_error: "error",
|
|
38
|
+
extension_error: "warn",
|
|
39
|
+
host_error: "warn",
|
|
40
|
+
listener_error: "warn",
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** One entry per harness report; the code names the kind, never the message. */
|
|
44
|
+
export const logReport = (sink: LogSink, report: Report): void => {
|
|
45
|
+
const fields: LogFields = { ...errorFields(report.error) };
|
|
46
|
+
if ("task" in report && report.task !== undefined) fields.taskId = report.task.id;
|
|
47
|
+
if ("extension" in report && report.extension !== undefined) {
|
|
48
|
+
fields.extension = report.extension;
|
|
49
|
+
}
|
|
50
|
+
if ("callback" in report) fields.callback = report.callback;
|
|
51
|
+
if ("reason" in report) fields.reason = report.reason;
|
|
52
|
+
if ("restarts" in report) fields.restarts = report.restarts;
|
|
53
|
+
sink.write(reportLevels[report.kind], `report.${report.kind}`, fields);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* One JSON object per line under the session directory. No rotation: a session
|
|
58
|
+
* writes a few short lines. A failed write is dropped, never thrown.
|
|
59
|
+
*/
|
|
60
|
+
export const fileLogSink = (path: string): LogSink => ({
|
|
61
|
+
write: (level, code, fields) => {
|
|
62
|
+
try {
|
|
63
|
+
appendFileSync(path, `${JSON.stringify({ ts: Date.now(), level, code, ...fields })}\n`);
|
|
64
|
+
} catch {
|
|
65
|
+
// A logger never throws: a dropped line is better than a failed call.
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
});
|