@wrongstack/core 0.1.7 → 0.1.9

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.
@@ -1,7 +1,7 @@
1
1
  import { g as Logger, f as LogLevel, P as PathResolver, r as ModelsRegistry, O as EventBus, z as ResolvedModel, j as MemoryStore, i as MemoryScope, S as SecretScrubber, t as PermissionPolicy, I as InputReader, s as PermissionDecision, B as RetryPolicy, E as ErrorHandler, a as Compactor, R as RecoveryDecision, H as SkillLoader, J as SkillManifest, G as SkillEntry, b as Config, c as ConfigLoader, d as ConfigStore, C as CompactReport, a0 as MiddlewareHandler, p as ModelsDevPayload, A as ResolvedProvider, W as WireFamily, n as ModeStore, l as ModeConfig, k as Mode, M as MCPServerConfig } from '../mode-Pjt5vMS6.js';
2
2
  import { u as TokenCounter, U as Usage, C as CacheStats, p as SessionStore, o as SessionMetadata, r as SessionWriter, l as ResumedSession, S as SessionData, q as SessionSummary, c as ContentBlock, v as Tool, a0 as Context, i as ProviderError, h as Provider, M as Message, F as ToolUseBlock, B as ToolResultBlock, n as SessionEvent } from '../provider-txgB0Oq9.js';
3
- import { h as AttachmentStore, A as AddAttachmentInput, g as AttachmentRef, d as Attachment, S as SecretVault, x as MultiAgentCoordinator, w as MultiAgentConfig, a9 as SubagentRunner, a5 as SubagentConfig, X as SpawnResult, al as TaskSpec, B as BridgeMessage, a as AgentBridge, l as CoordinatorStatus, aj as TaskResult, aC as Agent, aE as AgentInput, j as BridgeTransport, b as AgentBridgeConfig, o as DoneCondition, aM as RunResult, a4 as Specification, Y as SpecAnalysis, a3 as SpecValidationResult, af as TaskGraph, ag as TaskNode, ae as TaskFilter, ak as TaskSort, ai as TaskProgress, an as TaskType, ah as TaskPriority, at as ToolExecutorOptions, au as ToolExecutorStrategy, ao as ToolBatchResult, O as SessionReader, n as DefaultSessionReaderOptions, N as SessionQuery, T as SessionSummaryLite, R as SessionSearchQuery, Q as SessionSearchHit, L as SessionExportOptions, u as MetricsSink, s as MetricLabels, v as MetricsSnapshot, q as HealthRegistry, H as HealthCheck, c as AggregateHealth, aw as Tracer, W as Span } from '../session-reader-7AutWHut.js';
4
- export { aG as BudgetExceededError, aH as BudgetKind, aI as BudgetLimits, aJ as BudgetUsage, aN as SubagentBudget } from '../session-reader-7AutWHut.js';
3
+ import { h as AttachmentStore, A as AddAttachmentInput, g as AttachmentRef, d as Attachment, S as SecretVault, x as MultiAgentCoordinator, w as MultiAgentConfig, a9 as SubagentRunner, a5 as SubagentConfig, X as SpawnResult, al as TaskSpec, B as BridgeMessage, a as AgentBridge, l as CoordinatorStatus, aj as TaskResult, aC as Agent, aE as AgentInput, j as BridgeTransport, b as AgentBridgeConfig, o as DoneCondition, aM as RunResult, a4 as Specification, Y as SpecAnalysis, a3 as SpecValidationResult, af as TaskGraph, ag as TaskNode, ae as TaskFilter, ak as TaskSort, ai as TaskProgress, an as TaskType, ah as TaskPriority, at as ToolExecutorOptions, au as ToolExecutorStrategy, ao as ToolBatchResult, O as SessionReader, n as DefaultSessionReaderOptions, N as SessionQuery, T as SessionSummaryLite, R as SessionSearchQuery, Q as SessionSearchHit, L as SessionExportOptions, u as MetricsSink, s as MetricLabels, v as MetricsSnapshot, q as HealthRegistry, H as HealthCheck, c as AggregateHealth, aw as Tracer, W as Span } from '../session-reader-9sOTgmeC.js';
4
+ export { aG as BudgetExceededError, aH as BudgetKind, aI as BudgetLimits, aJ as BudgetUsage, aN as SubagentBudget } from '../session-reader-9sOTgmeC.js';
5
5
  import { a as WstackPaths } from '../wstack-paths-BGu2INTm.js';
6
6
  import { EventEmitter } from 'node:events';
7
7
 
@@ -882,6 +882,118 @@ interface AgentRunnerOptions {
882
882
  */
883
883
  declare function makeAgentSubagentRunner(opts: AgentRunnerOptions): SubagentRunner;
884
884
 
885
+ /**
886
+ * Single fleet-wide event with subagent attribution. Whatever a child
887
+ * agent emits on its own EventBus gets re-published here, prefixed with
888
+ * `subagentId` so a single subscriber can multiplex across the fleet.
889
+ *
890
+ * The director uses `FleetBus.filter('tool.executed', …)` to see every
891
+ * tool call across the fleet; the TUI uses
892
+ * `FleetBus.subscribe(id, handler)` to render a per-subagent panel.
893
+ */
894
+ interface FleetEvent {
895
+ subagentId: string;
896
+ taskId?: string;
897
+ ts: number;
898
+ type: string;
899
+ payload: unknown;
900
+ }
901
+ type FleetHandler = (event: FleetEvent) => void;
902
+ /**
903
+ * Fan-in for per-subagent EventBuses. Each subagent's bus is plugged in
904
+ * via `attach()`; the FleetBus re-emits every event with subagent
905
+ * attribution. Detachment is automatic via the returned disposer — call
906
+ * it when a subagent terminates so we don't leak listeners.
907
+ *
908
+ * The bus exposes two subscription modes: by `subagentId` (everything
909
+ * from one child) and by `type` (one event-type across the fleet). They
910
+ * compose — if you need a per-subagent + per-type slice, subscribe by
911
+ * type and filter on `event.subagentId` in your handler.
912
+ */
913
+ declare class FleetBus {
914
+ private readonly byId;
915
+ private readonly byType;
916
+ private readonly any;
917
+ /**
918
+ * Hook a subagent's EventBus into the fleet. EventBus is strongly
919
+ * typed and doesn't expose an `onAny` hook, so we subscribe to the
920
+ * canonical set of event types a subagent emits during a run. New
921
+ * event types added to the kernel must be added here too — but the
922
+ * cost is a tiny single line per type, and the explicit list keeps
923
+ * the wire format clear.
924
+ *
925
+ * Returns a disposer that detaches every subscription; call on
926
+ * subagent teardown so the listeners don't outlive the run.
927
+ */
928
+ attach(subagentId: string, bus: EventBus, taskId?: string): () => void;
929
+ /** Subscribe to every event from one subagent. */
930
+ subscribe(subagentId: string, handler: FleetHandler): () => void;
931
+ /** Subscribe to one event type across all subagents. */
932
+ filter(type: string, handler: FleetHandler): () => void;
933
+ /** Subscribe to literally everything. The fleet roll-up uses this. */
934
+ onAny(handler: FleetHandler): () => void;
935
+ emit(event: FleetEvent): void;
936
+ }
937
+ /**
938
+ * Roll-up of token usage + cost across an entire director run. The
939
+ * director's `fleet_status` tool returns this so the model can reason
940
+ * about budget in its next turn ("the researcher already burned $0.40,
941
+ * lean on summaries for the next task").
942
+ */
943
+ interface FleetUsage {
944
+ total: {
945
+ input: number;
946
+ output: number;
947
+ cacheRead: number;
948
+ cacheWrite: number;
949
+ cost: number;
950
+ };
951
+ perSubagent: Record<string, SubagentUsageSnapshot>;
952
+ }
953
+ interface SubagentUsageSnapshot {
954
+ subagentId: string;
955
+ provider?: string;
956
+ model?: string;
957
+ input: number;
958
+ output: number;
959
+ cacheRead: number;
960
+ cacheWrite: number;
961
+ cost: number;
962
+ toolCalls: number;
963
+ iterations: number;
964
+ startedAt: number;
965
+ lastEventAt: number;
966
+ }
967
+ /**
968
+ * Aggregates provider.response + tool.executed events from the FleetBus
969
+ * into a live `FleetUsage` snapshot. Costs are computed by the caller
970
+ * via a `priceLookup(subagentId)` so we don't bake provider-pricing
971
+ * coupling into core; the CLI/tests supply a function that resolves
972
+ * each subagent's per-token rates from the models registry.
973
+ */
974
+ declare class FleetUsageAggregator {
975
+ private readonly bus;
976
+ private readonly priceLookup?;
977
+ private readonly metaLookup?;
978
+ private readonly perSubagent;
979
+ private readonly total;
980
+ constructor(bus: FleetBus, priceLookup?: ((subagentId: string) => {
981
+ input?: number;
982
+ output?: number;
983
+ cacheRead?: number;
984
+ cacheWrite?: number;
985
+ } | undefined) | undefined, metaLookup?: ((subagentId: string) => {
986
+ provider?: string;
987
+ model?: string;
988
+ } | undefined) | undefined);
989
+ /** Live snapshot — safe to call from a tool's execute() body. */
990
+ snapshot(): FleetUsage;
991
+ private ensure;
992
+ private onProviderResponse;
993
+ private onToolExecuted;
994
+ private onIterationStarted;
995
+ }
996
+
885
997
  /**
886
998
  * In-memory pub/sub transport for agent-to-agent messaging.
887
999
  * Subscribers register by agentId and receive messages via callback.
@@ -910,6 +1022,485 @@ declare class InMemoryAgentBridge implements AgentBridge {
910
1022
  }
911
1023
  declare function createMessage<T = unknown>(type: BridgeMessage['type'], from: string, payload: T, to?: string): BridgeMessage<T>;
912
1024
 
1025
+ /**
1026
+ * Director — high-level orchestrator that owns a `MultiAgentCoordinator`,
1027
+ * a `FleetBus`, and a `FleetUsageAggregator`. Exposes a small imperative
1028
+ * API (`spawn`, `assign`, `awaitTasks`, `terminate`, `status`, `usage`)
1029
+ * that's easy to test, and a `tools()` factory that wraps the same API
1030
+ * as agent-callable `Tool`s so an LLM can drive the orchestration.
1031
+ *
1032
+ * This class is intentionally *not* an `Agent`. It's a coordinator +
1033
+ * observability surface. To make it LLM-driven, construct an Agent
1034
+ * with `director.tools()` registered. That keeps the construction
1035
+ * symmetric with how other agents are built and avoids smuggling a
1036
+ * heavy LLM dependency into core just for the director path.
1037
+ */
1038
+ interface DirectorOptions {
1039
+ config: MultiAgentConfig;
1040
+ runner?: SubagentRunner;
1041
+ /**
1042
+ * When set, the director writes a `fleet.json` manifest to this path
1043
+ * recording every spawned subagent (id, provider, model, role, task
1044
+ * ids). Used by `wstack replay <runId>` to rehydrate a fleet. Pass an
1045
+ * absolute file path — the directory must already exist (the
1046
+ * director-session factory creates it when used together).
1047
+ */
1048
+ manifestPath?: string;
1049
+ /**
1050
+ * Optional roster used by `leaderSystemPrompt()` to render a roles
1051
+ * summary into the leader's preamble. Same shape as the roster passed
1052
+ * to `tools()` — typically the same value.
1053
+ */
1054
+ roster?: Record<string, SubagentConfig>;
1055
+ /**
1056
+ * Override the built-in fleet preamble (see `DEFAULT_DIRECTOR_PREAMBLE`).
1057
+ * Pass an empty string to suppress the preamble entirely.
1058
+ */
1059
+ directorPreamble?: string;
1060
+ /**
1061
+ * Override the built-in subagent baseline (see
1062
+ * `DEFAULT_SUBAGENT_BASELINE`). Pass an empty string to suppress.
1063
+ */
1064
+ subagentBaseline?: string;
1065
+ /**
1066
+ * Absolute path to a directory the fleet can use as a shared scratchpad
1067
+ * (read + write by every subagent). When set, the director creates it on
1068
+ * construction and `subagentSystemPrompt()` automatically injects a
1069
+ * "Shared notes" block telling subagents where to drop their findings.
1070
+ * This is the cheap fleet-coordination channel — agents don't need each
1071
+ * other's transcripts, just each other's conclusions.
1072
+ *
1073
+ * Convention: under a fleet run rooted at `<sessionsRoot>/<runId>/`,
1074
+ * pass `<sessionsRoot>/<runId>/shared/` here.
1075
+ */
1076
+ sharedScratchpadPath?: string;
1077
+ /**
1078
+ * Maximum number of spawns this director can perform across its
1079
+ * lifetime. Default: unlimited. Acts as a hard fleet-wide cost cap —
1080
+ * a runaway leader that keeps spawning workers gets cut off cleanly
1081
+ * instead of burning provider tokens until the user kills the
1082
+ * process. The N+1-th spawn call rejects with a `DirectorBudgetError`.
1083
+ */
1084
+ maxSpawns?: number;
1085
+ /**
1086
+ * Maximum nesting depth for spawns. The director constructed by the
1087
+ * user is at depth `spawnDepth` (default 0); any subagent that itself
1088
+ * acts as a director would construct its own `Director` with
1089
+ * `spawnDepth: parent.spawnDepth + 1`. When `spawnDepth >= maxSpawnDepth`,
1090
+ * `spawn()` rejects. Default: 2 (root director can spawn workers; a
1091
+ * worker that becomes a sub-director cannot itself spawn further).
1092
+ * This stops infinite recursive director chains from a hostile or
1093
+ * confused prompt.
1094
+ */
1095
+ maxSpawnDepth?: number;
1096
+ /**
1097
+ * Current spawn-chain depth for this director instance. Defaults to 0.
1098
+ * A nested director should pass `parent.spawnDepth + 1`. Together with
1099
+ * `maxSpawnDepth` this bounds the chain.
1100
+ */
1101
+ spawnDepth?: number;
1102
+ }
1103
+ /**
1104
+ * Thrown by `Director.spawn()` when a configured spawn cap (`maxSpawns`,
1105
+ * `maxSpawnDepth`) is hit. Distinct error class so callers — including
1106
+ * the `spawn_subagent` tool surface — can recognize the budget case and
1107
+ * report it cleanly instead of treating it like an unexpected failure.
1108
+ */
1109
+ declare class DirectorBudgetError extends Error {
1110
+ readonly kind: 'max_spawns' | 'max_spawn_depth';
1111
+ readonly limit: number;
1112
+ readonly observed: number;
1113
+ constructor(kind: 'max_spawns' | 'max_spawn_depth', limit: number, observed: number);
1114
+ }
1115
+ declare class Director {
1116
+ readonly id: string;
1117
+ readonly fleet: FleetBus;
1118
+ readonly usage: FleetUsageAggregator;
1119
+ /**
1120
+ * Director-side bridge endpoint. Subagents are wired to the same
1121
+ * in-memory transport so the director can `ask()` them synchronously
1122
+ * and they can `send()` progress back. Exposed so external code (e.g.
1123
+ * the TUI) can subscribe to inbound messages.
1124
+ */
1125
+ readonly bridge: InMemoryAgentBridge;
1126
+ private readonly transport;
1127
+ private readonly coordinator;
1128
+ /** Resolves with the matching `TaskResult` the first time the
1129
+ * coordinator emits `task.completed` for a given task id. Each entry
1130
+ * is created lazily on first poll/await and cleared once consumed. */
1131
+ private readonly taskWaiters;
1132
+ /** Cache of completed results in case the consumer asks AFTER the
1133
+ * coordinator already fired the event — `awaitTasks(['t-1'])` after
1134
+ * t-1 finished should resolve immediately, not hang. */
1135
+ private readonly completed;
1136
+ /** Per-subagent provider/model metadata, captured at spawn time so the
1137
+ * FleetUsageAggregator's metaLookup can surface readable rows. */
1138
+ private readonly subagentMeta;
1139
+ private readonly priceLookups;
1140
+ /** Bridge endpoints we created per subagent (so we can `stop()` them
1141
+ * on shutdown and free transport subscriptions). */
1142
+ private readonly subagentBridges;
1143
+ /** Tracks per-spawn config + assigned task ids for manifest writing. */
1144
+ private readonly manifestEntries;
1145
+ private readonly manifestPath?;
1146
+ private readonly roster?;
1147
+ private readonly directorPreamble;
1148
+ private readonly subagentBaseline;
1149
+ /** Absolute path to the fleet's shared scratchpad directory, or null
1150
+ * when none was configured. Exposed as a readonly getter for callers
1151
+ * that need to surface the path to the user (e.g. the CLI logging
1152
+ * the location after `--director` boots). */
1153
+ readonly sharedScratchpadPath: string | null;
1154
+ /** Spawn cap (lifetime total). Infinity means unlimited. */
1155
+ readonly maxSpawns: number;
1156
+ /** Nesting cap. The N-th director in a chain has `spawnDepth = N-1`. */
1157
+ readonly maxSpawnDepth: number;
1158
+ /** This director's position in a director chain. Root director = 0. */
1159
+ readonly spawnDepth: number;
1160
+ /** Live spawn counter for `maxSpawns` enforcement. */
1161
+ private spawnCount;
1162
+ constructor(opts: DirectorOptions);
1163
+ /**
1164
+ * Spawn a subagent. Identical to the coordinator's `spawn()` but
1165
+ * captures provider/model metadata for the usage aggregator and
1166
+ * lets the FleetBus attach to the runner's EventBus when the task
1167
+ * actually runs (see `attachSubagentBus`).
1168
+ *
1169
+ * Caller-supplied `priceLookup` is optional but recommended — without
1170
+ * it the `cost` column in `usage.snapshot()` stays at 0.
1171
+ */
1172
+ spawn(config: SubagentConfig, priceLookup?: {
1173
+ input?: number;
1174
+ output?: number;
1175
+ cacheRead?: number;
1176
+ cacheWrite?: number;
1177
+ }): Promise<string>;
1178
+ /**
1179
+ * Synchronously ask a subagent something via the bridge. Sends a
1180
+ * `task` message addressed to the subagent and awaits a matching
1181
+ * reply (matched by message id). Subagent runners that handle these
1182
+ * requests subscribe to `ctx.bridge` and reply with a message whose
1183
+ * `id` equals the incoming request's id (see `InMemoryAgentBridge`'s
1184
+ * `request<T>` implementation).
1185
+ *
1186
+ * Returns the response payload directly (the bridge wrapper is
1187
+ * unwrapped for ergonomics). Times out after `timeoutMs` (default
1188
+ * matches the bridge's own default of 30s) — surface those rejections
1189
+ * to the caller as actionable errors instead of letting tools hang.
1190
+ */
1191
+ ask<T = unknown>(subagentId: string, payload: unknown, timeoutMs?: number): Promise<T>;
1192
+ /**
1193
+ * Read completed task results and format them as a structured text
1194
+ * block the director's LLM can paste into its own context. The
1195
+ * Director keeps every completed `TaskResult` in `completed` so this
1196
+ * is a pure read — no bridge round-trip, cheap to call.
1197
+ *
1198
+ * The returned string is intentionally markdown-flavored: headers per
1199
+ * subagent, a one-line meta row (iter / tools / ms), and the task's
1200
+ * result text. Pass `style: 'json'` for a programmatic shape instead
1201
+ * (useful when the director model is doing structured-output work).
1202
+ */
1203
+ rollUp(taskIds: string[], style?: 'markdown' | 'json'): string;
1204
+ /**
1205
+ * Write the fleet manifest to `manifestPath`. Returns the path written
1206
+ * or null when no path was configured. Captures every spawn + its
1207
+ * assigned tasks — paired with per-subagent JSONLs, this is enough to
1208
+ * replay an entire director run.
1209
+ */
1210
+ writeManifest(): Promise<string | null>;
1211
+ /**
1212
+ * Tear down the director: stop every subagent, close every bridge
1213
+ * endpoint, and (when configured) write the final manifest. Idempotent
1214
+ * — calling shutdown twice is a no-op on the second invocation.
1215
+ */
1216
+ shutdown(): Promise<void>;
1217
+ /**
1218
+ * Hand a task to the coordinator. Returns the assigned task id so
1219
+ * callers can wait on it via `awaitTasks([id])`. The coordinator's
1220
+ * concurrency limit applies — the task may queue before running.
1221
+ */
1222
+ assign(task: TaskSpec): Promise<string>;
1223
+ /**
1224
+ * Block until every task id resolves. Returns results in the same
1225
+ * order as the input. If any task hasn't completed by the time this
1226
+ * is called, the promise hangs until it does — pair with a timeout
1227
+ * at the caller if that's a concern. Resolves immediately for ids
1228
+ * whose results were already cached.
1229
+ */
1230
+ awaitTasks(taskIds: string[]): Promise<TaskResult[]>;
1231
+ terminate(subagentId: string): Promise<void>;
1232
+ terminateAll(): Promise<void>;
1233
+ status(): CoordinatorStatus;
1234
+ /**
1235
+ * Subscribe to coordinator events. Currently only `task.completed` is
1236
+ * exposed (the others are internal lifecycle). Returns an unsubscribe
1237
+ * function. External callers (e.g. the CLI's `MultiAgentHost`) use this
1238
+ * to drive their own pending/results tracking without poking the
1239
+ * coordinator directly.
1240
+ */
1241
+ on(event: 'task.completed', handler: (payload: {
1242
+ task: TaskSpec;
1243
+ result: TaskResult;
1244
+ }) => void): () => void;
1245
+ /**
1246
+ * Snapshot of every task that has resolved (success, failed, timeout,
1247
+ * stopped) since the director started. Returned in completion order
1248
+ * via the internal map's iteration order. Used by `/fleet status` to
1249
+ * paint the completed table without reaching into private state.
1250
+ */
1251
+ completedResults(): TaskResult[];
1252
+ snapshot(): FleetUsage;
1253
+ /**
1254
+ * Compose the leader/director-agent system prompt: fleet preamble +
1255
+ * (optional) roster summary + user base prompt. Pass the result to your
1256
+ * leader Agent's `ctx.systemPrompt` when constructing it.
1257
+ *
1258
+ * `basePrompt` defaults to `config.leaderSystemPrompt` so callers can
1259
+ * use the no-arg form when the multi-agent config already carries it.
1260
+ */
1261
+ leaderSystemPrompt(basePrompt?: string): string;
1262
+ /**
1263
+ * Compose a subagent's system prompt for a given `SubagentConfig`:
1264
+ * baseline + role + task + per-spawn override. Returned by value — does
1265
+ * not mutate the config. Factories (the user-supplied `AgentFactory`)
1266
+ * should call this when building each subagent's Agent so the bridge
1267
+ * contract, role context, and override are all surfaced.
1268
+ *
1269
+ * When `taskBrief` is omitted the Task section is dropped. Pass the
1270
+ * actual task description here to reinforce it in the system prompt
1271
+ * (the runner already passes it as user input — duplicating in the
1272
+ * system prompt is optional but improves anchoring on small models).
1273
+ */
1274
+ subagentSystemPrompt(config: SubagentConfig, taskBrief?: string): string;
1275
+ /**
1276
+ * Build the tool set the LLM-driven director uses to orchestrate.
1277
+ * Returns an array of `Tool` definitions; register these on the
1278
+ * director's `Agent` to expose `spawn_subagent`, `assign_task`, etc.
1279
+ * Each tool's `execute()` delegates straight to the matching method
1280
+ * above.
1281
+ *
1282
+ * Tools all carry `permission: 'auto'` — the *user* has already
1283
+ * approved running the director when they kicked off the run, so
1284
+ * gating individual orchestration calls behind a confirm prompt
1285
+ * would just be noise. The actual subagent tools they spawn are
1286
+ * still permission-checked normally.
1287
+ */
1288
+ tools(roster?: Record<string, SubagentConfig>): Tool[];
1289
+ }
1290
+
1291
+ /**
1292
+ * Per-subagent session factory.
1293
+ *
1294
+ * Director runs produce many parallel transcripts — one per spawned
1295
+ * subagent — and we want them all rooted under the same director-run
1296
+ * directory so a future `wstack replay <runId>` can rehydrate the whole
1297
+ * fleet from a single tree.
1298
+ *
1299
+ * The factory builds (or accepts) a `SessionStore` whose `dir` points at
1300
+ * `<sessionsRoot>/<directorRunId>/`, and returns a small `create()`
1301
+ * function that the orchestration layer calls per-spawn. Each call
1302
+ * yields a fresh `SessionWriter` whose JSONL file lives in that
1303
+ * directory, named by either the caller-supplied `subagentId` (preferred,
1304
+ * so the file name is human-readable) or a derived id.
1305
+ *
1306
+ * **Why a thin factory instead of plumbing options through every spawn
1307
+ * site?** Because the director is the only caller that needs this
1308
+ * isolation pattern, and shoving `sessionStore` options into
1309
+ * `SubagentConfig` would leak storage details into a config shape that
1310
+ * agents and the coordinator have no business knowing about.
1311
+ */
1312
+ interface DirectorSessionFactoryOptions {
1313
+ /**
1314
+ * Either a parent directory where `<directorRunId>/` will be created,
1315
+ * or a pre-built `SessionStore` whose `dir` already points at the
1316
+ * director run directory. Tests pass an in-memory store for isolation;
1317
+ * production code passes the path under `~/.wrongstack/sessions/`.
1318
+ */
1319
+ store?: SessionStore;
1320
+ sessionsRoot?: string;
1321
+ /**
1322
+ * Director run id — namespaces all subagent JSONLs under one folder.
1323
+ * Defaults to a timestamped id; supplied explicitly when resuming a
1324
+ * prior fleet manifest.
1325
+ */
1326
+ directorRunId?: string;
1327
+ }
1328
+ interface DirectorSessionFactory {
1329
+ /** Absolute directory where this director run's transcripts live. */
1330
+ readonly dir: string;
1331
+ /** The director run id used to namespace the directory. */
1332
+ readonly directorRunId: string;
1333
+ /**
1334
+ * Create a fresh `SessionWriter` for the named subagent. Each
1335
+ * subagent gets its own JSONL file. The writer's `id` matches the
1336
+ * supplied `subagentId` so disk paths line up with in-memory ids.
1337
+ */
1338
+ createSubagentSession(args: {
1339
+ subagentId: string;
1340
+ provider?: string;
1341
+ model?: string;
1342
+ title?: string;
1343
+ }): Promise<SessionWriter>;
1344
+ }
1345
+ /**
1346
+ * Build a `DirectorSessionFactory`. Pass either a pre-configured
1347
+ * `SessionStore` (tests) or a `sessionsRoot` path (production). When
1348
+ * neither is supplied the factory throws — there's no sane default for
1349
+ * "where do these JSONLs live".
1350
+ */
1351
+ declare function makeDirectorSessionFactory(opts: DirectorSessionFactoryOptions): DirectorSessionFactory;
1352
+
1353
+ /**
1354
+ * System-prompt composition helpers for the Director ecosystem.
1355
+ *
1356
+ * Two callers need composed prompts:
1357
+ *
1358
+ * 1. The **leader** (the director's own Agent) — needs a preamble that
1359
+ * explains the fleet protocol: when to spawn, when to await, how to
1360
+ * roll up, and the eight orchestration tools it owns.
1361
+ *
1362
+ * 2. Each **subagent** — needs a baseline that explains it has a parent
1363
+ * it can call via the bridge, a role-specific block, the task brief,
1364
+ * and finally any per-spawn `systemPromptOverride` from `SubagentConfig`.
1365
+ *
1366
+ * Both composers are pure functions: feed them parts, they return a string.
1367
+ * No I/O, no side effects, no implicit defaults beyond the ones exported
1368
+ * here. Callers (CLI multi-agent factory, Director itself) decide which
1369
+ * parts to fill in — that keeps the composition seam visible and testable.
1370
+ */
1371
+ /**
1372
+ * Default fleet-protocol preamble injected at the **front** of the
1373
+ * director-agent's system prompt. Kept deliberately short — long preambles
1374
+ * crowd out the user's leader prompt and the LLM stops attending. The tool
1375
+ * descriptions live on the tool definitions themselves; this preamble only
1376
+ * teaches *when* to reach for them.
1377
+ */
1378
+ declare const DEFAULT_DIRECTOR_PREAMBLE = "You are the Director of a multi-agent fleet. You orchestrate worker\nsubagents by spawning them, assigning tasks, awaiting completions, and\nrolling up their outputs into your next decision.\n\nCore fleet tools available to you:\n - spawn_subagent \u2014 create a worker with a chosen provider / model / role\n - assign_task \u2014 hand a piece of work to a specific subagent\n - await_tasks \u2014 block until named task ids complete (parallel-safe)\n - ask_subagent \u2014 synchronously query a running subagent via the bridge\n - roll_up \u2014 aggregate finished tasks into a markdown/json summary\n - terminate_subagent \u2014 abort a stuck worker (use sparingly)\n - fleet_status \u2014 snapshot of all subagents and pending tasks\n - fleet_usage \u2014 token + cost breakdown per subagent and total\n\nWorking rules:\n 1. Decompose first. Before spawning, decide which sub-tasks are\n independent and can run in parallel. Sequential work doesn't need a\n subagent \u2014 do it yourself.\n 2. Match worker to job. Cheap/fast model for triage, capable model for\n synthesis. Different providers per sibling is allowed and encouraged.\n 3. Always pair an assign with an await. Don't fire-and-forget; you owe\n the user a single coherent answer at the end.\n 4. Roll up before deciding. After await_tasks resolves, call roll_up so\n the results are folded back into your context in a compact form.\n 5. Budget is real. Check fleet_usage periodically. If a subagent is\n thrashing, terminate it rather than letting cost climb silently.\n 6. Never claim a subagent's work as your own without verifying it. If a\n result looks wrong, ask_subagent for clarification before passing it\n to the user.";
1379
+ /**
1380
+ * Default baseline prepended to every subagent's system prompt. Tells the
1381
+ * subagent its place in the hierarchy and the bridge contract — without
1382
+ * this, a subagent has no way to know it *can* ask the parent for
1383
+ * clarification, and it will hallucinate answers when context is missing.
1384
+ *
1385
+ * Bridge contract: subagents may `send` progress and `request` answers, but
1386
+ * MAY NOT exfiltrate the parent's full system prompt or tools list. The
1387
+ * baseline reinforces this in plain text — the actual enforcement is at
1388
+ * the bridge transport layer.
1389
+ */
1390
+ declare const DEFAULT_SUBAGENT_BASELINE = "You are a subagent operating under a Director. You were spawned to handle\na specific slice of a larger plan \u2014 do that slice well and report back.\n\nBridge contract:\n - You have a parent (the Director). You may call `request` on the\n parent bridge to ask a clarifying question. Use this sparingly; the\n parent is also working.\n - You MAY NOT request the parent's system prompt, tool list, or other\n subagents' context. Those are not yours to read.\n - Your final task output is what the Director sees. Be concise,\n structured, and self-contained \u2014 assume the Director will paste your\n output into its own context.";
1391
+ /** Parts the leader-prompt composer accepts. All optional. */
1392
+ interface DirectorPromptParts {
1393
+ /** The user's existing leader system prompt — typically what was passed
1394
+ * via `MultiAgentConfig.leaderSystemPrompt`. */
1395
+ basePrompt?: string;
1396
+ /** Override the built-in fleet preamble. Pass empty string to suppress. */
1397
+ directorPreamble?: string;
1398
+ /** Optional roster summary block — a short list of pre-configured roles
1399
+ * the director can spawn (e.g. "researcher, coder, reviewer"). Helps
1400
+ * small models discover the available shapes without scanning tools. */
1401
+ rosterSummary?: string;
1402
+ }
1403
+ /**
1404
+ * Compose the leader/director's system prompt. Order:
1405
+ * 1. Director preamble (fleet protocol)
1406
+ * 2. Roster summary (optional, when provided)
1407
+ * 3. User base prompt (the per-project leader prompt)
1408
+ *
1409
+ * Sections are separated by a blank line. Empty parts are skipped so the
1410
+ * output never contains stray blank-line runs.
1411
+ */
1412
+ declare function composeDirectorPrompt(parts?: DirectorPromptParts): string;
1413
+ /** Parts the subagent-prompt composer accepts. Layered from generic to
1414
+ * specific; later layers override earlier ones when they conflict. */
1415
+ interface SubagentPromptParts {
1416
+ /** Base persona/identity for *every* subagent. Defaults to the bridge
1417
+ * contract baseline. Pass empty string to suppress. */
1418
+ baseline?: string;
1419
+ /** Role-specific block, e.g. "You are a code reviewer. Focus on…". */
1420
+ role?: string;
1421
+ /** Task brief — usually the same string the runner passes as user input,
1422
+ * but exposed here in case the factory wants it duplicated in the
1423
+ * system prompt for reinforcement. */
1424
+ task?: string;
1425
+ /**
1426
+ * Absolute path to a shared scratchpad directory the whole fleet can
1427
+ * read/write. When set, the composer adds a "Shared notes" block that
1428
+ * tells the subagent where to drop findings and where to look for
1429
+ * sibling output. This is the cheap fleet-coordination channel —
1430
+ * agents don't need each other's transcripts, just each other's
1431
+ * conclusions. Falls between `task` and `override` so the override
1432
+ * can still narrow or replace it.
1433
+ */
1434
+ sharedScratchpad?: string;
1435
+ /** Final per-spawn override from `SubagentConfig.systemPromptOverride`.
1436
+ * Added last so it wins on conflict — that's by design: the spawn site
1437
+ * knows the most about what this specific subagent should do. */
1438
+ override?: string;
1439
+ }
1440
+ /**
1441
+ * Compose a subagent's system prompt. Order:
1442
+ * 1. Baseline (bridge contract)
1443
+ * 2. Role
1444
+ * 3. Task brief
1445
+ * 4. Per-spawn override
1446
+ *
1447
+ * Same blank-line-separated joining as the director composer.
1448
+ *
1449
+ * Layering rationale: the baseline never needs to change between
1450
+ * subagents; the role is the "what kind of worker is this"; the task is
1451
+ * the "what should you do *now*"; the override is the spawn-site escape
1452
+ * hatch ("…and respond only in JSON"). Putting override last means it
1453
+ * never gets squashed by something earlier in the chain.
1454
+ */
1455
+ declare function composeSubagentPrompt(parts?: SubagentPromptParts): string;
1456
+ /**
1457
+ * Render a short bullet list summarising a roster — useful for stuffing
1458
+ * into `composeDirectorPrompt({ rosterSummary })` so the director model
1459
+ * can see available roles without scanning tool descriptions.
1460
+ *
1461
+ * Each entry: `- <role-id>: <name>[ (provider/model)] — <prompt-headline>`
1462
+ * The prompt headline is the first non-empty line of `config.prompt`,
1463
+ * truncated to 80 chars. Skipped entirely when the role has no prompt.
1464
+ */
1465
+ declare function rosterSummaryFromConfigs(roster: Record<string, {
1466
+ name: string;
1467
+ provider?: string;
1468
+ model?: string;
1469
+ prompt?: string;
1470
+ role?: string;
1471
+ }>): string;
1472
+
1473
+ /**
1474
+ * Pre-built subagent role configurations for the WrongStack fleet.
1475
+ * These can be passed to `MultiAgentHost.spawn()` or used as templates
1476
+ * for the director's roster.
1477
+ */
1478
+
1479
+ /**
1480
+ * Audit Log Agent — analyzes session logs, event streams, and traces.
1481
+ * Use for: post-mortems, trend analysis, operational insights.
1482
+ */
1483
+ declare const AUDIT_LOG_AGENT: SubagentConfig;
1484
+ /**
1485
+ * Bug Hunter Agent — systematic bug and code smell detection.
1486
+ * Use for: pre-refactoring health checks, code review, regression prevention.
1487
+ */
1488
+ declare const BUG_HUNTER_AGENT: SubagentConfig;
1489
+ /**
1490
+ * Refactor Planner Agent — structured refactoring planning.
1491
+ * Use for: large rewrites, technical debt reduction, architecture improvements.
1492
+ */
1493
+ declare const REFACTOR_PLANNER_AGENT: SubagentConfig;
1494
+ /**
1495
+ * Security Scanner Agent — vulnerability and secret detection.
1496
+ * Use for: CI checks, pre-release audits, dependency vulnerability scanning.
1497
+ */
1498
+ declare const SECURITY_SCANNER_AGENT: SubagentConfig;
1499
+ /** All pre-built agents in a map for easy lookup by role. */
1500
+ declare const FLEET_ROSTER: Record<string, SubagentConfig>;
1501
+ /** Quick-access list for spawning all at once. */
1502
+ declare const ALL_FLEET_AGENTS: SubagentConfig[];
1503
+
913
1504
  type AutonomousResult = RunResult & {
914
1505
  toolCalls: number;
915
1506
  reason?: string;
@@ -950,6 +1541,7 @@ declare class AutonomousRunner {
950
1541
  private readonly doneChecker;
951
1542
  constructor(opts: AutonomousRunnerOptions);
952
1543
  run(): Promise<AutonomousResult>;
1544
+ private runLoop;
953
1545
  stop(): void;
954
1546
  }
955
1547
 
@@ -1714,4 +2306,4 @@ declare const sentinelServer: () => MCPServerConfig;
1714
2306
  /** Everything bundled — full set of built-in servers. Useful for `wstack mcp add --all`. */
1715
2307
  declare const allServers: () => Record<string, MCPServerConfig>;
1716
2308
 
1717
- export { type AbandonedSession, type AgentFactory, type AgentFactoryResult, type AgentRunnerOptions, type AttachmentStoreOptions, AutoCompactionMiddleware, AutonomousRunner, type AutonomousRunnerOptions, type CompactorOptions, type ConfigLoaderOptions, type ConfigMigration, ConfigMigrationError, type ConfigSource, type ContextManagerAction, type ContextManagerInput, type ContextManagerResult, type ContextManagerToolOptions, DEFAULT_CONFIG_MIGRATIONS, DefaultAttachmentStore, DefaultConfigLoader, DefaultConfigStore, DefaultErrorHandler, DefaultHealthRegistry, DefaultLogger, type DefaultLoggerOptions, DefaultMemoryStore, DefaultModeStore, DefaultModelsRegistry, type DefaultModelsRegistryOptions, DefaultMultiAgentCoordinator, DefaultPathResolver, DefaultPermissionPolicy, DefaultRetryPolicy, DefaultSecretScrubber, DefaultSecretVault, DefaultSessionReader, DefaultSessionStore, DefaultSkillLoader, DefaultTaskStore, DefaultTokenCounter, type DoneCheckResult, DoneConditionChecker, type GeneratedTask, HybridCompactor, InMemoryAgentBridge, InMemoryBridgeTransport, InMemoryMetricsSink, IntelligentCompactor, type IntelligentCompactorOptions, LLMSelector, type LLMSelectorOptions, type MemoryStoreOptions, type MetricsServerHandle, type MetricsServerOptions, type MigrationContext, type MigrationResult, type ModeLoaderOptions, type MultiAgentCoordinatorOptions, NoopMetricsSink, NoopTracer, OTelTracer, type OtlpMetricsExporterHandle, type OtlpMetricsExporterOptions, type OtlpTraceExporterHandle, type OtlpTraceExporterOptions, PROMETHEUS_CONTENT_TYPE, type PermissionPolicyOptions, type PersistedQueueItem, QueueStore, RecoveryLock, type RecoveryLockOptions, type SecretVaultOptions, SelectiveCompactor, type SelectiveCompactorOptions, type SessionStoreOptions, type SkillLoaderOptions, SpecDrivenDev, type SpecDrivenDevOptions, SpecParser, TaskFlow, type TaskFlowEventMap, type TaskFlowEventName, type TaskFlowExecutionContext, type TaskFlowOptions, type TaskFlowPhase, TaskGenerator, type TaskGeneratorOptions, type TaskStore, TaskTracker, type TaskTrackerOptions, type TaskTransition, ToolExecutor, allServers, awsServer, blockServer, braveSearchServer, buildOtlpMetricsRequest, buildOtlpTracesRequest, classifyFamily, context7Server, contextManagerTool, createContextManagerTool, createMessage, decryptConfigSecrets, encryptConfigSecrets, everArtServer, filesystemServer, githubServer, googleMapsServer, loadProjectModes, loadUserModes, makeAgentSubagentRunner, migratePlaintextSecrets, renderPrometheus, rewriteConfigEncrypted, runConfigMigrations, sentinelServer, slackServer, startMetricsServer, startOtlpMetricsExporter, startOtlpTraceExporter, wireMetricsToEvents };
2309
+ export { ALL_FLEET_AGENTS, AUDIT_LOG_AGENT, type AbandonedSession, type AgentFactory, type AgentFactoryResult, type AgentRunnerOptions, type AttachmentStoreOptions, AutoCompactionMiddleware, AutonomousRunner, type AutonomousRunnerOptions, BUG_HUNTER_AGENT, type CompactorOptions, type ConfigLoaderOptions, type ConfigMigration, ConfigMigrationError, type ConfigSource, type ContextManagerAction, type ContextManagerInput, type ContextManagerResult, type ContextManagerToolOptions, DEFAULT_CONFIG_MIGRATIONS, DEFAULT_DIRECTOR_PREAMBLE, DEFAULT_SUBAGENT_BASELINE, DefaultAttachmentStore, DefaultConfigLoader, DefaultConfigStore, DefaultErrorHandler, DefaultHealthRegistry, DefaultLogger, type DefaultLoggerOptions, DefaultMemoryStore, DefaultModeStore, DefaultModelsRegistry, type DefaultModelsRegistryOptions, DefaultMultiAgentCoordinator, DefaultPathResolver, DefaultPermissionPolicy, DefaultRetryPolicy, DefaultSecretScrubber, DefaultSecretVault, DefaultSessionReader, DefaultSessionStore, DefaultSkillLoader, DefaultTaskStore, DefaultTokenCounter, Director, DirectorBudgetError, type DirectorPromptParts, type DirectorSessionFactory, type DirectorSessionFactoryOptions, type DoneCheckResult, DoneConditionChecker, FLEET_ROSTER, FleetBus, type FleetEvent, type FleetHandler, type FleetUsage, FleetUsageAggregator, type GeneratedTask, HybridCompactor, InMemoryAgentBridge, InMemoryBridgeTransport, InMemoryMetricsSink, IntelligentCompactor, type IntelligentCompactorOptions, LLMSelector, type LLMSelectorOptions, type MemoryStoreOptions, type MetricsServerHandle, type MetricsServerOptions, type MigrationContext, type MigrationResult, type ModeLoaderOptions, type MultiAgentCoordinatorOptions, NoopMetricsSink, NoopTracer, OTelTracer, type OtlpMetricsExporterHandle, type OtlpMetricsExporterOptions, type OtlpTraceExporterHandle, type OtlpTraceExporterOptions, PROMETHEUS_CONTENT_TYPE, type PermissionPolicyOptions, type PersistedQueueItem, QueueStore, REFACTOR_PLANNER_AGENT, RecoveryLock, type RecoveryLockOptions, SECURITY_SCANNER_AGENT, type SecretVaultOptions, SelectiveCompactor, type SelectiveCompactorOptions, type SessionStoreOptions, type SkillLoaderOptions, SpecDrivenDev, type SpecDrivenDevOptions, SpecParser, type SubagentPromptParts, type SubagentUsageSnapshot, TaskFlow, type TaskFlowEventMap, type TaskFlowEventName, type TaskFlowExecutionContext, type TaskFlowOptions, type TaskFlowPhase, TaskGenerator, type TaskGeneratorOptions, type TaskStore, TaskTracker, type TaskTrackerOptions, type TaskTransition, ToolExecutor, allServers, awsServer, blockServer, braveSearchServer, buildOtlpMetricsRequest, buildOtlpTracesRequest, classifyFamily, composeDirectorPrompt, composeSubagentPrompt, context7Server, contextManagerTool, createContextManagerTool, createMessage, decryptConfigSecrets, encryptConfigSecrets, everArtServer, filesystemServer, githubServer, googleMapsServer, loadProjectModes, loadUserModes, makeAgentSubagentRunner, makeDirectorSessionFactory, migratePlaintextSecrets, renderPrometheus, rewriteConfigEncrypted, rosterSummaryFromConfigs, runConfigMigrations, sentinelServer, slackServer, startMetricsServer, startOtlpMetricsExporter, startOtlpTraceExporter, wireMetricsToEvents };