@denizokcu/haze 0.9.0 → 0.10.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 +57 -17
- package/README.md +119 -64
- package/dist/cli/chat/chatMetrics.d.ts +17 -1
- package/dist/cli/chat/chatMetrics.js +22 -5
- package/dist/cli/chat/contextReport.d.ts +11 -0
- package/dist/cli/chat/contextReport.js +44 -0
- package/dist/cli/chat/fileMentionSuggestions.d.ts +29 -0
- package/dist/cli/chat/fileMentionSuggestions.js +91 -0
- package/dist/cli/chat/inputSuggestions.d.ts +5 -0
- package/dist/cli/chat/inputSuggestions.js +17 -3
- package/dist/cli/chat/sessionLifecycle.d.ts +58 -0
- package/dist/cli/chat/sessionLifecycle.js +155 -0
- package/dist/cli/chat/sessionRecorder.js +7 -9
- package/dist/cli/chat/startupInfo.d.ts +1 -0
- package/dist/cli/chat/startupInfo.js +11 -0
- package/dist/cli/chat/tips.d.ts +21 -0
- package/dist/cli/chat/tips.js +43 -0
- package/dist/cli/chat/wizardDispatch.d.ts +64 -0
- package/dist/cli/chat/wizardDispatch.js +645 -0
- package/dist/cli/commands/chat.d.ts +1 -0
- package/dist/cli/commands/chat.js +181 -700
- package/dist/cli/commands/chatModes.d.ts +1 -1
- package/dist/cli/commands/chatModes.js +7 -2
- package/dist/cli/commands/commandHelp.js +5 -3
- package/dist/cli/commands/commands.d.ts +3 -2
- package/dist/cli/commands/commands.js +12 -13
- package/dist/cli/commands/fleetCommand.d.ts +14 -0
- package/dist/cli/commands/fleetCommand.js +46 -0
- package/dist/cli/commands/formatters.d.ts +5 -0
- package/dist/cli/commands/formatters.js +33 -6
- package/dist/cli/commands/logsCommand.js +1 -7
- package/dist/cli/commands/modelCommand.js +1 -1
- package/dist/cli/commands/providerWizard.d.ts +1 -0
- package/dist/cli/commands/providerWizard.js +13 -0
- package/dist/cli/commands/runCommand.d.ts +1 -0
- package/dist/cli/commands/runCommand.js +25 -5
- package/dist/cli/commands/sessionPicker.d.ts +9 -0
- package/dist/cli/commands/sessionPicker.js +29 -0
- package/dist/cli/commands/settingsSummary.js +6 -4
- package/dist/cli/commands/skillConfirmRemove.js +4 -3
- package/dist/cli/commands/skillWizard.d.ts +2 -0
- package/dist/cli/commands/skillWizard.js +16 -6
- package/dist/cli/commands/streaming/idleTimer.d.ts +26 -0
- package/dist/cli/commands/streaming/idleTimer.js +24 -0
- package/dist/cli/commands/streaming/toolGroupRenderer.d.ts +2 -0
- package/dist/cli/commands/streaming/toolGroupRenderer.js +4 -2
- package/dist/cli/commands/streaming/turnRuntime.js +6 -4
- package/dist/cli/commands/streaming.d.ts +13 -1
- package/dist/cli/commands/streaming.js +42 -19
- package/dist/cli/commands/wizardActions.d.ts +6 -0
- package/dist/cli/commands/wizardActions.js +6 -0
- package/dist/cli/commands/wizardSuggestions.d.ts +8 -0
- package/dist/cli/commands/wizardSuggestions.js +60 -12
- package/dist/cli/commands/wizardTransition.d.ts +4 -0
- package/dist/cli/commands/wizardTransition.js +1 -2
- package/dist/cli/index.js +17 -3
- package/dist/cli/terminalTitle.d.ts +6 -0
- package/dist/cli/terminalTitle.js +36 -0
- package/dist/config/inputHistory.d.ts +3 -3
- package/dist/config/inputHistory.js +12 -18
- package/dist/config/modelDiscovery.d.ts +20 -0
- package/dist/config/modelDiscovery.js +51 -0
- package/dist/config/providerPresets.d.ts +16 -4
- package/dist/config/providerPresets.js +263 -34
- package/dist/config/providers.d.ts +2 -0
- package/dist/config/providers.js +7 -0
- package/dist/config/settings.d.ts +22 -0
- package/dist/config/settings.js +29 -1
- package/dist/config/skillSettings.d.ts +6 -13
- package/dist/config/skillSettings.js +20 -19
- package/dist/core/agent/budgets.d.ts +26 -0
- package/dist/core/agent/budgets.js +34 -0
- package/dist/core/agent/compaction.js +17 -3
- package/dist/core/agent/contextBudget.d.ts +8 -0
- package/dist/core/agent/contextBudget.js +29 -0
- package/dist/core/agent/errors.js +6 -2
- package/dist/core/agent/events.d.ts +12 -0
- package/dist/core/agent/toolOutputStore.d.ts +5 -0
- package/dist/core/agent/toolOutputStore.js +18 -1
- package/dist/core/agent/workState.d.ts +1 -1
- package/dist/core/agent/workState.js +0 -4
- package/dist/core/attachments/imageAttachments.d.ts +90 -0
- package/dist/core/attachments/imageAttachments.js +245 -0
- package/dist/core/attachments/readBlessings.d.ts +37 -0
- package/dist/core/attachments/readBlessings.js +79 -0
- package/dist/core/goal/requestClassifier.d.ts +0 -3
- package/dist/core/goal/requestClassifier.js +0 -13
- package/dist/core/io/boundedRead.js +76 -29
- package/dist/core/limits/byteBudgets.d.ts +5 -0
- package/dist/core/limits/byteBudgets.js +5 -0
- package/dist/core/limits/textBudgets.d.ts +23 -0
- package/dist/core/limits/textBudgets.js +23 -0
- package/dist/core/process/backgroundRegistry.d.ts +35 -0
- package/dist/core/process/backgroundRegistry.js +194 -0
- package/dist/core/process/runBoundedProcess.d.ts +3 -0
- package/dist/core/process/runBoundedProcess.js +65 -29
- package/dist/core/safety/urlGuard.d.ts +2 -1
- package/dist/core/safety/urlGuard.js +3 -2
- package/dist/core/session/sessionSlimming.js +47 -4
- package/dist/core/session/sessionStore.d.ts +37 -0
- package/dist/core/session/sessionStore.js +95 -9
- package/dist/core/subagent/contracts.d.ts +116 -0
- package/dist/core/subagent/contracts.js +54 -0
- package/dist/core/subagent/executionProfiles.d.ts +28 -0
- package/dist/core/subagent/executionProfiles.js +45 -0
- package/dist/core/subagent/subagentCoordinator.d.ts +74 -0
- package/dist/core/subagent/subagentCoordinator.js +134 -0
- package/dist/core/subagent/subagentRunner.d.ts +211 -145
- package/dist/core/subagent/subagentRunner.js +228 -98
- package/dist/core/subagent/workspaceMutationPolicy.d.ts +11 -0
- package/dist/core/subagent/workspaceMutationPolicy.js +52 -0
- package/dist/core/tasks/taskStorage.js +12 -1
- package/dist/llm/client.d.ts +20 -29
- package/dist/llm/client.js +29 -28
- package/dist/llm/hazeTools.d.ts +2013 -78
- package/dist/llm/hazeTools.js +16 -7
- package/dist/llm/lsp.d.ts +2 -0
- package/dist/llm/lsp.js +18 -3
- package/dist/llm/requestContext.d.ts +22 -1
- package/dist/llm/requestContext.js +45 -5
- package/dist/llm/systemPrompt.d.ts +4 -1
- package/dist/llm/systemPrompt.js +14 -4
- package/dist/llm/toolResultTypes.d.ts +1 -1
- package/dist/llm/tools/bashTool.d.ts +874 -32
- package/dist/llm/tools/bashTool.js +25 -4
- package/dist/llm/tools/fetchTool.d.ts +26 -26
- package/dist/llm/tools/fetchTool.js +1 -1
- package/dist/llm/tools/fileToolShared.d.ts +2 -1
- package/dist/llm/tools/fileToolShared.js +5 -5
- package/dist/llm/tools/grepRunner.d.ts +5 -0
- package/dist/llm/tools/grepRunner.js +52 -71
- package/dist/llm/tools/outputCap.d.ts +2 -6
- package/dist/llm/tools/outputCap.js +2 -6
- package/dist/llm/tools/processTool.d.ts +1077 -0
- package/dist/llm/tools/processTool.js +41 -0
- package/dist/llm/tools/toolContext.d.ts +8 -0
- package/dist/llm/tools/toolContext.js +22 -11
- package/dist/llm/tools/workspaceFile.d.ts +9 -1
- package/dist/llm/tools/workspaceFile.js +33 -4
- package/dist/llm/webFetch.js +24 -11
- package/dist/llm/workerContext.d.ts +18 -0
- package/dist/llm/workerContext.js +55 -0
- package/dist/skills/SkillLoader.d.ts +2 -2
- package/dist/skills/SkillRegistry.d.ts +3 -2
- package/dist/skills/SkillRegistry.js +43 -13
- package/dist/skills/builder/SkillBuilder.d.ts +3 -0
- package/dist/skills/builder/SkillBuilder.js +23 -5
- package/dist/skills/skillTools.js +11 -4
- package/dist/skills/types.d.ts +6 -1
- package/dist/ui/components/TextInput.d.ts +5 -2
- package/dist/ui/components/TextInput.js +76 -8
- package/dist/utils/format.d.ts +2 -0
- package/dist/utils/format.js +8 -0
- package/dist/utils/fs.d.ts +0 -1
- package/dist/utils/fs.js +0 -4
- package/dist/utils/openPath.d.ts +2 -0
- package/dist/utils/openPath.js +9 -0
- package/package.json +1 -1
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { JSONValue, LanguageModel } from 'ai';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export declare const workerModes: readonly ["inspect", "research", "implement", "validate"];
|
|
4
|
+
export type WorkerMode = typeof workerModes[number];
|
|
5
|
+
export declare const subagentTaskInputV2Schema: z.ZodObject<{
|
|
6
|
+
objective: z.ZodString;
|
|
7
|
+
deliverable: z.ZodString;
|
|
8
|
+
mode: z.ZodEnum<{
|
|
9
|
+
inspect: "inspect";
|
|
10
|
+
research: "research";
|
|
11
|
+
implement: "implement";
|
|
12
|
+
validate: "validate";
|
|
13
|
+
}>;
|
|
14
|
+
scope: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
15
|
+
acceptanceCriteria: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
16
|
+
}, z.core.$strip>;
|
|
17
|
+
export declare const subagentInputSchema: z.ZodObject<{
|
|
18
|
+
objective: z.ZodString;
|
|
19
|
+
deliverable: z.ZodString;
|
|
20
|
+
mode: z.ZodEnum<{
|
|
21
|
+
inspect: "inspect";
|
|
22
|
+
research: "research";
|
|
23
|
+
implement: "implement";
|
|
24
|
+
validate: "validate";
|
|
25
|
+
}>;
|
|
26
|
+
scope: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
27
|
+
acceptanceCriteria: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
28
|
+
}, z.core.$strip>;
|
|
29
|
+
export type SubagentToolInput = z.infer<typeof subagentInputSchema>;
|
|
30
|
+
export interface SubagentTaskCapsule {
|
|
31
|
+
id: string;
|
|
32
|
+
objective: string;
|
|
33
|
+
deliverable: string;
|
|
34
|
+
mode: WorkerMode;
|
|
35
|
+
scope: string[];
|
|
36
|
+
acceptanceCriteria: string[];
|
|
37
|
+
legacyMaxSteps?: number;
|
|
38
|
+
}
|
|
39
|
+
export type WorkerTermination = 'completed' | 'no_output' | 'step_limit' | 'tool_limit' | 'deadline_exceeded' | 'cancelled' | 'provider_error' | 'policy_blocked';
|
|
40
|
+
export interface SubagentResultCapsule {
|
|
41
|
+
id: string;
|
|
42
|
+
termination: WorkerTermination;
|
|
43
|
+
usable: boolean;
|
|
44
|
+
deliverable: string;
|
|
45
|
+
changedPaths: string[];
|
|
46
|
+
validation: Array<{
|
|
47
|
+
command: string;
|
|
48
|
+
ok: boolean;
|
|
49
|
+
}>;
|
|
50
|
+
coverageGaps: string[];
|
|
51
|
+
truncated: boolean;
|
|
52
|
+
resultHandle?: string;
|
|
53
|
+
}
|
|
54
|
+
export interface SubagentTelemetry {
|
|
55
|
+
modelSelector: string;
|
|
56
|
+
profile: string;
|
|
57
|
+
durationMs: number;
|
|
58
|
+
queueMs: number;
|
|
59
|
+
toolCallCount: number;
|
|
60
|
+
toolCalls: Array<{
|
|
61
|
+
name: string;
|
|
62
|
+
summary: string;
|
|
63
|
+
durationMs: number;
|
|
64
|
+
}>;
|
|
65
|
+
usage: {
|
|
66
|
+
inputTokens?: number;
|
|
67
|
+
outputTokens?: number;
|
|
68
|
+
};
|
|
69
|
+
estimates: {
|
|
70
|
+
taskCapsuleTokens: number;
|
|
71
|
+
initialInputTokens: number;
|
|
72
|
+
privateContextTokens: number;
|
|
73
|
+
resultCapsuleTokens: number;
|
|
74
|
+
mainContextTokensAvoided: number;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export interface SubagentExecutionResult {
|
|
78
|
+
capsule: SubagentResultCapsule;
|
|
79
|
+
telemetry: SubagentTelemetry;
|
|
80
|
+
status: 'ok' | 'error' | 'timeout' | 'cancelled';
|
|
81
|
+
summary: string;
|
|
82
|
+
toolCalls: SubagentTelemetry['toolCalls'];
|
|
83
|
+
toolCallCount: number;
|
|
84
|
+
tokens: {
|
|
85
|
+
in?: number;
|
|
86
|
+
out?: number;
|
|
87
|
+
};
|
|
88
|
+
durationMs: number;
|
|
89
|
+
error?: string;
|
|
90
|
+
}
|
|
91
|
+
export interface ProviderCapabilities {
|
|
92
|
+
reportsCacheUsage: boolean;
|
|
93
|
+
supportsPromptCacheKey: boolean;
|
|
94
|
+
supportsExtendedCacheRetention: boolean;
|
|
95
|
+
supportsStickySessionId: boolean;
|
|
96
|
+
supportsServerCompaction: boolean;
|
|
97
|
+
supportsTextVerbosity: boolean;
|
|
98
|
+
}
|
|
99
|
+
export interface ProviderRequestOptions {
|
|
100
|
+
providerOptions?: Record<string, Record<string, JSONValue | undefined>>;
|
|
101
|
+
headers?: Record<string, string>;
|
|
102
|
+
}
|
|
103
|
+
export interface WorkerRuntime {
|
|
104
|
+
model: LanguageModel;
|
|
105
|
+
selector: string;
|
|
106
|
+
providerName: string;
|
|
107
|
+
capabilities: ProviderCapabilities;
|
|
108
|
+
requestOptions: ProviderRequestOptions;
|
|
109
|
+
}
|
|
110
|
+
/** All-false capabilities + empty request options, used when no real provider runtime is known (CR-018). */
|
|
111
|
+
export declare function fallbackProviderCapabilities(): ProviderCapabilities;
|
|
112
|
+
/** Single source for the placeholder runtime used when only a bare model is available (CR-018). */
|
|
113
|
+
export declare function fallbackWorkerRuntime(model: WorkerRuntime['model'], selector?: string, providerName?: string): WorkerRuntime;
|
|
114
|
+
export declare function normalizeSubagentInput(input: SubagentToolInput, id: string): SubagentTaskCapsule;
|
|
115
|
+
export declare function legacyStatus(termination: WorkerTermination): SubagentExecutionResult['status'];
|
|
116
|
+
export declare function withLegacyProjection(capsule: SubagentResultCapsule, telemetry: SubagentTelemetry, error?: string): SubagentExecutionResult;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { SUBAGENT_ACCEPTANCE_CHARS, SUBAGENT_ACCEPTANCE_ITEMS, SUBAGENT_DELIVERABLE_CHARS, SUBAGENT_OBJECTIVE_CHARS, SUBAGENT_SCOPE_CHARS, SUBAGENT_SCOPE_ITEMS, } from '../agent/budgets.js';
|
|
3
|
+
export const workerModes = ['inspect', 'research', 'implement', 'validate'];
|
|
4
|
+
export const subagentTaskInputV2Schema = z.object({
|
|
5
|
+
objective: z.string().trim().min(1).max(SUBAGENT_OBJECTIVE_CHARS).describe('Self-contained outcome for the worker. Keep it under 1000 characters; do not paste conversation history or file contents.'),
|
|
6
|
+
deliverable: z.string().trim().min(1).max(SUBAGENT_DELIVERABLE_CHARS).describe('Exact compact result the worker must return to the main agent.'),
|
|
7
|
+
mode: z.enum(workerModes).describe('inspect/research are read-only; implement may edit; validate may run commands.'),
|
|
8
|
+
scope: z.array(z.string().trim().min(1).max(SUBAGENT_SCOPE_CHARS)).max(SUBAGENT_SCOPE_ITEMS).optional().describe('Optional workspace-relative paths that bound the task. Prefer at most 12 concise directory/file hints.'),
|
|
9
|
+
acceptanceCriteria: z.array(z.string().trim().min(1).max(SUBAGENT_ACCEPTANCE_CHARS)).max(SUBAGENT_ACCEPTANCE_ITEMS).optional().describe('Optional concise conditions the deliverable must satisfy.'),
|
|
10
|
+
}).describe('A single self-contained task capsule for a disposable subagent.');
|
|
11
|
+
// Keep the model-facing schema flat and unambiguous. In particular, do not
|
|
12
|
+
// union it with the pre-capsule {task, tools, maxSteps} shape: several local
|
|
13
|
+
// OpenAI-compatible models respond to union tool schemas with an empty object.
|
|
14
|
+
export const subagentInputSchema = subagentTaskInputV2Schema;
|
|
15
|
+
/** All-false capabilities + empty request options, used when no real provider runtime is known (CR-018). */
|
|
16
|
+
export function fallbackProviderCapabilities() {
|
|
17
|
+
return {
|
|
18
|
+
reportsCacheUsage: false,
|
|
19
|
+
supportsPromptCacheKey: false,
|
|
20
|
+
supportsExtendedCacheRetention: false,
|
|
21
|
+
supportsStickySessionId: false,
|
|
22
|
+
supportsServerCompaction: false,
|
|
23
|
+
supportsTextVerbosity: false,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** Single source for the placeholder runtime used when only a bare model is available (CR-018). */
|
|
27
|
+
export function fallbackWorkerRuntime(model, selector = 'active-model', providerName = 'active') {
|
|
28
|
+
return { model, selector, providerName, capabilities: fallbackProviderCapabilities(), requestOptions: {} };
|
|
29
|
+
}
|
|
30
|
+
export function normalizeSubagentInput(input, id) {
|
|
31
|
+
return { id, objective: input.objective, deliverable: input.deliverable, mode: input.mode, scope: input.scope ?? [], acceptanceCriteria: input.acceptanceCriteria ?? [] };
|
|
32
|
+
}
|
|
33
|
+
export function legacyStatus(termination) {
|
|
34
|
+
if (termination === 'completed' || termination === 'no_output')
|
|
35
|
+
return 'ok';
|
|
36
|
+
if (termination === 'cancelled')
|
|
37
|
+
return 'cancelled';
|
|
38
|
+
if (termination === 'step_limit' || termination === 'tool_limit' || termination === 'deadline_exceeded')
|
|
39
|
+
return 'timeout';
|
|
40
|
+
return 'error';
|
|
41
|
+
}
|
|
42
|
+
export function withLegacyProjection(capsule, telemetry, error) {
|
|
43
|
+
return {
|
|
44
|
+
capsule,
|
|
45
|
+
telemetry,
|
|
46
|
+
status: legacyStatus(capsule.termination),
|
|
47
|
+
summary: capsule.deliverable,
|
|
48
|
+
toolCalls: telemetry.toolCalls,
|
|
49
|
+
toolCallCount: telemetry.toolCallCount,
|
|
50
|
+
tokens: { in: telemetry.usage.inputTokens, out: telemetry.usage.outputTokens },
|
|
51
|
+
durationMs: telemetry.durationMs,
|
|
52
|
+
...(error ? { error } : {}),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { WorkerMode } from './contracts.js';
|
|
3
|
+
export interface SubagentExecutionProfile {
|
|
4
|
+
name: string;
|
|
5
|
+
maxConcurrency: number;
|
|
6
|
+
maxSteps: number;
|
|
7
|
+
maxToolCalls: number;
|
|
8
|
+
maxOutputTokens: number;
|
|
9
|
+
maxSummaryChars: number;
|
|
10
|
+
maxInputTokens: number;
|
|
11
|
+
deadlineMs: number;
|
|
12
|
+
maxRetries: number;
|
|
13
|
+
}
|
|
14
|
+
export declare const customProfileSchema: z.ZodObject<{
|
|
15
|
+
maxConcurrency: z.ZodOptional<z.ZodNumber>;
|
|
16
|
+
maxSteps: z.ZodOptional<z.ZodNumber>;
|
|
17
|
+
maxToolCalls: z.ZodOptional<z.ZodNumber>;
|
|
18
|
+
maxOutputTokens: z.ZodOptional<z.ZodNumber>;
|
|
19
|
+
maxSummaryChars: z.ZodOptional<z.ZodNumber>;
|
|
20
|
+
maxInputTokens: z.ZodOptional<z.ZodNumber>;
|
|
21
|
+
deadlineMs: z.ZodOptional<z.ZodNumber>;
|
|
22
|
+
maxRetries: z.ZodOptional<z.ZodNumber>;
|
|
23
|
+
}, z.core.$loose>;
|
|
24
|
+
export declare const COMPATIBILITY_PROFILE: SubagentExecutionProfile;
|
|
25
|
+
export declare const BUILT_IN_SUBAGENT_PROFILES: Readonly<Record<string, SubagentExecutionProfile>>;
|
|
26
|
+
export declare const MODE_TOOL_NAMES: Readonly<Record<WorkerMode, readonly string[]>>;
|
|
27
|
+
export declare function isMutationMode(mode: WorkerMode): mode is "implement" | "validate";
|
|
28
|
+
export declare function resolveExecutionProfile(name: string | undefined, custom: Record<string, unknown> | undefined, concurrency?: number): SubagentExecutionProfile | undefined;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { SUBAGENT_DEFAULT_DEADLINE_MS, SUBAGENT_DEFAULT_INPUT_TOKENS, SUBAGENT_DEFAULT_OUTPUT_TOKENS, SUBAGENT_DEFAULT_STEPS, SUBAGENT_DEFAULT_SUMMARY_CHARS, SUBAGENT_DEFAULT_TOOL_CALLS, SUBAGENT_MAX_CONCURRENCY, SUBAGENT_MAX_DEADLINE_MS, SUBAGENT_MAX_INPUT_TOKENS, SUBAGENT_MAX_OUTPUT_TOKENS, SUBAGENT_MAX_RETRIES, SUBAGENT_MAX_STEPS, SUBAGENT_MAX_SUMMARY_CHARS, SUBAGENT_MAX_TOOL_CALLS, SUBAGENT_MIN_DEADLINE_MS, SUBAGENT_MIN_STEPS, } from '../agent/budgets.js';
|
|
3
|
+
export const customProfileSchema = z.object({
|
|
4
|
+
maxConcurrency: z.number().int().min(1).max(SUBAGENT_MAX_CONCURRENCY).optional(),
|
|
5
|
+
maxSteps: z.number().int().min(SUBAGENT_MIN_STEPS).max(SUBAGENT_MAX_STEPS).optional(),
|
|
6
|
+
maxToolCalls: z.number().int().min(1).max(SUBAGENT_MAX_TOOL_CALLS).optional(),
|
|
7
|
+
maxOutputTokens: z.number().int().min(256).max(SUBAGENT_MAX_OUTPUT_TOKENS).optional(),
|
|
8
|
+
maxSummaryChars: z.number().int().min(500).max(SUBAGENT_MAX_SUMMARY_CHARS).optional(),
|
|
9
|
+
maxInputTokens: z.number().int().min(1_000).max(SUBAGENT_MAX_INPUT_TOKENS).optional(),
|
|
10
|
+
deadlineMs: z.number().int().min(SUBAGENT_MIN_DEADLINE_MS).max(SUBAGENT_MAX_DEADLINE_MS).optional(),
|
|
11
|
+
maxRetries: z.number().int().min(0).max(SUBAGENT_MAX_RETRIES).optional(),
|
|
12
|
+
}).passthrough();
|
|
13
|
+
export const COMPATIBILITY_PROFILE = {
|
|
14
|
+
name: 'compatibility', maxConcurrency: 5, maxSteps: SUBAGENT_DEFAULT_STEPS,
|
|
15
|
+
maxToolCalls: SUBAGENT_DEFAULT_TOOL_CALLS, maxOutputTokens: SUBAGENT_DEFAULT_OUTPUT_TOKENS,
|
|
16
|
+
maxSummaryChars: SUBAGENT_DEFAULT_SUMMARY_CHARS, maxInputTokens: SUBAGENT_DEFAULT_INPUT_TOKENS,
|
|
17
|
+
deadlineMs: SUBAGENT_DEFAULT_DEADLINE_MS, maxRetries: 2,
|
|
18
|
+
};
|
|
19
|
+
export const BUILT_IN_SUBAGENT_PROFILES = {
|
|
20
|
+
'local-safe': { ...COMPATIBILITY_PROFILE, name: 'local-safe', maxConcurrency: 1, maxSteps: 16, maxToolCalls: 12, maxOutputTokens: 2_048, maxInputTokens: 20_000, maxRetries: 0 },
|
|
21
|
+
'local-throughput': { ...COMPATIBILITY_PROFILE, name: 'local-throughput', maxConcurrency: 2, maxSteps: 20, maxToolCalls: 16, maxOutputTokens: 3_072, maxInputTokens: 24_000, maxRetries: 0 },
|
|
22
|
+
'cloud-balanced': { ...COMPATIBILITY_PROFILE, name: 'cloud-balanced', maxConcurrency: 3, deadlineMs: 180_000 },
|
|
23
|
+
'cloud-fast': { ...COMPATIBILITY_PROFILE, name: 'cloud-fast', maxConcurrency: 5, deadlineMs: 120_000, maxRetries: 1 },
|
|
24
|
+
};
|
|
25
|
+
export const MODE_TOOL_NAMES = {
|
|
26
|
+
inspect: ['listFiles', 'readFile', 'grep', 'readToolOutput'],
|
|
27
|
+
research: ['listFiles', 'readFile', 'grep', 'readToolOutput', 'fetch'],
|
|
28
|
+
implement: ['listFiles', 'readFile', 'grep', 'readToolOutput', 'editFile', 'replaceLines', 'writeFile', 'bash'],
|
|
29
|
+
validate: ['listFiles', 'readFile', 'grep', 'readToolOutput', 'bash'],
|
|
30
|
+
};
|
|
31
|
+
export function isMutationMode(mode) {
|
|
32
|
+
return mode === 'implement' || mode === 'validate';
|
|
33
|
+
}
|
|
34
|
+
export function resolveExecutionProfile(name, custom, concurrency) {
|
|
35
|
+
const selected = name ? BUILT_IN_SUBAGENT_PROFILES[name] : COMPATIBILITY_PROFILE;
|
|
36
|
+
const customValue = name ? custom?.[name] : undefined;
|
|
37
|
+
if (!selected && customValue == null)
|
|
38
|
+
return undefined;
|
|
39
|
+
const parsed = customValue == null ? {} : customProfileSchema.parse(customValue);
|
|
40
|
+
const base = selected ?? { ...COMPATIBILITY_PROFILE, name: name };
|
|
41
|
+
const merged = { ...base, ...parsed, name: name ?? base.name };
|
|
42
|
+
if (concurrency != null)
|
|
43
|
+
return { ...merged, maxConcurrency: z.number().int().min(1).max(SUBAGENT_MAX_CONCURRENCY).parse(concurrency) };
|
|
44
|
+
return merged;
|
|
45
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { WorkerMode, WorkerTermination } from './contracts.js';
|
|
2
|
+
import { type SubagentExecutionProfile } from './executionProfiles.js';
|
|
3
|
+
export type CoordinatorEvent = {
|
|
4
|
+
type: 'queued';
|
|
5
|
+
id: string;
|
|
6
|
+
mode: WorkerMode;
|
|
7
|
+
queued: number;
|
|
8
|
+
running: number;
|
|
9
|
+
} | {
|
|
10
|
+
type: 'started';
|
|
11
|
+
id: string;
|
|
12
|
+
mode: WorkerMode;
|
|
13
|
+
queueMs: number;
|
|
14
|
+
running: number;
|
|
15
|
+
} | {
|
|
16
|
+
type: 'terminal';
|
|
17
|
+
id: string;
|
|
18
|
+
mode: WorkerMode;
|
|
19
|
+
termination: WorkerTermination;
|
|
20
|
+
execution: 'settled' | 'quarantined';
|
|
21
|
+
queueMs: number;
|
|
22
|
+
durationMs: number;
|
|
23
|
+
running: number;
|
|
24
|
+
} | {
|
|
25
|
+
type: 'settled';
|
|
26
|
+
id: string;
|
|
27
|
+
mode: WorkerMode;
|
|
28
|
+
termination: WorkerTermination;
|
|
29
|
+
queueMs: number;
|
|
30
|
+
durationMs: number;
|
|
31
|
+
running: number;
|
|
32
|
+
};
|
|
33
|
+
interface Submission<T> {
|
|
34
|
+
id: string;
|
|
35
|
+
mode: WorkerMode;
|
|
36
|
+
sequence: number;
|
|
37
|
+
batch: number;
|
|
38
|
+
submittedAt: number;
|
|
39
|
+
signal?: AbortSignal;
|
|
40
|
+
run: (context: {
|
|
41
|
+
id: string;
|
|
42
|
+
signal: AbortSignal;
|
|
43
|
+
queueMs: number;
|
|
44
|
+
deadlineExpired: () => boolean;
|
|
45
|
+
}) => Promise<T>;
|
|
46
|
+
terminal: (termination: WorkerTermination, queueMs: number) => T;
|
|
47
|
+
terminationOf: (value: T) => WorkerTermination;
|
|
48
|
+
resolve: (value: T) => void;
|
|
49
|
+
abortListener?: () => void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Turn-scoped worker admission. A deadline returns logical control immediately,
|
|
53
|
+
* but an uncooperative underlying execution remains quarantined and continues
|
|
54
|
+
* to consume its real concurrency/mutation slot until it physically settles.
|
|
55
|
+
*/
|
|
56
|
+
export declare class SubagentCoordinator {
|
|
57
|
+
private readonly profile;
|
|
58
|
+
private readonly onEvent?;
|
|
59
|
+
private readonly queue;
|
|
60
|
+
private running;
|
|
61
|
+
private mutationRunning;
|
|
62
|
+
private nextId;
|
|
63
|
+
private nextSequence;
|
|
64
|
+
private submissionBatch;
|
|
65
|
+
private admissionScheduled;
|
|
66
|
+
peakConcurrency: number;
|
|
67
|
+
constructor(profile: SubagentExecutionProfile, onEvent?: ((event: CoordinatorEvent) => void) | undefined);
|
|
68
|
+
createId(): string;
|
|
69
|
+
submit<T>(input: Omit<Submission<T>, 'resolve' | 'submittedAt' | 'sequence' | 'batch' | 'abortListener'>): Promise<T>;
|
|
70
|
+
private scheduleAdmission;
|
|
71
|
+
private admit;
|
|
72
|
+
private start;
|
|
73
|
+
}
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { isMutationMode } from './executionProfiles.js';
|
|
2
|
+
/**
|
|
3
|
+
* Turn-scoped worker admission. A deadline returns logical control immediately,
|
|
4
|
+
* but an uncooperative underlying execution remains quarantined and continues
|
|
5
|
+
* to consume its real concurrency/mutation slot until it physically settles.
|
|
6
|
+
*/
|
|
7
|
+
export class SubagentCoordinator {
|
|
8
|
+
profile;
|
|
9
|
+
onEvent;
|
|
10
|
+
queue = [];
|
|
11
|
+
running = 0;
|
|
12
|
+
mutationRunning = false;
|
|
13
|
+
nextId = 1;
|
|
14
|
+
nextSequence = 1;
|
|
15
|
+
submissionBatch = 1;
|
|
16
|
+
admissionScheduled = false;
|
|
17
|
+
peakConcurrency = 0;
|
|
18
|
+
constructor(profile, onEvent) {
|
|
19
|
+
this.profile = profile;
|
|
20
|
+
this.onEvent = onEvent;
|
|
21
|
+
}
|
|
22
|
+
createId() { return `worker-${this.nextId++}`; }
|
|
23
|
+
submit(input) {
|
|
24
|
+
const submittedAt = performance.now();
|
|
25
|
+
return new Promise(resolve => {
|
|
26
|
+
const item = { ...input, sequence: this.nextSequence++, batch: this.submissionBatch, submittedAt, resolve };
|
|
27
|
+
if (input.signal?.aborted) {
|
|
28
|
+
const value = input.terminal('cancelled', 0);
|
|
29
|
+
this.onEvent?.({ type: 'terminal', id: input.id, mode: input.mode, termination: 'cancelled', execution: 'settled', queueMs: 0, durationMs: 0, running: this.running });
|
|
30
|
+
resolve(value);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (input.signal) {
|
|
34
|
+
item.abortListener = () => {
|
|
35
|
+
const index = this.queue.indexOf(item);
|
|
36
|
+
if (index < 0)
|
|
37
|
+
return;
|
|
38
|
+
this.queue.splice(index, 1);
|
|
39
|
+
const queueMs = performance.now() - submittedAt;
|
|
40
|
+
resolve(input.terminal('cancelled', queueMs));
|
|
41
|
+
this.onEvent?.({ type: 'terminal', id: input.id, mode: input.mode, termination: 'cancelled', execution: 'settled', queueMs, durationMs: 0, running: this.running });
|
|
42
|
+
};
|
|
43
|
+
input.signal.addEventListener('abort', item.abortListener, { once: true });
|
|
44
|
+
}
|
|
45
|
+
this.queue.push(item);
|
|
46
|
+
this.onEvent?.({ type: 'queued', id: input.id, mode: input.mode, queued: this.queue.length, running: this.running });
|
|
47
|
+
this.scheduleAdmission();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
scheduleAdmission() {
|
|
51
|
+
if (this.admissionScheduled)
|
|
52
|
+
return;
|
|
53
|
+
this.admissionScheduled = true;
|
|
54
|
+
queueMicrotask(() => {
|
|
55
|
+
this.admissionScheduled = false;
|
|
56
|
+
const batch = this.submissionBatch++;
|
|
57
|
+
// Only reorder calls emitted in the same model batch. Older blocked work
|
|
58
|
+
// always remains ahead of later submissions, preventing starvation.
|
|
59
|
+
this.queue.sort((a, b) => {
|
|
60
|
+
if (a.batch !== b.batch || a.batch !== batch)
|
|
61
|
+
return a.sequence - b.sequence;
|
|
62
|
+
if (a.mode === 'implement' && b.mode === 'validate')
|
|
63
|
+
return -1;
|
|
64
|
+
if (a.mode === 'validate' && b.mode === 'implement')
|
|
65
|
+
return 1;
|
|
66
|
+
return a.sequence - b.sequence;
|
|
67
|
+
});
|
|
68
|
+
this.admit();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
admit() {
|
|
72
|
+
while (this.running < this.profile.maxConcurrency) {
|
|
73
|
+
const item = this.queue[0];
|
|
74
|
+
if (!item || (isMutationMode(item.mode) && this.mutationRunning))
|
|
75
|
+
break;
|
|
76
|
+
this.queue.shift();
|
|
77
|
+
if (item.signal && item.abortListener)
|
|
78
|
+
item.signal.removeEventListener('abort', item.abortListener);
|
|
79
|
+
this.start(item);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
start(item) {
|
|
83
|
+
const mutation = isMutationMode(item.mode);
|
|
84
|
+
this.running++;
|
|
85
|
+
if (mutation)
|
|
86
|
+
this.mutationRunning = true;
|
|
87
|
+
this.peakConcurrency = Math.max(this.peakConcurrency, this.running);
|
|
88
|
+
const queueMs = performance.now() - item.submittedAt;
|
|
89
|
+
const controller = new AbortController();
|
|
90
|
+
let abortSource;
|
|
91
|
+
let terminalDelivered = false;
|
|
92
|
+
let deliveredTermination;
|
|
93
|
+
const startedAt = performance.now();
|
|
94
|
+
const deliverTerminal = (termination, execution, value) => {
|
|
95
|
+
if (terminalDelivered)
|
|
96
|
+
return;
|
|
97
|
+
terminalDelivered = true;
|
|
98
|
+
deliveredTermination = termination;
|
|
99
|
+
const terminalValue = value ?? item.terminal(termination, queueMs);
|
|
100
|
+
this.onEvent?.({ type: 'terminal', id: item.id, mode: item.mode, termination, execution, queueMs, durationMs: performance.now() - startedAt, running: execution === 'settled' ? this.running - 1 : this.running });
|
|
101
|
+
item.resolve(terminalValue);
|
|
102
|
+
};
|
|
103
|
+
const abort = (source, reason) => {
|
|
104
|
+
if (abortSource)
|
|
105
|
+
return;
|
|
106
|
+
abortSource = source;
|
|
107
|
+
const termination = source === 'deadline' ? 'deadline_exceeded' : 'cancelled';
|
|
108
|
+
controller.abort(reason);
|
|
109
|
+
// Restore caller control now, without pretending the underlying execution
|
|
110
|
+
// or its real admission/mutation slot has been released.
|
|
111
|
+
deliverTerminal(termination, 'quarantined');
|
|
112
|
+
};
|
|
113
|
+
const parentAbort = () => abort('parent', item.signal?.reason);
|
|
114
|
+
item.signal?.addEventListener('abort', parentAbort, { once: true });
|
|
115
|
+
if (item.signal?.aborted)
|
|
116
|
+
parentAbort();
|
|
117
|
+
const timer = setTimeout(() => abort('deadline', 'subagent deadline exceeded'), this.profile.deadlineMs);
|
|
118
|
+
this.onEvent?.({ type: 'started', id: item.id, mode: item.mode, queueMs, running: this.running });
|
|
119
|
+
void Promise.resolve().then(() => item.run({ id: item.id, signal: controller.signal, queueMs, deadlineExpired: () => abortSource === 'deadline' }))
|
|
120
|
+
.then(value => deliverTerminal(item.terminationOf(value), 'settled', value))
|
|
121
|
+
.catch(() => deliverTerminal(abortSource === 'deadline' ? 'deadline_exceeded' : abortSource === 'parent' ? 'cancelled' : 'provider_error', 'settled'))
|
|
122
|
+
.finally(() => {
|
|
123
|
+
clearTimeout(timer);
|
|
124
|
+
item.signal?.removeEventListener('abort', parentAbort);
|
|
125
|
+
this.running--;
|
|
126
|
+
if (mutation)
|
|
127
|
+
this.mutationRunning = false;
|
|
128
|
+
if (deliveredTermination && abortSource) {
|
|
129
|
+
this.onEvent?.({ type: 'settled', id: item.id, mode: item.mode, termination: deliveredTermination, queueMs, durationMs: performance.now() - startedAt, running: this.running });
|
|
130
|
+
}
|
|
131
|
+
this.admit();
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|