claude-code-best/src/services/rawDump/index.ts
林凯90331 f3ece2f469 refactor(rawDump): prevent 429 with queue + batch worker
Replace per-turn detached workers with a file-backed queue consumed by
a single long-running batch worker (30s interval, 0-10s jitter).

- queue.ts, batchWorker.ts: file queue with pid-based lock for worker
  singleton; tasks deduped by sessionID:messageID before processing
- worker.ts: retry with backoff (5s, 10s) for 429 and network errors;
  update commit state per upload so partial failures resume cleanly;
  export auth, loadSessionMessages, getSessionDirectory and upload*
  helpers for batchWorker reuse
- git.ts: cap commit log at 50 entries within last 7 days; pause 500ms
  every 10 commits to spread load
- worker.ts: resolve session jsonl via ~/.claude/projects/{normalized}
  with fallbacks, scanning for the file containing the target session
- logger.ts: file + stderr logger gated by CSC_RAW_DUMP_DEBUG, default
  silent
- sessionDataUploader.ts: implement createSessionTurnUploader to pick
  the last assistant message; query.ts fires uploadSessionTurn after
  query_api_streaming_end (non-blocking)

Signed-off-by: 林凯90331 <90331@sangfor.com>
Co-authored-by: CoStrict <zgsm@sangfor.com.cn>
2026-05-07 21:45:20 +08:00

38 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Raw Dump 主入口
* 队列模式:主进程只 enqueue单 batch worker 顺序消费
*/
import { enqueue } from './queue.js'
import { spawnBatchWorker } from './spawn.js'
let batchWorkerSpawned = false
function isEnabled(): boolean {
if (process.env.CSC_DISABLE_RAW_DUMP === '1' || process.env.CSC_DISABLE_RAW_DUMP === 'true') return false
if (process.env.COSTRICT_DISABLE_RAW_DUMP === '1' || process.env.COSTRICT_DISABLE_RAW_DUMP === 'true') return false
return true
}
function ensureBatchWorker() {
if (batchWorkerSpawned) return
batchWorkerSpawned = true
spawnBatchWorker()
}
/**
* 上报一轮对话
* 只写入队列,由 batch worker 顺序消费
*/
export function reportTurn(sessionID: string, messageID: string, directory: string): void {
if (!isEnabled()) return
enqueue({ sessionID, messageID, directory })
ensureBatchWorker()
}
export function reportSession(sessionID: string, directory: string): void {
if (!isEnabled()) return
enqueue({ sessionID, messageID: '__summary__', directory })
ensureBatchWorker()
}