claude-db 0.12.1 → 0.12.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/dist/capture/active.d.ts +12 -0
- package/dist/capture/active.js +74 -0
- package/dist/capture/flush.d.ts +1 -1
- package/dist/capture/flush.js +22 -3
- package/dist/capture/git.d.ts +1 -0
- package/dist/capture/git.js +3 -2
- package/dist/capture/index.d.ts +1 -0
- package/dist/capture/index.js +1 -0
- package/dist/capture/transcript.d.ts +1 -0
- package/dist/capture/transcript.js +3 -2
- package/dist/capture/turn-extractor.d.ts +3 -0
- package/dist/capture/turn-extractor.js +12 -2
- package/dist/capture/vectors.d.ts +7 -0
- package/dist/capture/vectors.js +38 -0
- package/dist/catchup/gather.d.ts +9 -4
- package/dist/catchup/gather.js +22 -15
- package/dist/catchup/render.js +17 -8
- package/dist/cli/commands/adoption.js +41 -29
- package/dist/cli/commands/catchup.js +2 -1
- package/dist/cli/commands/languages.js +10 -6
- package/dist/cli/commands/transfer.js +24 -5
- package/dist/cli/install.js +1 -0
- package/dist/facts/claude-memory.js +1 -1
- package/dist/facts/handoff.d.ts +2 -0
- package/dist/facts/handoff.js +7 -0
- package/dist/facts/health.js +2 -1
- package/dist/facts/last-chat.d.ts +13 -0
- package/dist/facts/last-chat.js +45 -0
- package/dist/facts/start.d.ts +2 -1
- package/dist/facts/start.js +44 -5
- package/dist/graph/query/lookup.d.ts +1 -0
- package/dist/graph/query/lookup.js +2 -1
- package/dist/hooks/background.js +5 -1
- package/dist/hooks/detached.d.ts +2 -0
- package/dist/hooks/detached.js +38 -2
- package/dist/hooks/payload.d.ts +5 -0
- package/dist/hooks/prefer-usages.js +1 -1
- package/dist/hooks/relevance.js +1 -5
- package/dist/hooks/session-end.js +6 -0
- package/dist/hooks/session-start.js +9 -6
- package/dist/hooks/turn-end.d.ts +2 -0
- package/dist/hooks/turn-end.js +26 -0
- package/dist/hooks/turn-job.d.ts +10 -0
- package/dist/hooks/turn-job.js +30 -0
- package/dist/hooks/turn-save.d.ts +2 -0
- package/dist/hooks/turn-save.js +25 -0
- package/dist/hooks/user-prompt.js +2 -0
- package/dist/pick/pending.js +7 -37
- package/dist/store/adapter.d.ts +1 -0
- package/dist/store/mongo/filters.d.ts +1 -0
- package/dist/store/mongo/filters.js +9 -3
- package/dist/store/mongo/observations.js +2 -3
- package/dist/store/postgres/filters.d.ts +1 -0
- package/dist/store/postgres/filters.js +9 -4
- package/dist/store/postgres/index.d.ts +1 -0
- package/dist/store/postgres/index.js +3 -0
- package/dist/store/postgres/observations.js +4 -1
- package/dist/store/sqlite/filters.d.ts +1 -0
- package/dist/store/sqlite/filters.js +9 -4
- package/dist/store/sqlite/observations.js +4 -1
- package/dist/store/sqlite/search.js +4 -5
- package/dist/types.d.ts +1 -0
- package/dist/util/day.d.ts +1 -0
- package/dist/util/day.js +4 -0
- package/dist/util/json-file.d.ts +4 -0
- package/dist/util/json-file.js +37 -0
- package/docs/how-it-works.md +6 -1
- package/package.json +6 -6
- package/skills/catchup/SKILL.md +6 -2
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BATCH } from '../constants.js';
|
|
2
2
|
const SESSION_LIMIT = 10000;
|
|
3
3
|
import { createContext } from '../../context.js';
|
|
4
|
-
import { embedObservations } from '../../capture/index.js';
|
|
4
|
+
import { embedObservations, finishReembed, releaseReembed, startReembed, } from '../../capture/index.js';
|
|
5
5
|
import { loadConfig } from '../../config/index.js';
|
|
6
6
|
import { readFileSync } from 'node:fs';
|
|
7
7
|
import { resolveProject } from '../../util/project.js';
|
|
@@ -127,39 +127,58 @@ export async function cmdPrune(argv) {
|
|
|
127
127
|
}
|
|
128
128
|
export async function cmdReembed(argv = []) {
|
|
129
129
|
const scoped = argv.includes('--project') || argv.includes('-p');
|
|
130
|
+
const background = argv.includes('--background');
|
|
131
|
+
const project = resolveProject(undefined);
|
|
130
132
|
const base = loadConfig();
|
|
131
133
|
const ctx = await createContext({ embeddings: { ...base.embeddings, timeoutMs: 0 } });
|
|
134
|
+
if (background)
|
|
135
|
+
startReembed(project);
|
|
136
|
+
let finished = false;
|
|
132
137
|
try {
|
|
133
138
|
const embedder = await ctx.embedder();
|
|
134
139
|
if (embedder.dimensions === 0) {
|
|
135
140
|
console.error('No embedder available; nothing to do.');
|
|
136
141
|
process.exit(1);
|
|
137
142
|
}
|
|
143
|
+
const stored = ctx.store.storedVectorDims?.() ?? null;
|
|
144
|
+
if (background && stored !== null && stored !== embedder.dimensions) {
|
|
145
|
+
finished = true;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
138
148
|
let migrated = false;
|
|
139
149
|
if (!scoped && ctx.store.migrateVectorDims) {
|
|
140
150
|
migrated = await ctx.store.migrateVectorDims(embedder.dimensions);
|
|
141
151
|
}
|
|
142
152
|
let updated = 0;
|
|
143
153
|
let skipped = 0;
|
|
144
|
-
const filter = scoped ? { project
|
|
154
|
+
const filter = scoped ? { project } : {};
|
|
145
155
|
const scanned = await eachObservation(ctx, filter, async (batch) => {
|
|
146
|
-
const stale = batch.filter((obs) => obs.embedder !== embedder.id
|
|
156
|
+
const stale = batch.filter((obs) => !obs.embedding?.length || (!background && obs.embedder !== embedder.id));
|
|
147
157
|
skipped += batch.length - stale.length;
|
|
148
158
|
if (stale.length === 0)
|
|
149
159
|
return;
|
|
150
160
|
await embedObservations(ctx, stale);
|
|
151
161
|
await ctx.store.insertObservations(stale);
|
|
152
162
|
updated += stale.length;
|
|
153
|
-
|
|
163
|
+
if (!background)
|
|
164
|
+
process.stderr.write(`\r${updated} re-embedded...`);
|
|
154
165
|
});
|
|
155
|
-
|
|
166
|
+
if (!background)
|
|
167
|
+
process.stderr.write('\r');
|
|
156
168
|
if (migrated) {
|
|
157
169
|
console.log(`Rebuilt vector storage at ${embedder.dimensions}d.`);
|
|
158
170
|
}
|
|
159
171
|
console.log(`Scanned ${scanned}, re-embedded ${updated} with ${embedder.id}` +
|
|
160
172
|
`${skipped > 0 ? `, ${skipped} already current` : ''}.`);
|
|
173
|
+
finished = true;
|
|
161
174
|
}
|
|
162
175
|
finally {
|
|
176
|
+
if (background) {
|
|
177
|
+
if (finished)
|
|
178
|
+
finishReembed(project);
|
|
179
|
+
else
|
|
180
|
+
releaseReembed(project);
|
|
181
|
+
}
|
|
163
182
|
await ctx.close();
|
|
164
183
|
}
|
|
165
184
|
}
|
package/dist/cli/install.js
CHANGED
|
@@ -9,6 +9,7 @@ const HOOKS = [
|
|
|
9
9
|
['SessionStart', 'session-start.js'],
|
|
10
10
|
['UserPromptSubmit', 'user-prompt.js'],
|
|
11
11
|
['SessionEnd', 'session-end.js'],
|
|
12
|
+
['Stop', 'turn-end.js'],
|
|
12
13
|
['PreToolUse', 'prefer-usages.js', 'Bash|Grep', 10],
|
|
13
14
|
['PreToolUse', 'pick-deliver.js', undefined, 5],
|
|
14
15
|
];
|
|
@@ -76,7 +76,7 @@ function memoryFiles(dir) {
|
|
|
76
76
|
for (const name of names) {
|
|
77
77
|
try {
|
|
78
78
|
const path = join(dir, name);
|
|
79
|
-
const parsed = parseMemoryFile(readFileSync(path, 'utf8'), statSync(path).mtimeMs);
|
|
79
|
+
const parsed = parseMemoryFile(readFileSync(path, 'utf8'), Math.trunc(statSync(path).mtimeMs));
|
|
80
80
|
if (parsed)
|
|
81
81
|
files.push(parsed);
|
|
82
82
|
}
|
package/dist/facts/handoff.d.ts
CHANGED
|
@@ -2,5 +2,7 @@ import type { RecallContext } from '../context.js';
|
|
|
2
2
|
import type { Observation } from '../types.js';
|
|
3
3
|
export declare const HANDOFF_TAG = "handoff";
|
|
4
4
|
export declare const HANDOFF_KEY = "handoff";
|
|
5
|
+
export declare function passedNote(noteAt: number, latestRequest: number | null): boolean;
|
|
6
|
+
export declare function handoffHeading(at: number, passed: boolean, now?: number): string;
|
|
5
7
|
export declare function newestHandoff(ctx: RecallContext, project: string, now?: number): Promise<Observation | null>;
|
|
6
8
|
export declare function handoffBodyLines(obs: Observation): string[];
|
package/dist/facts/handoff.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { formatDay } from '../util/day.js';
|
|
1
2
|
import { MANUAL_SESSION } from './render.js';
|
|
2
3
|
export const HANDOFF_TAG = 'handoff';
|
|
3
4
|
export const HANDOFF_KEY = 'handoff';
|
|
@@ -6,6 +7,12 @@ const HANDOFF_MAX_AGE_MS = 14 * DAY_MS;
|
|
|
6
7
|
const MANUAL_NOTES_SCANNED = 50;
|
|
7
8
|
const HANDOFF_MAX_LINES = 8;
|
|
8
9
|
const HANDOFF_LINE_CHARS = 220;
|
|
10
|
+
export function passedNote(noteAt, latestRequest) {
|
|
11
|
+
return latestRequest !== null && latestRequest > noteAt;
|
|
12
|
+
}
|
|
13
|
+
export function handoffHeading(at, passed, now = Date.now()) {
|
|
14
|
+
return `Last handoff (${formatDay(at, now)}${passed ? ', older than the last chat' : ''}):`;
|
|
15
|
+
}
|
|
9
16
|
export async function newestHandoff(ctx, project, now = Date.now()) {
|
|
10
17
|
const notes = await ctx.store.list({
|
|
11
18
|
project,
|
package/dist/facts/health.js
CHANGED
|
@@ -6,5 +6,6 @@ export function waitingWarning(waiting, oldestAt, usage, now = Date.now()) {
|
|
|
6
6
|
return null;
|
|
7
7
|
if (usage.used > 0 || usage.pausedUntil > now)
|
|
8
8
|
return null;
|
|
9
|
-
return `warning : ${waiting} chat(s) have waited over a day and no facts were tried today.
|
|
9
|
+
return (`warning : ${waiting} chat(s) have waited over a day and no facts were tried today. ` +
|
|
10
|
+
'Run claude-db distill --backfill to work through them now, or claude-db doctor to check Haiku');
|
|
10
11
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { RecallContext } from '../context.js';
|
|
2
|
+
import type { Observation } from '../types.js';
|
|
3
|
+
export interface RecentChat {
|
|
4
|
+
sessionId: string;
|
|
5
|
+
at: number;
|
|
6
|
+
summary: string | null;
|
|
7
|
+
turns: Observation[];
|
|
8
|
+
}
|
|
9
|
+
export declare function recentChats(ctx: RecallContext, project: string, current?: string): Promise<RecentChat[]>;
|
|
10
|
+
export declare function latestRequestAt(chats: RecentChat[], unfinished: {
|
|
11
|
+
askedAt: number;
|
|
12
|
+
}[]): number | null;
|
|
13
|
+
export declare function turnLine(turn: Observation): string;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { GIT_SESSION } from '../capture/git.js';
|
|
2
|
+
import { askedIn } from '../capture/turn-extractor.js';
|
|
3
|
+
import { toSnippet } from '../util/snippet.js';
|
|
4
|
+
import { FACT_SESSION } from './model.js';
|
|
5
|
+
import { MANUAL_SESSION } from './render.js';
|
|
6
|
+
const NOT_CHATS = [FACT_SESSION, MANUAL_SESSION, GIT_SESSION];
|
|
7
|
+
const ROWS_READ = 10;
|
|
8
|
+
const TURNS_SHOWN = 5;
|
|
9
|
+
const SUMMARY_CHARS = 300;
|
|
10
|
+
const SUMMARY_PARTS = ' | ';
|
|
11
|
+
export async function recentChats(ctx, project, current) {
|
|
12
|
+
const rows = (await ctx.store.list({
|
|
13
|
+
project,
|
|
14
|
+
excludeSessions: current ? [...NOT_CHATS, current] : NOT_CHATS,
|
|
15
|
+
newest: true,
|
|
16
|
+
limit: ROWS_READ,
|
|
17
|
+
}))
|
|
18
|
+
.filter((row) => row.status !== 'replaced')
|
|
19
|
+
.slice(0, TURNS_SHOWN);
|
|
20
|
+
const byChat = new Map();
|
|
21
|
+
for (const row of rows)
|
|
22
|
+
byChat.set(row.sessionId, [...(byChat.get(row.sessionId) ?? []), row]);
|
|
23
|
+
return Promise.all([...byChat].map(async ([sessionId, newestFirst]) => toChat(sessionId, newestFirst, await ctx.store.getSession(sessionId))));
|
|
24
|
+
}
|
|
25
|
+
function toChat(sessionId, newestFirst, session) {
|
|
26
|
+
const turns = [...newestFirst].reverse();
|
|
27
|
+
const at = newestFirst[0]?.createdAt ?? 0;
|
|
28
|
+
return { sessionId, at, summary: chatSummary(session, at, turns), turns };
|
|
29
|
+
}
|
|
30
|
+
function chatSummary(session, at, turns) {
|
|
31
|
+
if (!session?.summary || session.endedAt === undefined || session.endedAt < at)
|
|
32
|
+
return null;
|
|
33
|
+
const shown = new Set(turns.map((turn) => turn.title));
|
|
34
|
+
if (session.summary.split(SUMMARY_PARTS).every((part) => shown.has(part)))
|
|
35
|
+
return null;
|
|
36
|
+
return toSnippet(session.summary, SUMMARY_CHARS) ?? null;
|
|
37
|
+
}
|
|
38
|
+
export function latestRequestAt(chats, unfinished) {
|
|
39
|
+
const times = [...chats.map((chat) => chat.at), ...unfinished.map((entry) => entry.askedAt)];
|
|
40
|
+
return times.length === 0 ? null : Math.max(...times);
|
|
41
|
+
}
|
|
42
|
+
export function turnLine(turn) {
|
|
43
|
+
const asked = askedIn(turn.body);
|
|
44
|
+
return asked ? `asked "${asked}": ${turn.title}` : turn.title;
|
|
45
|
+
}
|
package/dist/facts/start.d.ts
CHANGED
|
@@ -3,4 +3,5 @@ export interface StartFacts {
|
|
|
3
3
|
block: string;
|
|
4
4
|
ids: Set<string>;
|
|
5
5
|
}
|
|
6
|
-
export declare function startFacts(ctx: RecallContext, project: string, hidden?: ReadonlySet<string
|
|
6
|
+
export declare function startFacts(ctx: RecallContext, project: string, hidden?: ReadonlySet<string>, current?: string): Promise<StartFacts | null>;
|
|
7
|
+
export declare function recentWorkBlock(ctx: RecallContext, project: string, current?: string, hidden?: ReadonlySet<string>): Promise<StartFacts | null>;
|
package/dist/facts/start.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { openWork } from '../capture/progress.js';
|
|
2
2
|
import { FACT_SESSION, factType, onePerKey, youScope } from './model.js';
|
|
3
|
-
import {
|
|
3
|
+
import { activeRequests } from '../capture/active.js';
|
|
4
|
+
import { handoffBodyLines, handoffHeading, newestHandoff, passedNote } from './handoff.js';
|
|
5
|
+
import { latestRequestAt, recentChats, turnLine } from './last-chat.js';
|
|
4
6
|
import { MANUAL_SESSION, factLine, seenByClaude } from './render.js';
|
|
5
|
-
import { formatDay } from '../util/day.js';
|
|
7
|
+
import { formatDay, formatDayTime } from '../util/day.js';
|
|
6
8
|
import { toShortId } from '../util/shortid.js';
|
|
7
9
|
const ABOUT_YOU = 6;
|
|
8
10
|
const THIS_PROJECT = 10;
|
|
9
11
|
const TODOS = 4;
|
|
10
12
|
const UNCOMMITTED = 3;
|
|
11
13
|
const ORDER = ['rule', 'decision', 'deadend', 'fact'];
|
|
12
|
-
export async function startFacts(ctx, project, hidden = new Set()) {
|
|
14
|
+
export async function startFacts(ctx, project, hidden = new Set(), current) {
|
|
13
15
|
const visible = (obs) => obs.status !== 'replaced' && !hidden.has(obs.id) && !seenByClaude(obs, project);
|
|
14
16
|
const list = async (owner, sessionId, limit) => (await ctx.store.list({ project: owner, sessionId, newest: true, limit })).filter(visible);
|
|
15
17
|
const you = onePerKey(await list(youScope(), FACT_SESSION, 50));
|
|
@@ -28,11 +30,15 @@ export async function startFacts(ctx, project, hidden = new Set()) {
|
|
|
28
30
|
const uncommitted = (await openWork(ctx.store, project))
|
|
29
31
|
.filter((obs) => !hidden.has(obs.id))
|
|
30
32
|
.slice(0, UNCOMMITTED);
|
|
33
|
+
const recent = await recentWork(ctx, project, current);
|
|
31
34
|
const sections = [
|
|
32
35
|
['About you:', you.slice(0, ABOUT_YOU).map((obs) => [factLine(obs), obs.id])],
|
|
33
36
|
['This project:', known.slice(0, THIS_PROJECT).map((obs) => [factLine(obs), obs.id])],
|
|
37
|
+
...recentSections(recent, hidden),
|
|
34
38
|
[
|
|
35
|
-
shownHandoff
|
|
39
|
+
shownHandoff
|
|
40
|
+
? handoffHeading(shownHandoff.createdAt, passedNote(shownHandoff.createdAt, recent.latest))
|
|
41
|
+
: '',
|
|
36
42
|
shownHandoff ? handoffEntries(shownHandoff) : [],
|
|
37
43
|
],
|
|
38
44
|
[
|
|
@@ -43,9 +49,29 @@ export async function startFacts(ctx, project, hidden = new Set()) {
|
|
|
43
49
|
],
|
|
44
50
|
],
|
|
45
51
|
];
|
|
52
|
+
return render(sections, ctx.config.inject.maxChars);
|
|
53
|
+
}
|
|
54
|
+
async function recentWork(ctx, project, current) {
|
|
55
|
+
const unfinished = activeRequests(await ctx.store.projectScope(project), current);
|
|
56
|
+
const chats = await recentChats(ctx, project, current);
|
|
57
|
+
return { unfinished, chats, latest: latestRequestAt(chats, unfinished) };
|
|
58
|
+
}
|
|
59
|
+
function recentSections(recent, hidden) {
|
|
60
|
+
return [
|
|
61
|
+
...recent.unfinished.map(unfinishedSection),
|
|
62
|
+
...recent.chats.map((chat, index) => chatSection(chat, index, hidden)),
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
export async function recentWorkBlock(ctx, project, current, hidden = new Set()) {
|
|
66
|
+
const sections = recentSections(await recentWork(ctx, project, current), hidden);
|
|
67
|
+
return sections.some(([, entries]) => entries.length > 0)
|
|
68
|
+
? render(sections, ctx.config.inject.maxChars)
|
|
69
|
+
: null;
|
|
70
|
+
}
|
|
71
|
+
function render(sections, maxChars) {
|
|
46
72
|
const lines = ['<memory>'];
|
|
47
73
|
const ids = new Set();
|
|
48
|
-
let budget =
|
|
74
|
+
let budget = maxChars;
|
|
49
75
|
for (const [heading, entries] of sections) {
|
|
50
76
|
if (entries.length === 0)
|
|
51
77
|
continue;
|
|
@@ -64,6 +90,19 @@ export async function startFacts(ctx, project, hidden = new Set()) {
|
|
|
64
90
|
lines.push('</memory>');
|
|
65
91
|
return { block: lines.join('\n'), ids };
|
|
66
92
|
}
|
|
93
|
+
function unfinishedSection(entry) {
|
|
94
|
+
return [
|
|
95
|
+
`Not finished yet in another chat (${formatDayTime(entry.askedAt)}):`,
|
|
96
|
+
[[`- asked "${entry.request}"`, null]],
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
function chatSection(chat, index, hidden) {
|
|
100
|
+
const heading = `${index === 0 ? 'Last chat' : 'Chat before'} (${formatDay(chat.at)}):`;
|
|
101
|
+
const turns = chat.turns
|
|
102
|
+
.filter((turn) => !hidden.has(turn.id))
|
|
103
|
+
.map((turn) => [`- ${turnLine(turn)} (${toShortId(turn.id)})`, turn.id]);
|
|
104
|
+
return [heading, chat.summary ? [[`- Summary: ${chat.summary}`, null], ...turns] : turns];
|
|
105
|
+
}
|
|
67
106
|
function handoffEntries(handoff) {
|
|
68
107
|
return handoffBodyLines(handoff).map((line, index) => [
|
|
69
108
|
index === 0 ? `${line} (${toShortId(handoff.id)})` : line,
|
|
@@ -56,7 +56,8 @@ export async function queryGraph(store, project, query) {
|
|
|
56
56
|
answer.inbound = [...direct, ...(await throughAliases(store, project, direct, edgeLimit))];
|
|
57
57
|
answer.outbound = edges.filter((edge) => idSet.has(edge.srcId) && !pointsAtSymbol(edge));
|
|
58
58
|
answer.empty = answer.definitions.length === 0 && answer.inbound.length === 0;
|
|
59
|
-
if (answer.empty)
|
|
59
|
+
if (answer.empty && query.suggest !== false) {
|
|
60
60
|
answer.suggestions = await suggestFor(store, project, query.symbol);
|
|
61
|
+
}
|
|
61
62
|
return answer;
|
|
62
63
|
}
|
package/dist/hooks/background.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { claimReingest, claimScrub, reingestDone, releaseReingest, releaseScrub, } from '../capture/index.js';
|
|
1
|
+
import { claimReembed, claimReingest, claimScrub, reingestDone, releaseReembed, releaseReingest, releaseScrub, } from '../capture/index.js';
|
|
2
2
|
import { FACTS_JOB } from '../facts/distill.js';
|
|
3
3
|
import { claimJob, jobMark, releaseJob } from '../util/job-lock.js';
|
|
4
4
|
import { CLI, PICK_WORKER, runDetached } from './detached.js';
|
|
@@ -14,6 +14,10 @@ export function startBackgroundWork(project, database, now = Date.now()) {
|
|
|
14
14
|
}
|
|
15
15
|
if (!reingestDone(project))
|
|
16
16
|
return;
|
|
17
|
+
if (claimReembed(project, now)) {
|
|
18
|
+
runDetached(CLI, ['reembed', '--project', '--background'], project, () => releaseReembed(project));
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
17
21
|
if (now - jobMark(FACTS_JOB, project) < FACTS_EVERY_MS)
|
|
18
22
|
return;
|
|
19
23
|
if (!claimJob(FACTS_JOB, project, now))
|
package/dist/hooks/detached.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export declare const CLI: string;
|
|
2
2
|
export declare const PICK_WORKER: string;
|
|
3
|
+
export declare const TURN_WORKER: string;
|
|
4
|
+
export declare const backgroundLog: () => string;
|
|
3
5
|
export declare function runDetached(script: string, args: string[], cwd: string, release: () => void): void;
|
package/dist/hooks/detached.js
CHANGED
|
@@ -1,16 +1,52 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
-
import {
|
|
2
|
+
import { appendFileSync, closeSync, mkdirSync, openSync, renameSync, statSync } from 'node:fs';
|
|
3
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
3
4
|
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { CONFIG_DIR } from '../config/dir.js';
|
|
4
6
|
const DIST = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
5
7
|
export const CLI = resolve(DIST, 'cli', 'index.js');
|
|
6
8
|
export const PICK_WORKER = resolve(DIST, 'pick', 'worker.js');
|
|
9
|
+
export const TURN_WORKER = resolve(DIST, 'hooks', 'turn-save.js');
|
|
10
|
+
const LOG_LIMIT = 1024 * 1024;
|
|
11
|
+
export const backgroundLog = () => join(CONFIG_DIR, 'logs', 'background.log');
|
|
12
|
+
function rotate(path) {
|
|
13
|
+
try {
|
|
14
|
+
if (statSync(path).size > LOG_LIMIT)
|
|
15
|
+
renameSync(path, `${path}.1`);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function openLog(script, args) {
|
|
22
|
+
try {
|
|
23
|
+
const path = backgroundLog();
|
|
24
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
25
|
+
rotate(path);
|
|
26
|
+
const name = script === CLI ? (args[0] ?? 'cli') : basename(script, '.js');
|
|
27
|
+
appendFileSync(path, `${new Date().toISOString()} ${name} started\n`);
|
|
28
|
+
return openSync(path, 'a');
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return 'ignore';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
7
34
|
export function runDetached(script, args, cwd, release) {
|
|
35
|
+
const log = openLog(script, args);
|
|
8
36
|
try {
|
|
9
|
-
spawn(process.execPath, [script, ...args], {
|
|
37
|
+
spawn(process.execPath, [script, ...args], {
|
|
38
|
+
cwd,
|
|
39
|
+
detached: true,
|
|
40
|
+
stdio: ['ignore', log, log],
|
|
41
|
+
})
|
|
10
42
|
.on('error', release)
|
|
11
43
|
.unref();
|
|
12
44
|
}
|
|
13
45
|
catch {
|
|
14
46
|
release();
|
|
15
47
|
}
|
|
48
|
+
finally {
|
|
49
|
+
if (typeof log === 'number')
|
|
50
|
+
closeSync(log);
|
|
51
|
+
}
|
|
16
52
|
}
|
package/dist/hooks/payload.d.ts
CHANGED
|
@@ -8,6 +8,11 @@ export interface HookPayload {
|
|
|
8
8
|
tool_input?: Record<string, unknown>;
|
|
9
9
|
tool_response?: unknown;
|
|
10
10
|
prompt?: string;
|
|
11
|
+
last_assistant_message?: string;
|
|
12
|
+
background_tasks?: {
|
|
13
|
+
status?: string;
|
|
14
|
+
}[];
|
|
15
|
+
session_crons?: unknown[];
|
|
11
16
|
}
|
|
12
17
|
export declare function capturingDisabled(scripted: boolean): boolean;
|
|
13
18
|
export declare function readPayload(): Promise<HookPayload>;
|
|
@@ -57,7 +57,7 @@ async function answerSymbols(ctx, project, symbols) {
|
|
|
57
57
|
store: ctx.store,
|
|
58
58
|
root: project,
|
|
59
59
|
project,
|
|
60
|
-
query: { mode: 'usages', symbol, limit: EDGE_LIMIT },
|
|
60
|
+
query: { mode: 'usages', symbol, limit: EDGE_LIMIT, suggest: false },
|
|
61
61
|
refresh: false,
|
|
62
62
|
text: false,
|
|
63
63
|
});
|
package/dist/hooks/relevance.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { redact } from '../capture/redact.js';
|
|
2
|
+
import { askedIn } from '../capture/turn-extractor.js';
|
|
2
3
|
import { meaningfulTokens } from '../search/stopwords.js';
|
|
3
4
|
import { formatDay } from '../util/day.js';
|
|
4
5
|
import { toShortId } from '../util/shortid.js';
|
|
@@ -46,11 +47,6 @@ export function renderPromptContext(entries, maxChars, expanded = [], expandMaxC
|
|
|
46
47
|
return null;
|
|
47
48
|
return ['<memory>', ...lines, '</memory>'].join('\n');
|
|
48
49
|
}
|
|
49
|
-
const ASKED_CHARS = 120;
|
|
50
|
-
function askedIn(body) {
|
|
51
|
-
const asked = /^Asked: (.*)$/m.exec(body)?.[1]?.replace(/\s+/g, ' ').trim() ?? '';
|
|
52
|
-
return asked.length > ASKED_CHARS ? `${asked.slice(0, ASKED_CHARS)}…` : asked;
|
|
53
|
-
}
|
|
54
50
|
export function renderPicked(picks, maxChars, shown = [], now = Date.now()) {
|
|
55
51
|
const lines = [];
|
|
56
52
|
let budget = maxChars;
|
|
@@ -11,6 +11,8 @@ import { resolveProject } from '../util/project.js';
|
|
|
11
11
|
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
12
12
|
import { distillInBackground } from './background.js';
|
|
13
13
|
import { clearPick, sweepPicks } from '../pick/pending.js';
|
|
14
|
+
import { clearActive, sweepActive } from '../capture/active.js';
|
|
15
|
+
import { dropTurnSave, sweepTurnSaves } from './turn-job.js';
|
|
14
16
|
silenceSqliteWarning();
|
|
15
17
|
await runHook(async () => {
|
|
16
18
|
if (capturingDisabled(loadConfig().capture.scripted))
|
|
@@ -42,6 +44,10 @@ await runHook(async () => {
|
|
|
42
44
|
clearCursor(sessionId);
|
|
43
45
|
clearPick(sessionId);
|
|
44
46
|
sweepPicks();
|
|
47
|
+
dropTurnSave(sessionId);
|
|
48
|
+
sweepTurnSaves();
|
|
49
|
+
clearActive(sessionId);
|
|
50
|
+
sweepActive();
|
|
45
51
|
if (ctx.config.distill.enabled)
|
|
46
52
|
distillInBackground(project, sessionId);
|
|
47
53
|
if (ctx.config.updates !== 'off' && isDue(readState())) {
|
|
@@ -5,7 +5,7 @@ import { dirname, join, resolve } from 'node:path';
|
|
|
5
5
|
import { fileURLToPath } from 'node:url';
|
|
6
6
|
import { createContext } from '../context.js';
|
|
7
7
|
import { distillNotice } from '../facts/notice.js';
|
|
8
|
-
import { startFacts } from '../facts/start.js';
|
|
8
|
+
import { recentWorkBlock, startFacts } from '../facts/start.js';
|
|
9
9
|
import { startBackgroundWork } from './background.js';
|
|
10
10
|
import { refreshGraphInBackground } from './graph-refresh.js';
|
|
11
11
|
import { recoverAfterCompact } from './compact.js';
|
|
@@ -61,12 +61,15 @@ await runHook(async () => {
|
|
|
61
61
|
? await recoverAfterCompact(ctx, project, payload.session_id, payload.transcript_path)
|
|
62
62
|
: null;
|
|
63
63
|
const restored = earlier?.ids ?? new Set();
|
|
64
|
-
const facts = await startFacts(ctx, project, restored);
|
|
64
|
+
const facts = await startFacts(ctx, project, restored, payload.session_id);
|
|
65
|
+
const recent = facts ? null : await recentWorkBlock(ctx, project, payload.session_id, restored);
|
|
65
66
|
const legacy = facts ? null : await legacyBlock(ctx, project, restored);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
const shown = [...(facts?.ids ?? []), ...(recent?.ids ?? [])];
|
|
68
|
+
if (shown.length > 0 && payload.session_id)
|
|
69
|
+
markShown(payload.session_id, shown);
|
|
70
|
+
const fallback = legacy?.hasMemory || !recent ? legacy?.text : undefined;
|
|
71
|
+
const parts = [earlier?.block, facts?.block ?? fallback, recent?.block].filter((part) => Boolean(part));
|
|
72
|
+
if ((facts || recent || legacy?.hasMemory) && mcpRegistered(project)) {
|
|
70
73
|
parts.push("Search this project's full history with the memory MCP tools before " +
|
|
71
74
|
'asking the user to re-explain prior decisions.');
|
|
72
75
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { activeToken } from '../capture/active.js';
|
|
3
|
+
import { loadConfig } from '../config/index.js';
|
|
4
|
+
import { resolveProject } from '../util/project.js';
|
|
5
|
+
import { TURN_WORKER, runDetached } from './detached.js';
|
|
6
|
+
import { capturingDisabled, readPayload, runHook } from './payload.js';
|
|
7
|
+
import { queueTurnSave } from './turn-job.js';
|
|
8
|
+
await runHook(async () => {
|
|
9
|
+
if (capturingDisabled(loadConfig().capture.scripted))
|
|
10
|
+
return;
|
|
11
|
+
const payload = await readPayload();
|
|
12
|
+
const sessionId = payload.session_id;
|
|
13
|
+
if (!sessionId)
|
|
14
|
+
return;
|
|
15
|
+
const project = resolveProject(payload.cwd);
|
|
16
|
+
const tasks = Array.isArray(payload.background_tasks) ? payload.background_tasks : [];
|
|
17
|
+
const crons = Array.isArray(payload.session_crons) ? payload.session_crons : [];
|
|
18
|
+
const stillWorking = crons.length > 0 || tasks.some((task) => task?.status === 'running');
|
|
19
|
+
queueTurnSave(sessionId, {
|
|
20
|
+
project,
|
|
21
|
+
...(payload.transcript_path ? { transcriptPath: payload.transcript_path } : {}),
|
|
22
|
+
finalReply: payload.last_assistant_message ?? '',
|
|
23
|
+
activeToken: stillWorking ? null : activeToken(sessionId),
|
|
24
|
+
});
|
|
25
|
+
runDetached(TURN_WORKER, [sessionId], project, () => { });
|
|
26
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface TurnJob {
|
|
2
|
+
project: string;
|
|
3
|
+
transcriptPath?: string;
|
|
4
|
+
finalReply: string;
|
|
5
|
+
activeToken: string | null;
|
|
6
|
+
}
|
|
7
|
+
export declare function queueTurnSave(sessionId: string, job: TurnJob): void;
|
|
8
|
+
export declare function takeTurnSave(sessionId: string): TurnJob | null;
|
|
9
|
+
export declare function dropTurnSave(sessionId: string): void;
|
|
10
|
+
export declare function sweepTurnSaves(now?: number): void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { rmSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { CONFIG_DIR } from '../config/dir.js';
|
|
4
|
+
import { readJson, safeName, sweepOlderThan, writeJsonAtomic } from '../util/json-file.js';
|
|
5
|
+
const KEPT_FOR_MS = 24 * 60 * 60 * 1000;
|
|
6
|
+
function folder() {
|
|
7
|
+
return join(CONFIG_DIR, 'turns');
|
|
8
|
+
}
|
|
9
|
+
function fileFor(sessionId) {
|
|
10
|
+
return join(folder(), `${safeName(sessionId)}.json`);
|
|
11
|
+
}
|
|
12
|
+
export function queueTurnSave(sessionId, job) {
|
|
13
|
+
writeJsonAtomic(fileFor(sessionId), job);
|
|
14
|
+
}
|
|
15
|
+
export function takeTurnSave(sessionId) {
|
|
16
|
+
const file = fileFor(sessionId);
|
|
17
|
+
const job = readJson(file);
|
|
18
|
+
rmSync(file, { force: true });
|
|
19
|
+
const valid = typeof job?.project === 'string' &&
|
|
20
|
+
typeof job.finalReply === 'string' &&
|
|
21
|
+
(job.transcriptPath === undefined || typeof job.transcriptPath === 'string') &&
|
|
22
|
+
(job.activeToken === null || typeof job.activeToken === 'string');
|
|
23
|
+
return valid ? job : null;
|
|
24
|
+
}
|
|
25
|
+
export function dropTurnSave(sessionId) {
|
|
26
|
+
rmSync(fileFor(sessionId), { force: true });
|
|
27
|
+
}
|
|
28
|
+
export function sweepTurnSaves(now = Date.now()) {
|
|
29
|
+
sweepOlderThan(folder(), KEPT_FOR_MS, now);
|
|
30
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { clearActive } from '../capture/active.js';
|
|
3
|
+
import { flushSession } from '../capture/index.js';
|
|
4
|
+
import { createContext } from '../context.js';
|
|
5
|
+
import { silenceSqliteWarning } from '../util/warnings.js';
|
|
6
|
+
import { runHook } from './payload.js';
|
|
7
|
+
import { takeTurnSave } from './turn-job.js';
|
|
8
|
+
silenceSqliteWarning();
|
|
9
|
+
await runHook(async () => {
|
|
10
|
+
const [sessionId] = process.argv.slice(2);
|
|
11
|
+
if (!sessionId)
|
|
12
|
+
return;
|
|
13
|
+
const job = takeTurnSave(sessionId);
|
|
14
|
+
if (!job)
|
|
15
|
+
return;
|
|
16
|
+
const ctx = await createContext();
|
|
17
|
+
try {
|
|
18
|
+
await flushSession(ctx, sessionId, job.project, job.transcriptPath, false, job.finalReply);
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
await ctx.close();
|
|
22
|
+
}
|
|
23
|
+
if (job.activeToken)
|
|
24
|
+
clearActive(sessionId, job.activeToken);
|
|
25
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { recordActive } from '../capture/active.js';
|
|
2
3
|
import { flushSession, redact } from '../capture/index.js';
|
|
3
4
|
import { createContext } from '../context.js';
|
|
4
5
|
import { capturingDisabled, emitContext, readPayload, runHook } from './payload.js';
|
|
@@ -22,6 +23,7 @@ await runHook(async () => {
|
|
|
22
23
|
const ctx = await createContext();
|
|
23
24
|
try {
|
|
24
25
|
await ctx.store.upsertSession({ id: sessionId, project, startedAt: Date.now() });
|
|
26
|
+
recordActive(sessionId, project, payload.prompt ?? '');
|
|
25
27
|
const { lastReply } = await flushSession(ctx, sessionId, project, payload.transcript_path);
|
|
26
28
|
const candidates = await promptCandidates(ctx, {
|
|
27
29
|
prompt: payload.prompt ?? '',
|