clementine-agent 1.18.34 → 1.18.35

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.
@@ -5390,7 +5390,24 @@ You have a cost budget per message — not a hard turn limit. Work until the tas
5390
5390
  }
5391
5391
  }
5392
5392
  catch { /* non-fatal — run without skills */ }
5393
+ // ── Sub-agent fan-out directive (Vision 2) ──────────────────────
5394
+ // Detect multi-item / broad-scope signals in the job spec and
5395
+ // prepend a hard-line fan-out mandate when found. This is what
5396
+ // keeps the parent context clean on long jobs: each slice of work
5397
+ // runs in an Agent sub-agent (its own context window, big tool
5398
+ // responses contained), and the parent only sees compact summaries.
5399
+ const { buildAlwaysOnParallelizationHint, buildFanoutDirectiveForText } = await import('./fanout-policy.js');
5400
+ const fanoutScope = `${jobName}\n${jobPrompt}\n${cronProfile?.description ?? ''}\n${cronProfile?.systemPromptBody ?? ''}`;
5401
+ const { directive: fanoutDirective, report: fanoutReport } = buildFanoutDirectiveForText(fanoutScope);
5402
+ if (fanoutReport.needsFanout) {
5403
+ logger.info({
5404
+ job: jobName,
5405
+ signals: fanoutReport.signals.map(s => s.pattern),
5406
+ }, 'Fanout policy: directive injected for cron job');
5407
+ }
5393
5408
  const prompt = `[Scheduled task: ${jobName}]\n\n` +
5409
+ (fanoutDirective ? fanoutDirective + '\n\n' : '') +
5410
+ buildAlwaysOnParallelizationHint() + '\n\n' +
5394
5411
  progressContext +
5395
5412
  goalContext +
5396
5413
  skillContext +
@@ -5782,22 +5799,30 @@ You have a cost budget per message — not a hard turn limit. Work until the tas
5782
5799
  }
5783
5800
  let prompt;
5784
5801
  if (phase === 1) {
5802
+ const { buildAlwaysOnParallelizationHint, buildFanoutDirectiveForText } = await import('./fanout-policy.js');
5803
+ const unleashedFanoutScope = `${jobName}\n${jobPrompt}\n${unleashedProfile?.description ?? ''}\n${unleashedProfile?.systemPromptBody ?? ''}`;
5804
+ const { directive: unleashedFanoutDirective, report: unleashedFanoutReport } = buildFanoutDirectiveForText(unleashedFanoutScope);
5805
+ if (unleashedFanoutReport.needsFanout) {
5806
+ logger.info({
5807
+ job: jobName,
5808
+ phase,
5809
+ signals: unleashedFanoutReport.signals.map(s => s.pattern),
5810
+ }, 'Fanout policy: directive injected for unleashed phase 1');
5811
+ }
5785
5812
  prompt =
5786
5813
  `[UNLEASHED TASK: ${jobName} — Phase ${phase} — ${timestamp}]\n\n` +
5787
5814
  `You are running in unleashed mode — a long-running autonomous task.\n` +
5788
5815
  `Time remaining: ${remainingHours} hours. You have ${turnsPerPhase} turns per phase.\n` +
5789
5816
  `After each phase completes, your session will be resumed with fresh context.\n\n` +
5817
+ (unleashedFanoutDirective ? unleashedFanoutDirective + '\n\n' : '') +
5818
+ buildAlwaysOnParallelizationHint() + '\n\n' +
5790
5819
  `TASK:\n${jobPrompt}\n\n` +
5791
5820
  unleashedSkillContext +
5792
5821
  `${unleashedContextSafety}\n\n` +
5793
5822
  `IMPORTANT:\n` +
5794
5823
  `- Work methodically through the task in phases\n` +
5795
5824
  `- At the end of this phase, output a STATUS SUMMARY of what you accomplished and what remains\n` +
5796
- `- Save important intermediate results to files so they persist across phases\n\n` +
5797
- `PARALLELIZATION: When processing multiple items (prospects, accounts, emails, analyses), ` +
5798
- `use the Agent tool to spawn sub-agents that work in parallel. For example, if you need to ` +
5799
- `research 10 prospects, spawn 3-5 sub-agents that each handle a batch — don't process them ` +
5800
- `one at a time. Each sub-agent should receive specific items and return structured results.`;
5825
+ `- Save important intermediate results to files so they persist across phases`;
5801
5826
  }
5802
5827
  else {
5803
5828
  // Phase 2+ — inject structured checkpoint from previous phase if available
@@ -5821,6 +5846,8 @@ You have a cost budget per message — not a hard turn limit. Work until the tas
5821
5846
  }
5822
5847
  }
5823
5848
  catch { /* fall back to no checkpoint */ }
5849
+ const { buildAlwaysOnParallelizationHint: hintFn } = await import('./fanout-policy.js');
5850
+ const phaseParallelHint = hintFn();
5824
5851
  if (sessionId) {
5825
5852
  // Resuming existing session — agent has conversation history + structured checkpoint
5826
5853
  prompt =
@@ -5829,6 +5856,7 @@ You have a cost budget per message — not a hard turn limit. Work until the tas
5829
5856
  `Time remaining: ${remainingHours} hours. You have ${turnsPerPhase} turns this phase.\n` +
5830
5857
  checkpointContext +
5831
5858
  `\n${unleashedContextSafety}\n` +
5859
+ `\n${phaseParallelHint}\n` +
5832
5860
  `\nContinue working on the task. Pick up where you left off.\n` +
5833
5861
  `If the task is COMPLETE, output "TASK_COMPLETE:" followed by a final summary.\n\n` +
5834
5862
  `IMPORTANT: Output a STATUS SUMMARY at the end of this phase.`;
@@ -5843,6 +5871,7 @@ You have a cost budget per message — not a hard turn limit. Work until the tas
5843
5871
  `TASK:\n${jobPrompt}\n` +
5844
5872
  checkpointContext +
5845
5873
  `\n${unleashedContextSafety}\n` +
5874
+ `\n${phaseParallelHint}\n` +
5846
5875
  `\nCheck any files or progress from prior phases, then continue the work.\n` +
5847
5876
  `If the task is COMPLETE, output "TASK_COMPLETE:" followed by a final summary.\n\n` +
5848
5877
  `IMPORTANT: Output a STATUS SUMMARY at the end of this phase.`;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Sub-agent fan-out policy for autonomous tasks.
3
+ *
4
+ * Why: even with a small tool surface, a single agent context can fill
5
+ * within a few turns when tool responses are large (Outlook list dumps,
6
+ * web search results, file reads, multi-prospect research). The SDK's
7
+ * autocompact then has nothing to compact and aborts with
8
+ * `rapid_refill_breaker`. The fix matching how Claude Code is designed:
9
+ * spawn sub-agents that each handle a slice of work in their own
10
+ * isolated context and return only a compact summary back to the parent.
11
+ *
12
+ * The Agent tool already exists in the SDK. The problem is timing —
13
+ * agents tend to discover the need for fan-out only after thrashing.
14
+ * This module front-loads the directive: scan the task description for
15
+ * signals that fan-out will be needed, and inject a strong, explicit
16
+ * mandate at the top of the prompt.
17
+ *
18
+ * Two outputs:
19
+ * - buildAlwaysOnParallelizationHint()
20
+ * Short reminder injected into every autonomous prompt. Cheap.
21
+ * - buildFanoutDirective(detectFanoutSignals(text).signals)
22
+ * Stronger, explicit fan-out contract. Only injected when signals
23
+ * indicate the task is genuinely multi-item or broad-scope.
24
+ */
25
+ export interface FanoutSignal {
26
+ /** Why fan-out matters for this task. Surfaced in the directive. */
27
+ reason: string;
28
+ /** The pattern that matched. Used for telemetry. */
29
+ pattern: string;
30
+ }
31
+ export interface FanoutSignalReport {
32
+ needsFanout: boolean;
33
+ signals: FanoutSignal[];
34
+ }
35
+ /**
36
+ * Detect patterns that strongly predict fan-out is needed. Conservative
37
+ * by design — false positives waste a few hundred tokens per turn; false
38
+ * negatives let the agent thrash. Tune for false positives.
39
+ */
40
+ export declare function detectFanoutSignals(text: string): FanoutSignalReport;
41
+ /**
42
+ * Always-on parallelization reminder. Short, designed to ride along in
43
+ * every autonomous prompt without inflating token cost.
44
+ */
45
+ export declare function buildAlwaysOnParallelizationHint(): string;
46
+ /**
47
+ * Strong fan-out contract injected when detector matches. Designed to be
48
+ * unambiguous: failing to fan out on these patterns *will* crash the run.
49
+ */
50
+ export declare function buildFanoutDirective(signals: FanoutSignal[]): string;
51
+ /**
52
+ * Convenience: detect signals and return the directive string in one
53
+ * call. Returns empty string when no fan-out is indicated.
54
+ */
55
+ export declare function buildFanoutDirectiveForText(text: string): {
56
+ directive: string;
57
+ report: FanoutSignalReport;
58
+ };
59
+ //# sourceMappingURL=fanout-policy.d.ts.map
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Sub-agent fan-out policy for autonomous tasks.
3
+ *
4
+ * Why: even with a small tool surface, a single agent context can fill
5
+ * within a few turns when tool responses are large (Outlook list dumps,
6
+ * web search results, file reads, multi-prospect research). The SDK's
7
+ * autocompact then has nothing to compact and aborts with
8
+ * `rapid_refill_breaker`. The fix matching how Claude Code is designed:
9
+ * spawn sub-agents that each handle a slice of work in their own
10
+ * isolated context and return only a compact summary back to the parent.
11
+ *
12
+ * The Agent tool already exists in the SDK. The problem is timing —
13
+ * agents tend to discover the need for fan-out only after thrashing.
14
+ * This module front-loads the directive: scan the task description for
15
+ * signals that fan-out will be needed, and inject a strong, explicit
16
+ * mandate at the top of the prompt.
17
+ *
18
+ * Two outputs:
19
+ * - buildAlwaysOnParallelizationHint()
20
+ * Short reminder injected into every autonomous prompt. Cheap.
21
+ * - buildFanoutDirective(detectFanoutSignals(text).signals)
22
+ * Stronger, explicit fan-out contract. Only injected when signals
23
+ * indicate the task is genuinely multi-item or broad-scope.
24
+ */
25
+ /**
26
+ * Detect patterns that strongly predict fan-out is needed. Conservative
27
+ * by design — false positives waste a few hundred tokens per turn; false
28
+ * negatives let the agent thrash. Tune for false positives.
29
+ */
30
+ export function detectFanoutSignals(text) {
31
+ const signals = [];
32
+ const lower = text.toLowerCase();
33
+ const checks = [
34
+ {
35
+ pattern: 'multi_item_iteration',
36
+ re: /\b(for each|for every|process each|iterate over|loop through|across all|across each)\b/,
37
+ reason: 'task explicitly iterates over multiple items — process them in parallel sub-agents, not one at a time in this conversation',
38
+ },
39
+ {
40
+ pattern: 'collective_with_quantifier',
41
+ re: /\b(all|every|each)\s+(prospects?|accounts?|leads?|contacts?|customers?|deals?|emails?|messages?|threads?|files?|records?|rows?|tasks?|items?|results?|pages?|articles?|posts?|repos?|repositories|projects?)\b/,
42
+ reason: 'task spans every item in a collection — fan out by batching items across sub-agents',
43
+ },
44
+ {
45
+ pattern: 'numeric_collection',
46
+ re: /\b\d{2,}\s+(prospects?|accounts?|leads?|contacts?|customers?|deals?|emails?|messages?|threads?|files?|records?|rows?|items?|results?|pages?|articles?)\b/,
47
+ reason: 'task names a numeric count of items (10+) — split into batches of 3-5 per sub-agent',
48
+ },
49
+ {
50
+ pattern: 'comprehensive_research',
51
+ re: /\b(comprehensive|exhaustive|deep[- ]dive|deep dive|full audit|competitive intel|market map|content intel|brief|landscape|panorama)\b/,
52
+ reason: 'broad-scope research task — each step (news, search, brand, competitor, social) should run in its own sub-agent so the parent context stays clean',
53
+ },
54
+ {
55
+ pattern: 'broad_scan_or_crawl',
56
+ re: /\b(scan all|crawl|backfill|inventory|migrate|refactor)\b.{0,80}\b(all|entire|every|full)\b/s,
57
+ reason: 'broad scan/crawl — partition by directory, date range, or ID range and fan out per partition',
58
+ },
59
+ {
60
+ pattern: 'long_history_pull',
61
+ re: /\b(last|past)\s+\d+\s+(days|weeks|months)|\bsince\s+(yesterday|last week|last month)\b/,
62
+ reason: 'pulling a history range that is likely to return many items — sub-agents per day/week chunk',
63
+ },
64
+ {
65
+ pattern: 'multiple_steps',
66
+ re: /\b(steps?|phases?|stages?)\s*[:0-9]/,
67
+ reason: 'task has explicit multi-step structure — each step in its own sub-agent, parent only sees the step summaries',
68
+ },
69
+ ];
70
+ for (const check of checks) {
71
+ if (check.re.test(lower)) {
72
+ signals.push({ pattern: check.pattern, reason: check.reason });
73
+ }
74
+ }
75
+ return {
76
+ needsFanout: signals.length > 0,
77
+ signals,
78
+ };
79
+ }
80
+ /**
81
+ * Always-on parallelization reminder. Short, designed to ride along in
82
+ * every autonomous prompt without inflating token cost.
83
+ */
84
+ export function buildAlwaysOnParallelizationHint() {
85
+ return [
86
+ '## Sub-agent fan-out',
87
+ 'When you process multiple items, spawn ONE Agent sub-agent per batch of 3–5 items. Sub-agents return ONE-LINE summaries (no raw tool output). Do not iterate sequentially in this conversation — that fills your context and aborts the run.',
88
+ ].join('\n');
89
+ }
90
+ /**
91
+ * Strong fan-out contract injected when detector matches. Designed to be
92
+ * unambiguous: failing to fan out on these patterns *will* crash the run.
93
+ */
94
+ export function buildFanoutDirective(signals) {
95
+ if (signals.length === 0)
96
+ return '';
97
+ const reasonLines = signals
98
+ .map((s, i) => `${i + 1}. ${s.reason}`)
99
+ .join('\n');
100
+ return [
101
+ '## Sub-agent fan-out is MANDATORY for this task',
102
+ '',
103
+ 'Preflight detected patterns that will fill the context window if you run them sequentially in this conversation:',
104
+ '',
105
+ reasonLines,
106
+ '',
107
+ '### Required pattern',
108
+ '',
109
+ 'Use the `Agent` tool to spawn parallel sub-agents. Each sub-agent runs in its own isolated context, so big tool responses live and die there — your context only sees the summary.',
110
+ '',
111
+ '- **Batch size**: 3–5 items per sub-agent (or one slice of work per sub-agent for research tasks)',
112
+ '- **Sub-agent prompt MUST include**: the narrow task, the exact return format (e.g. `Return ONE LINE: <id> | <status> | <next-action>`), and an explicit "do not include raw tool output" directive',
113
+ '- **Parent context keeps**: only the sub-agent return strings, not their tool transcripts',
114
+ '',
115
+ 'If you anticipate a single tool call returning more than ~5 KB of text (full email lists, web search result pages, large database queries, file dumps), wrap THAT call in an Agent invocation too. The sub-agent runs the tool, extracts only the fields you need, and returns those.',
116
+ '',
117
+ 'Failing to fan out on this task will cause the SDK to abort with `rapid_refill_breaker` and the run will be lost.',
118
+ ].join('\n');
119
+ }
120
+ /**
121
+ * Convenience: detect signals and return the directive string in one
122
+ * call. Returns empty string when no fan-out is indicated.
123
+ */
124
+ export function buildFanoutDirectiveForText(text) {
125
+ const report = detectFanoutSignals(text);
126
+ return {
127
+ directive: buildFanoutDirective(report.signals),
128
+ report,
129
+ };
130
+ }
131
+ //# sourceMappingURL=fanout-policy.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clementine-agent",
3
- "version": "1.18.34",
3
+ "version": "1.18.35",
4
4
  "description": "Clementine — Personal AI Assistant (TypeScript)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",