@causyne/agent 1.20.127 → 1.20.128
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/docs/THIRD_PARTY_NOTICES.md +36 -2
- package/package.json +3 -1
- package/src/agentLoop.js +59 -26
- package/src/agentLoop.ts +59 -24
- package/src/browser/causyneBrowser.js +31 -0
- package/src/browser/causyneBrowser.ts +32 -0
- package/src/commands.js +177 -13
- package/src/commands.ts +194 -13
- package/src/memory/episodicStore.js +40 -0
- package/src/memory/episodicStore.ts +46 -0
- package/src/memory.js +67 -0
- package/src/memory.ts +65 -0
- package/src/personalization.js +365 -0
- package/src/personalization.ts +471 -0
- package/src/projectInstructions.js +30 -10
- package/src/projectInstructions.ts +31 -12
- package/src/providers/acp.js +3 -2
- package/src/providers/acp.ts +3 -2
- package/src/providers/antigravity.js +2 -0
- package/src/providers/antigravity.ts +2 -0
- package/src/tools/memoryWrite.js +15 -1
- package/src/tools/memoryWrite.ts +16 -1
- package/src/tools/osSandbox.js +1 -1
- package/src/tools/osSandbox.ts +1 -1
- package/src/tools/ptyTerminal.js +17 -5
- package/src/tools/ptyTerminal.ts +19 -5
- package/src/web/client/browserPanel.js +234 -31
- package/src/web/client/browserPanel.ts +240 -31
- package/src/web/client/chatStream.js +19 -7
- package/src/web/client/chatStream.ts +17 -9
- package/src/web/client/cliPanel.js +3 -3
- package/src/web/client/cliPanel.ts +3 -3
- package/src/web/client/dom.js +53 -2
- package/src/web/client/dom.ts +54 -3
- package/src/web/client/filesAndCanvas.js +3 -1
- package/src/web/client/filesAndCanvas.ts +3 -1
- package/src/web/client/main.js +28 -13
- package/src/web/client/main.ts +35 -12
- package/src/web/client/settingsModal.js +641 -8
- package/src/web/client/settingsModal.ts +791 -8
- package/src/web/client/state.js +31 -1
- package/src/web/client/state.ts +27 -1
- package/src/web/client/studioLayout.js +39 -4
- package/src/web/client/studioLayout.ts +47 -4
- package/src/web/client/studioShell.js +370 -37
- package/src/web/client/studioShell.ts +368 -36
- package/src/web/client/terminalPanel.js +378 -129
- package/src/web/client/terminalPanel.ts +394 -107
- package/src/web/client/terminalTheme.js +249 -0
- package/src/web/client/terminalTheme.ts +258 -0
- package/src/web/client/types.ts +3 -0
- package/src/web/client/validators.js +51 -10
- package/src/web/client/validators.ts +54 -10
- package/src/web/server/routesBrowser.js +64 -10
- package/src/web/server/routesBrowser.ts +62 -13
- package/src/web/server/routesSettings.js +344 -0
- package/src/web/server/routesSettings.ts +397 -0
- package/src/web/server/staticAssets.js +24 -0
- package/src/web/server/staticAssets.ts +25 -0
- package/src/web/skybridgeServer.js +3 -0
- package/src/web/skybridgeServer.ts +2 -0
- package/src/web/skybridgeUi.js +5 -1
- package/src/web/skybridgeUi.ts +8 -1
- package/src/web/styles/overlays.css +499 -0
- package/src/web/styles/studio.css +274 -46
- package/src/web/ui/modalsTemplate.js +320 -71
- package/src/web/ui/modalsTemplate.ts +320 -71
- package/src/web/ui/studioTemplate.js +19 -21
- package/src/web/ui/studioTemplate.ts +19 -21
package/src/memory.js
CHANGED
|
@@ -101,6 +101,73 @@ export function clearMemory() {
|
|
|
101
101
|
logMemoryError('clear', e);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
+
function canonicalWorkspaceKey(workspaceRoot) {
|
|
105
|
+
const resolved = path.resolve(workspaceRoot);
|
|
106
|
+
let canonical = resolved;
|
|
107
|
+
try {
|
|
108
|
+
canonical = typeof fs.realpathSync.native === 'function'
|
|
109
|
+
? fs.realpathSync.native(resolved)
|
|
110
|
+
: fs.realpathSync(resolved);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
canonical = resolved;
|
|
114
|
+
}
|
|
115
|
+
return process.platform === 'win32' ? canonical.toLowerCase() : canonical;
|
|
116
|
+
}
|
|
117
|
+
export function clearMemoryForWorkspace(workspaceRoot) {
|
|
118
|
+
try {
|
|
119
|
+
const resolvedWs = path.resolve(workspaceRoot);
|
|
120
|
+
const targetKey = canonicalWorkspaceKey(workspaceRoot);
|
|
121
|
+
const mem = getMemory();
|
|
122
|
+
const entries = mem.entries || [];
|
|
123
|
+
const removedTexts = new Set();
|
|
124
|
+
const keptEntries = entries.filter((entry) => {
|
|
125
|
+
if (entry.workspace && canonicalWorkspaceKey(entry.workspace) === targetKey) {
|
|
126
|
+
removedTexts.add(entry.text);
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
});
|
|
131
|
+
const stillReferenced = new Set(keptEntries.map((e) => e.text));
|
|
132
|
+
const keptFacts = mem.facts.filter((fact) => {
|
|
133
|
+
if (removedTexts.has(fact) && !stillReferenced.has(fact))
|
|
134
|
+
return false;
|
|
135
|
+
if (fact.includes(resolvedWs) || fact.includes(targetKey)) {
|
|
136
|
+
removedTexts.add(fact);
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
return true;
|
|
140
|
+
});
|
|
141
|
+
const removedCount = Math.max(mem.facts.length - keptFacts.length, entries.length - keptEntries.length);
|
|
142
|
+
if (removedCount > 0) {
|
|
143
|
+
saveMemory({ facts: keptFacts, entries: keptEntries });
|
|
144
|
+
}
|
|
145
|
+
return removedCount;
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
logMemoryError('clear-workspace', e);
|
|
149
|
+
return 0;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export function getMemoryStats(workspaceRoot) {
|
|
153
|
+
const mem = getMemory();
|
|
154
|
+
const totalFacts = mem.facts.length;
|
|
155
|
+
if (!workspaceRoot)
|
|
156
|
+
return { projectFacts: totalFacts, totalFacts };
|
|
157
|
+
const resolvedWs = path.resolve(workspaceRoot);
|
|
158
|
+
const targetKey = canonicalWorkspaceKey(workspaceRoot);
|
|
159
|
+
const matchingTexts = new Set();
|
|
160
|
+
for (const entry of mem.entries || []) {
|
|
161
|
+
if (entry.workspace && canonicalWorkspaceKey(entry.workspace) === targetKey) {
|
|
162
|
+
matchingTexts.add(entry.text);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
for (const fact of mem.facts) {
|
|
166
|
+
if (fact.includes(resolvedWs) || fact.includes(targetKey))
|
|
167
|
+
matchingTexts.add(fact);
|
|
168
|
+
}
|
|
169
|
+
return { projectFacts: matchingTexts.size, totalFacts };
|
|
170
|
+
}
|
|
104
171
|
export function getMemoryContextString(options) {
|
|
105
172
|
const mem = getMemory();
|
|
106
173
|
return formatMemoryContext(mem, options);
|
package/src/memory.ts
CHANGED
|
@@ -105,6 +105,71 @@ export function clearMemory(): void {
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
+
function canonicalWorkspaceKey(workspaceRoot: string): string {
|
|
109
|
+
const resolved = path.resolve(workspaceRoot);
|
|
110
|
+
let canonical = resolved;
|
|
111
|
+
try {
|
|
112
|
+
canonical = typeof fs.realpathSync.native === 'function'
|
|
113
|
+
? fs.realpathSync.native(resolved)
|
|
114
|
+
: fs.realpathSync(resolved);
|
|
115
|
+
} catch {
|
|
116
|
+
canonical = resolved;
|
|
117
|
+
}
|
|
118
|
+
return process.platform === 'win32' ? canonical.toLowerCase() : canonical;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function clearMemoryForWorkspace(workspaceRoot: string): number {
|
|
122
|
+
try {
|
|
123
|
+
const resolvedWs = path.resolve(workspaceRoot);
|
|
124
|
+
const targetKey = canonicalWorkspaceKey(workspaceRoot);
|
|
125
|
+
const mem = getMemory();
|
|
126
|
+
const entries = mem.entries || [];
|
|
127
|
+
const removedTexts = new Set<string>();
|
|
128
|
+
const keptEntries = entries.filter((entry) => {
|
|
129
|
+
if (entry.workspace && canonicalWorkspaceKey(entry.workspace) === targetKey) {
|
|
130
|
+
removedTexts.add(entry.text);
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
return true;
|
|
134
|
+
});
|
|
135
|
+
const stillReferenced = new Set(keptEntries.map((e) => e.text));
|
|
136
|
+
const keptFacts = mem.facts.filter((fact) => {
|
|
137
|
+
if (removedTexts.has(fact) && !stillReferenced.has(fact)) return false;
|
|
138
|
+
if (fact.includes(resolvedWs) || fact.includes(targetKey)) {
|
|
139
|
+
removedTexts.add(fact);
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
return true;
|
|
143
|
+
});
|
|
144
|
+
const removedCount = Math.max(mem.facts.length - keptFacts.length, entries.length - keptEntries.length);
|
|
145
|
+
if (removedCount > 0) {
|
|
146
|
+
saveMemory({ facts: keptFacts, entries: keptEntries });
|
|
147
|
+
}
|
|
148
|
+
return removedCount;
|
|
149
|
+
} catch (e: unknown) {
|
|
150
|
+
logMemoryError('clear-workspace', e);
|
|
151
|
+
return 0;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function getMemoryStats(workspaceRoot?: string): { projectFacts: number; totalFacts: number } {
|
|
156
|
+
const mem = getMemory();
|
|
157
|
+
const totalFacts = mem.facts.length;
|
|
158
|
+
if (!workspaceRoot) return { projectFacts: totalFacts, totalFacts };
|
|
159
|
+
const resolvedWs = path.resolve(workspaceRoot);
|
|
160
|
+
const targetKey = canonicalWorkspaceKey(workspaceRoot);
|
|
161
|
+
const matchingTexts = new Set<string>();
|
|
162
|
+
for (const entry of mem.entries || []) {
|
|
163
|
+
if (entry.workspace && canonicalWorkspaceKey(entry.workspace) === targetKey) {
|
|
164
|
+
matchingTexts.add(entry.text);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (const fact of mem.facts) {
|
|
168
|
+
if (fact.includes(resolvedWs) || fact.includes(targetKey)) matchingTexts.add(fact);
|
|
169
|
+
}
|
|
170
|
+
return { projectFacts: matchingTexts.size, totalFacts };
|
|
171
|
+
}
|
|
172
|
+
|
|
108
173
|
export function getMemoryContextString(options?: string | FormatMemoryOptions): string {
|
|
109
174
|
const mem = getMemory();
|
|
110
175
|
return formatMemoryContext(mem, options);
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne Agent — Per-Project & Global Personalization + Memory Governance
|
|
3
|
+
*
|
|
4
|
+
* Primary behavior is per-project (workspace-scoped) so each repository/project
|
|
5
|
+
* maintains its own high-priority personalization contract and memory policy
|
|
6
|
+
* without leaking into unrelated workspaces:
|
|
7
|
+
* - Project instructions : `<workspaceRoot>/.causyne/personalization.md`
|
|
8
|
+
* - Project settings : `<workspaceRoot>/.causyne/settings.json`
|
|
9
|
+
* - Global instructions : `~/.causyne/CAUSYNE.md`
|
|
10
|
+
* - Global memory prefs : `~/.causyne/config.json` (`memory: { enabled, allowToolAssisted }`)
|
|
11
|
+
*
|
|
12
|
+
* Security invariants:
|
|
13
|
+
* - Fixed allowlisted filenames only (no arbitrary path writes).
|
|
14
|
+
* - Refuses symlinks on both parent `.causyne` directory and target files.
|
|
15
|
+
* - Atomic file writes via `atomicWriteFileSync`.
|
|
16
|
+
* - Enforces 16 KiB single-file cap matching `projectInstructions.ts`.
|
|
17
|
+
*/
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { causyneHome } from './utils/paths.js';
|
|
21
|
+
import { loadConfig, saveConfig } from './config.js';
|
|
22
|
+
import { atomicWriteFileSync } from './infra/atomicWriter.js';
|
|
23
|
+
import { clearMemory, clearMemoryForWorkspace, getMemoryStats } from './memory.js';
|
|
24
|
+
import { episodicStore } from './memory/episodicStore.js';
|
|
25
|
+
import { loadProjectInstructions } from './projectInstructions.js';
|
|
26
|
+
import { safeDisplay } from './tui/text.js';
|
|
27
|
+
export const MAX_PERSONALIZATION_BYTES = 16_384;
|
|
28
|
+
const MAX_SETTINGS_JSON_BYTES = 64 * 1024;
|
|
29
|
+
const FORBIDDEN_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
30
|
+
const EXTERNAL_TOOL_ASSISTED_NAMES = new Set([
|
|
31
|
+
'web_search',
|
|
32
|
+
'web_fetch',
|
|
33
|
+
'browser_act',
|
|
34
|
+
'search_web',
|
|
35
|
+
'read_url_content',
|
|
36
|
+
'browser_navigate',
|
|
37
|
+
'browser_read_page',
|
|
38
|
+
'browser_interact',
|
|
39
|
+
'browser_extract',
|
|
40
|
+
'browser_screenshot',
|
|
41
|
+
'browser_tabs'
|
|
42
|
+
]);
|
|
43
|
+
function resolveHomeDir(env = process.env) {
|
|
44
|
+
const override = env.CAUSYNE_HOME && String(env.CAUSYNE_HOME).trim();
|
|
45
|
+
return override ? path.resolve(override) : causyneHome();
|
|
46
|
+
}
|
|
47
|
+
export function resolveProjectPersonalizationPath(workspaceRoot) {
|
|
48
|
+
return path.join(path.resolve(workspaceRoot), '.causyne', 'personalization.md');
|
|
49
|
+
}
|
|
50
|
+
export function resolveProjectSettingsPath(workspaceRoot) {
|
|
51
|
+
return path.join(path.resolve(workspaceRoot), '.causyne', 'settings.json');
|
|
52
|
+
}
|
|
53
|
+
export function resolveGlobalPersonalizationPath(env = process.env) {
|
|
54
|
+
return path.join(resolveHomeDir(env), 'CAUSYNE.md');
|
|
55
|
+
}
|
|
56
|
+
function assertNotSymlinkIfPresent(targetPath, expectedKind) {
|
|
57
|
+
try {
|
|
58
|
+
const stat = fs.lstatSync(targetPath);
|
|
59
|
+
if (stat.isSymbolicLink()) {
|
|
60
|
+
throw new Error(`Refusing symlink at ${safeDisplay(path.basename(targetPath))}`);
|
|
61
|
+
}
|
|
62
|
+
if (expectedKind === 'dir' && !stat.isDirectory()) {
|
|
63
|
+
throw new Error(`Expected directory at ${safeDisplay(path.basename(targetPath))}`);
|
|
64
|
+
}
|
|
65
|
+
if (expectedKind === 'file' && !stat.isFile()) {
|
|
66
|
+
throw new Error(`Expected regular file at ${safeDisplay(path.basename(targetPath))}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
const code = typeof err === 'object' && err !== null && 'code' in err ? String(err.code) : '';
|
|
71
|
+
if (code === 'ENOENT')
|
|
72
|
+
return;
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function ensureSafeDirectory(dirPath, mode) {
|
|
77
|
+
assertNotSymlinkIfPresent(dirPath, 'dir');
|
|
78
|
+
fs.mkdirSync(dirPath, { recursive: true, mode });
|
|
79
|
+
assertNotSymlinkIfPresent(dirPath, 'dir');
|
|
80
|
+
}
|
|
81
|
+
function readSafeMarkdownFile(filePath, scope, displayPath) {
|
|
82
|
+
try {
|
|
83
|
+
const stat = fs.lstatSync(filePath);
|
|
84
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
85
|
+
return {
|
|
86
|
+
scope,
|
|
87
|
+
path: filePath,
|
|
88
|
+
displayPath,
|
|
89
|
+
exists: false,
|
|
90
|
+
content: '',
|
|
91
|
+
bytes: 0,
|
|
92
|
+
maxBytes: MAX_PERSONALIZATION_BYTES
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const raw = fs.readFileSync(filePath, 'utf8').replace(/^\uFEFF/, '').replace(/\r\n/g, '\n');
|
|
96
|
+
const clamped = raw.length > MAX_PERSONALIZATION_BYTES ? raw.slice(0, MAX_PERSONALIZATION_BYTES) : raw;
|
|
97
|
+
return {
|
|
98
|
+
scope,
|
|
99
|
+
path: filePath,
|
|
100
|
+
displayPath,
|
|
101
|
+
exists: true,
|
|
102
|
+
content: clamped,
|
|
103
|
+
bytes: Buffer.byteLength(clamped, 'utf8'),
|
|
104
|
+
maxBytes: MAX_PERSONALIZATION_BYTES
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return {
|
|
109
|
+
scope,
|
|
110
|
+
path: filePath,
|
|
111
|
+
displayPath,
|
|
112
|
+
exists: false,
|
|
113
|
+
content: '',
|
|
114
|
+
bytes: 0,
|
|
115
|
+
maxBytes: MAX_PERSONALIZATION_BYTES
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function readProjectSettingsRaw(workspaceRoot) {
|
|
120
|
+
const settingsPath = resolveProjectSettingsPath(workspaceRoot);
|
|
121
|
+
try {
|
|
122
|
+
const dirPath = path.dirname(settingsPath);
|
|
123
|
+
const dirStat = fs.lstatSync(dirPath);
|
|
124
|
+
if (dirStat.isSymbolicLink() || !dirStat.isDirectory())
|
|
125
|
+
return {};
|
|
126
|
+
const stat = fs.lstatSync(settingsPath);
|
|
127
|
+
if (stat.isSymbolicLink() || !stat.isFile() || stat.size === 0 || stat.size > MAX_SETTINGS_JSON_BYTES) {
|
|
128
|
+
return {};
|
|
129
|
+
}
|
|
130
|
+
const parsed = JSON.parse(fs.readFileSync(settingsPath, 'utf8').replace(/^\uFEFF/, ''));
|
|
131
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
132
|
+
return {};
|
|
133
|
+
const clean = Object.create(null);
|
|
134
|
+
for (const [k, v] of Object.entries(parsed)) {
|
|
135
|
+
if (!FORBIDDEN_KEYS.has(k))
|
|
136
|
+
clean[k] = v;
|
|
137
|
+
}
|
|
138
|
+
return { ...clean };
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return {};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function writeProjectSettingsRaw(workspaceRoot, nextData) {
|
|
145
|
+
const settingsPath = resolveProjectSettingsPath(workspaceRoot);
|
|
146
|
+
const dirPath = path.dirname(settingsPath);
|
|
147
|
+
ensureSafeDirectory(dirPath, 0o755);
|
|
148
|
+
assertNotSymlinkIfPresent(settingsPath, 'file');
|
|
149
|
+
atomicWriteFileSync(settingsPath, `${JSON.stringify(nextData, null, 2)}\n`, { mode: 0o644 });
|
|
150
|
+
}
|
|
151
|
+
function readGlobalMemoryDefaults() {
|
|
152
|
+
const config = loadConfig();
|
|
153
|
+
const raw = config.memory && typeof config.memory === 'object' && !Array.isArray(config.memory)
|
|
154
|
+
? config.memory
|
|
155
|
+
: {};
|
|
156
|
+
return {
|
|
157
|
+
enabled: raw.enabled !== false,
|
|
158
|
+
allowToolAssisted: raw.allowToolAssisted !== false
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function readProjectMemoryOverrides(workspaceRoot) {
|
|
162
|
+
const rawSettings = readProjectSettingsRaw(workspaceRoot);
|
|
163
|
+
const rawMem = rawSettings.memory && typeof rawSettings.memory === 'object' && !Array.isArray(rawSettings.memory)
|
|
164
|
+
? rawSettings.memory
|
|
165
|
+
: {};
|
|
166
|
+
const overrides = {};
|
|
167
|
+
if (typeof rawMem.enabled === 'boolean')
|
|
168
|
+
overrides.enabled = rawMem.enabled;
|
|
169
|
+
if (typeof rawMem.allowToolAssisted === 'boolean')
|
|
170
|
+
overrides.allowToolAssisted = rawMem.allowToolAssisted;
|
|
171
|
+
return overrides;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Resolves the effective Causyne Memory policy for a workspace:
|
|
175
|
+
* per-project overrides in `<workspace>/.causyne/settings.json` take precedence
|
|
176
|
+
* over global defaults in `~/.causyne/config.json`.
|
|
177
|
+
*/
|
|
178
|
+
export function resolveEffectiveMemoryPolicy(workspaceRoot) {
|
|
179
|
+
const globalDefaults = readGlobalMemoryDefaults();
|
|
180
|
+
const projectOverrides = workspaceRoot ? readProjectMemoryOverrides(workspaceRoot) : {};
|
|
181
|
+
const hasProjectOverride = typeof projectOverrides.enabled === 'boolean' || typeof projectOverrides.allowToolAssisted === 'boolean';
|
|
182
|
+
return {
|
|
183
|
+
enabled: typeof projectOverrides.enabled === 'boolean' ? projectOverrides.enabled : globalDefaults.enabled,
|
|
184
|
+
allowToolAssisted: typeof projectOverrides.allowToolAssisted === 'boolean'
|
|
185
|
+
? projectOverrides.allowToolAssisted
|
|
186
|
+
: globalDefaults.allowToolAssisted,
|
|
187
|
+
source: hasProjectOverride ? 'project' : 'global',
|
|
188
|
+
projectOverrides,
|
|
189
|
+
globalDefaults
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Returns true if a tool invocation brings external/untrusted context
|
|
194
|
+
* (MCP servers, web search, web fetch, or live web browsing), which is gated
|
|
195
|
+
* when `allowToolAssisted` memory generation is turned off.
|
|
196
|
+
*/
|
|
197
|
+
export function isExternalToolAssistedToolName(toolName) {
|
|
198
|
+
const normalized = String(toolName || '').trim().toLowerCase();
|
|
199
|
+
if (!normalized)
|
|
200
|
+
return false;
|
|
201
|
+
if (normalized.startsWith('mcp__') || normalized.startsWith('mcp_') || normalized.startsWith('mcp:')) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
return EXTERNAL_TOOL_ASSISTED_NAMES.has(normalized);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Atomically saves project-scoped (`.causyne/personalization.md`) or global (`~/.causyne/CAUSYNE.md`)
|
|
208
|
+
* personalization instructions. Refuses symlinks and enforces the 16 KiB size ceiling.
|
|
209
|
+
*/
|
|
210
|
+
export function savePersonalizationInstructions(workspaceRoot, scope, rawContent, env = process.env) {
|
|
211
|
+
if (typeof rawContent !== 'string') {
|
|
212
|
+
throw new Error('Personalization instructions must be a string.');
|
|
213
|
+
}
|
|
214
|
+
if (rawContent.includes('\0')) {
|
|
215
|
+
throw new Error('Personalization instructions must not contain NUL bytes.');
|
|
216
|
+
}
|
|
217
|
+
const normalized = rawContent.replace(/\r\n/g, '\n');
|
|
218
|
+
const byteLen = Buffer.byteLength(normalized, 'utf8');
|
|
219
|
+
if (byteLen > MAX_PERSONALIZATION_BYTES) {
|
|
220
|
+
throw new Error(`Personalization instructions exceed maximum size (${MAX_PERSONALIZATION_BYTES} bytes).`);
|
|
221
|
+
}
|
|
222
|
+
const targetPath = scope === 'global'
|
|
223
|
+
? resolveGlobalPersonalizationPath(env)
|
|
224
|
+
: resolveProjectPersonalizationPath(workspaceRoot);
|
|
225
|
+
const displayPath = scope === 'global' ? '~/.causyne/CAUSYNE.md' : '.causyne/personalization.md';
|
|
226
|
+
const dirPath = path.dirname(targetPath);
|
|
227
|
+
if (normalized.trim().length === 0) {
|
|
228
|
+
assertNotSymlinkIfPresent(dirPath, 'dir');
|
|
229
|
+
assertNotSymlinkIfPresent(targetPath, 'file');
|
|
230
|
+
try {
|
|
231
|
+
if (fs.existsSync(targetPath))
|
|
232
|
+
fs.unlinkSync(targetPath);
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
// Ignore if absent
|
|
236
|
+
}
|
|
237
|
+
return readSafeMarkdownFile(targetPath, scope, displayPath);
|
|
238
|
+
}
|
|
239
|
+
ensureSafeDirectory(dirPath, scope === 'global' ? 0o700 : 0o755);
|
|
240
|
+
assertNotSymlinkIfPresent(targetPath, 'file');
|
|
241
|
+
const payload = normalized.endsWith('\n') ? normalized : `${normalized}\n`;
|
|
242
|
+
atomicWriteFileSync(targetPath, payload, { mode: scope === 'global' ? 0o600 : 0o644 });
|
|
243
|
+
return readSafeMarkdownFile(targetPath, scope, displayPath);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Updates memory preferences for either `'project'` (`<workspace>/.causyne/settings.json`)
|
|
247
|
+
* or `'global'` (`~/.causyne/config.json`). Passing `null` on a project field clears the
|
|
248
|
+
* per-project override so it inherits the global default.
|
|
249
|
+
*/
|
|
250
|
+
export function saveMemoryPreferences(workspaceRoot, scope, patch) {
|
|
251
|
+
if (scope === 'global') {
|
|
252
|
+
const config = loadConfig();
|
|
253
|
+
const current = readGlobalMemoryDefaults();
|
|
254
|
+
const next = {
|
|
255
|
+
enabled: typeof patch.enabled === 'boolean' ? patch.enabled : current.enabled,
|
|
256
|
+
allowToolAssisted: typeof patch.allowToolAssisted === 'boolean'
|
|
257
|
+
? patch.allowToolAssisted
|
|
258
|
+
: current.allowToolAssisted
|
|
259
|
+
};
|
|
260
|
+
config.memory = next;
|
|
261
|
+
saveConfig(config);
|
|
262
|
+
return resolveEffectiveMemoryPolicy(workspaceRoot);
|
|
263
|
+
}
|
|
264
|
+
const settings = readProjectSettingsRaw(workspaceRoot);
|
|
265
|
+
const currentMem = settings.memory && typeof settings.memory === 'object' && !Array.isArray(settings.memory)
|
|
266
|
+
? { ...settings.memory }
|
|
267
|
+
: {};
|
|
268
|
+
if (patch.enabled === null) {
|
|
269
|
+
delete currentMem.enabled;
|
|
270
|
+
}
|
|
271
|
+
else if (typeof patch.enabled === 'boolean') {
|
|
272
|
+
currentMem.enabled = patch.enabled;
|
|
273
|
+
}
|
|
274
|
+
if (patch.allowToolAssisted === null) {
|
|
275
|
+
delete currentMem.allowToolAssisted;
|
|
276
|
+
}
|
|
277
|
+
else if (typeof patch.allowToolAssisted === 'boolean') {
|
|
278
|
+
currentMem.allowToolAssisted = patch.allowToolAssisted;
|
|
279
|
+
}
|
|
280
|
+
if (Object.keys(currentMem).length === 0) {
|
|
281
|
+
delete settings.memory;
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
settings.memory = currentMem;
|
|
285
|
+
}
|
|
286
|
+
writeProjectSettingsRaw(workspaceRoot, settings);
|
|
287
|
+
return resolveEffectiveMemoryPolicy(workspaceRoot);
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Collects live memory counts across persistent facts (`memory.json`) and
|
|
291
|
+
* episodic observations/summaries (`episodic-memory.json`).
|
|
292
|
+
*/
|
|
293
|
+
export function collectMemoryStats(workspaceRoot) {
|
|
294
|
+
const factStats = getMemoryStats(workspaceRoot);
|
|
295
|
+
const epStats = episodicStore.getStats(workspaceRoot);
|
|
296
|
+
return {
|
|
297
|
+
projectFacts: factStats.projectFacts,
|
|
298
|
+
totalFacts: factStats.totalFacts,
|
|
299
|
+
projectObservations: epStats.projectObservations,
|
|
300
|
+
totalObservations: epStats.totalObservations,
|
|
301
|
+
projectSummaries: epStats.projectSummaries,
|
|
302
|
+
totalSummaries: epStats.totalSummaries
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Deletes stored Causyne memories for either the active project (`'project'`)
|
|
307
|
+
* or across all projects (`'all'`).
|
|
308
|
+
*/
|
|
309
|
+
export function deleteCausyneMemories(workspaceRoot, scope = 'project') {
|
|
310
|
+
let factsRemoved = 0;
|
|
311
|
+
let observationsRemoved = 0;
|
|
312
|
+
let summariesRemoved = 0;
|
|
313
|
+
if (scope === 'all') {
|
|
314
|
+
const beforeFacts = getMemoryStats(workspaceRoot).totalFacts;
|
|
315
|
+
clearMemory();
|
|
316
|
+
factsRemoved = beforeFacts;
|
|
317
|
+
const epCleared = episodicStore.clearAll();
|
|
318
|
+
observationsRemoved = epCleared.observationsRemoved;
|
|
319
|
+
summariesRemoved = epCleared.summariesRemoved;
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
factsRemoved = clearMemoryForWorkspace(workspaceRoot);
|
|
323
|
+
const epCleared = episodicStore.clearProject(workspaceRoot);
|
|
324
|
+
observationsRemoved = epCleared.observationsRemoved;
|
|
325
|
+
summariesRemoved = epCleared.summariesRemoved;
|
|
326
|
+
}
|
|
327
|
+
return {
|
|
328
|
+
scope,
|
|
329
|
+
factsRemoved,
|
|
330
|
+
observationsRemoved,
|
|
331
|
+
summariesRemoved,
|
|
332
|
+
memoryStats: collectMemoryStats(workspaceRoot)
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Returns the full Personalization + Memory state for Web Studio GUI and CLI inspection.
|
|
337
|
+
*/
|
|
338
|
+
export function readPersonalizationOverview(workspaceRoot, env = process.env) {
|
|
339
|
+
const resolvedWs = path.resolve(workspaceRoot);
|
|
340
|
+
const projectDoc = readSafeMarkdownFile(resolveProjectPersonalizationPath(resolvedWs), 'project', '.causyne/personalization.md');
|
|
341
|
+
const globalDoc = readSafeMarkdownFile(resolveGlobalPersonalizationPath(env), 'global', '~/.causyne/CAUSYNE.md');
|
|
342
|
+
const loaded = loadProjectInstructions(resolvedWs, env, { includeUnmatchedScoped: true });
|
|
343
|
+
const activeRules = loaded.entries.map((entry) => {
|
|
344
|
+
const rel = path.relative(loaded.workspaceRoot, entry.sourcePath);
|
|
345
|
+
const label = entry.scope === 'global'
|
|
346
|
+
? `~/.causyne/${path.basename(entry.sourcePath)}`
|
|
347
|
+
: (rel && !rel.startsWith('..') ? rel.replace(/\\/g, '/') : entry.sourcePath);
|
|
348
|
+
return {
|
|
349
|
+
scope: entry.scope,
|
|
350
|
+
label,
|
|
351
|
+
sourcePath: entry.sourcePath,
|
|
352
|
+
bytes: entry.bytes,
|
|
353
|
+
truncated: entry.truncated,
|
|
354
|
+
...(entry.globs && entry.globs.length > 0 ? { globs: entry.globs } : {})
|
|
355
|
+
};
|
|
356
|
+
});
|
|
357
|
+
return {
|
|
358
|
+
workspaceRoot: resolvedWs,
|
|
359
|
+
project: projectDoc,
|
|
360
|
+
global: globalDoc,
|
|
361
|
+
memory: resolveEffectiveMemoryPolicy(resolvedWs),
|
|
362
|
+
memoryStats: collectMemoryStats(resolvedWs),
|
|
363
|
+
activeRules
|
|
364
|
+
};
|
|
365
|
+
}
|