@denizokcu/haze 0.8.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 +67 -17
- package/README.md +121 -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.d.ts +3 -0
- package/dist/cli/chat/sessionRecorder.js +21 -8
- 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 +246 -779
- 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 +4 -3
- 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 +36 -9
- package/dist/cli/commands/logsCommand.js +8 -28
- package/dist/cli/commands/mcpWizard.js +24 -2
- package/dist/cli/commands/modelCommand.js +1 -1
- package/dist/cli/commands/providerWizard.d.ts +1 -0
- package/dist/cli/commands/providerWizard.js +31 -5
- package/dist/cli/commands/runCommand.d.ts +1 -0
- package/dist/cli/commands/runCommand.js +32 -20
- package/dist/cli/commands/sessionPicker.d.ts +9 -0
- package/dist/cli/commands/sessionPicker.js +29 -0
- package/dist/cli/commands/settingsSummary.js +8 -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/turnOutcome.d.ts +10 -0
- package/dist/cli/commands/streaming/turnOutcome.js +13 -0
- package/dist/cli/commands/streaming/turnRuntime.js +6 -4
- package/dist/cli/commands/streaming.d.ts +13 -1
- package/dist/cli/commands/streaming.js +104 -44
- package/dist/cli/commands/wizardActions.d.ts +6 -0
- package/dist/cli/commands/wizardActions.js +6 -0
- package/dist/cli/commands/wizardPrompts.js +1 -1
- package/dist/cli/commands/wizardSuggestions.d.ts +8 -0
- package/dist/cli/commands/wizardSuggestions.js +62 -13
- package/dist/cli/commands/wizardTransition.d.ts +37 -0
- package/dist/cli/commands/wizardTransition.js +43 -0
- 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/contextFiles.js +6 -3
- package/dist/config/endpointSecurity.d.ts +2 -0
- package/dist/config/endpointSecurity.js +28 -0
- package/dist/config/inputHistory.d.ts +3 -3
- package/dist/config/inputHistory.js +13 -17
- package/dist/config/modelDiscovery.d.ts +20 -0
- package/dist/config/modelDiscovery.js +51 -0
- package/dist/config/privateStorage.d.ts +6 -0
- package/dist/config/privateStorage.js +61 -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 +33 -5
- package/dist/config/skillSettings.d.ts +6 -13
- package/dist/config/skillSettings.js +20 -19
- package/dist/config/updateCheck.js +4 -2
- 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 +8 -0
- package/dist/core/agent/toolOutputStore.js +49 -11
- 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/completionPolicy.d.ts +0 -28
- package/dist/core/goal/completionPolicy.js +0 -88
- package/dist/core/goal/requestClassifier.d.ts +0 -3
- package/dist/core/goal/requestClassifier.js +0 -13
- package/dist/core/io/boundedRead.d.ts +15 -0
- package/dist/core/io/boundedRead.js +144 -0
- package/dist/core/limits/byteBudgets.d.ts +18 -0
- package/dist/core/limits/byteBudgets.js +18 -0
- package/dist/core/limits/textBudgets.d.ts +23 -0
- package/dist/core/limits/textBudgets.js +23 -0
- package/dist/core/log/llmLog.d.ts +10 -0
- package/dist/core/log/llmLog.js +59 -13
- package/dist/core/persistence/orderedFileWriter.d.ts +11 -0
- package/dist/core/persistence/orderedFileWriter.js +29 -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 +30 -0
- package/dist/core/process/runBoundedProcess.js +116 -0
- 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 +136 -27
- 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 +33 -29
- package/dist/llm/hazeTools.d.ts +2396 -67
- package/dist/llm/hazeTools.js +50 -37
- package/dist/llm/lsp.d.ts +23 -3
- package/dist/llm/lsp.js +73 -16
- package/dist/llm/lspTools.d.ts +96 -48
- package/dist/llm/mcp.d.ts +1 -1
- package/dist/llm/mcp.js +65 -16
- package/dist/llm/requestContext.d.ts +23 -1
- package/dist/llm/requestContext.js +50 -7
- 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 +1210 -16
- package/dist/llm/tools/bashTool.js +43 -60
- 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 +20 -0
- package/dist/llm/tools/grepRunner.js +66 -0
- 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/SkillLoader.js +17 -5
- package/dist/skills/SkillRegistry.d.ts +3 -2
- package/dist/skills/SkillRegistry.js +53 -10
- 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 +11 -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/dist/utils/path.d.ts +2 -0
- package/dist/utils/path.js +9 -4
- package/package.json +1 -1
|
@@ -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
|
+
}
|