memorix 1.6.2 → 1.6.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 +13 -0
- package/dist/cli/index.js +1015 -789
- package/dist/index.js +769 -578
- package/dist/maintenance-runner.js +1061 -1017
- package/dist/media-audio-runner.js +1052 -1009
- package/dist/media-video-runner.js +1052 -1009
- package/dist/memcode-runtime/CHANGELOG.md +13 -0
- package/dist/sdk.js +1393 -1292
- package/dist/vector-backfill-runner.js +1050 -1007
- package/package.json +1 -1
- package/plugins/claude/memorix/.claude-plugin/plugin.json +1 -1
- package/plugins/codebuddy/memorix/.codebuddy-plugin/plugin.json +1 -1
- package/plugins/codex/memorix/.codex-plugin/plugin.json +1 -1
- package/plugins/copilot/memorix/plugin.json +1 -1
- package/plugins/gemini/memorix/gemini-extension.json +1 -1
- package/plugins/omp/memorix/package.json +1 -1
- package/plugins/openclaw/memorix/.codex-plugin/plugin.json +1 -1
- package/plugins/pi/memorix/package.json +1 -1
- package/server.json +2 -2
- package/src/cli/commands/background.ts +17 -3
- package/src/cli/commands/operator-shared.ts +12 -8
- package/src/cli/commands/serve-http.ts +25 -7
- package/src/cli/commands/serve-shared.ts +49 -6
- package/src/cli/commands/serve.ts +4 -0
- package/src/memory/session.ts +13 -3
- package/src/project/launch-root.ts +115 -0
- package/src/runtime/isolated-maintenance.ts +12 -1
- package/src/server.ts +38 -5
- package/src/store/obs-store.ts +2 -2
- package/src/store/sqlite-db.ts +2 -0
- package/src/store/sqlite-store.ts +19 -4
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "memorix",
|
|
3
3
|
"mcpName": "io.github.AVIDS2/memorix",
|
|
4
|
-
"version": "1.6.
|
|
4
|
+
"version": "1.6.3",
|
|
5
5
|
"description": "Local-first shared memory layer for AI coding agents across MCP clients, Git history, reasoning context, and project sessions.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"sideEffects": false,
|
package/server.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"url": "https://github.com/AVIDS2/memorix",
|
|
8
8
|
"source": "github"
|
|
9
9
|
},
|
|
10
|
-
"version": "1.6.
|
|
10
|
+
"version": "1.6.3",
|
|
11
11
|
"websiteUrl": "https://github.com/AVIDS2/memorix#readme",
|
|
12
12
|
"icons": [
|
|
13
13
|
{
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
{
|
|
23
23
|
"registryType": "npm",
|
|
24
24
|
"identifier": "memorix",
|
|
25
|
-
"version": "1.6.
|
|
25
|
+
"version": "1.6.3",
|
|
26
26
|
"runtimeHint": "npx",
|
|
27
27
|
"packageArguments": [
|
|
28
28
|
{
|
|
@@ -23,7 +23,9 @@ import path from 'node:path';
|
|
|
23
23
|
import { fileURLToPath } from 'node:url';
|
|
24
24
|
import { spawn, execFileSync } from 'node:child_process';
|
|
25
25
|
import { randomBytes } from 'node:crypto';
|
|
26
|
+
import os from 'node:os';
|
|
26
27
|
import { parseTcpPort } from '../port.js';
|
|
28
|
+
import { homeProjectRootError, inheritProjectRootFromHome, isHomeDirectory } from '../../project/launch-root.js';
|
|
27
29
|
|
|
28
30
|
// ============================================================
|
|
29
31
|
// Paths & Types
|
|
@@ -40,6 +42,16 @@ interface BackgroundState {
|
|
|
40
42
|
startCwd?: string;
|
|
41
43
|
}
|
|
42
44
|
|
|
45
|
+
export function resolveBackgroundCwd(cwd: string, homeDir: string = os.homedir()): string {
|
|
46
|
+
if (!isHomeDirectory(cwd, homeDir)) return cwd;
|
|
47
|
+
const inherited = inheritProjectRootFromHome({
|
|
48
|
+
homeDir,
|
|
49
|
+
envProjectRoot: process.env.MEMORIX_PROJECT_ROOT,
|
|
50
|
+
});
|
|
51
|
+
if (inherited) return inherited;
|
|
52
|
+
throw new Error(homeProjectRootError(cwd));
|
|
53
|
+
}
|
|
54
|
+
|
|
43
55
|
function getMemorixDir(): string {
|
|
44
56
|
const home = process.env.HOME || process.env.USERPROFILE || '';
|
|
45
57
|
// Use join-style path to ensure native separators on Windows
|
|
@@ -380,6 +392,7 @@ export async function doStart(port: number): Promise<void> {
|
|
|
380
392
|
// can be reclaimed with its parent by terminal hosts on Windows; a detached Node
|
|
381
393
|
// child can flash a console. Start-Process gives us a hidden, independent child
|
|
382
394
|
// and its real Node PID. POSIX retains the direct detached child path.
|
|
395
|
+
const startCwd = resolveBackgroundCwd(process.cwd());
|
|
383
396
|
let pid: number;
|
|
384
397
|
try {
|
|
385
398
|
if (process.platform === 'win32') {
|
|
@@ -387,7 +400,7 @@ export async function doStart(port: number): Promise<void> {
|
|
|
387
400
|
nodePath: process.execPath,
|
|
388
401
|
cliEntry,
|
|
389
402
|
port,
|
|
390
|
-
cwd:
|
|
403
|
+
cwd: startCwd,
|
|
391
404
|
stdoutLogFile: `${logFile}.stdout`,
|
|
392
405
|
stderrLogFile: logFile,
|
|
393
406
|
});
|
|
@@ -397,7 +410,7 @@ export async function doStart(port: number): Promise<void> {
|
|
|
397
410
|
detached: true,
|
|
398
411
|
stdio: ['ignore', logFd, logFd],
|
|
399
412
|
env: { ...process.env },
|
|
400
|
-
cwd:
|
|
413
|
+
cwd: startCwd,
|
|
401
414
|
});
|
|
402
415
|
child.unref();
|
|
403
416
|
fs.closeSync(logFd);
|
|
@@ -418,7 +431,7 @@ export async function doStart(port: number): Promise<void> {
|
|
|
418
431
|
startedAt: new Date().toISOString(),
|
|
419
432
|
logFile,
|
|
420
433
|
instanceToken,
|
|
421
|
-
startCwd
|
|
434
|
+
startCwd,
|
|
422
435
|
});
|
|
423
436
|
|
|
424
437
|
// 8. Print essential info immediately
|
|
@@ -824,4 +837,5 @@ export const _testing = {
|
|
|
824
837
|
formatBackgroundCommandError,
|
|
825
838
|
resolveBackgroundCliEntry,
|
|
826
839
|
resolveBackgroundCliEntryFrom,
|
|
840
|
+
resolveBackgroundCwd,
|
|
827
841
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { detectProjectWithDiagnostics } from '../../project/detector.js';
|
|
2
|
+
import { homeProjectRootError, isHomeDirectory, writeLastProjectRoot } from '../../project/launch-root.js';
|
|
2
3
|
import { getProjectDataDir } from '../../store/persistence.js';
|
|
3
4
|
import { initObservations, prepareSearchIndex } from '../../memory/observations.js';
|
|
4
5
|
import { initSessionStore } from '../../store/session-store.js';
|
|
@@ -42,19 +43,22 @@ async function resolveCliProjectContext(options?: CliContextOptions): Promise<{
|
|
|
42
43
|
invocation: ReturnType<typeof getCliInvocation>;
|
|
43
44
|
}> {
|
|
44
45
|
const invocation = getCliInvocation();
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
const requestedRoot = options?.projectRoot
|
|
47
|
+
?? invocation.projectRoot
|
|
48
|
+
?? process.env.MEMORIX_PROJECT_ROOT
|
|
49
|
+
?? process.cwd();
|
|
50
|
+
if (isHomeDirectory(requestedRoot)) {
|
|
51
|
+
throw new Error(homeProjectRootError(requestedRoot));
|
|
52
|
+
}
|
|
53
|
+
const detection = detectProjectWithDiagnostics(requestedRoot);
|
|
54
|
+
if (!detection.project || isHomeDirectory(detection.project.rootPath)) {
|
|
55
|
+
const detail = detection.failure?.detail ?? homeProjectRootError(requestedRoot);
|
|
53
56
|
throw new Error(detail);
|
|
54
57
|
}
|
|
55
58
|
|
|
56
59
|
const project = detection.project;
|
|
57
60
|
const dataDir = await getProjectDataDir(project.id);
|
|
61
|
+
writeLastProjectRoot(project.rootPath);
|
|
58
62
|
return { project, dataDir, invocation };
|
|
59
63
|
}
|
|
60
64
|
|
|
@@ -130,10 +130,29 @@ export default defineCommand({
|
|
|
130
130
|
const toolProfile = resolveToolProfile({ explicit: args.mode, envValue: process.env.MEMORIX_MODE, fallback: 'team' });
|
|
131
131
|
|
|
132
132
|
// Priority: explicit --cwd arg > MEMORIX_PROJECT_ROOT env > process.cwd().
|
|
133
|
-
//
|
|
133
|
+
// $HOME is never a project root; inherit last-project-root instead of creating ~/memorix.db.
|
|
134
134
|
let safeCwd: string;
|
|
135
135
|
try { safeCwd = process.cwd(); } catch { safeCwd = homedir(); }
|
|
136
|
-
|
|
136
|
+
const { resolveServeProject } = await import('./serve-shared.js');
|
|
137
|
+
const { writeLastProjectRoot } = await import('../../project/launch-root.js');
|
|
138
|
+
const persistMod = await import('../../store/persistence.js');
|
|
139
|
+
const detectorMod = await import('../../project/detector.js');
|
|
140
|
+
const serveResolution = resolveServeProject(
|
|
141
|
+
{
|
|
142
|
+
cwdArg: args.cwd,
|
|
143
|
+
envProjectRoot: process.env.MEMORIX_PROJECT_ROOT,
|
|
144
|
+
processCwd: safeCwd,
|
|
145
|
+
homeDir: homedir(),
|
|
146
|
+
},
|
|
147
|
+
{ detectProject: detectorMod.detectProject, findGitInSubdirs: detectorMod.findGitInSubdirs, isSystemDirectory: detectorMod.isSystemDirectory },
|
|
148
|
+
);
|
|
149
|
+
for (const message of serveResolution.messages) {
|
|
150
|
+
console.error(message);
|
|
151
|
+
}
|
|
152
|
+
let projectRoot = serveResolution.projectRoot;
|
|
153
|
+
if (serveResolution.detectedProject) {
|
|
154
|
+
writeLastProjectRoot(serveResolution.detectedProject.rootPath);
|
|
155
|
+
}
|
|
137
156
|
|
|
138
157
|
console.error(`[memorix] HTTP transport starting on ${host}:${port}`);
|
|
139
158
|
console.error(`[memorix] Project root: ${projectRoot}`);
|
|
@@ -157,11 +176,10 @@ export default defineCommand({
|
|
|
157
176
|
return store;
|
|
158
177
|
}
|
|
159
178
|
|
|
160
|
-
// Bootstrap the initial project's TeamStore
|
|
161
|
-
|
|
162
|
-
const
|
|
163
|
-
const
|
|
164
|
-
const teamDataDir = detectedProj ? await persistMod.getProjectDataDir(detectedProj.rootPath) : projectRoot;
|
|
179
|
+
// Bootstrap the initial project's TeamStore. Never open SQLite at $HOME
|
|
180
|
+
// (that creates a decoy ~/memorix.db). Unresolved launches share ~/.memorix/data.
|
|
181
|
+
const detectedProj = serveResolution.detectedProject ?? detectorMod.detectProject(projectRoot);
|
|
182
|
+
const teamDataDir = await persistMod.getProjectDataDir(detectedProj?.id ?? '__unresolved__');
|
|
165
183
|
const sharedTeamStore = await getTeamStore(teamDataDir);
|
|
166
184
|
|
|
167
185
|
type SessionState = {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { inheritProjectRootFromHome, isHomeDirectory } from '../../project/launch-root.js';
|
|
3
2
|
import type { ProjectInfo } from '../../types.js';
|
|
4
3
|
|
|
5
4
|
export interface ResolveServeProjectOptions {
|
|
@@ -19,7 +18,7 @@ export interface ResolveServeProjectDeps {
|
|
|
19
18
|
export interface ServeProjectResolution {
|
|
20
19
|
projectRoot: string;
|
|
21
20
|
detectedProject: ProjectInfo | null;
|
|
22
|
-
source: 'direct' | 'subdir' | 'unresolved';
|
|
21
|
+
source: 'direct' | 'subdir' | 'last-project-root' | 'unresolved';
|
|
23
22
|
messages: string[];
|
|
24
23
|
error?: string;
|
|
25
24
|
}
|
|
@@ -35,8 +34,54 @@ export function resolveServeProject(
|
|
|
35
34
|
options.processCwd;
|
|
36
35
|
|
|
37
36
|
const messages: string[] = [`[memorix] Starting with cwd: ${projectRoot}`];
|
|
37
|
+
const hasExplicitRoot = Boolean(options.cwdArg || options.envProjectRoot || options.initCwd);
|
|
38
|
+
const startsAtHome = isHomeDirectory(projectRoot, options.homeDir);
|
|
39
|
+
|
|
40
|
+
if (startsAtHome) {
|
|
41
|
+
if (hasExplicitRoot) {
|
|
42
|
+
messages.push(`[memorix] Refusing explicit $HOME project root: ${projectRoot}`);
|
|
43
|
+
messages.push('[memorix] Memorix will wait for MCP Roots or an explicit memorix_session_start projectRoot.');
|
|
44
|
+
return {
|
|
45
|
+
projectRoot,
|
|
46
|
+
detectedProject: null,
|
|
47
|
+
source: 'unresolved',
|
|
48
|
+
messages,
|
|
49
|
+
error: 'Refusing to bind $HOME as a project root. Pass a git project path instead of $HOME.',
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const inherited = inheritProjectRootFromHome({
|
|
53
|
+
homeDir: options.homeDir,
|
|
54
|
+
envProjectRoot: options.envProjectRoot,
|
|
55
|
+
});
|
|
56
|
+
if (inherited) {
|
|
57
|
+
const inheritedProject = deps.detectProject(inherited);
|
|
58
|
+
if (inheritedProject && !isHomeDirectory(inheritedProject.rootPath, options.homeDir)) {
|
|
59
|
+
messages.push(`[memorix] Unreliable launch directory detected: ${projectRoot}`);
|
|
60
|
+
messages.push(`[memorix] Inherited last git project: ${inheritedProject.rootPath}`);
|
|
61
|
+
return {
|
|
62
|
+
projectRoot: inheritedProject.rootPath,
|
|
63
|
+
detectedProject: inheritedProject,
|
|
64
|
+
source: 'last-project-root',
|
|
65
|
+
messages,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
messages.push(`[memorix] Unreliable launch directory detected: ${projectRoot}`);
|
|
70
|
+
messages.push('[memorix] Memorix will wait for MCP Roots or an explicit memorix_session_start projectRoot.');
|
|
71
|
+
messages.push('[memorix] It will not create ~/memorix.db or treat $HOME as a project.');
|
|
72
|
+
return {
|
|
73
|
+
projectRoot,
|
|
74
|
+
detectedProject: null,
|
|
75
|
+
source: 'unresolved',
|
|
76
|
+
messages,
|
|
77
|
+
error: 'No reliable git project was provided by the launcher.',
|
|
78
|
+
};
|
|
79
|
+
}
|
|
38
80
|
|
|
39
81
|
let detected = deps.detectProject(projectRoot);
|
|
82
|
+
if (detected && isHomeDirectory(detected.rootPath, options.homeDir)) {
|
|
83
|
+
detected = null;
|
|
84
|
+
}
|
|
40
85
|
if (detected) {
|
|
41
86
|
return {
|
|
42
87
|
projectRoot,
|
|
@@ -46,9 +91,7 @@ export function resolveServeProject(
|
|
|
46
91
|
};
|
|
47
92
|
}
|
|
48
93
|
|
|
49
|
-
|
|
50
|
-
const startsAtHome = path.resolve(projectRoot) === path.resolve(options.homeDir);
|
|
51
|
-
if (deps.isSystemDirectory(projectRoot) || (!hasExplicitRoot && startsAtHome)) {
|
|
94
|
+
if (deps.isSystemDirectory(projectRoot)) {
|
|
52
95
|
messages.push(`[memorix] Unreliable launch directory detected: ${projectRoot}`);
|
|
53
96
|
messages.push('[memorix] Memorix will wait for MCP Roots or an explicit memorix_session_start projectRoot.');
|
|
54
97
|
messages.push('[memorix] It will not restore a previous project automatically, to prevent cross-project memory access.');
|
|
@@ -71,6 +71,10 @@ export default defineCommand({
|
|
|
71
71
|
|
|
72
72
|
const detected = resolution.detectedProject;
|
|
73
73
|
const projectRoot = resolution.projectRoot;
|
|
74
|
+
if (detected) {
|
|
75
|
+
const { writeLastProjectRoot } = await import('../../project/launch-root.js');
|
|
76
|
+
writeLastProjectRoot(detected.rootPath);
|
|
77
|
+
}
|
|
74
78
|
|
|
75
79
|
// Always register ALL tools BEFORE connecting transport.
|
|
76
80
|
// This ensures tools/list returns the full tool set immediately on connect.
|
package/src/memory/session.ts
CHANGED
|
@@ -140,10 +140,20 @@ async function loadAliasActiveSessions(projectIds: Set<string>): Promise<Session
|
|
|
140
140
|
return groups.flat();
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
|
|
143
|
+
const SESSION_RESUME_OBSERVATION_LIMIT = 64;
|
|
144
|
+
const SESSION_CONTEXT_OBSERVATION_LIMIT = 128;
|
|
145
|
+
|
|
146
|
+
async function loadAliasActiveObservations(
|
|
147
|
+
projectIds: Set<string>,
|
|
148
|
+
limit: number = SESSION_RESUME_OBSERVATION_LIMIT,
|
|
149
|
+
): Promise<Observation[]> {
|
|
144
150
|
const store = getObservationStore();
|
|
145
151
|
const groups = await Promise.all(
|
|
146
|
-
[...projectIds].map((projectId) => store.loadByProject(projectId, {
|
|
152
|
+
[...projectIds].map((projectId) => store.loadByProject(projectId, {
|
|
153
|
+
status: 'active',
|
|
154
|
+
limit,
|
|
155
|
+
newestFirst: true,
|
|
156
|
+
})),
|
|
147
157
|
);
|
|
148
158
|
return groups.flat();
|
|
149
159
|
}
|
|
@@ -522,7 +532,7 @@ export async function getSessionContext(
|
|
|
522
532
|
const aliasSet = await resolveProjectIds(projectId);
|
|
523
533
|
const [sessions, allObs] = await Promise.all([
|
|
524
534
|
loadAliasSessions(aliasSet),
|
|
525
|
-
loadAliasActiveObservations(aliasSet),
|
|
535
|
+
loadAliasActiveObservations(aliasSet, SESSION_CONTEXT_OBSERVATION_LIMIT),
|
|
526
536
|
]);
|
|
527
537
|
const readableObs = readableAliasObservations(allObs, aliasSet, reader);
|
|
528
538
|
/** Check if a session summary contains noise/system-self content */
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Launch-root guards for HTTP/stdio/background.
|
|
3
|
+
*
|
|
4
|
+
* LaunchAgents and Cursor stdio often start with cwd=$HOME. Binding that path
|
|
5
|
+
* treats the home directory as a project and can create a decoy ~/memorix.db
|
|
6
|
+
* while the real store lives in ~/.memorix/data/memorix.db.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import os from 'node:os';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
|
|
13
|
+
export const LAST_PROJECT_ROOT_FILENAME = 'last-project-root';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Resolve the home directory used by Memorix boundary guards.
|
|
17
|
+
*
|
|
18
|
+
* Node's os.homedir() is intentionally stable on Windows and does not always
|
|
19
|
+
* follow a process-level HOME/USERPROFILE override. The CLI and test harnesses
|
|
20
|
+
* use those environment variables as the configured home, so all guards need
|
|
21
|
+
* one shared resolution rule instead of mixing the two sources.
|
|
22
|
+
*/
|
|
23
|
+
export function effectiveHomeDirectory(): string {
|
|
24
|
+
const configuredHome = process.platform === 'win32'
|
|
25
|
+
? process.env.USERPROFILE?.trim() || process.env.HOME?.trim()
|
|
26
|
+
: process.env.HOME?.trim() || process.env.USERPROFILE?.trim();
|
|
27
|
+
return path.resolve(configuredHome || os.homedir());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function isHomeDirectory(candidate: string, homeDir: string = effectiveHomeDirectory()): boolean {
|
|
31
|
+
try {
|
|
32
|
+
return path.resolve(candidate) === path.resolve(homeDir);
|
|
33
|
+
} catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function lastProjectRootPath(homeDir: string = effectiveHomeDirectory()): string {
|
|
39
|
+
return path.join(homeDir, '.memorix', LAST_PROJECT_ROOT_FILENAME);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function readLastProjectRoot(homeDir: string = effectiveHomeDirectory()): string | null {
|
|
43
|
+
try {
|
|
44
|
+
const raw = readFileSync(lastProjectRootPath(homeDir), 'utf8').trim();
|
|
45
|
+
if (!raw) return null;
|
|
46
|
+
const resolved = path.resolve(raw);
|
|
47
|
+
if (isHomeDirectory(resolved, homeDir)) return null;
|
|
48
|
+
if (!existsSync(resolved)) return null;
|
|
49
|
+
return resolved;
|
|
50
|
+
} catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function writeLastProjectRoot(projectRoot: string, homeDir?: string): void {
|
|
56
|
+
// Tests pass an explicit homeDir. Default writes are skipped under Vitest so
|
|
57
|
+
// unit tests do not mutate the developer's ~/.memorix/last-project-root.
|
|
58
|
+
if (process.env.VITEST && homeDir === undefined) return;
|
|
59
|
+
const resolvedHome = homeDir ?? effectiveHomeDirectory();
|
|
60
|
+
const resolved = path.resolve(projectRoot);
|
|
61
|
+
if (isHomeDirectory(resolved, resolvedHome)) return;
|
|
62
|
+
if (!existsSync(resolved)) return;
|
|
63
|
+
const memorixDir = path.join(resolvedHome, '.memorix');
|
|
64
|
+
mkdirSync(memorixDir, { recursive: true });
|
|
65
|
+
writeFileSync(lastProjectRootPath(resolvedHome), `${resolved}\n`, 'utf8');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function homeProjectRootError(candidate: string): string {
|
|
69
|
+
return (
|
|
70
|
+
`Refusing to bind $HOME as a project root (${candidate}). ` +
|
|
71
|
+
'Pass a git project via --cwd, MEMORIX_PROJECT_ROOT, or memorix_session_start({ projectRoot }). ' +
|
|
72
|
+
'Memorix will not create ~/memorix.db.'
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* When the launcher cwd is $HOME, inherit the last git project or an explicit
|
|
78
|
+
* MEMORIX_PROJECT_ROOT. System directories stay fail-closed (no restore).
|
|
79
|
+
*/
|
|
80
|
+
export function inheritProjectRootFromHome(options: {
|
|
81
|
+
homeDir: string;
|
|
82
|
+
envProjectRoot?: string;
|
|
83
|
+
readLastRoot?: () => string | null;
|
|
84
|
+
}): string | null {
|
|
85
|
+
const envRoot = options.envProjectRoot?.trim();
|
|
86
|
+
if (envRoot && !isHomeDirectory(envRoot, options.homeDir) && existsSync(envRoot)) {
|
|
87
|
+
return path.resolve(envRoot);
|
|
88
|
+
}
|
|
89
|
+
const lastRoot = (options.readLastRoot ?? (() => readLastProjectRoot(options.homeDir)))();
|
|
90
|
+
if (lastRoot && !isHomeDirectory(lastRoot, options.homeDir) && existsSync(lastRoot)) {
|
|
91
|
+
return lastRoot;
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Shared SQLite must never be opened at $HOME — that creates a decoy ~/memorix.db.
|
|
98
|
+
*/
|
|
99
|
+
export function resolveControlPlaneDataDir(options: {
|
|
100
|
+
requestedDataDir?: string;
|
|
101
|
+
homeDir?: string;
|
|
102
|
+
globalDataDir: string;
|
|
103
|
+
}): string {
|
|
104
|
+
const homeDir = options.homeDir ?? effectiveHomeDirectory();
|
|
105
|
+
if (options.requestedDataDir && !isHomeDirectory(options.requestedDataDir, homeDir)) {
|
|
106
|
+
return options.requestedDataDir;
|
|
107
|
+
}
|
|
108
|
+
return options.globalDataDir;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function assertNotHomeDataDir(dataDir: string, homeDir: string = effectiveHomeDirectory()): void {
|
|
112
|
+
if (isHomeDirectory(dataDir, homeDir)) {
|
|
113
|
+
throw new Error(homeProjectRootError(dataDir));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
|
|
2
2
|
import { existsSync } from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
3
4
|
import path from 'node:path';
|
|
4
5
|
import { fileURLToPath } from 'node:url';
|
|
5
6
|
|
|
@@ -52,6 +53,16 @@ function childError(prefix: string, detail: string): Error {
|
|
|
52
53
|
return new Error(cleaned ? `${prefix}: ${cleaned}` : prefix);
|
|
53
54
|
}
|
|
54
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Isolated jobs already receive an absolute projectRoot on stdin. Using that
|
|
58
|
+
* directory as spawn cwd keeps the folder locked on Windows (EBUSY on rmdir)
|
|
59
|
+
* for the life of the child. Park the process in a disposable temp dir instead.
|
|
60
|
+
*/
|
|
61
|
+
export function isolatedMaintenanceCwd(): string {
|
|
62
|
+
const tmp = os.tmpdir();
|
|
63
|
+
return existsSync(tmp) ? tmp : process.cwd();
|
|
64
|
+
}
|
|
65
|
+
|
|
55
66
|
/**
|
|
56
67
|
* Resolve the runner beside the compiled package entry. The CLI bundle lives
|
|
57
68
|
* in dist/cli while the library entry lives directly in dist.
|
|
@@ -148,7 +159,7 @@ export async function runMaintenanceInChildProcess(
|
|
|
148
159
|
let child: ChildProcessWithoutNullStreams;
|
|
149
160
|
try {
|
|
150
161
|
child = spawn(process.execPath, [runnerPath], {
|
|
151
|
-
cwd:
|
|
162
|
+
cwd: isolatedMaintenanceCwd(),
|
|
152
163
|
env,
|
|
153
164
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
154
165
|
windowsHide: true,
|
package/src/server.ts
CHANGED
|
@@ -33,6 +33,7 @@ import { canManageObservation, canReadObservation, filterReadableObservations, r
|
|
|
33
33
|
import { compactSearch, compactTimeline, compactDetail } from './compact/engine.js';
|
|
34
34
|
import { buildGraphContextPacket, formatGraphContextPrompt } from './memory/graph-context.js';
|
|
35
35
|
import { detectProject } from './project/detector.js';
|
|
36
|
+
import { homeProjectRootError, isHomeDirectory, writeLastProjectRoot } from './project/launch-root.js';
|
|
36
37
|
import { registerAlias, initAliasRegistry, resolveAliases, autoMergeByBaseName } from './project/aliases.js';
|
|
37
38
|
import { getProjectDataDir } from './store/persistence.js';
|
|
38
39
|
import type { ObservationType, RuleSource, AgentTarget, MCPServerEntry, ObservationReader } from './types.js';
|
|
@@ -228,6 +229,7 @@ export const BOOTSTRAP_SAFE_TOOL_NAMES = new Set([
|
|
|
228
229
|
'memorix_codegraph_status',
|
|
229
230
|
'memorix_graph_context',
|
|
230
231
|
'memorix_context_pack',
|
|
232
|
+
'memorix_session_start',
|
|
231
233
|
]);
|
|
232
234
|
|
|
233
235
|
const AUTOPILOT_RETRIEVAL_BOUNDARY_TTL_MS = 2 * 60 * 1000;
|
|
@@ -265,7 +267,10 @@ export async function createMemorixServer(
|
|
|
265
267
|
});
|
|
266
268
|
const teamFeaturesEnabled = isToolInProfile('team_manage', toolProfile);
|
|
267
269
|
const projectBinding = options.projectBinding ?? createProjectBindingController(cwd ?? process.cwd());
|
|
268
|
-
const
|
|
270
|
+
const detectedProjectRaw = detectProject(cwd);
|
|
271
|
+
const detectedProject = detectedProjectRaw && !isHomeDirectory(detectedProjectRaw.rootPath)
|
|
272
|
+
? detectedProjectRaw
|
|
273
|
+
: null;
|
|
269
274
|
let rawProject: import('./types.js').ProjectInfo;
|
|
270
275
|
let projectResolved = true;
|
|
271
276
|
let projectResolutionError: string | null = null;
|
|
@@ -316,6 +321,7 @@ export async function createMemorixServer(
|
|
|
316
321
|
}
|
|
317
322
|
if (projectResolved) {
|
|
318
323
|
projectBinding.recordResolvedProject(project.id, project.rootPath);
|
|
324
|
+
writeLastProjectRoot(project.rootPath);
|
|
319
325
|
}
|
|
320
326
|
|
|
321
327
|
const registerMaintenanceTarget = async (): Promise<void> => {
|
|
@@ -362,6 +368,7 @@ export async function createMemorixServer(
|
|
|
362
368
|
|
|
363
369
|
const lightweightUnresolvedSession = !projectResolved && deferProjectInitUntilBound;
|
|
364
370
|
let projectRuntimeInitPromise: Promise<void> | null = null;
|
|
371
|
+
let runtimeHydratedForDir: string | null = null;
|
|
365
372
|
|
|
366
373
|
const initializeProjectRuntime = async (logPrefix: 'startup' | 'switch'): Promise<void> => {
|
|
367
374
|
await initObservationStore(projectDir);
|
|
@@ -430,14 +437,20 @@ export async function createMemorixServer(
|
|
|
430
437
|
} catch { /* migration is optional */ }
|
|
431
438
|
|
|
432
439
|
await initializeProjectRuntime('startup');
|
|
440
|
+
runtimeHydratedForDir = projectDir;
|
|
433
441
|
} else {
|
|
434
442
|
// Intentionally silent — serve-http.ts deferred logging handles session lifecycle visibility.
|
|
435
443
|
// Noisy per-probe 'awaiting binding' log was removed to reduce terminal spam.
|
|
436
444
|
}
|
|
437
445
|
|
|
438
446
|
const ensureProjectRuntimeInitialized = async (): Promise<void> => {
|
|
439
|
-
if (
|
|
447
|
+
if (!projectResolved) return;
|
|
448
|
+
if (runtimeHydratedForDir === projectDir) {
|
|
449
|
+
if (projectRuntimeInitPromise) await projectRuntimeInitPromise;
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
440
452
|
if (!projectRuntimeInitPromise) {
|
|
453
|
+
const targetDir = projectDir;
|
|
441
454
|
projectRuntimeInitPromise = (async () => {
|
|
442
455
|
// Auto-merge obvious alias groups by scanning observed projectIds in data.
|
|
443
456
|
try {
|
|
@@ -470,7 +483,8 @@ export async function createMemorixServer(
|
|
|
470
483
|
}
|
|
471
484
|
} catch { /* migration is optional */ }
|
|
472
485
|
|
|
473
|
-
await initializeProjectRuntime('startup');
|
|
486
|
+
await initializeProjectRuntime(runtimeHydratedForDir ? 'switch' : 'startup');
|
|
487
|
+
runtimeHydratedForDir = targetDir;
|
|
474
488
|
})().catch((err) => {
|
|
475
489
|
projectRuntimeInitPromise = null;
|
|
476
490
|
throw err;
|
|
@@ -3846,6 +3860,15 @@ export async function createMemorixServer(
|
|
|
3846
3860
|
// BEFORE checking whether the project is resolved. This is the primary
|
|
3847
3861
|
// mechanism for HTTP/control-plane multi-project support.
|
|
3848
3862
|
if (explicitRoot && typeof explicitRoot === 'string') {
|
|
3863
|
+
if (isHomeDirectory(explicitRoot)) {
|
|
3864
|
+
return {
|
|
3865
|
+
content: [{
|
|
3866
|
+
type: 'text' as const,
|
|
3867
|
+
text: homeProjectRootError(explicitRoot),
|
|
3868
|
+
}],
|
|
3869
|
+
isError: true as const,
|
|
3870
|
+
};
|
|
3871
|
+
}
|
|
3849
3872
|
let bound = await switchProject(explicitRoot, 'explicit-project-root');
|
|
3850
3873
|
// switchProject returns false for both "same project, no-op" and "no git repo".
|
|
3851
3874
|
// Only scan subdirectories when explicitRoot is not itself a git repo; otherwise
|
|
@@ -5503,8 +5526,12 @@ export async function createMemorixServer(
|
|
|
5503
5526
|
): Promise<boolean> => {
|
|
5504
5527
|
if (source === 'mcp-roots' && projectBinding.isExplicit()) return false;
|
|
5505
5528
|
const { detectProjectWithDiagnostics } = await import('./project/detector.js');
|
|
5529
|
+
if (isHomeDirectory(newCwd)) {
|
|
5530
|
+
console.error(`[memorix] Refusing to bind $HOME as a project root: ${newCwd}`);
|
|
5531
|
+
return false;
|
|
5532
|
+
}
|
|
5506
5533
|
const result = detectProjectWithDiagnostics(newCwd);
|
|
5507
|
-
if (!result.project) {
|
|
5534
|
+
if (!result.project || isHomeDirectory(result.project.rootPath)) {
|
|
5508
5535
|
if (result.failure) {
|
|
5509
5536
|
console.error(`[memorix] Project detection failed for "${newCwd}": [${result.failure.reason}] ${result.failure.detail}`);
|
|
5510
5537
|
}
|
|
@@ -5523,6 +5550,7 @@ export async function createMemorixServer(
|
|
|
5523
5550
|
else if (source === 'mcp-roots') projectBinding.bindFromRoots(newDetected.rootPath);
|
|
5524
5551
|
else projectBinding.bindStartup(newDetected.rootPath);
|
|
5525
5552
|
projectBinding.recordResolvedProject(project.id, project.rootPath);
|
|
5553
|
+
writeLastProjectRoot(project.rootPath);
|
|
5526
5554
|
return false; // same project, no-op
|
|
5527
5555
|
}
|
|
5528
5556
|
|
|
@@ -5580,7 +5608,12 @@ export async function createMemorixServer(
|
|
|
5580
5608
|
}
|
|
5581
5609
|
} catch { /* best-effort - coordination features degrade gracefully */ }
|
|
5582
5610
|
|
|
5583
|
-
|
|
5611
|
+
writeLastProjectRoot(project.rootPath);
|
|
5612
|
+
runtimeHydratedForDir = null;
|
|
5613
|
+
projectRuntimeInitPromise = null;
|
|
5614
|
+
void ensureProjectRuntimeInitialized().catch((err) => {
|
|
5615
|
+
console.error(`[memorix] Background project runtime init failed: ${err instanceof Error ? err.message : err}`);
|
|
5616
|
+
});
|
|
5584
5617
|
try {
|
|
5585
5618
|
await startProjectMaintenanceWorker();
|
|
5586
5619
|
} catch {
|