@trans-ai/trans-code 0.8.2 → 0.9.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.
@@ -0,0 +1,84 @@
1
+ # 自动压缩 Implementation Plan
2
+
3
+ > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
4
+
5
+ **Goal:** 每轮提交前估算上下文,超阈值自动压缩(与 /compact、resume 同一 `runCompaction`),带配置与连败熔断。
6
+
7
+ **Architecture:** 纯逻辑(估算 + 配置归一)落 compact.ts/config.ts 走 TDD;REPL 在 handleSubmit 进入 agent loop 前接检查点,复用宿主条目流式显示;基于已审查的 `feat/compact-speed` 分支续作。
8
+
9
+ **Tech Stack:** TypeScript、bun:test。
10
+
11
+ **Spec:** `docs/superpowers/specs/2026-11-21-auto-compact-design.md`
12
+
13
+ ## Global Constraints
14
+
15
+ - 阈值默认 `thresholdChars = 200_000`,估算 `estimateTokens = Σ contentText(chars)/4`
16
+ - 配置 `autoCompact?: { enabled?: boolean; thresholdChars?: number }`,默认 **enabled = true**;坏类型/空值全量回落默认(仿 normalizeAutoCompact 与 normalizeAutoUpdate 同风格)
17
+ - 熔断:连续失败 **3** 次停止自动压缩,成功复位;失败提示仅每会话首次显示
18
+ - 压缩参数 keepTurns = `DEFAULT_KEEP_TURNS`;三入口共用 `runCompaction`
19
+ - 跳过:无进行中会话、`TRANSCODE_MOCK=1`、busy
20
+
21
+ ## Review Focus
22
+
23
+ - 首轮(无 conversationRef)与未达阈值不触发(接线)
24
+ - 恰好等于阈值触发(估算 `>=` 边界,Task 1 测试)
25
+ - 连败 3 次停、成功复位(接线,ref 语义)
26
+ - 坏配置(字符串 thresholdChars、null、数组)回落默认不崩(Task 1 测试)
27
+ - 压缩后本轮 loop 使用已压缩 messagesRef 且提要注入生效(复用现有注入点,接线核对)
28
+ - autoCompact 与用户提问回显的条目顺序不串(接线)
29
+
30
+ ---
31
+
32
+ ### Task 1: 估算 + 配置归一(纯逻辑)
33
+
34
+ **Files:**
35
+ - Modify: `src/sessions/compact.ts`、`src/sessions/compact.test.ts`、`src/config.ts`、`src/config.test.ts`
36
+
37
+ **Interfaces:**
38
+ - Produces:
39
+ - `estimateTokens(messages: BaseMessage[]): number`(compact.ts;contentText 求和 / 4,向下取整)
40
+ - config.ts:`interface AutoCompactConfig { enabled?: boolean; thresholdChars?: number }`、`interface NormalizedAutoCompact { enabled: boolean; thresholdChars: number }`、`normalizeAutoCompact(raw: unknown): NormalizedAutoCompact`、`TranscodeConfig.autoCompact?: boolean | AutoCompactConfig`(boolean 直通,与 autoUpdate 同款 raw 形状)
41
+
42
+ - [ ] **Step 1: 失败测试**
43
+
44
+ ```ts
45
+ // compact.test.ts
46
+ describe('estimateTokens', () => {
47
+ test('sums content chars / 4, floors; non-string content via JSON; empty → 0', () => {
48
+ expect(estimateTokens([])).toBe(0);
49
+ expect(estimateTokens([new HumanMessage('a'.repeat(40))])).toBe(10);
50
+ expect(estimateTokens([new HumanMessage('x'.repeat(10)), new AIMessage('y'.repeat(30))])).toBe(10);
51
+ });
52
+ });
53
+ // config.test.ts
54
+ describe('normalizeAutoCompact', () => {
55
+ test('undefined → enabled default 200_000; false → disabled; valid override kept', () => {
56
+ expect(normalizeAutoCompact(undefined)).toEqual({ enabled: true, thresholdChars: 200_000 });
57
+ expect(normalizeAutoCompact(false)).toEqual({ enabled: false, thresholdChars: 200_000 });
58
+ expect(normalizeAutoCompact({ thresholdChars: 50_000 })).toEqual({ enabled: true, thresholdChars: 50_000 });
59
+ });
60
+ test('garbage (string/null/array/negative) falls back to defaults', () => {
61
+ expect(normalizeAutoCompact('yes')).toEqual({ enabled: true, thresholdChars: 200_000 });
62
+ expect(normalizeAutoCompact([1])).toEqual({ enabled: true, thresholdChars: 200_000 });
63
+ expect(normalizeAutoCompact({ thresholdChars: -5 })).toEqual({ enabled: true, thresholdChars: 200_000 });
64
+ });
65
+ });
66
+ ```
67
+
68
+ - [ ] **Step 2: 确认失败**(导出不存在)
69
+ - [ ] **Step 3: 实现** — `estimateTokens` 用已有 `contentText`;`normalizeAutoCompact` 仿 `normalizeAutoUpdate` 分支
70
+ - [ ] **Step 4: 确认通过**
71
+ - [ ] **Step 5: Commit** — `feat: token estimation and autoCompact config normalization`
72
+
73
+ ### Task 2: REPL 检查点接线
74
+
75
+ **Files:**
76
+ - Modify: `src/screens/REPL.tsx`(handleSubmit 进入 agent loop 前;import estimateTokens/normalizeAutoCompact/loadConfig 已有)
77
+
78
+ **Interfaces:**
79
+ - Consumes: Task 1 全部;`runCompaction`、`buildSessionRecord`、`transcriptEntries`、`DEFAULT_KEEP_TURNS`、`saveSession`
80
+
81
+ - [ ] **Step 1: 状态** — `const autoCompactFailuresRef = useRef(0);`、`const autoCompactWarnedRef = useRef(false);`
82
+ - [ ] **Step 2: 检查点**(handleSubmit 内、构建 provider/进入 try 之前):条件 `normalizeAutoCompact(loadConfig().autoCompact).enabled && conversationRef.current && process.env.TRANSCODE_MOCK !== '1' && autoCompactFailuresRef.current < 3 && estimateTokens(messagesRef.current) >= cfg.thresholdChars` 满足时:`setBusyBoth(true)` → 空宿主条目 → `runCompaction(buildSessionRecord({...}), DEFAULT_KEEP_TURNS)`;成功 → 回写 refs + `saveSession` + 失败计数清零 + `transcriptEntries` 渲染;失败/异常 → 计数 +1,首次时 `⚠ 自动压缩失败(原因),本轮继续;连续 3 次将暂停自动压缩`,达 3 次时提示「自动压缩已暂停」;finally `setBusyBoth(false)`
83
+ - [ ] **Step 3: 全量验证** — `bun test` + `npm run typecheck` + `npm run build:repl`
84
+ - [ ] **Step 4: Commit** — `feat: proactive auto-compact with threshold config and failure circuit breaker`
@@ -0,0 +1,85 @@
1
+ # 压缩提速(流式 + compact 路由 + 硬上限)Implementation Plan
2
+
3
+ > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
4
+
5
+ **Goal:** 压缩调用改为流式并实时显示、可路由快速模型、输出字符硬上限,消除感知与实际慢。
6
+
7
+ **Architecture:** `CompactModel` 换成 stream 接口,`compressSession` 增 `onDelta` 回调与 `COMPACT_MAX_SUMMARY_CHARS` 截断;REPL 两入口接 `appendChatOutput` 流式显示,模型经 `getRouting()['compact'] ?? ctx.modelId` 选择。
8
+
9
+ **Tech Stack:** TypeScript、LangChain(`model.stream()`)、bun:test。
10
+
11
+ **Spec:** `docs/superpowers/specs/2026-11-21-compact-speed-design.md`
12
+
13
+ ## Global Constraints
14
+
15
+ - `COMPACT_MAX_SUMMARY_CHARS = 1200`,累积超限即停止消费流并 `slice` 到上限
16
+ - `CompactModel = { stream(input: string): AsyncIterable<{ content: unknown }> }`;chunk 文本取 `typeof content === 'string' ? content : ''`
17
+ - 模型选择:`createModel(getRouting().compact ?? ctx.modelId)`;未配置 routing 行为与现状一致
18
+ - 失败回退(流中抛错/空摘要)、滚动合并、≤keepTurns no-op 语义不变;`TRANSCODE_MOCK` 跳过压缩不变
19
+
20
+ ## Review Focus
21
+
22
+ - 流中第 2 块抛错 → 原会话完整回退、无文件写入(Task 1 测试)
23
+ - 显示与保存一致:onDelta 逐块文本拼接 === 最终 summary(Task 1 测试)
24
+ - 超限:5000 字符流 → summary ≤ 1200 且提前停止消费(Task 1 测试 + 生成器消费计数)
25
+ - 未配 `routing.compact` → 用当前模型(接线:?? 右侧默认)
26
+ - 多块 content 非字符串(如数组块)不崩、按空串跳过(Task 1 测试)
27
+
28
+ ---
29
+
30
+ ### Task 1: 流式压缩核心
31
+
32
+ **Files:**
33
+ - Modify: `src/sessions/compact.ts`、`src/sessions/compact.test.ts`
34
+
35
+ **Interfaces:**
36
+ - Produces:
37
+ - `export const COMPACT_MAX_SUMMARY_CHARS = 1200`
38
+ - `export interface CompactModel { stream(input: string): AsyncIterable<{ content: unknown }> }`
39
+ - `compressSession(record, model, keepTurns = DEFAULT_KEEP_TURNS, onDelta?: (delta: string) => void)`(返回形不变)
40
+
41
+ - [ ] **Step 1: 失败测试** — mock 改 async generator(`streamModel(chunks: string[], opts?: {throwAt?: number; trackPulled?: number[]}): CompactModel & { prompts: string[] }`),新增/改写:
42
+
43
+ ```ts
44
+ test('streams chunks into the summary and forwards each delta', async () => {
45
+ const deltas: string[] = [];
46
+ const { record } = await compressSession(recordWith(3), streamModel(['前情', '提要']), 1, d => deltas.push(d));
47
+ expect(record.summary).toBe('前情提要');
48
+ expect(deltas).toEqual(['前情', '提要']);
49
+ });
50
+ test('caps summary at COMPACT_MAX_SUMMARY_CHARS and stops consuming early', async () => {
51
+ const pulled: number[] = [];
52
+ const model = streamModel(['x'.repeat(1000), 'y'.repeat(1000), 'z'.repeat(1000)], { trackPulled: pulled });
53
+ const { record } = await compressSession(recordWith(3), model, 1);
54
+ expect(record.summary!.length).toBeLessThanOrEqual(COMPACT_MAX_SUMMARY_CHARS);
55
+ expect(pulled.length).toBe(2); // 第三块未被消费
56
+ });
57
+ test('mid-stream error falls back with error set', async () => {
58
+ const { compressed, record, error } = await compressSession(recordWith(3), streamModel(['前情'], { throwAt: 2 }), 1);
59
+ expect(compressed).toBe(false); expect(error).toBeDefined(); expect(record).toBeDefined();
60
+ });
61
+ test('non-string chunk content is skipped', async () => {
62
+ const model = { stream: async function* () { yield { content: '前情' }; yield { content: [{ text: 'x' }] }; yield { content: '提要' }; } };
63
+ const { record } = await compressSession(recordWith(3), model as never, 1);
64
+ expect(record.summary).toBe('前情提要');
65
+ });
66
+ ```
67
+ (既有 8 个压缩用例换用 `streamModel(['提要内容'])` 等形式,断言不变)
68
+
69
+ - [ ] **Step 2: 确认失败** — `bun test src/sessions/compact.test.ts` → FAIL(stream 不存在/断言不匹配)
70
+ - [ ] **Step 3: 实现** — `COMPACT_MAX_SUMMARY_CHARS`;`compressSession` 内:`let summary = ''` + `for await (const chunk of model.stream(prompt))` 逐块取串文本、`onDelta?.(text)`、超限 `break`;循环后 `summary = summary.slice(0, COMPACT_MAX_SUMMARY_CHARS).trim()`;空 → error;catch 覆盖流中抛错
71
+ - [ ] **Step 4: 确认通过** — 同命令 → PASS
72
+ - [ ] **Step 5: Commit** — `git add src/sessions/compact.ts src/sessions/compact.test.ts && git commit -m "feat: streaming compaction with per-chunk deltas and hard char cap"`
73
+
74
+ ### Task 2: REPL 流式显示 + compact 路由
75
+
76
+ **Files:**
77
+ - Modify: `src/screens/REPL.tsx`(resumeWith、/compact 分支;import `getRouting`)
78
+
79
+ **Interfaces:**
80
+ - Consumes: Task 1 全部导出;`getRouting()`(provider.js 已导出)
81
+
82
+ - [ ] **Step 1: 共享 helper** — 抽 `runCompaction(record, keepTurns): Promise<{compressed, record, error?}>`:内部统一「正在提炼前情提要…」提示 + `createModel(getRouting().compact ?? ctx.modelId).model` + `onDelta: delta => appendChatOutput(delta)` + 完成后补 `\n\n`;resumeWith 与 /compact 两入口**都只调它**,行为完全一致
83
+ - [ ] **Step 2: 两入口接线** — resumeWith 与 /compact 分支替换各自的模型构造与调用为 `runCompaction`(mock 跳过逻辑保留在 helper 内)
84
+ - [ ] **Step 3: 全量验证** — `bun test` 全绿 + `npm run typecheck` + `npm run build:repl`
85
+ - [ ] **Step 4: Commit** — `git commit -m "feat: live-stream compaction summary and compact-role fast-model routing"`
@@ -0,0 +1,25 @@
1
+ # 自动压缩(每轮提交前阈值检查)— 设计
2
+
3
+ ## 意图
4
+
5
+ 对齐 Claude Code 的 proactive autocompact:会话进行中每轮提交前检查上下文占用,超阈值自动压缩,避免长会话撞上下文上限。与恢复压缩、`/compact` 三路归一(同一个 `runCompaction`)。
6
+
7
+ ## 设计(参考 claudecode/src/services/compact/autoCompact.ts)
8
+
9
+ - **检查点**:每次提交聊天消息时(`handleSubmit` 在进入 agent loop 之前),估算 `messagesRef` 总量,超阈值先压缩再继续本轮
10
+ - **估算**:纯函数 `estimateTokens(messages) = 全部消息文本长度 / 4`(CC 同款粗估,chars≈4/token)
11
+ - **阈值**:`autoCompact.thresholdChars`(默认 **200_000** 字符 ≈ 50k token —— 多厂商无窗口注册表,取保守值;128k 窗口模型够用,32k 模型也不会先撞墙),命中即压缩
12
+ - **配置**:`~/.transcode.json` 的 `autoCompact?: { enabled?: boolean; thresholdChars?: number }`,`normalizeAutoCompact` 容错(仿 `normalizeAutoUpdate`:坏配置回落默认,**默认开启**)
13
+ - **压缩参数**:keepTurns 用 `DEFAULT_KEEP_TURNS`(=1,与两入口一致的「一套逻辑」);失败回退语义不变
14
+ - **熔断器**:`useRef` 记连续失败次数,**连续 3 次停止自动压缩**(CC 同款,真实事故换来的),成功即复位;自动失败提示仅首次显示,后续静默(CC:自动失败不打扰)
15
+ - **显示**:压缩前先落一条空宿主条目,流式摘要照常打进聊天区,完成后 `transcriptEntries` 渲染窗口(与 /compact 同款)
16
+ - **跳过**:无进行中会话(首轮)、`TRANSCODE_MOCK=1`、已 busy、压缩中重入
17
+
18
+ ## 非目标
19
+
20
+ - 不做 per-model 上下文窗口注册表、microcompact(工具结果修剪)、reactive(PTL 错误后被动压缩)、阻塞上限/警告分层 —— 有 per-model 窗口数据后再上
21
+
22
+ ## 备选(已否)
23
+
24
+ - 默认关闭待配置:用户点明参考 CC(CC 默认开启)— 默认开
25
+ - 按轮数触发(如每 20 轮):轮长差异大,token 估算更贴近 CC 语义
@@ -0,0 +1,21 @@
1
+ # 压缩提速(流式 + 快速模型路由 + 硬上限)— 设计
2
+
3
+ ## 意图
4
+
5
+ 压缩慢的两个根源:阻塞式 `invoke()` 全程无反馈(感知慢)、用当前推理模型烧思考 token(真慢)。对齐 Claude Code 的做法:流式展示、可路由快速模型、输出硬上限。
6
+
7
+ ## 设计
8
+
9
+ - **流式**:`CompactModel` 接口从 `{ invoke }` 改为 `{ stream(input): AsyncIterable<{content}> }`;`compressSession(record, model, keepTurns, onDelta?)` 逐块累积摘要,`onDelta(块文本)` 回调供 REPL 实时打进聊天区(先显示「正在提炼前情提要…」再流式输出)。流中抛错/空结果 → 原有失败回退语义不变
10
+ - **字符硬上限**:`COMPACT_MAX_SUMMARY_CHARS = 1200`,累积超限即停止消费流(provider 无关的输出钳制,替代依赖各厂商 maxTokens 参数)
11
+ - **快速模型路由**:压缩调用用 `createModel(getRouting()['compact'] ?? 当前模型)` —— 在 `~/.transcode.json` 的 `routing` 里配 `"compact": "<快速模型>"`(如 glm-4.6-air / deepseek-chat)即可换快模型;未配置则维持当前模型(流式 + 上限仍生效)
12
+ - 测试:mock 改为 async generator;新增流式累积、超限截断、流中报错、onDelta 传递用例
13
+
14
+ ## 非目标
15
+
16
+ - 不做 prompt-cache 复用(单请求形态不同)、不做增量边界总结(数据模型不同)、不引入 <summary> 八段式(后续可单独迭代)
17
+
18
+ ## 备选(已否)
19
+
20
+ - per-call maxTokens 参数:LangChain 跨厂商 bind 参数行为不一,字符上限更可靠
21
+ - 默认硬编码快速模型:用户 key/厂商各异,routing 显式配置更诚实
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trans-ai/trans-code",
3
- "version": "0.8.2",
3
+ "version": "0.9.0",
4
4
  "description": "AI-powered multi-model coding CLI",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
@@ -47,3 +47,28 @@ describe('normalizeAutoUpdate', () => {
47
47
  }
48
48
  });
49
49
  });
50
+
51
+ import { normalizeAutoCompact } from './config.js';
52
+
53
+ describe('normalizeAutoCompact', () => {
54
+ test('undefined → enabled default 200_000; false → disabled; valid override kept', () => {
55
+ expect(normalizeAutoCompact(undefined)).toEqual({ enabled: true, thresholdChars: 200_000, keepTurns: 1 });
56
+ expect(normalizeAutoCompact(false)).toEqual({ enabled: false, thresholdChars: 200_000, keepTurns: 1 });
57
+ expect(normalizeAutoCompact({ thresholdChars: 50_000 })).toEqual({
58
+ enabled: true,
59
+ thresholdChars: 50_000,
60
+ keepTurns: 1,
61
+ });
62
+ expect(normalizeAutoCompact({ enabled: false, thresholdChars: 80_000, keepTurns: 5 })).toEqual({
63
+ enabled: false,
64
+ thresholdChars: 80_000,
65
+ keepTurns: 5,
66
+ });
67
+ });
68
+
69
+ test('garbage (string/null/array/negative) falls back to defaults', () => {
70
+ for (const raw of ['yes', 5, null, [1], { thresholdChars: -5 }, { thresholdChars: 'big' }, { keepTurns: 0 }, { keepTurns: 'ten' }]) {
71
+ expect(normalizeAutoCompact(raw)).toEqual({ enabled: true, thresholdChars: 200_000, keepTurns: 1 });
72
+ }
73
+ });
74
+ });
package/src/config.ts CHANGED
@@ -23,6 +23,8 @@ export interface TranscodeConfig {
23
23
  routing?: Record<string, string>;
24
24
  /** Auto-update on startup: `false` disables, an object configures it. */
25
25
  autoUpdate?: boolean | AutoUpdateConfig;
26
+ /** Auto-compact on threshold: `false` disables, an object configures it. */
27
+ autoCompact?: boolean | AutoCompactConfig;
26
28
  }
27
29
 
28
30
  /** Raw (on-disk) shape of the autoUpdate config block. */
@@ -65,6 +67,54 @@ export function normalizeAutoUpdate(raw: unknown): NormalizedAutoUpdate {
65
67
  };
66
68
  }
67
69
 
70
+ /** Raw (on-disk) shape of the autoCompact config block. */
71
+ export interface AutoCompactConfig {
72
+ enabled?: boolean;
73
+ /** Compact when the estimated context reaches this many chars. */
74
+ thresholdChars?: number;
75
+ /** Turns kept verbatim when auto-compacting (default 1). */
76
+ keepTurns?: number;
77
+ }
78
+
79
+ /** Normalized form used by the REPL checkpoint. */
80
+ export interface NormalizedAutoCompact {
81
+ enabled: boolean;
82
+ thresholdChars: number;
83
+ keepTurns: number;
84
+ }
85
+
86
+ const DEFAULT_AUTO_COMPACT: NormalizedAutoCompact = {
87
+ enabled: true,
88
+ thresholdChars: 200_000,
89
+ keepTurns: 1,
90
+ };
91
+
92
+ /**
93
+ * Normalizes the raw autoCompact config value. Anything unrecognizable
94
+ * falls back to the enabled default rather than throwing — same contract
95
+ * as normalizeAutoUpdate.
96
+ */
97
+ export function normalizeAutoCompact(raw: unknown): NormalizedAutoCompact {
98
+ if (raw === false) return { ...DEFAULT_AUTO_COMPACT, enabled: false };
99
+ if (raw === null || raw === undefined || typeof raw !== 'object' || Array.isArray(raw)) {
100
+ return { ...DEFAULT_AUTO_COMPACT };
101
+ }
102
+ const obj = raw as AutoCompactConfig;
103
+ const threshold =
104
+ typeof obj.thresholdChars === 'number' && obj.thresholdChars > 0
105
+ ? obj.thresholdChars
106
+ : DEFAULT_AUTO_COMPACT.thresholdChars;
107
+ const keepTurns =
108
+ typeof obj.keepTurns === 'number' && obj.keepTurns >= 1
109
+ ? Math.floor(obj.keepTurns)
110
+ : DEFAULT_AUTO_COMPACT.keepTurns;
111
+ return {
112
+ enabled: typeof obj.enabled === 'boolean' ? obj.enabled : true,
113
+ thresholdChars: threshold,
114
+ keepTurns,
115
+ };
116
+ }
117
+
68
118
  // Overridable so tests never touch the user's real ~/.transcode.json.
69
119
  const configPath = (): string =>
70
120
  process.env.TRANSCODE_CONFIG_PATH ?? join(homedir(), '.transcode.json');
@@ -5,6 +5,7 @@ import { findCommand, getCommands, type CommandContext } from '../commands.js';
5
5
  import {
6
6
  getAvailableModels,
7
7
  createModel,
8
+ getRouting,
8
9
  setPreferredModel,
9
10
  registerApiKey,
10
11
  lookupApiKey,
@@ -16,7 +17,7 @@ import {
16
17
  switchNote,
17
18
  type ModelId,
18
19
  } from '../provider/provider.js';
19
- import { loadConfig } from '../config.js';
20
+ import { loadConfig, normalizeAutoCompact } from '../config.js';
20
21
  import { runAgentLoop, type PermissionRequest } from '../agent/loop.js';
21
22
  import { createToolSession } from '../agent/tools/index.js';
22
23
  import { getPermissionMode } from '../agent/permission.js';
@@ -30,7 +31,7 @@ import {
30
31
  type SessionRecord,
31
32
  } from '../sessions/store.js';
32
33
  import { listProjectSessions, resolveResumeTarget, splitResumeArgs } from '../sessions/resume.js';
33
- import { DEFAULT_KEEP_TURNS, compressSession, parseKeepCount, transcriptEntries, splitTurns } from '../sessions/compact.js';
34
+ import { DEFAULT_KEEP_TURNS, compressSession, parseKeepCount, shouldAutoCompact, transcriptEntries, splitTurns } from '../sessions/compact.js';
34
35
  import { walkFiles } from '../agent/tools/fs-utils.js';
35
36
  import { applyCompletion, filterFiles, mentionQuery } from './file-completion.js';
36
37
  import {
@@ -141,6 +142,9 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
141
142
  const conversationTitleRef = useRef('');
142
143
  // Compaction digest of turns older than the kept window (from resume//compact).
143
144
  const summaryRef = useRef<string | null>(null);
145
+ // Auto-compact breaker state: consecutive failures + warned-once flag.
146
+ const autoCompactFailuresRef = useRef(0);
147
+ const autoCompactWarnedRef = useRef(false);
144
148
  const lastSessionsRef = useRef<string[]>([]);
145
149
  // Snapshot for the /resume arrow-key picker (set together with the phase).
146
150
  const sessionPickerRef = useRef<SessionRecord[]>([]);
@@ -240,20 +244,23 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
240
244
  /** Shared resume flow: auto-compact over-long sessions, then load + render. */
241
245
  const resumeWith = async (record: SessionRecord, inputLine: string, keepTurns = DEFAULT_KEEP_TURNS) => {
242
246
  const { turns } = splitTurns(record.messages);
243
- let finalRecord = record;
247
+ // Load first so the confirmation entry hosts the streamed digest.
248
+ applyResume(record, inputLine);
244
249
  let oldTurns = 0;
245
- if (turns.length > keepTurns) {
250
+ if (turns.length > keepTurns && process.env.TRANSCODE_MOCK !== '1') {
246
251
  setBusyBoth(true);
247
252
  try {
248
- if (process.env.TRANSCODE_MOCK !== '1') {
249
- const outcome = await compressSession(record, createModel(ctx.modelId).model, keepTurns);
250
- if (outcome.compressed) {
251
- finalRecord = outcome.record;
252
- oldTurns = turns.length - keepTurns;
253
- saveSession(finalRecord);
254
- } else {
255
- addHistory({ input: '', output: `⚠ 提炼失败(${outcome.error ?? '未知'}),已恢复完整对话。` });
256
- }
253
+ const outcome = await runCompaction(record, keepTurns);
254
+ if (outcome.compressed) {
255
+ oldTurns = turns.length - keepTurns;
256
+ summaryRef.current = outcome.record.summary ?? null;
257
+ messagesRef.current = outcome.record.messages;
258
+ saveSession(outcome.record);
259
+ // Manual success heals the auto-compact breaker too.
260
+ autoCompactFailuresRef.current = 0;
261
+ autoCompactWarnedRef.current = false;
262
+ } else {
263
+ addHistory({ input: '', output: `⚠ 提炼失败(${outcome.error ?? '未知'}),已恢复完整对话。` });
257
264
  }
258
265
  } catch {
259
266
  addHistory({ input: '', output: '⚠ 提炼失败,已恢复完整对话。' });
@@ -261,7 +268,6 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
261
268
  setBusyBoth(false);
262
269
  }
263
270
  }
264
- applyResume(finalRecord, inputLine);
265
271
  // Restore in the original conversation form: each message becomes a
266
272
  // history entry (rule-framed questions, full-length replies).
267
273
  for (const entry of transcriptEntries(summaryRef.current, messagesRef.current, oldTurns || undefined)) {
@@ -280,6 +286,35 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
280
286
  });
281
287
  }, []);
282
288
 
289
+ /**
290
+ * One shared compaction run for /resume and /compact: fast-model routing
291
+ * (routing.compact → fallback current model) with the digest streamed
292
+ * live into the chat area.
293
+ */
294
+ const runCompaction = async (
295
+ record: SessionRecord,
296
+ keepTurns: number,
297
+ ): Promise<{ compressed: boolean; record: SessionRecord; error?: string }> => {
298
+ appendChatOutput('\n正在提炼前情提要…\n');
299
+ try {
300
+ const outcome = await compressSession(
301
+ record,
302
+ createModel(getRouting().compact ?? ctx.modelId).model,
303
+ keepTurns,
304
+ (delta) => appendChatOutput(delta),
305
+ );
306
+ appendChatOutput('\n\n');
307
+ return outcome;
308
+ } catch (error) {
309
+ appendChatOutput('\n\n');
310
+ return {
311
+ compressed: false,
312
+ record,
313
+ error: error instanceof Error ? error.message : String(error),
314
+ };
315
+ }
316
+ };
317
+
283
318
  const askInTerminal = useCallback(
284
319
  (request: PermissionRequest, signal: AbortSignal): Promise<boolean> => {
285
320
  const mode = getPermissionMode();
@@ -301,11 +336,65 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
301
336
  [],
302
337
  );
303
338
 
339
+ // Auto-compact (Claude Code style proactive): threshold check before each
340
+ // turn, same runCompaction as /compact and resume. Circuit breaker stops
341
+ // after 3 consecutive failures; auto failures warn once per session.
342
+ const autoCompactIfNeeded = async (): Promise<void> => {
343
+ const cfg = normalizeAutoCompact(loadConfig().autoCompact);
344
+ if (!cfg.enabled || !conversationRef.current) return;
345
+ if (process.env.TRANSCODE_MOCK === '1') return;
346
+ if (autoCompactFailuresRef.current >= 3) return;
347
+ if (!shouldAutoCompact(messagesRef.current, cfg)) return;
348
+ try {
349
+ // Dedicated host entry before the question echo, so the digest neither
350
+ // mixes into the turn entry nor receives the turn's streamed reply.
351
+ addHistory({ input: '', output: '' });
352
+ const record = buildSessionRecord({
353
+ id: conversationRef.current.id,
354
+ title: conversationTitleRef.current,
355
+ createdAt: conversationRef.current.createdAt,
356
+ cwd: process.cwd(),
357
+ model: ctx.modelId,
358
+ messages: messagesRef.current,
359
+ summary: summaryRef.current,
360
+ });
361
+ const outcome = await runCompaction(record, cfg.keepTurns);
362
+ if (!outcome.compressed) {
363
+ // Without an error it is a no-op (≤ keepTurns turns — e.g. one giant
364
+ // turn over threshold): nothing compressible, skip without touching
365
+ // the breaker.
366
+ if (outcome.error) throw new Error(outcome.error);
367
+ return;
368
+ }
369
+ summaryRef.current = outcome.record.summary ?? null;
370
+ messagesRef.current = outcome.record.messages;
371
+ saveSession(outcome.record);
372
+ autoCompactFailuresRef.current = 0;
373
+ autoCompactWarnedRef.current = false;
374
+ for (const entry of transcriptEntries(summaryRef.current, messagesRef.current)) {
375
+ addHistory(entry);
376
+ }
377
+ } catch (error) {
378
+ autoCompactFailuresRef.current += 1;
379
+ const reason = error instanceof Error ? error.message : String(error);
380
+ if (autoCompactFailuresRef.current >= 3) {
381
+ addHistory({
382
+ input: '',
383
+ output: `⚠ 自动压缩已暂停(连续 3 次失败,最后原因:${reason})。可 /compact 手动压缩或检查 routing.compact 配置。`,
384
+ });
385
+ } else if (!autoCompactWarnedRef.current) {
386
+ autoCompactWarnedRef.current = true;
387
+ addHistory({ input: '', output: `⚠ 自动压缩失败(${reason}),本轮继续。` });
388
+ }
389
+ }
390
+ };
391
+
304
392
  const runChat = useCallback(
305
393
  async (text: string, display?: string, options?: { planMode?: boolean }) => {
306
394
  setBusyBoth(true);
307
395
  const controller = new AbortController();
308
396
  abortRef.current = controller;
397
+ await autoCompactIfNeeded();
309
398
  addHistory({ input: display ?? text, output: '' });
310
399
  if (options?.planMode) {
311
400
  appendChatOutput('[plan 模式:只读探索,产出方案,PLAN READY 后可让我执行]\n');
@@ -767,25 +856,28 @@ export default function REPL({ ctx, updateNotice }: REPLProps) {
767
856
  messages: messagesRef.current,
768
857
  summary: summaryRef.current,
769
858
  });
859
+ // Host entry first so the streamed digest renders inside this
860
+ // command's own frame (same contract as the resume path).
861
+ addHistory({ input: trimmed, output: '' });
770
862
  const outcome = process.env.TRANSCODE_MOCK === '1'
771
863
  ? { compressed: false as const, record, error: 'mock 模式无模型' }
772
- : await compressSession(record, createModel(ctx.modelId).model, keepN);
864
+ : await runCompaction(record, keepN);
773
865
  if (outcome.compressed) {
774
866
  summaryRef.current = outcome.record.summary ?? null;
775
867
  messagesRef.current = outcome.record.messages;
776
868
  saveSession(outcome.record);
777
- addHistory({
778
- input: trimmed,
779
- output: `已压缩:旧 ${turns.length - keepN} 轮提炼为前情提要,保留最近 ${keepN} 轮。`,
780
- });
869
+ // Manual success heals the auto-compact breaker too.
870
+ autoCompactFailuresRef.current = 0;
871
+ autoCompactWarnedRef.current = false;
872
+ appendChatOutput(`已压缩:旧 ${turns.length - keepN} 轮提炼为前情提要,保留最近 ${keepN} 轮。`);
781
873
  for (const entry of transcriptEntries(summaryRef.current, messagesRef.current)) {
782
874
  addHistory(entry);
783
875
  }
784
876
  } else {
785
- addHistory({ input: trimmed, output: `⚠ 压缩失败(${outcome.error ?? '未知'}),会话保持原样。` });
877
+ appendChatOutput(`⚠ 压缩失败(${outcome.error ?? '未知'}),会话保持原样。`);
786
878
  }
787
879
  } catch {
788
- addHistory({ input: trimmed, output: '⚠ 压缩失败,会话保持原样。' });
880
+ appendChatOutput('⚠ 压缩失败,会话保持原样。');
789
881
  } finally {
790
882
  setBusyBoth(false);
791
883
  }
@@ -2,10 +2,13 @@ import { describe, expect, test } from 'bun:test';
2
2
  import { AIMessage, HumanMessage, SystemMessage, ToolMessage, type BaseMessage } from '@langchain/core/messages';
3
3
  import type { SessionRecord } from './store.js';
4
4
  import {
5
+ COMPACT_MAX_SUMMARY_CHARS,
5
6
  DEFAULT_KEEP_TURNS,
6
7
  TRANSCRIPT_LIMIT,
7
8
  compressSession,
9
+ estimateTokens,
8
10
  parseKeepCount,
11
+ shouldAutoCompact,
9
12
  transcriptEntries,
10
13
  splitTurns,
11
14
  } from './compact.js';
@@ -31,13 +34,21 @@ function recordWith(turns: number, opts: { summary?: string; longTurn?: boolean
31
34
  };
32
35
  }
33
36
 
34
- function mockModel(text: string): { invoke: (input: string) => Promise<{ content: string }>; prompts: string[] } {
37
+ /** Streaming mock: yields chunks; can throw at chunk N and track pulls. */
38
+ function streamModel(
39
+ chunks: string[],
40
+ opts: { throwAt?: number; trackPulled?: string[] } = {},
41
+ ): { stream: (input: string) => AsyncGenerator<{ content: string }>; prompts: string[] } {
35
42
  const prompts: string[] = [];
36
43
  return {
37
44
  prompts,
38
- invoke: async (input: string) => {
45
+ stream: async function* (input: string) {
39
46
  prompts.push(input);
40
- return { content: text };
47
+ for (let i = 0; i < chunks.length; i++) {
48
+ opts.trackPulled?.push(chunks[i]!);
49
+ if (opts.throwAt === i + 1) throw new Error('stream blew up');
50
+ yield { content: chunks[i]! };
51
+ }
41
52
  },
42
53
  };
43
54
  }
@@ -62,66 +73,120 @@ describe('splitTurns', () => {
62
73
  });
63
74
 
64
75
  describe('compressSession', () => {
76
+ test('streams chunks into the summary and forwards each delta', async () => {
77
+ const deltas: string[] = [];
78
+ const { compressed, record } = await compressSession(
79
+ recordWith(3),
80
+ streamModel(['前情', '提要']),
81
+ 1,
82
+ (d) => deltas.push(d),
83
+ );
84
+ expect(compressed).toBe(true);
85
+ expect(record.summary).toBe('前情提要');
86
+ expect(deltas).toEqual(['前情', '提要']);
87
+ });
88
+
89
+ test('caps summary at COMPACT_MAX_SUMMARY_CHARS and stops consuming early', async () => {
90
+ const pulled: string[] = [];
91
+ const model = streamModel(['x'.repeat(1000), 'y'.repeat(1000), 'z'.repeat(1000)], { trackPulled: pulled });
92
+ const { record } = await compressSession(recordWith(3), model, 1);
93
+ expect(record.summary!.length).toBeLessThanOrEqual(COMPACT_MAX_SUMMARY_CHARS);
94
+ expect(pulled.length).toBe(2); // 第三块未消费
95
+ });
96
+
97
+ test('mid-stream error falls back with error set', async () => {
98
+ const rec = recordWith(3);
99
+ const { compressed, record, error } = await compressSession(rec, streamModel(['前情', '提要'], { throwAt: 2 }), 1);
100
+ expect(compressed).toBe(false);
101
+ expect(error).toBeDefined();
102
+ expect(record).toBe(rec);
103
+ });
104
+
105
+ test('non-string chunk content is skipped', async () => {
106
+ const model = {
107
+ stream: async function* () {
108
+ yield { content: '前情' };
109
+ yield { content: [{ text: 'x' }] };
110
+ yield { content: '提要' };
111
+ },
112
+ };
113
+ const { record } = await compressSession(recordWith(3), model as never, 1);
114
+ expect(record.summary).toBe('前情提要');
115
+ });
116
+
65
117
  test('default keeps only 1 turn', async () => {
66
- const { compressed, record } = await compressSession(recordWith(3), mockModel('提要内容'));
118
+ const { compressed, record } = await compressSession(recordWith(3), streamModel(['提要内容']));
67
119
  expect(compressed).toBe(true);
68
120
  expect(record.summary).toBe('提要内容');
69
- expect(splitTurns(record.messages).turns.length).toBe(DEFAULT_KEEP_TURNS);
70
121
  expect(splitTurns(record.messages).turns.length).toBe(1);
71
122
  expect(record.messages[0]!.getType()).toBe('system');
72
123
  expect(record.updatedAt).toBeGreaterThan(1);
73
124
  });
74
125
 
75
126
  test('explicit keepTurns=10 keeps 10', async () => {
76
- const { record } = await compressSession(recordWith(12), mockModel('提要'), 10);
127
+ const { record } = await compressSession(recordWith(12), streamModel(['提要']), 10);
77
128
  expect(splitTurns(record.messages).turns.length).toBe(10);
78
129
  });
79
130
 
80
131
  test('keepTurns below 1 clamps to 1', async () => {
81
- const { record } = await compressSession(recordWith(3), mockModel('提要'), 0);
132
+ const { record } = await compressSession(recordWith(3), streamModel(['提要']), 0);
82
133
  expect(splitTurns(record.messages).turns.length).toBe(1);
83
134
  });
84
135
 
85
136
  test('at or below keepTurns is a no-op returning the original record', async () => {
86
137
  const rec = recordWith(10);
87
- const { compressed, record } = await compressSession(rec, mockModel('x'), 10);
138
+ const { compressed, record } = await compressSession(rec, streamModel(['x']), 10);
88
139
  expect(compressed).toBe(false);
89
140
  expect(record).toBe(rec);
90
141
  });
91
142
 
92
- test('model failure returns original record with error', async () => {
93
- const rec = recordWith(12);
94
- const failing = {
95
- invoke: async () => {
96
- throw new Error('API down');
97
- },
98
- };
99
- const { compressed, record, error } = await compressSession(rec, failing, 10);
100
- expect(compressed).toBe(false);
101
- expect(error).toBeDefined();
102
- expect(record).toBe(rec);
103
- });
104
-
105
- test('empty model reply is treated as failure', async () => {
106
- const { compressed, error } = await compressSession(recordWith(12), mockModel(' '), 10);
143
+ test('empty summary is treated as failure', async () => {
144
+ const { compressed, error } = await compressSession(recordWith(12), streamModel([' ']), 10);
107
145
  expect(compressed).toBe(false);
108
146
  expect(error).toBeDefined();
109
147
  });
110
148
 
111
149
  test('rolling merge: previous summary appears in the prompt', async () => {
112
- const model = mockModel('新提要');
150
+ const model = streamModel(['新提要']);
113
151
  await compressSession(recordWith(12, { summary: '旧提要内容' }), model, 10);
114
152
  expect(model.prompts[0]).toContain('旧提要内容');
115
153
  });
116
154
 
117
155
  test('huge old transcript is truncated before invoking the model', async () => {
118
- const model = mockModel('提要');
156
+ const model = streamModel(['提要']);
119
157
  await compressSession(recordWith(12, { longTurn: true }), model, 10);
120
158
  expect(model.prompts[0]!.length).toBeLessThan(TRANSCRIPT_LIMIT + 2_000);
121
159
  expect(model.prompts[0]).toContain('更早内容已截断');
122
160
  });
123
161
  });
124
162
 
163
+ describe('estimateTokens', () => {
164
+ test('sums content chars / 4, floors; empty → 0', () => {
165
+ expect(estimateTokens([])).toBe(0);
166
+ expect(estimateTokens([new HumanMessage('a'.repeat(40))])).toBe(10);
167
+ expect(
168
+ estimateTokens([new HumanMessage('x'.repeat(10)), new AIMessage('y'.repeat(30))]),
169
+ ).toBe(10);
170
+ expect(estimateTokens([new HumanMessage('abcd')])).toBe(1);
171
+ });
172
+
173
+ test('counts tool_calls args payloads (often the largest text)', () => {
174
+ const ai = new AIMessage({
175
+ content: '',
176
+ tool_calls: [{ id: 'c1', name: 'write_file', args: { content: 'x'.repeat(200) } }],
177
+ });
178
+ expect(estimateTokens([ai])).toBeGreaterThanOrEqual(50);
179
+ });
180
+ });
181
+
182
+ describe('shouldAutoCompact', () => {
183
+ test('compares in CHARS: at threshold triggers, just below does not', () => {
184
+ const cfg = { thresholdChars: 200 };
185
+ expect(shouldAutoCompact([new HumanMessage('a'.repeat(200))], cfg)).toBe(true);
186
+ expect(shouldAutoCompact([new HumanMessage('a'.repeat(196))], cfg)).toBe(false);
187
+ });
188
+ });
189
+
125
190
  describe('parseKeepCount', () => {
126
191
  test('undefined → default; "5" → 5; "0"/"abc" → error, no keep', () => {
127
192
  expect(parseKeepCount(undefined)).toEqual({});
@@ -7,9 +7,38 @@ export const DEFAULT_KEEP_TURNS = 1;
7
7
  /** Cap on the transcript fed to the distilling model (protects its context). */
8
8
  export const TRANSCRIPT_LIMIT = 40_000;
9
9
 
10
- /** Minimal model surface needed for distillation (mock-friendly). */
10
+ /** Cap on the distilled summary: stop consuming the stream past this. */
11
+ export const COMPACT_MAX_SUMMARY_CHARS = 1200;
12
+
13
+ /** Minimal streaming model surface needed for distillation (mock-friendly).
14
+ * LangChain's model.stream() returns Promise<IterableReadableStream>, plain
15
+ * async generators return the iterable directly — both are accepted. */
11
16
  export interface CompactModel {
12
- invoke(input: string): Promise<{ content: unknown }>;
17
+ stream(
18
+ input: string,
19
+ ): AsyncIterable<{ content: unknown }> | Promise<AsyncIterable<{ content: unknown }>>;
20
+ }
21
+
22
+ /** Rough token estimate for auto-compact: total content chars / 4.
23
+ * Tool-call args are included — in a coding session they are often the
24
+ * largest text (write_file/edit payloads). */
25
+ export function estimateTokens(messages: BaseMessage[]): number {
26
+ let chars = 0;
27
+ for (const message of messages) {
28
+ chars += contentText(message.content).length;
29
+ const calls = (message as AIMessage).tool_calls;
30
+ if (calls?.length) chars += JSON.stringify(calls).length;
31
+ }
32
+ return Math.floor(chars / 4);
33
+ }
34
+
35
+ /** Auto-compact decision. thresholdChars is in CHARS; estimateTokens is in
36
+ * tokens (chars/4) — convert back so units match. */
37
+ export function shouldAutoCompact(
38
+ messages: BaseMessage[],
39
+ cfg: { thresholdChars: number },
40
+ ): boolean {
41
+ return estimateTokens(messages) * 4 >= cfg.thresholdChars;
13
42
  }
14
43
 
15
44
  /**
@@ -68,10 +97,6 @@ function buildPrompt(previousSummary: string | undefined, oldTurns: BaseMessage[
68
97
  return `${head}${prior}\n对话历史:\n${renderTranscriptText(oldTurns)}`;
69
98
  }
70
99
 
71
- function replyText(content: unknown): string {
72
- return (typeof content === 'string' ? content : JSON.stringify(content)).trim();
73
- }
74
-
75
100
  /**
76
101
  * Validate a raw keep-turns argument: undefined → default (no opinion),
77
102
  * pure digits ≥1 → {keep}, anything else → {error} so callers can show a
@@ -93,19 +118,26 @@ export async function compressSession(
93
118
  record: SessionRecord,
94
119
  model: CompactModel,
95
120
  keepTurns = DEFAULT_KEEP_TURNS,
121
+ onDelta?: (delta: string) => void,
96
122
  ): Promise<{ compressed: boolean; record: SessionRecord; error?: string }> {
97
123
  const window = Math.max(1, keepTurns);
98
124
  const { leading, turns } = splitTurns(record.messages);
99
125
  if (turns.length <= window) return { compressed: false, record };
100
126
  const keep = turns.slice(-window);
101
127
  const old = turns.slice(0, turns.length - window);
102
- let summary: string;
128
+ let raw = '';
103
129
  try {
104
- const response = await model.invoke(buildPrompt(record.summary, old));
105
- summary = replyText(response.content);
130
+ const stream = await model.stream(buildPrompt(record.summary, old));
131
+ for await (const chunk of stream) {
132
+ const text = typeof chunk.content === 'string' ? chunk.content : '';
133
+ if (text) onDelta?.(text);
134
+ raw += text;
135
+ if (raw.length >= COMPACT_MAX_SUMMARY_CHARS) break;
136
+ }
106
137
  } catch (error) {
107
138
  return { compressed: false, record, error: error instanceof Error ? error.message : String(error) };
108
139
  }
140
+ const summary = raw.slice(0, COMPACT_MAX_SUMMARY_CHARS).trim();
109
141
  if (!summary) return { compressed: false, record, error: '模型返回了空前情提要' };
110
142
  return {
111
143
  compressed: true,
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Auto-generated from package.json by scripts/sync-version.ts (npm version hook)
2
- export const VERSION = '0.8.2';
2
+ export const VERSION = '0.9.0';