@nexus-cortex/core 4.51.0 → 4.52.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/dist/adapters/ChatCompletionsAPIAdapter.d.ts.map +1 -1
- package/dist/adapters/ChatCompletionsAPIAdapter.js +16 -0
- package/dist/adapters/ChatCompletionsAPIAdapter.js.map +1 -1
- package/dist/adapters/FormatAdapter.interface.d.ts +4 -4
- package/dist/adapters/FormatAdapter.interface.js +4 -4
- package/dist/adapters/GatewayTranslationLayer.d.ts +9 -0
- package/dist/adapters/GatewayTranslationLayer.d.ts.map +1 -1
- package/dist/adapters/GatewayTranslationLayer.js +24 -1
- package/dist/adapters/GatewayTranslationLayer.js.map +1 -1
- package/dist/adapters/ResponsesAPIAdapter.d.ts +66 -0
- package/dist/adapters/ResponsesAPIAdapter.d.ts.map +1 -1
- package/dist/adapters/ResponsesAPIAdapter.js +66 -0
- package/dist/adapters/ResponsesAPIAdapter.js.map +1 -1
- package/dist/adapters/ToolNamingHandler.d.ts +4 -4
- package/dist/adapters/ToolNamingHandler.js +4 -4
- package/dist/adapters/servedModelDrift.d.ts +44 -0
- package/dist/adapters/servedModelDrift.d.ts.map +1 -0
- package/dist/adapters/servedModelDrift.js +0 -0
- package/dist/adapters/servedModelDrift.js.map +1 -0
- package/dist/agents/AgentStore.d.ts.map +1 -1
- package/dist/agents/AgentStore.js +5 -2
- package/dist/agents/AgentStore.js.map +1 -1
- package/dist/config/RuntimeConfigRegistry.d.ts.map +1 -1
- package/dist/config/RuntimeConfigRegistry.js +1 -0
- package/dist/config/RuntimeConfigRegistry.js.map +1 -1
- package/dist/config/SettingsLoader.js +1 -1
- package/dist/config/SettingsLoader.js.map +1 -1
- package/dist/config/SettingsSchema.d.ts +2 -2
- package/dist/config/SettingsSchema.d.ts.map +1 -1
- package/dist/config/SettingsSchema.js +13 -8
- package/dist/config/SettingsSchema.js.map +1 -1
- package/dist/conversation/ContextBudgetManager.d.ts.map +1 -1
- package/dist/conversation/ContextBudgetManager.js +4 -2
- package/dist/conversation/ContextBudgetManager.js.map +1 -1
- package/dist/conversation/ToolResultPruner.d.ts +45 -0
- package/dist/conversation/ToolResultPruner.d.ts.map +1 -0
- package/dist/conversation/ToolResultPruner.js +94 -0
- package/dist/conversation/ToolResultPruner.js.map +1 -0
- package/dist/middleware/ErrorClassificationMiddleware.d.ts +19 -0
- package/dist/middleware/ErrorClassificationMiddleware.d.ts.map +1 -1
- package/dist/middleware/ErrorClassificationMiddleware.js +55 -1
- package/dist/middleware/ErrorClassificationMiddleware.js.map +1 -1
- package/dist/middleware/PermissionsMiddleware.d.ts +2 -2
- package/dist/middleware/PermissionsMiddleware.js +2 -2
- package/dist/middleware/RetryMiddleware.d.ts.map +1 -1
- package/dist/middleware/RetryMiddleware.js +8 -2
- package/dist/middleware/RetryMiddleware.js.map +1 -1
- package/dist/middleware/contracts/MiddlewareContracts.d.ts +4 -0
- package/dist/middleware/contracts/MiddlewareContracts.d.ts.map +1 -1
- package/dist/middleware/permissions/BashCommandPolicy.d.ts +2 -2
- package/dist/middleware/permissions/BashCommandPolicy.js +2 -2
- package/dist/middleware/permissions/BlacklistPolicy.d.ts +1 -1
- package/dist/middleware/permissions/BlacklistPolicy.js +1 -1
- package/dist/middleware/permissions/FileOperationPolicy.d.ts +2 -2
- package/dist/middleware/permissions/FileOperationPolicy.js +2 -2
- package/dist/middleware/permissions/PermissionAuditLogger.d.ts +1 -1
- package/dist/middleware/permissions/PermissionAuditLogger.js +1 -1
- package/dist/middleware/permissions/PermissionConfig.js +2 -2
- package/dist/middleware/permissions/PermissionConfig.js.map +1 -1
- package/dist/middleware/permissions/PermissionEvaluator.d.ts +2 -2
- package/dist/middleware/permissions/PermissionEvaluator.js +2 -2
- package/dist/middleware/permissions/WhitelistPolicy.d.ts +2 -2
- package/dist/middleware/permissions/WhitelistPolicy.js +2 -2
- package/dist/middleware/permissions/WorkspaceBoundaryPolicy.d.ts.map +1 -1
- package/dist/middleware/permissions/WorkspaceBoundaryPolicy.js +2 -3
- package/dist/middleware/permissions/WorkspaceBoundaryPolicy.js.map +1 -1
- package/dist/models/ModelConfig.interface.js +1 -1
- package/dist/orchestrator/APIClient.d.ts +10 -0
- package/dist/orchestrator/APIClient.d.ts.map +1 -1
- package/dist/orchestrator/APIClient.js +81 -6
- package/dist/orchestrator/APIClient.js.map +1 -1
- package/dist/orchestrator/CortexOrchestrator.d.ts +30 -0
- package/dist/orchestrator/CortexOrchestrator.d.ts.map +1 -1
- package/dist/orchestrator/CortexOrchestrator.js +256 -36
- package/dist/orchestrator/CortexOrchestrator.js.map +1 -1
- package/dist/orchestrator/OrchestratorFactory.js +1 -1
- package/dist/orchestrator/OrchestratorFactory.js.map +1 -1
- package/dist/orchestrator/SubAgentPermissionChecker.d.ts.map +1 -1
- package/dist/orchestrator/SubAgentPermissionChecker.js +13 -2
- package/dist/orchestrator/SubAgentPermissionChecker.js.map +1 -1
- package/dist/orchestrator/apiErrorClassifier.d.ts.map +1 -1
- package/dist/orchestrator/apiErrorClassifier.js +8 -1
- package/dist/orchestrator/apiErrorClassifier.js.map +1 -1
- package/dist/orchestrator/emptyResponseClassifier.d.ts +29 -0
- package/dist/orchestrator/emptyResponseClassifier.d.ts.map +1 -0
- package/dist/orchestrator/emptyResponseClassifier.js +42 -0
- package/dist/orchestrator/emptyResponseClassifier.js.map +1 -0
- package/dist/orchestrator/index.d.ts +1 -0
- package/dist/orchestrator/index.d.ts.map +1 -1
- package/dist/orchestrator/index.js +3 -0
- package/dist/orchestrator/index.js.map +1 -1
- package/dist/orchestrator/structuredOutput.d.ts +144 -0
- package/dist/orchestrator/structuredOutput.d.ts.map +1 -0
- package/dist/orchestrator/structuredOutput.js +230 -0
- package/dist/orchestrator/structuredOutput.js.map +1 -0
- package/dist/orchestrator/tailRepetitionDetector.d.ts +57 -0
- package/dist/orchestrator/tailRepetitionDetector.d.ts.map +1 -0
- package/dist/orchestrator/tailRepetitionDetector.js +98 -0
- package/dist/orchestrator/tailRepetitionDetector.js.map +1 -0
- package/dist/system-messages/SystemReminderInjector.d.ts +3 -3
- package/dist/system-messages/SystemReminderInjector.d.ts.map +1 -1
- package/dist/system-messages/SystemReminderInjector.js +4 -4
- package/dist/system-messages/SystemReminderInjector.js.map +1 -1
- package/dist/system-messages/messages/EXAMPLES.md +18 -18
- package/dist/system-messages/messages/SYSTEM_PROMPT.md +15 -13
- package/dist/system-messages/messages/TASK_AGENT_GUIDE.md +2 -2
- package/dist/system-messages/messages/TOOL_USAGE_GUIDE.md +43 -42
- package/dist/system-messages/messages/WORK_QUALITY.md +12 -10
- package/dist/tools/ToolFactory.d.ts.map +1 -1
- package/dist/tools/ToolFactory.js +3 -1
- package/dist/tools/ToolFactory.js.map +1 -1
- package/dist/tools/context-management/InitCortexContext.js +1 -1
- package/dist/tools/context-management/InitCortexContext.js.map +1 -1
- package/dist/tools/mcp-management/InitMcpConfig.js +1 -1
- package/dist/tools/registries/BaseToolRegistry.js +45 -45
- package/dist/tools/registries/BaseToolRegistry.js.map +1 -1
- package/package.json +4 -3
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StructuredOutput — schema-constrained JSON output via a synthetic tool
|
|
3
|
+
* (grok-build port; see grok-build turn.rs structured-output mechanism).
|
|
4
|
+
*
|
|
5
|
+
* Gives callers schema-constrained JSON output on EVERY provider path with no
|
|
6
|
+
* transport/wire changes: when a request carries `options.jsonSchema`, a
|
|
7
|
+
* request-scoped synthetic tool named `StructuredOutput` is appended to the
|
|
8
|
+
* tools array for every request of the turn. The orchestrator intercepts calls
|
|
9
|
+
* to it BEFORE executor dispatch (it is never registered in any registry and
|
|
10
|
+
* never routed to a real executor), validates the arguments against the
|
|
11
|
+
* caller's JSON Schema with Ajv, and steers the model with synthetic
|
|
12
|
+
* tool_results:
|
|
13
|
+
*
|
|
14
|
+
* - co-emitted with other tools -> corrective (not an error, no retry burn)
|
|
15
|
+
* - invalid args, attempts < max -> corrective error listing schema failures
|
|
16
|
+
* - invalid args, attempts >= max-> fail-open: accept with validation errors
|
|
17
|
+
* (never wedge the loop)
|
|
18
|
+
* - valid args -> accepted; captured on the turn state
|
|
19
|
+
*
|
|
20
|
+
* The captured result surfaces as `metadata.structuredOutput` on the
|
|
21
|
+
* orchestrator response (non-streaming) and on the `message_stop` chunk data
|
|
22
|
+
* (streaming).
|
|
23
|
+
*
|
|
24
|
+
* ALL steering lives in the injected tool's DESCRIPTION and in the synthetic
|
|
25
|
+
* tool_result texts below — message history is never mutated for steering.
|
|
26
|
+
*
|
|
27
|
+
* Naming: the canonical tool name is PascalCase `StructuredOutput`; the
|
|
28
|
+
* GatewayTranslationLayer converts to the provider's naming convention on the
|
|
29
|
+
* wire and restores canonical names on return. This tool is request-scoped and
|
|
30
|
+
* is never registered in BaseToolRegistry.
|
|
31
|
+
*/
|
|
32
|
+
import AjvImport from 'ajv';
|
|
33
|
+
// ajv ships CJS; depending on the loader the ESM default import is either the
|
|
34
|
+
// class itself or a namespace object carrying it on `.default`. Normalize.
|
|
35
|
+
const AjvCtor = (AjvImport.default ??
|
|
36
|
+
AjvImport);
|
|
37
|
+
/** Canonical (PascalCase) name of the synthetic structured-output tool. */
|
|
38
|
+
export const STRUCTURED_OUTPUT_TOOL_NAME = 'StructuredOutput';
|
|
39
|
+
/** Maximum validation-failure retries before failing open. */
|
|
40
|
+
export const MAX_STRUCTURED_OUTPUT_RETRIES = 3;
|
|
41
|
+
/**
|
|
42
|
+
* Corrective tool_result text used when the model calls the structured-output
|
|
43
|
+
* tool in the same assistant round as other tool calls. Not an error and not
|
|
44
|
+
* a counted retry — the model simply has not finished its tool work yet.
|
|
45
|
+
*/
|
|
46
|
+
export const COEMISSION_CORRECTIVE = 'Structured output was not captured: you called StructuredOutput together with other tools in the same response. ' +
|
|
47
|
+
'Finish your other tool work first, then call StructuredOutput alone as your final action.';
|
|
48
|
+
/** Text of the tool_result synthesized when validation succeeds. */
|
|
49
|
+
export const ACCEPTED_RESULT_TEXT = 'Structured output accepted. Your structured answer has been captured; you can end the turn now.';
|
|
50
|
+
/**
|
|
51
|
+
* Text of the tool_result synthesized when the retry budget is exhausted and
|
|
52
|
+
* the last arguments are accepted anyway (fail-open — never wedge the loop).
|
|
53
|
+
*/
|
|
54
|
+
export const ACCEPTED_WITH_ERRORS_RESULT_TEXT = 'Accepted with validation errors. The retry limit was reached, so your last arguments were captured as-is ' +
|
|
55
|
+
'even though they do not fully satisfy the schema. You can end the turn now.';
|
|
56
|
+
/**
|
|
57
|
+
* Build the request-scoped synthetic StructuredOutput tool.
|
|
58
|
+
*
|
|
59
|
+
* The element shape matches the canonical tools array the orchestrator passes
|
|
60
|
+
* to gatewayTranslation.prepareRequest ({ name, description, schema }, see
|
|
61
|
+
* BaseToolRegistry entries / toCanonicalTool). discoveryTier 'essential'
|
|
62
|
+
* guarantees the tool SURVIVES ClientSideToolFilter's deferred-loading
|
|
63
|
+
* reduction — same rationale as server-side tools (ServerSideTools.ts).
|
|
64
|
+
*
|
|
65
|
+
* The caller's schema is expected to be a top-level `object` JSON Schema
|
|
66
|
+
* (the same contract as provider-native structured outputs).
|
|
67
|
+
*/
|
|
68
|
+
export function buildStructuredOutputTool(schema) {
|
|
69
|
+
return {
|
|
70
|
+
name: STRUCTURED_OUTPUT_TOOL_NAME,
|
|
71
|
+
description: 'REQUIRED: you MUST return your final answer by calling this tool — a plain text reply will NOT be ' +
|
|
72
|
+
'accepted as the answer for this request. ' +
|
|
73
|
+
'Captures the FINAL structured answer for this request as machine-readable JSON. ' +
|
|
74
|
+
'The calling application reads the arguments of this tool call as the result of the request, so the ' +
|
|
75
|
+
'arguments must satisfy the provided JSON schema exactly: every required field present, correct types, ' +
|
|
76
|
+
'and no wrapping, markdown, or commentary — the arguments ARE the answer. ' +
|
|
77
|
+
'Call this tool exactly once, as your FINAL action, after all other tool work is complete. ' +
|
|
78
|
+
'Do not call it together with other tools. If validation fails you will receive the list of schema ' +
|
|
79
|
+
'violations; call StructuredOutput again with corrected arguments.',
|
|
80
|
+
schema: schema,
|
|
81
|
+
discoveryTier: 'essential',
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** Create the turn-local state for a request that set options.jsonSchema. */
|
|
85
|
+
export function createStructuredOutputTurnState(schema) {
|
|
86
|
+
return {
|
|
87
|
+
schema,
|
|
88
|
+
tool: buildStructuredOutputTool(schema),
|
|
89
|
+
attempts: 0,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* True when a (canonical) tool-call name addresses the structured-output tool.
|
|
94
|
+
* The GatewayTranslationLayer restores canonical names on return, but the
|
|
95
|
+
* snake_case wire form is matched too as a defensive belt.
|
|
96
|
+
*/
|
|
97
|
+
export function isStructuredOutputToolName(name) {
|
|
98
|
+
return name === STRUCTURED_OUTPUT_TOOL_NAME || name === 'structured_output';
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Return a tools array that contains the structured-output tool, cloning
|
|
102
|
+
* instead of mutating (clone-on-write — the input array is never modified).
|
|
103
|
+
*/
|
|
104
|
+
export function ensureStructuredOutputTool(tools, state) {
|
|
105
|
+
const base = tools ?? [];
|
|
106
|
+
if (base.some((t) => t?.name === STRUCTURED_OUTPUT_TOOL_NAME)) {
|
|
107
|
+
return base;
|
|
108
|
+
}
|
|
109
|
+
return [...base, state.tool];
|
|
110
|
+
}
|
|
111
|
+
// Compiled-validator cache keyed by schema JSON. Ajv compilation is not free;
|
|
112
|
+
// a turn validates the same schema up to MAX_STRUCTURED_OUTPUT_RETRIES times.
|
|
113
|
+
const validatorCache = new Map();
|
|
114
|
+
const VALIDATOR_CACHE_MAX = 32;
|
|
115
|
+
function getValidator(schema) {
|
|
116
|
+
const key = JSON.stringify(schema);
|
|
117
|
+
const cached = validatorCache.get(key);
|
|
118
|
+
if (cached)
|
|
119
|
+
return cached;
|
|
120
|
+
const ajv = new AjvCtor({ allErrors: true, strict: false });
|
|
121
|
+
const validate = ajv.compile(schema);
|
|
122
|
+
if (validatorCache.size >= VALIDATOR_CACHE_MAX) {
|
|
123
|
+
// Drop the oldest entry (Map preserves insertion order).
|
|
124
|
+
const oldest = validatorCache.keys().next().value;
|
|
125
|
+
if (oldest !== undefined)
|
|
126
|
+
validatorCache.delete(oldest);
|
|
127
|
+
}
|
|
128
|
+
validatorCache.set(key, validate);
|
|
129
|
+
return validate;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Validate candidate structured-output arguments against the caller's schema.
|
|
133
|
+
* Errors are rendered as short "instancePath: message" strings.
|
|
134
|
+
*/
|
|
135
|
+
export function validateStructuredOutput(schema, args) {
|
|
136
|
+
let validate;
|
|
137
|
+
try {
|
|
138
|
+
validate = getValidator(schema);
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
// An uncompilable schema is a caller bug — fail open (accept anything)
|
|
142
|
+
// rather than wedging the turn on every validation attempt.
|
|
143
|
+
return {
|
|
144
|
+
valid: true,
|
|
145
|
+
errors: [`schema compilation failed, validation skipped: ${err?.message ?? String(err)}`],
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
const valid = validate(args) === true;
|
|
149
|
+
if (valid)
|
|
150
|
+
return { valid: true, errors: [] };
|
|
151
|
+
const errors = (validate.errors ?? []).map((e) => {
|
|
152
|
+
const where = e.instancePath && e.instancePath.length > 0 ? e.instancePath : '(root)';
|
|
153
|
+
return `${where}: ${e.message ?? 'invalid'}`;
|
|
154
|
+
});
|
|
155
|
+
return { valid: false, errors: errors.length > 0 ? errors : ['(root): invalid'] };
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Corrective tool_result text for a failed validation attempt: tells the model
|
|
159
|
+
* exactly what failed and to call StructuredOutput again with corrected args.
|
|
160
|
+
*/
|
|
161
|
+
export function buildCorrectiveResult(errors, attempt, max) {
|
|
162
|
+
const list = errors.map((e) => ` - ${e}`).join('\n');
|
|
163
|
+
return (`Structured output validation failed (attempt ${attempt}/${max}):\n${list}\n\n` +
|
|
164
|
+
'Call StructuredOutput again with corrected arguments that satisfy the schema exactly. ' +
|
|
165
|
+
'Fix every listed violation; do not change fields that already validate.');
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Pure decision function for one StructuredOutput tool call.
|
|
169
|
+
*
|
|
170
|
+
* @param schema caller's JSON Schema
|
|
171
|
+
* @param args the tool call's input arguments
|
|
172
|
+
* @param attemptsBefore validation attempts consumed before this call
|
|
173
|
+
* @param hasOtherToolCallsInRound whether the assistant round contained any
|
|
174
|
+
* other tool call (co-emission)
|
|
175
|
+
*/
|
|
176
|
+
export function evaluateStructuredOutputCall(schema, args, attemptsBefore, hasOtherToolCallsInRound) {
|
|
177
|
+
if (hasOtherToolCallsInRound) {
|
|
178
|
+
// Not counted as a retry, not an error — plain steering.
|
|
179
|
+
return {
|
|
180
|
+
toolResultText: COEMISSION_CORRECTIVE,
|
|
181
|
+
isError: false,
|
|
182
|
+
attemptsAfter: attemptsBefore,
|
|
183
|
+
accepted: false,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
const attemptsAfter = attemptsBefore + 1;
|
|
187
|
+
const { valid, errors } = validateStructuredOutput(schema, args);
|
|
188
|
+
if (valid) {
|
|
189
|
+
return {
|
|
190
|
+
toolResultText: ACCEPTED_RESULT_TEXT,
|
|
191
|
+
isError: false,
|
|
192
|
+
attemptsAfter,
|
|
193
|
+
accepted: true,
|
|
194
|
+
result: { value: args, valid: true, attempts: attemptsAfter },
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
if (attemptsAfter < MAX_STRUCTURED_OUTPUT_RETRIES) {
|
|
198
|
+
return {
|
|
199
|
+
toolResultText: buildCorrectiveResult(errors, attemptsAfter, MAX_STRUCTURED_OUTPUT_RETRIES),
|
|
200
|
+
isError: true,
|
|
201
|
+
attemptsAfter,
|
|
202
|
+
accepted: false,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
// Retry budget exhausted — fail open so the turn can end (mirrors the
|
|
206
|
+
// grok-build infra-error path: never wedge the loop).
|
|
207
|
+
return {
|
|
208
|
+
toolResultText: ACCEPTED_WITH_ERRORS_RESULT_TEXT,
|
|
209
|
+
isError: false,
|
|
210
|
+
attemptsAfter,
|
|
211
|
+
accepted: true,
|
|
212
|
+
result: { value: args, valid: false, attempts: attemptsAfter, errors },
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Finalize the turn's structured-output surface. When the model never called
|
|
217
|
+
* the tool, a deterministic invalid result is returned so callers that set
|
|
218
|
+
* options.jsonSchema always find `metadata.structuredOutput`.
|
|
219
|
+
*/
|
|
220
|
+
export function finalizeStructuredOutput(state) {
|
|
221
|
+
if (state.result)
|
|
222
|
+
return state.result;
|
|
223
|
+
return {
|
|
224
|
+
value: undefined,
|
|
225
|
+
valid: false,
|
|
226
|
+
attempts: state.attempts,
|
|
227
|
+
errors: ['StructuredOutput tool was not called before the turn ended'],
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
//# sourceMappingURL=structuredOutput.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structuredOutput.js","sourceRoot":"","sources":["../../src/orchestrator/structuredOutput.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,SAAS,MAAM,KAAK,CAAC;AAI5B,8EAA8E;AAC9E,2EAA2E;AAC3E,MAAM,OAAO,GAAG,CAAE,SAA8C,CAAC,OAAO;IACtE,SAAS,CAAqB,CAAC;AAEjC,2EAA2E;AAC3E,MAAM,CAAC,MAAM,2BAA2B,GAAG,kBAAkB,CAAC;AAE9D,8DAA8D;AAC9D,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,CAAC;AAE/C;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAChC,kHAAkH;IAClH,2FAA2F,CAAC;AAE9F,oEAAoE;AACpE,MAAM,CAAC,MAAM,oBAAoB,GAC/B,iGAA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MAAM,EAAE,IAAI,CAAC,CAAC;IAEjE,IAAI,KAAK,EAAE,CAAC;QACV,OAAO;YACL,cAAc,EAAE,oBAAoB;YACpC,OAAO,EAAE,KAAK;YACd,aAAa;YACb,QAAQ,EAAE,IAAI;YACd,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,aAAa,EAAE;SAC9D,CAAC;IACJ,CAAC;IAED,IAAI,aAAa,GAAG,6BAA6B,EAAE,CAAC;QAClD,OAAO;YACL,cAAc,EAAE,qBAAqB,CAAC,MAAM,EAAE,aAAa,EAAE,6BAA6B,CAAC;YAC3F,OAAO,EAAE,IAAI;YACb,aAAa;YACb,QAAQ,EAAE,KAAK;SAChB,CAAC;IACJ,CAAC;IAED,sEAAsE;IACtE,sDAAsD;IACtD,OAAO;QACL,cAAc,EAAE,gCAAgC;QAChD,OAAO,EAAE,KAAK;QACd,aAAa;QACb,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE;KACvE,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CAAC,KAAgC;IACvE,IAAI,KAAK,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC,MAAM,CAAC;IACtC,OAAO;QACL,KAAK,EAAE,SAAS;QAChB,KAAK,EAAE,KAAK;QACZ,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,MAAM,EAAE,CAAC,4DAA4D,CAAC;KACvE,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tail-repetition detector (grok-build doom-loop port, 2026-08-01).
|
|
3
|
+
*
|
|
4
|
+
* grok-build's server watches the generation stream and emits
|
|
5
|
+
* `response.doom_loop_check` events with triggers like `tail_repetition:8@thinking`
|
|
6
|
+
* — "the last N units of the thinking channel are repeating." The public xAI
|
|
7
|
+
* API does not expose that event, so this is the CLIENT-SIDE equivalent: a pure
|
|
8
|
+
* detector over the accumulated thinking text, with NO server dependency.
|
|
9
|
+
*
|
|
10
|
+
* It targets the documented failure mode where a reasoning model gets stuck in
|
|
11
|
+
* a thinking-channel attractor (the grok conceptual-grind / thinking-only loop),
|
|
12
|
+
* repeating the same phrase or line block without terminating. Detection lets
|
|
13
|
+
* the caller abort the doomed completion and resample.
|
|
14
|
+
*
|
|
15
|
+
* Pure and allocation-cheap: bounded to the tail window so it can run on every
|
|
16
|
+
* thinking delta. Returns looping=false until the tail genuinely repeats.
|
|
17
|
+
*/
|
|
18
|
+
export interface TailRepetitionOptions {
|
|
19
|
+
/** Chars of the tail to examine. Default 2400. */
|
|
20
|
+
tailWindow: number;
|
|
21
|
+
/** Shortest repeating cycle (chars) to consider. Default 24 — below this,
|
|
22
|
+
* natural language coincidentally repeats short fragments. */
|
|
23
|
+
minCycleLen: number;
|
|
24
|
+
/** Longest repeating cycle (chars) to consider. Default 600. */
|
|
25
|
+
maxCycleLen: number;
|
|
26
|
+
/** Consecutive identical cycles required to call it a loop. Default 4. */
|
|
27
|
+
minRepeats: number;
|
|
28
|
+
}
|
|
29
|
+
export interface TailRepetitionResult {
|
|
30
|
+
looping: boolean;
|
|
31
|
+
/** Label mirroring grok-build's, e.g. "tail_repetition:5@thinking". */
|
|
32
|
+
trigger?: string;
|
|
33
|
+
/** The repeating unit (trimmed to 120 chars for logging). */
|
|
34
|
+
cycle?: string;
|
|
35
|
+
/** Number of consecutive repeats observed. */
|
|
36
|
+
repeats?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether the tail-loop guard is active for a given provider.
|
|
40
|
+
* XAI_TAIL_LOOP_GUARD=true -> xAI only (the documented failure mode)
|
|
41
|
+
* TAIL_LOOP_GUARD_ALL_PROVIDERS=true -> every provider's reasoning channel
|
|
42
|
+
* Both default OFF. The all-provider mode is opt-in because each provider's
|
|
43
|
+
* reasoning has a different natural-repetition profile — canary the
|
|
44
|
+
* false-positive rate per provider before enabling it. When neither is set,
|
|
45
|
+
* this is false and the guarded code path is byte-identical to unguarded.
|
|
46
|
+
*/
|
|
47
|
+
export declare function tailLoopGuardEnabled(provider?: string): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Detect a repeating suffix in `text`. Two complementary checks:
|
|
50
|
+
* 1. char-cycle: does the tail end with the same block repeated >= minRepeats?
|
|
51
|
+
* 2. line-cycle: are the last minRepeats non-empty lines identical?
|
|
52
|
+
* The line check catches loops that repeat whole reasoning lines separated by
|
|
53
|
+
* newlines (the common grok thinking-loop shape); the char check catches
|
|
54
|
+
* newline-free phrase spins.
|
|
55
|
+
*/
|
|
56
|
+
export declare function detectTailRepetition(text: string, opts?: Partial<TailRepetitionOptions>): TailRepetitionResult;
|
|
57
|
+
//# sourceMappingURL=tailRepetitionDetector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tailRepetitionDetector.d.ts","sourceRoot":"","sources":["../../src/orchestrator/tailRepetitionDetector.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,WAAW,qBAAqB;IACpC,kDAAkD;IAClD,UAAU,EAAE,MAAM,CAAC;IACnB;mEAC+D;IAC/D,WAAW,EAAE,MAAM,CAAC;IACpB,gEAAgE;IAChE,WAAW,EAAE,MAAM,CAAC;IACpB,0EAA0E;IAC1E,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,OAAO,CAAC;IACjB,uEAAuE;IACvE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6DAA6D;IAC7D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8CAA8C;IAC9C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAG/D;AAeD;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,IAAI,GAAE,OAAO,CAAC,qBAAqB,CAAM,GACxC,oBAAoB,CA8CtB"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tail-repetition detector (grok-build doom-loop port, 2026-08-01).
|
|
3
|
+
*
|
|
4
|
+
* grok-build's server watches the generation stream and emits
|
|
5
|
+
* `response.doom_loop_check` events with triggers like `tail_repetition:8@thinking`
|
|
6
|
+
* — "the last N units of the thinking channel are repeating." The public xAI
|
|
7
|
+
* API does not expose that event, so this is the CLIENT-SIDE equivalent: a pure
|
|
8
|
+
* detector over the accumulated thinking text, with NO server dependency.
|
|
9
|
+
*
|
|
10
|
+
* It targets the documented failure mode where a reasoning model gets stuck in
|
|
11
|
+
* a thinking-channel attractor (the grok conceptual-grind / thinking-only loop),
|
|
12
|
+
* repeating the same phrase or line block without terminating. Detection lets
|
|
13
|
+
* the caller abort the doomed completion and resample.
|
|
14
|
+
*
|
|
15
|
+
* Pure and allocation-cheap: bounded to the tail window so it can run on every
|
|
16
|
+
* thinking delta. Returns looping=false until the tail genuinely repeats.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Whether the tail-loop guard is active for a given provider.
|
|
20
|
+
* XAI_TAIL_LOOP_GUARD=true -> xAI only (the documented failure mode)
|
|
21
|
+
* TAIL_LOOP_GUARD_ALL_PROVIDERS=true -> every provider's reasoning channel
|
|
22
|
+
* Both default OFF. The all-provider mode is opt-in because each provider's
|
|
23
|
+
* reasoning has a different natural-repetition profile — canary the
|
|
24
|
+
* false-positive rate per provider before enabling it. When neither is set,
|
|
25
|
+
* this is false and the guarded code path is byte-identical to unguarded.
|
|
26
|
+
*/
|
|
27
|
+
export function tailLoopGuardEnabled(provider) {
|
|
28
|
+
if (process.env.TAIL_LOOP_GUARD_ALL_PROVIDERS === 'true')
|
|
29
|
+
return true;
|
|
30
|
+
return process.env.XAI_TAIL_LOOP_GUARD === 'true' && provider === 'xai';
|
|
31
|
+
}
|
|
32
|
+
const DEFAULTS = {
|
|
33
|
+
tailWindow: 2400,
|
|
34
|
+
minCycleLen: 24,
|
|
35
|
+
maxCycleLen: 600,
|
|
36
|
+
minRepeats: 4,
|
|
37
|
+
};
|
|
38
|
+
/** Collapse runs of whitespace so cosmetic spacing differences don't hide a
|
|
39
|
+
* loop, but keep it a cheap single pass. */
|
|
40
|
+
function normalize(s) {
|
|
41
|
+
return s.replace(/\s+/g, ' ');
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Detect a repeating suffix in `text`. Two complementary checks:
|
|
45
|
+
* 1. char-cycle: does the tail end with the same block repeated >= minRepeats?
|
|
46
|
+
* 2. line-cycle: are the last minRepeats non-empty lines identical?
|
|
47
|
+
* The line check catches loops that repeat whole reasoning lines separated by
|
|
48
|
+
* newlines (the common grok thinking-loop shape); the char check catches
|
|
49
|
+
* newline-free phrase spins.
|
|
50
|
+
*/
|
|
51
|
+
export function detectTailRepetition(text, opts = {}) {
|
|
52
|
+
const o = { ...DEFAULTS, ...opts };
|
|
53
|
+
if (!text)
|
|
54
|
+
return { looping: false };
|
|
55
|
+
// ── Line-cycle check (cheap, catches the common shape) ──
|
|
56
|
+
const lines = text.split('\n').map((l) => l.trim()).filter((l) => l.length > 0);
|
|
57
|
+
if (lines.length >= o.minRepeats) {
|
|
58
|
+
const last = lines[lines.length - 1];
|
|
59
|
+
if (last.length >= 8) {
|
|
60
|
+
let same = 1;
|
|
61
|
+
for (let i = lines.length - 2; i >= 0 && lines[i] === last; i--)
|
|
62
|
+
same++;
|
|
63
|
+
if (same >= o.minRepeats) {
|
|
64
|
+
return {
|
|
65
|
+
looping: true,
|
|
66
|
+
trigger: `tail_repetition:${same}@thinking`,
|
|
67
|
+
cycle: last.slice(0, 120),
|
|
68
|
+
repeats: same,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// ── Char-cycle check over the normalized tail ──
|
|
74
|
+
const tail = normalize(text).slice(-o.tailWindow);
|
|
75
|
+
const n = tail.length;
|
|
76
|
+
const maxLen = Math.min(o.maxCycleLen, Math.floor(n / o.minRepeats));
|
|
77
|
+
for (let len = o.minCycleLen; len <= maxLen; len++) {
|
|
78
|
+
const unit = tail.slice(n - len, n);
|
|
79
|
+
if (!unit.trim())
|
|
80
|
+
continue;
|
|
81
|
+
let repeats = 1;
|
|
82
|
+
let pos = n - len;
|
|
83
|
+
while (pos - len >= 0 && tail.slice(pos - len, pos) === unit) {
|
|
84
|
+
repeats++;
|
|
85
|
+
pos -= len;
|
|
86
|
+
}
|
|
87
|
+
if (repeats >= o.minRepeats) {
|
|
88
|
+
return {
|
|
89
|
+
looping: true,
|
|
90
|
+
trigger: `tail_repetition:${repeats}@thinking`,
|
|
91
|
+
cycle: unit.trim().slice(0, 120),
|
|
92
|
+
repeats,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return { looping: false };
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=tailRepetitionDetector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tailRepetitionDetector.js","sourceRoot":"","sources":["../../src/orchestrator/tailRepetitionDetector.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAwBH;;;;;;;;GAQG;AACH,MAAM,UAAU,oBAAoB,CAAC,QAAiB;IACpD,IAAI,OAAO,CAAC,GAAG,CAAC,6BAA6B,KAAK,MAAM;QAAE,OAAO,IAAI,CAAC;IACtE,OAAO,OAAO,CAAC,GAAG,CAAC,mBAAmB,KAAK,MAAM,IAAI,QAAQ,KAAK,KAAK,CAAC;AAC1E,CAAC;AAED,MAAM,QAAQ,GAA0B;IACtC,UAAU,EAAE,IAAI;IAChB,WAAW,EAAE,EAAE;IACf,WAAW,EAAE,GAAG;IAChB,UAAU,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14,7 @@ export interface TodoItem {
|
|
|
14
14
|
/**
|
|
15
15
|
* System reminder types
|
|
16
16
|
*/
|
|
17
|
-
export type SystemReminderType = 'empty_todo' | 'todo_update' | 'claude_md' | 'tool_call' | 'tool_result' | 'file_security' | '
|
|
17
|
+
export type SystemReminderType = 'empty_todo' | 'todo_update' | 'claude_md' | 'tool_call' | 'tool_result' | 'file_security' | 'todo_reminder' | 'command_caveat';
|
|
18
18
|
/**
|
|
19
19
|
* System reminder content
|
|
20
20
|
*/
|
|
@@ -81,7 +81,7 @@ export declare class SystemReminderInjector {
|
|
|
81
81
|
createEmptyTodoReminder(): SystemReminder;
|
|
82
82
|
/**
|
|
83
83
|
* Pattern 2: Todo List Update Notification
|
|
84
|
-
* Frequency: After
|
|
84
|
+
* Frequency: After TodoCreate/TodoUpdate tool usage
|
|
85
85
|
*/
|
|
86
86
|
createTodoUpdateReminder(todos: TodoItem[]): SystemReminder;
|
|
87
87
|
/**
|
|
@@ -109,7 +109,7 @@ export declare class SystemReminderInjector {
|
|
|
109
109
|
* Pattern 7: Todo Tool Reminder
|
|
110
110
|
* Frequency: Periodic during long conversations
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
createTodoReminder(currentTodos: TodoItem[]): SystemReminder;
|
|
113
113
|
/**
|
|
114
114
|
* Pattern 8: Command Caveat Injection
|
|
115
115
|
* Frequency: Before command metadata in messages containing commands
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SystemReminderInjector.d.ts","sourceRoot":"","sources":["../../src/system-messages/SystemReminderInjector.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,SAAS,GAAG,aAAa,GAAG,WAAW,CAAC;IAChD,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAC1B,YAAY,GACZ,aAAa,GACb,WAAW,GACX,WAAW,GACX,aAAa,GACb,eAAe,GACf,
|
|
1
|
+
{"version":3,"file":"SystemReminderInjector.d.ts","sourceRoot":"","sources":["../../src/system-messages/SystemReminderInjector.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,SAAS,GAAG,aAAa,GAAG,WAAW,CAAC;IAChD,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAC1B,YAAY,GACZ,aAAa,GACb,WAAW,GACX,WAAW,GACX,aAAa,GACb,eAAe,GACf,eAAe,GACf,gBAAgB,CAAC;AAErB;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,kBAAkB,CAAC;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,qBAAa,sBAAsB;IACjC,OAAO,CAAC,aAAa,CAAuB;IAC5C,OAAO,CAAC,gBAAgB,CAAuB;IAE/C;;;;;OAKG;IACH,OAAO,CAAC,kBAAkB,CAA6D;IAEvF,qBAAqB,CAAC,EAAE,EAAE,MAAM;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE,EAAE,GAAG,IAAI;IAI3E;;;;;;;;;;;;;;;OAeG;IACH,sBAAsB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAmE1D;;;;;;OAMG;IACH,kCAAkC,IAAI,MAAM,GAAG,IAAI;IAwCnD;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IAO3B;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAI1B;;;OAGG;IACH,uBAAuB,IAAI,cAAc;IAUzC;;;OAGG;IACH,wBAAwB,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,cAAc;IAgB3D;;;OAGG;IACG,sBAAsB,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC;IA4BlF;;;OAGG;IACH,sBAAsB,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,cAAc;IAWxF;;;OAGG;IACH,wBAAwB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc;IAU1E;;;;OAIG;IACH,yBAAyB,IAAI,cAAc;IAU3C;;;OAGG;IACH,kBAAkB,CAAC,YAAY,EAAE,QAAQ,EAAE,GAAG,cAAc;IAe5D;;;OAGG;IACH,mBAAmB,IAAI,cAAc;IAUrC;;;OAGG;IACH,mCAAmC,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc;IAarF;;;OAGG;IACH,kBAAkB,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,OAAO;IAK9C;;;OAGG;IACH,kBAAkB,IAAI,IAAI;CAG3B;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,eAAe,GAAG,MAAM,CAalE"}
|
|
@@ -116,7 +116,7 @@ export class SystemReminderInjector {
|
|
|
116
116
|
'auto-research tool surface into your own context or run experiments here. Follow this workflow:\n' +
|
|
117
117
|
'\n' +
|
|
118
118
|
'1. PLAN FIRST — the harness BLOCKS the launch until you do (interactive TUI: draft the plan and get it\n' +
|
|
119
|
-
' approved in PLAN MODE via
|
|
119
|
+
' approved in PLAN MODE, presenting it via ExitPlanMode; headless: create a TodoCreate planning checklist). The plan covers:\n' +
|
|
120
120
|
' - Evaluate the backlog (ResearchBacklog list/next); triage the SINGLE highest-value deficiency.\n' +
|
|
121
121
|
' - Define a MEASURABLE experiment: the metric + how it is measured (an eval/command), the pass/fail\n' +
|
|
122
122
|
' threshold + verifier, the control (base ref vs candidate, train + a HELD-OUT set), and the\n' +
|
|
@@ -170,7 +170,7 @@ export class SystemReminderInjector {
|
|
|
170
170
|
}
|
|
171
171
|
/**
|
|
172
172
|
* Pattern 2: Todo List Update Notification
|
|
173
|
-
* Frequency: After
|
|
173
|
+
* Frequency: After TodoCreate/TodoUpdate tool usage
|
|
174
174
|
*/
|
|
175
175
|
createTodoUpdateReminder(todos) {
|
|
176
176
|
const todoJson = JSON.stringify(todos);
|
|
@@ -257,7 +257,7 @@ ${this.claudeMdCache}
|
|
|
257
257
|
* Pattern 7: Todo Tool Reminder
|
|
258
258
|
* Frequency: Periodic during long conversations
|
|
259
259
|
*/
|
|
260
|
-
|
|
260
|
+
createTodoReminder(currentTodos) {
|
|
261
261
|
const todoJson = JSON.stringify(currentTodos);
|
|
262
262
|
const content = `The task tools haven't been used recently. If you're working on tasks that would benefit from tracking progress, consider using TodoCreate to add new tasks and TodoUpdate to update task status (set to in_progress when starting, completed when done). Also consider cleaning up the task list if it has become stale. Only use these if relevant to the current work. This is just a gentle reminder - ignore if not applicable.
|
|
263
263
|
|
|
@@ -265,7 +265,7 @@ Here are the existing contents of your todo list:
|
|
|
265
265
|
|
|
266
266
|
${todoJson}`;
|
|
267
267
|
return {
|
|
268
|
-
type: '
|
|
268
|
+
type: 'todo_reminder',
|
|
269
269
|
content: this.wrapSystemReminder(content),
|
|
270
270
|
contentHash: this.generateContentHash(content)
|
|
271
271
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SystemReminderInjector.js","sourceRoot":"","sources":["../../src/system-messages/SystemReminderInjector.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,E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CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QAEvC,OAAO,CACL,+DAA+D;YAC/D,gEAAgE;YAChE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC;YACrB,mBAAmB,CACpB,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACH,kCAAkC;QAChC,MAAM,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QAC7E,IAAI,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,KAAK;YAAE,OAAO,IAAI,CAAC,CAAQ,0BAA0B;QACvF,IAAI,OAAO,CAAC,GAAG,CAAC,iBAAiB,KAAK,MAAM;YAAE,OAAO,IAAI,CAAC,CAAG,oCAAoC;QACjG,MAAM,IAAI,GAAG,IAAI,KAAK,KAAK;YACzB,CAAC,CAAC,kGAAkG;YACpG,CAAC,CAAC,6IAA6I,CAAC;QAClJ,OAAO,CACL,+DAA+D;YAC/D,sGAAsG;YACtG,yGAAyG;YACzG,mGAAmG;YACnG,IAAI;YACJ,0GAA0G;YAC1G,
|
|
1
|
+
{"version":3,"file":"SystemReminderInjector.js","sourceRoot":"","sources":["../../src/system-messages/SystemReminderInjector.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAiC7C;;GAEG;AACH,MAAM,OAAO,sBAAsB;IACzB,aAAa,GAAkB,IAAI,CAAC;IACpC,gBAAgB,GAAkB,IAAI,CAAC;IAE/C;;;;;OAKG;IACK,kBAAkB,GAAwD,IAAI,CAAC;IAEvF,qBAAqB,CAAC,EAA8C;QAClE,IAAI,CAAC,kBAAkB,GAAG,EAAE,CAAC;IAC/B,CAAC;IAED;;;;;;;;;;;;;;;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7
7
|
```
|
|
8
|
-
Step 1:
|
|
8
|
+
Step 1: Grep(pattern="class User", output_mode="content")
|
|
9
9
|
→ Result: "src/models/User.ts:10:export class User {"
|
|
10
10
|
|
|
11
|
-
Step 2:
|
|
11
|
+
Step 2: Read(file_path="src/models/User.ts")
|
|
12
12
|
→ Result: Full file content with exact indentation
|
|
13
13
|
|
|
14
|
-
Step 3:
|
|
14
|
+
Step 3: Edit(file_path="src/models/User.ts", old_string="<exact text from Read>", new_string="<modified text>")
|
|
15
15
|
→ Result: File updated
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
Key:
|
|
18
|
+
Key: Read before Edit. Copy exact text from Read output for old_string.
|
|
19
19
|
|
|
20
20
|
## Example 2: Parallel Tool Calls
|
|
21
21
|
|
|
@@ -23,7 +23,7 @@ Key: read before edit. Copy exact text from read output for old_string.
|
|
|
23
23
|
|
|
24
24
|
Output both tool calls in one response for parallel execution:
|
|
25
25
|
```
|
|
26
|
-
|
|
26
|
+
Read(file_path="/workspace/package.json") + Read(file_path="/workspace/tsconfig.json")
|
|
27
27
|
```
|
|
28
28
|
|
|
29
29
|
Both results arrive together. Respond with analysis of both files.
|
|
@@ -32,10 +32,10 @@ Both results arrive together. Respond with analysis of both files.
|
|
|
32
32
|
|
|
33
33
|
**Task**: Analyze both the authentication system and database layer.
|
|
34
34
|
|
|
35
|
-
Output both
|
|
35
|
+
Output both Task calls in one response:
|
|
36
36
|
```
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
Task(description="Analyze auth", subagent_type="explore", prompt="Search for authentication...")
|
|
38
|
+
Task(description="Analyze database", subagent_type="explore", prompt="Search for database...")
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
Both agents run concurrently. Results return together for synthesis.
|
|
@@ -46,28 +46,28 @@ Both agents run concurrently. Results return together for synthesis.
|
|
|
46
46
|
|
|
47
47
|
```
|
|
48
48
|
Step 1: Create tasks (one call per task)
|
|
49
|
-
|
|
49
|
+
TodoCreate({ content: "Search for auth module files", activeForm: "Searching for auth files" })
|
|
50
50
|
→ Created task #1
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
TodoCreate({ content: "Read and identify the bug", activeForm: "Reading auth code" })
|
|
53
53
|
→ Created task #2
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
TodoCreate({ content: "Fix the bug", activeForm: "Fixing auth bug" })
|
|
56
56
|
→ Created task #3
|
|
57
57
|
|
|
58
58
|
Step 2: Start first task
|
|
59
|
-
|
|
59
|
+
TodoUpdate({ taskId: "1", status: "in_progress" })
|
|
60
60
|
|
|
61
61
|
Step 3: Complete first, start second
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
TodoUpdate({ taskId: "1", status: "completed" })
|
|
63
|
+
TodoUpdate({ taskId: "2", status: "in_progress" })
|
|
64
64
|
|
|
65
65
|
Step 4: Complete second, start third
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
TodoUpdate({ taskId: "2", status: "completed" })
|
|
67
|
+
TodoUpdate({ taskId: "3", status: "in_progress" })
|
|
68
68
|
|
|
69
69
|
Step 5: Complete final task
|
|
70
|
-
|
|
70
|
+
TodoUpdate({ taskId: "3", status: "completed" })
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
-
Key: One task per
|
|
73
|
+
Key: One task per TodoCreate call. TodoUpdate sends only changed fields (no full list resend).
|
|
@@ -24,24 +24,26 @@ Do not claim or imply that you are Claude, GPT, Gemini, or any model other than
|
|
|
24
24
|
- Build on previous findings across turns. Reference earlier tool results instead of re-reading the same files.
|
|
25
25
|
- Respond concisely. Focus on what the user asked for.
|
|
26
26
|
|
|
27
|
-
**Do not repeat searches.** Before issuing a `
|
|
27
|
+
**Do not repeat searches.** Before issuing a `Grep` or `Glob`, check whether you already searched for that symbol or pattern this turn. If you did, the answer is already in your earlier tool results — re-read those results, do NOT run the search again with a reordered or slightly-varied pattern. Re-running the same search (e.g. greping `ensureHistoryFitsModel` a second time, or `A|B|C` then `C|B|A`) wastes a full round-trip and tells you nothing new. Each distinct symbol needs at most ONE content search. Once you have located the relevant files, switch to reading them — stop searching and synthesize your answer.
|
|
28
28
|
|
|
29
29
|
## Tool Selection
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
- **Read files** → use read, NOT `cat`, `head`, `tail`, or `sed`
|
|
33
|
-
- **Edit files** → use edit, NOT `sed -i`, `awk`, or `perl -pi`
|
|
34
|
-
- **Create/write files** → use write, NOT `echo >`, `cat <<EOF >`, or `tee`
|
|
35
|
-
- **Search file contents** → use grep, NOT `grep`, `rg`, or `ag`
|
|
36
|
-
- **Find files by name** → use glob, NOT `find` or `ls -R`
|
|
31
|
+
Tool names in these instructions are written in their canonical PascalCase form (Read, TodoCreate, WebSearch). They may appear in a different case in your tools array (e.g. `read`, `todo_create`, `web_search`) — same tools; call them by the names in your tools array.
|
|
37
32
|
|
|
38
|
-
|
|
33
|
+
Do NOT use Bash when a dedicated tool exists. Using dedicated tools produces better output and is CRITICAL:
|
|
34
|
+
- **Read files** → use Read, NOT `cat`, `head`, `tail`, or `sed`
|
|
35
|
+
- **Edit files** → use Edit, NOT `sed -i`, `awk`, or `perl -pi`
|
|
36
|
+
- **Create/write files** → use Write, NOT `echo >`, `cat <<EOF >`, or `tee`
|
|
37
|
+
- **Search file contents** → use Grep, NOT `grep`, `rg`, or `ag`
|
|
38
|
+
- **Find files by name** → use Glob, NOT `find` or `ls -R`
|
|
39
|
+
|
|
40
|
+
Reserve Bash exclusively for system commands: `git`, `npm`, `docker`, `curl`, builds, tests, process management — operations that have no dedicated tool equivalent.
|
|
39
41
|
|
|
40
42
|
**Tool guidance**:
|
|
41
|
-
- **
|
|
42
|
-
- **
|
|
43
|
-
- **
|
|
44
|
-
- **
|
|
45
|
-
- **
|
|
43
|
+
- **Read/Edit/Write**: For all file operations. Always Read before Edit to get exact content for string matching.
|
|
44
|
+
- **Grep**: Search file contents by regex. Use `output_mode: "files_with_matches"` first, then read specific files.
|
|
45
|
+
- **Glob**: Find files by name pattern. Use `**/*.ts` for recursive search.
|
|
46
|
+
- **Task**: Complex multi-step work with autonomous sub-agents. Batch independent Task calls in one response for parallel execution.
|
|
47
|
+
- **WebSearch/WebFetch**: Current information from the web.
|
|
46
48
|
|
|
47
49
|
When tools are not needed (factual questions, explanations from training data), answer directly without tool calls.
|