Skip to content

Commit 49227da

Browse files
doctarockedelauna
authored andcommitted
refactor(task): extract usage tracker
Refs Zoo-Code-Org#18
1 parent 4e4b4ac commit 49227da

3 files changed

Lines changed: 154 additions & 70 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { AskIgnoredError } from "./AskIgnoredError"
99

1010
import { Anthropic } from "@anthropic-ai/sdk"
1111
import OpenAI from "openai"
12-
import debounce from "lodash.debounce"
1312
import delay from "delay"
1413
import pWaitFor from "p-wait-for"
1514
import { serializeError } from "serialize-error"
@@ -64,10 +63,7 @@ import { maybeRemoveImageBlocks } from "../../api/transform/image-cleaning"
6463

6564
// shared
6665
import { findLastIndex } from "../../shared/array"
67-
import { combineApiRequests } from "../../shared/combineApiRequests"
68-
import { combineCommandSequences } from "../../shared/combineCommandSequences"
6966
import { t } from "../../i18n"
70-
import { getApiMetrics, hasTokenUsageChanged, hasToolUsageChanged } from "../../shared/getApiMetrics"
7167
import { ClineAskResponse } from "../../shared/WebviewMessage"
7268
import { defaultModeSlug, getModeBySlug } from "../../shared/modes"
7369
import { DiffStrategy, type ToolUse, type ToolParamName, toolParamNames } from "../../shared/tools"
@@ -132,6 +128,7 @@ import { AutoApprovalHandler, checkAutoApproval } from "../auto-approval"
132128
import { MessageManager } from "../message-manager"
133129
import { validateAndFixToolResultIds } from "./validateToolResultIds"
134130
import { mergeConsecutiveApiMessages } from "./mergeConsecutiveApiMessages"
131+
import { UsageTracker } from "./UsageTracker"
135132

136133
const MAX_EXPONENTIAL_BACKOFF_SECONDS = 600 // 10 minutes
137134
const DEFAULT_USAGE_COLLECTION_TIMEOUT_MS = 5000 // 5 seconds
@@ -323,7 +320,6 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
323320
consecutiveMistakeCountForEditFile: Map<string, number> = new Map()
324321
consecutiveNoToolUseCount: number = 0
325322
consecutiveNoAssistantMessagesCount: number = 0
326-
toolUsage: ToolUsage = {}
327323

328324
// Checkpoints
329325
enableCheckpoints: boolean
@@ -397,16 +393,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
397393
// This prevents excessive getModel() calls during tool execution
398394
cachedStreamingModel?: { id: string; info: ModelInfo }
399395

400-
// Token Usage Cache
401-
private tokenUsageSnapshot?: TokenUsage
402-
private tokenUsageSnapshotAt?: number
403-
404-
// Tool Usage Cache
405-
private toolUsageSnapshot?: ToolUsage
406-
407-
// Token Usage Throttling - Debounced emit function
408-
private readonly TOKEN_USAGE_EMIT_INTERVAL_MS = 2000 // 2 seconds
409-
private debouncedEmitTokenUsage: ReturnType<typeof debounce>
396+
private usageTracker: UsageTracker
410397

411398
// Historical cloud sync tracking retained only to avoid task resume churn.
412399
private cloudSyncedMessageTimestamps: Set<number> = new Set()
@@ -541,27 +528,12 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
541528
this.todoList = initialTodos
542529
}
543530

544-
// Initialize debounced token usage emit function
545-
// Uses debounce with maxWait to achieve throttle-like behavior:
546-
// - leading: true - Emit immediately on first call
547-
// - trailing: true - Emit final state when updates stop
548-
// - maxWait - Ensures at most one emit per interval during rapid updates (throttle behavior)
549-
this.debouncedEmitTokenUsage = debounce(
550-
(tokenUsage: TokenUsage, toolUsage: ToolUsage) => {
551-
const tokenChanged = hasTokenUsageChanged(tokenUsage, this.tokenUsageSnapshot)
552-
const toolChanged = hasToolUsageChanged(toolUsage, this.toolUsageSnapshot)
553-
554-
if (tokenChanged || toolChanged) {
555-
this.emit(RooCodeEventName.TaskTokenUsageUpdated, this.taskId, tokenUsage, toolUsage)
556-
this.tokenUsageSnapshot = tokenUsage
557-
this.tokenUsageSnapshotAt = this.clineMessages.at(-1)?.ts
558-
// Deep copy tool usage for snapshot
559-
this.toolUsageSnapshot = JSON.parse(JSON.stringify(toolUsage))
560-
}
561-
},
562-
this.TOKEN_USAGE_EMIT_INTERVAL_MS,
563-
{ leading: true, trailing: true, maxWait: this.TOKEN_USAGE_EMIT_INTERVAL_MS },
564-
)
531+
this.usageTracker = new UsageTracker({
532+
taskId: this.taskId,
533+
getMessages: () => this.clineMessages,
534+
getLastMessageTs: () => this.clineMessages.at(-1)?.ts,
535+
emit: (event, ...args) => (this.emit as any)(event, ...args),
536+
})
565537

566538
onCreated?.(this)
567539

@@ -1238,7 +1210,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
12381210
// - Immediate first emit (leading: true)
12391211
// - At most one emit per interval during rapid updates (maxWait)
12401212
// - Final state is emitted when updates stop (trailing: true)
1241-
this.debouncedEmitTokenUsage(tokenUsage, this.toolUsage)
1213+
this.usageTracker.emitTokenUsageUpdate(tokenUsage)
12421214

12431215
await this.providerRef.deref()?.updateTaskHistory(historyItem)
12441216
return true
@@ -2249,9 +2221,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
22492221
* Triggers the debounce with current values and immediately flushes to ensure emit.
22502222
*/
22512223
public emitFinalTokenUsageUpdate(): void {
2252-
const tokenUsage = this.getTokenUsage()
2253-
this.debouncedEmitTokenUsage(tokenUsage, this.toolUsage)
2254-
this.debouncedEmitTokenUsage.flush()
2224+
this.usageTracker.emitFinalTokenUsageUpdate()
22552225
}
22562226

22572227
public async abortTask(isAbandoned = false) {
@@ -4638,31 +4608,19 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
46384608
// Metrics
46394609

46404610
public combineMessages(messages: ClineMessage[]) {
4641-
return combineApiRequests(combineCommandSequences(messages))
4611+
return this.usageTracker.combineMessages(messages)
46424612
}
46434613

46444614
public getTokenUsage(): TokenUsage {
4645-
return getApiMetrics(this.combineMessages(this.clineMessages.slice(1)))
4615+
return this.usageTracker.getTokenUsage()
46464616
}
46474617

46484618
public recordToolUsage(toolName: ToolName) {
4649-
if (!this.toolUsage[toolName]) {
4650-
this.toolUsage[toolName] = { attempts: 0, failures: 0 }
4651-
}
4652-
4653-
this.toolUsage[toolName].attempts++
4619+
this.usageTracker.recordToolUsage(toolName)
46544620
}
46554621

46564622
public recordToolError(toolName: ToolName, error?: string) {
4657-
if (!this.toolUsage[toolName]) {
4658-
this.toolUsage[toolName] = { attempts: 0, failures: 0 }
4659-
}
4660-
4661-
this.toolUsage[toolName].failures++
4662-
4663-
if (error) {
4664-
this.emit(RooCodeEventName.TaskToolFailed, this.taskId, toolName, error)
4665-
}
4623+
this.usageTracker.recordToolError(toolName, error)
46664624
}
46674625

46684626
// Getters
@@ -4692,14 +4650,15 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
46924650
}
46934651

46944652
public get tokenUsage(): TokenUsage | undefined {
4695-
if (this.tokenUsageSnapshot && this.tokenUsageSnapshotAt) {
4696-
return this.tokenUsageSnapshot
4697-
}
4653+
return this.usageTracker.tokenUsage
4654+
}
46984655

4699-
this.tokenUsageSnapshot = this.getTokenUsage()
4700-
this.tokenUsageSnapshotAt = this.clineMessages.at(-1)?.ts
4656+
public get toolUsage(): ToolUsage {
4657+
return this.usageTracker.toolUsage
4658+
}
47014659

4702-
return this.tokenUsageSnapshot
4660+
public set toolUsage(toolUsage: ToolUsage) {
4661+
this.usageTracker.toolUsage = toolUsage
47034662
}
47044663

47054664
public get cwd() {

‎src/core/task/UsageTracker.ts‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import debounce from "lodash.debounce"
2+
3+
import { type ClineMessage, type TokenUsage, type ToolName, type ToolUsage, RooCodeEventName } from "@roo-code/types"
4+
5+
import { combineApiRequests } from "../../shared/combineApiRequests"
6+
import { combineCommandSequences } from "../../shared/combineCommandSequences"
7+
import { getApiMetrics, hasTokenUsageChanged, hasToolUsageChanged } from "../../shared/getApiMetrics"
8+
9+
type EmitTaskEvent = (event: RooCodeEventName, ...args: any[]) => boolean
10+
11+
export interface UsageTrackerOptions {
12+
taskId: string
13+
getMessages: () => ClineMessage[]
14+
getLastMessageTs: () => number | undefined
15+
emit: EmitTaskEvent
16+
emitIntervalMs?: number
17+
}
18+
19+
const DEFAULT_TOKEN_USAGE_EMIT_INTERVAL_MS = 2000
20+
21+
export class UsageTracker {
22+
private readonly taskId: string
23+
private readonly getMessages: () => ClineMessage[]
24+
private readonly getLastMessageTs: () => number | undefined
25+
private readonly emit: EmitTaskEvent
26+
private readonly debouncedEmitTokenUsage: ReturnType<typeof debounce>
27+
28+
private tokenUsageSnapshot?: TokenUsage
29+
private tokenUsageSnapshotAt?: number
30+
private toolUsageSnapshot?: ToolUsage
31+
private currentToolUsage: ToolUsage = {}
32+
33+
constructor({
34+
taskId,
35+
getMessages,
36+
getLastMessageTs,
37+
emit,
38+
emitIntervalMs = DEFAULT_TOKEN_USAGE_EMIT_INTERVAL_MS,
39+
}: UsageTrackerOptions) {
40+
this.taskId = taskId
41+
this.getMessages = getMessages
42+
this.getLastMessageTs = getLastMessageTs
43+
this.emit = emit
44+
45+
// Uses debounce with maxWait to achieve throttle-like behavior:
46+
// - leading: true - Emit immediately on first call
47+
// - trailing: true - Emit final state when updates stop
48+
// - maxWait - Ensures at most one emit per interval during rapid updates
49+
this.debouncedEmitTokenUsage = debounce(
50+
(tokenUsage: TokenUsage, toolUsage: ToolUsage) => {
51+
const tokenChanged = hasTokenUsageChanged(tokenUsage, this.tokenUsageSnapshot)
52+
const toolChanged = hasToolUsageChanged(toolUsage, this.toolUsageSnapshot)
53+
54+
if (tokenChanged || toolChanged) {
55+
this.emit(RooCodeEventName.TaskTokenUsageUpdated, this.taskId, tokenUsage, toolUsage)
56+
this.tokenUsageSnapshot = tokenUsage
57+
this.tokenUsageSnapshotAt = this.getLastMessageTs()
58+
this.toolUsageSnapshot = JSON.parse(JSON.stringify(toolUsage))
59+
}
60+
},
61+
emitIntervalMs,
62+
{ leading: true, trailing: true, maxWait: emitIntervalMs },
63+
)
64+
}
65+
66+
public combineMessages(messages: ClineMessage[]) {
67+
return combineApiRequests(combineCommandSequences(messages))
68+
}
69+
70+
public getTokenUsage(): TokenUsage {
71+
return getApiMetrics(this.combineMessages(this.getMessages().slice(1)))
72+
}
73+
74+
public emitTokenUsageUpdate(tokenUsage: TokenUsage): void {
75+
this.debouncedEmitTokenUsage(tokenUsage, this.currentToolUsage)
76+
}
77+
78+
public emitFinalTokenUsageUpdate(): void {
79+
this.emitTokenUsageUpdate(this.getTokenUsage())
80+
this.debouncedEmitTokenUsage.flush()
81+
}
82+
83+
public recordToolUsage(toolName: ToolName): void {
84+
const usage = this.ensureToolUsageEntry(toolName)
85+
usage.attempts++
86+
}
87+
88+
public recordToolError(toolName: ToolName, error?: string): void {
89+
const usage = this.ensureToolUsageEntry(toolName)
90+
usage.failures++
91+
92+
if (error) {
93+
this.emit(RooCodeEventName.TaskToolFailed, this.taskId, toolName, error)
94+
}
95+
}
96+
97+
public get toolUsage(): ToolUsage {
98+
return this.currentToolUsage
99+
}
100+
101+
public set toolUsage(toolUsage: ToolUsage) {
102+
this.currentToolUsage = toolUsage
103+
}
104+
105+
public get tokenUsage(): TokenUsage | undefined {
106+
if (this.tokenUsageSnapshot && this.tokenUsageSnapshotAt) {
107+
return this.tokenUsageSnapshot
108+
}
109+
110+
this.tokenUsageSnapshot = this.getTokenUsage()
111+
this.tokenUsageSnapshotAt = this.getLastMessageTs()
112+
113+
return this.tokenUsageSnapshot
114+
}
115+
116+
private ensureToolUsageEntry(toolName: ToolName): { attempts: number; failures: number } {
117+
if (!this.currentToolUsage[toolName]) {
118+
this.currentToolUsage[toolName] = { attempts: 0, failures: 0 }
119+
}
120+
121+
return this.currentToolUsage[toolName]!
122+
}
123+
}

‎src/core/task/__tests__/Task.throttle.test.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ describe("Task token usage throttling", () => {
296296
})
297297

298298
// Get the initial snapshot
299-
const initialSnapshot = (task as any).tokenUsageSnapshot
299+
const initialSnapshot = (task as any).usageTracker.tokenUsageSnapshot
300300

301301
// Add another message within throttle window
302302
vi.advanceTimersByTime(500)
@@ -308,7 +308,7 @@ describe("Task token usage throttling", () => {
308308
})
309309

310310
// Snapshot should still be the same (throttled)
311-
expect((task as any).tokenUsageSnapshot).toBe(initialSnapshot)
311+
expect((task as any).usageTracker.tokenUsageSnapshot).toBe(initialSnapshot)
312312

313313
// Add message after throttle window
314314
vi.advanceTimersByTime(1600) // Total: 2100ms
@@ -320,9 +320,11 @@ describe("Task token usage throttling", () => {
320320
})
321321

322322
// Snapshot should be updated now (new object reference)
323-
expect((task as any).tokenUsageSnapshot).not.toBe(initialSnapshot)
323+
expect((task as any).usageTracker.tokenUsageSnapshot).not.toBe(initialSnapshot)
324324
// Values should be different
325-
expect((task as any).tokenUsageSnapshot.totalTokensIn).toBeGreaterThan(initialSnapshot.totalTokensIn)
325+
expect((task as any).usageTracker.tokenUsageSnapshot.totalTokensIn).toBeGreaterThan(
326+
initialSnapshot.totalTokensIn,
327+
)
326328
})
327329

328330
test("should not emit if token usage has not changed even after throttle period", async () => {
@@ -456,7 +458,7 @@ describe("Task token usage throttling", () => {
456458
})
457459

458460
// Initially toolUsageSnapshot should be set to current toolUsage (empty object)
459-
const initialSnapshot = (task as any).toolUsageSnapshot
461+
const initialSnapshot = (task as any).usageTracker.toolUsageSnapshot
460462
expect(initialSnapshot).toBeDefined()
461463
expect(Object.keys(initialSnapshot)).toHaveLength(0)
462464

@@ -478,7 +480,7 @@ describe("Task token usage throttling", () => {
478480
})
479481

480482
// Snapshot should be updated to match the new toolUsage
481-
const newSnapshot = (task as any).toolUsageSnapshot
483+
const newSnapshot = (task as any).usageTracker.toolUsageSnapshot
482484
expect(newSnapshot).not.toBe(initialSnapshot)
483485
expect(newSnapshot.read_file).toEqual({ attempts: 3, failures: 0 })
484486
expect(newSnapshot.write_to_file).toEqual({ attempts: 2, failures: 1 })
@@ -488,8 +490,8 @@ describe("Task token usage throttling", () => {
488490
const emitSpy = vi.spyOn(task, "emit")
489491

490492
// Set initial tool usage and simulate previous emission
491-
;(task as any).tokenUsageSnapshot = task.getTokenUsage()
492-
;(task as any).toolUsageSnapshot = {}
493+
;(task as any).usageTracker.tokenUsageSnapshot = task.getTokenUsage()
494+
;(task as any).usageTracker.toolUsageSnapshot = {}
493495

494496
// Change tool usage
495497
task.toolUsage = {

0 commit comments

Comments
 (0)