Skip to content

Commit 0236bd9

Browse files
authored
Merge pull request #92 from phantom5099/refactor
Refactor
2 parents a4e116e + d905d9e commit 0236bd9

24 files changed

Lines changed: 212 additions & 108 deletions

packages/codingcode/src/agent/agent.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { LLMFactoryService } from '../llm/factory.js';
2828
import { getBuiltinTools } from '../tools/providers.js';
2929
import { canonicalizeSchema } from '../tools/utils/canonicalize-schema.js';
3030
import { normalizePath } from '../core/path.js';
31+
32+
const REACTIVE_COMPACT_MAX_RETRIES = 3;
3133
import { RulesService } from '../rules/index.js';
3234

3335
const logger = createLogger();
@@ -250,7 +252,7 @@ export function agentLoop(
250252
const system = [basePrompt, memorySection].filter(Boolean).join('\n\n');
251253

252254
const config = getContextConfig();
253-
const maxOverflowRetries = config.reactiveCompactMaxRetries;
255+
const maxOverflowRetries = REACTIVE_COMPACT_MAX_RETRIES;
254256
const model = state.sessionMeta?.model ?? 'unknown';
255257
const effectiveMaxSteps = opts.maxStepsOverride ?? maxSteps;
256258

packages/codingcode/src/context/service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ const COMPACTABLE_TOOLS = new Set([
2929
'edit_file',
3030
]);
3131

32+
const MICRO_COMPACT_THRESHOLD = 0.25;
33+
const MICRO_COMPACT_MIN_CHARS = 120;
34+
const COMPACTION_THRESHOLD = 0.9;
35+
const KEEP_RECENT_TURNS = 1;
36+
const REACTIVE_COMPACT_MAX_RETRIES = 3;
37+
3238
export class ContextService extends Effect.Service<ContextService>()('Context', {
3339
effect: Effect.gen(function* () {
3440
const session = yield* SessionService;
@@ -107,7 +113,7 @@ export class ContextService extends Effect.Service<ContextService>()('Context',
107113
contextWindow: number,
108114
jsonlPath: string
109115
): boolean {
110-
if (promptEstimate <= contextWindow * config.microCompactThreshold) return false;
116+
if (promptEstimate <= contextWindow * MICRO_COMPACT_THRESHOLD) return false;
111117

112118
const compactedTurnIds = new Set<number>();
113119
for (const ev of events) {
@@ -124,7 +130,7 @@ export class ContextService extends Effect.Service<ContextService>()('Context',
124130
if (ev.turnId >= currentTurnId - 1) continue;
125131
if (compactedTurnIds.has(ev.turnId)) continue;
126132
if (!COMPACTABLE_TOOLS.has(ev.toolName.toLowerCase())) continue;
127-
if (ev.output.length <= config.microCompactMinChars) continue;
133+
if (ev.output.length <= MICRO_COMPACT_MIN_CHARS) continue;
128134
oldResults.push(ev);
129135
}
130136

@@ -165,7 +171,7 @@ export class ContextService extends Effect.Service<ContextService>()('Context',
165171
return { didCompress: false, released: 0, promptEstimate };
166172
}
167173

168-
const threshold = modelMaxTokens * config.compactionThreshold;
174+
const threshold = modelMaxTokens * COMPACTION_THRESHOLD;
169175
if (promptEstimate <= threshold) {
170176
return { didCompress: false, released: 0, promptEstimate };
171177
}
@@ -208,7 +214,7 @@ export class ContextService extends Effect.Service<ContextService>()('Context',
208214

209215
let released = 0;
210216

211-
const threshold = modelMaxTokens ? modelMaxTokens * config.compactionThreshold : Infinity;
217+
const threshold = modelMaxTokens ? modelMaxTokens * COMPACTION_THRESHOLD : Infinity;
212218
if (usage === undefined || usage - released > threshold) {
213219
released += await tryCompaction(
214220
sessionId,
@@ -236,7 +242,7 @@ export class ContextService extends Effect.Service<ContextService>()('Context',
236242
currentTurnId: number,
237243
compactedTurnIds: Set<number>
238244
): Promise<number> {
239-
const endTurn = currentTurnId - config.keepRecentTurns - 1;
245+
const endTurn = currentTurnId - KEEP_RECENT_TURNS - 1;
240246
if (endTurn < 1) return 0;
241247

242248
const inRange = compactedEvents.filter((ev) => {

packages/codingcode/src/session/messages.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const COMPACTABLE_TOOLS = new Set([
1515
'edit_file',
1616
]);
1717

18+
const MICRO_COMPACT_MIN_CHARS = 120;
19+
1820
export interface VisibilityResult {
1921
hidden: Set<string>;
2022
compactedTurnIds: Set<number>;
@@ -109,7 +111,7 @@ export function buildMessagesFromEvents(
109111
if (
110112
compactedTurnIds.has(event.turnId) &&
111113
COMPACTABLE_TOOLS.has(event.toolName.toLowerCase()) &&
112-
event.output.length > getContextConfig().microCompactMinChars
114+
event.output.length > MICRO_COMPACT_MIN_CHARS
113115
) {
114116
output = `[Earlier: used ${event.toolName}]`;
115117
}

packages/codingcode/test/agent/agent-cache-stability.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import { MemoryService } from '../../src/memory/index.js';
99
vi.mock('@codingcode/infra/config', () => ({
1010
loadConfig: () => ({
1111
context: {
12-
microCompactThreshold: 0.7,
13-
microCompactMinChars: 200,
14-
compactionThreshold: 0.8,
15-
keepRecentTurns: 10,
1612
compactionModel: '',
17-
reactiveCompactMaxRetries: 1,
1813
},
1914
memory: {
2015
enabled: false,

packages/codingcode/test/agent/agent-concurrent.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import { MemoryService } from '../../src/memory/index.js';
99
vi.mock('@codingcode/infra/config', () => ({
1010
loadConfig: () => ({
1111
context: {
12-
microCompactThreshold: 0.7,
13-
microCompactMinChars: 200,
14-
compactionThreshold: 0.8,
15-
keepRecentTurns: 10,
1612
compactionModel: '',
17-
reactiveCompactMaxRetries: 1,
1813
},
1914
memory: {
2015
enabled: false,

packages/codingcode/test/agent/agent-todo-event.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import { MemoryService } from '../../src/memory/index.js';
99
vi.mock('@codingcode/infra/config', () => ({
1010
loadConfig: () => ({
1111
context: {
12-
microCompactThreshold: 0.7,
13-
microCompactMinChars: 200,
14-
compactionThreshold: 0.8,
15-
keepRecentTurns: 10,
1612
compactionModel: '',
17-
reactiveCompactMaxRetries: 1,
1813
},
1914
memory: {
2015
enabled: false,

packages/codingcode/test/agent/agent.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ import { MemoryService } from '../../src/memory/index.js';
1515
vi.mock('@codingcode/infra/config', () => ({
1616
loadConfig: () => ({
1717
context: {
18-
microCompactThreshold: 0.7,
19-
microCompactMinChars: 200,
20-
compactionThreshold: 0.8,
21-
keepRecentTurns: 10,
2218
compactionModel: '',
23-
reactiveCompactMaxRetries: 1,
2419
},
2520
memory: {
2621
enabled: false,

packages/codingcode/test/agent/config.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import { resolveConfig } from '../../src/agent/config.js';
44
vi.mock('@codingcode/infra/config', () => ({
55
loadConfig: () => ({
66
context: {
7-
microCompactThreshold: 0.7,
8-
microCompactMinChars: 200,
9-
compactionThreshold: 0.8,
10-
keepRecentTurns: 10,
117
compactionModel: '',
12-
reactiveCompactMaxRetries: 1,
138
},
149
memory: {
1510
enabled: false,

packages/codingcode/test/agent/hooks-deps-type.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import { MemoryService } from '../../src/memory/index.js';
99
vi.mock('@codingcode/infra/config', () => ({
1010
loadConfig: () => ({
1111
context: {
12-
microCompactThreshold: 0.7,
13-
microCompactMinChars: 200,
14-
compactionThreshold: 0.8,
15-
keepRecentTurns: 10,
1612
compactionModel: '',
17-
reactiveCompactMaxRetries: 1,
1813
},
1914
memory: {
2015
enabled: false,

packages/codingcode/test/agent/loop-options.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import { MemoryService } from '../../src/memory/index.js';
99
vi.mock('@codingcode/infra/config', () => ({
1010
loadConfig: () => ({
1111
context: {
12-
microCompactThreshold: 0.7,
13-
microCompactMinChars: 200,
14-
compactionThreshold: 0.8,
15-
keepRecentTurns: 10,
1612
compactionModel: '',
17-
reactiveCompactMaxRetries: 1,
1813
},
1914
memory: {
2015
enabled: false,

0 commit comments

Comments
 (0)