Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

feat(core): context window v2, per-turn context block, faster step feedback, provider fixes, /new

Salman Qureshi82c067382acc
diff --git a/.gitignore b/.gitignore
index 1a49f7db..c009508b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,4 +10,6 @@ website/static/install.ps1
*.tsbuildinfo
config/
soul/
-memory/
\ No newline at end of file
+memory/
+# Claude Code worktrees / local state
+.claude/
diff --git a/src/channels/telegram.ts b/src/channels/telegram.ts
index 545d3845..fde122db 100644
--- a/src/channels/telegram.ts
+++ b/src/channels/telegram.ts
@@ -296,6 +296,7 @@ export class TelegramChannel extends BaseChannel {
{ command: 'spotify', description: 'Spotify playback controls' },
{ command: 'skills', description: 'Browse and install skills from the registry' },
{ command: 'unpair', description: 'Reset all Telegram access (admin only)' },
+ { command: 'new', description: 'Start a fresh conversation (memory is kept)' },
];
try {
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 49fece53..968f4462 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -85,6 +85,7 @@ import { compactConversation, memoryGovernorThresholds, memoryGovernorVerdict }
import { classifyStreamCompletion, isLengthTruncation, truncationContinuationPrompt, toolTruncationContinuationPrompt } from './stream-completion.js';
import { MAX_EXECUTE_CONTINUATIONS, MAX_VERIFICATION_CONTINUATIONS, executeContinuationPrompt, shouldForceExecuteContinuation, isFailedToolResult, shouldRequireVerification, isVerificationOutputOk, verificationPrompt, responseAsksUser, EXECUTE_MUTATING_TOOLS, VERIFICATION_COMMAND_PATTERN, wakeUpPrompt } from './execute-guard.js';
import { classifyTurnEnd, stepsExhaustedPrompt, STEPS_PAUSED_BANNER, WORK_NOT_STARTED_BANNER, VERIFICATION_FAILED_BANNER, type LoopEndCause } from './completion-verdict.js';
+import { HISTORY_TOKEN_BUDGET, HISTORY_TOKEN_BUDGET_SAVER, TOOL_TRACE_KIND, selectHistoryWindow, toModelMessage, withReasoningParts, formatToolTrace, formatToolTraceLine, summarizeToolArgs, summarizeToolResult } from './context-window.js';
import { buildStatusVerbPrompt, parseStatusVerbs, shouldRefreshStatusVerbs } from './status-verbs.js';
import { verbPoolFor } from '../ui/status-word.js';
import { StallWatchdog } from './stall-watchdog.js';
@@ -2430,6 +2431,8 @@ export class Agent {
private async runInlineContinuationRound(opts: {
messages: unknown[];
systemPrompt: string;
+ /** Per-turn context block (memory, loop warnings); see withCachedSystem. */
+ volatileSystem?: string;
provider: any;
maxOutputTokens: number;
maxSteps: number;
@@ -2444,7 +2447,7 @@ export class Agent {
const deadlineAt = Date.now() + MAX_PROVIDER_ATTEMPT_MS;
const stream = streamText({
model: opts.provider.getModelInstance(),
- messages: this.withCachedSystem(opts.systemPrompt, opts.messages),
+ messages: this.withCachedSystem(opts.systemPrompt, opts.messages, opts.volatileSystem),
tools: this.capabilities.getTools(),
maxOutputTokens: opts.maxOutputTokens,
stopWhen: stepCountIs(opts.maxSteps),
@@ -3054,16 +3057,27 @@ export class Agent {
externalMessageId: msg.id,
metadata: { channelType: msg.channelType, channelId: msg.channelId, ...(requestId ? { requestId } : {}) },
});
+ // One read of the session per turn (it used to be re-read three times).
+ const activeSession = this.sessions.get(canonicalSession.id);
if (msg.channelType === 'cli') {
- const activeSession = this.sessions.get(canonicalSession.id);
const cliChannel = this.channels.get('cli');
if (cliChannel instanceof CLIChannel) cliChannel.setCurrentSession(activeSession);
}
- const recentMemory = this.sessions.get(canonicalSession.id).messages
- .filter((entry) => entry.kind === 'message' && entry.role !== 'tool')
- .slice(-this.saverMode.adjustHistoryWindow(10));
+ // Context window v2: history is budgeted by tokens rather than a fixed
+ // ten messages, and includes the compact tool trace written at the end
+ // of each turn, so this turn remembers what the previous one read and
+ // wrote instead of starting blind.
+ const recentMemory = selectHistoryWindow(
+ activeSession.messages,
+ this.saverMode.isActive() ? HISTORY_TOKEN_BUDGET_SAVER : HISTORY_TOKEN_BUDGET,
+ );
const messages: any[] = [];
+ // Per-turn context (retrieved memory, shared-pool hits, loop warnings)
+ // goes into a second, volatile system block instead of being staged as
+ // fake user/assistant dialogue: the fake "Noted. I'll keep this in
+ // mind." turns primed terse acknowledgements and polluted history.
+ const contextBlocks: string[] = [];
const recentSteps = recentMemory.slice(-6);
let loopWarning: string | null = null;
@@ -3102,27 +3116,25 @@ export class Agent {
}
if (loopWarning) {
- messages.push({ role: 'user', content: loopWarning });
- messages.push({ role: 'assistant', content: 'Acknowledged. I will stop repeating and respond differently, or clearly state if the task cannot be completed.' });
+ contextBlocks.push(loopWarning);
}
if (this.userMemory) {
const memoryContext = this.userMemory.retrieveRelevant(msg.content, { maxRecords: 5, maxChars: 900 });
if (memoryContext.context) {
- messages.push({
- role: 'user',
- content: `[Second Brain — auto-retrieved context]\n${memoryContext.context}\n[End auto-retrieved context]`,
- });
- messages.push({ role: 'assistant', content: 'Noted. I\'ll keep this in mind.' });
+ contextBlocks.push(`Second Brain — memory retrieved for this message:\n${memoryContext.context}`);
}
// Local-first, then pool: query the cloud SharedMemoryPool for additional
// context the second brain didn't surface. Gated by config, fails open
// (any error → proceed with local-only context). 5-min cache per query.
+ // Skipped when local recall was already strong: the pool call is an
+ // awaited network round-trip on the path to the first token.
const ck = this.config.memory.collaborativeKnowledge;
const cloud = this.config.cloud;
const tokenStore = getCloudTokenStore();
- if (ck?.poolSearch !== false && cloud?.enabled && cloud?.jwt && cloud?.apiUrl) {
+ const localRecallIsStrong = memoryContext.records.length >= 3;
+ if (ck?.poolSearch !== false && cloud?.enabled && cloud?.jwt && cloud?.apiUrl && !localRecallIsStrong) {
try {
const { searchPool, formatPoolContextBlock, dedupeAgainstLocal } = await import('../cloud/pool-search.js');
const localSummaries = memoryContext.records.map((r) => r.summary);
@@ -3148,10 +3160,7 @@ export class Agent {
);
const deduped = dedupeAgainstLocal(poolHits, localSummaries);
const poolBlock = formatPoolContextBlock(deduped, 1500);
- if (poolBlock) {
- messages.push({ role: 'user', content: poolBlock });
- messages.push({ role: 'assistant', content: 'Noted. I\'ll use this shared context.' });
- }
+ if (poolBlock) contextBlocks.push(poolBlock);
} catch (err) {
logger.debug({ err: (err as Error).message }, 'pool search failed (fail-open)');
}
@@ -3159,25 +3168,18 @@ export class Agent {
} else {
const relevantFacts = this.longTerm.search(msg.content, 3);
if (relevantFacts.length > 0) {
- messages.push({
- role: 'user',
- content: 'Relevant facts from memory:\n' + relevantFacts.map(f => `- ${f.fact}`).join('\n'),
- });
- messages.push({ role: 'assistant', content: 'Noted. I\'ll use these facts.' });
+ contextBlocks.push('Relevant facts from memory:\n' + relevantFacts.map(f => `- ${f.fact}`).join('\n'));
}
}
- if (recentMemory.length > 0) {
- for (const m of recentMemory) {
- messages.push({
- role: m.role,
- content: m.content,
- });
- }
- }
+ for (const m of recentMemory) messages.push(toModelMessage(m));
if (!canonicalSessionId) messages.push({ role: 'user', content: msg.content });
+ const volatileSystem = contextBlocks.length > 0
+ ? `## Context for this turn\n\n${contextBlocks.join('\n\n')}`
+ : '';
+
// ── Skill Intent Routing & Batch Execution ──
//
// Routing strategy:
@@ -3356,6 +3358,22 @@ export class Agent {
};
const verifiedAfterLastMutation = (): boolean =>
lastVerification != null && lastVerification.ok && lastVerification.step >= lastMutationStep;
+ // Compact per-turn tool trace, written to the session at turn end so
+ // the next turn's history carries what was read and written.
+ const turnToolTrace: string[] = [];
+ const noteToolSteps = (toolCalls: readonly any[] | undefined, toolResults: readonly any[] | undefined): void => {
+ if (!toolCalls || !toolResults) return;
+ for (let i = 0; i < toolCalls.length; i++) {
+ const tc = toolCalls[i];
+ const raw = (toolResults[i] as any)?.result ?? toolResults[i];
+ const text = typeof raw === 'string' ? raw : JSON.stringify(raw ?? '');
+ turnToolTrace.push(formatToolTraceLine({
+ tool: tc.toolName,
+ args: summarizeToolArgs(tc.input),
+ outcome: summarizeToolResult(raw, !isFailedToolResult(text)),
+ }));
+ }
+ };
let lastStepHadToolCalls = false;
let lastRoundSteps = 0;
let stepBudgetContinuations = 0;
@@ -3433,6 +3451,13 @@ export class Agent {
const deepseekProviderOptions = provider instanceof DeepSeekProvider && provider.isReasoner
? { deepseek: { thinking: { type: 'enabled' as const } } }
: undefined;
+ // DeepSeek thinking models require prior reasoning_content to be
+ // passed back with assistant turns (issue #24); other providers
+ // get plain text.
+ const providerMessages = deepseekProviderOptions ? withReasoningParts(messages) : messages;
+ // Per-provider output ceiling: a 32K request is rejected outright by
+ // providers with smaller limits and burned a halve-and-retry round.
+ const providerMaxOutputTokens = Math.min(effectiveMaxOutputTokens, provider.getMaxOutputTokens() ?? Infinity);
logger.info({ provider: provider.name, model: provider.getModel(), steps: MAX_STEPS, stream: canStream }, 'Generating agentic response');
@@ -3446,9 +3471,9 @@ export class Agent {
let streamAborted = false;
const streamResult = streamText({
model: provider.getModelInstance(),
- messages: this.withCachedSystem(systemPrompt, messages),
+ messages: this.withCachedSystem(systemPrompt, providerMessages, volatileSystem),
tools: this.programmingMode.isPlan() ? this.capabilities.getPlanTools() : this.capabilities.getTools(),
- maxOutputTokens: effectiveMaxOutputTokens,
+ maxOutputTokens: providerMaxOutputTokens,
stopWhen: stepCountIs(effectiveMaxSteps),
abortSignal: loopAbortController.signal,
// Memory: the SDK retains a structuredClone of the whole
@@ -3687,7 +3712,7 @@ export class Agent {
const tr = toolResults[i] as any;
const tcName = toolCalls[i]?.toolName as string | undefined;
if (tcName) {
- await tgCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
+ void tgCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
}
}
}
@@ -3701,7 +3726,7 @@ export class Agent {
const tr = toolResults[i] as any;
const tcName = toolCalls[i]?.toolName as string | undefined;
if (tcName) {
- await sigCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
+ void sigCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
}
}
}
@@ -3729,7 +3754,7 @@ export class Agent {
const tr = toolResults[i] as any;
const tcName = toolCalls[i]?.toolName as string | undefined;
if (tcName) {
- await dcCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
+ void dcCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
}
}
}
@@ -3743,12 +3768,12 @@ export class Agent {
const tr = toolResults[i] as any;
const tcName = toolCalls[i]?.toolName as string | undefined;
if (tcName) {
- await slCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId);
+ void Promise.resolve(slCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId)).catch((e) => logger.warn({ e }, 'channel send failed'));
}
}
}
} else {
- await channel.send(` [Using: ${names}]`, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
+ void channel.send(` [Using: ${names}]`, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
}
this.markProgress();
}
@@ -3853,12 +3878,12 @@ export class Agent {
Promise.resolve(streamText({
model: provider.getModelInstance(),
messages: this.withCachedSystem(systemPrompt, [
- ...messages,
+ ...providerMessages,
{ role: 'assistant', content: continuationText },
{ role: 'user', content: lastStepHadToolCalls
? toolTruncationContinuationPrompt(msg.content)
: truncationContinuationPrompt(msg.content) },
- ]),
+ ], volatileSystem),
tools: this.capabilities.getTools(),
maxOutputTokens: effectiveMaxOutputTokens,
// FULL budget: a one-step round can only read files — the
@@ -3900,9 +3925,9 @@ export class Agent {
} else {
result = await this.withProviderDeadline(generateText({
model: provider.getModelInstance(),
- messages: this.withCachedSystem(systemPrompt, messages),
+ messages: this.withCachedSystem(systemPrompt, providerMessages, volatileSystem),
tools: this.programmingMode.isPlan() ? this.capabilities.getPlanTools() : this.capabilities.getTools(),
- maxOutputTokens: effectiveMaxOutputTokens,
+ maxOutputTokens: providerMaxOutputTokens,
stopWhen: stepCountIs(effectiveMaxSteps),
abortSignal: loopAbortController.signal,
// Same O(N²) step retention as streamText (see comment above).
@@ -4126,7 +4151,7 @@ export class Agent {
const tr = toolResults[i] as any;
const tcName = toolCalls[i]?.toolName as string | undefined;
if (tcName) {
- await tgCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
+ void tgCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
}
}
}
@@ -4140,7 +4165,7 @@ export class Agent {
const tr = toolResults[i] as any;
const tcName = toolCalls[i]?.toolName as string | undefined;
if (tcName) {
- await sigCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
+ void sigCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
}
}
}
@@ -4168,7 +4193,7 @@ export class Agent {
const tr = toolResults[i] as any;
const tcName = toolCalls[i]?.toolName as string | undefined;
if (tcName) {
- await dcCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
+ void dcCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
}
}
}
@@ -4182,12 +4207,12 @@ export class Agent {
const tr = toolResults[i] as any;
const tcName = toolCalls[i]?.toolName as string | undefined;
if (tcName) {
- await slCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId);
+ void Promise.resolve(slCh.sendStepDone(tcName, tr.result ?? tr, msg.channelId)).catch((e) => logger.warn({ e }, 'channel send failed'));
}
}
}
} else {
- await channel.send(` [Using: ${names}]`, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
+ void channel.send(` [Using: ${names}]`, msg.channelId).catch((e) => logger.warn({ e }, 'channel send failed'));
}
this.markProgress();
}
@@ -4541,7 +4566,7 @@ export class Agent {
const guardDeadlineAt = Date.now() + MAX_PROVIDER_ATTEMPT_MS;
const guardStream = streamText({
model: guardProvider.getModelInstance(),
- messages: this.withCachedSystem(systemPrompt, messages),
+ messages: this.withCachedSystem(systemPrompt, messages, volatileSystem),
tools: this.capabilities.getTools(),
maxOutputTokens: effectiveMaxOutputTokens,
stopWhen: stepCountIs(effectiveMaxSteps),
@@ -4576,6 +4601,7 @@ export class Agent {
if (cliChGen instanceof CLIChannel) cliChGen.bumpLiveActivitySteps();
if (toolCalls && toolResults && toolCalls.length > 0) {
hasCompletedTool = true;
+ noteToolSteps(toolCalls, toolResults);
for (let i = 0; i < toolCalls.length; i++) {
const tc = toolCalls[i];
executeTurnToolsUsed.add(tc.toolName);
@@ -4697,6 +4723,7 @@ export class Agent {
const round = await this.runInlineContinuationRound({
messages,
systemPrompt,
+ volatileSystem,
provider: resumeProvider,
maxOutputTokens: effectiveMaxOutputTokens,
maxSteps: effectiveMaxSteps,
@@ -4711,6 +4738,7 @@ export class Agent {
if (cliChResume instanceof CLIChannel) cliChResume.bumpLiveActivitySteps();
if (toolCalls && toolResults && toolCalls.length > 0) {
hasCompletedTool = true;
+ noteToolSteps(toolCalls, toolResults);
for (let i = 0; i < toolCalls.length; i++) {
const tc = toolCalls[i];
executeTurnToolsUsed.add(tc.toolName);
@@ -4784,6 +4812,7 @@ export class Agent {
const round = await this.runInlineContinuationRound({
messages,
systemPrompt,
+ volatileSystem,
provider: verifyProvider,
maxOutputTokens: effectiveMaxOutputTokens,
maxSteps: effectiveMaxSteps,
@@ -4800,6 +4829,7 @@ export class Agent {
if (cliChVerify instanceof CLIChannel) cliChVerify.bumpLiveActivitySteps();
if (toolCalls && toolResults && toolCalls.length > 0) {
hasCompletedTool = true;
+ noteToolSteps(toolCalls, toolResults);
for (let i = 0; i < toolCalls.length; i++) {
const tc = toolCalls[i];
executeTurnToolsUsed.add(tc.toolName);
@@ -4941,6 +4971,17 @@ export class Agent {
channelType: msg.channelType,
agentId: this.config.cloud.agentId || undefined,
});
+ logger.info({
+ provider: usedProvider?.name,
+ model: usedProvider?.model,
+ inputTokens: result.usage?.inputTokens ?? 0,
+ cachedInputTokens: result.usage?.cachedInputTokens ?? 0,
+ outputTokens: result.usage?.outputTokens ?? 0,
+ steps: this.completedStepCount,
+ toolCalls: turnToolTrace.length,
+ historyEntries: recentMemory.length,
+ elapsedMs: Date.now() - startTime,
+ }, 'Turn usage');
this.syncTokenInfoToCli();
// Estimate tokens saved by Saver Mode (cap headroom + history trim).
@@ -4960,6 +5001,15 @@ export class Agent {
}
if (canonicalSessionId) {
+ if (turnToolTrace.length > 0) {
+ this.sessions.appendMessage(canonicalSessionId, {
+ role: 'assistant',
+ kind: TOOL_TRACE_KIND,
+ content: formatToolTrace(turnToolTrace),
+ externalMessageId: `${msg.id}:tools`,
+ metadata: { channelType: msg.channelType, channelId: msg.channelId, toolCalls: turnToolTrace.length },
+ });
+ }
this.sessions.appendMessage(canonicalSessionId, {
role: 'assistant',
content: finalText,
@@ -5232,13 +5282,16 @@ export class Agent {
* and turns; providers that do not know the option ignore it, and the
* AI SDK treats a leading system message exactly like `system:`.
*/
- private withCachedSystem(systemPrompt: string, messages: unknown[]): any[] {
+ private withCachedSystem(systemPrompt: string, messages: unknown[], volatileSystem = ''): any[] {
return [
{
role: 'system',
content: systemPrompt,
providerOptions: { anthropic: { cacheControl: { type: 'ephemeral' } } },
},
+ // Per-turn context lives in its own block AFTER the cache marker so it
+ // never invalidates the stable prefix.
+ ...(volatileSystem ? [{ role: 'system', content: volatileSystem }] : []),
...messages,
];
}
@@ -6140,6 +6193,13 @@ Is this productive iteration or a stuck loop?`,
return true;
}
+ // /new — fresh conversation context for this chat; memory is kept.
+ // Unlike /reset this never halts agents, clears queues, or wipes state.
+ if (cmd === '/new') {
+ await this.handleSessionCommand('/session new', channelType as ChannelType, channelId);
+ return true;
+ }
+
if (cmd === '/help') {
const helpText = channelType === 'telegram' ? getTelegramHelp() : channelType === 'discord' ? getDiscordHelp() : channelType === 'slack' ? getSlackHelp() : ctx.manual();
await channel.send(helpText, channelId);
diff --git a/src/core/context-window.test.ts b/src/core/context-window.test.ts
new file mode 100644
index 00000000..fe71d543
--- /dev/null
+++ b/src/core/context-window.test.ts
@@ -0,0 +1,107 @@
+import { describe, expect, it } from 'vitest';
+import type { SessionMessage } from '../sessions/types.js';
+import {
+ HISTORY_MIN_ENTRIES,
+ TOOL_TRACE_KIND,
+ formatToolTrace,
+ formatToolTraceLine,
+ selectHistoryWindow,
+ summarizeToolArgs,
+ summarizeToolResult,
+ toModelMessage,
+ withReasoningParts,
+} from './context-window.js';
+
+let seq = 0;
+const entry = (role: SessionMessage['role'], content: string, extra: Partial<SessionMessage> = {}): SessionMessage => ({
+ id: `m${++seq}`,
+ sessionId: 's',
+ role,
+ kind: 'message',
+ content,
+ timestamp: seq,
+ sequence: seq,
+ ...extra,
+});
+
+describe('selectHistoryWindow', () => {
+ it('keeps the newest entries under the token budget, oldest first', () => {
+ const msgs = [
+ entry('user', 'a'.repeat(400)),
+ entry('assistant', 'b'.repeat(400)),
+ entry('user', 'c'.repeat(400)),
+ entry('assistant', 'd'.repeat(400)),
+ ];
+ const picked = selectHistoryWindow(msgs, 250);
+ expect(picked.map((m) => m.content[0])).toEqual(['c', 'd']);
+ });
+
+ it('always keeps the last exchange even over budget', () => {
+ const msgs = [entry('user', 'x'.repeat(4000)), entry('assistant', 'y'.repeat(4000))];
+ expect(selectHistoryWindow(msgs, 10)).toHaveLength(HISTORY_MIN_ENTRIES);
+ });
+
+ it('includes tool-trace entries and excludes other kinds', () => {
+ const msgs = [
+ entry('user', 'read the config'),
+ entry('assistant', '[Tool activity]\n- read_file path=x → ok', { kind: TOOL_TRACE_KIND }),
+ entry('assistant', 'progress…', { kind: 'progress' }),
+ entry('tool', 'raw tool output', { kind: 'tool-result' }),
+ entry('assistant', 'Here is the config.'),
+ ];
+ const picked = selectHistoryWindow(msgs, 10_000);
+ expect(picked.map((m) => m.kind)).toEqual(['message', TOOL_TRACE_KIND, 'message']);
+ });
+
+ it('respects the entry cap', () => {
+ const msgs = Array.from({ length: 100 }, (_, i) => entry(i % 2 ? 'assistant' : 'user', `m${i}`));
+ expect(selectHistoryWindow(msgs, 1_000_000, 10)).toHaveLength(10);
+ });
+});
+
+describe('reasoning parts', () => {
+ it('attaches stored reasoning as parts only for assistant messages that have it', () => {
+ const msgs = [
+ toModelMessage(entry('user', 'hi')),
+ toModelMessage(entry('assistant', 'hello', { reasoning: 'the user greeted me' })),
+ toModelMessage(entry('assistant', 'plain')),
+ ];
+ expect(msgs.map((m) => ({ ...m }))).toEqual([
+ { role: 'user', content: 'hi' },
+ { role: 'assistant', content: 'hello' },
+ { role: 'assistant', content: 'plain' },
+ ]);
+ const withParts = withReasoningParts(msgs) as any[];
+ expect(withParts[0]).toEqual({ role: 'user', content: 'hi' });
+ expect(withParts[1]).toEqual({
+ role: 'assistant',
+ content: [
+ { type: 'reasoning', text: 'the user greeted me' },
+ { type: 'text', text: 'hello' },
+ ],
+ });
+ expect(withParts[2]).toEqual({ role: 'assistant', content: 'plain' });
+ });
+});
+
+describe('tool trace formatting', () => {
+ it('summarises args by the most useful field and results by outcome', () => {
+ expect(summarizeToolArgs({ path: 'src/app.ts', encoding: 'utf8' })).toBe('path=src/app.ts');
+ expect(summarizeToolArgs({ command: 'npm test' })).toBe('command=npm test');
+ expect(summarizeToolArgs({ foo: 'bar' })).toBe('foo=bar');
+ expect(summarizeToolArgs(null)).toBe('');
+ expect(summarizeToolResult('line1\nline2\nline3\nline4', true)).toBe('ok (4 lines)');
+ expect(summarizeToolResult('done', true)).toBe('ok done');
+ expect(summarizeToolResult('Error: Permission denied for read access', false)).toMatch(/^✗ Error: Permission denied/);
+ });
+
+ it('builds bounded trace lines and caps the trace length', () => {
+ const line = formatToolTraceLine({ tool: 'read_file', args: 'path=' + 'x'.repeat(300), outcome: 'ok' });
+ expect(line.length).toBeLessThanOrEqual(160);
+ const lines = Array.from({ length: 60 }, (_, i) => `- call ${i}`);
+ const trace = formatToolTrace(lines);
+ expect(trace.startsWith('[Tool activity in my previous turn]')).toBe(true);
+ expect(trace.split('\n')).toHaveLength(41);
+ expect(trace).toContain('21 more tool calls');
+ });
+});
diff --git a/src/core/context-window.ts b/src/core/context-window.ts
new file mode 100644
index 00000000..a52862c8
--- /dev/null
+++ b/src/core/context-window.ts
@@ -0,0 +1,139 @@
+/**
+ * Context window v2 — what the model sees from the session.
+ *
+ * Replaces the "last ten text messages" window with one that is budgeted by
+ * tokens and that includes the compact per-turn tool trace, so turn N+1
+ * remembers which files turn N read and wrote instead of starting blind.
+ * Retrieved memory and loop warnings are NOT turned into fake user/assistant
+ * dialogue any more; callers put them in the volatile system block.
+ */
+import type { SessionMessage } from '../sessions/types.js';
+
+/** Token budget for history in normal mode. Rough: 4 chars per token. */
+export const HISTORY_TOKEN_BUDGET = 6000;
+/** Token budget for history in Saver Mode. */
+export const HISTORY_TOKEN_BUDGET_SAVER = 2400;
+/** Hard cap on history entries regardless of budget. */
+export const HISTORY_MAX_ENTRIES = 40;
+/** Always keep at least this many entries so the model sees the last exchange. */
+export const HISTORY_MIN_ENTRIES = 2;
+/** Session message kind used for the per-turn tool trace entry. */
+export const TOOL_TRACE_KIND = 'tool-call' as const;
+/** Max lines kept in one turn's trace and max chars per line. */
+export const TOOL_TRACE_MAX_LINES = 40;
+export const TOOL_TRACE_LINE_CHARS = 160;
+
+export const estimateTokens = (text: string): number => Math.ceil((text?.length ?? 0) / 4);
+
+/** Entries the model may see: user/assistant text and our own tool traces. */
+export function isHistoryEligible(entry: SessionMessage): boolean {
+ if (entry.role !== 'user' && entry.role !== 'assistant') return false;
+ if (entry.kind === 'message') return true;
+ return entry.kind === TOOL_TRACE_KIND;
+}
+
+/**
+ * Newest-first selection under a token budget, returned oldest-first.
+ * The budget counts content only (reasoning is attached separately and only
+ * for providers that want it).
+ */
+export function selectHistoryWindow(
+ messages: readonly SessionMessage[],
+ budgetTokens: number,
+ maxEntries = HISTORY_MAX_ENTRIES,
+): SessionMessage[] {
+ const eligible = messages.filter(isHistoryEligible);
+ const picked: SessionMessage[] = [];
+ let used = 0;
+ for (let i = eligible.length - 1; i >= 0; i--) {
+ const entry = eligible[i];
+ const cost = estimateTokens(entry.content);
+ const mustKeep = picked.length < HISTORY_MIN_ENTRIES;
+ if (!mustKeep && (picked.length >= maxEntries || used + cost > budgetTokens)) break;
+ picked.push(entry);
+ used += cost;
+ }
+ return picked.reverse();
+}
+
+// Reasoning is kept beside the message object, never on it: the AI SDK
+// validates message shapes and some providers reject unknown fields. Only
+// DeepSeek thinking models want it back (issue #24).
+const REASONING = new WeakMap<object, string>();
+
+export interface ModelMessage {
+ role: 'user' | 'assistant';
+ content: string;
+}
+
+export function toModelMessage(entry: SessionMessage): ModelMessage {
+ const message: ModelMessage = { role: entry.role as 'user' | 'assistant', content: entry.content };
+ if (entry.role === 'assistant' && typeof entry.reasoning === 'string' && entry.reasoning.trim()) {
+ REASONING.set(message, entry.reasoning);
+ }
+ return message;
+}
+
+/**
+ * For providers that require prior reasoning to be passed back (DeepSeek
+ * thinking mode), convert assistant messages that carry stored reasoning
+ * into part arrays. Other messages are returned untouched.
+ */
+export function withReasoningParts(messages: readonly unknown[]): unknown[] {
+ return messages.map((m) => {
+ const reasoning = typeof m === 'object' && m ? REASONING.get(m) : undefined;
+ if (!reasoning) return m;
+ const msg = m as ModelMessage;
+ return {
+ role: 'assistant',
+ content: [
+ { type: 'reasoning', text: reasoning },
+ { type: 'text', text: msg.content },
+ ],
+ };
+ });
+}
+
+/** Fields worth showing for a tool call, in priority order. */
+const ARG_KEYS = ['path', 'file', 'command', 'query', 'url', 'pattern', 'name', 'skill', 'task', 'message'];
+
+export function summarizeToolArgs(input: unknown): string {
+ if (!input || typeof input !== 'object') return '';
+ const record = input as Record<string, unknown>;
+ for (const key of ARG_KEYS) {
+ const value = record[key];
+ if (typeof value === 'string' && value.trim()) return `${key}=${truncate(value.replace(/\s+/g, ' ').trim(), 80)}`;
+ }
+ const first = Object.entries(record).find(([, v]) => typeof v === 'string' && (v as string).trim());
+ return first ? `${first[0]}=${truncate((first[1] as string).replace(/\s+/g, ' ').trim(), 80)}` : '';
+}
+
+export function summarizeToolResult(result: unknown, ok: boolean): string {
+ const text = typeof result === 'string' ? result : JSON.stringify(result ?? '');
+ const head = text.replace(/\s+/g, ' ').trim();
+ if (!ok) return `✗ ${truncate(head, 70)}`;
+ const lines = typeof result === 'string' ? result.split('\n').length : 1;
+ return lines > 3 ? `ok (${lines} lines)` : `ok ${truncate(head, 50)}`;
+}
+
+export interface ToolTraceStep {
+ tool: string;
+ args: string;
+ outcome: string;
+}
+
+export function formatToolTraceLine(step: ToolTraceStep): string {
+ return truncate(`- ${step.tool}${step.args ? ` ${step.args}` : ''} → ${step.outcome}`, TOOL_TRACE_LINE_CHARS);
+}
+
+/** The single session entry written at turn end summarising tool activity. */
+export function formatToolTrace(lines: readonly string[]): string {
+ const kept = lines.length > TOOL_TRACE_MAX_LINES
+ ? [...lines.slice(0, TOOL_TRACE_MAX_LINES - 1), `- … ${lines.length - (TOOL_TRACE_MAX_LINES - 1)} more tool calls`]
+ : [...lines];
+ return `[Tool activity in my previous turn]\n${kept.join('\n')}`;
+}
+
+function truncate(text: string, max: number): string {
+ return text.length > max ? `${text.slice(0, max - 1)}…` : text;
+}
diff --git a/src/memory/user-memory.ts b/src/memory/user-memory.ts
index ffac1d39..f126887c 100644
--- a/src/memory/user-memory.ts
+++ b/src/memory/user-memory.ts
@@ -251,18 +251,12 @@ export class UserMemoryStore {
}
}
+ // No relevant memory → no context. The profile summary is already part
+ // of the stable system prompt (Agent.buildSystemPrompt → getSummary), so
+ // re-injecting it here on every zero-hit turn only added tokens and
+ // primed stale facts.
if (selected.length === 0) {
- const profile = this.getProfile().trim();
- if (!profile) {
- return { records: [], context: '' };
- }
- return {
- records: [],
- context: [
- this.getActiveSummary() ? `User active state:\n- ${this.getActiveSummary()}` : '',
- `User profile summary:\n- ${profile}`,
- ].filter(Boolean).join('\n\n'),
- };
+ return { records: [], context: '' };
}
const contextLines: string[] = [];
@@ -272,12 +266,6 @@ export class UserMemoryStore {
contextLines.push(`- ${activeSummary}`);
contextLines.push('');
}
- const profileSummary = this.getProfile();
- if (profileSummary) {
- contextLines.push('User profile summary:');
- contextLines.push(`- ${profileSummary}`);
- contextLines.push('');
- }
contextLines.push('Relevant user memory:');
contextLines.push(...selected.map(row => `- [${row.type}] ${row.summary}`));
diff --git a/src/providers/base.ts b/src/providers/base.ts
index 46ccc4a1..6ad188a1 100644
--- a/src/providers/base.ts
+++ b/src/providers/base.ts
@@ -31,4 +31,13 @@ export abstract class BaseProvider {
getModel(): string {
return this.config.model;
}
+
+ /**
+ * Largest `maxOutputTokens` this provider/model accepts, or undefined for
+ * "no known ceiling". The agent loop clamps its request to this so a
+ * 32K request is not rejected outright and retried at half size.
+ */
+ getMaxOutputTokens(): number | undefined {
+ return this.config.maxOutputTokens;
+ }
}
\ No newline at end of file
diff --git a/src/providers/deepseek.ts b/src/providers/deepseek.ts
index 1ea91ccd..02439b11 100644
--- a/src/providers/deepseek.ts
+++ b/src/providers/deepseek.ts
@@ -12,7 +12,10 @@ export class DeepSeekProvider extends BaseProvider {
super(config);
this.name = config.name;
this.model = config.model;
- this.isReasoner = config.model === 'deepseek-reasoner';
+ // Thinking is not only `deepseek-reasoner`: V4-family and any model
+ // whose name says reasoner/thinking expose reasoning_content and need
+ // thinking enabled (issue #24).
+ this.isReasoner = /^deepseek-reasoner$|^deepseek-v4|reasoner|thinking/i.test(config.model);
const client = createDeepSeek({
apiKey: config.apiKey,
@@ -36,4 +39,9 @@ export class DeepSeekProvider extends BaseProvider {
getModelInstance() {
return this.modelInstance;
}
+
+ /** deepseek-chat accepts at most 8K output tokens; reasoning models allow more. */
+ getMaxOutputTokens(): number | undefined {
+ return this.config.maxOutputTokens ?? (this.isReasoner ? undefined : 8192);
+ }
}
\ No newline at end of file
diff --git a/src/providers/registry.ts b/src/providers/registry.ts
index 6aa970ea..9b6c3b09 100644
--- a/src/providers/registry.ts
+++ b/src/providers/registry.ts
@@ -127,11 +127,11 @@ export class ProviderRegistry {
}
getDefault(): BaseProvider {
- if (this.lastSuccessful) {
- const provider = this.providers.get(this.lastSuccessful);
- if (provider) return provider;
- }
-
+ // The configured default always leads. markSuccess used to make the
+ // last provider that happened to work sticky, so a single transient
+ // failure silently moved every following turn — often to a weaker
+ // model — until restart. The fallback chain still handles outages per
+ // turn; lastSuccessful is kept for diagnostics only.
const provider = this.providers.get(this.defaultName);
if (!provider) {
const first = this.providers.values().next().value;
@@ -173,6 +173,11 @@ export class ProviderRegistry {
this.lastSuccessful = name;
}
+ /** Name of the provider that completed the most recent turn, if any. */
+ getLastSuccessful(): string | null {
+ return this.lastSuccessful;
+ }
+
listAvailable(): string[] {
return [...this.providers.keys()];
}
diff --git a/src/utils/config.ts b/src/utils/config.ts
index 0f69d872..5523aa87 100644
--- a/src/utils/config.ts
+++ b/src/utils/config.ts
@@ -34,6 +34,8 @@ export interface ProviderConfig {
baseUrl: string;
model: string;
enabled: boolean;
+ /** Optional per-provider cap on output tokens per request. */
+ maxOutputTokens?: number;
}
export interface TelegramAccessUser {
@@ -327,6 +329,7 @@ export function getDefaultConfig(): MercuryConfig {
baseUrl: getEnv('DEEPSEEK_BASE_URL', 'https://api.deepseek.com/v1'),
model: getEnv('DEEPSEEK_MODEL', 'deepseek-chat'),
enabled: getEnvBool('DEEPSEEK_ENABLED', true),
+ ...(getEnv('DEEPSEEK_MAX_OUTPUT_TOKENS', '') ? { maxOutputTokens: Number(getEnv('DEEPSEEK_MAX_OUTPUT_TOKENS', '')) } : {}),
},
grok: {
name: 'grok',