tide-commander 1.174.0 → 1.176.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.
- package/CHANGELOG.md +24 -0
- package/dist/assets/{BossLogsModal-CsjhF9hQ.js → BossLogsModal-qzLlhqPZ.js} +1 -1
- package/dist/assets/{BossSpawnModal-DRgrZpYr.js → BossSpawnModal-Bo5btfZs.js} +1 -1
- package/dist/assets/{ControlsModal-zYwhMaaL.js → ControlsModal-CCP1SS-b.js} +1 -1
- package/dist/assets/{DockerLogsModal-D7bewBqJ.js → DockerLogsModal-d2Isd330.js} +1 -1
- package/dist/assets/{EmbeddedEditor-IxYwI5JV.js → EmbeddedEditor-C2whRCAd.js} +1 -1
- package/dist/assets/{GmailOAuthSetup-CfOuUZnu.js → GmailOAuthSetup-BM4xBt67.js} +1 -1
- package/dist/assets/{GoogleOAuthSetup-Cw1tGSti.js → GoogleOAuthSetup-BMvbdHEK.js} +1 -1
- package/dist/assets/{IframeModal-Bi8MNGZY.js → IframeModal-Diz8yWFu.js} +1 -1
- package/dist/assets/{IntegrationsPanel-01iVXM-l.js → IntegrationsPanel-W_8l0DxS.js} +2 -2
- package/dist/assets/{LogViewerModal-BXhxgz7Z.js → LogViewerModal-DU64WFDr.js} +1 -1
- package/dist/assets/{MonitoringModal-Gf0AkCEl.js → MonitoringModal-BoG0Hr7X.js} +1 -1
- package/dist/assets/{PM2LogsModal-BfoL_mEK.js → PM2LogsModal-ZemBmh8q.js} +1 -1
- package/dist/assets/{RestoreArchivedAreaModal-Ctss__a9.js → RestoreArchivedAreaModal-CEkL1jB3.js} +1 -1
- package/dist/assets/{Scene2DCanvas-BuCW0Zaw.js → Scene2DCanvas-BpX0JWYQ.js} +1 -1
- package/dist/assets/{SceneManager-BFnKKU1I.js → SceneManager-CdCkKNKF.js} +2 -2
- package/dist/assets/{SkillsPanel-D7kJE7eT.js → SkillsPanel-CcJJoxVd.js} +1 -1
- package/dist/assets/{SlackMultiInstanceSetup-Hp7r3YK0.js → SlackMultiInstanceSetup-D0QFzYL8.js} +1 -1
- package/dist/assets/{SpawnModal-Dq9P-FdI.js → SpawnModal-C8EMz9dV.js} +1 -1
- package/dist/assets/{StatisticsModal-BQOkmrmY.js → StatisticsModal-DBWeBd6u.js} +1 -1
- package/dist/assets/{SubordinateAssignmentModal-D2-IpRLn.js → SubordinateAssignmentModal-g2VK-d4p.js} +1 -1
- package/dist/assets/{TriggerManagerPanel-BCZhLuoZ.js → TriggerManagerPanel-CybQfvUd.js} +1 -1
- package/dist/assets/{WorkflowEditorPanel-BEjublaK.js → WorkflowEditorPanel-Doo1fthh.js} +1 -1
- package/dist/assets/{abnfDiagram-VRR7QNED-jjWEEISc.js → abnfDiagram-VRR7QNED-DIzWJ00e.js} +1 -1
- package/dist/assets/{arc-xqT5-bwQ.js → arc-DYnuDU5M.js} +1 -1
- package/dist/assets/{architectureDiagram-ZJ3FMSHR-BXEbOurL.js → architectureDiagram-ZJ3FMSHR-DcQkZge3.js} +1 -1
- package/dist/assets/{area-logos-BH-HM2a-.js → area-logos-CvKL6axv.js} +1 -1
- package/dist/assets/{blockDiagram-677ZJIJ3-D-0HVLDv.js → blockDiagram-677ZJIJ3-P9qfYIyd.js} +1 -1
- package/dist/assets/{c4Diagram-LMCZKHZV-BK39PXfT.js → c4Diagram-LMCZKHZV-Brvvf8IR.js} +1 -1
- package/dist/assets/channel-BjM2BONO.js +1 -0
- package/dist/assets/{chunk-2Q5K7J3B-CuW4NYbO.js → chunk-2Q5K7J3B-huNYyvxh.js} +1 -1
- package/dist/assets/{chunk-32BRIVSS-eJLo7W-b.js → chunk-32BRIVSS-BPwUeNlq.js} +1 -1
- package/dist/assets/{chunk-5VM5RSS4-E2Yaai0b.js → chunk-5VM5RSS4-ClvwfT9j.js} +1 -1
- package/dist/assets/{chunk-EX3LRPZG-9uAbQnMb.js → chunk-EX3LRPZG-r7lw8w07.js} +1 -1
- package/dist/assets/{chunk-JWPE2WC7-kKtXyMkv.js → chunk-JWPE2WC7-Cg-NNKhK.js} +1 -1
- package/dist/assets/{chunk-MOJQB5TN-DPN5ReJ7.js → chunk-MOJQB5TN-D1jRsGiz.js} +1 -1
- package/dist/assets/{chunk-RYQCIY6F-d3yFo4Hu.js → chunk-RYQCIY6F-DzLe8Mhn.js} +1 -1
- package/dist/assets/{chunk-V7JOEXUC--3A8oVa7.js → chunk-V7JOEXUC-CP3S5ECO.js} +1 -1
- package/dist/assets/{chunk-VR4S4FIN-KpHuZy9_.js → chunk-VR4S4FIN-CJsUxduu.js} +1 -1
- package/dist/assets/{chunk-XXDRQBXY-hpU30gZQ.js → chunk-XXDRQBXY-CG5BEMYm.js} +1 -1
- package/dist/assets/classDiagram-OUVF2IWQ-Bo96wh-7.js +1 -0
- package/dist/assets/classDiagram-v2-EOCWNBFH-Bo96wh-7.js +1 -0
- package/dist/assets/{cose-bilkent-JH36ORCC-DOhYmYxe.js → cose-bilkent-JH36ORCC-ByxdBNNQ.js} +1 -1
- package/dist/assets/{cynefin-VYW2F7L2-UDH0NYTi.js → cynefin-VYW2F7L2-DUAFhLA3.js} +1 -1
- package/dist/assets/{cynefinDiagram-TSTJHNR4-DnGUiXaD.js → cynefinDiagram-TSTJHNR4-B2Kk-MGz.js} +1 -1
- package/dist/assets/{dagre-VKFMJZFB-BQ6C_GWY.js → dagre-VKFMJZFB-DFCRJf_G.js} +1 -1
- package/dist/assets/{diagram-FQU43EPY-CREz1035.js → diagram-FQU43EPY-wYMGeAdu.js} +1 -1
- package/dist/assets/{diagram-G47NLZAW-9Jr7GbsE.js → diagram-G47NLZAW-rgjw8pQP.js} +1 -1
- package/dist/assets/{diagram-NH7WQ7WH-DtN5x74X.js → diagram-NH7WQ7WH-BpZASdpx.js} +1 -1
- package/dist/assets/{diagram-OA4YK3LP-UuOi8FAP.js → diagram-OA4YK3LP-CakEHPj1.js} +1 -1
- package/dist/assets/{diagram-WEI45ONY-CWiUGHfM.js → diagram-WEI45ONY-BgF2sKn7.js} +1 -1
- package/dist/assets/{ebnfDiagram-CCIWWBDH-3eMfQx1G.js → ebnfDiagram-CCIWWBDH-D0Kf9aJj.js} +1 -1
- package/dist/assets/{erDiagram-Q63AITRT-DYzSgp8n.js → erDiagram-Q63AITRT-sKvL1I4l.js} +1 -1
- package/dist/assets/{flowDiagram-23GEKE2U-DBLDGx8O.js → flowDiagram-23GEKE2U-DgS4-v6l.js} +1 -1
- package/dist/assets/{ganttDiagram-NO4QXBWP-Dv7Uyp2U.js → ganttDiagram-NO4QXBWP-DxkYU6kL.js} +1 -1
- package/dist/assets/{gitGraphDiagram-IHSO6WYX-leeHrOrC.js → gitGraphDiagram-IHSO6WYX-BP9sXgF1.js} +1 -1
- package/dist/assets/{index-lHF2zw41.js → index-B5rQe41U.js} +1 -1
- package/dist/assets/index-BLo9z-jE.js +1 -0
- package/dist/assets/{index-CFiAyn9P.js → index-CcSdiUnJ.js} +3 -3
- package/dist/assets/index-Cf8OqaQe.js +1 -0
- package/dist/assets/{index-Bnk8mX62.js → index-Ct8OBbU_.js} +1 -1
- package/dist/assets/{index-fP8UTiVP.js → index-D1S-PYLr.js} +2 -2
- package/dist/assets/{index-DY2LUL1D.js → index-DCRtV-sQ.js} +1 -1
- package/dist/assets/{index-CtGcfchE.js → index-DSC1km47.js} +1 -1
- package/dist/assets/{index-Cpf2LUXa.js → index-MzszL_qp.js} +4 -4
- package/dist/assets/{infoDiagram-FWYZ7A6U-B23FL64X.js → infoDiagram-FWYZ7A6U-ODz77wW8.js} +1 -1
- package/dist/assets/{ishikawaDiagram-FXEZZL3T-BPsjNXWV.js → ishikawaDiagram-FXEZZL3T-B-zkHET8.js} +1 -1
- package/dist/assets/{journeyDiagram-5HDEW3XC-3jqJYemY.js → journeyDiagram-5HDEW3XC-3-0ZxNaY.js} +1 -1
- package/dist/assets/{kanban-definition-HUTT4EX6-rHfIfWqi.js → kanban-definition-HUTT4EX6-6TXFrj9B.js} +1 -1
- package/dist/assets/{linear-CvofFTBe.js → linear-D4INeZan.js} +1 -1
- package/dist/assets/main-BNsRZCIt.css +1 -0
- package/dist/assets/{main-ts-uUN3p.js → main-awRasOMk.js} +142 -139
- package/dist/assets/{mermaid.core-BuuQCwr7.js → mermaid.core-VfI8B8Nu.js} +4 -4
- package/dist/assets/{mindmap-definition-LN4V7U3C-CdMbsouS.js → mindmap-definition-LN4V7U3C-BtnLuWah.js} +1 -1
- package/dist/assets/{pegDiagram-2B236MQR-Cl7drnf-.js → pegDiagram-2B236MQR-C7u_WQ8m.js} +1 -1
- package/dist/assets/{pieDiagram-ENE6RG2P-CrfvJw6b.js → pieDiagram-ENE6RG2P-FP2oK2aq.js} +1 -1
- package/dist/assets/{quadrantDiagram-ABIIQ3AL-D6Z7VqX6.js → quadrantDiagram-ABIIQ3AL-C7R49DqX.js} +1 -1
- package/dist/assets/{railroadDiagram-RFXS5EU6-DBhqTpwY.js → railroadDiagram-RFXS5EU6-8Y-09cJG.js} +1 -1
- package/dist/assets/{requirementDiagram-TGXJPOKE-DjYygiNl.js → requirementDiagram-TGXJPOKE-CuLFvAIT.js} +1 -1
- package/dist/assets/{sankeyDiagram-HTMAVEWB-Cb8su5H6.js → sankeyDiagram-HTMAVEWB-C83wAget.js} +1 -1
- package/dist/assets/{sequenceDiagram-DBY2YBRQ-DH326uFa.js → sequenceDiagram-DBY2YBRQ-2bX54qCB.js} +1 -1
- package/dist/assets/{sizeCapture-X5ZJPWSS-BA-8zei3.js → sizeCapture-X5ZJPWSS-CwcxMidm.js} +1 -1
- package/dist/assets/{stateDiagram-2N3HPSRC-DM412fD5.js → stateDiagram-2N3HPSRC-BRLu0Nh3.js} +1 -1
- package/dist/assets/stateDiagram-v2-6OUMAXLB-HDFSwLIz.js +1 -0
- package/dist/assets/{swimlanes-5IMT3BWC-BouPQ02z.js → swimlanes-5IMT3BWC-0ZHVD_6W.js} +2 -2
- package/dist/assets/swimlanesDiagram-G3AALYLV-DIR86oWK.js +8 -0
- package/dist/assets/{timeline-definition-FHXFAJF6-CHbeQn6a.js → timeline-definition-FHXFAJF6-mZxmuno3.js} +1 -1
- package/dist/assets/{vennDiagram-L72KCM5P-HSzYjTi7.js → vennDiagram-L72KCM5P-529a4wgg.js} +1 -1
- package/dist/assets/{wardleyDiagram-EHGQE667-DvrdmzxK.js → wardleyDiagram-EHGQE667-DsYcsK_D.js} +1 -1
- package/dist/assets/{web-BvtZvOK9.js → web-CUfujGZR.js} +1 -1
- package/dist/assets/{web-DT7q-yw_.js → web-CzSim3jK.js} +1 -1
- package/dist/assets/{web-DwD7eqxR.js → web-VJlfgT9G.js} +1 -1
- package/dist/assets/{xychartDiagram-FW5EYKEG-DgSgkHvg.js → xychartDiagram-FW5EYKEG-DImLqIu4.js} +1 -1
- package/dist/build-info.json +1 -1
- package/dist/index.html +2 -2
- package/dist/locales/de/terminal.json +2 -1
- package/dist/locales/en/terminal.json +3 -1
- package/dist/locales/es/terminal.json +3 -1
- package/dist/locales/fr/terminal.json +2 -1
- package/dist/locales/hi/terminal.json +2 -1
- package/dist/locales/it/terminal.json +2 -1
- package/dist/locales/ja/terminal.json +2 -1
- package/dist/locales/pt/terminal.json +2 -1
- package/dist/locales/ru/terminal.json +2 -1
- package/dist/locales/zh-CN/terminal.json +2 -1
- package/dist/src/packages/server/claude/backend.js +26 -2
- package/dist/src/packages/server/claude/interactive/interactive-backend-args.js +1 -1
- package/dist/src/packages/server/claude/interactive/interactive-jsonl-translator.js +20 -0
- package/dist/src/packages/server/claude/interactive/interactive-jsonl-watcher.js +9 -1
- package/dist/src/packages/server/claude/runner/process-lifecycle.js +3 -1
- package/dist/src/packages/server/claude/runner/restart-policy.js +65 -2
- package/dist/src/packages/server/claude/runner/stdout-pipeline.js +10 -1
- package/dist/src/packages/server/claude/runner/watchdog.js +29 -0
- package/dist/src/packages/server/claude/session-loader.js +641 -45
- package/dist/src/packages/server/claude/types.js +10 -1
- package/dist/src/packages/server/codex/app-server/app-server-event-adapter.js +27 -11
- package/dist/src/packages/server/grok/session-watcher.js +60 -0
- package/dist/src/packages/server/routes/agents.js +26 -0
- package/dist/src/packages/server/routes/sessions.js +9 -4
- package/dist/src/packages/server/routes/system.js +16 -0
- package/dist/src/packages/server/services/agent-service.js +10 -0
- package/dist/src/packages/server/services/codex-usage-service.js +141 -2
- package/dist/src/packages/server/services/runtime-service.js +22 -0
- package/dist/src/packages/server/websocket/listeners/runtime-listeners.js +27 -0
- package/dist/src/packages/shared/model-fallback.js +203 -0
- package/dist/src/packages/shared/types.js +1 -0
- package/package.json +1 -1
- package/dist/assets/channel-BgBFpxsT.js +0 -1
- package/dist/assets/classDiagram-OUVF2IWQ-CXHgnAgF.js +0 -1
- package/dist/assets/classDiagram-v2-EOCWNBFH-CXHgnAgF.js +0 -1
- package/dist/assets/index-BrN1fSea.js +0 -1
- package/dist/assets/index-jlRDILHc.js +0 -1
- package/dist/assets/main-CZA5Uyu6.css +0 -1
- package/dist/assets/stateDiagram-v2-6OUMAXLB-BZJwjeVk.js +0 -1
- package/dist/assets/swimlanesDiagram-G3AALYLV-BRQaNmpd.js +0 -8
|
@@ -9,6 +9,7 @@ import * as fs from 'fs';
|
|
|
9
9
|
import * as path from 'path';
|
|
10
10
|
import * as os from 'os';
|
|
11
11
|
import * as readline from 'readline';
|
|
12
|
+
import { spawn, spawnSync } from 'child_process';
|
|
12
13
|
import Database from 'better-sqlite3';
|
|
13
14
|
import { createLogger } from '../utils/logger.js';
|
|
14
15
|
import { materializeCodexGeneratedImage } from '../codex/generated-image.js';
|
|
@@ -671,6 +672,19 @@ function extractClaudeMessageText(message) {
|
|
|
671
672
|
}
|
|
672
673
|
return '';
|
|
673
674
|
}
|
|
675
|
+
const sessionHeaderCache = new Map();
|
|
676
|
+
const SESSION_HEADER_CACHE_MAX = 8000;
|
|
677
|
+
async function readSessionHeaderCached(filePath, sizeBytes) {
|
|
678
|
+
const cached = sessionHeaderCache.get(filePath);
|
|
679
|
+
if (cached && ((cached.cwd && cached.firstPrompt) || cached.sizeAtRead === sizeBytes)) {
|
|
680
|
+
return cached;
|
|
681
|
+
}
|
|
682
|
+
const header = await readSessionHeader(filePath);
|
|
683
|
+
if (sessionHeaderCache.size >= SESSION_HEADER_CACHE_MAX)
|
|
684
|
+
sessionHeaderCache.clear();
|
|
685
|
+
sessionHeaderCache.set(filePath, { ...header, sizeAtRead: sizeBytes });
|
|
686
|
+
return header;
|
|
687
|
+
}
|
|
674
688
|
/**
|
|
675
689
|
* Read the first up-to-`maxLines` JSONL records of a session file. Used to
|
|
676
690
|
* cheaply recover `cwd` and the first user prompt without parsing the whole
|
|
@@ -777,7 +791,7 @@ export async function listAllSessions(options) {
|
|
|
777
791
|
const idx = cursor++;
|
|
778
792
|
const item = limited[idx];
|
|
779
793
|
const filePath = path.join(PROJECTS_DIR, item.projectDir, `${item.sessionId}.jsonl`);
|
|
780
|
-
const header = await
|
|
794
|
+
const header = await readSessionHeaderCached(filePath, item.sizeBytes);
|
|
781
795
|
item.projectPath = header.cwd;
|
|
782
796
|
item.firstPrompt = header.firstPrompt;
|
|
783
797
|
if (options?.includeMessageCount) {
|
|
@@ -805,9 +819,484 @@ export async function listAllSessions(options) {
|
|
|
805
819
|
await Promise.all(workers);
|
|
806
820
|
return limited;
|
|
807
821
|
}
|
|
822
|
+
// ── Global search fast path ──────────────────────────────────────────────────
|
|
823
|
+
/** Per-file scan stops counting past this — enough signal for display/ranking,
|
|
824
|
+
* and it stops burning IO on a hot file with thousands of hits. */
|
|
825
|
+
const SEARCH_MATCHES_PER_FILE_CAP = 500;
|
|
826
|
+
const SEARCH_FILE_CACHE_MAX = 6000;
|
|
827
|
+
/** Refinement-answer bounds: matching lines are only retained when there are
|
|
828
|
+
* few and they're short — enough to answer every follow-up keystroke from
|
|
829
|
+
* memory for the typical file, without letting the cache balloon. */
|
|
830
|
+
const SEARCH_CACHE_LINES_MAX = 60;
|
|
831
|
+
const SEARCH_CACHE_LINE_LEN_MAX = 4096;
|
|
832
|
+
/** filePath → recent computed outcomes for that file (newest first, small cap).
|
|
833
|
+
* Multiple entries matter for real typing: "vir"→"virtu"→"virtual" then a
|
|
834
|
+
* backspace must hit the still-valid "virtu" entry instead of rescanning. */
|
|
835
|
+
const searchFileCache = new Map();
|
|
836
|
+
const SEARCH_CACHE_ENTRIES_PER_FILE = 3;
|
|
837
|
+
/** Bumped by every searchAllSessions call; in-flight workers of an older
|
|
838
|
+
* generation stop scheduling new files (their partial response is discarded by
|
|
839
|
+
* the client's seq guard anyway). */
|
|
840
|
+
let searchAllSessionsGeneration = 0;
|
|
841
|
+
/**
|
|
842
|
+
* Decide whether a cached per-file result answers `queryLower` without
|
|
843
|
+
* re-reading the file. Exact same query on an unchanged file → reuse. A
|
|
844
|
+
* ZERO-match entry also answers any longer query that CONTAINS the cached one
|
|
845
|
+
* (if "scro" appears nowhere in the file, "scroll" cannot either) — which is
|
|
846
|
+
* exactly what happens on every keystroke while the user types, so refining a
|
|
847
|
+
* query only re-reads the files that were still matching.
|
|
848
|
+
*/
|
|
849
|
+
/** Head answer (count/snippet/lines) derivable from a cache entry for
|
|
850
|
+
* `queryLower` WITHOUT reading the bytes the entry covers. */
|
|
851
|
+
function headAnswerFrom(entry, queryLower) {
|
|
852
|
+
if (entry.query === queryLower) {
|
|
853
|
+
return { totalMatches: entry.totalMatches, snippet: entry.snippet, matchingLines: entry.matchingLines };
|
|
854
|
+
}
|
|
855
|
+
if (!queryLower.includes(entry.query))
|
|
856
|
+
return null;
|
|
857
|
+
if (entry.totalMatches === 0)
|
|
858
|
+
return { totalMatches: 0, snippet: '', matchingLines: [] };
|
|
859
|
+
// Refined query: its matches are a subset of the cached query's matching
|
|
860
|
+
// lines — when we have ALL of them, answer by re-testing just those lines.
|
|
861
|
+
if (entry.matchingLines) {
|
|
862
|
+
const matcher = new RegExp(escapeRegExp(queryLower), 'i');
|
|
863
|
+
const matchingLines = [];
|
|
864
|
+
let snippet = '';
|
|
865
|
+
for (const line of entry.matchingLines) {
|
|
866
|
+
if (!matcher.test(line))
|
|
867
|
+
continue;
|
|
868
|
+
matchingLines.push(line);
|
|
869
|
+
if (!snippet)
|
|
870
|
+
snippet = deriveSearchSnippet(line, queryLower);
|
|
871
|
+
}
|
|
872
|
+
return { totalMatches: matchingLines.length, snippet, matchingLines };
|
|
873
|
+
}
|
|
874
|
+
return null;
|
|
875
|
+
}
|
|
808
876
|
/**
|
|
809
|
-
*
|
|
810
|
-
*
|
|
877
|
+
* Decide how to answer `queryLower` for one file given its cached outcomes:
|
|
878
|
+
* - unchanged file + answerable from an entry → 'reuse' (no IO at all);
|
|
879
|
+
* - grown file (sessions are append-only) + answerable head → 'tail', scan
|
|
880
|
+
* only the appended bytes — the actively-streaming sessions are the newest
|
|
881
|
+
* files, scanned first on every keystroke, and this is what keeps them from
|
|
882
|
+
* being re-read whole each time;
|
|
883
|
+
* - anything else (shrunk/rewritten file, underivable head) → 'full'.
|
|
884
|
+
* Entries are tried newest-first; the freshest answerable one wins.
|
|
885
|
+
*/
|
|
886
|
+
export function planFileSearch(entries, queryLower, mtimeMs, sizeBytes) {
|
|
887
|
+
if (!entries || entries.length === 0)
|
|
888
|
+
return { kind: 'full' };
|
|
889
|
+
let tailPlan = null;
|
|
890
|
+
for (const entry of entries) {
|
|
891
|
+
const head = headAnswerFrom(entry, queryLower);
|
|
892
|
+
if (!head)
|
|
893
|
+
continue;
|
|
894
|
+
if (entry.mtimeMs === mtimeMs && entry.sizeBytes === sizeBytes) {
|
|
895
|
+
return { kind: 'reuse', totalMatches: head.totalMatches, snippet: head.snippet };
|
|
896
|
+
}
|
|
897
|
+
if (entry.scannedBytes !== undefined && sizeBytes >= entry.scannedBytes) {
|
|
898
|
+
// Prefer the entry that leaves the smallest tail to scan.
|
|
899
|
+
if (tailPlan === null || (tailPlan.kind === 'tail' && entry.scannedBytes > tailPlan.startByte)) {
|
|
900
|
+
tailPlan = { kind: 'tail', startByte: entry.scannedBytes, head };
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
return tailPlan ?? { kind: 'full' };
|
|
905
|
+
}
|
|
906
|
+
/** Insert/replace an outcome in a file's entry list (newest first, capped). */
|
|
907
|
+
function storeSearchEntry(filePath, entry) {
|
|
908
|
+
if (searchFileCache.size >= SEARCH_FILE_CACHE_MAX)
|
|
909
|
+
searchFileCache.clear();
|
|
910
|
+
const existing = searchFileCache.get(filePath) ?? [];
|
|
911
|
+
const kept = existing.filter((e) => e.query !== entry.query);
|
|
912
|
+
kept.unshift(entry);
|
|
913
|
+
searchFileCache.set(filePath, kept.slice(0, SEARCH_CACHE_ENTRIES_PER_FILE));
|
|
914
|
+
}
|
|
915
|
+
function escapeRegExp(text) {
|
|
916
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
917
|
+
}
|
|
918
|
+
/** Snippet quality ranks — higher wins. The Session Finder shows the snippet
|
|
919
|
+
* to a human: real conversation text beats tool chatter beats raw JSON. */
|
|
920
|
+
const SNIPPET_RANK_RAW = 1;
|
|
921
|
+
const SNIPPET_RANK_TOOL = 2;
|
|
922
|
+
const SNIPPET_RANK_MESSAGE = 3;
|
|
923
|
+
/** Readable one-liner for a tool_use / tool_result content block, if any. */
|
|
924
|
+
function firstToolText(content) {
|
|
925
|
+
if (!Array.isArray(content))
|
|
926
|
+
return '';
|
|
927
|
+
for (const block of content) {
|
|
928
|
+
if (!block || typeof block !== 'object')
|
|
929
|
+
continue;
|
|
930
|
+
const b = block;
|
|
931
|
+
if (b.type === 'tool_use') {
|
|
932
|
+
const input = b.input;
|
|
933
|
+
const arg = input && (input.command ?? input.file_path ?? input.path ?? input.pattern ?? input.query ?? input.url ?? input.description);
|
|
934
|
+
const argText = typeof arg === 'string' ? arg : input ? JSON.stringify(input) : '';
|
|
935
|
+
return `${b.name ?? 'tool'}: ${argText}`;
|
|
936
|
+
}
|
|
937
|
+
if (b.type === 'tool_result') {
|
|
938
|
+
const inner = b.content;
|
|
939
|
+
if (typeof inner === 'string' && inner.trim())
|
|
940
|
+
return inner;
|
|
941
|
+
if (Array.isArray(inner)) {
|
|
942
|
+
for (const part of inner) {
|
|
943
|
+
const text = part?.text;
|
|
944
|
+
if (typeof text === 'string' && text.trim())
|
|
945
|
+
return text;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return '';
|
|
951
|
+
}
|
|
952
|
+
/** Best human-readable text for a matched JSONL line, with its quality rank. */
|
|
953
|
+
function extractReadableLineText(line) {
|
|
954
|
+
try {
|
|
955
|
+
const obj = JSON.parse(line);
|
|
956
|
+
if (obj.type === 'user' || obj.type === 'assistant') {
|
|
957
|
+
const text = extractClaudeMessageText(obj.message);
|
|
958
|
+
if (text.trim())
|
|
959
|
+
return { text, rank: SNIPPET_RANK_MESSAGE };
|
|
960
|
+
const toolText = firstToolText(obj.message?.content);
|
|
961
|
+
if (toolText)
|
|
962
|
+
return { text: toolText, rank: SNIPPET_RANK_TOOL };
|
|
963
|
+
}
|
|
964
|
+
if (obj.type === 'queue-operation' && typeof obj.content === 'string' && obj.content.trim()) {
|
|
965
|
+
return { text: obj.content, rank: SNIPPET_RANK_MESSAGE };
|
|
966
|
+
}
|
|
967
|
+
if (typeof obj.summary === 'string' && obj.summary.trim()) {
|
|
968
|
+
return { text: obj.summary, rank: SNIPPET_RANK_TOOL };
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
catch { /* not JSON — treat as raw */ }
|
|
972
|
+
return { text: line, rank: SNIPPET_RANK_RAW };
|
|
973
|
+
}
|
|
974
|
+
/** Collapse to one line and window around the first query occurrence, so the
|
|
975
|
+
* user sees the matched context instead of the start of a long message. */
|
|
976
|
+
function windowSnippet(text, queryLower) {
|
|
977
|
+
const collapsed = text.replace(/\s+/g, ' ').trim();
|
|
978
|
+
if (collapsed.length <= SEARCH_SNIPPET_MAX_LEN)
|
|
979
|
+
return collapsed;
|
|
980
|
+
const at = queryLower ? collapsed.toLowerCase().indexOf(queryLower) : -1;
|
|
981
|
+
if (at <= 80)
|
|
982
|
+
return truncate(collapsed, SEARCH_SNIPPET_MAX_LEN);
|
|
983
|
+
const start = at - 80;
|
|
984
|
+
const end = Math.min(collapsed.length, start + SEARCH_SNIPPET_MAX_LEN - 2);
|
|
985
|
+
return `…${collapsed.slice(start, end)}${end < collapsed.length ? '…' : ''}`;
|
|
986
|
+
}
|
|
987
|
+
/** Clean display snippet from a matching JSONL line (message text preferred). */
|
|
988
|
+
function deriveSearchSnippet(line, queryLower) {
|
|
989
|
+
return windowSnippet(extractReadableLineText(line).text, queryLower);
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Scan one session file for a case-insensitive substring match.
|
|
993
|
+
*
|
|
994
|
+
* Reads 1 MB chunks and runs ONE compiled case-insensitive regex over each
|
|
995
|
+
* chunk; a chunk is split into lines only when it matched. The previous
|
|
996
|
+
* readline implementation allocated a string per line PLUS a lowercased copy
|
|
997
|
+
* of every line — ~2× the corpus in throwaway allocations per search (the
|
|
998
|
+
* session corpus here is measured in GBs, and single lines carrying base64
|
|
999
|
+
* attachments run to hundreds of KB).
|
|
1000
|
+
*/
|
|
1001
|
+
export async function scanSessionFileForQuery(filePath, query, startByte,
|
|
1002
|
+
/** Also collect the first N matching lines (windowed around the match when
|
|
1003
|
+
* huge) regardless of the refinement-cache retention rules — used by the
|
|
1004
|
+
* preview's raw-match fallback, which must SHOW hits that live outside the
|
|
1005
|
+
* parsed conversation. */
|
|
1006
|
+
keepFirst) {
|
|
1007
|
+
const matcher = new RegExp(escapeRegExp(query), 'i');
|
|
1008
|
+
const queryLower = query.toLowerCase();
|
|
1009
|
+
let totalMatches = 0;
|
|
1010
|
+
let snippet = '';
|
|
1011
|
+
let snippetRank = 0;
|
|
1012
|
+
let snippetAttempts = 0;
|
|
1013
|
+
let reachedEof = true;
|
|
1014
|
+
const firstLines = keepFirst ? [] : undefined;
|
|
1015
|
+
const FIRST_LINE_WINDOW = 2400;
|
|
1016
|
+
// Matching lines retained for refinement answers (see SearchFileCacheEntry);
|
|
1017
|
+
// null once the file exceeds the retention bounds.
|
|
1018
|
+
let matchingLines = [];
|
|
1019
|
+
// Cap how much un-newlined text accumulates (giant single lines): scan and
|
|
1020
|
+
// drop, keeping a query-sized overlap so a match can't fall through the cut.
|
|
1021
|
+
// Counts may be ±1 on such lines — irrelevant for display.
|
|
1022
|
+
const MAX_CARRY = 4 * 1024 * 1024;
|
|
1023
|
+
let carry = '';
|
|
1024
|
+
const countMatchingLines = (text) => {
|
|
1025
|
+
if (!matcher.test(text))
|
|
1026
|
+
return;
|
|
1027
|
+
for (const line of text.split('\n')) {
|
|
1028
|
+
if (!line || !matcher.test(line))
|
|
1029
|
+
continue;
|
|
1030
|
+
totalMatches++;
|
|
1031
|
+
if (firstLines && keepFirst && firstLines.length < keepFirst) {
|
|
1032
|
+
if (line.length <= FIRST_LINE_WINDOW) {
|
|
1033
|
+
firstLines.push(line);
|
|
1034
|
+
}
|
|
1035
|
+
else {
|
|
1036
|
+
const at = line.toLowerCase().indexOf(queryLower);
|
|
1037
|
+
const start = Math.max(0, at - 400);
|
|
1038
|
+
const end = Math.min(line.length, start + FIRST_LINE_WINDOW);
|
|
1039
|
+
firstLines.push(`${start > 0 ? '…' : ''}${line.slice(start, end)}${end < line.length ? '…' : ''}`);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
// Upgrade the snippet until real conversation text is found (bounded:
|
|
1043
|
+
// each attempt JSON-parses one matched line).
|
|
1044
|
+
if (snippetRank < SNIPPET_RANK_MESSAGE && snippetAttempts < 30) {
|
|
1045
|
+
snippetAttempts++;
|
|
1046
|
+
const readable = extractReadableLineText(line);
|
|
1047
|
+
if (readable.rank > snippetRank) {
|
|
1048
|
+
snippetRank = readable.rank;
|
|
1049
|
+
snippet = windowSnippet(readable.text, queryLower);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
if (matchingLines) {
|
|
1053
|
+
// Partial giant-line slices are also unsafe to retain, but they always
|
|
1054
|
+
// exceed the length bound anyway.
|
|
1055
|
+
if (line.length > SEARCH_CACHE_LINE_LEN_MAX || matchingLines.length >= SEARCH_CACHE_LINES_MAX) {
|
|
1056
|
+
matchingLines = null;
|
|
1057
|
+
}
|
|
1058
|
+
else {
|
|
1059
|
+
matchingLines.push(line);
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
};
|
|
1064
|
+
const stream = fs.createReadStream(filePath, {
|
|
1065
|
+
encoding: 'utf-8',
|
|
1066
|
+
highWaterMark: 1 << 20,
|
|
1067
|
+
start: startByte,
|
|
1068
|
+
});
|
|
1069
|
+
try {
|
|
1070
|
+
for await (const chunk of stream) {
|
|
1071
|
+
const text = carry + chunk;
|
|
1072
|
+
const lastNewline = text.lastIndexOf('\n');
|
|
1073
|
+
if (lastNewline === -1) {
|
|
1074
|
+
if (text.length > MAX_CARRY) {
|
|
1075
|
+
countMatchingLines(text);
|
|
1076
|
+
const overlap = Math.max(query.length - 1, 0);
|
|
1077
|
+
carry = overlap > 0 ? text.slice(-overlap) : '';
|
|
1078
|
+
}
|
|
1079
|
+
else {
|
|
1080
|
+
carry = text;
|
|
1081
|
+
}
|
|
1082
|
+
continue;
|
|
1083
|
+
}
|
|
1084
|
+
countMatchingLines(text.slice(0, lastNewline));
|
|
1085
|
+
carry = text.slice(lastNewline + 1);
|
|
1086
|
+
if (totalMatches >= SEARCH_MATCHES_PER_FILE_CAP) {
|
|
1087
|
+
reachedEof = false;
|
|
1088
|
+
break;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
finally {
|
|
1093
|
+
stream.destroy();
|
|
1094
|
+
}
|
|
1095
|
+
if (reachedEof && carry)
|
|
1096
|
+
countMatchingLines(carry);
|
|
1097
|
+
return {
|
|
1098
|
+
totalMatches,
|
|
1099
|
+
snippet,
|
|
1100
|
+
// Only a COMPLETE set answers refinements (a capped scan saw a subset).
|
|
1101
|
+
matchingLines: matchingLines !== null && reachedEof ? matchingLines : undefined,
|
|
1102
|
+
firstLines,
|
|
1103
|
+
reachedEof,
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
// ── ripgrep engine ───────────────────────────────────────────────────────────
|
|
1107
|
+
// When rg is on PATH it does the cold scanning: SIMD + all cores make it
|
|
1108
|
+
// ~10-40× the JS chunk scanner on this corpus (measured 0.1-0.5s vs 2-4.5s).
|
|
1109
|
+
// The JS scanner remains the fallback engine and the tail-resume path.
|
|
1110
|
+
let rgAvailableCache = null;
|
|
1111
|
+
function isRgAvailable() {
|
|
1112
|
+
if (rgAvailableCache === null) {
|
|
1113
|
+
try {
|
|
1114
|
+
rgAvailableCache = spawnSync('rg', ['--version'], { timeout: 3000 }).status === 0;
|
|
1115
|
+
}
|
|
1116
|
+
catch {
|
|
1117
|
+
rgAvailableCache = false;
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
return rgAvailableCache;
|
|
1121
|
+
}
|
|
1122
|
+
/** Run rg and return stdout ('' on no matches). Null = rg failed to run. */
|
|
1123
|
+
function runRg(args, timeoutMs = 20_000) {
|
|
1124
|
+
return new Promise((resolve) => {
|
|
1125
|
+
const child = spawn('rg', args, { stdio: ['ignore', 'pipe', 'ignore'] });
|
|
1126
|
+
const chunks = [];
|
|
1127
|
+
const timer = setTimeout(() => child.kill('SIGKILL'), timeoutMs);
|
|
1128
|
+
child.stdout.on('data', (c) => chunks.push(c));
|
|
1129
|
+
child.on('error', () => {
|
|
1130
|
+
clearTimeout(timer);
|
|
1131
|
+
resolve(null);
|
|
1132
|
+
});
|
|
1133
|
+
child.on('close', (code) => {
|
|
1134
|
+
clearTimeout(timer);
|
|
1135
|
+
// rg exits 0 with matches, 1 with none — both are success here.
|
|
1136
|
+
if (code === 0 || code === 1)
|
|
1137
|
+
resolve(Buffer.concat(chunks).toString('utf-8'));
|
|
1138
|
+
else
|
|
1139
|
+
resolve(null);
|
|
1140
|
+
});
|
|
1141
|
+
});
|
|
1142
|
+
}
|
|
1143
|
+
/** Parse `rg -c` output (`path:count` per line) into a path → count map. */
|
|
1144
|
+
export function parseRgCounts(output) {
|
|
1145
|
+
const counts = new Map();
|
|
1146
|
+
for (const line of output.split('\n')) {
|
|
1147
|
+
if (!line)
|
|
1148
|
+
continue;
|
|
1149
|
+
const sep = line.lastIndexOf(':');
|
|
1150
|
+
if (sep <= 0)
|
|
1151
|
+
continue;
|
|
1152
|
+
const count = Number(line.slice(sep + 1));
|
|
1153
|
+
if (Number.isFinite(count) && count > 0)
|
|
1154
|
+
counts.set(line.slice(0, sep), count);
|
|
1155
|
+
}
|
|
1156
|
+
return counts;
|
|
1157
|
+
}
|
|
1158
|
+
/** Parse plain rg output (`path:matched line text`) into path → sample lines.
|
|
1159
|
+
* Our absolute session paths contain no ':', so the first colon splits.
|
|
1160
|
+
* Lines rg replaced under --max-columns ("[Omitted long matching line]") are
|
|
1161
|
+
* dropped — they'd otherwise leak into snippets as literal marker text. */
|
|
1162
|
+
export function parseRgSampleLines(output) {
|
|
1163
|
+
const byPath = new Map();
|
|
1164
|
+
for (const line of output.split('\n')) {
|
|
1165
|
+
if (!line.startsWith('/'))
|
|
1166
|
+
continue;
|
|
1167
|
+
const sep = line.indexOf(':');
|
|
1168
|
+
if (sep <= 0)
|
|
1169
|
+
continue;
|
|
1170
|
+
const filePath = line.slice(0, sep);
|
|
1171
|
+
const text = line.slice(sep + 1);
|
|
1172
|
+
if (text.startsWith('[Omitted long'))
|
|
1173
|
+
continue;
|
|
1174
|
+
const lines = byPath.get(filePath) ?? [];
|
|
1175
|
+
if (lines.length < 8)
|
|
1176
|
+
lines.push(text);
|
|
1177
|
+
byPath.set(filePath, lines);
|
|
1178
|
+
}
|
|
1179
|
+
return byPath;
|
|
1180
|
+
}
|
|
1181
|
+
/** How many sample lines pass B asks rg for, per file. A cache entry whose
|
|
1182
|
+
* totalMatches is within this bound therefore retains its COMPLETE match set
|
|
1183
|
+
* and can answer refined queries from memory. */
|
|
1184
|
+
const RG_SAMPLE_LINES_PER_FILE = 5;
|
|
1185
|
+
/**
|
|
1186
|
+
* ripgrep-engined search over the candidate sessions. Two passes:
|
|
1187
|
+
* A) one `rg -c` over every file the cache can't answer → per-file counts
|
|
1188
|
+
* (also learns which files have ZERO matches — that knowledge prunes
|
|
1189
|
+
* every refined query later);
|
|
1190
|
+
* B) one `rg -m N` over just the displayed top files → sample lines for
|
|
1191
|
+
* readable snippets and (small files) the refinement cache.
|
|
1192
|
+
* Returns null when rg misbehaves so the caller can use the JS engine.
|
|
1193
|
+
*/
|
|
1194
|
+
async function searchViaRipgrep(candidates, query, queryLower, limit, generation) {
|
|
1195
|
+
const reused = [];
|
|
1196
|
+
const toScan = [];
|
|
1197
|
+
for (const session of candidates) {
|
|
1198
|
+
const filePath = path.join(PROJECTS_DIR, session.projectDir, `${session.sessionId}.jsonl`);
|
|
1199
|
+
const mtimeMs = session.lastModified.getTime();
|
|
1200
|
+
const plan = planFileSearch(searchFileCache.get(filePath), queryLower, mtimeMs, session.sizeBytes);
|
|
1201
|
+
if (plan.kind === 'reuse') {
|
|
1202
|
+
if (plan.totalMatches > 0)
|
|
1203
|
+
reused.push({ session, filePath, totalMatches: plan.totalMatches, snippet: plan.snippet });
|
|
1204
|
+
}
|
|
1205
|
+
else {
|
|
1206
|
+
toScan.push({ session, filePath, mtimeMs });
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
let counts = new Map();
|
|
1210
|
+
if (toScan.length > 0) {
|
|
1211
|
+
const output = await runRg([
|
|
1212
|
+
'-i', '--fixed-strings', '-a', '--no-config', '--no-messages', '-c', '--',
|
|
1213
|
+
query, ...toScan.map((f) => f.filePath),
|
|
1214
|
+
]);
|
|
1215
|
+
if (output === null)
|
|
1216
|
+
return null;
|
|
1217
|
+
if (generation !== searchAllSessionsGeneration)
|
|
1218
|
+
return []; // superseded — client discards
|
|
1219
|
+
counts = parseRgCounts(output);
|
|
1220
|
+
}
|
|
1221
|
+
const all = [
|
|
1222
|
+
...reused,
|
|
1223
|
+
...toScan
|
|
1224
|
+
.filter((f) => (counts.get(f.filePath) ?? 0) > 0)
|
|
1225
|
+
.map((f) => ({ session: f.session, filePath: f.filePath, totalMatches: counts.get(f.filePath), snippet: '' })),
|
|
1226
|
+
];
|
|
1227
|
+
all.sort((a, b) => b.session.lastModified.getTime() - a.session.lastModified.getTime());
|
|
1228
|
+
const top = all.slice(0, limit);
|
|
1229
|
+
const needLines = top.filter((h) => !h.snippet).map((h) => h.filePath);
|
|
1230
|
+
let samples = new Map();
|
|
1231
|
+
if (needLines.length > 0) {
|
|
1232
|
+
const output = await runRg([
|
|
1233
|
+
'-i', '--fixed-strings', '-a', '--no-config', '--no-messages', '--with-filename',
|
|
1234
|
+
'--max-columns', '4096', '-m', String(RG_SAMPLE_LINES_PER_FILE), '--',
|
|
1235
|
+
query, ...needLines,
|
|
1236
|
+
]);
|
|
1237
|
+
if (output !== null)
|
|
1238
|
+
samples = parseRgSampleLines(output);
|
|
1239
|
+
}
|
|
1240
|
+
for (const hit of top) {
|
|
1241
|
+
if (hit.snippet)
|
|
1242
|
+
continue;
|
|
1243
|
+
let bestRank = 0;
|
|
1244
|
+
for (const line of samples.get(hit.filePath) ?? []) {
|
|
1245
|
+
const readable = extractReadableLineText(line);
|
|
1246
|
+
if (readable.rank > bestRank) {
|
|
1247
|
+
bestRank = readable.rank;
|
|
1248
|
+
hit.snippet = windowSnippet(readable.text, queryLower);
|
|
1249
|
+
}
|
|
1250
|
+
if (bestRank >= SNIPPET_RANK_MESSAGE)
|
|
1251
|
+
break;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
// Refresh the cache with everything this run learned — zero-match files
|
|
1255
|
+
// included (that's the refinement pruning for the next keystrokes).
|
|
1256
|
+
const topByPath = new Map(top.map((h) => [h.filePath, h]));
|
|
1257
|
+
for (const f of toScan) {
|
|
1258
|
+
const totalMatches = counts.get(f.filePath) ?? 0;
|
|
1259
|
+
const lines = samples.get(f.filePath);
|
|
1260
|
+
// Retained lines must be the COMPLETE, untruncated match set: rg's
|
|
1261
|
+
// --max-columns replaces over-long lines with an omission marker, so
|
|
1262
|
+
// "every line still contains the query" is the integrity check.
|
|
1263
|
+
const matchingLines = totalMatches === 0
|
|
1264
|
+
? []
|
|
1265
|
+
: lines !== undefined
|
|
1266
|
+
&& totalMatches <= RG_SAMPLE_LINES_PER_FILE
|
|
1267
|
+
&& lines.length >= totalMatches
|
|
1268
|
+
&& lines.every((l) => l.length <= SEARCH_CACHE_LINE_LEN_MAX && l.toLowerCase().includes(queryLower))
|
|
1269
|
+
? lines.slice(0, totalMatches)
|
|
1270
|
+
: undefined;
|
|
1271
|
+
storeSearchEntry(f.filePath, {
|
|
1272
|
+
mtimeMs: f.mtimeMs,
|
|
1273
|
+
sizeBytes: f.session.sizeBytes,
|
|
1274
|
+
query: queryLower,
|
|
1275
|
+
totalMatches,
|
|
1276
|
+
snippet: topByPath.get(f.filePath)?.snippet ?? '',
|
|
1277
|
+
matchingLines,
|
|
1278
|
+
});
|
|
1279
|
+
}
|
|
1280
|
+
return top.map((h) => ({
|
|
1281
|
+
sessionId: h.session.sessionId,
|
|
1282
|
+
projectPath: h.session.projectPath,
|
|
1283
|
+
projectDir: h.session.projectDir,
|
|
1284
|
+
lastModified: h.session.lastModified,
|
|
1285
|
+
totalMatches: h.totalMatches,
|
|
1286
|
+
snippet: h.snippet,
|
|
1287
|
+
firstPrompt: h.session.firstPrompt,
|
|
1288
|
+
}));
|
|
1289
|
+
}
|
|
1290
|
+
/**
|
|
1291
|
+
* Full-text search every Claude session for `query`.
|
|
1292
|
+
*
|
|
1293
|
+
* Fast paths layered on top of the raw scan (which is O(corpus) and the corpus
|
|
1294
|
+
* grows forever): cached headers (listAllSessions), per-file result cache with
|
|
1295
|
+
* query-refinement pruning (planFileSearch), a ripgrep cold-scan engine when
|
|
1296
|
+
* available (single process, all cores), chunked JS scanning as the fallback,
|
|
1297
|
+
* and early termination — sessions are scheduled newest-first, so once `limit`
|
|
1298
|
+
* sessions have matched, every unscheduled session is older than every
|
|
1299
|
+
* collected match and cannot make the cut.
|
|
811
1300
|
*/
|
|
812
1301
|
export async function searchAllSessions(query, options) {
|
|
813
1302
|
const trimmed = query.trim();
|
|
@@ -816,64 +1305,90 @@ export async function searchAllSessions(query, options) {
|
|
|
816
1305
|
const queryLower = trimmed.toLowerCase();
|
|
817
1306
|
const limit = options?.limit ?? 100;
|
|
818
1307
|
const cwdFilter = options?.cwdFilter?.toLowerCase();
|
|
1308
|
+
// Stale-search cancelation: while the user types, each keystroke supersedes
|
|
1309
|
+
// the in-flight scan; without this, overlapping searches keep burning IO on
|
|
1310
|
+
// results the client will discard (it seq-guards responses).
|
|
1311
|
+
const generation = ++searchAllSessionsGeneration;
|
|
819
1312
|
const sessions = await listAllSessions({ limit: 1000 });
|
|
1313
|
+
const candidates = cwdFilter
|
|
1314
|
+
? sessions.filter((s) => s.projectPath.toLowerCase().includes(cwdFilter))
|
|
1315
|
+
: sessions;
|
|
1316
|
+
if (isRgAvailable()) {
|
|
1317
|
+
const viaRg = await searchViaRipgrep(candidates, trimmed, queryLower, limit, generation);
|
|
1318
|
+
if (viaRg !== null)
|
|
1319
|
+
return viaRg;
|
|
1320
|
+
// rg hiccup — fall through to the JS engine.
|
|
1321
|
+
}
|
|
820
1322
|
const out = [];
|
|
821
|
-
const concurrency =
|
|
1323
|
+
const concurrency = 10;
|
|
822
1324
|
let cursor = 0;
|
|
823
1325
|
const workers = [];
|
|
824
1326
|
for (let i = 0; i < concurrency; i++) {
|
|
825
1327
|
workers.push((async () => {
|
|
826
|
-
while (
|
|
1328
|
+
while (true) {
|
|
1329
|
+
if (generation !== searchAllSessionsGeneration)
|
|
1330
|
+
break; // superseded
|
|
1331
|
+
if (out.length >= limit)
|
|
1332
|
+
break; // see early-termination note above
|
|
827
1333
|
const idx = cursor++;
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
1334
|
+
if (idx >= candidates.length)
|
|
1335
|
+
break;
|
|
1336
|
+
const session = candidates[idx];
|
|
831
1337
|
const filePath = path.join(PROJECTS_DIR, session.projectDir, `${session.sessionId}.jsonl`);
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
1338
|
+
const mtimeMs = session.lastModified.getTime();
|
|
1339
|
+
const plan = planFileSearch(searchFileCache.get(filePath), queryLower, mtimeMs, session.sizeBytes);
|
|
1340
|
+
let result;
|
|
1341
|
+
if (plan.kind === 'reuse') {
|
|
1342
|
+
result = plan;
|
|
1343
|
+
}
|
|
1344
|
+
else {
|
|
1345
|
+
let scan;
|
|
1346
|
+
try {
|
|
1347
|
+
scan = await scanSessionFileForQuery(filePath, trimmed, plan.kind === 'tail' ? plan.startByte : undefined);
|
|
1348
|
+
}
|
|
1349
|
+
catch {
|
|
1350
|
+
continue; // unreadable — skip
|
|
1351
|
+
}
|
|
1352
|
+
const head = plan.kind === 'tail'
|
|
1353
|
+
? plan.head
|
|
1354
|
+
: { totalMatches: 0, snippet: '', matchingLines: [] };
|
|
1355
|
+
const merged = {
|
|
1356
|
+
totalMatches: head.totalMatches + scan.totalMatches,
|
|
1357
|
+
snippet: head.snippet || scan.snippet,
|
|
1358
|
+
matchingLines: head.matchingLines && scan.matchingLines
|
|
1359
|
+
&& head.matchingLines.length + scan.matchingLines.length <= SEARCH_CACHE_LINES_MAX
|
|
1360
|
+
? [...head.matchingLines, ...scan.matchingLines]
|
|
1361
|
+
: undefined,
|
|
1362
|
+
};
|
|
1363
|
+
// Resume bookkeeping: where EOF sat when this scan finished. Stat
|
|
1364
|
+
// AFTER the read so an append landing mid-scan re-tails next time
|
|
1365
|
+
// instead of being skipped forever.
|
|
1366
|
+
let scannedBytes;
|
|
1367
|
+
if (scan.reachedEof) {
|
|
1368
|
+
try {
|
|
1369
|
+
scannedBytes = fs.statSync(filePath).size;
|
|
863
1370
|
}
|
|
1371
|
+
catch { /* keep undefined */ }
|
|
864
1372
|
}
|
|
865
|
-
|
|
866
|
-
|
|
1373
|
+
storeSearchEntry(filePath, {
|
|
1374
|
+
mtimeMs,
|
|
1375
|
+
sizeBytes: session.sizeBytes,
|
|
1376
|
+
query: queryLower,
|
|
1377
|
+
totalMatches: merged.totalMatches,
|
|
1378
|
+
snippet: merged.snippet,
|
|
1379
|
+
matchingLines: merged.matchingLines,
|
|
1380
|
+
scannedBytes,
|
|
1381
|
+
});
|
|
1382
|
+
result = merged;
|
|
867
1383
|
}
|
|
868
|
-
|
|
869
|
-
if (totalMatches > 0) {
|
|
1384
|
+
if (result.totalMatches > 0) {
|
|
870
1385
|
out.push({
|
|
871
1386
|
sessionId: session.sessionId,
|
|
872
1387
|
projectPath: session.projectPath,
|
|
873
1388
|
projectDir: session.projectDir,
|
|
874
1389
|
lastModified: session.lastModified,
|
|
875
|
-
totalMatches,
|
|
876
|
-
snippet,
|
|
1390
|
+
totalMatches: result.totalMatches,
|
|
1391
|
+
snippet: result.snippet,
|
|
877
1392
|
firstPrompt: session.firstPrompt,
|
|
878
1393
|
});
|
|
879
1394
|
}
|
|
@@ -1899,6 +2414,87 @@ export async function loadSession(cwd, sessionId, limit = 50, offset = 0) {
|
|
|
1899
2414
|
hasMore: startIndex > 0,
|
|
1900
2415
|
};
|
|
1901
2416
|
}
|
|
2417
|
+
/**
|
|
2418
|
+
* Preview window anchored on the most recent message matching `query`.
|
|
2419
|
+
* The Session Finder preview must SHOW the match: the plain tail window made
|
|
2420
|
+
* the match navigator report "0/0" whenever the hit sat deeper in the session
|
|
2421
|
+
* than the last `limit` messages.
|
|
2422
|
+
*/
|
|
2423
|
+
export async function loadSessionAroundMatch(cwd, sessionId, limit, query) {
|
|
2424
|
+
const resolved = resolveSessionFile(cwd, sessionId);
|
|
2425
|
+
if (!resolved) {
|
|
2426
|
+
log.log(` Session file not found for session ${sessionId}`);
|
|
2427
|
+
return null;
|
|
2428
|
+
}
|
|
2429
|
+
const { messages } = await parseSessionMessagesCached(resolved);
|
|
2430
|
+
const totalCount = messages.length;
|
|
2431
|
+
const q = query.trim().toLowerCase();
|
|
2432
|
+
let anchor = -1;
|
|
2433
|
+
if (q) {
|
|
2434
|
+
for (let i = totalCount - 1; i >= 0; i--) {
|
|
2435
|
+
const m = messages[i];
|
|
2436
|
+
// Mirror what the preview renders (and what its match counter scans):
|
|
2437
|
+
// message content, plus the pretty-printed tool input of tool_use rows.
|
|
2438
|
+
let text = m.content || '';
|
|
2439
|
+
if (m.toolInput !== undefined) {
|
|
2440
|
+
try {
|
|
2441
|
+
text += ' ' + JSON.stringify(m.toolInput, null, 2);
|
|
2442
|
+
}
|
|
2443
|
+
catch { /* unstringifiable input */ }
|
|
2444
|
+
}
|
|
2445
|
+
if (text.toLowerCase().includes(q)) {
|
|
2446
|
+
anchor = i;
|
|
2447
|
+
break;
|
|
2448
|
+
}
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
if (anchor === -1) {
|
|
2452
|
+
// The hit lives in raw JSONL the preview doesn't render (subagent
|
|
2453
|
+
// sidechains, file snapshots, wire metadata). Surface the matching raw
|
|
2454
|
+
// lines as synthetic rows so the user still SEES — and can navigate —
|
|
2455
|
+
// what actually matched, instead of a lying empty "0/0" preview.
|
|
2456
|
+
if (resolved.filePath.endsWith('.jsonl')) {
|
|
2457
|
+
try {
|
|
2458
|
+
const rawScan = await scanSessionFileForQuery(resolved.filePath, q, undefined, 20);
|
|
2459
|
+
const rawLines = rawScan.firstLines ?? [];
|
|
2460
|
+
if (rawLines.length > 0) {
|
|
2461
|
+
const rows = rawLines.map((line, i) => {
|
|
2462
|
+
// Prefer the readable text when it actually contains the hit;
|
|
2463
|
+
// otherwise show the (windowed) raw line — that's where it lives.
|
|
2464
|
+
const readable = extractReadableLineText(line);
|
|
2465
|
+
const content = readable.rank > SNIPPET_RANK_RAW && readable.text.toLowerCase().includes(q)
|
|
2466
|
+
? readable.text
|
|
2467
|
+
: line;
|
|
2468
|
+
let timestamp = '';
|
|
2469
|
+
try {
|
|
2470
|
+
const obj = JSON.parse(line);
|
|
2471
|
+
if (typeof obj.timestamp === 'string')
|
|
2472
|
+
timestamp = obj.timestamp;
|
|
2473
|
+
}
|
|
2474
|
+
catch { /* windowed/non-JSON line */ }
|
|
2475
|
+
return {
|
|
2476
|
+
type: 'tool_result',
|
|
2477
|
+
content,
|
|
2478
|
+
timestamp,
|
|
2479
|
+
uuid: `raw-match-${i}`,
|
|
2480
|
+
toolName: 'Session data',
|
|
2481
|
+
};
|
|
2482
|
+
});
|
|
2483
|
+
return { sessionId, messages: rows, cwd, totalCount, hasMore: false };
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
catch { /* raw scan failed — fall through to the tail window */ }
|
|
2487
|
+
}
|
|
2488
|
+
const startIndex = Math.max(0, totalCount - limit);
|
|
2489
|
+
return { sessionId, messages: messages.slice(startIndex), cwd, totalCount, hasMore: startIndex > 0 };
|
|
2490
|
+
}
|
|
2491
|
+
// Put the anchor near the window's end with some trailing context, so the
|
|
2492
|
+
// window also covers as many EARLIER matches as possible.
|
|
2493
|
+
const contextAfter = Math.min(50, Math.floor(limit / 4));
|
|
2494
|
+
const endIndex = Math.min(totalCount, anchor + 1 + contextAfter);
|
|
2495
|
+
const startIndex = Math.max(0, endIndex - limit);
|
|
2496
|
+
return { sessionId, messages: messages.slice(startIndex, endIndex), cwd, totalCount, hasMore: startIndex > 0 };
|
|
2497
|
+
}
|
|
1902
2498
|
/**
|
|
1903
2499
|
* Search conversation history for matching messages
|
|
1904
2500
|
* @param cwd - Working directory
|