neuralbrain 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +2 -2
- package/README.md +19 -4
- package/dist/ai/chat.d.ts +1 -2
- package/dist/ai/chat.d.ts.map +1 -1
- package/dist/ai/chat.js +1 -2
- package/dist/ai/chat.js.map +1 -1
- package/dist/ai/config.d.ts +1 -1
- package/dist/ai/config.js +2 -2
- package/dist/ai/config.js.map +1 -1
- package/dist/app.d.ts +1 -0
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +417 -50
- package/dist/app.js.map +1 -1
- package/dist/cli.d.ts +53 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +437 -37
- package/dist/cli.js.map +1 -1
- package/dist/consolidation/candidates.d.ts.map +1 -1
- package/dist/consolidation/candidates.js +19 -4
- package/dist/consolidation/candidates.js.map +1 -1
- package/dist/consolidation/fixtures.js +1 -1
- package/dist/consolidation/fixtures.js.map +1 -1
- package/dist/consolidation/gate.d.ts.map +1 -1
- package/dist/consolidation/gate.js +145 -109
- package/dist/consolidation/gate.js.map +1 -1
- package/dist/consolidation/runner.d.ts +6 -0
- package/dist/consolidation/runner.d.ts.map +1 -1
- package/dist/consolidation/runner.js +22 -2
- package/dist/consolidation/runner.js.map +1 -1
- package/dist/dashboard/clientScript.d.ts +1 -1
- package/dist/dashboard/clientScript.d.ts.map +1 -1
- package/dist/dashboard/clientScript.js +3774 -905
- package/dist/dashboard/clientScript.js.map +1 -1
- package/dist/dashboard/html.d.ts.map +1 -1
- package/dist/dashboard/html.js +605 -101
- package/dist/dashboard/html.js.map +1 -1
- package/dist/dashboard/styles.d.ts +1 -1
- package/dist/dashboard/styles.d.ts.map +1 -1
- package/dist/dashboard/styles.js +1591 -359
- package/dist/dashboard/styles.js.map +1 -1
- package/dist/learning/embedWorker.d.ts +13 -0
- package/dist/learning/embedWorker.d.ts.map +1 -1
- package/dist/learning/embedWorker.js +329 -59
- package/dist/learning/embedWorker.js.map +1 -1
- package/dist/learning/embedding.d.ts +1 -0
- package/dist/learning/embedding.d.ts.map +1 -1
- package/dist/learning/embedding.js +101 -26
- package/dist/learning/embedding.js.map +1 -1
- package/dist/learning/engine.js +1 -1
- package/dist/learning/engine.js.map +1 -1
- package/dist/learning/search.d.ts +17 -1
- package/dist/learning/search.d.ts.map +1 -1
- package/dist/learning/search.js +2094 -354
- package/dist/learning/search.js.map +1 -1
- package/dist/learning/storeMeta.d.ts +9 -0
- package/dist/learning/storeMeta.d.ts.map +1 -1
- package/dist/learning/storeMeta.js +30 -24
- package/dist/learning/storeMeta.js.map +1 -1
- package/dist/learning/types.d.ts +57 -8
- package/dist/learning/types.d.ts.map +1 -1
- package/dist/lib/condenser.d.ts +112 -0
- package/dist/lib/condenser.d.ts.map +1 -0
- package/dist/lib/condenser.js +1337 -0
- package/dist/lib/condenser.js.map +1 -0
- package/dist/lib/db.d.ts +10 -0
- package/dist/lib/db.d.ts.map +1 -1
- package/dist/lib/db.js +654 -167
- package/dist/lib/db.js.map +1 -1
- package/dist/lib/logger.d.ts.map +1 -1
- package/dist/lib/logger.js +3 -2
- package/dist/lib/logger.js.map +1 -1
- package/dist/lib/projectSlug.d.ts +12 -5
- package/dist/lib/projectSlug.d.ts.map +1 -1
- package/dist/lib/projectSlug.js +62 -23
- package/dist/lib/projectSlug.js.map +1 -1
- package/dist/lib/recurrence.d.ts +16 -0
- package/dist/lib/recurrence.d.ts.map +1 -0
- package/dist/lib/recurrence.js +293 -0
- package/dist/lib/recurrence.js.map +1 -0
- package/dist/lib/scope.d.ts +36 -1
- package/dist/lib/scope.d.ts.map +1 -1
- package/dist/lib/scope.js +183 -19
- package/dist/lib/scope.js.map +1 -1
- package/dist/lib/signalTags.d.ts +39 -0
- package/dist/lib/signalTags.d.ts.map +1 -0
- package/dist/lib/signalTags.js +203 -0
- package/dist/lib/signalTags.js.map +1 -0
- package/dist/lib/spool.d.ts +2 -0
- package/dist/lib/spool.d.ts.map +1 -1
- package/dist/lib/spool.js +176 -65
- package/dist/lib/spool.js.map +1 -1
- package/dist/lib/storeDiagnostics.d.ts +41 -0
- package/dist/lib/storeDiagnostics.d.ts.map +1 -0
- package/dist/lib/storeDiagnostics.js +148 -0
- package/dist/lib/storeDiagnostics.js.map +1 -0
- package/dist/lib/temporal.d.ts +44 -0
- package/dist/lib/temporal.d.ts.map +1 -0
- package/dist/lib/temporal.js +1063 -0
- package/dist/lib/temporal.js.map +1 -0
- package/dist/mcp/server.d.ts +93 -3
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +499 -68
- package/dist/mcp/server.js.map +1 -1
- package/dist/memory/actionStore.d.ts +94 -3
- package/dist/memory/actionStore.d.ts.map +1 -1
- package/dist/memory/actionStore.js +412 -124
- package/dist/memory/actionStore.js.map +1 -1
- package/dist/memory/deadLetterStore.d.ts.map +1 -1
- package/dist/memory/deadLetterStore.js +4 -2
- package/dist/memory/deadLetterStore.js.map +1 -1
- package/dist/memory/embedQueueStore.d.ts +19 -8
- package/dist/memory/embedQueueStore.d.ts.map +1 -1
- package/dist/memory/embedQueueStore.js +55 -25
- package/dist/memory/embedQueueStore.js.map +1 -1
- package/dist/memory/failurePatternStore.d.ts +17 -1
- package/dist/memory/failurePatternStore.d.ts.map +1 -1
- package/dist/memory/failurePatternStore.js +54 -31
- package/dist/memory/failurePatternStore.js.map +1 -1
- package/dist/memory/graphStore.d.ts +133 -3
- package/dist/memory/graphStore.d.ts.map +1 -1
- package/dist/memory/graphStore.js +980 -155
- package/dist/memory/graphStore.js.map +1 -1
- package/dist/memory/noteStore.d.ts +27 -2
- package/dist/memory/noteStore.d.ts.map +1 -1
- package/dist/memory/noteStore.js +89 -26
- package/dist/memory/noteStore.js.map +1 -1
- package/dist/memory/observationStore.d.ts +11 -1
- package/dist/memory/observationStore.d.ts.map +1 -1
- package/dist/memory/observationStore.js +90 -15
- package/dist/memory/observationStore.js.map +1 -1
- package/dist/memory/projectRename.d.ts.map +1 -1
- package/dist/memory/projectRename.js +5 -1
- package/dist/memory/projectRename.js.map +1 -1
- package/dist/memory/rollupSweeper.d.ts.map +1 -1
- package/dist/memory/rollupSweeper.js +43 -23
- package/dist/memory/rollupSweeper.js.map +1 -1
- package/dist/memory/sessionStore.d.ts +25 -7
- package/dist/memory/sessionStore.d.ts.map +1 -1
- package/dist/memory/sessionStore.js +145 -33
- package/dist/memory/sessionStore.js.map +1 -1
- package/dist/memory/trajectoryStore.d.ts +8 -0
- package/dist/memory/trajectoryStore.d.ts.map +1 -1
- package/dist/memory/trajectoryStore.js +196 -130
- package/dist/memory/trajectoryStore.js.map +1 -1
- package/dist/middleware/rateLimiter.d.ts.map +1 -1
- package/dist/middleware/rateLimiter.js +24 -1
- package/dist/middleware/rateLimiter.js.map +1 -1
- package/dist/routes/dashboard.d.ts.map +1 -1
- package/dist/routes/dashboard.js +181 -8
- package/dist/routes/dashboard.js.map +1 -1
- package/dist/routes/mcp.d.ts.map +1 -1
- package/dist/routes/mcp.js +173 -21
- package/dist/routes/mcp.js.map +1 -1
- package/dist/routes/memories.js +9 -1
- package/dist/routes/memories.js.map +1 -1
- package/dist/scripts/doctor.d.ts +26 -2
- package/dist/scripts/doctor.d.ts.map +1 -1
- package/dist/scripts/doctor.js +371 -259
- package/dist/scripts/doctor.js.map +1 -1
- package/dist/scripts/installAgent.d.ts.map +1 -1
- package/dist/scripts/installAgent.js +101 -19
- package/dist/scripts/installAgent.js.map +1 -1
- package/dist/scripts/migrateAgentMemory.js +2 -2
- package/dist/scripts/smokeTest.js +1 -1
- package/dist/scripts/smokeTest.js.map +1 -1
- package/docker-compose.yml +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +3 -2
- package/plugin/hooks/postToolUse.js +155 -50
- package/plugin/hooks/sessionStart.js +179 -27
- package/plugin/hooks/sessionStop.js +152 -37
- package/plugin/plugin.json +1 -1
- package/plugin/rules/AGENTS.md +10 -8
- package/plugin/skills/brain-memory/REFERENCE.md +15 -14
- package/plugin/skills/brain-memory/SKILL.md +2 -2
- package/plugin/skills/digest/SKILL.md +1 -1
- package/plugin/skills/handoff/SKILL.md +1 -1
- package/plugin/skills/save/SKILL.md +2 -2
- package/plugin/skills/search/SKILL.md +2 -2
- package/src/ai/chat.ts +1 -2
- package/src/ai/config.ts +2 -2
- package/src/app.ts +475 -54
- package/src/cli.ts +500 -38
- package/src/consolidation/candidates.ts +22 -4
- package/src/consolidation/fixtures.ts +1 -1
- package/src/consolidation/gate.ts +177 -140
- package/src/consolidation/runner.ts +26 -2
- package/src/dashboard/clientScript.ts +3774 -905
- package/src/dashboard/html.ts +605 -101
- package/src/dashboard/styles.ts +1591 -359
- package/src/learning/embedWorker.ts +350 -70
- package/src/learning/embedding.ts +141 -28
- package/src/learning/engine.ts +1 -1
- package/src/learning/search.ts +2533 -397
- package/src/learning/storeMeta.ts +36 -29
- package/src/learning/types.ts +66 -8
- package/src/lib/condenser.ts +1515 -0
- package/src/lib/db.ts +690 -171
- package/src/lib/logger.ts +4 -2
- package/src/lib/projectSlug.ts +77 -23
- package/src/lib/recurrence.ts +382 -0
- package/src/lib/scope.ts +220 -22
- package/src/lib/signalTags.ts +257 -0
- package/src/lib/spool.ts +181 -71
- package/src/lib/storeDiagnostics.ts +181 -0
- package/src/lib/temporal.ts +1250 -0
- package/src/mcp/server.ts +559 -88
- package/src/memory/actionStore.ts +642 -171
- package/src/memory/deadLetterStore.ts +4 -2
- package/src/memory/embedQueueStore.ts +63 -27
- package/src/memory/failurePatternStore.ts +84 -31
- package/src/memory/graphStore.ts +1396 -215
- package/src/memory/noteStore.ts +151 -30
- package/src/memory/observationStore.ts +117 -17
- package/src/memory/projectRename.ts +6 -2
- package/src/memory/rollupSweeper.ts +46 -23
- package/src/memory/sessionStore.ts +197 -38
- package/src/memory/trajectoryStore.ts +260 -140
- package/src/middleware/rateLimiter.ts +24 -1
- package/src/routes/dashboard.ts +194 -9
- package/src/routes/mcp.ts +185 -26
- package/src/routes/memories.ts +8 -1
- package/src/scripts/doctor.ts +461 -262
- package/src/scripts/installAgent.ts +106 -19
- package/src/scripts/migrateAgentMemory.ts +2 -2
- package/src/scripts/smokeTest.ts +1 -1
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
import { getDb } from '../lib/db';
|
|
25
25
|
import { logger } from '../lib/logger';
|
|
26
26
|
import { resolveScope } from '../lib/scope';
|
|
27
|
+
import { transaction } from '../lib/sqlite';
|
|
27
28
|
import { logTrajectory } from './trajectoryStore';
|
|
28
29
|
import { endSession } from './sessionStore';
|
|
29
30
|
|
|
@@ -157,12 +158,16 @@ export function sweepIdleSessions(options?: SweepOptions): SweepResult {
|
|
|
157
158
|
SELECT id, scope, project_id, agent, kind, tool_name, input, output,
|
|
158
159
|
duration_ms, failed, created_at
|
|
159
160
|
FROM (
|
|
160
|
-
SELECT id, scope, project_id, agent, kind, tool_name,
|
|
161
|
+
SELECT id, scope, project_id, agent, kind, tool_name,
|
|
162
|
+
SUBSTR(input, 1, 1000) AS input,
|
|
163
|
+
CASE WHEN failed = 1 THEN SUBSTR(output, 1, 1000) ELSE NULL END AS output,
|
|
161
164
|
duration_ms, failed, created_at
|
|
162
165
|
FROM observations
|
|
163
166
|
WHERE session_id = ?
|
|
164
167
|
UNION ALL
|
|
165
|
-
SELECT original_observation_id AS id, scope, project_id, agent, kind, tool_name,
|
|
168
|
+
SELECT original_observation_id AS id, scope, project_id, agent, kind, tool_name,
|
|
169
|
+
SUBSTR(input, 1, 1000) AS input,
|
|
170
|
+
CASE WHEN failed = 1 THEN SUBSTR(output, 1, 1000) ELSE NULL END AS output,
|
|
166
171
|
duration_ms, failed, created_at
|
|
167
172
|
FROM observations_archive
|
|
168
173
|
WHERE session_id = ?
|
|
@@ -200,28 +205,41 @@ export function sweepIdleSessions(options?: SweepOptions): SweepResult {
|
|
|
200
205
|
const durationMs = rows.reduce((sum, r) => sum + (r.duration_ms || 0), 0);
|
|
201
206
|
const toolCalls = rows.filter((r) => r.kind === 'tool_call').length;
|
|
202
207
|
|
|
203
|
-
let trajectoryId: string;
|
|
208
|
+
let trajectoryId: string | null = null;
|
|
204
209
|
try {
|
|
205
|
-
trajectoryId =
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
210
|
+
trajectoryId = transaction(db, () => {
|
|
211
|
+
const alreadyExists = db
|
|
212
|
+
.prepare(
|
|
213
|
+
`SELECT 1 FROM trajectories WHERE session_id = ?
|
|
214
|
+
UNION ALL
|
|
215
|
+
SELECT 1 FROM trajectory_archive WHERE session_id = ?
|
|
216
|
+
LIMIT 1`
|
|
217
|
+
)
|
|
218
|
+
.get(candidate.session_id, candidate.session_id);
|
|
219
|
+
if (alreadyExists) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
return logTrajectory({
|
|
223
|
+
scope: scoped.scope,
|
|
224
|
+
projectId: scoped.projectId,
|
|
225
|
+
sessionId: candidate.session_id,
|
|
226
|
+
agent: modeOf(rows.map((r) => r.agent)) ?? null,
|
|
227
|
+
// taskType is not derivable from observations, and guessing one would
|
|
228
|
+
// pollute the failure-pattern grouping key.
|
|
229
|
+
taskType: 'unknown',
|
|
230
|
+
rollupSource: 'swept',
|
|
231
|
+
observationIds: rows.map((r) => r.id),
|
|
232
|
+
preFetchedObservations: rows,
|
|
233
|
+
toolCalls,
|
|
234
|
+
durationMs,
|
|
235
|
+
// success is deliberately omitted: trajectoryStore derives it from the
|
|
236
|
+
// cited rows under the 'swept' contract, which yields NULL (unknown)
|
|
237
|
+
// unless a source row recorded a failure or a completion.
|
|
238
|
+
// The attempt happened when its observations happened, not when the
|
|
239
|
+
// sweeper noticed, so retention sees it in the right place in time.
|
|
240
|
+
createdAt: rows[rows.length - 1].created_at,
|
|
241
|
+
});
|
|
242
|
+
})();
|
|
225
243
|
} catch (err) {
|
|
226
244
|
logger.error(
|
|
227
245
|
{
|
|
@@ -234,6 +252,11 @@ export function sweepIdleSessions(options?: SweepOptions): SweepResult {
|
|
|
234
252
|
continue;
|
|
235
253
|
}
|
|
236
254
|
|
|
255
|
+
if (!trajectoryId) {
|
|
256
|
+
skipped++;
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
|
|
237
260
|
const stored = db
|
|
238
261
|
.prepare('SELECT success FROM trajectories WHERE id = ?')
|
|
239
262
|
.get(trajectoryId) as { success: 0 | 1 | null } | undefined;
|
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
2
|
import { getDb } from '../lib/db';
|
|
3
|
-
import {
|
|
3
|
+
import { redactSecrets } from '../lib/redactSecrets';
|
|
4
|
+
import {
|
|
5
|
+
buildHierarchicalScopeSql,
|
|
6
|
+
classifyMatchTier,
|
|
7
|
+
getProjectAncestors,
|
|
8
|
+
MatchTier,
|
|
9
|
+
normalizeProjectId,
|
|
10
|
+
resolveScope,
|
|
11
|
+
ScopeValue,
|
|
12
|
+
} from '../lib/scope';
|
|
4
13
|
import type { ObservationRecord } from './observationStore';
|
|
5
|
-
import type
|
|
6
|
-
import type
|
|
14
|
+
import { NOTE_COLUMNS, type NoteRecord } from './noteStore';
|
|
15
|
+
import { ACTION_COLUMNS, type ActionRecord } from './actionStore';
|
|
16
|
+
|
|
17
|
+
const DEFAULT_DETAIL_LIMIT = 200;
|
|
18
|
+
const MAX_DETAIL_LIMIT = 500;
|
|
19
|
+
const MAX_PREVIEW_CHARS = 2000;
|
|
20
|
+
const TRUNCATED_SUFFIX = '... [truncated]';
|
|
7
21
|
|
|
8
22
|
export type SessionStatus = 'active' | 'paused' | 'completed' | 'abandoned';
|
|
9
23
|
|
|
@@ -18,6 +32,8 @@ export interface SessionRecord {
|
|
|
18
32
|
id: string;
|
|
19
33
|
scope: ScopeValue;
|
|
20
34
|
project_id: string | null;
|
|
35
|
+
user_id?: string | null;
|
|
36
|
+
agent_id?: string | null;
|
|
21
37
|
agent_name: string | null;
|
|
22
38
|
cwd: string | null;
|
|
23
39
|
status: SessionStatus;
|
|
@@ -26,6 +42,7 @@ export interface SessionRecord {
|
|
|
26
42
|
summary: string | null;
|
|
27
43
|
files_touched_json: string | null;
|
|
28
44
|
handoff_summary_id: string | null;
|
|
45
|
+
match_tier?: MatchTier;
|
|
29
46
|
}
|
|
30
47
|
|
|
31
48
|
export interface StartSessionInput {
|
|
@@ -33,6 +50,10 @@ export interface StartSessionInput {
|
|
|
33
50
|
scope?: ScopeValue;
|
|
34
51
|
project_id?: string | null;
|
|
35
52
|
projectId?: string | null;
|
|
53
|
+
user_id?: string | null;
|
|
54
|
+
userId?: string | null;
|
|
55
|
+
agent_id?: string | null;
|
|
56
|
+
agentId?: string | null;
|
|
36
57
|
agent_name?: string | null;
|
|
37
58
|
agentName?: string | null;
|
|
38
59
|
cwd?: string | null;
|
|
@@ -54,6 +75,11 @@ export interface SessionDetails {
|
|
|
54
75
|
actions: ActionRecord[];
|
|
55
76
|
}
|
|
56
77
|
|
|
78
|
+
export interface SessionQueryOptions {
|
|
79
|
+
limit?: number;
|
|
80
|
+
offset?: number;
|
|
81
|
+
}
|
|
82
|
+
|
|
57
83
|
export interface SessionReplayItem {
|
|
58
84
|
kind: 'observation' | 'note' | 'action';
|
|
59
85
|
timestamp: string;
|
|
@@ -95,6 +121,10 @@ export function startSession(input: StartSessionInput = {}): SessionRecord {
|
|
|
95
121
|
const db = getDb();
|
|
96
122
|
const customId = optionalString(input.id, 'id');
|
|
97
123
|
const id = customId || `sess_${randomUUID().replace(/-/g, '').slice(0, 16)}`;
|
|
124
|
+
const rawUser = input.userId !== undefined ? input.userId : input.user_id;
|
|
125
|
+
const userId = optionalString(rawUser, 'user_id');
|
|
126
|
+
const rawAgentId = input.agentId !== undefined ? input.agentId : input.agent_id;
|
|
127
|
+
const agentId = optionalString(rawAgentId, 'agent_id');
|
|
98
128
|
const rawAgent = input.agentName !== undefined ? input.agentName : input.agent_name;
|
|
99
129
|
const agentName = optionalString(rawAgent, 'agent_name');
|
|
100
130
|
const cwd = optionalString(input.cwd, 'cwd');
|
|
@@ -104,26 +134,45 @@ export function startSession(input: StartSessionInput = {}): SessionRecord {
|
|
|
104
134
|
| undefined;
|
|
105
135
|
|
|
106
136
|
if (existing) {
|
|
137
|
+
let nextScope = existing.scope;
|
|
138
|
+
let nextProjectId = existing.project_id;
|
|
139
|
+
|
|
140
|
+
if (existing.scope === 'unknown' && scoped.scope !== 'unknown') {
|
|
141
|
+
nextScope = scoped.scope;
|
|
142
|
+
nextProjectId = scoped.projectId;
|
|
143
|
+
} else if (
|
|
144
|
+
existing.scope !== 'unknown' &&
|
|
145
|
+
(scoped.scope !== existing.scope || scoped.projectId !== existing.project_id)
|
|
146
|
+
) {
|
|
147
|
+
throw makeSchemaError(
|
|
148
|
+
`Session '${id}' already exists with scope '${existing.scope}'${existing.project_id ? ` (project '${existing.project_id}')` : ''}; cannot reactivate with scope '${scoped.scope}'${scoped.projectId ? ` (project '${scoped.projectId}')` : ''}`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
107
152
|
db.prepare(
|
|
108
153
|
`
|
|
109
154
|
UPDATE sessions
|
|
110
155
|
SET status = 'active',
|
|
111
156
|
ended_at = NULL,
|
|
157
|
+
scope = ?,
|
|
158
|
+
project_id = ?,
|
|
159
|
+
user_id = COALESCE(?, user_id),
|
|
160
|
+
agent_id = COALESCE(?, agent_id),
|
|
112
161
|
agent_name = COALESCE(?, agent_name),
|
|
113
162
|
cwd = COALESCE(?, cwd)
|
|
114
163
|
WHERE id = ?
|
|
115
164
|
`
|
|
116
|
-
).run(agentName, cwd, id);
|
|
165
|
+
).run(nextScope, nextProjectId, userId, agentId, agentName, cwd, id);
|
|
117
166
|
|
|
118
167
|
return getSession(id)!;
|
|
119
168
|
}
|
|
120
169
|
|
|
121
170
|
db.prepare(
|
|
122
171
|
`
|
|
123
|
-
INSERT INTO sessions (id, scope, project_id, agent_name, cwd, status)
|
|
124
|
-
VALUES (?, ?, ?, ?, ?, 'active')
|
|
172
|
+
INSERT INTO sessions (id, scope, project_id, user_id, agent_id, agent_name, cwd, status)
|
|
173
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, 'active')
|
|
125
174
|
`
|
|
126
|
-
).run(id, scoped.scope, scoped.projectId, agentName, cwd);
|
|
175
|
+
).run(id, scoped.scope, scoped.projectId, userId, agentId, agentName, cwd);
|
|
127
176
|
|
|
128
177
|
return getSession(id)!;
|
|
129
178
|
}
|
|
@@ -210,7 +259,7 @@ export function endSession(
|
|
|
210
259
|
: existing.files_touched_json;
|
|
211
260
|
|
|
212
261
|
const providedSummary = optionalString(input.summary, 'summary');
|
|
213
|
-
const summary = providedSummary ?? buildArithmeticSessionSummary(db, id);
|
|
262
|
+
const summary = redactSecrets(providedSummary ?? buildArithmeticSessionSummary(db, id));
|
|
214
263
|
|
|
215
264
|
const rawHandoff =
|
|
216
265
|
input.handoffSummaryId !== undefined
|
|
@@ -243,6 +292,10 @@ export function getSession(id: string): SessionRecord | undefined {
|
|
|
243
292
|
|
|
244
293
|
export function listSessions(filters?: {
|
|
245
294
|
projectId?: string;
|
|
295
|
+
includeAncestors?: boolean;
|
|
296
|
+
includeChildren?: boolean;
|
|
297
|
+
includeGlobal?: boolean;
|
|
298
|
+
userId?: string;
|
|
246
299
|
status?: string;
|
|
247
300
|
agentName?: string;
|
|
248
301
|
limit?: number;
|
|
@@ -251,9 +304,46 @@ export function listSessions(filters?: {
|
|
|
251
304
|
const conditions: string[] = [];
|
|
252
305
|
const params: unknown[] = [];
|
|
253
306
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
307
|
+
const cleanProjectId =
|
|
308
|
+
typeof filters?.projectId === 'string' && filters.projectId.trim()
|
|
309
|
+
? filters.projectId.trim()
|
|
310
|
+
: undefined;
|
|
311
|
+
const cleanUserId =
|
|
312
|
+
typeof filters?.userId === 'string' && filters.userId.trim() ? filters.userId.trim() : undefined;
|
|
313
|
+
const hasIncludeGlobal = filters?.includeGlobal !== undefined;
|
|
314
|
+
|
|
315
|
+
let queryProjectId: string | null = null;
|
|
316
|
+
const shouldAttachTier = Boolean(cleanProjectId || hasIncludeGlobal || cleanUserId);
|
|
317
|
+
|
|
318
|
+
if (cleanProjectId) {
|
|
319
|
+
const scopeSql = buildHierarchicalScopeSql({
|
|
320
|
+
projectId: cleanProjectId,
|
|
321
|
+
includeAncestors: filters?.includeAncestors ?? true,
|
|
322
|
+
includeChildren: filters?.includeChildren ?? false,
|
|
323
|
+
includeGlobal: filters?.includeGlobal === true,
|
|
324
|
+
userId: cleanUserId,
|
|
325
|
+
});
|
|
326
|
+
conditions.push(scopeSql.clause);
|
|
327
|
+
params.push(...scopeSql.params);
|
|
328
|
+
queryProjectId = scopeSql.normalizedProjectId;
|
|
329
|
+
} else if (hasIncludeGlobal) {
|
|
330
|
+
if (filters!.includeGlobal === true) {
|
|
331
|
+
const scopeSql = buildHierarchicalScopeSql({
|
|
332
|
+
includeGlobal: true,
|
|
333
|
+
userId: cleanUserId,
|
|
334
|
+
});
|
|
335
|
+
conditions.push(scopeSql.clause);
|
|
336
|
+
params.push(...scopeSql.params);
|
|
337
|
+
} else {
|
|
338
|
+
conditions.push("scope != 'global'");
|
|
339
|
+
if (cleanUserId) {
|
|
340
|
+
conditions.push('user_id = ?');
|
|
341
|
+
params.push(cleanUserId);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
} else if (cleanUserId) {
|
|
345
|
+
conditions.push('user_id = ?');
|
|
346
|
+
params.push(cleanUserId);
|
|
257
347
|
}
|
|
258
348
|
|
|
259
349
|
if (typeof filters?.status === 'string' && filters.status.trim()) {
|
|
@@ -270,7 +360,7 @@ export function listSessions(filters?: {
|
|
|
270
360
|
const limit = filters?.limit ?? 50;
|
|
271
361
|
params.push(limit);
|
|
272
362
|
|
|
273
|
-
|
|
363
|
+
const rows = db
|
|
274
364
|
.prepare(
|
|
275
365
|
`
|
|
276
366
|
SELECT * FROM sessions
|
|
@@ -280,24 +370,64 @@ export function listSessions(filters?: {
|
|
|
280
370
|
`
|
|
281
371
|
)
|
|
282
372
|
.all(...params) as SessionRecord[];
|
|
373
|
+
|
|
374
|
+
if (!shouldAttachTier) {
|
|
375
|
+
return rows;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return rows.map((row) => ({
|
|
379
|
+
...row,
|
|
380
|
+
match_tier: classifyMatchTier(row.scope, row.project_id, queryProjectId).match_tier,
|
|
381
|
+
}));
|
|
283
382
|
}
|
|
284
383
|
|
|
285
|
-
|
|
384
|
+
function clampDetailLimit(raw?: number): number {
|
|
385
|
+
if (typeof raw !== 'number' || !Number.isFinite(raw) || raw <= 0) {
|
|
386
|
+
return DEFAULT_DETAIL_LIMIT;
|
|
387
|
+
}
|
|
388
|
+
return Math.min(Math.floor(raw), MAX_DETAIL_LIMIT);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function clampOffset(raw?: number): number {
|
|
392
|
+
if (typeof raw !== 'number' || !Number.isFinite(raw) || raw < 0) {
|
|
393
|
+
return 0;
|
|
394
|
+
}
|
|
395
|
+
return Math.floor(raw);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function truncatePreview(value: string | null): string | null {
|
|
399
|
+
if (value === null || value.length <= MAX_PREVIEW_CHARS) return value;
|
|
400
|
+
return value.slice(0, MAX_PREVIEW_CHARS) + TRUNCATED_SUFFIX;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
export function getSessionDetails(
|
|
404
|
+
id: string,
|
|
405
|
+
options: SessionQueryOptions = {}
|
|
406
|
+
): SessionDetails | undefined {
|
|
286
407
|
const session = getSession(id);
|
|
287
408
|
if (!session) return undefined;
|
|
288
409
|
|
|
410
|
+
const limit = clampDetailLimit(options.limit);
|
|
411
|
+
const offset = clampOffset(options.offset);
|
|
412
|
+
|
|
289
413
|
const db = getDb();
|
|
290
|
-
const
|
|
291
|
-
.prepare('SELECT * FROM observations WHERE session_id = ? ORDER BY created_at ASC')
|
|
292
|
-
.all(id) as ObservationRecord[];
|
|
414
|
+
const rawObservations = db
|
|
415
|
+
.prepare('SELECT * FROM observations WHERE session_id = ? ORDER BY created_at ASC LIMIT ? OFFSET ?')
|
|
416
|
+
.all(id, limit, offset) as ObservationRecord[];
|
|
417
|
+
|
|
418
|
+
const observations = rawObservations.map((obs) => ({
|
|
419
|
+
...obs,
|
|
420
|
+
input: truncatePreview(obs.input),
|
|
421
|
+
output: truncatePreview(obs.output),
|
|
422
|
+
}));
|
|
293
423
|
|
|
294
424
|
const notes = db
|
|
295
|
-
.prepare(
|
|
296
|
-
.all(id) as NoteRecord[];
|
|
425
|
+
.prepare(`SELECT ${NOTE_COLUMNS} FROM notes WHERE session_id = ? ORDER BY created_at ASC LIMIT ? OFFSET ?`)
|
|
426
|
+
.all(id, limit, offset) as NoteRecord[];
|
|
297
427
|
|
|
298
428
|
const actions = db
|
|
299
|
-
.prepare(
|
|
300
|
-
.all(id) as ActionRecord[];
|
|
429
|
+
.prepare(`SELECT ${ACTION_COLUMNS} FROM actions WHERE session_id = ? ORDER BY created_at ASC LIMIT ? OFFSET ?`)
|
|
430
|
+
.all(id, limit, offset) as ActionRecord[];
|
|
301
431
|
|
|
302
432
|
return { session, observations, notes, actions };
|
|
303
433
|
}
|
|
@@ -317,8 +447,11 @@ function toIsoTimestamp(ts: number | string): string {
|
|
|
317
447
|
* Build a chronological replay timeline of observations, notes, and actions
|
|
318
448
|
* associated with a session.
|
|
319
449
|
*/
|
|
320
|
-
export function getSessionReplay(
|
|
321
|
-
|
|
450
|
+
export function getSessionReplay(
|
|
451
|
+
id: string,
|
|
452
|
+
options: SessionQueryOptions = {}
|
|
453
|
+
): SessionReplay | undefined {
|
|
454
|
+
const details = getSessionDetails(id, options);
|
|
322
455
|
if (!details) return undefined;
|
|
323
456
|
|
|
324
457
|
const timeline: SessionReplayItem[] = [
|
|
@@ -372,26 +505,52 @@ export function getSessionReplay(id: string): SessionReplay | undefined {
|
|
|
372
505
|
|
|
373
506
|
/**
|
|
374
507
|
* Return the most recent ended session with a non-empty summary for a project
|
|
375
|
-
* (
|
|
508
|
+
* (falling back through ancestor projects when includeAncestors !== false, or
|
|
509
|
+
* global if no projectId is given), or null if none exists.
|
|
376
510
|
*/
|
|
377
|
-
export function getLatestSessionSummary(
|
|
511
|
+
export function getLatestSessionSummary(
|
|
512
|
+
projectId?: string,
|
|
513
|
+
options?: { includeAncestors?: boolean }
|
|
514
|
+
): SessionRecord | null {
|
|
378
515
|
const db = getDb();
|
|
379
516
|
const cleanProject = typeof projectId === 'string' ? projectId.trim() || null : null;
|
|
380
517
|
|
|
381
518
|
if (cleanProject) {
|
|
382
|
-
const
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
519
|
+
const norm = normalizeProjectId(cleanProject);
|
|
520
|
+
const targetProject = norm.ok ? norm.projectId : cleanProject;
|
|
521
|
+
const stmt = db.prepare(
|
|
522
|
+
`
|
|
523
|
+
SELECT * FROM sessions
|
|
524
|
+
WHERE project_id = ? AND scope = 'project'
|
|
525
|
+
AND ended_at IS NOT NULL
|
|
526
|
+
AND summary IS NOT NULL AND summary != ''
|
|
527
|
+
ORDER BY ended_at DESC, rowid DESC
|
|
528
|
+
LIMIT 1
|
|
529
|
+
`
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
const exactRow = stmt.get(targetProject) as SessionRecord | undefined;
|
|
533
|
+
if (exactRow) {
|
|
534
|
+
return {
|
|
535
|
+
...exactRow,
|
|
536
|
+
match_tier: 'exact',
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (options?.includeAncestors !== false) {
|
|
541
|
+
const ancestors = getProjectAncestors(targetProject).slice(0, -1).reverse();
|
|
542
|
+
for (const ancestor of ancestors) {
|
|
543
|
+
const ancestorRow = stmt.get(ancestor) as SessionRecord | undefined;
|
|
544
|
+
if (ancestorRow) {
|
|
545
|
+
return {
|
|
546
|
+
...ancestorRow,
|
|
547
|
+
match_tier: classifyMatchTier(ancestorRow.scope, ancestorRow.project_id, targetProject).match_tier,
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
return null;
|
|
395
554
|
}
|
|
396
555
|
|
|
397
556
|
const row = db
|
|
@@ -406,5 +565,5 @@ export function getLatestSessionSummary(projectId?: string): SessionRecord | nul
|
|
|
406
565
|
`
|
|
407
566
|
)
|
|
408
567
|
.get() as SessionRecord | undefined;
|
|
409
|
-
return row
|
|
568
|
+
return row ? { ...row, match_tier: 'global' } : null;
|
|
410
569
|
}
|