Roo-Code/src/shared
roomote[bot] b598efb422
feat: add per-task file-based history store for cross-instance safety (#11490)
* feat: add per-task file-based history store for cross-instance safety

Implement TaskHistoryStore service that stores each task's HistoryItem
as an individual JSON file in its existing task directory. This prevents
silent data loss when multiple VS Code windows write to the shared
globalState taskHistory array concurrently.

Key changes:
- New TaskHistoryStore class with per-task file writes via safeWriteJson
- Index file (_index.json) for fast startup reads
- Reconciliation logic to detect and fix drift between instances
- fs.watch for cross-instance reactivity
- Debounced index writes (2s window) for streaming performance
- Migration from globalState on first startup
- Write-through to globalState during transition period
- Fallback lookups from globalState for backward compatibility

Files created:
- src/core/task-persistence/TaskHistoryStore.ts
- src/core/task-persistence/__tests__/TaskHistoryStore.spec.ts
- src/core/task-persistence/__tests__/TaskHistoryStore.crossInstance.spec.ts

Files modified:
- src/shared/globalFileNames.ts (added historyItem, historyIndex)
- src/core/task-persistence/index.ts (export TaskHistoryStore)
- src/core/webview/ClineProvider.ts (integrate store, remove write lock)
- Test files updated for new store-based approach

* fix: address review feedback - reconcile lock, init promise, write-through serialization

- reconcile() now runs through withLock() to prevent interleaving with
  upsert/delete at async boundaries
- Added initialized promise so callers can await store readiness before
  reading (getStateToPostToWebview now awaits it)
- Write-through to globalState now happens inside the store lock via
  onWrite callback, preventing concurrent call races on the transition
  period fallback
- Removed separate updateGlobalState("taskHistory") calls from
  ClineProvider since the onWrite callback handles it serialized

* fix: add TaskHistoryStore to task-persistence mock in Task.persistence.spec.ts

The test mocks task-persistence with an explicit factory that was
missing the new TaskHistoryStore export, causing all 9 tests to fail
with "No TaskHistoryStore export is defined on the mock".

* perf: debounce globalState write-through to avoid full-array writes on every mutation

Instead of writing the entire HistoryItem[] array to globalState on
every upsert/delete (expensive with 5000+ tasks), the write-through
is now debounced with a 5-second window. Per-task file writes remain
immediate (~200 bytes each). The globalState is flushed on dispose
to ensure no data loss on shutdown.

This makes the hot path during streaming (token count updates) write
only the per-task file, not the full array.

---------

Co-authored-by: Roo Code <roomote@roocode.com>
2026-02-19 00:01:31 -07:00
..
__tests__ Reapply Batches 3-4: Skills, browser removal, provider removals (6 major-conflict cherry-picks) (#11475) 2026-02-14 22:06:24 -07:00
utils fix: create new Requesty profile during OAuth (#8699) 2025-11-02 01:07:16 -04:00
api.ts Reapply Batches 3-4: Skills, browser removal, provider removals (6 major-conflict cherry-picks) (#11475) 2026-02-14 22:06:24 -07:00
array.ts Initial streaming refactor 2024-10-09 01:49:54 -04:00
checkExistApiConfig.ts Revert to pre-AI-SDK state (January 29, 2026) (#11462) 2026-02-13 16:45:18 -05:00
combineApiRequests.ts Add some functionality to @roo-code/core for the cli (#10584) 2026-01-09 13:05:15 -08:00
combineCommandSequences.ts Add some functionality to @roo-code/core for the cli (#10584) 2026-01-09 13:05:15 -08:00
context-mentions.ts fix: restrict @-mention parsing to line-start or whitespace boundaries (#7876) 2025-09-11 19:37:10 -04:00
core.ts Add some functionality to @roo-code/core for the cli (#10584) 2026-01-09 13:05:15 -08:00
cost.ts Fix cost and token tracking between provider styles (#8954) 2025-10-31 15:14:29 -04:00
embeddingModels.ts Reapply Batch 1: 22 clean non-AI-SDK cherry-picks (#11473) 2026-02-14 13:47:12 -07:00
experiments.ts Enable parallel tool calls by default (#11031) 2026-01-29 01:29:15 -05:00
getApiMetrics.ts Add some functionality to @roo-code/core for the cli (#10584) 2026-01-09 13:05:15 -08:00
globalFileNames.ts feat: add per-task file-based history store for cross-instance safety (#11490) 2026-02-19 00:01:31 -07:00
language.ts feat: Indonesian translation (#4672) 2025-06-13 20:25:05 -04:00
modes.ts Move isToolAllowedForMode out of shared directory (#10089) 2025-12-15 02:18:08 -08:00
package.ts Focus the extension when receiving bridge commands (#7633) 2025-09-03 12:23:50 -07:00
parse-command.ts Move auto-approval from ChatView to Task (#9157) 2025-11-10 17:01:50 -08:00
ProfileValidator.ts Reapply Batches 3-4: Skills, browser removal, provider removals (6 major-conflict cherry-picks) (#11475) 2026-02-14 22:06:24 -07:00
skills.ts Reapply Batches 3-4: Skills, browser removal, provider removals (6 major-conflict cherry-picks) (#11475) 2026-02-14 22:06:24 -07:00
string-extensions.d.ts feat: recursively load .roo/rules and AGENTS.md from subdirectories (#10446) 2026-01-03 15:05:48 -05:00
support-prompt.ts Intelligent Context Condensation v2 (#10873) 2026-01-23 12:33:35 -07:00
todo.ts Add some functionality to @roo-code/core for the cli (#10584) 2026-01-09 13:05:15 -08:00
tools.ts Reapply Batches 3-4: Skills, browser removal, provider removals (6 major-conflict cherry-picks) (#11475) 2026-02-14 22:06:24 -07:00
vsCodeSelectorUtils.ts Prettier backfill 2025-01-17 14:11:28 -05:00
WebviewMessage.ts Move more types to @roo-code/types (for the cli) (#10583) 2026-01-09 11:59:06 -08:00