Skip to content

Make Codex and Claude Code subscriptions go further: a local model router that gives routine agent work to economical models and keeps your strongest model for the hard parts.

fix(search): page matching sessions with live authorized cursors (#109)

澄潭560ed2c44286
diff --git a/apps/desktop/src/features/Sessions.tsx b/apps/desktop/src/features/Sessions.tsx
index 2d6dffeb..d3d036e1 100644
--- a/apps/desktop/src/features/Sessions.tsx
+++ b/apps/desktop/src/features/Sessions.tsx
@@ -53,6 +53,7 @@ export function Sessions({ initialSession = null, initialRequest = null, languag
const [searchCursor, setSearchCursor] = useState<string | null>(null);
const [partial, setPartial] = useState(false);
const [listLoaded, setListLoaded] = useState(false);
+ const [listBusy, setListBusy] = useState(false);
const [onlySwitch, setOnlySwitch] = useState(false);
const [listError, setListError] = useState('');
const [timelineError, setTimelineError] = useState('');
@@ -78,38 +79,44 @@ export function Sessions({ initialSession = null, initialRequest = null, languag
const timelineGeneration = useRef(0);
const factGeneration = useRef(0);
const searchMounted = useRef(false);
+ const keywordTimer = useRef<number | null>(null);
const initialTarget = useRef(initialSession);
const initialRequestTarget = useRef(initialRequest);
+ function clearTimelineSelection() {
+ timelineGeneration.current++;
+ setBusy(false); setTimelineError('');
+ setSession(null); setRequests([]); setReadingRequests([]); setRequest(null);
+ setRequestCursor(null); setFocusedRequest(null); setAnchor(null); setFactsOpen(false);
+ }
+ function changeOnlySwitch(value: boolean) {
+ if (keywordTimer.current !== null) { window.clearTimeout(keywordTimer.current); keywordTimer.current = null; }
+ setOnlySwitch(value); setHits([]); setPartial(false); clearTimelineSelection();
+ void load(null, { onlySwitch: value });
+ }
async function load(cursor: string | null = null, override?: { keyword?: string; onlySwitch?: boolean }) {
const effectiveKeyword = override?.keyword ?? keyword;
const effectiveOnlySwitch = override?.onlySwitch ?? onlySwitch;
const searching = Boolean(effectiveKeyword.trim());
if (!cursor) {
- if (searching) { searchQuery.current = currentWindow(); setSearchCursor(null); }
+ if (searching) { searchQuery.current = currentWindow(); setSearchCursor(null); setPartial(false); }
else { sessionQuery.current = currentWindow(); setSessionCursor(null); }
}
const queryWindow = searching ? searchQuery.current : sessionQuery.current;
- const epoch = ++generation.current; setBusy(true); setListError('');
+ const epoch = ++generation.current; setListBusy(true); setListError('');
try {
if (searching) {
- const page = await read<Page<{ hits: Hit[]; index_partial: boolean; budget_exhausted: boolean }>>('search', { ...queryWindow, session_id: null, keyword: effectiveKeyword.trim(), limit: 50, cursor, agent_id: null, plan_id: null, native_model: null, outcome: null, only_model_switch: effectiveOnlySwitch });
+ const page = await read<Page<{ hits: Hit[]; index_partial: boolean }>>('session_search', { ...queryWindow, session_id: null, keyword: effectiveKeyword.trim(), limit: 50, cursor, agent_id: null, plan_id: null, native_model: null, outcome: null, only_model_switch: effectiveOnlySwitch });
if (epoch !== generation.current) return;
- setHits(current => cursor ? [...current, ...page.hits] : page.hits); setSearchCursor(page.next_cursor); setPartial(page.index_partial || page.budget_exhausted);
- if (!cursor) {
- const first = page.hits[0];
- if (first) void timeline(first.session_id, null, first);
- else {
- timelineGeneration.current++;
- setSession(null); setRequests([]); setReadingRequests([]); setRequest(null); setAnchor(null); setFactsOpen(false);
- }
- }
+ setHits(current => [...new Map((cursor ? [...current, ...page.hits] : page.hits).map(hit => [hit.session_id, hit])).values()]); setSearchCursor(page.next_cursor); setPartial(page.index_partial);
+ if (!cursor) clearTimelineSelection();
} else {
const page = await read<Page<{ sessions: Summary[] }>>('sessions', { ...queryWindow, session_id: null, limit: 50, cursor, agent_id: null, plan_id: null, native_model: null, outcome: null, only_model_switch: effectiveOnlySwitch });
if (epoch !== generation.current) return;
setListLoaded(true); setSessions(current => cursor ? [...current, ...page.sessions] : page.sessions); setSessionCursor(page.next_cursor); setHits([]); setPartial(false);
}
- } catch (e) { if (epoch === generation.current) setListError(errorCode(e)); }
- finally { if (epoch === generation.current) setBusy(false); }
+ } catch (e) {
+ if (epoch === generation.current) { setListError(errorCode(e)); setSearchCursor(null); setSessionCursor(null); }
+ } finally { if (epoch === generation.current) setListBusy(false); }
}
useEffect(() => {
if (!active) return;
@@ -123,8 +130,9 @@ export function Sessions({ initialSession = null, initialRequest = null, languag
}, [reload, refreshVersion, active]);
useEffect(() => {
if (!searchMounted.current) { searchMounted.current = true; return; }
- const timer = window.setTimeout(() => void load(null, { keyword }), 260);
- return () => window.clearTimeout(timer);
+ const timer = window.setTimeout(() => { keywordTimer.current = null; void load(null, { keyword }); }, 260);
+ keywordTimer.current = timer;
+ return () => { window.clearTimeout(timer); if (keywordTimer.current === timer) keywordTimer.current = null; };
}, [keyword]);
async function timeline(id: string, cursor: string | null = null, hit?: Hit, exactRequest?: string | null) {
const epoch = ++timelineGeneration.current; setBusy(true); setTimelineError('');
@@ -218,7 +226,7 @@ export function Sessions({ initialSession = null, initialRequest = null, languag
}
function refresh() {
if (focusedRequest && session) { initialTarget.current = session; initialRequestTarget.current = focusedRequest; }
- window.dispatchEvent(new Event('hiroute-content-invalidated')); generation.current++; timelineGeneration.current++; factGeneration.current++; setListError(''); setTimelineError(''); setSession(null); setRequest(null); setReadingRequests([]); setRequests([]); setFactsOpen(false); setFacts([]); setHits([]); setReload(current => current + 1);
+ window.dispatchEvent(new Event('hiroute-content-invalidated')); generation.current++; factGeneration.current++; clearTimelineSelection(); setListError(''); setFacts([]); setHits([]); setReload(current => current + 1);
}
const selectedSummary = sessions.find(item => item.session_id === session)
?? (focusedSummary?.session_id === session ? focusedSummary : undefined);
@@ -227,7 +235,7 @@ export function Sessions({ initialSession = null, initialRequest = null, languag
: id.toLowerCase().includes('codex') ? 'Codex'
: id.toLowerCase().includes('claude') ? 'Claude Code'
: text('本机 Agent', 'Local Agent');
- const emptyFirstUse = listLoaded && !busy && !listError && !keyword.trim() && !onlySwitch && sessions.length === 0;
+ const emptyFirstUse = listLoaded && !listBusy && !listError && !keyword.trim() && !onlySwitch && sessions.length === 0;
const selectedRequest = requests.find(item => item.request_id === request);
const displayedRequestIds = request ? (readingRequests.length ? readingRequests : [request]) : [];
const lastDisplayedIndex = requests.findIndex(item => item.request_id === displayedRequestIds.at(-1));
@@ -245,9 +253,9 @@ export function Sessions({ initialSession = null, initialRequest = null, languag
}, []);
useEffect(() => {
- if (!listLoaded || busy || listError || session || keyword.trim() || !sessions[0]) return;
+ if (!listLoaded || listBusy || busy || listError || session || keyword.trim() || !sessions[0]) return;
void timeline(sessions[0].session_id);
- }, [listLoaded, busy, listError, session, keyword, onlySwitch, sessions]);
+ }, [listLoaded, listBusy, busy, listError, session, keyword, onlySwitch, sessions]);
useEffect(() => {
setFacts([]); setFactsError(''); setFactCursor(null); setFactPartial(false);
@@ -259,23 +267,35 @@ export function Sessions({ initialSession = null, initialRequest = null, languag
return <ProductPage
title={text('会话', 'Sessions')}
subtitle={text('查看 Agent 实际经过 HiRoute 的对话、请求内回退和运行事实', 'View agent conversations, in-request fallbacks and run details captured by HiRoute')}
- actions={<>{onReturnToQuality && <button className="btn btn-quiet" type="button" onClick={onReturnToQuality}><UiIcon name="arrowLeft" />{text('返回模型表现', 'Back to model performance')}</button>}{partial && <span className="badge warn">{text('搜索结果可能不完整', 'Search may be incomplete')}</span>}</>}
+ actions={<>{onReturnToQuality && <button className="btn btn-quiet" type="button" onClick={onReturnToQuality}><UiIcon name="arrowLeft" />{text('返回模型表现', 'Back to model performance')}</button>}{partial && <span className="badge warn">{text('部分正文暂不可搜索', 'Some content is not searchable yet')}</span>}</>}
flush={!emptyFirstUse}
className="sessions-page"
>
<Toast notice={toast} />
- {!listLoaded && busy && <div className="empty-state" role="status"><div><span className="oc-spinner" /><p>{text('正在读取本机会话…', 'Reading local sessions…')}</p></div></div>}
+ {!listLoaded && listBusy && <div className="empty-state" role="status"><div><span className="oc-spinner" /><p>{text('正在读取本机会话…', 'Reading local sessions…')}</p></div></div>}
<MaintenanceStatus key={reload} language={language} />
- {!listLoaded && listError ? <div className="empty-state"><div><span className="empty-icon"><UiIcon name="warning" /></span><h3>{text('暂时无法读取会话', 'Sessions are temporarily unavailable')}</h3><p>{text('请确认本机服务正在运行,然后重试。', 'Make sure the local service is running, then retry.')}</p><button className="btn btn-primary" type="button" onClick={refresh}>{text('重试', 'Retry')}</button></div></div> : !listLoaded && busy ? null : emptyFirstUse ? <div className="empty-state"><div><span className="empty-icon"><UiIcon name="sessions" /></span><h3>{text('还没有会话记录', 'No sessions yet')}</h3><p>{text('在已接入的 Agent 中开始使用,经过 HiRoute 的会话会显示在这里。', 'Start in a connected Agent. Sessions routed through HiRoute will appear here.')}</p>{onOpenAgents && <button className="btn btn-primary" type="button" onClick={onOpenAgents}>{text('查看 Agent 接入', 'View Agent connections')}</button>}</div></div> : <div className={`session-layout${session ? ' has-selection' : ''}${factsOpen ? ' with-inspector' : ''}`}>
+ {!listLoaded && listError && !keyword.trim() ? <div className="empty-state"><div><span className="empty-icon"><UiIcon name="warning" /></span><h3>{text('暂时无法读取会话', 'Sessions are temporarily unavailable')}</h3><p>{text('请确认本机服务正在运行,然后重试。', 'Make sure the local service is running, then retry.')}</p><button className="btn btn-primary" type="button" onClick={refresh}>{text('重试', 'Retry')}</button></div></div> : !listLoaded && listBusy ? null : emptyFirstUse ? <div className="empty-state"><div><span className="empty-icon"><UiIcon name="sessions" /></span><h3>{text('还没有会话记录', 'No sessions yet')}</h3><p>{text('在已接入的 Agent 中开始使用,经过 HiRoute 的会话会显示在这里。', 'Start in a connected Agent. Sessions routed through HiRoute will appear here.')}</p>{onOpenAgents && <button className="btn btn-primary" type="button" onClick={onOpenAgents}>{text('查看 Agent 接入', 'View Agent connections')}</button>}</div></div> : <div className={`session-layout${session ? ' has-selection' : ''}${factsOpen ? ' with-inspector' : ''}${keyword.trim() ? ' is-searching' : ''}${listError ? ' with-list-error' : ''}`}>
<aside className="session-master" aria-label={text('会话列表', 'Session list')}>
<form onSubmit={event => { event.preventDefault(); void load(); }} className="session-filters">
- <label className="search-box"><span className="sr-only">{text('搜索会话正文', 'Search session content')}</span><UiIcon name="search" /><input className="input" value={keyword} maxLength={256} onChange={event => { generation.current++; setBusy(false); setKeyword(event.target.value); }} placeholder={text('搜索会话正文', 'Search session content')} /></label>
- <div className="segmented" role="group" aria-label={text('会话筛选', 'Session filter')}><button className={`segment${!onlySwitch ? ' active' : ''}`} type="button" aria-pressed={!onlySwitch} onClick={() => { setOnlySwitch(false); setSession(null); setRequest(null); setFactsOpen(false); void load(null, { onlySwitch: false }); }}>{text('全部', 'All')}</button><button className={`segment${onlySwitch ? ' active' : ''}`} type="button" aria-pressed={onlySwitch} onClick={() => { setOnlySwitch(true); setSession(null); setRequest(null); setFactsOpen(false); void load(null, { onlySwitch: true }); }}>{text('发生过请求内回退', 'In-request fallbacks')}</button></div>
+ <label className="search-box"><span className="sr-only">{text('搜索会话正文', 'Search session content')}</span><UiIcon name="search" /><input className="input" value={keyword} maxLength={256} onChange={event => { generation.current++; clearTimelineSelection(); setSearchCursor(null); setSessionCursor(null); setHits([]); setPartial(false); setListError(''); setListBusy(true); setKeyword(event.target.value); }} placeholder={text('搜索会话正文', 'Search session content')} /></label>
+ <div className="segmented" role="group" aria-label={text('会话筛选', 'Session filter')}><button className={`segment${!onlySwitch ? ' active' : ''}`} type="button" aria-pressed={!onlySwitch} onClick={() => changeOnlySwitch(false)}>{text('全部', 'All')}</button><button className={`segment${onlySwitch ? ' active' : ''}`} type="button" aria-pressed={onlySwitch} onClick={() => changeOnlySwitch(true)}>{text('发生过请求内回退', 'In-request fallbacks')}</button></div>
</form>
- {listError && <div className="callout bad session-local-feedback" role="alert" data-error-code={listError}><UiIcon name="warning" /><div><strong>{text('列表没有刷新', 'The list was not refreshed')}</strong><p>{text('已加载的会话仍然保留。', 'Previously loaded sessions are still available.')}</p><button className="btn" type="button" onClick={() => void load()}>{text('重试', 'Retry')}</button></div></div>}
+ {listError && <div className="callout bad session-local-feedback" role="alert" data-error-code={listError}><UiIcon name="warning" /><div><strong>{keyword.trim() ? text('搜索暂时失败', 'Search is temporarily unavailable') : text('列表没有刷新', 'The list was not refreshed')}</strong><p>{keyword.trim() ? text('请重试,重新读取当前可搜索的会话。', 'Retry to search the currently available sessions.') : text('已加载的会话仍然保留。', 'Previously loaded sessions are still available.')}</p><button className="btn" type="button" disabled={listBusy} onClick={() => void load()}>{text('重试', 'Retry')}</button></div></div>}
<div className="session-list">
- {keyword.trim() ? <>{hits.map((hit, index) => { const context = sessions.find(item => item.session_id === hit.session_id); return <button className={`session-item${session === hit.session_id ? ' active' : ''}`} key={`${hit.content_id}-${hit.original_text_offset}-${index}`} aria-current={session === hit.session_id ? 'page' : undefined} onClick={() => void timeline(hit.session_id, null, hit)}><div className="session-item-top"><span className="muted session-item-time">{context ? sessionTimeLabel(context.last_request_at_ms, language) : text('搜索命中', 'Match')}</span></div><div className="session-item-title"><ContentExcerpt hit={hit} language={language} fallback={text('查看命中正文', 'Open matching content')} revision={refreshVersion + reload} /></div><div className="session-item-meta">{context ? <><span>{agentName(context.agent_id)}</span>{sessionRoutes[context.session_id] && <><span>·</span><span>{sessionRoutes[context.session_id]}</span></>}{sessionModels[context.session_id] && <><span>·</span><span>{sessionModels[context.session_id]}</span></>}</> : text('点击读取所在会话', 'Open the matching session')}</div></button>; })}{searchCursor && <button className="btn session-more" disabled={busy} onClick={() => void load(searchCursor)}>{text('加载更多结果', 'Load more results')}</button>}{!busy && !listError && !hits.length && <div className="empty-state session-list-empty"><div><span className="empty-icon"><UiIcon name="search" /></span><h3>{partial || searchCursor ? text('尚未找到匹配', 'No match found yet') : text('没有匹配的会话', 'No matching sessions')}</h3><p>{searchCursor ? text('还有内容待搜索,请继续加载。', 'More content remains to be searched. Continue loading.') : partial ? text('部分内容尚未建立索引,可稍后刷新。', 'Some content is not indexed yet. Refresh later.') : text('试试其他关键词。', 'Try another keyword.')}</p></div></div>}</> : <>{sessions.map(item => { const contentState = sessionContentStates[item.session_id] ?? (['deleted', 'expired'].includes(item.content_completeness) ? 'cleared' : item.content_completeness === 'complete' ? 'recorded' : 'partial'); return <button className={`session-item${session === item.session_id ? ' active' : ''}`} key={item.session_id} aria-current={session === item.session_id ? 'page' : undefined} onClick={() => void timeline(item.session_id)}><div className="session-item-top"><span className="muted session-item-time">{sessionTimeLabel(item.last_request_at_ms, language)}</span>{contentState === 'cleared' ? <span className="badge warn no-dot">{text('正文已清理', 'Content cleared')}</span> : contentState === 'partial' ? <span className="badge warn no-dot">{text('记录不完整', 'Incomplete record')}</span> : item.fallback_request_count > 0 && <span className="badge warn no-dot">{text('请求内回退', 'Request fallback')}</span>}</div><div className="session-item-title"><ContentExcerpt session={item.session_id} language={language} fallback={text('会话记录', 'Session')} limit={48} revision={`${refreshVersion + reload}:${item.last_request_at_ms}:${item.request_count}`} onModel={model => setSessionModels(current => current[item.session_id] === model ? current : { ...current, [item.session_id]: model })} onRoute={route => setSessionRoutes(current => current[item.session_id] === route ? current : { ...current, [item.session_id]: route })} /></div><div className="session-item-meta"><span>{agentName(item.agent_id)}</span>{sessionRoutes[item.session_id] && <><span>·</span><span>{sessionRoutes[item.session_id]}</span></>}{sessionModels[item.session_id] && <><span>·</span><span>{sessionModels[item.session_id]}</span></>}{!sessionRoutes[item.session_id] && !sessionModels[item.session_id] && <><span>·</span><span>{item.request_count} {text('个请求', 'requests')}</span></>}</div></button>; })}{sessionCursor && <button className="btn session-more" disabled={busy} onClick={() => void load(sessionCursor)}>{text('加载更多会话', 'Load more sessions')}</button>}{!busy && !listError && listLoaded && !sessions.length && <div className="empty-state session-list-empty"><div><h3>{text('当前筛选没有会话', 'No sessions match this filter')}</h3><p>{text('切换到全部会话。', 'Show all sessions.')}</p></div></div>}</>}
+ {keyword.trim() ? <>
+ {listBusy && <p className="oc-meta session-local-feedback" role="status"><span className="oc-spinner" />{text('正在搜索…', 'Searching…')}</p>}
+ {hits.map(hit => {
+ const context = sessions.find(item => item.session_id === hit.session_id);
+ return <button className={`session-item${session === hit.session_id ? ' active' : ''}`} key={hit.session_id} aria-current={session === hit.session_id ? 'page' : undefined} onClick={() => void timeline(hit.session_id, null, hit)}>
+ <div className="session-item-top"><span className="muted session-item-time">{context ? sessionTimeLabel(context.last_request_at_ms, language) : text('搜索命中', 'Match')}</span></div>
+ <div className="session-item-title"><ContentExcerpt hit={hit} language={language} fallback={text('查看命中正文', 'Open matching content')} revision={refreshVersion + reload} /></div>
+ <div className="session-item-meta">{context ? <><span>{agentName(context.agent_id)}</span>{sessionRoutes[context.session_id] && <><span>·</span><span>{sessionRoutes[context.session_id]}</span></>}{sessionModels[context.session_id] && <><span>·</span><span>{sessionModels[context.session_id]}</span></>}</> : text('点击读取所在会话', 'Open the matching session')}</div>
+ </button>;
+ })}
+ {searchCursor && !listError && <button className="btn session-more" disabled={listBusy} onClick={() => void load(searchCursor)}>{text('加载更多结果', 'Load more results')}</button>}
+ {!listBusy && !listError && !hits.length && <div className="empty-state session-list-empty"><div><span className="empty-icon"><UiIcon name="search" /></span><h3>{partial ? text('尚未找到匹配', 'No match found yet') : text('没有匹配的会话', 'No matching sessions')}</h3><p>{partial ? text('部分正文暂不可搜索,可稍后刷新。', 'Some content is not searchable yet. Refresh later.') : text('试试其他关键词。', 'Try another keyword.')}</p></div></div>}
+ </> : <>{sessions.map(item => { const contentState = sessionContentStates[item.session_id] ?? (['deleted', 'expired'].includes(item.content_completeness) ? 'cleared' : item.content_completeness === 'complete' ? 'recorded' : 'partial'); return <button className={`session-item${session === item.session_id ? ' active' : ''}`} key={item.session_id} aria-current={session === item.session_id ? 'page' : undefined} onClick={() => void timeline(item.session_id)}><div className="session-item-top"><span className="muted session-item-time">{sessionTimeLabel(item.last_request_at_ms, language)}</span>{contentState === 'cleared' ? <span className="badge warn no-dot">{text('正文已清理', 'Content cleared')}</span> : contentState === 'partial' ? <span className="badge warn no-dot">{text('记录不完整', 'Incomplete record')}</span> : item.fallback_request_count > 0 && <span className="badge warn no-dot">{text('请求内回退', 'Request fallback')}</span>}</div><div className="session-item-title"><ContentExcerpt session={item.session_id} language={language} fallback={text('会话记录', 'Session')} limit={48} revision={`${refreshVersion + reload}:${item.last_request_at_ms}:${item.request_count}`} onModel={model => setSessionModels(current => current[item.session_id] === model ? current : { ...current, [item.session_id]: model })} onRoute={route => setSessionRoutes(current => current[item.session_id] === route ? current : { ...current, [item.session_id]: route })} /></div><div className="session-item-meta"><span>{agentName(item.agent_id)}</span>{sessionRoutes[item.session_id] && <><span>·</span><span>{sessionRoutes[item.session_id]}</span></>}{sessionModels[item.session_id] && <><span>·</span><span>{sessionModels[item.session_id]}</span></>}{!sessionRoutes[item.session_id] && !sessionModels[item.session_id] && <><span>·</span><span>{item.request_count} {text('个请求', 'requests')}</span></>}</div></button>; })}{sessionCursor && !listError && <button className="btn session-more" disabled={listBusy} onClick={() => void load(sessionCursor)}>{text('加载更多会话', 'Load more sessions')}</button>}{!busy && !listError && listLoaded && !sessions.length && <div className="empty-state session-list-empty"><div><h3>{text('当前筛选没有会话', 'No sessions match this filter')}</h3><p>{text('切换到全部会话。', 'Show all sessions.')}</p></div></div>}</>}
</div>
</aside>
diff --git a/apps/desktop/src/occami/styles/pages.css b/apps/desktop/src/occami/styles/pages.css
index 2837099b..379be2d1 100644
--- a/apps/desktop/src/occami/styles/pages.css
+++ b/apps/desktop/src/occami/styles/pages.css
@@ -483,6 +483,12 @@ button.option-row[aria-pressed='true'] { border-color: var(--accent); background
.session-master { display:flex;flex-direction:column;min-width: 0; min-height: 0; overflow: hidden; border-right: 1px solid var(--divider); background: color-mix(in srgb, var(--panel) 62%, transparent); }
.session-filters { display: grid; gap: 9px; padding: 13px; border-bottom: 1px solid var(--divider); }
.session-list { flex:1;min-height:0;overflow: auto; }
+.session-master > .session-local-feedback { flex-shrink: 0; }
+@media (max-width: 800px) {
+ .session-layout.is-searching:not(.has-selection) { grid-template-rows: minmax(0, 1fr); }
+ .session-layout.is-searching:not(.has-selection) > .session-transcript { display: none; }
+ .session-layout.is-searching.has-selection.with-list-error { grid-template-rows: minmax(260px, 50%) minmax(0, 1fr); }
+}
.session-item { display: block; width: 100%; padding: 13px; border: 0; border-bottom: 1px solid var(--divider); background: transparent; color: inherit; text-align: left; }
.session-item:hover { background: var(--bg-hover); }
.session-item.active { background: var(--accent-soft); }
diff --git a/apps/desktop/tests/README.md b/apps/desktop/tests/README.md
index f41f7bb8..48c6a6a6 100644
--- a/apps/desktop/tests/README.md
+++ b/apps/desktop/tests/README.md
@@ -9,6 +9,7 @@ proves native authorization, persistence, process cleanup or real model calls.
| User capability | Representative tests | Unique guarantee |
| --- | --- | --- |
+| Search session content and continue reading matches | [Session scenarios](v3/browser/session-window-scenarios.mjs), [session layout captures](v3/visual/session-window-active.mjs) | Results represent distinct matching sessions; keyword edits discard stale pages, failed continuation withdraws its cursor, and clearing search resumes automatic selection after an in-flight detail read. Pending indexing and complete empty results remain distinct. Chinese/English searching, results, empty, pending and retry states stay readable at 1200×813 and 755×600 using production styles. Real lookup, fresh authorization and Worker activity require native acceptance. |
| Connect an API through the service directory | [Connection flow scenarios](v3/visual/connection-flow-active.mjs), [directory and draft rules](connection-directory.test.mjs) | Full branded directory, separate product drafts and keys, checked source name, cancellation, explicit model selection and inherited append behavior. Uses production styles; native save and live calls need separate acceptance. |
| Enable or edit Codex routing | `codex.enable.draft-cancel`, `codex.routing.fixed-binding`, `codex.routing.mixed-native-default` in [Agent scenarios](v3/browser/agent-trust-scenarios.mjs) | Cancelling never applies; configured fixed overrides, native reasoning and the chosen default survive save; service responsibility is explained. |
| Configure Claude routing and collaboration | `claude.enable.shared-presets`, `agent.configuration.independent-facets`, `claude.collaboration.check-before-save`, `claude.collaboration.retry-preserves-edit` | Ordinary Claude entry and account Default limitations remain clear; prerequisite checks precede mutation; retry preserves edits. |
diff --git a/apps/desktop/tests/v3/browser/product-fixtures.ts b/apps/desktop/tests/v3/browser/product-fixtures.ts
index 0c1ac317..d096ae7f 100644
--- a/apps/desktop/tests/v3/browser/product-fixtures.ts
+++ b/apps/desktop/tests/v3/browser/product-fixtures.ts
@@ -658,7 +658,7 @@ export function mockProductInvoke(command: string, payload: Record<string, any>
: sessionRows;
return { sessions: fresh || scenario === 'configured_no_sessions' ? [] : rows.map(row => ({ ...row, content_completeness: row.session_id === 'session/stream-gap' ? 'partial' : 'complete' })), next_cursor: null };
}
- if (view === 'search') {
+ if (view === 'search' || view === 'session_search') {
const needle = String(query.keyword ?? '').trim().toLocaleLowerCase();
const allowedSessions = new Set((query.only_model_switch
? sessionRows.filter(row => row.fallback_request_count > 0)
@@ -679,7 +679,9 @@ export function mockProductInvoke(command: string, payload: Record<string, any>
}];
});
});
- return { hits, next_cursor: null, index_partial: false, budget_exhausted: false };
+ return view === 'session_search'
+ ? { hits: [...new Map(hits.map(hit => [hit.session_id, hit])).values()], next_cursor: null, index_partial: false }
+ : { hits, next_cursor: null, index_partial: false, budget_exhausted: false };
}
if (view === 'timeline') {
const requestId = requestBySession[query.session_id];
diff --git a/apps/desktop/tests/v3/browser/session-window-scenarios.mjs b/apps/desktop/tests/v3/browser/session-window-scenarios.mjs
index b12bbf8f..bc0d5502 100644
--- a/apps/desktop/tests/v3/browser/session-window-scenarios.mjs
+++ b/apps/desktop/tests/v3/browser/session-window-scenarios.mjs
@@ -108,24 +108,112 @@ const scenarios = [
completeness = 'complete'; c().bumpRefresh();
await until(() => rows()[0] && !rows()[0].textContent.includes('记录不完整'), 'recovered authoritative summary');
}],
- ['unfinished empty search pages never claim that no match exists', async () => {
+ ['pending indexes and complete empty searches have distinct outcomes', async () => {
await fresh(control => {
seedSimple(control);
- control.views.search = query => query.keyword === 'settled'
- ? { hits: [], next_cursor: null, index_partial: false, budget_exhausted: false }
- : query.cursor
- ? { hits: [], next_cursor: null, index_partial: true, budget_exhausted: false }
- : { hits: [], next_cursor: 'continue-search', index_partial: false, budget_exhausted: true };
+ control.views.session_search = query => ({ hits: [], next_cursor: null, index_partial: query.keyword !== 'settled' });
});
await type('pending');
- await until(() => button('加载更多结果'), 'unfinished search window');
- assert(text().includes('尚未找到匹配') && !text().includes('没有匹配的会话'), 'Unsearched history was reported as no matches');
- button('加载更多结果').click();
- await until(() => !button('加载更多结果') && text().includes('部分内容尚未建立索引'), 'incomplete index after scanning');
- assert(!text().includes('没有匹配的会话'), 'An index gap was reported as no matches');
+ await until(() => text().includes('部分正文暂不可搜索'), 'pending-index outcome');
+ assert(text().includes('尚未找到匹配') && !text().includes('没有匹配的会话'), 'An index gap was reported as complete empty');
+ assert(!button('加载更多结果'), 'Pending indexing exposed a scan-window action');
await type('settled');
await until(() => text().includes('没有匹配的会话'), 'complete empty search');
}],
+ ['failed search continuation withdraws the cursor and retry starts a new query', async () => {
+ const hit = { session_id: 'observation-session/simple', request_id: 'request/simple/0', content_id: 'search-content', original_text_offset: 0 };
+ let attempts = 0;
+ await fresh(control => {
+ seedSimple(control); control.texts['search-content'] = 'needle';
+ control.views.session_search = query => {
+ attempts += 1;
+ if (query.cursor) throw { code: 'CHANGE_PREVIEW_STALE' };
+ return { hits: [hit], next_cursor: attempts === 1 ? 'stale-cursor' : null, index_partial: false };
+ };
+ });
+ await type('needle');
+ await until(() => button('加载更多结果'), 'first matching session');
+ button('加载更多结果').click();
+ await until(() => document.querySelector('[data-error-code="CHANGE_PREVIEW_STALE"]'), 'continuation error');
+ assert(!button('加载更多结果'), 'The failed cursor remained actionable');
+ assert(rows().length === 1, 'A page failure discarded the matching session');
+ button('重试').click();
+ await until(() => attempts === 3 && !document.querySelector('[data-error-code]'), 'fresh successful retry');
+ assert(queryOf(reads('session_search').at(-1)).cursor === null, 'Retry replayed the stale cursor');
+ }],
+ ['keyword edits withdraw old results immediately and ignore delayed pages', async () => {
+ const slow = deferred();
+ const hit = { session_id: 'observation-session/simple', request_id: 'request/simple/0', content_id: 'old-match', original_text_offset: 0 };
+ await fresh(control => {
+ seedSimple(control); control.texts['old-match'] = 'old needle';
+ control.views.session_search = query => query.cursor ? slow.promise : query.keyword === 'old'
+ ? { hits: [hit], next_cursor: 'old-page', index_partial: false }
+ : { hits: [], next_cursor: null, index_partial: false };
+ });
+ await type('old'); await until(() => button('加载更多结果'), 'old results');
+ button('加载更多结果').click(); await until(() => reads('session_search').length === 2, 'delayed continuation');
+ const input = document.querySelector('.session-filters input.input');
+ Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set.call(input, 'new');
+ input.dispatchEvent(new Event('input', { bubbles: true })); await tick();
+ assert(!button('加载更多结果') && rows().length === 0, 'An edited keyword retained old cursor/results');
+ slow.resolve({ hits: [hit], next_cursor: 'old-next', index_partial: true });
+ await until(() => reads('session_search').length === 3 && text().includes('没有匹配的会话'), 'new complete search');
+ assert(rows().length === 0 && !button('加载更多结果') && !text().includes('部分正文暂不可搜索'), 'The delayed page contaminated the new keyword');
+ }],
+ ['live pages merge a repeated session once', async () => {
+ const hit = { session_id: 'observation-session/simple', request_id: 'request/simple/0', content_id: 'match', original_text_offset: 0 };
+ await fresh(control => {
+ seedSimple(control); control.texts.match = 'needle';
+ control.views.session_search = query => ({ hits: [hit], next_cursor: query.cursor ? null : 'next', index_partial: false });
+ });
+ await type('needle'); await until(() => button('加载更多结果'), 'first session');
+ button('加载更多结果').click(); await until(() => reads('session_search').length === 2 && !button('加载更多结果'), 'last session page');
+ assert(rows().length === 1, 'A session appeared more than once after live continuation');
+ }],
+ ['clearing search resumes automatic selection after an invalidated detail read', async () => {
+ for (const searchFails of [false, true]) {
+ const slow = deferred();
+ let delayedQuery;
+ let hold = true;
+ await fresh(control => {
+ seedSimple(control);
+ const request = control.requests['observation-session/simple'][0];
+ seedUserText(control, request, 'Recovered automatically after clearing search');
+ control.views.timeline = query => {
+ if (hold && Object.hasOwn(query, 'request_id')) { delayedQuery = query; return slow.promise; }
+ return control.defaultRead('timeline', query);
+ };
+ control.views.session_search = () => {
+ if (searchFails) throw { code: 'DAEMON_UNAVAILABLE' };
+ return { hits: [], next_cursor: null, index_partial: false };
+ };
+ });
+ await until(() => delayedQuery && document.querySelector('.session-detail'), 'in-flight detail read');
+ await type('new');
+ await until(() => searchFails ? document.querySelector('.session-master [role="alert"]') : text().includes('没有匹配的会话'), 'search outcome');
+ hold = false; slow.resolve(c().defaultRead('timeline', delayedQuery)); await tick();
+ assert(!document.querySelector('.session-detail'), 'An invalidated detail read restored the old selection');
+ await type('');
+ await until(() => document.querySelector('.session-detail') && text().includes('Recovered automatically after clearing search'), 'automatic selection after clearing search');
+ assert(!document.querySelector('[aria-label="清理此会话"]').disabled, 'The invalidated read left the detail busy');
+ }
+ }],
+ ['a quick filter change cancels the queued keyword query', async () => {
+ const hit = { session_id: 'observation-session/simple', request_id: 'request/simple/0', content_id: 'match', original_text_offset: 0 };
+ await fresh(control => {
+ seedSimple(control, Date.now() - 1000, { within_request_fallback: true });
+ control.texts.match = 'needle';
+ control.views.session_search = query => ({ hits: query.only_model_switch ? [hit] : [], next_cursor: null, index_partial: false });
+ });
+ const input = document.querySelector('.session-filters input.input');
+ Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set.call(input, 'needle');
+ input.dispatchEvent(new Event('input', { bubbles: true })); await tick();
+ button('发生过请求内回退').click();
+ await until(() => reads('session_search').length > 0 && rows().length === 1, 'immediate filtered search');
+ await pause(340);
+ assert(reads('session_search').every(call => queryOf(call).only_model_switch === true), 'A queued query restored the previous filter');
+ assert(button('发生过请求内回退').getAttribute('aria-pressed') === 'true' && rows().length === 1, 'The selected filter disagreed with the displayed results');
+ }],
['switching from a filtered 21-stage session resets the filter for a two-stage session', async () => {
const largeSession = 'observation-session/low-21';
const smallSession = 'observation-session/high-2';
@@ -378,26 +466,27 @@ const scenarios = [
assert(second.from_ms === first.from_ms && second.to_ms === first.to_ms, 'Pagination changed the pinned window');
assert(!document.querySelector('[data-error-code]'), `Pagination surfaced ${document.querySelector('[data-error-code]')?.outerHTML.slice(0, 300)}`);
}],
- ['keyword search starts a new window and keeps it while paging', async () => {
+ ['keyword search pins its window while pages contain distinct matching sessions', async () => {
await fresh(control => {
seedSimple(control);
- control.texts['content/seed'] = '关于路由编译边界的说明';
control.hits = Array.from({ length: 51 }, (_, index) => {
+ const session = `observation-session/hit-${index}`;
+ const request = control.makeRequest(session, 0, Date.now() - 60_000 + index);
+ control.requests[session] = [request];
control.texts[`content/hit-${index}`] = '关于路由编译边界的说明';
- return { session_id: 'observation-session/simple', request_id: 'request/simple/0', content_id: `content/hit-${index}`, original_text_offset: 0 };
+ return { session_id: session, request_id: request.request_id, content_id: `content/hit-${index}`, original_text_offset: 0 };
});
});
- await until(() => rows().length === 1, 'initial row');
+ await until(() => rows().length === 50, 'initial list');
const mountWindow = queryOf(reads('sessions')[0]);
await type('路由');
- await until(() => reads('search').length === 1, 'search query');
- const searchWindow = queryOf(reads('search')[0]);
+ await until(() => reads('session_search').length === 1 && rows().length === 50 && button('加载更多结果'), 'first matching-session page');
+ const searchWindow = queryOf(reads('session_search')[0]);
assert(searchWindow.to_ms > mountWindow.to_ms, 'Search reused the mount-time upper bound');
- await until(() => button('加载更多结果'), 'search pagination');
button('加载更多结果').click();
- await until(() => reads('search').length === 2, 'second search page');
- const second = queryOf(reads('search').at(-1));
- assert(second.from_ms === searchWindow.from_ms && second.to_ms === searchWindow.to_ms, 'Search pagination recomputed the window');
+ await until(() => reads('session_search').length === 2 && rows().length === 51, 'second matching-session page');
+ const second = queryOf(reads('session_search').at(-1));
+ assert(second.from_ms === searchWindow.from_ms && second.to_ms === searchWindow.to_ms, 'Pagination recomputed the window');
assert(!document.querySelector('[data-error-code]'), 'Search pagination surfaced an error');
}],
['refreshVersion re-queries with a fresh window', async () => {
@@ -510,34 +599,30 @@ const scenarios = [
['a failed search re-query cannot offer its previous cursor for pagination', async () => {
await fresh(control => {
seedSimple(control);
- control.texts['content/seed'] = '关于路由编译边界的说明';
- control.hits = Array.from({ length: 51 }, (_, index) => {
+ control.hits = Array.from({ length: 51 }, (_, index) => {
+ const session = `observation-session/match-${index}`;
+ const request = control.makeRequest(session, 0, Date.now() - 60_000 + index);
+ control.requests[session] = [request];
control.texts[`content/hit-${index}`] = '关于路由编译边界的说明';
- return { session_id: 'observation-session/simple', request_id: 'request/simple/0', content_id: `content/hit-${index}`, original_text_offset: 0 };
+ return { session_id: session, request_id: request.request_id, content_id: `content/hit-${index}`, original_text_offset: 0 };
});
});
- await until(() => rows().length === 1, 'initial row');
+ await until(() => rows().length === 50, 'initial rows');
await type('路由');
- await until(() => reads('search').length === 1, 'search query');
+ await until(() => reads('session_search').length === 1, 'search query');
await until(() => button('加载更多结果'), 'search pagination');
- c().views.search = () => { throw { code: 'DAEMON_UNAVAILABLE' }; };
+ c().views.session_search = () => { throw { code: 'DAEMON_UNAVAILABLE' }; };
c().bumpRefresh();
await until(() => document.querySelector('[data-error-code="DAEMON_UNAVAILABLE"]'), 'failed search re-query error');
assert(rows().length === 50, 'Failed search re-query dropped the loaded hits');
- delete c().views.search;
- const stalePager = button('加载更多结果');
- if (stalePager) {
- stalePager.click();
- await pause(150);
- const failure = document.querySelector('[data-error-code]')?.getAttribute('data-error-code');
- assert(failure !== 'CHANGE_PREVIEW_STALE', `A search cursor from before the failed re-query stayed clickable and surfaced ${failure}`);
- }
+ delete c().views.session_search;
+ assert(!button('加载更多结果'), 'A failed re-query left its old cursor actionable');
button('重试').click();
await until(() => !document.querySelector('[data-error-code]') && button('加载更多结果'), 'retry recovered');
- const retried = queryOf(reads('search').at(-1));
+ const retried = queryOf(reads('session_search').at(-1));
button('加载更多结果').click();
await until(() => rows().length === 51, 'second search page after retry');
- const paged = queryOf(reads('search').at(-1));
+ const paged = queryOf(reads('session_search').at(-1));
assert(paged.from_ms === retried.from_ms && paged.to_ms === retried.to_ms, 'Search pagination after retry did not reuse the recovered window');
assert(!document.querySelector('[data-error-code]'), 'Recovered search pagination surfaced an error');
}],
diff --git a/apps/desktop/tests/v3/browser/session-window.tsx b/apps/desktop/tests/v3/browser/session-window.tsx
index 1089b432..85be9ef2 100644
--- a/apps/desktop/tests/v3/browser/session-window.tsx
+++ b/apps/desktop/tests/v3/browser/session-window.tsx
@@ -19,6 +19,7 @@ const digestOf = (view: string, query: Record<string, any>) => [view, query.from
const inWindow = (startedAt: number, query: Record<string, any>) => startedAt >= query.from_ms && startedAt < query.to_ms;
const control = {
+ language: 'zh' as 'zh' | 'en',
requests: {} as Record<string, RequestRow[]>,
catalog: {} as Record<string, any[]>,
texts: {} as Record<string, string>,
@@ -88,13 +89,13 @@ function observationRead(view: string, query: Record<string, any>) {
const { page, next_cursor } = paginate(sessionSummaries(query), query, digest);
return { next_cursor, sessions: page };
}
- case 'search': {
- const digest = digestOf('search', query);
+ case 'session_search': {
+ const digest = digestOf('session_search', query);
const keyword = String(query.keyword ?? '');
- const visible = new Set(sessionSummaries({ ...query, only_model_switch: false, session_id: null }).map(row => row.session_id));
- const matched = control.hits.filter(hit => visible.has(hit.session_id) && (control.texts[hit.content_id] ?? '').includes(keyword));
+ const visible = new Set(sessionSummaries(query).map(row => row.session_id));
+ const matched = [...new Map(control.hits.filter(hit => visible.has(hit.session_id) && (control.texts[hit.content_id] ?? '').includes(keyword)).map(hit => [hit.session_id, hit])).values()];
const { page, next_cursor } = paginate(matched, query, digest);
- return { hits: page, next_cursor, index_partial: false, budget_exhausted: false };
+ return { hits: page, next_cursor, index_partial: false };
}
case 'timeline': {
const digest = digestOf('timeline', query);
@@ -170,6 +171,7 @@ function Harness() {
control.handlers = {};
control.commands = [];
control.qualityModels = [];
+ control.language = 'zh';
setQualityVisible(false);
setHomeVisible(false);
setRefresh(0);
@@ -191,7 +193,7 @@ function Harness() {
activity: { status: 'ready', data: homeActivity },
value: { status: 'loading' },
};
- return <PresentationRoot language="zh" theme="dark" textScale={1}>
+ return <PresentationRoot language={control.language} theme="dark" textScale={1}>
<div className="app-window">
<header className="titlebar"><div className="window-controls" /><div className="titlebar-main"><div className="window-title"><span>HiRoute</span><span>/</span><span>会话</span></div></div></header>
<HomeNavigation language="zh" items={[...navItems]} current="sessions" serviceLabel="仅在本机运行" serviceReady onNavigate={() => {}} onOpenSettings={() => {}} />
@@ -199,7 +201,7 @@ function Harness() {
<p className="sr-only">组件测试:所有 IPC 为 mock;不连接 Tauri、daemon 或真实数据。</p>
{homeVisible && <Home language="zh" reads={homeReads} onAction={() => {}} />}
{qualityVisible && <PlanQuality key={'quality-' + generation} planId="plan/quality" planRevision={1} currentModels={control.qualityModels} active={!homeVisible} refreshVersion={refresh} language="zh" />}
- <div hidden={homeVisible || qualityVisible}><Sessions key={generation} active={!homeVisible && !qualityVisible} language="zh" refreshVersion={refresh} onOpenAgents={() => {}} /></div>
+ <div hidden={homeVisible || qualityVisible}><Sessions key={generation} active={!homeVisible && !qualityVisible} language={control.language} refreshVersion={refresh} onOpenAgents={() => {}} /></div>
</div></main>
</div>
</PresentationRoot>;
diff --git a/apps/desktop/tests/v3/visual/session-window-active.mjs b/apps/desktop/tests/v3/visual/session-window-active.mjs
index 9dfefd72..cc3ba97d 100644
--- a/apps/desktop/tests/v3/visual/session-window-active.mjs
+++ b/apps/desktop/tests/v3/visual/session-window-active.mjs
@@ -26,14 +26,70 @@ try {
process.stdout.write(`${item.state === 'green' ? 'green' : 'red'}: ${item.name}${item.error ? ` · ${item.error}` : ''}\n`);
}
}
+ const layout = [];
+ for (const [width, height] of [[1200, 813], [755, 600]]) {
+ for (const language of ['zh', 'en']) {
+ for (const state of ['searching', 'results', 'empty', 'pending', 'error']) {
+ await navigate(client, new URL('session-window.html', baseUrl).href, { width, height });
+ await waitFor(client, 'Boolean(window.sessionWindow)');
+ await evaluate(client, `(() => {
+ const control = window.sessionWindow;
+ control.reset(); control.language = ${JSON.stringify(language)};
+ const session = 'observation-session/layout';
+ const request = control.makeRequest(session, 0, Date.now() - 1000);
+ control.requests[session] = [request];
+ control.texts['layout-content'] = '中文 literal checksum matching content';
+ const state = ${JSON.stringify(state)};
+ control.views.session_search = () => {
+ if (state === 'searching') return new Promise(() => {});
+ if (state === 'error') throw { code: 'DAEMON_UNAVAILABLE' };
+ return { hits: state === 'results' ? [{ session_id: session, request_id: request.request_id, content_id: 'layout-content', original_text_offset: 15 }] : [], next_cursor: state === 'results' ? 'next' : null, index_partial: state === 'pending' };
+ };
+ })()`);
+ await evaluate(client, 'new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))');
+ await waitFor(client, 'Boolean(document.querySelector(".session-filters input"))');
+ await evaluate(client, `(() => {
+ const input = document.querySelector('.session-filters input');
+ Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set.call(input, 'checksum');
+ input.dispatchEvent(new Event('input', { bubbles: true }));
+ })()`);
+ const condition = state === 'searching' ? 'Boolean(document.querySelector(".session-list [role=status]"))'
+ : state === 'error' ? 'Boolean(document.querySelector(".session-master [role=alert]"))'
+ : state === 'results' ? 'Boolean(document.querySelector(".session-more"))'
+ : 'Boolean(document.querySelector(".session-list-empty"))';
+ await waitFor(client, condition);
+ const geometry = await evaluate(client, `(() => {
+ const elements = [...document.querySelectorAll('.session-filters input,.session-master button,.session-list-empty h3,.session-list-empty p,.session-master [role=alert],.session-list [role=status]')].filter(el => el.getClientRects().length);
+ const outside = elements.filter(el => { const b=el.getBoundingClientRect(); return b.left < -1 || b.right > innerWidth+1 || b.top < -1 || b.bottom > innerHeight+1; }).map(el => el.className);
+ const clipped = elements.filter(el => {
+ const b=el.getBoundingClientRect(); let left=b.left,right=b.right,top=b.top,bottom=b.bottom;
+ for (let parent=el.parentElement;parent;parent=parent.parentElement) {
+ const style=getComputedStyle(parent),p=parent.getBoundingClientRect();
+ if (['hidden','clip','auto','scroll'].includes(style.overflowX)) { left=Math.max(left,p.left);right=Math.min(right,p.right); }
+ if (['hidden','clip','auto','scroll'].includes(style.overflowY)) { top=Math.max(top,p.top);bottom=Math.min(bottom,p.bottom); }
+ }
+ return right-left < b.width-2 || bottom-top < b.height-2;
+ }).map(el => el.className);
+ return { width: innerWidth, height: innerHeight, outside, clipped, inspected: elements.length, documentOverflow: document.documentElement.scrollWidth > innerWidth+1 };
+ })()`);
+ const image = await client.send('Page.captureScreenshot', { format: 'png' });
+ const file = `session-search-${language}-${width}x${height}-${state}.png`;
+ await writeFile(path.join(outputRoot, file), Buffer.from(image.data, 'base64'));
+ const result = { name: `session search layout ${language} ${width}x${height} ${state}`, state: !geometry.inspected || geometry.outside.length || geometry.clipped.length || geometry.documentOverflow ? 'red' : 'green', geometry, screenshot: file };
+ layout.push(result);
+ process.stdout.write(`${result.state}: ${result.name}\n`);
+ }
+ }
+ }
const report = {
generatedAt: new Date().toISOString(),
evidence: 'React components + mock IPC (window and cursor binding mirrored) only; not native/Tauri and not the real daemon',
checks,
+ layout,
summary: { total: checks.length, green: checks.filter(check => check.state === 'green').length, red: checks.filter(check => check.state === 'red').length },
};
await writeFile(path.join(outputRoot, 'session-window-report.json'), `${JSON.stringify(report, null, 2)}\n`);
- if (report.summary.total !== count || report.summary.red > 0) process.exitCode = 1;
+ if (report.summary.total !== count || report.summary.red > 0 || layout.some(item => item.state !== 'green')) process.exitCode = 1;
} finally {
client.close();
}
diff --git a/contracts/compatibility-support.v1.json b/contracts/compatibility-support.v1.json
index aa6be23f..aa20fa5a 100644
--- a/contracts/compatibility-support.v1.json
+++ b/contracts/compatibility-support.v1.json
@@ -1365,6 +1365,18 @@
"reason": "Released public compute connection test callers from supported 0.2.0 and 0.3.0 omit the label. Normalize only that omission to the authenticated catalog option name; current named requests preserve the supplied label and protected Desktop checks still require an explicit label. Explicit null/invalid names, unknown fields and untrusted catalog facts remain rejected.",
"removal_condition": "Remove only after the supported released public request shape is explicitly retired with a caller migration path under the release support policy. Do not regenerate the frozen released request with the current producer."
},
+ {
+ "contract": "hiroute.observation.query/v2",
+ "field": "released V0.3.1 text block cache schema and lowercase offsets",
+ "mode": "frozen_fixture",
+ "owner": "observation-text-index",
+ "reason": "The released 13856abd873da5b4373d39322493b4cc105c4867 SQL and fixed block bytes verify startup keyword-lookup backfill without rewriting retained bodies, offsets, request identities or occurrence-query semantics. These inputs never grant reader authority.",
+ "removal_condition": "Retire only when supported stores and backups can no longer contain the V0.3.1 block cache, preserving an equivalent released-input regression if the cache reader remains.",
+ "allowed_paths": [
+ "crates/observation/tests/fixtures/released-v0.3.1-text-index.sql",
+ "crates/observation/src/query_v2/session_search_tests.rs"
+ ]
+ },
{
"contract": "hiroute.observation.product-execution-envelope/v2",
"field": "function-only execution digest sha256:80b4ff13af8abc79d7721fea42141300beade2acf1d2659983ceaafcdc6e4799 and omitted required_named.tool_kind",
diff --git a/crates/application-api/src/observation_v2.rs b/crates/application-api/src/observation_v2.rs
index 9d9b59bf..a008e3b5 100644
--- a/crates/application-api/src/observation_v2.rs
+++ b/crates/application-api/src/observation_v2.rs
@@ -4,9 +4,9 @@ pub use hiroute_domain::{
ObservationCatalogQueryV2, ObservationContentPageV2, ObservationContentQueryV2,
ObservationFactsPageV2, ObservationFactsQueryV2, ObservationRequestPage,
ObservationRequestQuery, ObservationSearchPageV2, ObservationSearchQueryV2,
- ObservationSessionCorrelationKindV1, ObservationSessionPageV2, ObservationSessionSummaryV2,
- ObservationValueQueryV2, ObservationValueSummaryV2, PlanCompetenceFilter,
- PlanQualitySamplesPage, PlanQualitySamplesQuery,
+ ObservationSessionCorrelationKindV1, ObservationSessionPageV2, ObservationSessionSearchPageV1,
+ ObservationSessionSummaryV2, ObservationValueQueryV2, ObservationValueSummaryV2,
+ PlanCompetenceFilter, PlanQualitySamplesPage, PlanQualitySamplesQuery,
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -36,6 +36,7 @@ pub enum ObservationReadIntentV2 {
Content(ObservationContentQueryV2),
Facts(ObservationFactsQueryV2),
Search(ObservationSearchQueryV2),
+ SessionSearch(ObservationSearchQueryV2),
Value(ObservationValueQueryV2),
HomeValue(ObservationHomeValueQueryV2),
ValueReport(ObservationValueReportOptionsV2),
@@ -52,7 +53,8 @@ impl ObservationReadRequestV2 {
match self.intent {
ObservationReadIntentV2::Status(_)
| ObservationReadIntentV2::Sessions(_)
- | ObservationReadIntentV2::Search(_) => "ListSessions",
+ | ObservationReadIntentV2::Search(_)
+ | ObservationReadIntentV2::SessionSearch(_) => "ListSessions",
ObservationReadIntentV2::PlanQuality(_) => "GetPlanQualitySamples",
ObservationReadIntentV2::Facts(_) => "GetRoutingReceipt",
ObservationReadIntentV2::Value(_)
@@ -71,7 +73,9 @@ impl ObservationReadRequestV2 {
ObservationReadIntentV2::Ancestry(_) => "ReadSessionAncestryV2",
ObservationReadIntentV2::Content(_) => "ReadSessionContentV2",
ObservationReadIntentV2::Facts(_) => "ReadSessionFactsV2",
- ObservationReadIntentV2::Search(_) => "SearchSessionContentV2",
+ ObservationReadIntentV2::Search(_) | ObservationReadIntentV2::SessionSearch(_) => {
+ "SearchSessionContentV2"
+ }
ObservationReadIntentV2::Value(_)
| ObservationReadIntentV2::HomeValue(_)
| ObservationReadIntentV2::ValueReport(_) => "GetValueV2",
diff --git a/crates/application/src/observation_query/control/v2.rs b/crates/application/src/observation_query/control/v2.rs
index 60df4364..248d5f8c 100644
--- a/crates/application/src/observation_query/control/v2.rs
+++ b/crates/application/src/observation_query/control/v2.rs
@@ -26,8 +26,12 @@ impl ObservationControl {
| ObservationReadIntentV2::Ancestry(_)
| ObservationReadIntentV2::Content(_)
| ObservationReadIntentV2::Search(_)
+ | ObservationReadIntentV2::SessionSearch(_)
+ );
+ let search = matches!(
+ request.intent,
+ ObservationReadIntentV2::Search(_) | ObservationReadIntentV2::SessionSearch(_)
);
- let search = matches!(request.intent, ObservationReadIntentV2::Search(_));
if grant.is_some() || content {
self.protected_principal(grant, request.protected_operation(), &request)?;
}
@@ -98,6 +102,9 @@ impl ObservationControl {
ObservationReadIntentV2::Search(q) => {
serde_json::to_value(self.service.search_observed_text(&reader, &q, now)?)
}
+ ObservationReadIntentV2::SessionSearch(q) => {
+ serde_json::to_value(self.service.search_observed_sessions(&reader, &q, now)?)
+ }
ObservationReadIntentV2::Value(q) => {
serde_json::to_value(self.service.observed_value_totals(&reader, &q, now)?)
}
diff --git a/crates/application/src/observation_query/mod.rs b/crates/application/src/observation_query/mod.rs
index ac4c7745..7e7a877a 100644
--- a/crates/application/src/observation_query/mod.rs
+++ b/crates/application/src/observation_query/mod.rs
@@ -149,6 +149,16 @@ impl ObservationQueryService {
self.port.observed_valuation(reader, request, now_ms)
}
+ pub fn search_observed_sessions(
+ &self,
+ reader: &hiroute_domain::ObservationReaderContext,
+ query: &hiroute_domain::ObservationSearchQueryV2,
+ now_ms: i64,
+ ) -> Result<hiroute_domain::ObservationSessionSearchPageV1, ObservationQueryError> {
+ reader.check(now_ms, true, true)?;
+ self.port.search_observed_sessions(reader, query, now_ms)
+ }
+
pub fn search_observed_text(
&self,
reader: &hiroute_domain::ObservationReaderContext,
diff --git a/crates/cpa-bridge/src/owner.rs b/crates/cpa-bridge/src/owner.rs
index bb3430db..8fe3c272 100644
--- a/crates/cpa-bridge/src/owner.rs
+++ b/crates/cpa-bridge/src/owner.rs
@@ -260,14 +260,26 @@ fn open_reclaim_file(parent: &Path) -> Result<fs::File, OwnerError> {
Ok(file)
}
-fn lock_reclaim(parent: &Path) -> Result<fs::File, OwnerError> {
+#[derive(Debug)]
+struct ReclaimGuard(fs::File);
+
+impl Drop for ReclaimGuard {
+ fn drop(&mut self) {
+ // This lock protects a completed synchronous critical section, not an
+ // inherited descriptor's lifetime. Forked children may retain CLOEXEC
+ // copies until exec; explicitly end the section before closing ours.
+ let _ = fs2::FileExt::unlock(&self.0);
+ }
+}
+
+fn lock_reclaim(parent: &Path) -> Result<ReclaimGuard, OwnerError> {
let file = open_reclaim_file(parent)?;
file.try_lock_exclusive()
.map_err(|error| match error.kind() {
std::io::ErrorKind::WouldBlock => OwnerError::ReclaimContended,
_ => OwnerError::Io(error),
})?;
- Ok(file)
+ Ok(ReclaimGuard(file))
}
#[cfg(unix)]
@@ -320,6 +332,27 @@ pub(crate) enum OwnerError {
mod tests {
use super::*;
+ #[test]
+ fn reclaim_critical_section_ends_before_an_unrelated_inherited_descriptor_closes() {
+ let root = tempfile::tempdir().unwrap();
+ let guard = lock_reclaim(root.path()).unwrap();
+ let inherited = guard.0.try_clone().unwrap();
+ assert!(matches!(
+ lock_reclaim(root.path()),
+ Err(OwnerError::ReclaimContended)
+ ));
+ drop(guard);
+ // A fork before exec can retain this descriptor after the synchronous
+ // critical section has completed. It does not own another reclaim.
+ let next = lock_reclaim(root.path());
+ assert!(
+ next.is_ok(),
+ "a completed reclaim retained its lock: {next:?}"
+ );
+ drop(next);
+ drop(inherited);
+ }
+
#[test]
fn stale_reclaim_installs_one_exact_replacement_and_rejects_replay() {
// Parallel process tests may fork while this test holds a file lock. A child
diff --git a/crates/cpa-bridge/src/runtime/tests/fixture_isolation.rs b/crates/cpa-bridge/src/runtime/tests/fixture_isolation.rs
index 20d195dd..72b3ae5f 100644
--- a/crates/cpa-bridge/src/runtime/tests/fixture_isolation.rs
+++ b/crates/cpa-bridge/src/runtime/tests/fixture_isolation.rs
@@ -2,6 +2,52 @@
#[cfg(unix)]
use super::*;
+#[cfg(unix)]
+#[test]
+fn shutdown_preserves_owner_until_the_final_reclaim_descriptor_closes() {
+ use fs2::FileExt as _;
+ if isolated_owner_recovery_case(
+ "runtime::tests::fixture_isolation::shutdown_preserves_owner_until_the_final_reclaim_descriptor_closes",
+ ) {
+ return;
+ }
+ let root = tempfile::tempdir().unwrap();
+ let backend = Arc::new(FakeBackend::default());
+ let runtime = fixture_runtime(
+ &root,
+ Arc::clone(&backend),
+ Arc::new(FakeControl::default()),
+ 2,
+ );
+ runtime.start().unwrap();
+ let instance = root.path().join("state/fixture-cpa");
+ let owner = instance.join("owner.lock/owner.json");
+ let before = std::fs::read(&owner).unwrap();
+ let held = std::fs::OpenOptions::new()
+ .read(true)
+ .write(true)
+ .open(instance.join(".owner-reclaim.lock"))
+ .unwrap();
+ held.try_lock_exclusive().unwrap();
+ // A fork before exec retains the same open-file description even when
+ // the originating thread closes it. Reproduce that lifetime explicitly.
+ let inherited = held.try_clone().unwrap();
+ drop(held);
+ assert!(matches!(
+ runtime.shutdown(),
+ Err(CpaLifecycleError::OwnerState)
+ ));
+ assert_eq!(std::fs::read(&owner).unwrap(), before);
+ assert!(runtime.inner.lock().live.is_some());
+ assert_eq!(backend.spawn_count(), 1);
+ assert_eq!(backend.attach_count(), 0);
+ drop(inherited);
+ assert_eq!(runtime.shutdown().unwrap().code, Some(0));
+ assert!(!instance.join("owner.lock").exists());
+ assert!(runtime.inner.lock().live.is_none());
+ assert_eq!(backend.spawn_count(), 1);
+}
+
#[cfg(unix)]
#[test]
fn runtime_fixture_does_not_execute_ambient_codex() {
diff --git a/crates/daemon/src/control/runtime_observation_tests.rs b/crates/daemon/src/control/runtime_observation_tests.rs
index 261dc1db..6dda5834 100644
--- a/crates/daemon/src/control/runtime_observation_tests.rs
+++ b/crates/daemon/src/control/runtime_observation_tests.rs
@@ -164,6 +164,54 @@ fn production_writer_store_drives_application_owned_defaults_and_all_plan_scope(
hiroute_application_api::ErrorCode::CapabilityDenied
);
+ let search = hiroute_application_api::ObservationReadRequestV2::new(
+ hiroute_application_api::ObservationReadIntentV2::SessionSearch(
+ hiroute_application_api::ObservationSearchQueryV2 {
+ from_ms: now_ms - 7 * 86_400_000,
+ to_ms: now_ms + 1000,
+ session_id: None,
+ keyword: "exact-search-test".into(),
+ limit: 50,
+ cursor: None,
+ agent_id: None,
+ plan_id: None,
+ native_model: None,
+ outcome: None,
+ only_model_switch: false,
+ },
+ ),
+ );
+ let mut search_wire = LocalControlRequestV2 {
+ schema_version: LOCAL_CONTROL_SCHEMA_V2,
+ request_id: "production-session-search".into(),
+ principal: PrincipalV1::ambient_local_peer(),
+ operation_id: search.operation().into(),
+ payload: serde_json::to_value(&search).unwrap(),
+ protected_grant: None,
+ };
+ assert_eq!(
+ application
+ .dispatch(search_wire.clone())
+ .error
+ .unwrap()
+ .code,
+ hiroute_application_api::ErrorCode::CapabilityDenied
+ );
+ search_wire.protected_grant = Some(grant(
+ search.protected_operation(),
+ CanonicalDigest::of(&search).unwrap(),
+ ));
+ let searched = application.dispatch(search_wire.clone());
+ assert!(searched.error.is_none(), "{searched:?}");
+ let page = searched.data.unwrap();
+ assert!(page["hits"].is_array() && page["index_partial"].is_boolean());
+ assert!(page.get("budget_exhausted").is_none());
+ search_wire.payload["intent"]["query"]["keyword"] = json!("different");
+ assert_eq!(
+ application.dispatch(search_wire).error.unwrap().code,
+ hiroute_application_api::ErrorCode::CapabilityDenied
+ );
+
let home = hiroute_application_api::ObservationReadRequestV2::new(
hiroute_application_api::ObservationReadIntentV2::HomeValue(
hiroute_application_api::ObservationHomeValueQueryV2 {
diff --git a/crates/domain/src/observation/query.rs b/crates/domain/src/observation/query.rs
index bfdceaed..4b0f3bda 100644
--- a/crates/domain/src/observation/query.rs
+++ b/crates/domain/src/observation/query.rs
@@ -313,6 +313,15 @@ pub trait ObservationQueryPort: Send + Sync {
Err(ObservationQueryError::Unavailable)
}
+ fn search_observed_sessions(
+ &self,
+ _reader: &super::ObservationReaderContext,
+ _query: &super::ObservationSearchQueryV2,
+ _now_ms: i64,
+ ) -> Result<super::ObservationSessionSearchPageV1, ObservationQueryError> {
+ Err(ObservationQueryError::Unavailable)
+ }
+
fn search_observed_text(
&self,
_reader: &super::ObservationReaderContext,
diff --git a/crates/domain/src/observation/query_v2.rs b/crates/domain/src/observation/query_v2.rs
index fc1826b3..fd3e380a 100644
--- a/crates/domain/src/observation/query_v2.rs
+++ b/crates/domain/src/observation/query_v2.rs
@@ -120,6 +120,20 @@ impl ObservationReaderContext {
.map(|digest| digest.to_string())
.map_err(|_| ObservationQueryError::InvalidQuery)
}
+
+ /// Live pagination binds identity and admitted scope, while each page must
+ /// validate a fresh grant. An authorization revision alone is not a snapshot.
+ pub fn live_scope_binding(&self) -> Result<String, ObservationQueryError> {
+ crate::CanonicalDigest::of(&(
+ &self.workspace,
+ &self.subject,
+ &self.allowed_runs,
+ self.read_content,
+ self.search_content,
+ ))
+ .map(|digest| digest.to_string())
+ .map_err(|_| ObservationQueryError::InvalidQuery)
+ }
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
@@ -478,6 +492,15 @@ pub struct ObservationSearchPageV2 {
pub budget_exhausted: bool,
}
+/// Live conversation results, distinct from released occurrence pagination.
+#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
+pub struct ObservationSessionSearchPageV1 {
+ pub hits: Vec<ObservationTextAnchorV2>,
+ pub next_cursor: Option<String>,
+ pub index_partial: bool,
+}
+
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ObservationContentQueryV2 {
diff --git a/crates/observation/src/query/mod.rs b/crates/observation/src/query/mod.rs
index 56764f59..b9b57303 100644
--- a/crates/observation/src/query/mod.rs
+++ b/crates/observation/src/query/mod.rs
@@ -153,6 +153,16 @@ impl ObservationQueryPort for LocalObservationStore {
.map_err(crate::query_v2::ObservationV2Error::into_domain)
}
+ fn search_observed_sessions(
+ &self,
+ reader: &hiroute_domain::ObservationReaderContext,
+ query: &hiroute_domain::ObservationSearchQueryV2,
+ now_ms: i64,
+ ) -> Result<hiroute_domain::ObservationSessionSearchPageV1, ObservationQueryError> {
+ self.search_observed_sessions(reader, query, now_ms)
+ .map_err(crate::query_v2::ObservationV2Error::into_domain)
+ }
+
fn search_observed_text(
&self,
reader: &hiroute_domain::ObservationReaderContext,
diff --git a/crates/observation/src/query_v2/README.md b/crates/observation/src/query_v2/README.md
new file mode 100644
index 00000000..b7f09e54
--- /dev/null
+++ b/crates/observation/src/query_v2/README.md
@@ -0,0 +1,75 @@
+# Session content search
+
+`session_search` is the Desktop conversation-search intent on the protected V2
+observation transport. It uses the existing search filters and returns one
+`ObservationTextAnchorV2` per matching session, newest matching request first,
+then session ID ascending. Within that request it chooses the first matching
+content occurrence/block. `limit` counts sessions, and `next_cursor` means there
+are more matching sessions, never more directory windows to scan.
+
+Each page is a live query under a fresh authorized reader. The authenticated
+cursor binds the reader's identity/scope, normalized keyword, filters, limit and
+last matching session's sort position. It does not pin request/content/index
+watermarks or the global relation generation. Unrelated requests and newly
+published indexes therefore cannot invalidate continuation. Changing query or
+reader scope requires a new search. Authorization generation renewal in the same
+scope is allowed only after the current grant is validated again.
+
+Pages are not a historical snapshot. New matches before an already consumed sort
+position, or sessions moved before it by new activity, appear after refresh.
+The Desktop merges by session ID. Deletion/expiry and run-link conflict are checked
+in every page and again for returned anchors after its short read transaction.
+Opening a body independently checks current content permission and visibility.
+
+Literal, case-insensitive substring semantics use the same per-character Unicode
+lowercase mapping for keyword and stored blocks. The rebuildable SQLite keyword
+lookup indexes unique one-, two- and three-character grams, encoded as ASCII
+tokens. A folded keyword of at most three characters is a single complete gram,
+so its lookup is exact. Longer keywords intersect their trigrams and verify the
+folded substring before selecting a content occurrence. Chinese, punctuation and
+short words need no word tokenizer. Anchors retain original UTF-8 byte offsets, including lowercase
+expansion and overlapping/cross-block matches.
+The live intent accepts up to 256 Unicode characters; it does not impose the
+released occurrence intent's 256-byte limit on Chinese input.
+
+Only digest-verified, live published sources with a complete keyword lookup can
+produce hits. Existing released blocks are backfilled in bounded maintenance
+cycles without rewriting requests, bodies, dates or identities. Interrupted
+backfills remain unpublished and resume idempotently. Block deletion removes its
+lookup entries in the same transaction. Missing/failed/pending authorized indexes
+set `index_partial`; no ready match is a complete empty result only when that flag
+is false. Four reader permits and the existing 500 ms SQLite deadline remain;
+exceeding the query budget is a failure/retry, not a misleading empty result or
+an exposed scan-window cursor.
+The SQL scans source identities through their index and tests the shared keyword
+block-rowid set before checking source publication or reading folded text.
+A rebuildable complete-content covering index supplies workspace/digest, content
+kind, media type, request identity and the implicit occurrence rowid. It chooses
+the first eligible matching content per request, then checks request authority
+and filters once before ranking sessions. Both sparse matches and common short
+keywords avoid loading every content occurrence or folded body. Returned anchors
+always verify the exact folded substring and load original offset maps. Current
+reads use returned content identities. A partial covering index on ready-source
+identities keeps publication checks in metadata; it still requires the complete
+lookup publication marker. A workspace-local ready-source set skips complete
+digests before inspecting missing sources under the reader's
+filters; a missing source index or incomplete publication still reports a gap.
+Scale regressions include distinct source digests and common short keywords;
+repeating many occurrences of a single blob does not cover these query costs.
+
+The released `search` intent retains occurrence pagination and its V2 response
+shape. It is a distinct supported interface, not the Desktop session-list query.
+
+Desktop shows searching, actual matching sessions, complete empty, partial index,
+or failure with retry. A failure withdraws continuation and retry starts a new
+query. Query changes invalidate delayed responses immediately and clear old
+results so new keywords cannot act on a previous cursor.
+Search results stay in the list until the user opens a matching conversation.
+At the minimum window size an unselected search uses the available pane, and a
+continuation failure keeps its retry action visible above the retained results.
+
+Regressions belong beside `session_search.rs` and `text_index/lookup.rs`. Browser
+scenarios exercise the production Sessions component; native acceptance must
+type the keyword in Desktop, find a known body after a large nonmatching corpus,
+continue across unrelated Worker activity and open the original anchor. Browser
+fixtures alone do not establish authorization or product completion.
diff --git a/crates/observation/src/query_v2/mod.rs b/crates/observation/src/query_v2/mod.rs
index 86da4762..446f12da 100644
--- a/crates/observation/src/query_v2/mod.rs
+++ b/crates/observation/src/query_v2/mod.rs
@@ -14,6 +14,9 @@ mod search;
mod search_tests;
#[cfg(test)]
mod search_window_tests;
+mod session_search;
+#[cfg(test)]
+mod session_search_tests;
mod sessions;
#[cfg(test)]
mod sessions_tests;
diff --git a/crates/observation/src/query_v2/session_search.rs b/crates/observation/src/query_v2/session_search.rs
new file mode 100644
index 00000000..49cfbbab
--- /dev/null
+++ b/crates/observation/src/query_v2/session_search.rs
@@ -0,0 +1,335 @@
+use super::*;
+use hiroute_domain::{
+ CanonicalDigest, ObservationSearchQueryV2, ObservationSessionSearchPageV1,
+ ObservationTextAnchorV2,
+};
+use rusqlite::{Connection, OpenFlags, params};
+use serde::{Deserialize, Serialize};
+use std::{collections::HashMap, time::Instant};
+
+#[derive(Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
+struct Cursor {
+ schema: String,
+ binding: String,
+ last_at: i64,
+ last_id: String,
+}
+
+// These predicates are shared by hits, gaps and the final current read.
+// Only content in the reader's workspace/run/query scope can contribute a hit
+// or an index gap; cache readiness never grants visibility on its own.
+const CONTENT_FILTERS: &str = "c.workspace_id=?1 AND c.state='complete'
+ AND c.content_kind NOT IN ('provider_state','reasoning_delta','reasoning_finished')
+ AND (c.canonical_media_type LIKE 'text/%' OR c.canonical_media_type='application/json' OR c.canonical_media_type LIKE 'application/vnd.hiroute.%')";
+const REQUEST_FILTERS: &str = "r.started_at_ms>=?2 AND r.started_at_ms<?3
+ AND (?4 IS NULL OR (l.conflicted=0 AND l.run_id IN(SELECT value FROM json_each(?4))))
+ AND (?5 IS NULL OR r.session_id=?5) AND (?6 IS NULL OR s.agent_id=?6)
+ AND (?7 IS NULL OR EXISTS(SELECT 1 FROM valuation_requests_v2 v WHERE v.workspace_id=r.workspace_id AND v.request_id=r.request_id AND v.plan_id=?7))
+ AND (?8 IS NULL OR EXISTS(SELECT 1 FROM observation_attempt_models_v2 a WHERE a.workspace_id=r.workspace_id AND a.request_id=r.request_id AND a.model_id=?8))
+ AND (?9 IS NULL OR r.outcome=?9)
+ AND (s.tombstone_reason IS NULL OR EXISTS(SELECT 1 FROM observation_tombstones t WHERE t.workspace_id=s.workspace_id AND t.session_id=s.session_id AND t.delete_scope='content_only'))";
+const READY_SOURCES: &str =
+ "SELECT i.digest FROM observation_text_index_v2 i INDEXED BY observation_text_index_ready
+ JOIN observation_text_lookup_sources_v1 k ON k.workspace=i.workspace AND k.digest=i.digest
+ WHERE i.workspace=?1 AND i.state='ready'";
+
+fn all_filters() -> String {
+ format!("{CONTENT_FILTERS} AND {REQUEST_FILTERS}")
+}
+
+const REQUEST_JOINS: &str =
+ // Keep known content/request identities driving PK lookups. Reordering
+ // through the request time range scatters the large readiness scan and
+ // amplifies common keyword blocks by every request in the range.
+ "CROSS JOIN logical_requests r ON r.workspace_id=c.workspace_id AND r.request_id=c.request_id
+ CROSS JOIN sessions s ON s.workspace_id=r.workspace_id AND s.session_id=r.session_id
+ LEFT JOIN observation_run_links l ON l.workspace_id=r.workspace_id AND l.request_id=r.request_id";
+
+fn switch_filter(enabled: bool) -> &'static str {
+ if enabled {
+ " AND ((SELECT COUNT(DISTINCT model_id) FROM observation_attempt_models_v2 a WHERE a.workspace_id=r.workspace_id AND a.request_id=r.request_id)>1 OR EXISTS(SELECT 1 FROM turn_changes t WHERE t.request_id=r.request_id AND t.changed=1))"
+ } else {
+ ""
+ }
+}
+
+fn prefix(enabled: bool) -> String {
+ if enabled {
+ super::turns::cte(4, "9223372036854775807")
+ } else {
+ String::new()
+ }
+}
+
+pub(super) fn candidate_sql(only_switch: bool, exact_gram: bool) -> String {
+ let with = if only_switch {
+ format!("{}, matched_content AS (", prefix(true))
+ } else {
+ "WITH matched_content AS (".into()
+ };
+ // One complete encoded gram is exact for a folded needle of 1..=3 scalars.
+ // Longer needles need substring verification before choosing any content.
+ // Scan source identities and covering content metadata, not every folded
+ // body. FTS membership precedes ready-source checks and optional body reads;
+ // grouping lets request authority run once before session ranking.
+ let exact = if exact_gram {
+ ""
+ } else {
+ " AND instr(b.folded,?10)>0"
+ };
+ format!(
+ "{with}
+ SELECT c.workspace_id,c.request_id,MIN(c.rowid) AS content_rowid
+ FROM observation_text_blocks_v2 b INDEXED BY observation_text_blocks_source
+ CROSS JOIN content_instances_v2 c INDEXED BY content_instances_v2_search_blob
+ ON c.workspace_id=b.workspace AND c.content_blob_digest=b.digest
+ WHERE b.workspace=?1
+ AND b.rowid IN(SELECT rowid FROM observation_text_lookup_v1 WHERE observation_text_lookup_v1 MATCH ?11)
+ AND EXISTS(SELECT 1 FROM ({READY_SOURCES}) ready WHERE ready.digest=b.digest)
+ {exact} AND {CONTENT_FILTERS}
+ GROUP BY c.workspace_id,c.request_id
+ ), matched_requests AS (
+ SELECT r.session_id,r.request_id,r.started_at_ms AS last_at,c.content_rowid
+ FROM matched_content c {REQUEST_JOINS}
+ WHERE {REQUEST_FILTERS}{}
+ ), ranked AS (
+ SELECT *,ROW_NUMBER() OVER(PARTITION BY session_id ORDER BY last_at DESC,request_id) AS pick
+ FROM matched_requests
+ ), page AS (
+ SELECT * FROM ranked WHERE pick=1 AND (last_at<?12 OR (last_at=?12 AND session_id>?13))
+ ORDER BY last_at DESC,session_id LIMIT ?14
+ )
+ SELECT p.session_id,p.request_id,c.message_instance_id,c.content_id,
+ b.original_start,substr(b.offsets,(instr(b.folded,?10)-1)*8+5,4) AS original_offset,
+ NULL AS link,p.last_at
+ FROM page p
+ CROSS JOIN content_instances_v2 c ON c.rowid=p.content_rowid
+ CROSS JOIN observation_text_blocks_v2 b ON b.rowid=(
+ SELECT first.rowid FROM observation_text_blocks_v2 first
+ WHERE first.workspace=c.workspace_id AND first.digest=c.content_blob_digest
+ AND instr(first.folded,?10)>0 ORDER BY first.ordinal LIMIT 1)
+ ORDER BY p.last_at DESC,p.session_id",
+ switch_filter(only_switch)
+ )
+}
+
+pub(super) fn index_partial_sql(only_switch: bool) -> String {
+ // The uncorrelated, workspace-local ready set is built once. Check the
+ // digest index before loading content metadata or authorizing a gap; ready
+ // sources cannot contribute a gap regardless of their visibility. Pin this
+ // order so the request-search index cannot scatter the complete gap scan.
+ let filters = all_filters();
+ format!(
+ "{} SELECT EXISTS(
+ SELECT 1 FROM content_instances_v2 c INDEXED BY content_instances_v2_live_blob {REQUEST_JOINS}
+ WHERE (c.content_blob_digest IS NULL OR c.content_blob_digest NOT IN({READY_SOURCES}))
+ AND {filters}{})",
+ prefix(only_switch),
+ switch_filter(only_switch)
+ )
+}
+
+pub(super) fn live_read_sql(only_switch: bool) -> String {
+ // Returned identities, not all workspace content, drive the current read.
+ let filters = all_filters();
+ format!("{} SELECT c.content_id,CASE WHEN l.conflicted=0 THEN l.body_json ELSE NULL END
+ FROM json_each(?10) ids CROSS JOIN content_instances_v2 c ON c.workspace_id=?1 AND c.content_id=ids.value
+ {REQUEST_JOINS} WHERE {filters}{}",prefix(only_switch),switch_filter(only_switch))
+}
+
+fn native_turn(body: Option<String>) -> Option<String> {
+ body.and_then(|json| serde_json::from_str::<RunObservationLink>(&json).ok())
+ .and_then(|link| link.native_turn_id)
+}
+
+impl crate::LocalObservationStore {
+ pub fn search_observed_sessions(
+ &self,
+ reader: &ObservationReaderContext,
+ query: &ObservationSearchQueryV2,
+ now_ms: i64,
+ ) -> Result<ObservationSessionSearchPageV1, ObservationV2Error> {
+ reader.check(now_ms, true, true)?;
+ let started = Instant::now();
+ let keyword = query.keyword.trim();
+ if keyword.is_empty()
+ || keyword.chars().count() > 256
+ || query.from_ms < 0
+ || query.from_ms >= query.to_ms
+ || query.limit == 0
+ || query.limit > 200
+ || [
+ &query.session_id,
+ &query.agent_id,
+ &query.plan_id,
+ &query.native_model,
+ &query.outcome,
+ ]
+ .into_iter()
+ .flatten()
+ .any(|id| !identifier(id))
+ {
+ return Err(ObservationV2Error::Invalid);
+ }
+ let _permit = self.query_permit()?;
+ let needle = crate::text_index::fold(keyword).0;
+ let expression = crate::text_index::lookup::expression(&needle);
+ let mut normalized = query.clone();
+ normalized.keyword = needle.clone();
+ normalized.cursor = None;
+ let binding = CanonicalDigest::of(&(reader.live_scope_binding()?, normalized))
+ .map_err(|_| ObservationV2Error::Invalid)?
+ .to_string();
+ let cursor = match &query.cursor {
+ Some(encoded) => {
+ let cursor: Cursor = self.decode_observation_cursor(encoded)?;
+ if cursor.schema != "session-search/v1" || cursor.binding != binding {
+ return Err(ObservationV2Error::Stale);
+ }
+ cursor
+ }
+ None => Cursor {
+ schema: "session-search/v1".into(),
+ binding,
+ last_at: i64::MAX,
+ last_id: String::new(),
+ },
+ };
+ let runs = reader
+ .allowed_runs()
+ .map(serde_json::to_string)
+ .transpose()
+ .map_err(|_| ObservationV2Error::Invalid)?;
+ let from = query.from_ms.max(
+ now_ms
+ .saturating_sub(crate::managed_text::RETENTION_MS)
+ .saturating_add(1),
+ );
+ let mut connection =
+ Connection::open_with_flags(&self.activity_path, OpenFlags::SQLITE_OPEN_READ_ONLY)?;
+ let _deadline = QueryDeadline::start_for(&connection, reader)?;
+ let tx = connection.transaction()?;
+ let mut hits = Vec::new();
+ let mut last_at = 0;
+ {
+ let mut stmt = tx.prepare(&candidate_sql(
+ query.only_model_switch,
+ needle.chars().count() <= 3,
+ ))?;
+ let mut rows = stmt.query(params![
+ reader.workspace().as_str(),
+ from,
+ query.to_ms,
+ runs,
+ query.session_id,
+ query.agent_id,
+ query.plan_id,
+ query.native_model,
+ query.outcome,
+ needle,
+ expression,
+ cursor.last_at,
+ cursor.last_id,
+ u64::from(query.limit) + 1
+ ])?;
+ while let Some(row) = rows.next()? {
+ let offset: Vec<u8> = row.get(5)?;
+ let offset = u32::from_le_bytes(
+ offset
+ .try_into()
+ .map_err(|_| ObservationV2Error::Unavailable)?,
+ );
+ let start: u64 = row.get(4)?;
+ hits.push(ObservationTextAnchorV2 {
+ session_id: row.get(0)?,
+ request_id: row.get(1)?,
+ native_turn_id: native_turn(row.get(6)?),
+ message_occurrence_id: row.get(2)?,
+ content_id: row.get(3)?,
+ original_text_offset: start + u64::from(offset),
+ match_kind: "literal_text".into(),
+ });
+ if hits.len() <= usize::from(query.limit) {
+ last_at = row.get(7)?;
+ }
+ }
+ }
+ let more = hits.len() > usize::from(query.limit);
+ hits.truncate(usize::from(query.limit));
+ let next_cursor = if more {
+ Some(
+ self.encode_observation_cursor(&Cursor {
+ last_at,
+ last_id: hits
+ .last()
+ .ok_or(ObservationV2Error::Unavailable)?
+ .session_id
+ .clone(),
+ ..cursor
+ })?,
+ )
+ } else {
+ None
+ };
+ let index_partial = tx.query_row(
+ &index_partial_sql(query.only_model_switch),
+ params![
+ reader.workspace().as_str(),
+ from,
+ query.to_ms,
+ runs,
+ query.session_id,
+ query.agent_id,
+ query.plan_id,
+ query.native_model,
+ query.outcome
+ ],
+ |r| r.get(0),
+ )?;
+ tx.commit()?;
+
+ // Re-read just the returned identities, rather than rejecting every page
+ // when unrelated append-only activity increments the global generation.
+ if !hits.is_empty() {
+ let identities =
+ serde_json::to_string(&hits.iter().map(|h| &h.content_id).collect::<Vec<_>>())
+ .map_err(|_| ObservationV2Error::Invalid)?;
+ let mut stmt = connection.prepare(&live_read_sql(query.only_model_switch))?;
+ let live: HashMap<String, Option<String>> = stmt
+ .query_map(
+ params![
+ reader.workspace().as_str(),
+ from,
+ query.to_ms,
+ runs,
+ query.session_id,
+ query.agent_id,
+ query.plan_id,
+ query.native_model,
+ query.outcome,
+ identities
+ ],
+ |r| Ok((r.get(0)?, native_turn(r.get(1)?))),
+ )?
+ .collect::<Result<_, _>>()?;
+ hits.retain_mut(|hit| match live.get(&hit.content_id) {
+ Some(turn) => {
+ hit.native_turn_id.clone_from(turn);
+ true
+ }
+ None => false,
+ });
+ }
+ reader.check(
+ now_ms.saturating_add(started.elapsed().as_millis() as i64),
+ true,
+ true,
+ )?;
+ Ok(ObservationSessionSearchPageV1 {
+ hits,
+ next_cursor,
+ index_partial,
+ })
+ }
+}
diff --git a/crates/observation/src/query_v2/session_search_tests.rs b/crates/observation/src/query_v2/session_search_tests.rs
new file mode 100644
index 00000000..6704d185
--- /dev/null
+++ b/crates/observation/src/query_v2/session_search_tests.rs
@@ -0,0 +1,678 @@
+use super::{
+ search_tests::{index, query, reader, source},
+ *,
+};
+use crate::{DigestAuthority, LocalObservationStore};
+use hiroute_domain::*;
+use rusqlite::params;
+
+#[test]
+fn live_search_large_distinct_source_cache_keeps_indexed_anchors_and_complete_pagination() {
+ let root = tempfile::tempdir().unwrap();
+ let store = LocalObservationStore::open(root.path(), DigestAuthority::new([2; 32])).unwrap();
+ {
+ let mut db = store.connection.lock();
+ let tx = db.transaction().unwrap();
+ for n in 0..111 {
+ tx.execute("INSERT INTO sessions(workspace_id,session_id,started_at_ms,updated_at_ms,agent_id) VALUES(?1,?2,?3,?3,'scale-agent')",params![WorkspaceId::DEFAULT,format!("scale-{n}"),100+n]).unwrap();
+ tx.execute("INSERT INTO logical_requests(workspace_id,request_id,session_id,turn_id,started_at_ms,outcome) VALUES(?1,?2,?3,'internal',?4,'accepted')",params![WorkspaceId::DEFAULT,format!("request-{n}"),format!("scale-{n}"),100+n]).unwrap();
+ }
+ // Component cache fixture: unlike the repeated single-blob corpus, each
+ // row has its own real digest, folded block and production lookup grams.
+ // Native acceptance separately proves capture, publication and body reads.
+ for n in 0..30_000 {
+ let body = if n < 51 {
+ format!("checksum {n}")
+ } else {
+ format!("ordinary background e ch {n}")
+ };
+ let digest = store
+ .authority
+ .content_blob_digest("text/plain", body.as_bytes())
+ .to_string();
+ let (folded, offsets) = crate::text_index::fold(&body);
+ tx.execute("INSERT INTO content_instances_v2(workspace_id,content_id,message_instance_id,request_id,direction,fork_id,part_ordinal,content_kind,canonical_media_type,content_blob_digest,has_content_ref,accumulated_byte_count,state,created_at_unix_nanos) VALUES(?1,?2,?2,?3,'request','fork',?4,'text','text/plain',?5,0,?6,'complete',100000000)",params![WorkspaceId::DEFAULT,format!("content-{n}"),format!("request-{}",n%111),n,digest,body.len()]).unwrap();
+ tx.execute(
+ "INSERT INTO observation_text_index_v2 VALUES(?1,?2,'ready',1)",
+ params![WorkspaceId::DEFAULT, digest],
+ )
+ .unwrap();
+ tx.execute(
+ "INSERT INTO observation_text_blocks_v2 VALUES(?1,?2,0,0,0,?3,?4,?5)",
+ params![WorkspaceId::DEFAULT, digest, folded, offsets, body],
+ )
+ .unwrap();
+ crate::text_index::insert_lookup_fixture(&tx, tx.last_insert_rowid(), &folded).unwrap();
+ tx.execute(
+ "INSERT INTO observation_text_lookup_sources_v1 VALUES(?1,?2)",
+ params![WorkspaceId::DEFAULT, digest],
+ )
+ .unwrap();
+ }
+ tx.commit().unwrap();
+ // Matching reads source identities and covering content metadata, not
+ // all folded bodies or every occurrence of a nonmatching request.
+ // Gap and final reads retain their supplied-identity lookup boundaries.
+ use rusqlite::types::Value;
+ let mut bindings = vec![
+ Value::Text(WorkspaceId::DEFAULT.into()),
+ Value::Integer(0),
+ Value::Integer(1000),
+ ];
+ bindings.extend(std::iter::repeat_n(Value::Null, 6));
+ let mut hit_bindings = bindings.clone();
+ hit_bindings.extend([
+ Value::Text("checksum".into()),
+ Value::Text(crate::text_index::lookup::expression("checksum")),
+ Value::Integer(i64::MAX),
+ Value::Text(String::new()),
+ Value::Integer(51),
+ ]);
+ let mut short_bindings = hit_bindings.clone();
+ short_bindings[9] = Value::Text("e".into());
+ short_bindings[10] = Value::Text(crate::text_index::lookup::expression("e"));
+ let mut live_bindings = bindings.clone();
+ live_bindings.push(Value::Text(
+ serde_json::to_string(&(0..50).map(|n| format!("content-{n}")).collect::<Vec<_>>())
+ .unwrap(),
+ ));
+ for (sql, bindings, exact_gram) in [
+ (
+ super::session_search::index_partial_sql(false),
+ bindings,
+ None,
+ ),
+ (
+ super::session_search::candidate_sql(false, false),
+ hit_bindings,
+ Some(false),
+ ),
+ (
+ super::session_search::candidate_sql(false, true),
+ short_bindings,
+ Some(true),
+ ),
+ (
+ super::session_search::live_read_sql(false),
+ live_bindings,
+ None,
+ ),
+ ] {
+ let returned_identities = bindings.len() == 10;
+ let mut stmt = db.prepare(&format!("EXPLAIN QUERY PLAN {sql}")).unwrap();
+ let plan = stmt
+ .query_map(rusqlite::params_from_iter(&bindings), |r| {
+ Ok((
+ r.get::<_, i64>(0)?,
+ r.get::<_, i64>(1)?,
+ r.get::<_, String>(3)?,
+ ))
+ })
+ .unwrap()
+ .collect::<Result<Vec<_>, _>>()
+ .unwrap();
+ if !returned_identities {
+ assert!(
+ plan.iter().any(|(_, _, step)| step.starts_with(
+ "SEARCH i USING COVERING INDEX observation_text_index_ready "
+ ) && step.contains("(workspace=?")),
+ "source readiness must not load every index table row: {plan:?}"
+ );
+ }
+ if let Some(exact_gram) = exact_gram {
+ assert!(
+ plan.iter().any(|(_, _, step)| step.starts_with(
+ "SEARCH c USING COVERING INDEX content_instances_v2_search_blob "
+ ) && step
+ .contains("(workspace_id=? AND content_blob_digest=?)")),
+ "matching metadata must remain covered: {plan:?}"
+ );
+ assert_eq!(
+ plan.iter()
+ .filter(|(_, _, step)| step
+ .contains("observation_text_lookup_v1 VIRTUAL TABLE"))
+ .count(),
+ 1,
+ "the shared keyword candidate set must be built once: {plan:?}"
+ );
+ let index = if exact_gram {
+ "SEARCH b USING COVERING INDEX observation_text_blocks_source "
+ } else {
+ "SEARCH b USING INDEX observation_text_blocks_source "
+ };
+ assert!(
+ plan.iter()
+ .any(|(_, _, step)| step.starts_with(index)
+ && step.contains("(workspace=?)")),
+ "source identity scan must precede readiness; short grams must avoid folded bodies: {plan:?}"
+ );
+ } else if !returned_identities {
+ assert!(
+ plan.iter().any(|(_, _, step)| step
+ .starts_with("SEARCH c USING INDEX content_instances_v2_live_blob ")
+ && step.contains("(workspace_id=?)")),
+ "gap checks must traverse digest order before loading request metadata: {plan:?}"
+ );
+ }
+ assert!(
+ plan.iter().any(|(_, _, step)| step.starts_with("SEARCH r ")
+ && step.contains("(workspace_id=? AND request_id=?)")),
+ "request identity lookup required: {plan:?}"
+ );
+ if returned_identities {
+ assert!(
+ plan.iter().any(|(_, _, step)| step.starts_with("SEARCH c ")
+ && step.contains("(workspace_id=? AND content_id=?)")),
+ "returned content identity lookup required: {plan:?}"
+ );
+ }
+ }
+ }
+ let mut q = query("checksum", 50);
+ let first = store
+ .search_observed_sessions(&reader(true), &q, 500)
+ .unwrap();
+ assert_eq!(first.hits.len(), 50);
+ assert!(!first.index_partial);
+ assert!(first.hits.iter().all(|h| h.original_text_offset == 0));
+ q.cursor = first.next_cursor;
+ assert!(q.cursor.is_some());
+ let last = store
+ .search_observed_sessions(&reader(true), &q, 500)
+ .unwrap();
+ assert_eq!(last.hits.len(), 1);
+ assert_eq!(last.hits[0].session_id, "scale-0");
+ assert!(last.next_cursor.is_none() && !last.index_partial);
+ // A common one-character keyword exercises nearly every distinct block,
+ // including the final current-visibility read under the same 500 ms budget.
+ for word in ["e", "ch", "o"] {
+ let common = store
+ .search_observed_sessions(&reader(true), &query(word, 200), 500)
+ .unwrap_or_else(|error| panic!("common keyword {word} failed: {error:?}"));
+ assert_eq!(common.hits.len(), 111, "{word}");
+ assert!(
+ common.next_cursor.is_none() && !common.index_partial,
+ "{word}"
+ );
+ }
+ let mut short = query("o", 200);
+ short.limit = 50;
+ let common = store
+ .search_observed_sessions(&reader(true), &short, 500)
+ .unwrap();
+ assert_eq!(common.hits.len(), 50);
+ assert!(common.next_cursor.is_some() && !common.index_partial);
+ let missing = store
+ .search_observed_sessions(&reader(true), &query("absent", 50), 500)
+ .unwrap();
+ assert!(missing.hits.is_empty() && !missing.index_partial);
+ store.connection.lock().execute("DELETE FROM observation_text_lookup_sources_v1 WHERE digest=(SELECT content_blob_digest FROM content_instances_v2 WHERE content_id='content-29999')",[]).unwrap();
+ let pending = store
+ .search_observed_sessions(&reader(true), &query("absent", 50), 500)
+ .unwrap();
+ assert!(pending.hits.is_empty() && pending.index_partial);
+ let mut hidden = query("absent", 50);
+ hidden.agent_id = Some("other-agent".into());
+ let hidden = store
+ .search_observed_sessions(&reader(true), &hidden, 500)
+ .unwrap();
+ assert!(hidden.hits.is_empty() && !hidden.index_partial);
+}
+
+fn add(store: &LocalObservationStore, session: &str, request: &str, at: i64, text: &str) {
+ let digest = store
+ .authority
+ .content_blob_digest("text/plain", text.as_bytes())
+ .to_string();
+ let path = store
+ .activity_path
+ .with_file_name(format!("source-{request}"));
+ std::fs::write(&path, text).unwrap();
+ let db = store.connection.lock();
+ db.execute("INSERT OR IGNORE INTO sessions(workspace_id,session_id,started_at_ms,updated_at_ms,agent_id) VALUES(?1,?2,?3,?3,'test-agent')",params![WorkspaceId::DEFAULT,session,at]).unwrap();
+ db.execute("INSERT INTO logical_requests(workspace_id,request_id,session_id,turn_id,started_at_ms,outcome) VALUES(?1,?2,?3,'internal',?4,'accepted')",params![WorkspaceId::DEFAULT,request,session,at]).unwrap();
+ db.execute("INSERT OR IGNORE INTO content_blobs_v2(workspace_id,blob_digest,object_path,byte_count,media_type,state,created_at_unix_nanos) VALUES(?1,?2,?3,?4,'text/plain','complete',?5)",params![WorkspaceId::DEFAULT,digest,path.to_string_lossy(),text.len(),at*1_000_000]).unwrap();
+ db.execute("INSERT INTO content_instances_v2(workspace_id,content_id,message_instance_id,request_id,direction,fork_id,part_ordinal,content_kind,canonical_media_type,content_blob_digest,has_content_ref,accumulated_byte_count,state,created_at_unix_nanos)
+ VALUES(?1,?2,?3,?3,'request','fork',0,'text','text/plain',?4,0,?5,'complete',?6)",params![WorkspaceId::DEFAULT,format!("content-{request}"),request,digest,text.len(),at*1_000_000]).unwrap();
+}
+
+#[test]
+fn session_search_reaches_late_matches_without_directory_pages_and_survives_new_worker_links() {
+ let root = tempfile::tempdir().unwrap();
+ let store = LocalObservationStore::open(root.path(), DigestAuthority::new([2; 32])).unwrap();
+ source(&store, b"background");
+ store.connection.lock().execute_batch("WITH RECURSIVE ids(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM ids WHERE n<27800)
+ INSERT INTO content_instances_v2 SELECT workspace_id,'copy-'||n,message_instance_id,request_id,direction,fork_id,part_ordinal,content_kind,canonical_media_type,content_blob_digest,expected_byte_count,has_content_ref,transport_frame_id,downstream_delivery,accumulated_byte_count,state,created_at_unix_nanos FROM content_instances_v2,ids WHERE content_id='content';").unwrap();
+ add(
+ &store,
+ "match-new",
+ "new",
+ 300,
+ "checksum checksum checksum",
+ );
+ add(&store, "match-middle", "middle", 200, "checksum");
+ add(&store, "match-old", "old", 150, "checksum");
+ index(&store);
+ let legacy = store
+ .search_observed_text(&reader(true), &query("checksum", 1), 500)
+ .unwrap();
+ assert!(legacy.hits.is_empty() && legacy.budget_exhausted && legacy.next_cursor.is_some());
+ let mut q = query("checksum", 1);
+ let first = store
+ .search_observed_sessions(&reader(true), &q, 500)
+ .unwrap();
+ assert_eq!(first.hits.len(), 1);
+ assert_eq!(first.hits[0].session_id, "match-new");
+ assert!(!first.index_partial);
+ q.cursor = first.next_cursor;
+ add(&store, "unrelated", "worker", 800, "another job");
+ store
+ .link_observed_request(&super::tests::link("worker", "another-run"))
+ .unwrap();
+ let renewed = ObservationReaderContext::local_user(
+ WorkspaceId::default(),
+ "user".into(),
+ 2,
+ 12000,
+ true,
+ true,
+ )
+ .unwrap();
+ let second = store.search_observed_sessions(&renewed, &q, 500).unwrap();
+ assert_eq!(
+ second
+ .hits
+ .iter()
+ .map(|h| h.session_id.as_str())
+ .collect::<Vec<_>>(),
+ ["match-middle"]
+ );
+ q.cursor = second.next_cursor;
+ // A newly published old match is live, rather than excluded by a frozen
+ // content/publication watermark. It is behind the consumed sort position.
+ add(&store, "match-late-index", "late-index", 175, "checksum");
+ index(&store);
+ let third = store.search_observed_sessions(&renewed, &q, 500).unwrap();
+ assert_eq!(third.hits[0].session_id, "match-late-index");
+ q.cursor = third.next_cursor;
+ let last = store.search_observed_sessions(&renewed, &q, 500).unwrap();
+ assert_eq!(last.hits[0].session_id, "match-old");
+ assert!(last.next_cursor.is_none());
+ // The exact pre-change occurrence reader exhibits the original stale
+ // behavior after the same real relation mutation.
+ let old = ObservationSearchQueryV2 {
+ cursor: legacy.next_cursor,
+ ..query("checksum", 1)
+ };
+ assert_eq!(
+ store.search_observed_text(&reader(true), &old, 500),
+ Err(ObservationV2Error::Stale)
+ );
+}
+
+#[test]
+fn session_search_literal_grams_preserve_short_unicode_cross_block_and_original_body_anchors() {
+ let root = tempfile::tempdir().unwrap();
+ let store = LocalObservationStore::open(root.path(), DigestAuthority::new([2; 32])).unwrap();
+ let prefix = "a".repeat(64 * 1024 - 4);
+ let long_chinese = "中文".repeat(128);
+ let body = format!("{prefix}NeEdLe 甲İ乙 中文检索 abcbcd . OR \" ΣΟΣ\0尾字 {long_chinese}");
+ source(&store, body.as_bytes());
+ index(&store);
+ for (keyword, literal) in [
+ ("needle", "NeEdLe"),
+ ("乙", "乙"),
+ ("中文", "中文"),
+ ("文检索", "文检索"),
+ ("İ", "İ"),
+ ("ΣΟΣ", "ΣΟΣ"),
+ (".", "."),
+ ("OR \"", "OR \""),
+ ("尾字", "尾字"),
+ ] {
+ let page = store
+ .search_observed_sessions(&reader(true), &query(keyword, 10), 500)
+ .unwrap();
+ assert_eq!(page.hits.len(), 1, "{keyword}");
+ assert!(page.next_cursor.is_none());
+ let hit = &page.hits[0];
+ assert_eq!(
+ hit.original_text_offset,
+ body.find(literal).unwrap() as u64,
+ "{keyword}"
+ );
+ let content = store
+ .observed_content_page(
+ &reader(true),
+ &ObservationContentQueryV2 {
+ request_id: LogicalRequestId::parse(hit.request_id.clone()).unwrap(),
+ content_id: hit.content_id.clone(),
+ anchor_offset: Some(hit.original_text_offset),
+ cursor: None,
+ },
+ 500,
+ )
+ .unwrap();
+ let start = content.chunks[0].original_byte_offset;
+ let visible: String = content.chunks.iter().map(|c| c.text.as_str()).collect();
+ assert!(visible[(hit.original_text_offset - start) as usize..].starts_with(literal));
+ }
+ let page = store
+ .search_observed_sessions(&reader(true), &query(&long_chinese, 10), 500)
+ .unwrap();
+ assert_eq!(
+ page.hits[0].original_text_offset,
+ body.find(&long_chinese).unwrap() as u64
+ );
+ assert_eq!(
+ store.search_observed_sessions(&reader(true), &query(&"字".repeat(257), 10), 500),
+ Err(ObservationV2Error::Invalid)
+ );
+ // Trigram intersection is only candidate evidence, never an exact hit.
+ assert!(
+ store
+ .search_observed_sessions(&reader(true), &query("abcd", 10), 500)
+ .unwrap()
+ .hits
+ .is_empty()
+ );
+}
+
+#[test]
+fn folded_scalar_count_keeps_long_trigram_intersections_out_of_session_ranking() {
+ let root = tempfile::tempdir().unwrap();
+ let store = LocalObservationStore::open(root.path(), DigestAuthority::new([2; 32])).unwrap();
+ // Two input scalars expand to four folded scalars. The newer request has
+ // both trigrams without the full substring; it must not hide the older hit.
+ add(&store, "folded", "false-grams", 200, "i̇i / ̇i̇");
+ add(&store, "folded", "exact-folded", 100, "İİ");
+ index(&store);
+ let page = store
+ .search_observed_sessions(&reader(true), &query("İİ", 10), 500)
+ .unwrap();
+ assert_eq!(page.hits.len(), 1);
+ assert_eq!(page.hits[0].request_id, "exact-folded");
+ assert_eq!(page.hits[0].original_text_offset, 0);
+ assert!(page.next_cursor.is_none() && !page.index_partial);
+}
+
+#[test]
+fn released_block_cache_backfills_without_changing_bodies_offsets_or_occurrence_search() {
+ let root = tempfile::tempdir().unwrap();
+ let authority = DigestAuthority::new([2; 32]);
+ let store = LocalObservationStore::open(root.path(), authority.clone()).unwrap();
+ let digest = source(&store, "xx甲İNeedle乙".as_bytes());
+ {
+ let db = store.connection.lock();
+ db.execute_batch(
+ "DROP TRIGGER observation_text_lookup_delete_block;
+ DROP TRIGGER observation_text_lookup_delete_source;
+ DROP INDEX content_instances_v2_search_blob;
+ DROP TABLE observation_text_lookup_v1; DROP TABLE observation_text_lookup_sources_v1;
+ DROP TABLE observation_text_blocks_v2; DROP TABLE observation_text_index_v2;",
+ )
+ .unwrap();
+ db.execute_batch(include_str!(
+ "../../tests/fixtures/released-v0.3.1-text-index.sql"
+ ))
+ .unwrap();
+ db.execute(
+ "INSERT INTO observation_text_index_v2 VALUES(?1,?2,'ready',1)",
+ params![WorkspaceId::DEFAULT, digest],
+ )
+ .unwrap();
+ // Frozen output of the released per-character lowercase producer for
+ // xx甲İNeedle乙. In particular İ expands while its original bytes do not.
+ db.execute("INSERT INTO observation_text_blocks_v2 VALUES(?1,?2,0,0,0,'xx甲i̇needle乙',X'00000000000000000100000001000000020000000200000005000000050000000600000005000000080000000700000009000000080000000a000000090000000b0000000a0000000c0000000b0000000d0000000c0000000e0000000d0000001100000010000000','xx甲İNeedle乙')",params![WorkspaceId::DEFAULT,digest]).unwrap();
+ }
+ drop(store);
+ let store = LocalObservationStore::open(root.path(), authority.clone()).unwrap();
+ let pending = store
+ .search_observed_sessions(&reader(true), &query("needle", 10), 500)
+ .unwrap();
+ assert!(pending.index_partial && pending.hits.is_empty());
+ assert_eq!(
+ store
+ .search_observed_text(&reader(true), &query("needle", 10), 500)
+ .unwrap()
+ .hits[0]
+ .original_text_offset,
+ 7
+ );
+ // A crash may leave unpublished lookup rows. They grant no hit and restart
+ // replaces them idempotently before publishing this complete source.
+ store.connection.lock().execute_batch("INSERT INTO observation_text_lookup_v1(rowid,grams) SELECT rowid,'g62' FROM observation_text_blocks_v2;").unwrap();
+ index(&store);
+ let page = store
+ .search_observed_sessions(&reader(true), &query("needle", 10), 500)
+ .unwrap();
+ assert!(!page.index_partial);
+ assert_eq!(page.hits[0].original_text_offset, 7);
+ drop(store);
+ let store = LocalObservationStore::open(root.path(), authority).unwrap();
+ index(&store);
+ for name in [
+ "content_instances_v2_search_blob",
+ "observation_text_index_ready",
+ ] {
+ assert_eq!(
+ store
+ .connection
+ .lock()
+ .query_row(
+ "SELECT COUNT(*) FROM sqlite_master WHERE type='index' AND name=?1",
+ [name],
+ |r| r.get::<_, u64>(0)
+ )
+ .unwrap(),
+ 1
+ );
+ }
+ assert_eq!(
+ store
+ .search_observed_sessions(&reader(true), &query("needle", 10), 500)
+ .unwrap(),
+ page
+ );
+ assert_eq!(
+ store
+ .connection
+ .lock()
+ .query_row("SELECT COUNT(*) FROM observation_text_lookup_v1", [], |r| r
+ .get::<_, u64>(0))
+ .unwrap(),
+ 1
+ );
+}
+
+#[test]
+fn live_search_deletion_removes_lookup_and_body_without_invalidating_other_sessions() {
+ let root = tempfile::tempdir().unwrap();
+ let store = LocalObservationStore::open(root.path(), DigestAuthority::new([2; 32])).unwrap();
+ add(&store, "new", "new", 300, "needle new");
+ add(&store, "middle", "middle", 200, "needle deleted");
+ add(&store, "old", "old", 100, "needle old");
+ index(&store);
+ let mut q = query("needle", 1);
+ q.cursor = store
+ .search_observed_sessions(&reader(true), &q, 500)
+ .unwrap()
+ .next_cursor;
+ let principal = ObservationPrincipalV1::local_user(WorkspaceId::default());
+ let spec = SessionDeletionSpecV1 {
+ workspace_id: WorkspaceId::default(),
+ session_id: SessionId::parse("middle").unwrap(),
+ data_class: DeletionDataClass::ContentOnly,
+ delete_rollups: false,
+ };
+ let preview = store.preview_session_deletion(&principal, &spec).unwrap();
+ store
+ .apply_session_deletion(
+ &principal,
+ &spec,
+ preview.store_revision,
+ &preview.change_digest,
+ 500,
+ )
+ .unwrap();
+ let next = store
+ .search_observed_sessions(&reader(true), &q, 500)
+ .unwrap();
+ assert_eq!(next.hits[0].session_id, "old");
+ assert!(next.next_cursor.is_none());
+ assert_eq!(store.connection.lock().query_row("SELECT count(*) FROM observation_text_lookup_v1 WHERE observation_text_lookup_v1 MATCH ?1",[crate::text_index::lookup::expression("deleted")],|r|r.get::<_,u64>(0)).unwrap(),0);
+ let body = store.observed_content_page(
+ &reader(true),
+ &ObservationContentQueryV2 {
+ request_id: LogicalRequestId::parse("middle").unwrap(),
+ content_id: "content-middle".into(),
+ anchor_offset: Some(0),
+ cursor: None,
+ },
+ 500,
+ );
+ assert!(!body.is_ok_and(|p| !p.chunks.is_empty()));
+}
+
+#[test]
+fn live_search_filters_current_run_authority_and_never_reports_hidden_gaps() {
+ let root = tempfile::tempdir().unwrap();
+ let store = LocalObservationStore::open(root.path(), DigestAuthority::new([2; 32])).unwrap();
+ add(&store, "new", "new", 300, "needle");
+ add(&store, "old", "old", 100, "needle");
+ index(&store);
+ add(&store, "hidden", "hidden", 250, "not indexed");
+ for request in ["new", "old"] {
+ store
+ .link_observed_request(&super::tests::link(request, "allowed"))
+ .unwrap();
+ }
+ let scope = ObservationReaderContext::run_scoped(
+ WorkspaceId::default(),
+ "worker".into(),
+ 1,
+ 10000,
+ ["allowed".into()].into_iter().collect(),
+ true,
+ true,
+ )
+ .unwrap();
+ let mut q = query("needle", 1);
+ let first = store.search_observed_sessions(&scope, &q, 500).unwrap();
+ assert_eq!(first.hits[0].session_id, "new");
+ assert!(!first.index_partial);
+ q.cursor = first.next_cursor;
+ assert_eq!(
+ store.link_observed_request(&super::tests::link("old", "other")),
+ Err(ObservationV2Error::RelationshipConflict)
+ );
+ let next = store.search_observed_sessions(&scope, &q, 500).unwrap();
+ assert!(next.hits.is_empty() && next.next_cursor.is_none() && !next.index_partial);
+ assert_eq!(
+ store.search_observed_sessions(&reader(false), &q, 500),
+ Err(ObservationV2Error::Unauthorized)
+ );
+ let changed = ObservationReaderContext::run_scoped(
+ WorkspaceId::default(),
+ "worker".into(),
+ 2,
+ 10000,
+ ["other".into()].into_iter().collect(),
+ true,
+ true,
+ )
+ .unwrap();
+ assert_eq!(
+ store.search_observed_sessions(&changed, &q, 500),
+ Err(ObservationV2Error::Stale)
+ );
+ let mut changed_query = q.clone();
+ changed_query.keyword = "different".into();
+ assert_eq!(
+ store.search_observed_sessions(&scope, &changed_query, 500),
+ Err(ObservationV2Error::Stale)
+ );
+ scope.cancel();
+ assert_eq!(
+ store.search_observed_sessions(&scope, &q, 500),
+ Err(ObservationV2Error::Unavailable)
+ );
+}
+
+#[test]
+fn live_search_respects_query_filters_and_does_not_call_unindexed_content_complete_empty() {
+ let root = tempfile::tempdir().unwrap();
+ let store = LocalObservationStore::open(root.path(), DigestAuthority::new([2; 32])).unwrap();
+ add(&store, "session", "request", 100, "needle");
+ let pending = store
+ .search_observed_sessions(&reader(true), &query("needle", 10), 500)
+ .unwrap();
+ assert!(pending.index_partial && pending.hits.is_empty() && pending.next_cursor.is_none());
+ index(&store);
+ // A building source with a leftover lookup marker remains incomplete;
+ // a workspace-local ready set must not hide an unpublished source.
+ store
+ .connection
+ .lock()
+ .execute("UPDATE observation_text_index_v2 SET state='building'", [])
+ .unwrap();
+ let building = store
+ .search_observed_sessions(&reader(true), &query("needle", 10), 500)
+ .unwrap();
+ assert!(building.hits.is_empty() && building.index_partial);
+ store
+ .connection
+ .lock()
+ .execute("UPDATE observation_text_index_v2 SET state='ready'", [])
+ .unwrap();
+ for keyword in ["missing", "needle"] {
+ let mut q = query(keyword, 10);
+ q.agent_id = Some("other".into());
+ let page = store
+ .search_observed_sessions(&reader(true), &q, 500)
+ .unwrap();
+ assert!(page.hits.is_empty() && !page.index_partial);
+ }
+ for kind in ["provider_state", "reasoning_delta", "reasoning_finished"] {
+ store
+ .connection
+ .lock()
+ .execute("UPDATE content_instances_v2 SET content_kind=?1", [kind])
+ .unwrap();
+ let page = store
+ .search_observed_sessions(&reader(true), &query("needle", 10), 500)
+ .unwrap();
+ assert!(page.hits.is_empty() && !page.index_partial);
+ }
+ store
+ .connection
+ .lock()
+ .execute("UPDATE content_instances_v2 SET content_kind='text'", [])
+ .unwrap();
+ let mut q = query("needle", 10);
+ q.only_model_switch = true;
+ assert!(
+ store
+ .search_observed_sessions(&reader(true), &q, 500)
+ .unwrap()
+ .hits
+ .is_empty()
+ );
+ store.connection.lock().execute("INSERT INTO observation_attempt_models_v2(workspace_id,request_id,ordinal,model_id) VALUES(?1,'request',0,'one'),(?1,'request',1,'two')",[WorkspaceId::DEFAULT]).unwrap();
+ assert_eq!(
+ store
+ .search_observed_sessions(&reader(true), &q, 500)
+ .unwrap()
+ .hits
+ .len(),
+ 1
+ );
+ q.native_model = Some("absent".into());
+ assert!(
+ store
+ .search_observed_sessions(&reader(true), &q, 500)
+ .unwrap()
+ .hits
+ .is_empty()
+ );
+}
diff --git a/crates/observation/src/store/schema.rs b/crates/observation/src/store/schema.rs
index 685e6dcd..df6a0b7d 100644
--- a/crates/observation/src/store/schema.rs
+++ b/crates/observation/src/store/schema.rs
@@ -611,6 +611,12 @@ const CONVERSATION_CONTENT_SCHEMA_V4: &str = r#"
CREATE INDEX IF NOT EXISTS content_instances_v2_live_blob
ON content_instances_v2(workspace_id, content_blob_digest) WHERE state='complete';
+ -- Cover live-search metadata without loading every content occurrence.
+ -- The implicit rowid preserves the first eligible occurrence per request.
+ CREATE INDEX IF NOT EXISTS content_instances_v2_search_blob
+ ON content_instances_v2(workspace_id, content_blob_digest,
+ content_kind, canonical_media_type, request_id) WHERE state='complete';
+
CREATE TABLE IF NOT EXISTS content_chunks_v2 (
workspace_id TEXT NOT NULL,
content_id TEXT NOT NULL,
diff --git a/crates/observation/src/store/tests/blob_gc.rs b/crates/observation/src/store/tests/blob_gc.rs
index 86d18171..dad839f6 100644
--- a/crates/observation/src/store/tests/blob_gc.rs
+++ b/crates/observation/src/store/tests/blob_gc.rs
@@ -49,7 +49,12 @@ fn blob_gc_live_lookup_stays_linear_and_existing_databases_gain_index() {
store
.connection
.lock()
- .execute_batch("DROP INDEX content_instances_v2_live_blob")
+ // Both current indexes can satisfy the workspace/digest lookup. Remove
+ // both to reproduce the original unindexed query, not a new fast path.
+ .execute_batch(
+ "DROP INDEX content_instances_v2_live_blob;
+ DROP INDEX content_instances_v2_search_blob;",
+ )
.unwrap();
let without_index = idle_query_steps(&store);
assert!(
diff --git a/crates/observation/src/text_index/lookup.rs b/crates/observation/src/text_index/lookup.rs
new file mode 100644
index 00000000..a9c7c07b
--- /dev/null
+++ b/crates/observation/src/text_index/lookup.rs
@@ -0,0 +1,139 @@
+//! Literal substring lookup, including one/two-character queries. A complete
+//! encoded gram is exact; longer needles require folded substring verification.
+use crate::LocalObservationStore;
+use hiroute_domain::ObservationQueryError as Error;
+use rusqlite::{Connection, OptionalExtension, params};
+use std::{collections::BTreeSet, time::Instant};
+
+pub(super) fn migrate(db: &Connection) -> rusqlite::Result<()> {
+ // The pinned bundled SQLite supports contentless deletion. No duplicate
+ // body/gram text is retained; block rowids are the lookup document identity.
+ db.execute_batch(
+ "CREATE VIRTUAL TABLE IF NOT EXISTS observation_text_lookup_v1 USING
+ fts5(grams,content='',contentless_delete=1,detail=none,tokenize='ascii');
+ CREATE TABLE IF NOT EXISTS observation_text_lookup_sources_v1(
+ workspace TEXT NOT NULL,digest TEXT NOT NULL,PRIMARY KEY(workspace,digest));
+ CREATE TRIGGER IF NOT EXISTS observation_text_lookup_delete_block
+ AFTER DELETE ON observation_text_blocks_v2 BEGIN
+ DELETE FROM observation_text_lookup_v1 WHERE rowid=OLD.rowid;
+ END;
+ CREATE TRIGGER IF NOT EXISTS observation_text_lookup_delete_source
+ AFTER DELETE ON observation_text_index_v2 BEGIN
+ DELETE FROM observation_text_lookup_sources_v1
+ WHERE workspace=OLD.workspace AND digest=OLD.digest;
+ END;",
+ )
+}
+
+fn grams(text: &str, sizes: std::ops::RangeInclusive<usize>) -> BTreeSet<&str> {
+ let mut boundaries: Vec<_> = text.char_indices().map(|(at, _)| at).collect();
+ boundaries.push(text.len());
+ let mut result = BTreeSet::new();
+ for size in sizes {
+ for window in boundaries.windows(size + 1) {
+ result.insert(&text[window[0]..window[size]]);
+ }
+ }
+ result
+}
+
+fn encode(grams: BTreeSet<&str>, separator: &str) -> String {
+ use std::fmt::Write;
+ let mut result = String::new();
+ for gram in grams {
+ if !result.is_empty() {
+ result.push_str(separator);
+ }
+ result.push('g');
+ for byte in gram.bytes() {
+ // ASCII hex cannot introduce FTS operators, token boundaries or a
+ // different Unicode tokenizer interpretation.
+ write!(result, "{byte:02x}").expect("String write");
+ }
+ }
+ result
+}
+
+pub(crate) fn expression(needle: &str) -> String {
+ let size = needle.chars().count().min(3);
+ encode(grams(needle, size..=size), " AND ")
+}
+
+pub(super) fn insert(db: &Connection, block: i64, folded: &str) -> rusqlite::Result<()> {
+ db.execute(
+ "INSERT OR REPLACE INTO observation_text_lookup_v1(rowid,grams) VALUES(?1,?2)",
+ params![block, encode(grams(folded, 1..=3), " ")],
+ )?;
+ Ok(())
+}
+
+#[derive(Default)]
+pub(super) struct Backfill {
+ after: i64,
+ source: Option<(String, String, u64)>,
+ complete: bool,
+}
+
+impl Backfill {
+ /// Consume at most half a maintenance cycle so existing caches cannot
+ /// starve indexing of newly captured bodies. Publication is per source.
+ pub(super) fn cycle(
+ &mut self,
+ store: &LocalObservationStore,
+ started: Instant,
+ ) -> Result<usize, Error> {
+ if self.complete {
+ return Ok(0);
+ }
+ let mut bytes = 0;
+ while bytes < 4 * 1024 * 1024 && started.elapsed().as_millis() < 250 {
+ let mut db = store.connection.lock();
+ if self.source.is_none() {
+ let through: Option<i64> = db.query_row(
+ "SELECT MAX(rowid) FROM (SELECT rowid FROM observation_text_index_v2 WHERE rowid>?1 ORDER BY rowid LIMIT 256)",
+ [self.after], |r| r.get(0),
+ ).map_err(|_| Error::Unavailable)?;
+ let Some(through) = through else {
+ // Current source publication already supplies its lookup.
+ // Once released caches are backfilled, do not rescan ready
+ // history on every idle maintenance tick.
+ self.complete = true;
+ break;
+ };
+ let source: Option<(i64, String, String)> = db.query_row(
+ "SELECT rowid,workspace,digest FROM observation_text_index_v2 i WHERE rowid>?1 AND rowid<=?2 AND state='ready'
+ AND NOT EXISTS(SELECT 1 FROM observation_text_lookup_sources_v1 k WHERE k.workspace=i.workspace AND k.digest=i.digest)
+ ORDER BY rowid LIMIT 1",
+ params![self.after, through], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
+ ).optional().map_err(|_| Error::Unavailable)?;
+ self.after = source.as_ref().map_or(through, |r| r.0);
+ self.source = source.map(|(_, workspace, digest)| (workspace, digest, 0));
+ if self.source.is_none() {
+ continue;
+ }
+ }
+ let (workspace, digest, ordinal) = self.source.as_mut().ok_or(Error::Unavailable)?;
+ let block: Option<(i64, u64, String)> = db.query_row(
+ "SELECT b.rowid,b.ordinal,b.folded FROM observation_text_blocks_v2 b
+ JOIN observation_text_index_v2 i ON i.workspace=b.workspace AND i.digest=b.digest AND i.state='ready'
+ WHERE b.workspace=?1 AND b.digest=?2 AND b.ordinal>=?3 ORDER BY b.ordinal LIMIT 1",
+ params![workspace.as_str(), digest.as_str(), *ordinal], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
+ ).optional().map_err(|_| Error::Unavailable)?;
+ if let Some((id, at, folded)) = block {
+ bytes += folded.len();
+ let tx = db.transaction().map_err(|_| Error::Unavailable)?;
+ insert(&tx, id, &folded).map_err(|_| Error::Unavailable)?;
+ tx.commit().map_err(|_| Error::Unavailable)?;
+ *ordinal = at + 1;
+ } else {
+ db.execute(
+ "INSERT OR IGNORE INTO observation_text_lookup_sources_v1(workspace,digest)
+ SELECT workspace,digest FROM observation_text_index_v2 WHERE workspace=?1 AND digest=?2 AND state='ready'",
+ params![workspace.as_str(), digest.as_str()],
+ ).map_err(|_| Error::Unavailable)?;
+ self.source = None;
+ }
+ }
+ Ok(bytes)
+ }
+}
diff --git a/crates/observation/src/text_index/mod.rs b/crates/observation/src/text_index/mod.rs
index 11056a07..e95e037c 100644
--- a/crates/observation/src/text_index/mod.rs
+++ b/crates/observation/src/text_index/mod.rs
@@ -1,11 +1,21 @@
//! Rebuildable content index. Incomplete or digest-invalid builds never publish
//! hits. The original content store and its visibility remain authoritative.
mod block;
+pub(crate) mod lookup;
mod worker;
pub(crate) use block::{fold, original_offset};
use rusqlite::Connection;
pub use worker::TextIndexBuilder;
+#[cfg(test)]
+pub(crate) fn insert_lookup_fixture(
+ db: &Connection,
+ block: i64,
+ folded: &str,
+) -> rusqlite::Result<()> {
+ lookup::insert(db, block, folded)
+}
+
pub(crate) fn migrate(
transaction: &Connection,
) -> Result<(), crate::writer::ObservationStoreError> {
@@ -19,6 +29,7 @@ pub(crate) fn migrate(
folded TEXT NOT NULL,offsets BLOB NOT NULL,original TEXT NOT NULL,
UNIQUE(workspace,digest,ordinal));
CREATE INDEX IF NOT EXISTS observation_text_blocks_source ON observation_text_blocks_v2(workspace,digest,ordinal);
+ CREATE INDEX IF NOT EXISTS observation_text_index_ready ON observation_text_index_v2(workspace,digest) WHERE state='ready';
CREATE INDEX IF NOT EXISTS observation_text_index_published ON observation_text_index_v2(published);"
).map_err(|_| crate::writer::ObservationStoreError::ActivityUnavailable)?;
let has_original: bool = {
@@ -37,5 +48,7 @@ pub(crate) fn migrate(
if !has_original {
transaction.execute_batch("ALTER TABLE observation_text_blocks_v2 ADD COLUMN original TEXT NOT NULL DEFAULT ''; DELETE FROM observation_text_blocks_v2; DELETE FROM observation_text_index_v2; UPDATE observation_meta SET value=CAST(value AS INTEGER)+1 WHERE key='query_visibility_generation';").map_err(|_|crate::writer::ObservationStoreError::ActivityUnavailable)?;
}
+ lookup::migrate(transaction)
+ .map_err(|_| crate::writer::ObservationStoreError::ActivityUnavailable)?;
Ok(())
}
diff --git a/crates/observation/src/text_index/worker.rs b/crates/observation/src/text_index/worker.rs
index 441879ea..cd976e6a 100644
--- a/crates/observation/src/text_index/worker.rs
+++ b/crates/observation/src/text_index/worker.rs
@@ -32,6 +32,7 @@ pub struct TextIndexBuilder {
_owner: crate::maintenance::LocalWorkerGuard,
building: Option<Building>,
scan_after: i64,
+ lookup_backfill: super::lookup::Backfill,
}
impl TextIndexBuilder {
pub fn new(store: &LocalObservationStore) -> Result<Self, Error> {
@@ -51,6 +52,7 @@ impl TextIndexBuilder {
Ok(Self {
building: None,
scan_after: 0,
+ lookup_backfill: Default::default(),
_owner: owner,
})
}
@@ -59,7 +61,7 @@ impl TextIndexBuilder {
/// block operations per cycle. Ordinary filesystem IO is not hard realtime.
pub fn cycle(&mut self, store: &LocalObservationStore) -> Result<usize, Error> {
let started = Instant::now();
- let mut bytes = 0;
+ let mut bytes = self.lookup_backfill.cycle(store, started)?;
let mut completed = 0;
while bytes < 8 * 1024 * 1024 && started.elapsed() < Duration::from_millis(500) {
if self.building.is_none() {
@@ -103,12 +105,18 @@ impl TextIndexBuilder {
let original_start = job.consumed.saturating_sub(job.overlap.len() as u64);
let text = format!("{}{new_text}", job.overlap);
let (folded, offsets) = super::fold(&text);
- let connection = store.connection.lock();
- connection.execute(
+ let mut connection = store.connection.lock();
+ let transaction = connection.transaction().map_err(|_| Error::Unavailable)?;
+ let inserted = transaction.execute(
"INSERT INTO observation_text_blocks_v2(workspace,digest,ordinal,original_start,primary_start,folded,offsets,original)
SELECT ?1,?2,?3,?4,?5,?6,?7,?8 WHERE EXISTS(SELECT 1 FROM observation_text_index_v2 WHERE workspace=?1 AND digest=?2 AND state='building')",
params![job.workspace,job.digest,job.ordinal,original_start,job.consumed,folded,offsets,text],
).map_err(|_| Error::Unavailable)?;
+ if inserted != 0 {
+ super::lookup::insert(&transaction, transaction.last_insert_rowid(), &folded)
+ .map_err(|_| Error::Unavailable)?;
+ }
+ transaction.commit().map_err(|_| Error::Unavailable)?;
job.consumed += valid as u64;
job.ordinal += 1;
let mut overlap_start = text.len().saturating_sub(OVERLAP);
@@ -214,6 +222,12 @@ fn finish(store: &LocalObservationStore, job: Building, readable: bool) -> Resul
],
)
.map_err(|_| Error::Unavailable)?;
+ if valid && live {
+ transaction.execute(
+ "INSERT OR IGNORE INTO observation_text_lookup_sources_v1(workspace,digest) VALUES(?1,?2)",
+ params![job.workspace, job.digest],
+ ).map_err(|_| Error::Unavailable)?;
+ }
transaction.commit().map_err(|_| Error::Unavailable)
}
diff --git a/crates/observation/tests/fixtures/released-v0.3.1-text-index.sql b/crates/observation/tests/fixtures/released-v0.3.1-text-index.sql
new file mode 100644
index 00000000..63d9bc4a
--- /dev/null
+++ b/crates/observation/tests/fixtures/released-v0.3.1-text-index.sql
@@ -0,0 +1,12 @@
+-- Frozen DDL from v0.3.1, 13856abd873da5b4373d39322493b4cc105c4867.
+-- crates/observation/src/text_index/mod.rs, first migration statement.
+CREATE TABLE IF NOT EXISTS observation_text_index_v2(
+ workspace TEXT NOT NULL,digest TEXT NOT NULL,state TEXT NOT NULL,published INTEGER NOT NULL DEFAULT 0,
+ PRIMARY KEY(workspace,digest));
+ CREATE TABLE IF NOT EXISTS observation_text_blocks_v2(
+ workspace TEXT NOT NULL,digest TEXT NOT NULL,ordinal INTEGER NOT NULL,
+ original_start INTEGER NOT NULL,primary_start INTEGER NOT NULL,
+ folded TEXT NOT NULL,offsets BLOB NOT NULL,original TEXT NOT NULL,
+ UNIQUE(workspace,digest,ordinal));
+ CREATE INDEX IF NOT EXISTS observation_text_blocks_source ON observation_text_blocks_v2(workspace,digest,ordinal);
+ CREATE INDEX IF NOT EXISTS observation_text_index_published ON observation_text_index_v2(published);
diff --git a/docs/code-map/architecture.md b/docs/code-map/architecture.md
index 4425f016..e4388a31 100644
--- a/docs/code-map/architecture.md
+++ b/docs/code-map/architecture.md
@@ -100,7 +100,7 @@ Scan → Prepare → Save → restart → native use, including stale-source rej
| Upstream fault attribution | [Wire projection](../../crates/gateway/src/observation/provider/wire_diagnostic.rs), [attempt association](../../crates/gateway/src/observation/request/diagnostic.rs), [decision calls](../../crates/gateway/src/core_runtime/classification/diagnostic.rs) | Actual bounded model/control fields, negotiated HTTP protocol, closed error codes, hashed request IDs and separate client headers/body commit facts must preserve the real outcome without prompts or provider error text |
| Source disabled before an upstream attempt | [Exact permission gate](../../crates/local-storage/src/secrets/runtime.rs), [product preparation reply](../../crates/gateway/src/runtime/driver/selection/preparation.rs), [Gateway error guide](../../crates/gateway/README.md#candidate-preparation-errors) | Preserve the closed lease denial in the request fact and local JSON response; a frozen route does not grant current source/Key permission, and rejected preparation remains zero attempts |
| Inspect CPA authentication recovery | [Private response metadata](../../crates/gateway/src/observation/provider/cpa_execution.rs), [pinned CPA adapter](../../vendor/cpa/README.md) | Logical attempts and actual HTTP sends differ; only verified CPA targets can supply recovery counters. Missing evidence remains unknown and does not create token usage. |
-| Search and gaps | [Query](../../crates/observation/src/query_v2/search.rs), [index worker](../../crates/observation/src/text_index/worker.rs), [status](../../crates/observation/src/query/status.rs) | Bounded scan windows, wraparound and explicit gaps distinguish incomplete search from an empty result |
+| Search and gaps | [Live session search](../../crates/observation/src/query_v2/session_search.rs), [query contract](../../crates/observation/src/query_v2/README.md), [keyword lookup](../../crates/observation/src/text_index/lookup.rs) | Fresh authorized pages count matching sessions; bounded background backfill and explicit gaps distinguish pending indexes from complete empty results. The released occurrence-search interface remains separate. |
| Business relations and visibility | [Relation owner](../../crates/observation/src/query_v2/relation.rs), [managed content](../../crates/observation/src/managed_text/README.md) | `query_v2` also writes relations; every content read checks current authorization and visibility |
| Native tool-history continuation | [Responses ingress](../../crates/gateway/src/adapters/responses_ingress.rs), [real continuation journey](../../tools/e2e-harness/tests/p0_gateway_protocol/continuation.rs) | Preserve model-generated empty tool names in native history without relaxing declarations/call IDs; unsafe conversion, ContextHold and reasoning cleanup stay disabled for that history |
| Decision and competence attribution | [Decision observation map](decision-foundation.md#observation-and-public-consumers) | Attribute scores to the stage that actually ran, with its frozen category/group/candidate and rubric |
diff --git a/docs/code-map/testing.md b/docs/code-map/testing.md
index 512505b4..7733febd 100644
--- a/docs/code-map/testing.md
+++ b/docs/code-map/testing.md
@@ -106,6 +106,11 @@ HTTP path or old restore fixture does not prove route revocation or native execu
reused evidence at its original SHA; assess the three slowest measured modules
once without automatically refactoring the frozen candidate.
+The managed backend DAG's preparation boundary lives in
+[`validation-schedule.py`](../../scripts/validation-schedule.py).
+Its [regressions](../../scripts/test-validation-schedule.py) check current Gateway
+runtime fixture consumers before an expensive workspace run.
+
Code-map and local ownership-guide edits require link/owner review and the selected
contract checks, not new model calls. Runtime-embedded Markdown and shipped Skills
can change behavior and need their actual consumers. Instructions and compilation
diff --git a/scripts/test-validation-schedule.py b/scripts/test-validation-schedule.py
index 2d2d6960..e4f94119 100644
--- a/scripts/test-validation-schedule.py
+++ b/scripts/test-validation-schedule.py
@@ -45,6 +45,14 @@ sys.exit(101 if bad else 0)
class Schedule(unittest.TestCase):
+ def test_current_gateway_runtime_consumers_require_product_preparation(self):
+ tests = Path(__file__).resolve().parent.parent / 'tools/e2e-harness/tests'
+ consumers = {source.stem for source in tests.glob('*.rs')
+ if 'mod runtime_support;' in source.read_text()}
+ self.assertTrue(consumers, 'Gateway runtime consumer inventory is empty')
+ self.assertEqual(consumers - scheduler.PRODUCT_TARGETS, set(),
+ 'Gateway runtime targets must use prepared product binaries')
+
def test_exec_audit_rejects_runtime_cargo_without_storing_arguments(self):
trace = ('123 execve("/tmp/bin/test", [...], 0x1) = 0\n'
'124 execve("/home/user/.cargo/bin/cargo", [...], 0x1) = 0\n'
diff --git a/scripts/validation-schedule.py b/scripts/validation-schedule.py
index 5b9ae445..eb90ba63 100644
--- a/scripts/validation-schedule.py
+++ b/scripts/validation-schedule.py
@@ -265,7 +265,7 @@ PRODUCT_TARGETS = frozenset({
"publication_process", "control_shell", "pre_gateway_compute_routing",
"p0_gateway_protocol", "p0_gateway_request_authority", "p0_gateway_runtime",
"p0_gateway_privacy", "p0_gateway_context_hold", "p0_gateway_commit_boundary",
- "p0_gateway_replay", "p0_gateway_observation",
+ "p0_gateway_replay", "p0_gateway_observation", "p0_gateway_native_sessions",
"smoke_cli",
})