jiva-core 0.3.2 → 0.3.3-dev.bd250bd
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/.github/workflows/npm-publish-dev.yml +39 -0
- package/.github/workflows/npm-publish.yml +31 -0
- package/Dockerfile +12 -7
- package/README.md +10 -0
- package/cloud-run.yaml +1 -1
- package/cloud-run.yaml.template +1 -1
- package/dist/core/agent-spawner.d.ts.map +1 -1
- package/dist/core/agent-spawner.js +3 -0
- package/dist/core/agent-spawner.js.map +1 -1
- package/dist/core/client-agent.d.ts +46 -19
- package/dist/core/client-agent.d.ts.map +1 -1
- package/dist/core/client-agent.js +332 -219
- package/dist/core/client-agent.js.map +1 -1
- package/dist/core/config.d.ts +73 -17
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +20 -6
- package/dist/core/config.js.map +1 -1
- package/dist/core/dual-agent.d.ts +20 -0
- package/dist/core/dual-agent.d.ts.map +1 -1
- package/dist/core/dual-agent.js +217 -49
- package/dist/core/dual-agent.js.map +1 -1
- package/dist/core/manager-agent.d.ts +9 -2
- package/dist/core/manager-agent.d.ts.map +1 -1
- package/dist/core/manager-agent.js +43 -14
- package/dist/core/manager-agent.js.map +1 -1
- package/dist/core/types/agent-context.d.ts +30 -0
- package/dist/core/types/agent-context.d.ts.map +1 -0
- package/dist/core/types/agent-context.js +8 -0
- package/dist/core/types/agent-context.js.map +1 -0
- package/dist/core/types/completion-signal.d.ts +17 -0
- package/dist/core/types/completion-signal.d.ts.map +1 -0
- package/dist/core/types/completion-signal.js +8 -0
- package/dist/core/types/completion-signal.js.map +1 -0
- package/dist/core/utils/serialize-agent-context.d.ts +23 -0
- package/dist/core/utils/serialize-agent-context.d.ts.map +1 -0
- package/dist/core/utils/serialize-agent-context.js +73 -0
- package/dist/core/utils/serialize-agent-context.js.map +1 -0
- package/dist/core/worker-agent.d.ts +9 -1
- package/dist/core/worker-agent.d.ts.map +1 -1
- package/dist/core/worker-agent.js +235 -39
- package/dist/core/worker-agent.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/interfaces/cli/index.js +91 -14
- package/dist/interfaces/cli/index.js.map +1 -1
- package/dist/interfaces/cli/setup-wizard.d.ts.map +1 -1
- package/dist/interfaces/cli/setup-wizard.js +93 -1
- package/dist/interfaces/cli/setup-wizard.js.map +1 -1
- package/dist/interfaces/http/session-manager.d.ts.map +1 -1
- package/dist/interfaces/http/session-manager.js +34 -7
- package/dist/interfaces/http/session-manager.js.map +1 -1
- package/dist/models/krutrim.d.ts +1 -1
- package/dist/models/krutrim.d.ts.map +1 -1
- package/dist/models/krutrim.js +4 -3
- package/dist/models/krutrim.js.map +1 -1
- package/dist/models/orchestrator.d.ts +24 -0
- package/dist/models/orchestrator.d.ts.map +1 -1
- package/dist/models/orchestrator.js +40 -6
- package/dist/models/orchestrator.js.map +1 -1
- package/dist/storage/gcp-bucket-provider.d.ts.map +1 -1
- package/dist/storage/gcp-bucket-provider.js +1 -11
- package/dist/storage/gcp-bucket-provider.js.map +1 -1
- package/dist/utils/platform.d.ts +13 -0
- package/dist/utils/platform.d.ts.map +1 -0
- package/dist/utils/platform.js +23 -0
- package/dist/utils/platform.js.map +1 -0
- package/package.json +7 -8
- package/.claude/settings.local.json +0 -18
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-context.js","sourceRoot":"","sources":["../../../src/core/types/agent-context.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CompletionSignal - Per-subtask assessment emitted by Client alongside ValidationResult
|
|
3
|
+
*
|
|
4
|
+
* Replaces the blunt failureCount escalation with a richer signal that DualAgent
|
|
5
|
+
* uses to decide corrective strategy per subtask.
|
|
6
|
+
*/
|
|
7
|
+
export interface CompletionSignal {
|
|
8
|
+
/** How confident the Client is that the subtask was completed correctly */
|
|
9
|
+
confidence: 'high' | 'medium' | 'low' | 'none';
|
|
10
|
+
/** Did Worker make any measurable forward progress? */
|
|
11
|
+
progressMade: boolean;
|
|
12
|
+
/** Classification of the blocker (if confidence is not 'high') */
|
|
13
|
+
blockerType?: 'tool_failure' | 'hallucination' | 'scope_drift' | 'partial' | 'loop' | 'capability_gap';
|
|
14
|
+
/** Suggested corrective strategy for DualAgent */
|
|
15
|
+
suggestedStrategy?: 'retry' | 'rephrase' | 'decompose' | 'skip' | 'escalate';
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=completion-signal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"completion-signal.d.ts","sourceRoot":"","sources":["../../../src/core/types/completion-signal.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,WAAW,gBAAgB;IAC/B,2EAA2E;IAC3E,UAAU,EAAE,MAAM,GAAG,QAAQ,GAAG,KAAK,GAAG,MAAM,CAAC;IAE/C,uDAAuD;IACvD,YAAY,EAAE,OAAO,CAAC;IAEtB,kEAAkE;IAClE,WAAW,CAAC,EAAE,cAAc,GACd,eAAe,GACf,aAAa,GACb,SAAS,GACT,MAAM,GACN,gBAAgB,CAAC;IAE/B,kDAAkD;IAClD,iBAAiB,CAAC,EAAE,OAAO,GACP,UAAU,GACV,WAAW,GACX,MAAM,GACN,UAAU,CAAC;CAChC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CompletionSignal - Per-subtask assessment emitted by Client alongside ValidationResult
|
|
3
|
+
*
|
|
4
|
+
* Replaces the blunt failureCount escalation with a richer signal that DualAgent
|
|
5
|
+
* uses to decide corrective strategy per subtask.
|
|
6
|
+
*/
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=completion-signal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"completion-signal.js","sourceRoot":"","sources":["../../../src/core/types/completion-signal.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* serializeAgentContext - Produces a consistent, role-appropriate string block
|
|
3
|
+
* that each agent embeds in its prompt.
|
|
4
|
+
*
|
|
5
|
+
* Using the same serializer for all three agents ensures the format is identical;
|
|
6
|
+
* the `role` parameter governs what is included, not how it is formatted.
|
|
7
|
+
*
|
|
8
|
+
* Token budget guidelines (tunable):
|
|
9
|
+
* Directive: ~500 tokens max (truncate with notice if exceeded)
|
|
10
|
+
* Conversation summary: ~300 tokens max
|
|
11
|
+
* Recent messages: last 6 messages of all roles, ~800 tokens
|
|
12
|
+
* Validation context: ~400 tokens max
|
|
13
|
+
*/
|
|
14
|
+
import { AgentContext } from '../types/agent-context.js';
|
|
15
|
+
/**
|
|
16
|
+
* Serialize AgentContext into a role-appropriate string block.
|
|
17
|
+
*
|
|
18
|
+
* - manager: directive + conversation (summary + recent). Omits validationContext.
|
|
19
|
+
* - worker: directive + conversation (summary + recent, incl. tool messages). Omits validationContext.
|
|
20
|
+
* - client: directive + conversation (summary + recent, incl. tool messages) + validationContext. Omits execution persona.
|
|
21
|
+
*/
|
|
22
|
+
export declare function serializeAgentContext(ctx: AgentContext, role: 'manager' | 'worker' | 'client'): string;
|
|
23
|
+
//# sourceMappingURL=serialize-agent-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize-agent-context.d.ts","sourceRoot":"","sources":["../../../src/core/utils/serialize-agent-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAgCzD;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,SAAS,GAAG,QAAQ,GAAG,QAAQ,GACpC,MAAM,CAuCR"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* serializeAgentContext - Produces a consistent, role-appropriate string block
|
|
3
|
+
* that each agent embeds in its prompt.
|
|
4
|
+
*
|
|
5
|
+
* Using the same serializer for all three agents ensures the format is identical;
|
|
6
|
+
* the `role` parameter governs what is included, not how it is formatted.
|
|
7
|
+
*
|
|
8
|
+
* Token budget guidelines (tunable):
|
|
9
|
+
* Directive: ~500 tokens max (truncate with notice if exceeded)
|
|
10
|
+
* Conversation summary: ~300 tokens max
|
|
11
|
+
* Recent messages: last 6 messages of all roles, ~800 tokens
|
|
12
|
+
* Validation context: ~400 tokens max
|
|
13
|
+
*/
|
|
14
|
+
// Rough estimate: 1 token ≈ 4 characters
|
|
15
|
+
const CHARS_PER_TOKEN = 4;
|
|
16
|
+
const DIRECTIVE_TOKEN_LIMIT = 500;
|
|
17
|
+
const SUMMARY_TOKEN_LIMIT = 300;
|
|
18
|
+
// Recent messages are no longer truncated — the underlying LLM supports a 128k
|
|
19
|
+
// context window and the conversation manager condenses history before this is
|
|
20
|
+
// called, so there is no need for an artificial per-role cap here.
|
|
21
|
+
const VALIDATION_CONTEXT_TOKEN_LIMIT = 400;
|
|
22
|
+
/**
|
|
23
|
+
* Truncate text to a token-approximate character limit, appending a notice if truncated.
|
|
24
|
+
*/
|
|
25
|
+
function truncate(text, tokenLimit) {
|
|
26
|
+
const charLimit = tokenLimit * CHARS_PER_TOKEN;
|
|
27
|
+
if (text.length <= charLimit)
|
|
28
|
+
return text;
|
|
29
|
+
return text.substring(0, charLimit) + '\n[...truncated due to token limit]';
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Serialize a single message to a compact string representation.
|
|
33
|
+
*/
|
|
34
|
+
function serializeMessage(msg) {
|
|
35
|
+
const role = msg.role.toUpperCase();
|
|
36
|
+
const content = typeof msg.content === 'string'
|
|
37
|
+
? msg.content
|
|
38
|
+
: msg.content.map(c => c.text || '[image]').join(' ');
|
|
39
|
+
return `[${role}]: ${content}`;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Serialize AgentContext into a role-appropriate string block.
|
|
43
|
+
*
|
|
44
|
+
* - manager: directive + conversation (summary + recent). Omits validationContext.
|
|
45
|
+
* - worker: directive + conversation (summary + recent, incl. tool messages). Omits validationContext.
|
|
46
|
+
* - client: directive + conversation (summary + recent, incl. tool messages) + validationContext. Omits execution persona.
|
|
47
|
+
*/
|
|
48
|
+
export function serializeAgentContext(ctx, role) {
|
|
49
|
+
const sections = [];
|
|
50
|
+
// ── Directive ──────────────────────────────────────────────────────────
|
|
51
|
+
if (ctx.directive) {
|
|
52
|
+
sections.push('=== DIRECTIVE ===\n' + truncate(ctx.directive, DIRECTIVE_TOKEN_LIMIT));
|
|
53
|
+
}
|
|
54
|
+
// ── Conversation context ──────────────────────────────────────────────
|
|
55
|
+
const convParts = [];
|
|
56
|
+
if (ctx.conversation.summary) {
|
|
57
|
+
convParts.push('[Conversation Summary]\n' + truncate(ctx.conversation.summary, SUMMARY_TOKEN_LIMIT));
|
|
58
|
+
}
|
|
59
|
+
if (ctx.conversation.recentMessages.length > 0) {
|
|
60
|
+
const serialized = ctx.conversation.recentMessages.map(serializeMessage).join('\n');
|
|
61
|
+
convParts.push('[Recent Messages]\n' + serialized);
|
|
62
|
+
}
|
|
63
|
+
if (convParts.length > 0) {
|
|
64
|
+
sections.push('=== CONVERSATION CONTEXT ===\n' + convParts.join('\n\n'));
|
|
65
|
+
}
|
|
66
|
+
// ── Validation context (client only) ──────────────────────────────────
|
|
67
|
+
if (role === 'client' && ctx.persona?.validationContext) {
|
|
68
|
+
sections.push('=== VALIDATION CONTEXT ===\n' +
|
|
69
|
+
truncate(ctx.persona.validationContext, VALIDATION_CONTEXT_TOKEN_LIMIT));
|
|
70
|
+
}
|
|
71
|
+
return sections.join('\n\n');
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=serialize-agent-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize-agent-context.js","sourceRoot":"","sources":["../../../src/core/utils/serialize-agent-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAKH,yCAAyC;AACzC,MAAM,eAAe,GAAG,CAAC,CAAC;AAC1B,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAClC,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAChC,+EAA+E;AAC/E,+EAA+E;AAC/E,mEAAmE;AACnE,MAAM,8BAA8B,GAAG,GAAG,CAAC;AAE3C;;GAEG;AACH,SAAS,QAAQ,CAAC,IAAY,EAAE,UAAkB;IAChD,MAAM,SAAS,GAAG,UAAU,GAAG,eAAe,CAAC;IAC/C,IAAI,IAAI,CAAC,MAAM,IAAI,SAAS;QAAE,OAAO,IAAI,CAAC;IAC1C,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,SAAS,CAAC,GAAG,qCAAqC,CAAC;AAC9E,CAAC;AAED;;GAEG;AACH,SAAS,gBAAgB,CAAC,GAAY;IACpC,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;IACpC,MAAM,OAAO,GAAG,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ;QAC7C,CAAC,CAAC,GAAG,CAAC,OAAO;QACb,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACxD,OAAO,IAAI,IAAI,MAAM,OAAO,EAAE,CAAC;AACjC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CACnC,GAAiB,EACjB,IAAqC;IAErC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,0EAA0E;IAC1E,IAAI,GAAG,CAAC,SAAS,EAAE,CAAC;QAClB,QAAQ,CAAC,IAAI,CACX,qBAAqB,GAAG,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,qBAAqB,CAAC,CACvE,CAAC;IACJ,CAAC;IAED,yEAAyE;IACzE,MAAM,SAAS,GAAa,EAAE,CAAC;IAE/B,IAAI,GAAG,CAAC,YAAY,CAAC,OAAO,EAAE,CAAC;QAC7B,SAAS,CAAC,IAAI,CACZ,0BAA0B,GAAG,QAAQ,CAAC,GAAG,CAAC,YAAY,CAAC,OAAO,EAAE,mBAAmB,CAAC,CACrF,CAAC;IACJ,CAAC;IAED,IAAI,GAAG,CAAC,YAAY,CAAC,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC/C,MAAM,UAAU,GAAG,GAAG,CAAC,YAAY,CAAC,cAAc,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpF,SAAS,CAAC,IAAI,CACZ,qBAAqB,GAAG,UAAU,CACnC,CAAC;IACJ,CAAC;IAED,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACzB,QAAQ,CAAC,IAAI,CAAC,gCAAgC,GAAG,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC3E,CAAC;IAED,yEAAyE;IACzE,IAAI,IAAI,KAAK,QAAQ,IAAI,GAAG,CAAC,OAAO,EAAE,iBAAiB,EAAE,CAAC;QACxD,QAAQ,CAAC,IAAI,CACX,8BAA8B;YAC9B,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,iBAAiB,EAAE,8BAA8B,CAAC,CACxE,CAAC;IACJ,CAAC;IAED,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -12,14 +12,22 @@ import { MCPServerManager } from '../mcp/server-manager.js';
|
|
|
12
12
|
import { WorkspaceManager } from './workspace.js';
|
|
13
13
|
import { PersonaManager } from '../personas/persona-manager.js';
|
|
14
14
|
import { AgentSpawner } from './agent-spawner.js';
|
|
15
|
+
import { AgentContext } from './types/agent-context.js';
|
|
15
16
|
export interface WorkerSubtask {
|
|
16
17
|
instruction: string;
|
|
17
18
|
context?: string;
|
|
18
19
|
}
|
|
20
|
+
export interface ToolFailure {
|
|
21
|
+
toolName: string;
|
|
22
|
+
args: Record<string, any>;
|
|
23
|
+
lastError: string;
|
|
24
|
+
attempts: number;
|
|
25
|
+
}
|
|
19
26
|
export interface WorkerResult {
|
|
20
27
|
success: boolean;
|
|
21
28
|
result: string;
|
|
22
29
|
toolsUsed: string[];
|
|
30
|
+
failedTools: ToolFailure[];
|
|
23
31
|
reasoning: string;
|
|
24
32
|
}
|
|
25
33
|
export declare class WorkerAgent {
|
|
@@ -38,7 +46,7 @@ export declare class WorkerAgent {
|
|
|
38
46
|
/**
|
|
39
47
|
* Execute a subtask assigned by Manager
|
|
40
48
|
*/
|
|
41
|
-
executeSubtask(subtask: WorkerSubtask): Promise<WorkerResult>;
|
|
49
|
+
executeSubtask(subtask: WorkerSubtask, agentContext?: AgentContext): Promise<WorkerResult>;
|
|
42
50
|
private extractReasoning;
|
|
43
51
|
/**
|
|
44
52
|
* Determine if we should prompt Worker to check for completion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-agent.d.ts","sourceRoot":"","sources":["../../src/core/worker-agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"worker-agent.d.ts","sourceRoot":"","sources":["../../src/core/worker-agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAsBxD,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,WAAW,EAAE,WAAW,EAAE,CAAC;IAC3B,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,qBAAa,WAAW;IACtB,OAAO,CAAC,YAAY,CAAoB;IACxC,OAAO,CAAC,UAAU,CAAmB;IACrC,OAAO,CAAC,SAAS,CAAmB;IACpC,OAAO,CAAC,cAAc,CAAC,CAAiB;IACxC,OAAO,CAAC,YAAY,CAAC,CAAe;IACpC,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,aAAa,CAAsB;gBAGzC,YAAY,EAAE,iBAAiB,EAC/B,UAAU,EAAE,gBAAgB,EAC5B,SAAS,EAAE,gBAAgB,EAC3B,aAAa,GAAE,MAAU,EACzB,cAAc,CAAC,EAAE,cAAc;IAqBjC;;OAEG;IACH,eAAe,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI;IAI5C;;OAEG;IACG,cAAc,CAAC,OAAO,EAAE,aAAa,EAAE,YAAY,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAkmBhG,OAAO,CAAC,gBAAgB;IAMxB;;;OAGG;IACH,OAAO,CAAC,yBAAyB;CA4ClC"}
|
|
@@ -45,7 +45,7 @@ export class WorkerAgent {
|
|
|
45
45
|
/**
|
|
46
46
|
* Execute a subtask assigned by Manager
|
|
47
47
|
*/
|
|
48
|
-
async executeSubtask(subtask) {
|
|
48
|
+
async executeSubtask(subtask, agentContext) {
|
|
49
49
|
logger.info(`[Worker] Starting: "${subtask.instruction}"`);
|
|
50
50
|
orchestrationLogger.logWorkerStart(subtask.instruction, subtask.context || '');
|
|
51
51
|
// Reset context memory for new subtask
|
|
@@ -55,7 +55,41 @@ export class WorkerAgent {
|
|
|
55
55
|
};
|
|
56
56
|
const conversationHistory = [];
|
|
57
57
|
const toolsUsed = [];
|
|
58
|
+
// Tracks every exact tool-call signature (toolName + serialized args) executed
|
|
59
|
+
// during this subtask. Used to prevent the same call from being repeated in a
|
|
60
|
+
// later iteration — e.g. reading the same file twice in consecutive turns.
|
|
61
|
+
const executedToolSignatures = new Set();
|
|
62
|
+
// Tracks consecutive failures per (toolName:args) signature.
|
|
63
|
+
// Drives the per-tool circuit breaker: warn at 2, hard-stop at 3.
|
|
64
|
+
const toolFailureCounts = new Map();
|
|
58
65
|
let iterationCount = 0;
|
|
66
|
+
// ── 2+2 API-level JSON parse failure strategy ─────────────────────────────
|
|
67
|
+
// Priority: if a dedicated tool-calling LLM is configured, use it from the
|
|
68
|
+
// very first iteration (it reliably serialises tool args as standard JSON).
|
|
69
|
+
// The reasoning model then serves as the phase-2 fallback.
|
|
70
|
+
// If no tool-calling model is configured the roles are reversed: reasoning
|
|
71
|
+
// model is phase 1 and there is no phase-2 fallback.
|
|
72
|
+
//
|
|
73
|
+
// Flow when tool-calling model IS configured:
|
|
74
|
+
// Phase 1 – tool-calling model (up to API_PARSE_FAIL_THRESHOLD=2 failures)
|
|
75
|
+
// Phase 2 – reasoning model (up to SECONDARY_MODEL_ATTEMPTS=2 failures)
|
|
76
|
+
// → hard-stop after 4 total failures; failedTools is non-empty so the
|
|
77
|
+
// Client's data-driven CompletionSignal fires (prevents infinite retries)
|
|
78
|
+
//
|
|
79
|
+
// Flow when NO tool-calling model is configured:
|
|
80
|
+
// Phase 1 – reasoning model (up to API_PARSE_FAIL_THRESHOLD=2 failures)
|
|
81
|
+
// → hard-stop immediately (no secondary model available)
|
|
82
|
+
const API_PARSE_FAIL_THRESHOLD = 2; // phase-1 failures before switching phases
|
|
83
|
+
const SECONDARY_MODEL_ATTEMPTS = 2; // extra attempts on the phase-2 model
|
|
84
|
+
let apiJsonParseFailures = 0; // total JSON-parse failures so far
|
|
85
|
+
// toolCallingIsMain: true when the tool-calling model is the primary model for this run
|
|
86
|
+
const toolCallingIsMain = this.orchestrator.hasToolCallingModel();
|
|
87
|
+
// useToolCallingFallback: flag passed to orchestrator.chatWithFallback()
|
|
88
|
+
// true → use the tool-calling model
|
|
89
|
+
// false → use the reasoning model
|
|
90
|
+
let useToolCallingFallback = toolCallingIsMain; // start with tool-calling model if available
|
|
91
|
+
let phaseSwitched = false; // true once we have swapped phases
|
|
92
|
+
const API_JSON_PARSE_FAILURE_SIG = 'api_json_parse:{}'; // key in toolFailureCounts
|
|
59
93
|
// Build system prompt for Worker
|
|
60
94
|
const personaPrompt = this.personaManager?.getSystemPromptAddition() || '';
|
|
61
95
|
let systemContent = `You are the Worker Agent in a two-agent system.
|
|
@@ -114,12 +148,18 @@ Available tools: ${this.mcpManager.getClient().getAllTools().map(t => t.name).jo
|
|
|
114
148
|
* persona (required): Persona name - ${availablePersonas.join(', ')}
|
|
115
149
|
* task (required): Specific task for the sub-agent
|
|
116
150
|
* context (optional): Additional domain-specific context (workspace path is automatically included)
|
|
151
|
+
- IMPORTANT: When spawning a sub-agent, include relevant directive constraints and a brief conversation summary in the context parameter so the sub-agent has sufficient background.
|
|
117
152
|
- Example: spawn_agent({ persona: "code-reviewer", task: "Review the authentication code in src/auth/", context: "Focus on security vulnerabilities and best practices" })
|
|
118
153
|
- The sub-agent will complete the task and return results to you`;
|
|
119
154
|
}
|
|
120
155
|
if (personaPrompt) {
|
|
121
156
|
systemContent += `\n\n${personaPrompt}`;
|
|
122
157
|
}
|
|
158
|
+
// Inject directive into Worker prompt (fixes bug: Worker previously had no directive)
|
|
159
|
+
const directivePrompt = agentContext?.directive || this.workspace.getDirectivePrompt() || '';
|
|
160
|
+
if (directivePrompt) {
|
|
161
|
+
systemContent += `\n\n${directivePrompt}`;
|
|
162
|
+
}
|
|
123
163
|
// System prompt for Worker
|
|
124
164
|
// Use 'developer' role for Harmony format compatibility (will be converted to 'system' by model)
|
|
125
165
|
conversationHistory.push({
|
|
@@ -184,23 +224,114 @@ Please complete this subtask and report your findings.`,
|
|
|
184
224
|
}
|
|
185
225
|
let response;
|
|
186
226
|
try {
|
|
187
|
-
response = await this.orchestrator.
|
|
227
|
+
response = await this.orchestrator.chatWithFallback({
|
|
188
228
|
messages: conversationHistory,
|
|
189
229
|
tools: tools.length > 0 ? tools : undefined,
|
|
190
230
|
temperature: 0.1, // Low temperature for deterministic tool execution
|
|
191
|
-
});
|
|
231
|
+
}, useToolCallingFallback);
|
|
192
232
|
}
|
|
193
233
|
catch (error) {
|
|
194
234
|
// API error (e.g., invalid tool call parameters)
|
|
195
235
|
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
196
236
|
logger.warn(` [Worker] API error - ${errorMsg.substring(0, 100)}...`);
|
|
197
|
-
//
|
|
237
|
+
// Detect the specific "Failed to parse tool call arguments as JSON" pattern.
|
|
238
|
+
// These 400 errors originate from the model generating malformed JSON for
|
|
239
|
+
// tool call arguments (e.g. large content with special Unicode characters).
|
|
240
|
+
// A generic "retry with error feedback" loop cannot fix this — the same model
|
|
241
|
+
// will keep producing the same malformed output. We need a different approach.
|
|
242
|
+
const isJsonParseError = errorMsg.includes('Failed to parse tool call arguments as JSON') ||
|
|
243
|
+
errorMsg.includes('tool_use_failed');
|
|
244
|
+
if (isJsonParseError) {
|
|
245
|
+
apiJsonParseFailures++;
|
|
246
|
+
// Record in toolFailureCounts so failedTools is always non-empty on exit.
|
|
247
|
+
// This ensures the Client's data-driven CompletionSignal fires (strategy=escalate)
|
|
248
|
+
// rather than the LLM-based path that would return strategy=retry, causing an
|
|
249
|
+
// infinite correction-subtask loop.
|
|
250
|
+
toolFailureCounts.set(API_JSON_PARSE_FAILURE_SIG, {
|
|
251
|
+
count: apiJsonParseFailures,
|
|
252
|
+
lastError: errorMsg,
|
|
253
|
+
});
|
|
254
|
+
const totalBudget = API_PARSE_FAIL_THRESHOLD + SECONDARY_MODEL_ATTEMPTS;
|
|
255
|
+
if (!phaseSwitched && apiJsonParseFailures >= API_PARSE_FAIL_THRESHOLD) {
|
|
256
|
+
// Phase 1 exhausted — attempt a phase switch
|
|
257
|
+
phaseSwitched = true;
|
|
258
|
+
if (toolCallingIsMain) {
|
|
259
|
+
// Tool-calling model (primary) failed → fall back to reasoning model
|
|
260
|
+
useToolCallingFallback = false;
|
|
261
|
+
logger.warn(` [Worker] Tool-calling model failed JSON tool-call serialisation ` +
|
|
262
|
+
`${apiJsonParseFailures} time(s) — switching to reasoning model for ` +
|
|
263
|
+
`up to ${SECONDARY_MODEL_ATTEMPTS} more attempt(s)`);
|
|
264
|
+
conversationHistory.push({
|
|
265
|
+
role: 'user',
|
|
266
|
+
content: `NOTE: The previous model had trouble formatting its tool call as valid JSON. ` +
|
|
267
|
+
`A different model is now being used. Please re-attempt the task — ` +
|
|
268
|
+
`use the same tools but ensure all argument values are valid JSON ` +
|
|
269
|
+
`(avoid raw special characters inside string values; escape them if needed).`,
|
|
270
|
+
});
|
|
271
|
+
continue; // retry with reasoning model
|
|
272
|
+
}
|
|
273
|
+
else if (this.orchestrator.hasToolCallingModel()) {
|
|
274
|
+
// Reasoning model (primary) failed → switch to tool-calling model
|
|
275
|
+
useToolCallingFallback = true;
|
|
276
|
+
logger.warn(` [Worker] Reasoning model failed JSON tool-call serialisation ` +
|
|
277
|
+
`${apiJsonParseFailures} time(s) — switching to tool-calling LLM for ` +
|
|
278
|
+
`up to ${SECONDARY_MODEL_ATTEMPTS} more attempt(s)`);
|
|
279
|
+
conversationHistory.push({
|
|
280
|
+
role: 'user',
|
|
281
|
+
content: `NOTE: The previous model had trouble formatting its tool call as valid JSON. ` +
|
|
282
|
+
`A different model is now being used. Please re-attempt the task — ` +
|
|
283
|
+
`use the same tools but ensure all argument values are valid JSON ` +
|
|
284
|
+
`(avoid raw special characters inside string values; escape them if needed).`,
|
|
285
|
+
});
|
|
286
|
+
continue; // retry with fallback model active
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
// No secondary model configured — hard-stop immediately
|
|
290
|
+
logger.error(` [Worker] JSON serialisation failure — no secondary model configured. ` +
|
|
291
|
+
`Hard-stopping after ${apiJsonParseFailures} attempt(s).`);
|
|
292
|
+
finalResult =
|
|
293
|
+
`Task failed: The reasoning model could not generate valid tool-call JSON ` +
|
|
294
|
+
`after ${apiJsonParseFailures} attempt(s). ` +
|
|
295
|
+
`Error: ${errorMsg}. ` +
|
|
296
|
+
`Tip: configure a tool-calling LLM in Jiva settings to enable automatic fallback.`;
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
else if (apiJsonParseFailures >= totalBudget) {
|
|
301
|
+
// Phase 2 also exhausted — hard-stop
|
|
302
|
+
const primaryLabel = toolCallingIsMain ? 'tool-calling' : 'reasoning';
|
|
303
|
+
const secondaryLabel = toolCallingIsMain ? 'reasoning' : 'tool-calling';
|
|
304
|
+
logger.error(` [Worker] Both ${primaryLabel} and ${secondaryLabel} models failed JSON serialisation. ` +
|
|
305
|
+
`Hard-stopping after ${apiJsonParseFailures} total attempt(s).`);
|
|
306
|
+
finalResult =
|
|
307
|
+
`Task failed: Both the ${primaryLabel} model and the ${secondaryLabel} fallback model ` +
|
|
308
|
+
`could not generate valid tool-call JSON after ${apiJsonParseFailures} total attempt(s). ` +
|
|
309
|
+
`Error: ${errorMsg}.`;
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
// Still within budget — add informative error and let the (possibly switched) model retry
|
|
313
|
+
logger.info(` [Worker] JSON parse failure ${apiJsonParseFailures}/${totalBudget} — ` +
|
|
314
|
+
`retrying with ${useToolCallingFallback ? 'tool-calling model' : 'reasoning model'} ` +
|
|
315
|
+
`(attempt ${iteration + 2}/${this.maxIterations})`);
|
|
316
|
+
conversationHistory.push({
|
|
317
|
+
role: 'user',
|
|
318
|
+
content: `ERROR: The model produced a tool call with invalid JSON arguments.\n` +
|
|
319
|
+
`Specific error: ${errorMsg}\n\n` +
|
|
320
|
+
`This usually happens with large file content containing special characters. ` +
|
|
321
|
+
`To fix this:\n` +
|
|
322
|
+
`1. Escape all special characters in string values (\\n, \\t, \\\\, etc.)\n` +
|
|
323
|
+
`2. Wrap Unicode symbols as plain ASCII equivalents (e.g. use [ ] instead of ☐)\n` +
|
|
324
|
+
`3. If the content is very long, write it to the file in smaller chunks\n` +
|
|
325
|
+
`Please retry the tool call with corrected arguments.`,
|
|
326
|
+
});
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
// Non-JSON-parse API error — regular retry with error feedback
|
|
198
330
|
if (iteration >= this.maxIterations - 1) {
|
|
199
331
|
logger.error(` [Worker] Max retries reached after API errors`);
|
|
200
332
|
finalResult = `Failed to complete subtask due to repeated errors: ${errorMsg}`;
|
|
201
333
|
break;
|
|
202
334
|
}
|
|
203
|
-
// Add error feedback to conversation so Worker can correct itself
|
|
204
335
|
logger.info(` [Worker] Retrying with error feedback (attempt ${iteration + 2}/${this.maxIterations})`);
|
|
205
336
|
conversationHistory.push({
|
|
206
337
|
role: 'user',
|
|
@@ -220,32 +351,42 @@ Please complete this subtask and report your findings.`,
|
|
|
220
351
|
const args = JSON.parse(tc.function.arguments);
|
|
221
352
|
return `${tc.function.name}:${JSON.stringify(args)}`;
|
|
222
353
|
});
|
|
223
|
-
// Check if we're about to repeat the same tool call
|
|
354
|
+
// Check if we're about to repeat the same tool call (name-only check)
|
|
224
355
|
const lastToolCalls = toolsUsed.slice(-2);
|
|
225
356
|
const isRepetitive = proposedTools.some(proposed => {
|
|
226
357
|
const toolName = proposed.split(':')[0];
|
|
227
358
|
return lastToolCalls.filter(t => t === toolName).length >= 2;
|
|
228
359
|
});
|
|
229
|
-
|
|
360
|
+
// Exact-signature check: catches same tool + same args within a subtask.
|
|
361
|
+
// This catches the case where the model reads the same file twice in
|
|
362
|
+
// consecutive iterations before the name-only check triggers.
|
|
363
|
+
const exactDuplicate = proposedTools.some(sig => executedToolSignatures.has(sig));
|
|
364
|
+
if ((isRepetitive && iteration >= 2) || exactDuplicate) {
|
|
230
365
|
logger.warn(` [Worker] Detected repetitive tool usage - interrupting loop`);
|
|
231
366
|
conversationHistory.push({
|
|
232
367
|
role: 'user',
|
|
233
|
-
content: `STOP: You are
|
|
368
|
+
content: `STOP: You are calling the same tool with the same arguments repeatedly. Do NOT repeat it again.
|
|
234
369
|
|
|
235
|
-
|
|
236
|
-
1.
|
|
237
|
-
2.
|
|
238
|
-
3. If navigation is already done, the task is COMPLETE - just provide your summary
|
|
370
|
+
You have two choices:
|
|
371
|
+
1. If there is a DIFFERENT next step required to complete the subtask, take it now using the appropriate tool.
|
|
372
|
+
2. If all required work is genuinely complete, respond with a thorough description of everything accomplished — do NOT call any more tools.
|
|
239
373
|
|
|
240
|
-
Do
|
|
374
|
+
Do not assume the task is complete just because one step succeeded. Review what the subtask requires and check whether you have actually finished all of it.`,
|
|
241
375
|
});
|
|
242
376
|
continue; // Skip executing the repetitive tools, let model reconsider
|
|
243
377
|
}
|
|
244
378
|
for (const toolCall of response.toolCalls) {
|
|
245
379
|
const toolName = toolCall.function.name;
|
|
246
380
|
logger.info(` [Worker] Tool: ${toolName}`);
|
|
381
|
+
// Parse args outside the try block so the catch block can reference
|
|
382
|
+
// them for the failure signature (circuit breaker key).
|
|
383
|
+
let args = {};
|
|
384
|
+
try {
|
|
385
|
+
args = JSON.parse(toolCall.function.arguments);
|
|
386
|
+
}
|
|
387
|
+
catch { /* use empty */ }
|
|
388
|
+
const failureSig = `${toolName}:${JSON.stringify(args)}`;
|
|
247
389
|
try {
|
|
248
|
-
const args = JSON.parse(toolCall.function.arguments);
|
|
249
390
|
orchestrationLogger.logWorkerToolCall(toolName, args);
|
|
250
391
|
// Handle spawn_agent specially
|
|
251
392
|
if (toolName === 'spawn_agent') {
|
|
@@ -260,6 +401,7 @@ Do NOT call the same tool again. Either move to the NEXT required step, or if th
|
|
|
260
401
|
maxIterations: args.maxIterations,
|
|
261
402
|
});
|
|
262
403
|
toolsUsed.push(toolName);
|
|
404
|
+
executedToolSignatures.add(`${toolName}:${JSON.stringify(args)}`);
|
|
263
405
|
const resultText = `Sub-agent spawned with persona '${spawnResult.persona}' completed the task.
|
|
264
406
|
|
|
265
407
|
RESULT:
|
|
@@ -276,6 +418,7 @@ Tools used: ${spawnResult.toolsUsed.join(', ')}`;
|
|
|
276
418
|
// Regular MCP tool execution
|
|
277
419
|
const result = await this.mcpManager.getClient().executeTool(toolName, args);
|
|
278
420
|
toolsUsed.push(toolName);
|
|
421
|
+
executedToolSignatures.add(`${toolName}:${JSON.stringify(args)}`);
|
|
279
422
|
// Check if tool returned images (multimodal support)
|
|
280
423
|
let toolResultText;
|
|
281
424
|
let hasImages = false;
|
|
@@ -297,14 +440,38 @@ Tools used: ${spawnResult.toolsUsed.join(', ')}`;
|
|
|
297
440
|
logger.debug(` ✓ [Worker] Tool ${toolName} completed`);
|
|
298
441
|
}
|
|
299
442
|
catch (error) {
|
|
443
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
300
444
|
logger.error(` ✗ [Worker] Tool ${toolName} failed:`, error);
|
|
301
445
|
orchestrationLogger.logWorkerToolResult(toolName, false, false);
|
|
446
|
+
// Always push the raw tool error so the LLM sees what happened
|
|
302
447
|
conversationHistory.push({
|
|
303
448
|
role: 'tool',
|
|
304
449
|
name: toolName,
|
|
305
450
|
tool_call_id: toolCall.id,
|
|
306
|
-
content: `Error: ${
|
|
451
|
+
content: `Error: ${errorMsg}`,
|
|
307
452
|
});
|
|
453
|
+
// Per-tool circuit breaker: track failure count by exact (tool, args) signature
|
|
454
|
+
const prev = toolFailureCounts.get(failureSig) || { count: 0, lastError: '' };
|
|
455
|
+
const newCount = prev.count + 1;
|
|
456
|
+
toolFailureCounts.set(failureSig, { count: newCount, lastError: errorMsg });
|
|
457
|
+
if (newCount === 2) {
|
|
458
|
+
// Second failure: explicit warning to the LLM to change approach
|
|
459
|
+
conversationHistory.push({
|
|
460
|
+
role: 'user',
|
|
461
|
+
content: `WARNING: Tool \`${toolName}\` has now failed twice with the same arguments.\nError: ${errorMsg}\n\nDo NOT call this tool with the same arguments again. Try a different approach, different arguments, or a completely different tool to achieve the same goal.`,
|
|
462
|
+
});
|
|
463
|
+
logger.warn(` [Worker] Tool ${toolName} has failed twice — warning injected`);
|
|
464
|
+
}
|
|
465
|
+
else if (newCount >= 3) {
|
|
466
|
+
// Third failure: hard stop — block the tool and force honest exit
|
|
467
|
+
// Add to executedToolSignatures so the exact-dedup guard prevents further calls
|
|
468
|
+
executedToolSignatures.add(failureSig);
|
|
469
|
+
conversationHistory.push({
|
|
470
|
+
role: 'user',
|
|
471
|
+
content: `HARD STOP: Tool \`${toolName}\` has failed ${newCount} times and will NOT succeed with these arguments.\nFinal error: ${errorMsg}\n\nYou MUST stop attempting this tool. Respond now with an honest report of what you tried and why it failed. Do not call any more tools — just describe the failure clearly so the user can be informed.`,
|
|
472
|
+
});
|
|
473
|
+
logger.warn(` [Worker] Tool ${toolName} circuit breaker triggered after ${newCount} failures`);
|
|
474
|
+
}
|
|
308
475
|
}
|
|
309
476
|
}
|
|
310
477
|
// If images are pending, attach them to next model call
|
|
@@ -331,12 +498,19 @@ Tools used: ${spawnResult.toolsUsed.join(', ')}`;
|
|
|
331
498
|
}
|
|
332
499
|
// After processing tool calls, check if we should prompt for completion
|
|
333
500
|
// This helps Worker recognize when task is done instead of over-iterating
|
|
334
|
-
const shouldPromptCompletion = this.shouldPromptForCompletion(
|
|
501
|
+
const shouldPromptCompletion = this.shouldPromptForCompletion(toolsUsed, iteration);
|
|
335
502
|
if (shouldPromptCompletion) {
|
|
336
503
|
logger.debug(` [Worker] Prompting for task completion check`);
|
|
337
504
|
conversationHistory.push({
|
|
338
505
|
role: 'user',
|
|
339
|
-
content: `
|
|
506
|
+
content: `Your most recent tools have run. Review the subtask instruction and the tool results above.
|
|
507
|
+
|
|
508
|
+
Is the subtask fully complete?
|
|
509
|
+
|
|
510
|
+
- If YES (all required work is done): respond with a thorough, detailed account of exactly what was accomplished — include file paths, content written, commands run, outputs observed, and any other relevant facts. Do NOT call any more tools.
|
|
511
|
+
- If NO (more work remains): continue immediately with the next required tool call. Do not stop early.
|
|
512
|
+
|
|
513
|
+
IMPORTANT: Do not claim completion unless the core deliverable (e.g. the file written, the data fetched, the action performed) is confirmed done. A directory being created is NOT the same as the file inside it being written.`,
|
|
340
514
|
});
|
|
341
515
|
}
|
|
342
516
|
// Continue to process tool results
|
|
@@ -360,28 +534,56 @@ Tools used: ${spawnResult.toolsUsed.join(', ')}`;
|
|
|
360
534
|
return false;
|
|
361
535
|
});
|
|
362
536
|
if (hasSuccessfulTools && !hasToolFailures) {
|
|
363
|
-
// Tools
|
|
364
|
-
|
|
365
|
-
|
|
537
|
+
// Tools ran but the model never produced a conclusive text response.
|
|
538
|
+
// Report the actual tools used so the Client coherence check can verify
|
|
539
|
+
// whether the right work was done — do NOT claim overall success here.
|
|
540
|
+
finalResult = `Max iterations reached. Tools executed (${toolsUsed.length}): ${toolsUsed.join(', ')}. The model did not produce a final confirmation. Validation required.`;
|
|
541
|
+
logger.warn(`[Worker] Max iterations reached after ${toolsUsed.length} tool(s) — no conclusive response from model`);
|
|
366
542
|
}
|
|
367
543
|
else if (hasToolFailures) {
|
|
368
|
-
|
|
369
|
-
|
|
544
|
+
// Build a specific failure summary from the circuit breaker data
|
|
545
|
+
const failureSummary = [...toolFailureCounts.entries()]
|
|
546
|
+
.filter(([, v]) => v.count > 0)
|
|
547
|
+
.map(([sig, v]) => {
|
|
548
|
+
const name = sig.split(':')[0];
|
|
549
|
+
return `${name} (${v.count} attempt${v.count > 1 ? 's' : ''}) — ${v.lastError}`;
|
|
550
|
+
})
|
|
551
|
+
.join('; ');
|
|
552
|
+
finalResult = `Subtask could not be completed. Tool failures: ${failureSummary || 'see errors above'}.`;
|
|
553
|
+
logger.warn(`[Worker] Max iterations reached with tool failures: ${failureSummary}`);
|
|
370
554
|
}
|
|
371
555
|
else {
|
|
372
556
|
finalResult = 'Subtask could not be completed within iteration limit.';
|
|
373
557
|
logger.warn(`[Worker] Max iterations reached with no work done`);
|
|
374
558
|
}
|
|
375
559
|
}
|
|
376
|
-
//
|
|
560
|
+
// Build structured failedTools list from the circuit breaker map.
|
|
561
|
+
// Include all failures (count >= 1) so even a single failed attempt is
|
|
562
|
+
// visible to Client and synthesis — not just those that hit the 3-attempt cap.
|
|
563
|
+
const failedTools = [...toolFailureCounts.entries()].map(([sig, v]) => {
|
|
564
|
+
const colonIdx = sig.indexOf(':');
|
|
565
|
+
const tName = sig.substring(0, colonIdx);
|
|
566
|
+
let tArgs = {};
|
|
567
|
+
try {
|
|
568
|
+
tArgs = JSON.parse(sig.substring(colonIdx + 1));
|
|
569
|
+
}
|
|
570
|
+
catch { /* ignore */ }
|
|
571
|
+
return { toolName: tName, args: tArgs, lastError: v.lastError, attempts: v.count };
|
|
572
|
+
});
|
|
573
|
+
// Determine success: only true when the model produced a natural conclusive
|
|
574
|
+
// response (broke out of the loop normally). The max-iterations fallback
|
|
575
|
+
// paths are all treated as non-success so the Client validates properly.
|
|
377
576
|
const success = !!finalResult &&
|
|
378
577
|
!finalResult.includes('could not be completed') &&
|
|
379
|
-
!finalResult.includes('encountered errors')
|
|
578
|
+
!finalResult.includes('encountered errors') &&
|
|
579
|
+
!finalResult.includes('Max iterations reached') &&
|
|
580
|
+
!finalResult.includes('Validation required');
|
|
380
581
|
orchestrationLogger.logWorkerComplete(success, toolsUsed, iterationCount);
|
|
381
582
|
return {
|
|
382
583
|
success,
|
|
383
584
|
result: finalResult,
|
|
384
585
|
toolsUsed,
|
|
586
|
+
failedTools,
|
|
385
587
|
reasoning: reasoning || 'Task executed',
|
|
386
588
|
};
|
|
387
589
|
}
|
|
@@ -394,7 +596,7 @@ Tools used: ${spawnResult.toolsUsed.join(', ')}`;
|
|
|
394
596
|
* Determine if we should prompt Worker to check for completion
|
|
395
597
|
* This helps prevent over-iteration by asking Worker to confirm task is done
|
|
396
598
|
*/
|
|
397
|
-
shouldPromptForCompletion(
|
|
599
|
+
shouldPromptForCompletion(toolsUsed, currentIteration) {
|
|
398
600
|
// Don't prompt on first iteration - let Worker do initial work
|
|
399
601
|
if (currentIteration === 0) {
|
|
400
602
|
return false;
|
|
@@ -415,24 +617,18 @@ Tools used: ${spawnResult.toolsUsed.join(', ')}`;
|
|
|
415
617
|
if (currentIteration % 2 !== 0) {
|
|
416
618
|
return false;
|
|
417
619
|
}
|
|
418
|
-
//
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
'delete', 'remove', 'open', 'navigate', 'browse',
|
|
424
|
-
];
|
|
425
|
-
const instructionLower = instruction.toLowerCase();
|
|
426
|
-
const hasCompletionIndicator = completionIndicators.some(indicator => instructionLower.includes(indicator));
|
|
427
|
-
// Prompt if we've seen successful file/content or browser operations
|
|
620
|
+
// Prompt only when genuinely substantive operations have completed.
|
|
621
|
+
// 'create_directory' is intentionally excluded — it is a setup step, not
|
|
622
|
+
// evidence that the main deliverable (e.g. file content) has been written.
|
|
623
|
+
// Pure tool-free responses (e.g. "explain X") never reach this point because
|
|
624
|
+
// no tools means hasSignificantOperations is false.
|
|
428
625
|
const hasSignificantOperations = toolsUsed.some(tool => tool.includes('write') ||
|
|
429
|
-
tool.includes('create') ||
|
|
430
626
|
tool.includes('edit') ||
|
|
431
627
|
tool.includes('read') ||
|
|
432
628
|
tool.includes('browser') ||
|
|
433
|
-
tool.includes('navigate')
|
|
434
|
-
|
|
435
|
-
return
|
|
629
|
+
tool.includes('navigate') ||
|
|
630
|
+
(tool.includes('create') && !tool.includes('directory') && !tool.includes('dir')));
|
|
631
|
+
return hasSignificantOperations && toolsUsed.length >= 2;
|
|
436
632
|
}
|
|
437
633
|
}
|
|
438
634
|
//# sourceMappingURL=worker-agent.js.map
|