operon-agents-core 0.1.0-alpha.2 → 0.1.0-alpha.4
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/dist/agent/index.d.ts +2 -2
- package/dist/agent/index.js +1 -1
- package/dist/agent/runner.d.ts +21 -15
- package/dist/agent/runner.js +23 -26
- package/dist/agent/session.d.ts +67 -73
- package/dist/agent/session.js +170 -174
- package/dist/agent/workflow/tool.js +8 -21
- package/dist/capabilities/assembler.d.ts +6 -4
- package/dist/capabilities/assembler.js +10 -5
- package/dist/capabilities/background/index.js +33 -26
- package/dist/capabilities/capability-state.d.ts +5 -7
- package/dist/capabilities/capability-state.js +6 -4
- package/dist/capabilities/capability.d.ts +52 -40
- package/dist/capabilities/compaction/index.js +5 -4
- package/dist/capabilities/goal/index.d.ts +1 -0
- package/dist/capabilities/goal/index.js +13 -6
- package/dist/capabilities/index.d.ts +3 -3
- package/dist/capabilities/index.js +2 -2
- package/dist/capabilities/plan/index.js +12 -6
- package/dist/capabilities/skills/flow-provider.js +3 -3
- package/dist/capabilities/skills/index.d.ts +12 -0
- package/dist/capabilities/skills/index.js +38 -24
- package/dist/capabilities/skills/registry.d.ts +3 -4
- package/dist/capabilities/skills/registry.js +4 -7
- package/dist/capabilities/skills/service.js +1 -1
- package/dist/capabilities/skills/skill-tool.d.ts +2 -0
- package/dist/capabilities/skills/skill-tool.js +1 -1
- package/dist/capabilities/task/index.js +11 -5
- package/dist/capabilities/todo/index.js +12 -6
- package/dist/capabilities/tool-provider.d.ts +3 -3
- package/dist/capabilities/user-hooks/index.js +22 -16
- package/dist/capabilities/workflow/index.d.ts +4 -2
- package/dist/capabilities/workflow/index.js +14 -7
- package/dist/commands/index.js +6 -4
- package/dist/events/events.d.ts +2 -1
- package/dist/events/publisher.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/internal.d.ts +2 -0
- package/dist/internal.js +2 -0
- package/dist/loop/steer.d.ts +2 -0
- package/dist/loop/steer.js +7 -2
- package/dist/mcp/capability.js +22 -17
- package/dist/mcp/filter.d.ts +2 -2
- package/dist/mcp/index.d.ts +1 -1
- package/dist/mcp/index.js +1 -1
- package/dist/mcp/manager.d.ts +45 -2
- package/dist/mcp/manager.js +162 -11
- package/dist/plugins/index.js +10 -4
- package/dist/scope/index.d.ts +7 -0
- package/dist/scope/index.js +4 -0
- package/dist/scope/scope.d.ts +143 -0
- package/dist/scope/scope.js +406 -0
- package/dist/scope/token.d.ts +29 -0
- package/dist/scope/token.js +29 -0
- package/dist/scope/tokens.d.ts +98 -0
- package/dist/scope/tokens.js +63 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/store/origin.d.ts +9 -0
- package/dist/store/store.d.ts +6 -2
- package/dist/testing.d.ts +62 -0
- package/dist/testing.js +118 -0
- package/package.json +1 -1
package/dist/mcp/manager.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TransportMCPServer } from "./server.ts";
|
|
2
|
-
import type { MCPTransport } from "./types.ts";
|
|
2
|
+
import type { MCPTool, MCPTransport } from "./types.ts";
|
|
3
3
|
import type { McpOAuthService } from "./oauth/index.ts";
|
|
4
|
-
import type { Capability } from "../index.ts";
|
|
4
|
+
import type { Capability, Scope, ToolProvider } from "../index.ts";
|
|
5
5
|
import type { McpServerConfig } from "../config/schema.ts";
|
|
6
6
|
export type McpServerStatus = "pending" | "connected" | "failed" | "disabled" | "needs-auth";
|
|
7
7
|
export interface McpServerView {
|
|
@@ -41,11 +41,54 @@ export interface McpServersCapabilityOptions {
|
|
|
41
41
|
readonly keepAlive?: KeepAlivePolicy;
|
|
42
42
|
readonly timer?: McpTimer;
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* What `connect` binds the controllers to: the scope whose `T.Events` receives status warnings
|
|
46
|
+
* (a session's, or a workspace's — which has none, so warnings go through `onStatusChange`)
|
|
47
|
+
* and the session id stamped on them. A `ProvisionContext` is one of these.
|
|
48
|
+
*/
|
|
49
|
+
export interface McpConnectContext {
|
|
50
|
+
readonly scope: Scope;
|
|
51
|
+
readonly sessionId: string;
|
|
52
|
+
}
|
|
44
53
|
export interface McpServersHandle {
|
|
45
54
|
list(): readonly McpServerView[];
|
|
55
|
+
/** Tools a connected server exposes. Empty for an unknown or disconnected server. */
|
|
56
|
+
listTools(name: string): Promise<readonly MCPTool[]>;
|
|
46
57
|
reconnect(name: string): Promise<void>;
|
|
47
58
|
onStatusChange(listener: McpStatusListener): () => void;
|
|
59
|
+
/** One runnable-tool provider per server (what a session hands the model). */
|
|
60
|
+
toolProviders(): readonly ToolProvider[];
|
|
61
|
+
/** Connect every server in parallel, fault-isolated (a failure is a status, never a throw). */
|
|
62
|
+
connect(ctx: McpConnectContext): Promise<void>;
|
|
48
63
|
shutdown(): Promise<void>;
|
|
49
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* The controller set behind a group of configured servers — buildable on its own so a WORKSPACE
|
|
67
|
+
* can hold one set of connections for every session under it (`T.McpServers`), or a session can
|
|
68
|
+
* own a private set (`mcpServersCapability`). Nothing connects until `handle.connect()`.
|
|
69
|
+
*/
|
|
70
|
+
export declare function createMcpServers(configs: Record<string, McpServerConfig>, options?: McpServersCapabilityOptions): McpServersHandle;
|
|
71
|
+
/**
|
|
72
|
+
* Session-owned MCP servers: the controllers are built and connected for THIS session and shut
|
|
73
|
+
* down with it. For servers shared by every session of a workspace, register a
|
|
74
|
+
* `createMcpServers()` handle as `T.McpServers` on the workspace scope and use
|
|
75
|
+
* {@link mcpSessionCapability} instead.
|
|
76
|
+
*/
|
|
50
77
|
export declare function mcpServersCapability(configs: Record<string, McpServerConfig>, options?: McpServersCapabilityOptions): Capability;
|
|
78
|
+
/**
|
|
79
|
+
* The session-tier MCP capability: a VIEW over the workspace's shared servers (`T.McpServers`),
|
|
80
|
+
* optionally with servers PRIVATE to this session layered on top.
|
|
81
|
+
*
|
|
82
|
+
* The workspace half connects nothing and shuts nothing down — the workspace scope owns those
|
|
83
|
+
* connections; this capability only hands the model their tools, exposes them as `T.Mcp` (so
|
|
84
|
+
* `session.listMcpServers()` and friends work) and mirrors status changes into this session's
|
|
85
|
+
* event stream as warnings.
|
|
86
|
+
*
|
|
87
|
+
* `configs` (from `SessionCapabilityContext.mcpServers` — a REPL kernel keyed by conversation id,
|
|
88
|
+
* say) is the OVERLAY: those servers are built, connected and shut down with THIS session. A name
|
|
89
|
+
* in `configs` SHADOWS the workspace server of the same name for this session — the two would
|
|
90
|
+
* otherwise produce identical `mcp__<name>__<tool>` tool names. Shadowing is a view-level
|
|
91
|
+
* decision: the workspace connection itself keeps running for every other session.
|
|
92
|
+
*/
|
|
93
|
+
export declare function mcpSessionCapability(configs?: Record<string, McpServerConfig>, options?: McpServersCapabilityOptions): Capability;
|
|
51
94
|
//# sourceMappingURL=manager.d.ts.map
|
package/dist/mcp/manager.js
CHANGED
|
@@ -3,6 +3,11 @@ import { mcpStdioTransport, mcpStreamableHttpTransport } from "./sdk-transport.j
|
|
|
3
3
|
import { mcpToolProvider } from "./provider.js";
|
|
4
4
|
import { qualifyMcpToolName } from "./tool-naming.js";
|
|
5
5
|
import { createMcpAuthTool } from "./auth-tool.js";
|
|
6
|
+
import { T } from "../scope/tokens.js";
|
|
7
|
+
/** The `ToolProvider.id` a server's provider carries — `mcp:<server name>`, unsanitized. */
|
|
8
|
+
function mcpProviderId(serverName) {
|
|
9
|
+
return `mcp:${serverName}`;
|
|
10
|
+
}
|
|
6
11
|
export const DEFAULT_RECONNECT_POLICY = {
|
|
7
12
|
maxRetries: 5,
|
|
8
13
|
initialDelayMs: 1000,
|
|
@@ -99,7 +104,7 @@ class McpServerController {
|
|
|
99
104
|
timer;
|
|
100
105
|
server;
|
|
101
106
|
authTool;
|
|
102
|
-
|
|
107
|
+
session;
|
|
103
108
|
_status = "pending";
|
|
104
109
|
_error;
|
|
105
110
|
reconnectAttempts = 0;
|
|
@@ -135,7 +140,7 @@ class McpServerController {
|
|
|
135
140
|
async connect(ctx) {
|
|
136
141
|
if (this.disposed)
|
|
137
142
|
return;
|
|
138
|
-
this.
|
|
143
|
+
this.session = { events: ctx.scope.get(T.Events), sessionId: ctx.sessionId };
|
|
139
144
|
if (this.config.enabled === false) {
|
|
140
145
|
this.setStatus("disabled");
|
|
141
146
|
return;
|
|
@@ -220,9 +225,23 @@ class McpServerController {
|
|
|
220
225
|
this.disposed = true;
|
|
221
226
|
await this.close();
|
|
222
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* What this server offers, for a host that wants to SHOW it rather than call it.
|
|
230
|
+
*
|
|
231
|
+
* Separate from `toolProvider().listTools`, which exists to hand the model runnable
|
|
232
|
+
* tools: that one substitutes an auth tool while a server is stuck on OAuth, and it
|
|
233
|
+
* namespaces every name. This returns the server's own list, unwrapped, and an empty
|
|
234
|
+
* one whenever the server is not connected — a disconnected server has no tools to
|
|
235
|
+
* describe, and asking is not worth an exception.
|
|
236
|
+
*/
|
|
237
|
+
async listTools() {
|
|
238
|
+
if (this._status !== "connected" || this.server === undefined)
|
|
239
|
+
return [];
|
|
240
|
+
return await this.server.listTools().catch(() => []);
|
|
241
|
+
}
|
|
223
242
|
toolProvider() {
|
|
224
243
|
return {
|
|
225
|
-
id:
|
|
244
|
+
id: mcpProviderId(this.name),
|
|
226
245
|
listTools: async (ctx) => {
|
|
227
246
|
if (this._status === "needs-auth")
|
|
228
247
|
return [this.authToolInstance()];
|
|
@@ -345,7 +364,7 @@ class McpServerController {
|
|
|
345
364
|
emitWarning(message) {
|
|
346
365
|
if (message === undefined)
|
|
347
366
|
return;
|
|
348
|
-
this.
|
|
367
|
+
this.session?.events?.emit({ type: "warning", address: "main", sessionId: this.session.sessionId, message });
|
|
349
368
|
}
|
|
350
369
|
}
|
|
351
370
|
function keepAliveOptions(config, defaults) {
|
|
@@ -355,7 +374,12 @@ function keepAliveOptions(config, defaults) {
|
|
|
355
374
|
const timeoutMs = config.keepAliveTimeoutMs ?? defaults?.timeoutMs;
|
|
356
375
|
return { keepAlivePolicy: timeoutMs === undefined ? { intervalMs } : { intervalMs, timeoutMs } };
|
|
357
376
|
}
|
|
358
|
-
|
|
377
|
+
/**
|
|
378
|
+
* The controller set behind a group of configured servers — buildable on its own so a WORKSPACE
|
|
379
|
+
* can hold one set of connections for every session under it (`T.McpServers`), or a session can
|
|
380
|
+
* own a private set (`mcpServersCapability`). Nothing connects until `handle.connect()`.
|
|
381
|
+
*/
|
|
382
|
+
export function createMcpServers(configs, options = {}) {
|
|
359
383
|
const listeners = new Set();
|
|
360
384
|
const notify = (view) => {
|
|
361
385
|
for (const listener of listeners)
|
|
@@ -384,8 +408,10 @@ export function mcpServersCapability(configs, options = {}) {
|
|
|
384
408
|
})();
|
|
385
409
|
return shuttingDown;
|
|
386
410
|
};
|
|
411
|
+
const providers = controllers.map((c) => c.toolProvider());
|
|
387
412
|
const handle = {
|
|
388
413
|
list: () => controllers.map((c) => c.view()),
|
|
414
|
+
listTools: async (name) => (await byName.get(name)?.listTools()) ?? [],
|
|
389
415
|
reconnect: async (name) => {
|
|
390
416
|
const controller = byName.get(name);
|
|
391
417
|
if (controller === undefined)
|
|
@@ -396,17 +422,142 @@ export function mcpServersCapability(configs, options = {}) {
|
|
|
396
422
|
listeners.add(listener);
|
|
397
423
|
return () => listeners.delete(listener);
|
|
398
424
|
},
|
|
425
|
+
toolProviders: () => providers,
|
|
426
|
+
connect: async (ctx) => {
|
|
427
|
+
await Promise.all(controllers.map((c) => c.connect(ctx)));
|
|
428
|
+
},
|
|
399
429
|
shutdown,
|
|
400
430
|
};
|
|
431
|
+
return handle;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Session-owned MCP servers: the controllers are built and connected for THIS session and shut
|
|
435
|
+
* down with it. For servers shared by every session of a workspace, register a
|
|
436
|
+
* `createMcpServers()` handle as `T.McpServers` on the workspace scope and use
|
|
437
|
+
* {@link mcpSessionCapability} instead.
|
|
438
|
+
*/
|
|
439
|
+
export function mcpServersCapability(configs, options = {}) {
|
|
440
|
+
const handle = createMcpServers(configs, options);
|
|
401
441
|
return {
|
|
402
442
|
name: "mcp",
|
|
403
|
-
toolProviders:
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
443
|
+
toolProviders: [...handle.toolProviders()],
|
|
444
|
+
provides: [
|
|
445
|
+
{
|
|
446
|
+
token: T.Mcp,
|
|
447
|
+
// Session-tier: connect all servers in parallel, fault-isolated (attempt() never throws).
|
|
448
|
+
create: async (ctx) => {
|
|
449
|
+
await handle.connect(ctx);
|
|
450
|
+
return handle;
|
|
451
|
+
},
|
|
452
|
+
dispose: () => handle.shutdown(),
|
|
453
|
+
},
|
|
454
|
+
],
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* The session-tier MCP capability: a VIEW over the workspace's shared servers (`T.McpServers`),
|
|
459
|
+
* optionally with servers PRIVATE to this session layered on top.
|
|
460
|
+
*
|
|
461
|
+
* The workspace half connects nothing and shuts nothing down — the workspace scope owns those
|
|
462
|
+
* connections; this capability only hands the model their tools, exposes them as `T.Mcp` (so
|
|
463
|
+
* `session.listMcpServers()` and friends work) and mirrors status changes into this session's
|
|
464
|
+
* event stream as warnings.
|
|
465
|
+
*
|
|
466
|
+
* `configs` (from `SessionCapabilityContext.mcpServers` — a REPL kernel keyed by conversation id,
|
|
467
|
+
* say) is the OVERLAY: those servers are built, connected and shut down with THIS session. A name
|
|
468
|
+
* in `configs` SHADOWS the workspace server of the same name for this session — the two would
|
|
469
|
+
* otherwise produce identical `mcp__<name>__<tool>` tool names. Shadowing is a view-level
|
|
470
|
+
* decision: the workspace connection itself keeps running for every other session.
|
|
471
|
+
*/
|
|
472
|
+
export function mcpSessionCapability(configs = {}, options = {}) {
|
|
473
|
+
const ownNames = new Set(Object.keys(configs));
|
|
474
|
+
const ownProviderIds = new Set([...ownNames].map(mcpProviderId));
|
|
475
|
+
const own = ownNames.size > 0 ? createMcpServers(configs, options) : undefined;
|
|
476
|
+
let unsubscribe;
|
|
477
|
+
const providers = [
|
|
478
|
+
...(own?.toolProviders() ?? []),
|
|
479
|
+
{
|
|
480
|
+
id: "mcp:workspace",
|
|
481
|
+
listTools: async (ctx) => {
|
|
482
|
+
const shared = ctx.scope.get(T.McpServers);
|
|
483
|
+
if (shared === undefined)
|
|
484
|
+
return [];
|
|
485
|
+
const tools = [];
|
|
486
|
+
for (const provider of shared.toolProviders()) {
|
|
487
|
+
// Skip a workspace server this session overlays: same name, same qualified tool names.
|
|
488
|
+
if (ownProviderIds.has(provider.id))
|
|
489
|
+
continue;
|
|
490
|
+
tools.push(...(await provider.listTools(ctx)));
|
|
491
|
+
}
|
|
492
|
+
return tools;
|
|
493
|
+
},
|
|
408
494
|
},
|
|
409
|
-
|
|
495
|
+
];
|
|
496
|
+
/** The merged handle behind `T.Mcp`: overlay first, workspace for everything it doesn't name. */
|
|
497
|
+
const merge = (shared, overlay) => ({
|
|
498
|
+
list: () => [...overlay.list(), ...shared.list().filter((v) => !ownNames.has(v.name))],
|
|
499
|
+
listTools: (name) => (ownNames.has(name) ? overlay.listTools(name) : shared.listTools(name)),
|
|
500
|
+
reconnect: (name) => (ownNames.has(name) ? overlay.reconnect(name) : shared.reconnect(name)),
|
|
501
|
+
onStatusChange: (listener) => {
|
|
502
|
+
const offOwn = overlay.onStatusChange(listener);
|
|
503
|
+
const offShared = shared.onStatusChange((view) => {
|
|
504
|
+
if (!ownNames.has(view.name))
|
|
505
|
+
listener(view);
|
|
506
|
+
});
|
|
507
|
+
return () => {
|
|
508
|
+
offOwn();
|
|
509
|
+
offShared();
|
|
510
|
+
};
|
|
511
|
+
},
|
|
512
|
+
toolProviders: () => providers,
|
|
513
|
+
// Only the overlay is this session's to run: the workspace half is already connected, and
|
|
514
|
+
// outlives the session.
|
|
515
|
+
connect: (ctx) => overlay.connect(ctx),
|
|
516
|
+
shutdown: () => overlay.shutdown(),
|
|
517
|
+
});
|
|
518
|
+
return {
|
|
519
|
+
name: "mcp",
|
|
520
|
+
toolProviders: providers,
|
|
521
|
+
provides: [
|
|
522
|
+
{
|
|
523
|
+
token: T.Mcp,
|
|
524
|
+
create: async (ctx) => {
|
|
525
|
+
const shared = ctx.scope.get(T.McpServers);
|
|
526
|
+
// No shared servers and no overlay means this capability should never have been
|
|
527
|
+
// assembled — keep the original diagnostic rather than handing back an empty handle.
|
|
528
|
+
if (shared === undefined && own === undefined)
|
|
529
|
+
return ctx.scope.require(T.McpServers);
|
|
530
|
+
// Session-owned controllers emit their own warnings once connected (they hold the
|
|
531
|
+
// session); the workspace ones have no session, so this session subscribes for them.
|
|
532
|
+
if (own !== undefined)
|
|
533
|
+
await own.connect(ctx);
|
|
534
|
+
if (shared !== undefined) {
|
|
535
|
+
const events = ctx.scope.get(T.Events);
|
|
536
|
+
const warn = (view) => {
|
|
537
|
+
if (ownNames.has(view.name))
|
|
538
|
+
return;
|
|
539
|
+
if (view.status !== "failed" || view.error === undefined)
|
|
540
|
+
return;
|
|
541
|
+
events?.emit({ type: "warning", address: "main", sessionId: ctx.sessionId, message: `MCP server "${view.name}" failed: ${view.error}` });
|
|
542
|
+
};
|
|
543
|
+
// Servers that failed before this session existed: tell it once at open.
|
|
544
|
+
for (const view of shared.list())
|
|
545
|
+
warn(view);
|
|
546
|
+
unsubscribe = shared.onStatusChange(warn);
|
|
547
|
+
}
|
|
548
|
+
if (shared === undefined)
|
|
549
|
+
return own;
|
|
550
|
+
if (own === undefined)
|
|
551
|
+
return shared;
|
|
552
|
+
return merge(shared, own);
|
|
553
|
+
},
|
|
554
|
+
dispose: async () => {
|
|
555
|
+
unsubscribe?.();
|
|
556
|
+
unsubscribe = undefined;
|
|
557
|
+
await own?.shutdown();
|
|
558
|
+
},
|
|
559
|
+
},
|
|
560
|
+
],
|
|
410
561
|
};
|
|
411
562
|
}
|
|
412
563
|
//# sourceMappingURL=manager.js.map
|
package/dist/plugins/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { T } from "../scope/tokens.js";
|
|
1
2
|
import { PluginManager } from "./manager.js";
|
|
2
3
|
import { PluginSessionStartInjector } from "./injector.js";
|
|
3
4
|
export { PluginManager } from "./manager.js";
|
|
@@ -13,10 +14,15 @@ export { PLUGIN_NAME_REGEX, normalizePluginId, } from "./types.js";
|
|
|
13
14
|
export function pluginsCapability(manager, resolveSkill) {
|
|
14
15
|
return {
|
|
15
16
|
name: "plugins",
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
provides: [
|
|
18
|
+
{
|
|
19
|
+
token: T.Plugins,
|
|
20
|
+
create: async () => {
|
|
21
|
+
await manager.load();
|
|
22
|
+
return manager;
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
],
|
|
20
26
|
injectors: [new PluginSessionStartInjector(manager, resolveSkill)],
|
|
21
27
|
};
|
|
22
28
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { Scope, ServiceUnavailableError, deadHandle, isProbeProperty } from "./scope.ts";
|
|
2
|
+
export type { RegisterOptions, CloseOptions, ServiceUnavailableReason, Below } from "./scope.ts";
|
|
3
|
+
export { token, SCOPE_ORDER, resetTokenDeclarationsForTest } from "./token.ts";
|
|
4
|
+
export type { Token, ScopeKind } from "./token.ts";
|
|
5
|
+
export { T } from "./tokens.ts";
|
|
6
|
+
export type { SessionLogReader } from "./tokens.ts";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scoped service registry with handle indirection — the one place that answers "who builds
|
|
3
|
+
* this object, how many exist, how long does it live, and what tears it down before what".
|
|
4
|
+
*
|
|
5
|
+
* Three tiers, each a `Scope` with a parent: harness → workspace → session. A lookup walks up
|
|
6
|
+
* the chain, so a session-scoped consumer reaches process-level services without anything
|
|
7
|
+
* being threaded through by hand, and a child that registers the same token OVERRIDES its
|
|
8
|
+
* parent's for everything under it. `register` takes a built instance; `provide` takes a
|
|
9
|
+
* synchronous default factory that runs on first use in this scope. Register wins over provide.
|
|
10
|
+
*
|
|
11
|
+
* Hot swapping (design: docs/architecture.md §5.5): consumers never hold a service instance.
|
|
12
|
+
* `handle()` returns a stable Proxy that resolves the CURRENT entry on every method call and
|
|
13
|
+
* holds a lease for exactly that call, so replacing a provider is a registry-local act — swap
|
|
14
|
+
* the entry, drain the old instance's leases, dispose it — with no consumer participation. The
|
|
15
|
+
* invariant this file exists to keep: at any moment only one generation of a service is being
|
|
16
|
+
* handed out.
|
|
17
|
+
*
|
|
18
|
+
* Handles expose METHODS ONLY. A method's return value must not carry the instance (no
|
|
19
|
+
* `return this`, no objects closing over it) — that is the one leak the Proxy cannot stop.
|
|
20
|
+
*
|
|
21
|
+
* Teardown: `close()` closes children first (newest first), then this scope's own entries in
|
|
22
|
+
* reverse registration order — drain, then dispose (default: `instance.close()` when present;
|
|
23
|
+
* `owned: false` skips disposal for objects the registrant merely lent). Parents are untouched.
|
|
24
|
+
*/
|
|
25
|
+
import { type ScopeKind, type Token } from "./token.ts";
|
|
26
|
+
/** The tiers a scope of kind `K` may open beneath it. */
|
|
27
|
+
export type Below<K extends ScopeKind> = K extends "harness" ? "workspace" | "session" : K extends "workspace" ? "session" : never;
|
|
28
|
+
export type ServiceUnavailableReason = "missing" | "draining";
|
|
29
|
+
export declare class ServiceUnavailableError extends Error {
|
|
30
|
+
readonly serviceName: string;
|
|
31
|
+
readonly reason: ServiceUnavailableReason;
|
|
32
|
+
constructor(serviceName: string, reason: ServiceUnavailableReason);
|
|
33
|
+
}
|
|
34
|
+
export interface RegisterOptions {
|
|
35
|
+
/** Absent/false = `replace()` rejects; swapping this service means restarting the process. */
|
|
36
|
+
readonly replaceable?: boolean;
|
|
37
|
+
/** Called once the old instance has drained. Default: call `instance.close()` when present. */
|
|
38
|
+
readonly dispose?: (instance: unknown) => void | Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* `false` = the scope does not own this object: it was lent by the registrant (a caller's
|
|
41
|
+
* machine, a repository's store) and is never disposed here. Default `true`.
|
|
42
|
+
*/
|
|
43
|
+
readonly owned?: boolean;
|
|
44
|
+
}
|
|
45
|
+
export interface CloseOptions {
|
|
46
|
+
/** How long to wait for in-flight handle calls before disposing anyway. Default 10s. */
|
|
47
|
+
readonly drainTimeoutMs?: number;
|
|
48
|
+
/** Deadline for one entry's dispose; a straggler is abandoned (logged) so close() never hangs. */
|
|
49
|
+
readonly disposeTimeoutMs?: number;
|
|
50
|
+
/** Where a dispose failure/timeout goes. Default: the scope's `warn`. */
|
|
51
|
+
readonly onDisposeError?: (name: string, error: unknown) => void;
|
|
52
|
+
}
|
|
53
|
+
export declare class Scope<K extends ScopeKind = ScopeKind> {
|
|
54
|
+
readonly kind: K;
|
|
55
|
+
readonly parent: Scope | undefined;
|
|
56
|
+
private readonly table;
|
|
57
|
+
private readonly providers;
|
|
58
|
+
/** Registration order (materialized providers included) — reversed by `close()`. */
|
|
59
|
+
private readonly order;
|
|
60
|
+
private readonly handles;
|
|
61
|
+
/** Per-name op chain: concurrent replace/unregister serialize instead of interleaving. */
|
|
62
|
+
private readonly ops;
|
|
63
|
+
private readonly children;
|
|
64
|
+
private readonly warn;
|
|
65
|
+
private _closed;
|
|
66
|
+
constructor(kind: K, parent?: Scope, options?: {
|
|
67
|
+
readonly warn?: (message: string) => void;
|
|
68
|
+
});
|
|
69
|
+
get closed(): boolean;
|
|
70
|
+
/** Open a nested scope. Tiers only go down: harness → workspace | session, workspace → session. */
|
|
71
|
+
child<C extends Below<K>>(kind: C): Scope<C>;
|
|
72
|
+
/** Register a built instance under a token. Duplicate names fail closed — a scope that wants
|
|
73
|
+
* to take over an existing name must go through `replace`, never shadow within one tier. */
|
|
74
|
+
register<T>(tok: Token<T, K>, instance: T, options?: RegisterOptions): void;
|
|
75
|
+
/**
|
|
76
|
+
* A default: `create` runs on the first `get` in THIS scope and the result is registered here
|
|
77
|
+
* (and disposed here). A prior `register` or `provide` of the same token wins silently — that
|
|
78
|
+
* is what "default" means; overriding is done with `register`.
|
|
79
|
+
*/
|
|
80
|
+
provide<T>(tok: Token<T, K>, create: (scope: Scope<K>) => T, options?: RegisterOptions): void;
|
|
81
|
+
/** This scope's entry, materializing a pending default, else the parent's. */
|
|
82
|
+
get<T>(tok: Token<T>): T | undefined;
|
|
83
|
+
require<T>(tok: Token<T>): T;
|
|
84
|
+
/** Registered (or provided) here or in any ancestor, and not draining. */
|
|
85
|
+
has<T>(tok: Token<T>): boolean;
|
|
86
|
+
/** Registered or provided in THIS scope (a pending default counts). */
|
|
87
|
+
hasLocal<T>(tok: Token<T>): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* The stable handle for a service — safe to take before the service is registered and to
|
|
90
|
+
* keep across replaces. Property access requires a method (a non-function property throws:
|
|
91
|
+
* replaceable services expose methods only); availability is judged at CALL time, so a
|
|
92
|
+
* handle taken during early setup never explodes on mere property access. Resolution walks
|
|
93
|
+
* the scope chain on every call, so a parent-level replace lands on a child's handle too.
|
|
94
|
+
*/
|
|
95
|
+
handle<T>(tok: Token<T>): T;
|
|
96
|
+
/**
|
|
97
|
+
* Swap the provider behind a token registered in THIS scope. Atomic from the consumers'
|
|
98
|
+
* side: the very next handle call lands on `next`; calls already in flight finish on the old
|
|
99
|
+
* instance, which is disposed once its leases drain (or after `drainTimeoutMs`, logged,
|
|
100
|
+
* default 10s).
|
|
101
|
+
*
|
|
102
|
+
* `force` skips the `replaceable` gate. It exists for ONE caller — `harness.replaceExtension`,
|
|
103
|
+
* where the extension that OWNS this service is being replaced along with it, inside the
|
|
104
|
+
* barrier's quiet moment. The gate guards the other case (a host swapping an instance under a
|
|
105
|
+
* live owner that never agreed to it), which is why it stays closed by default.
|
|
106
|
+
*/
|
|
107
|
+
replace<T>(tok: Token<T, K>, next: T, options?: {
|
|
108
|
+
readonly drainTimeoutMs?: number;
|
|
109
|
+
readonly force?: boolean;
|
|
110
|
+
}): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Remove a service of THIS scope entirely (extension unload). Unlike `replace`, callers see
|
|
113
|
+
* "draining" during the drain and "missing" after — there is deliberately no replaceable gate
|
|
114
|
+
* here: unload is the owner tearing its service down, not swapping code under consumers.
|
|
115
|
+
*/
|
|
116
|
+
unregister<T>(tok: Token<T, K>, options?: CloseOptions): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* Tear the scope down: children first (newest first), then this scope's entries in reverse
|
|
119
|
+
* registration order. Idempotent. Afterwards `register`/`provide`/`child` throw; `get` still
|
|
120
|
+
* reads the parent chain. Parents are never touched.
|
|
121
|
+
*/
|
|
122
|
+
close(options?: CloseOptions): Promise<void>;
|
|
123
|
+
private lookup;
|
|
124
|
+
private invoke;
|
|
125
|
+
private enqueueOp;
|
|
126
|
+
private retire;
|
|
127
|
+
private drainLeases;
|
|
128
|
+
private disposeOf;
|
|
129
|
+
private assertOpen;
|
|
130
|
+
private assertTier;
|
|
131
|
+
}
|
|
132
|
+
/** A handle whose every call throws "missing" — handed to detached extensions and to
|
|
133
|
+
* extensions on hosts that expose no services, mirroring the actions revocation collar. */
|
|
134
|
+
export declare function deadHandle<T>(tok: Token<T>): T;
|
|
135
|
+
/**
|
|
136
|
+
* Properties a proxy must NEVER intercept with a throw or a fake method — the probe surface
|
|
137
|
+
* generic JS pokes at (await → `then`, JSON.stringify → `toJSON` via the absent-property rule,
|
|
138
|
+
* class checks → `prototype`/`constructor`, test/inspect utilities → `_`-prefixed, serializers →
|
|
139
|
+
* numeric indices, symbols everywhere). Cordis's reflect layer converged on this exact list
|
|
140
|
+
* over years of proxy bugs (`isSpecialProperty`); we adopt it wholesale.
|
|
141
|
+
*/
|
|
142
|
+
export declare function isProbeProperty(prop: string | symbol): boolean;
|
|
143
|
+
//# sourceMappingURL=scope.d.ts.map
|