Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions src/search-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/**
* @fileoverview Pure cross-session federated search core (COD-9).
*
* `searchSources()` is the testable heart of `GET /api/search`: it takes a
* normalized query plus already-collected, in-memory source data and returns
* grouped, ranked, and capped results. It performs NO I/O — the route wrapper
* (`src/web/routes/search-routes.ts`) is responsible for harvesting the source
* arrays from the live server stores (sessions, run-summary trackers, attachment
* histories) in a bounded way before calling this.
*
* v1 scope (do not expand here): three sources — sessions/cases, run-summary
* events, file paths. Terminal-buffer scanning and any persisted index are
* explicitly deferred.
*
* Ranking: results are grouped by source type in the fixed order
* sessions → events → files. Within each group, exact (case-insensitive)
* name/path matches come first, then recency (newest timestamp first) as the
* tiebreak. There is no relevance-scoring pass in v1.
*
* Safety: file results only ever expose a workspace-relative path — server-
* private absolute paths are never placed in a result. Per-group and total caps
* bound the output so a broad query cannot return an unbounded payload.
*
* Key exports:
* - searchSources() — the pure core.
* - SEARCH_TOTAL_CAP / SEARCH_PER_GROUP_CAP — the output bounds.
* - SearchSources and the *Input row types — the source-data contract.
*/

import type { SearchResult, SearchResultGroup, SearchResponseData, SearchSourceType } from './types/search.js';

/** Maximum results returned across all groups combined. */
export const SEARCH_TOTAL_CAP = 60;
/** Maximum results returned within any single source group. */
export const SEARCH_PER_GROUP_CAP = 25;
/** Maximum characters in a result snippet. */
export const SEARCH_SNIPPET_MAX = 200;

/** A live-session row harvested for the session/case source. */
export interface SessionSearchInput {
sessionId: string;
sessionName: string;
workingDir: string;
/** Recency timestamp (e.g. lastActivityAt or createdAt). */
timestamp: number;
}

/** A run-summary timeline event harvested for the event source. */
export interface EventSearchInput {
sessionId: string;
sessionName: string;
eventId: string;
title: string;
details: string;
timestamp: number;
}

/** A per-session attachment harvested for the file source. */
export interface FileSearchInput {
sessionId: string;
sessionName: string;
fileName: string;
/** Workspace-relative path, if known. Absolute/external paths are never passed in. */
relativePath: string | undefined;
timestamp: number;
/** Attachment history item id, used as the jump-to target. */
itemId: string;
}

/** The full set of in-memory source data the pure core searches over. */
export interface SearchSources {
sessions: SessionSearchInput[];
events: EventSearchInput[];
files: FileSearchInput[];
}

/** Fixed group/render order. */
const GROUP_ORDER: SearchSourceType[] = ['session', 'event', 'file'];

function truncate(text: string, max = SEARCH_SNIPPET_MAX): string {
const trimmed = text.trim().replace(/\s+/g, ' ');
return trimmed.length > max ? trimmed.slice(0, max - 1) + '…' : trimmed;
}

/**
* Sort a group's results: exact matches first, then newest timestamp first.
* Stable for equal keys.
*/
function sortGroup(rows: SearchResult[]): SearchResult[] {
return rows
.map((result, index) => ({ result, index }))
.sort((a, b) => {
if (a.result.exactMatch !== b.result.exactMatch) {
return a.result.exactMatch ? -1 : 1;
}
if (a.result.timestamp !== b.result.timestamp) {
return b.result.timestamp - a.result.timestamp;
}
return a.index - b.index;
})
.map((r) => r.result);
}

/**
* Search the provided in-memory sources for `query`.
*
* @param query Raw query string (already length-validated by the route). Blank
* queries return an empty result set.
* @param sources Harvested, bounded source arrays.
*/
export function searchSources(query: string, sources: SearchSources): SearchResponseData {
const needle = query.trim().toLowerCase();
if (needle.length === 0) {
return { query: query.trim(), groups: [], totalResults: 0, truncated: false };
}

const contains = (s: string | undefined): boolean => !!s && s.toLowerCase().includes(needle);
const isExact = (s: string | undefined): boolean => !!s && s.toLowerCase() === needle;

// -- Source: sessions/cases --
const sessionRows: SearchResult[] = [];
for (const s of sources.sessions) {
if (contains(s.sessionName) || contains(s.workingDir) || contains(s.sessionId)) {
sessionRows.push({
type: 'session',
sessionId: s.sessionId,
sessionName: s.sessionName,
timestamp: s.timestamp,
snippet: truncate(s.workingDir ? `${s.sessionName} — ${s.workingDir}` : s.sessionName),
exactMatch: isExact(s.sessionName),
jumpTo: { kind: 'session', sessionId: s.sessionId },
});
}
}

// -- Source: run-summary events --
const eventRows: SearchResult[] = [];
for (const e of sources.events) {
if (contains(e.title) || contains(e.details)) {
const snippetBase = e.details && contains(e.details) ? `${e.title}: ${e.details}` : e.title;
eventRows.push({
type: 'event',
sessionId: e.sessionId,
sessionName: e.sessionName,
timestamp: e.timestamp,
snippet: truncate(snippetBase),
exactMatch: isExact(e.title),
jumpTo: { kind: 'run-summary', sessionId: e.sessionId, targetId: e.eventId },
});
}
}

// -- Source: file paths --
const fileRows: SearchResult[] = [];
for (const f of sources.files) {
if (contains(f.fileName) || contains(f.relativePath)) {
fileRows.push({
type: 'file',
sessionId: f.sessionId,
sessionName: f.sessionName,
timestamp: f.timestamp,
snippet: truncate(f.relativePath ?? f.fileName),
// Exact match keys off the safe path (or filename) — never an absolute path.
exactMatch: isExact(f.relativePath) || isExact(f.fileName),
jumpTo: {
kind: 'file-preview',
sessionId: f.sessionId,
targetId: f.itemId,
// Only ever expose a relative path; absolute/external paths are not passed in.
relativePath: f.relativePath,
},
});
}
}

const byType: Record<SearchSourceType, SearchResult[]> = {
session: sortGroup(sessionRows),
event: sortGroup(eventRows),
file: sortGroup(fileRows),
};

const groups: SearchResultGroup[] = [];
let total = 0;
let truncated = false;

for (const type of GROUP_ORDER) {
const all = byType[type];
if (all.length === 0) continue;

// Per-group cap.
let capped = all.slice(0, SEARCH_PER_GROUP_CAP);
if (all.length > capped.length) truncated = true;

// Total cap (never exceed the global budget).
const remaining = SEARCH_TOTAL_CAP - total;
if (capped.length > remaining) {
capped = capped.slice(0, Math.max(0, remaining));
truncated = true;
}
if (capped.length === 0) continue;

groups.push({ type, results: capped });
total += capped.length;
}

return { query: query.trim(), groups, totalResults: total, truncated };
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ export * from './plan.js';
export * from './orchestrator.js';
export * from './update.js';
export * from './workflow-run.js';
export * from './search.js';
77 changes: 77 additions & 0 deletions src/types/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @fileoverview Cross-session federated search types (COD-9).
*
* Defines the typed shapes for `GET /api/search` — a bounded, in-memory
* federated search across three v1 sources: live sessions/cases, run-summary
* timeline events, and per-session attachment file paths. Terminal-buffer scans
* and any persisted index are explicitly out of scope for v1.
*
* Key exports:
* - SearchSourceType — the federated source kinds, also the group order key.
* - SearchResult — a single typed result card (source, session id/name,
* timestamp, snippet, jump-to action target).
* - SearchJumpTarget — where the frontend should navigate when a card is opened.
* - SearchResponseData — grouped result payload returned in the ApiResponse envelope.
*
* No I/O, no dependencies on other domain modules. The pure search core lives
* in `src/search-service.ts`; the route wrapper in `src/web/routes/search-routes.ts`.
*/

/** Federated source kinds. Group/render order is sessions → events → files. */
export type SearchSourceType = 'session' | 'event' | 'file';

/** Where the frontend should jump when a result card is activated. */
export interface SearchJumpTarget {
/** Kind of navigation target. */
kind: 'session' | 'run-summary' | 'file-preview';
/** Owning Codeman session id (always present — every result is session-scoped). */
sessionId: string;
/**
* Secondary identifier for the target:
* - kind 'run-summary': the run-summary event id
* - kind 'file-preview': the attachment history item id
* - kind 'session': undefined (the sessionId is sufficient)
*/
targetId?: string;
/**
* Workspace-relative path for file-preview targets. Never an absolute path —
* server-private external paths are intentionally omitted to avoid leakage.
*/
relativePath?: string;
}

/** A single typed search result card. */
export interface SearchResult {
/** Which federated source produced this result. */
type: SearchSourceType;
/** Owning Codeman session id. */
sessionId: string;
/** Display name of the owning session / case. */
sessionName: string;
/** Millisecond timestamp used for recency ranking and display. */
timestamp: number;
/** Short, already-truncated snippet describing the match. */
snippet: string;
/** True when the query matched the primary name/path exactly (case-insensitive). */
exactMatch: boolean;
/** Navigation target for the jump-to action. */
jumpTo: SearchJumpTarget;
}

/** A group of results for one source type, in render order. */
export interface SearchResultGroup {
type: SearchSourceType;
results: SearchResult[];
}

/** Payload returned as `data` inside the standard ApiResponse envelope. */
export interface SearchResponseData {
/** The normalized query that was executed. */
query: string;
/** Results grouped by source type, ordered sessions → events → files. */
groups: SearchResultGroup[];
/** Total number of results across all groups (after caps applied). */
totalResults: number;
/** True if any group or the total was capped (more matches existed). */
truncated: boolean;
}
41 changes: 40 additions & 1 deletion src/web/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,46 @@ <h1 class="welcome-title">Codeman</h1>
<div class="welcome-qr-url" id="welcomeQrUrl"></div>
</div>
<div class="history-sessions" id="historySessions" style="display:none">
<h3 class="history-title">Resume Conversation</h3>
<div class="search-panel" id="searchPanel">
<div class="search-input-row">
<input
type="search"
id="searchInput"
class="search-input"
placeholder="Search sessions, events, files…"
autocomplete="off"
spellcheck="false"
maxlength="200"
aria-label="Search across sessions"
/>
<button type="button" id="searchClearBtn" class="search-clear-btn" aria-label="Clear search" hidden>×</button>
</div>
<div class="search-filters" id="searchFilters">
<div class="search-filter-group" role="group" aria-label="Source type filter">
<button type="button" class="search-filter-chip active" data-type-filter="session">Sessions</button>
<button type="button" class="search-filter-chip active" data-type-filter="event">Events</button>
<button type="button" class="search-filter-chip active" data-type-filter="file">Files</button>
</div>
<div class="search-filter-group search-filter-secondary">
<select id="searchCaseFilter" class="search-select" aria-label="Filter by case">
<option value="">All cases</option>
</select>
<select id="searchStatusFilter" class="search-select" aria-label="Filter by session status">
<option value="">Any status</option>
<option value="active">Active</option>
<option value="history">History</option>
</select>
<select id="searchDateFilter" class="search-select" aria-label="Filter by date range">
<option value="">Any time</option>
<option value="1">Past 24h</option>
<option value="7">Past 7 days</option>
<option value="30">Past 30 days</option>
</select>
</div>
</div>
<div class="search-results" id="searchResults" hidden></div>
</div>
<h3 class="history-title" id="historyTitle">Resume Conversation</h3>
<div class="history-list" id="historyList"></div>
</div>
<p class="welcome-hint">Or press <kbd>Ctrl</kbd>+<kbd>Enter</kbd> to start</p>
Expand Down
Loading