@ftrdotdev/pi-delegates 0.0.0-stage → 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.
- package/CHANGELOG.md +128 -0
- package/LICENSE +21 -0
- package/OPERATIONS.md +130 -0
- package/README.md +456 -2
- package/ROADMAP.md +179 -0
- package/TESTING.md +896 -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 +94 -4
- package/skills/pi-delegates/SKILL.md +176 -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 +184 -0
- package/src/docs-contract.test.ts +106 -0
- package/src/domain/definitions.test.ts +41 -0
- package/src/domain/definitions.ts +163 -0
- package/src/domain/failure.ts +33 -0
- package/src/domain/ids.ts +13 -0
- package/src/domain/model-source.ts +30 -0
- package/src/domain/names.ts +35 -0
- package/src/domain/orchestration/admission.ts +78 -0
- package/src/domain/orchestration/control.test.ts +1332 -0
- package/src/domain/orchestration/control.ts +457 -0
- package/src/domain/orchestration/fanout.test.ts +524 -0
- package/src/domain/orchestration/fanout.ts +372 -0
- package/src/domain/orchestration/gates.test.ts +685 -0
- package/src/domain/orchestration/gates.ts +360 -0
- package/src/domain/orchestration/launch.test.ts +372 -0
- package/src/domain/orchestration/launch.ts +691 -0
- package/src/domain/orchestration/requests.test.ts +695 -0
- package/src/domain/orchestration/requests.ts +782 -0
- package/src/domain/orchestration/review.test.ts +292 -0
- package/src/domain/orchestration/review.ts +168 -0
- package/src/domain/orchestration/run-wait.test.ts +265 -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 +1566 -0
- package/src/domain/orchestration/workflow.ts +778 -0
- package/src/domain/run-records.test.ts +27 -0
- package/src/domain/run-records.ts +689 -0
- package/src/domain/settlement.test.ts +186 -0
- package/src/domain/settlement.ts +137 -0
- package/src/domain/shapes.test.ts +185 -0
- package/src/domain/skills.test.ts +477 -0
- package/src/domain/skills.ts +114 -0
- package/src/domain/tool-schema.test.ts +46 -0
- package/src/domain/tool-schema.ts +20 -0
- package/src/domain/toolkit.test.ts +215 -0
- package/src/domain/toolkit.ts +374 -0
- package/src/domain/turn-budget.test.ts +37 -0
- package/src/domain/turn-budget.ts +23 -0
- package/src/domain/view.test.ts +803 -0
- package/src/domain/view.ts +491 -0
- package/src/example.test.ts +15 -0
- package/src/extension/commands.test.ts +485 -0
- package/src/extension/commands.ts +230 -0
- package/src/extension/entry.test.ts +1276 -0
- package/src/extension/entry.ts +380 -0
- package/src/extension/gate-alerts.test.ts +237 -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 +1343 -0
- package/src/extension/inspector-command.ts +685 -0
- package/src/extension/prompt.test.ts +108 -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/retry-observer.test.ts +292 -0
- package/src/extension/retry-observer.ts +150 -0
- package/src/extension/revocation.test.ts +81 -0
- package/src/extension/revocation.ts +84 -0
- package/src/extension/settle-alerts.test.ts +579 -0
- package/src/extension/settle-alerts.ts +175 -0
- package/src/extension/shortcut.test.ts +62 -0
- package/src/extension/shortcut.ts +105 -0
- package/src/extension/tools.test.ts +1294 -0
- package/src/extension/tools.ts +553 -0
- package/src/extension/version.test.ts +115 -0
- package/src/extension/version.ts +97 -0
- package/src/goldens.test.ts +294 -0
- package/src/harness-policy.test.ts +76 -0
- package/src/layers.test.ts +309 -0
- package/src/layers.ts +405 -0
- package/src/package-manifest.test.ts +170 -0
- package/src/presentation/actions.test.ts +221 -0
- package/src/presentation/actions.ts +85 -0
- package/src/presentation/activity.test.ts +386 -0
- package/src/presentation/activity.ts +66 -0
- package/src/presentation/close.test.ts +359 -0
- package/src/presentation/inspector.test.ts +682 -0
- package/src/presentation/inspector.ts +442 -0
- package/src/presentation/overlay.test.ts +553 -0
- package/src/presentation/overlay.ts +229 -0
- package/src/presentation/repaint.test.ts +365 -0
- package/src/presentation/shortcut.ts +2 -0
- package/src/presentation/snapshot.test.ts +535 -0
- package/src/presentation/snapshot.ts +306 -0
- package/src/presentation/status.test.ts +43 -0
- package/src/presentation/status.ts +30 -0
- package/src/presentation/strip.test.ts +203 -0
- package/src/presentation/strip.ts +67 -0
- package/src/presentation/wording.test.ts +388 -0
- package/src/presentation/wording.ts +294 -0
- package/src/runtime/bridge.test.ts +131 -0
- package/src/runtime/bridge.ts +128 -0
- package/src/runtime/clock.test.ts +31 -0
- package/src/runtime/clock.ts +31 -0
- package/src/runtime/config.test.ts +63 -0
- package/src/runtime/config.ts +92 -0
- package/src/runtime/context.test.ts +19 -0
- package/src/runtime/context.ts +9 -0
- package/src/runtime/execution-env.test.ts +33 -0
- package/src/runtime/execution-env.ts +15 -0
- package/src/runtime/extensions.test.ts +86 -0
- package/src/runtime/extensions.ts +152 -0
- package/src/runtime/handle.ts +54 -0
- package/src/runtime/log.test.ts +140 -0
- package/src/runtime/log.ts +91 -0
- package/src/runtime/mcp.test.ts +1450 -0
- package/src/runtime/mcp.ts +1005 -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/runtime-state.test.ts +25 -0
- package/src/runtime/runtime-state.ts +36 -0
- package/src/runtime/session-tag.ts +11 -0
- package/src/runtime/session.test.ts +352 -0
- package/src/runtime/session.ts +263 -0
- package/src/runtime/storage.ts +29 -0
- package/src/test-hygiene.test.ts +97 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { MODEL_THINKING_LEVELS } from "@earendil-works/pi-ai/providers/model-schema";
|
|
3
|
+
import { CODING_EXTENSION } from "../domain/names.ts";
|
|
4
|
+
import { ApprovalRule } from "./toolkit.ts";
|
|
5
|
+
|
|
6
|
+
export type { ApprovalRule } from "./toolkit.ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A one-off definition sent with a single launch. It narrows a registered
|
|
10
|
+
* agent, or defines an unknown name with the coding toolkit. It owns no
|
|
11
|
+
* extensions and no approval rules.
|
|
12
|
+
*/
|
|
13
|
+
export const InlineDefinition = Schema.Struct({
|
|
14
|
+
/** The registered or preset name to narrow, or a new one-off name. */
|
|
15
|
+
name: Schema.String,
|
|
16
|
+
/** Short label shown in status and in the prompt section. */
|
|
17
|
+
role: Schema.String,
|
|
18
|
+
/** Rendered into `pi.agent.instructions`, shared part first. */
|
|
19
|
+
prompt: Schema.Struct({ shared: Schema.optional(Schema.String), role: Schema.String }),
|
|
20
|
+
/** Omitted: the registered agent's model, then the host session's model at launch. */
|
|
21
|
+
model: Schema.optional(Schema.Struct({ provider: Schema.String, modelId: Schema.String })),
|
|
22
|
+
/** Omitted: the registered agent's level, then the host session's level at launch. */
|
|
23
|
+
thinkingLevel: Schema.optional(Schema.Literals(MODEL_THINKING_LEVELS)),
|
|
24
|
+
/**
|
|
25
|
+
* Tool names the child may call. Each must resolve at launch. When the name
|
|
26
|
+
* is registered, this list must be a subset of the registered tools.
|
|
27
|
+
*/
|
|
28
|
+
tools: Schema.Array(Schema.String),
|
|
29
|
+
/** Omitted: the host session's working directory at launch. */
|
|
30
|
+
cwd: Schema.optional(Schema.String),
|
|
31
|
+
});
|
|
32
|
+
export type InlineDefinition = typeof InlineDefinition.Type;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A definition registered by an extension and passed to
|
|
36
|
+
* `createDelegatesExtension({ definitions })`. It adds the owner-only fields to
|
|
37
|
+
* the inline shape. A registered name overrides the preset of the same name.
|
|
38
|
+
*/
|
|
39
|
+
export const AgentDefinition = Schema.Struct({
|
|
40
|
+
...InlineDefinition.fields,
|
|
41
|
+
/**
|
|
42
|
+
* Installed durable extension names. `coding-tools` is the built-in coding
|
|
43
|
+
* extension. `gate-guard` is added when `approval.gates` is non-empty.
|
|
44
|
+
*/
|
|
45
|
+
extensions: Schema.Array(Schema.String),
|
|
46
|
+
/** Rules committed with the run's toolkit at launch. Empty: no gate. */
|
|
47
|
+
approval: Schema.Struct({ gates: Schema.Array(ApprovalRule) }),
|
|
48
|
+
/**
|
|
49
|
+
* Optional on the wire; the binder reads `skills ?? []`. A definition that
|
|
50
|
+
* declares skills needs the `read` tool: the prompt section tells the model
|
|
51
|
+
* to load a body with it.
|
|
52
|
+
*/
|
|
53
|
+
skills: Schema.Array(Schema.String).pipe(Schema.optional),
|
|
54
|
+
/** Set to `runs` to let a workflow launch run a script in this definition's conversation. */
|
|
55
|
+
codemode: Schema.optional(Schema.Literal("runs")),
|
|
56
|
+
/** The turn budget for every run of this agent; a request or child value wins. */
|
|
57
|
+
maxTurns: Schema.optional(Schema.Finite),
|
|
58
|
+
});
|
|
59
|
+
export type AgentDefinition = typeof AgentDefinition.Type;
|
|
60
|
+
|
|
61
|
+
export class DefinitionNotFound extends Schema.TaggedError<
|
|
62
|
+
DefinitionNotFound,
|
|
63
|
+
{ readonly brand: unique symbol }
|
|
64
|
+
>()("DefinitionNotFound", { message: Schema.String }) {}
|
|
65
|
+
|
|
66
|
+
export class DefinitionWidened extends Schema.TaggedError<
|
|
67
|
+
DefinitionWidened,
|
|
68
|
+
{ readonly brand: unique symbol }
|
|
69
|
+
>()("DefinitionWidened", { message: Schema.String }) {}
|
|
70
|
+
|
|
71
|
+
export class DefinitionFieldNotAllowed extends Schema.TaggedError<
|
|
72
|
+
DefinitionFieldNotAllowed,
|
|
73
|
+
{ readonly brand: unique symbol }
|
|
74
|
+
>()("DefinitionFieldNotAllowed", { field: Schema.String, message: Schema.String }) {}
|
|
75
|
+
|
|
76
|
+
const OWNER_ONLY_FIELDS = ["extensions", "approval", "skills", "codemode", "maxTurns"] as const;
|
|
77
|
+
|
|
78
|
+
/** Pre-decode guard: only a registered definition owns these fields. */
|
|
79
|
+
export const assertInlineDefinition = (input: unknown): void => {
|
|
80
|
+
if (typeof input !== "object" || input === null) return;
|
|
81
|
+
for (const field of OWNER_ONLY_FIELDS) {
|
|
82
|
+
if (field in input) {
|
|
83
|
+
throw DefinitionFieldNotAllowed.make({
|
|
84
|
+
field,
|
|
85
|
+
message: `An inline definition cannot set "${field}"; only a registered definition owns it.`,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** Decode an inline definition, refusing the owner-only fields before they are stripped. */
|
|
92
|
+
export const decodeInlineDefinition = (input: unknown): InlineDefinition => {
|
|
93
|
+
assertInlineDefinition(input);
|
|
94
|
+
return Schema.decodeUnknownSync(InlineDefinition)(input);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const preset = (
|
|
98
|
+
name: string,
|
|
99
|
+
role: string,
|
|
100
|
+
tools: readonly string[],
|
|
101
|
+
prompt: string,
|
|
102
|
+
): AgentDefinition => ({
|
|
103
|
+
name,
|
|
104
|
+
role,
|
|
105
|
+
prompt: { role: prompt },
|
|
106
|
+
tools,
|
|
107
|
+
extensions: [CODING_EXTENSION],
|
|
108
|
+
approval: { gates: [] },
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
/** The built-in presets. Gates are opt-in: a preset carries no rules. */
|
|
112
|
+
export const presets: readonly AgentDefinition[] = [
|
|
113
|
+
preset(
|
|
114
|
+
"scout",
|
|
115
|
+
"Read-only explorer",
|
|
116
|
+
["read"],
|
|
117
|
+
"You explore the repository and report what you find. You cannot change files.",
|
|
118
|
+
),
|
|
119
|
+
preset(
|
|
120
|
+
"builder",
|
|
121
|
+
"Implementation agent",
|
|
122
|
+
["read", "bash", "write", "edit"],
|
|
123
|
+
"You implement the assigned change and verify it with the repository's checks.",
|
|
124
|
+
),
|
|
125
|
+
preset(
|
|
126
|
+
"reviewer",
|
|
127
|
+
"Review agent",
|
|
128
|
+
["read"],
|
|
129
|
+
"You review the diff and report your findings. You cannot change files.",
|
|
130
|
+
),
|
|
131
|
+
];
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Types a definition without validating it. The launch resolves every tool and
|
|
135
|
+
* extension name; a missing name fails with `Unresolved toolkit names`.
|
|
136
|
+
*/
|
|
137
|
+
export const defineAgent = (definition: AgentDefinition): AgentDefinition => definition;
|
|
138
|
+
|
|
139
|
+
export interface DefinitionCatalogue {
|
|
140
|
+
readonly all: readonly AgentDefinition[];
|
|
141
|
+
readonly presets: readonly AgentDefinition[];
|
|
142
|
+
find(name: string): AgentDefinition | undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Presets plus the definitions the extension was created with; a registered name overrides a preset. */
|
|
146
|
+
export const createCatalogue = (
|
|
147
|
+
definitions: readonly AgentDefinition[] = [],
|
|
148
|
+
): DefinitionCatalogue => {
|
|
149
|
+
const registered = new Map(definitions.map((definition) => [definition.name, definition]));
|
|
150
|
+
const promptPresets = presets.map((entry) => registered.get(entry.name) ?? entry);
|
|
151
|
+
const all = [...promptPresets];
|
|
152
|
+
for (const [name, definition] of registered) {
|
|
153
|
+
if (!presets.some((entry) => entry.name === name)) all.push(definition);
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
all,
|
|
157
|
+
presets: promptPresets,
|
|
158
|
+
find: (name) => registered.get(name) ?? presets.find((entry) => entry.name === name),
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
export const availableDefinitionNames = (catalogue: DefinitionCatalogue): string =>
|
|
163
|
+
catalogue.all.map((definition) => definition.name).join(", ");
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
/** The ten durable reasons plus `internal` for package-native failures. */
|
|
4
|
+
export const FailureReason = Schema.Literals([
|
|
5
|
+
"model_error",
|
|
6
|
+
"no_model",
|
|
7
|
+
"aborted",
|
|
8
|
+
"reset",
|
|
9
|
+
"faulted",
|
|
10
|
+
"orphaned",
|
|
11
|
+
"interrupted",
|
|
12
|
+
"restart_limit",
|
|
13
|
+
"nothing_to_answer",
|
|
14
|
+
"turn_limit",
|
|
15
|
+
"internal",
|
|
16
|
+
]);
|
|
17
|
+
export type FailureReason = typeof FailureReason.Type;
|
|
18
|
+
export const FAILURE_REASONS = FailureReason.literals;
|
|
19
|
+
|
|
20
|
+
/** The settle outcome, one per task. A stopped or denied run records it and no failure fact. */
|
|
21
|
+
export const Resolution = Schema.Literals(["answered", "failed", "denied", "stopped"]);
|
|
22
|
+
export type Resolution = typeof Resolution.Type;
|
|
23
|
+
|
|
24
|
+
/** How a launch was asked to run: wait for the answer, or return at claim. */
|
|
25
|
+
export const Execution = Schema.Literals(["wait", "detached"]);
|
|
26
|
+
export type Execution = typeof Execution.Type;
|
|
27
|
+
|
|
28
|
+
/** Evidence only: `retryable` and `remedy` are derived at read and log time. */
|
|
29
|
+
export const FailureFact = Schema.Struct({
|
|
30
|
+
reason: FailureReason,
|
|
31
|
+
detail: Schema.NullOr(Schema.String),
|
|
32
|
+
});
|
|
33
|
+
export type FailureFact = typeof FailureFact.Type;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ConversationId, TaskId } from "@earendil-works/pi-durable";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A durable task id from the plain number a document stores. `Id` is an erased
|
|
5
|
+
* nominal number, so this is the one place the brand is asserted.
|
|
6
|
+
*/
|
|
7
|
+
export const taskId = <Result = unknown>(value: number): TaskId<Result> => value as TaskId<Result>;
|
|
8
|
+
|
|
9
|
+
/** A durable conversation id from the plain number a document stores. */
|
|
10
|
+
export const conversationId = (value: number): ConversationId => value as ConversationId;
|
|
11
|
+
|
|
12
|
+
/** The plain number a durable id carries, for documents and views. */
|
|
13
|
+
export const idNumber = (id: TaskId<unknown> | ConversationId): number => id;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Models } from "@earendil-works/pi-ai/models";
|
|
2
|
+
import { Context, Layer } from "effect";
|
|
3
|
+
import type { HostDefaults } from "./orchestration/launch.ts";
|
|
4
|
+
|
|
5
|
+
/** The models a session runs on, and the host model a scripted child fills in at each launch. */
|
|
6
|
+
export interface ModelSource {
|
|
7
|
+
readonly models: Models;
|
|
8
|
+
/** Read at each launch, so a host change reaches the next child. */
|
|
9
|
+
readonly host: () => HostDefaults;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The session's one model source; both reads come from here. */
|
|
13
|
+
export class ModelSourceService extends Context.Service<ModelSourceService, ModelSource>()(
|
|
14
|
+
"@ftrdotdev/pi-delegates/domain/model-source/ModelSourceService",
|
|
15
|
+
) {
|
|
16
|
+
/** `host` is a thunk: `models` freezes at open, the host stays live. */
|
|
17
|
+
static layer(
|
|
18
|
+
models: Models,
|
|
19
|
+
host: (() => HostDefaults) | undefined,
|
|
20
|
+
): Layer.Layer<ModelSourceService> {
|
|
21
|
+
return Layer.sync(ModelSourceService, () => ({
|
|
22
|
+
models,
|
|
23
|
+
host:
|
|
24
|
+
host ??
|
|
25
|
+
(() => {
|
|
26
|
+
throw new Error("No host model for this session.");
|
|
27
|
+
}),
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** Names the package and durable's registry agree on. */
|
|
2
|
+
export const CODING_EXTENSION = "coding-tools";
|
|
3
|
+
export const GATE_GUARD_EXTENSION = "gate-guard";
|
|
4
|
+
/** Internal extension that registers the run task definition. */
|
|
5
|
+
export const RUN_TASK_EXTENSION = "delegates-runs";
|
|
6
|
+
/** The extension that renders a child's declared skills. */
|
|
7
|
+
export const SKILLS_EXTENSION = "skills";
|
|
8
|
+
/** The extension that installs `ask`, so a child may put a question to the user. */
|
|
9
|
+
export const QUESTIONS_EXTENSION = "questions";
|
|
10
|
+
/** Durable's code mode tool; selected with a definition's `codemode: "runs"`. */
|
|
11
|
+
export const CODEMODE_EXTENSION = "codemode";
|
|
12
|
+
/** The script-only run tools; selected beside `codemode` and never offered to the model. */
|
|
13
|
+
export const RUNS_EXTENSION = "runs";
|
|
14
|
+
/**
|
|
15
|
+
* Durable's tool-search extension; it installs `tool_search`, which loads a deferred tool into the
|
|
16
|
+
* model's offer. A definition selects the extension, never the tool name.
|
|
17
|
+
*/
|
|
18
|
+
export const TOOL_SEARCH_EXTENSION = "tool-search";
|
|
19
|
+
|
|
20
|
+
/** The `runs` namespace's eight tools, in declaration order. */
|
|
21
|
+
export const RUN_TOOL_NAMES = [
|
|
22
|
+
"launch",
|
|
23
|
+
"launch_many",
|
|
24
|
+
"lane",
|
|
25
|
+
"steer",
|
|
26
|
+
"stop",
|
|
27
|
+
"status",
|
|
28
|
+
"ref",
|
|
29
|
+
"gate",
|
|
30
|
+
] as const;
|
|
31
|
+
export type RunToolName = (typeof RUN_TOOL_NAMES)[number];
|
|
32
|
+
|
|
33
|
+
/** The `questions` extension's tools, in declaration order. */
|
|
34
|
+
export const ASK_TOOL_NAMES = ["ask"] as const;
|
|
35
|
+
export type AskToolName = (typeof ASK_TOOL_NAMES)[number];
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { HarnessInspection, TaskRecord } from "@earendil-works/pi-durable";
|
|
2
|
+
import { Predicate } from "effect";
|
|
3
|
+
|
|
4
|
+
/** How many more children may start now. Never negative, never above either difference. */
|
|
5
|
+
export const available = (
|
|
6
|
+
laneCap: number,
|
|
7
|
+
laneLive: number,
|
|
8
|
+
sessionCeiling: number,
|
|
9
|
+
sessionLive: number,
|
|
10
|
+
): number => {
|
|
11
|
+
const laneRoom = laneCap - laneLive;
|
|
12
|
+
const sessionRoom = sessionCeiling - sessionLive;
|
|
13
|
+
if (laneRoom <= 0 || sessionRoom <= 0) return 0;
|
|
14
|
+
return Math.min(laneRoom, sessionRoom);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export interface LiveCounts {
|
|
18
|
+
readonly laneLive: number;
|
|
19
|
+
readonly sessionLive: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A run holds a slot while it runs or while it sleeps on a signal (its child is
|
|
24
|
+
* working). A task that was created but not admitted yet reserves its slot, so
|
|
25
|
+
* a concurrent fanout cannot overfill the cap. A task sleeping on its admission
|
|
26
|
+
* retry holds no slot.
|
|
27
|
+
*/
|
|
28
|
+
const holdsSlot = (record: {
|
|
29
|
+
readonly state: {
|
|
30
|
+
readonly status: string;
|
|
31
|
+
readonly signals?: readonly unknown[];
|
|
32
|
+
};
|
|
33
|
+
}): boolean =>
|
|
34
|
+
record.state.status === "pending" ||
|
|
35
|
+
record.state.status === "running" ||
|
|
36
|
+
(record.state.status === "sleeping" && (record.state.signals?.length ?? 0) > 0);
|
|
37
|
+
|
|
38
|
+
/** Live run tasks, read outside any commit callback. `self` excludes the caller's own task. */
|
|
39
|
+
export const liveCounts = (
|
|
40
|
+
inspection: HarnessInspection,
|
|
41
|
+
lane: string,
|
|
42
|
+
options: {
|
|
43
|
+
readonly excludeTaskId?: number;
|
|
44
|
+
/** Direct children a task runtime owns; merged with `inspection` by task id. */
|
|
45
|
+
readonly ownedTasks?: readonly TaskRecord<unknown, unknown, unknown>[];
|
|
46
|
+
} = {},
|
|
47
|
+
): LiveCounts => {
|
|
48
|
+
const records = new Map<
|
|
49
|
+
number,
|
|
50
|
+
Pick<TaskRecord<unknown, unknown, unknown>, "id" | "kind" | "input" | "state">
|
|
51
|
+
>();
|
|
52
|
+
for (const task of inspection.tasks) records.set(task.record.id, task.record);
|
|
53
|
+
for (const task of options.ownedTasks ?? []) records.set(task.id, task);
|
|
54
|
+
|
|
55
|
+
let laneLive = 0;
|
|
56
|
+
let sessionLive = 0;
|
|
57
|
+
for (const record of records.values()) {
|
|
58
|
+
if (record.kind !== "delegates.run") continue;
|
|
59
|
+
if (options.excludeTaskId !== undefined && record.id === options.excludeTaskId) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (!holdsSlot({ state: record.state })) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
sessionLive += 1;
|
|
66
|
+
const taskLane = Predicate.isObject(record.input) ? record.input.lane : undefined;
|
|
67
|
+
if (taskLane === lane) laneLive += 1;
|
|
68
|
+
}
|
|
69
|
+
return { laneLive, sessionLive };
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export interface AdmissionHost {
|
|
73
|
+
/** `harness.inspect()`, never read inside a commit callback. */
|
|
74
|
+
readonly inspect: () => Promise<HarnessInspection>;
|
|
75
|
+
readonly caps: () => { readonly laneCap: number; readonly sessionCeiling: number };
|
|
76
|
+
/** The admission poll interval, on the injected clock. */
|
|
77
|
+
readonly pollMs: number;
|
|
78
|
+
}
|