memax-cli 0.1.0-alpha.4 → 0.1.0-alpha.41
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/assets/skills/memax-memory/SKILL.md +154 -0
- package/dist/commands/agent-sessions.d.ts +62 -0
- package/dist/commands/agent-sessions.d.ts.map +1 -0
- package/dist/commands/agent-sessions.js +1338 -0
- package/dist/commands/agent-sessions.js.map +1 -0
- package/dist/commands/agent-sessions.test.d.ts +2 -0
- package/dist/commands/agent-sessions.test.d.ts.map +1 -0
- package/dist/commands/agent-sessions.test.js +255 -0
- package/dist/commands/agent-sessions.test.js.map +1 -0
- package/dist/commands/agents.d.ts +3 -0
- package/dist/commands/agents.d.ts.map +1 -0
- package/dist/commands/agents.js +36 -0
- package/dist/commands/agents.js.map +1 -0
- package/dist/commands/ask.d.ts +13 -0
- package/dist/commands/ask.d.ts.map +1 -0
- package/dist/commands/ask.js +459 -0
- package/dist/commands/ask.js.map +1 -0
- package/dist/commands/auth.d.ts +3 -0
- package/dist/commands/auth.d.ts.map +1 -1
- package/dist/commands/auth.js +38 -7
- package/dist/commands/auth.js.map +1 -1
- package/dist/commands/capture.d.ts +19 -0
- package/dist/commands/capture.d.ts.map +1 -0
- package/dist/commands/capture.js +69 -0
- package/dist/commands/capture.js.map +1 -0
- package/dist/commands/config.d.ts +2 -0
- package/dist/commands/config.d.ts.map +1 -1
- package/dist/commands/config.js +13 -0
- package/dist/commands/config.js.map +1 -1
- package/dist/commands/delete.d.ts +3 -1
- package/dist/commands/delete.d.ts.map +1 -1
- package/dist/commands/delete.js +30 -5
- package/dist/commands/delete.js.map +1 -1
- package/dist/commands/dreams.d.ts +22 -0
- package/dist/commands/dreams.d.ts.map +1 -0
- package/dist/commands/dreams.js +205 -0
- package/dist/commands/dreams.js.map +1 -0
- package/dist/commands/dreams.test.d.ts +2 -0
- package/dist/commands/dreams.test.d.ts.map +1 -0
- package/dist/commands/dreams.test.js +39 -0
- package/dist/commands/dreams.test.js.map +1 -0
- package/dist/commands/hook.d.ts +2 -0
- package/dist/commands/hook.d.ts.map +1 -1
- package/dist/commands/hook.js +6 -0
- package/dist/commands/hook.js.map +1 -1
- package/dist/commands/hub.d.ts +31 -0
- package/dist/commands/hub.d.ts.map +1 -0
- package/dist/commands/hub.js +295 -0
- package/dist/commands/hub.js.map +1 -0
- package/dist/commands/hub.test.d.ts +2 -0
- package/dist/commands/hub.test.d.ts.map +1 -0
- package/dist/commands/hub.test.js +62 -0
- package/dist/commands/hub.test.js.map +1 -0
- package/dist/commands/list.d.ts +8 -1
- package/dist/commands/list.d.ts.map +1 -1
- package/dist/commands/list.js +110 -9
- package/dist/commands/list.js.map +1 -1
- package/dist/commands/list.test.d.ts +2 -0
- package/dist/commands/list.test.d.ts.map +1 -0
- package/dist/commands/list.test.js +14 -0
- package/dist/commands/list.test.js.map +1 -0
- package/dist/commands/login.d.ts +2 -0
- package/dist/commands/login.d.ts.map +1 -1
- package/dist/commands/login.js +50 -19
- package/dist/commands/login.js.map +1 -1
- package/dist/commands/mcp.d.ts.map +1 -1
- package/dist/commands/mcp.js +285 -42
- package/dist/commands/mcp.js.map +1 -1
- package/dist/commands/push.d.ts +6 -1
- package/dist/commands/push.d.ts.map +1 -1
- package/dist/commands/push.js +73 -11
- package/dist/commands/push.js.map +1 -1
- package/dist/commands/recall.d.ts +9 -0
- package/dist/commands/recall.d.ts.map +1 -1
- package/dist/commands/recall.js +206 -39
- package/dist/commands/recall.js.map +1 -1
- package/dist/commands/recall.test.d.ts +2 -0
- package/dist/commands/recall.test.d.ts.map +1 -0
- package/dist/commands/recall.test.js +31 -0
- package/dist/commands/recall.test.js.map +1 -0
- package/dist/commands/setup-hooks.d.ts +12 -0
- package/dist/commands/setup-hooks.d.ts.map +1 -0
- package/dist/commands/setup-hooks.js +193 -0
- package/dist/commands/setup-hooks.js.map +1 -0
- package/dist/commands/setup-instructions.d.ts +21 -0
- package/dist/commands/setup-instructions.d.ts.map +1 -0
- package/dist/commands/setup-instructions.js +172 -0
- package/dist/commands/setup-instructions.js.map +1 -0
- package/dist/commands/setup-mcp.d.ts +14 -0
- package/dist/commands/setup-mcp.d.ts.map +1 -0
- package/dist/commands/setup-mcp.js +288 -0
- package/dist/commands/setup-mcp.js.map +1 -0
- package/dist/commands/setup-types.d.ts +20 -0
- package/dist/commands/setup-types.d.ts.map +1 -0
- package/dist/commands/setup-types.js +60 -0
- package/dist/commands/setup-types.js.map +1 -0
- package/dist/commands/setup.d.ts +8 -1
- package/dist/commands/setup.d.ts.map +1 -1
- package/dist/commands/setup.js +163 -307
- package/dist/commands/setup.js.map +1 -1
- package/dist/commands/show.d.ts +2 -0
- package/dist/commands/show.d.ts.map +1 -1
- package/dist/commands/show.js +25 -13
- package/dist/commands/show.js.map +1 -1
- package/dist/commands/sync.d.ts +47 -2
- package/dist/commands/sync.d.ts.map +1 -1
- package/dist/commands/sync.js +1299 -119
- package/dist/commands/sync.js.map +1 -1
- package/dist/commands/sync.test.d.ts +2 -0
- package/dist/commands/sync.test.d.ts.map +1 -0
- package/dist/commands/sync.test.js +130 -0
- package/dist/commands/sync.test.js.map +1 -0
- package/dist/commands/topic.d.ts +32 -0
- package/dist/commands/topic.d.ts.map +1 -0
- package/dist/commands/topic.js +223 -0
- package/dist/commands/topic.js.map +1 -0
- package/dist/commands/topic.test.d.ts +2 -0
- package/dist/commands/topic.test.d.ts.map +1 -0
- package/dist/commands/topic.test.js +114 -0
- package/dist/commands/topic.test.js.map +1 -0
- package/dist/index.js +35 -125
- package/dist/index.js.map +1 -1
- package/dist/lib/client.d.ts +9 -0
- package/dist/lib/client.d.ts.map +1 -0
- package/dist/lib/client.js +77 -0
- package/dist/lib/client.js.map +1 -0
- package/dist/lib/config.d.ts +45 -0
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +74 -1
- package/dist/lib/config.js.map +1 -1
- package/dist/lib/hubs.d.ts +7 -0
- package/dist/lib/hubs.d.ts.map +1 -0
- package/dist/lib/hubs.js +33 -0
- package/dist/lib/hubs.js.map +1 -0
- package/dist/lib/hubs.test.d.ts +2 -0
- package/dist/lib/hubs.test.d.ts.map +1 -0
- package/dist/lib/hubs.test.js +58 -0
- package/dist/lib/hubs.test.js.map +1 -0
- package/dist/lib/project-context.d.ts +56 -0
- package/dist/lib/project-context.d.ts.map +1 -0
- package/dist/lib/project-context.js +231 -0
- package/dist/lib/project-context.js.map +1 -0
- package/dist/lib/project-context.test.d.ts +2 -0
- package/dist/lib/project-context.test.d.ts.map +1 -0
- package/dist/lib/project-context.test.js +75 -0
- package/dist/lib/project-context.test.js.map +1 -0
- package/dist/lib/prompt.d.ts +7 -0
- package/dist/lib/prompt.d.ts.map +1 -0
- package/dist/lib/prompt.js +41 -0
- package/dist/lib/prompt.js.map +1 -0
- package/dist/lib/trash.d.ts +6 -0
- package/dist/lib/trash.d.ts.map +1 -0
- package/dist/lib/trash.js +28 -0
- package/dist/lib/trash.js.map +1 -0
- package/package.json +17 -13
- package/.vscode/mcp.json +0 -8
- package/dist/lib/api.d.ts +0 -4
- package/dist/lib/api.d.ts.map +0 -1
- package/dist/lib/api.js +0 -95
- package/dist/lib/api.js.map +0 -1
- package/src/commands/auth.ts +0 -92
- package/src/commands/config.ts +0 -27
- package/src/commands/delete.ts +0 -20
- package/src/commands/hook.ts +0 -243
- package/src/commands/list.ts +0 -38
- package/src/commands/login.ts +0 -162
- package/src/commands/mcp.ts +0 -357
- package/src/commands/push.ts +0 -82
- package/src/commands/recall.ts +0 -163
- package/src/commands/setup.ts +0 -682
- package/src/commands/show.ts +0 -35
- package/src/commands/sync.ts +0 -403
- package/src/index.ts +0 -192
- package/src/lib/api.ts +0 -110
- package/src/lib/config.ts +0 -61
- package/src/lib/credentials.ts +0 -42
- package/tsconfig.json +0 -9
package/dist/commands/sync.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { readFileSync, writeFileSync, mkdirSync, readdirSync, statSync, watch, existsSync, } from "node:fs";
|
|
4
|
+
import { join, relative, extname, resolve, dirname } from "node:path";
|
|
4
5
|
import { homedir } from "node:os";
|
|
5
|
-
import {
|
|
6
|
+
import { getClient } from "../lib/client.js";
|
|
7
|
+
import { getProjectScope, resolveProjectScope, resolveClaudeProjectFolder, normalizeFilePath, readMemaxYmlConfig, detectProjectContext, isCanonicalProjectScope, } from "../lib/project-context.js";
|
|
8
|
+
import { getOrCreateDeviceID, listSyncSources, updateSyncSourceRun, } from "../lib/config.js";
|
|
9
|
+
import { confirm, ask, confirmDefault } from "../lib/prompt.js";
|
|
10
|
+
import { moveFileToTrash } from "../lib/trash.js";
|
|
11
|
+
import { syncAgentSessionsCommand } from "./agent-sessions.js";
|
|
6
12
|
const DEFAULT_IGNORE = new Set([
|
|
7
13
|
"node_modules",
|
|
8
14
|
".git",
|
|
@@ -37,47 +43,568 @@ const SUPPORTED_EXTENSIONS = new Set([
|
|
|
37
43
|
".proto",
|
|
38
44
|
".dockerfile",
|
|
39
45
|
]);
|
|
40
|
-
export async function syncAgentMemoryCommand() {
|
|
41
|
-
await syncAgentMemory();
|
|
46
|
+
export async function syncAgentMemoryCommand(options = {}) {
|
|
47
|
+
await syncAgentMemory(options);
|
|
42
48
|
}
|
|
43
|
-
export async function
|
|
44
|
-
|
|
45
|
-
|
|
49
|
+
export async function listAgentConfigsCommand() {
|
|
50
|
+
let configs;
|
|
51
|
+
try {
|
|
52
|
+
const result = await getClient().configs.list();
|
|
53
|
+
configs = result.configs;
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
console.error(chalk.red(` Failed to fetch configs: ${err.message}\n`));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (!configs || configs.length === 0) {
|
|
60
|
+
console.log(chalk.yellow(" No synced configs. Run: memax agents configs sync\n"));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// Group by agent
|
|
64
|
+
const byAgent = new Map();
|
|
65
|
+
for (const c of configs) {
|
|
66
|
+
const list = byAgent.get(c.agent) ?? [];
|
|
67
|
+
list.push({
|
|
68
|
+
id: c.id,
|
|
69
|
+
filePath: c.file_path,
|
|
70
|
+
scope: c.scope,
|
|
71
|
+
updatedAt: c.updated_at,
|
|
72
|
+
});
|
|
73
|
+
byAgent.set(c.agent, list);
|
|
74
|
+
}
|
|
75
|
+
console.log();
|
|
76
|
+
for (const [agent, files] of byAgent) {
|
|
77
|
+
console.log(` ${chalk.cyan(agent)}`);
|
|
78
|
+
for (const f of files) {
|
|
79
|
+
const scopeTag = f.scope === "global"
|
|
80
|
+
? chalk.dim("global")
|
|
81
|
+
: chalk.dim(f.scope.replace("project:", ""));
|
|
82
|
+
const age = formatAge(f.updatedAt);
|
|
83
|
+
console.log(` ${f.filePath} ${scopeTag} ${chalk.dim(age)} ${chalk.dim(f.id.slice(0, 8))}`);
|
|
84
|
+
}
|
|
85
|
+
console.log();
|
|
86
|
+
}
|
|
87
|
+
console.log(chalk.gray(` ${configs.length} config${configs.length > 1 ? "s" : ""} synced to cloud.\n`));
|
|
88
|
+
}
|
|
89
|
+
export async function listDeletedAgentConfigsCommand() {
|
|
90
|
+
let deleted;
|
|
91
|
+
try {
|
|
92
|
+
deleted = await getClient().configs.listDeleted();
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
console.error(chalk.red(` Failed to fetch deleted configs: ${err.message}\n`));
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (!deleted.configs || deleted.configs.length === 0) {
|
|
99
|
+
console.log(chalk.gray(" No recoverable deleted configs.\n"));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
console.log(chalk.bold("\n Recoverable Deleted Configs\n"));
|
|
103
|
+
deleted.configs.forEach((item, index) => {
|
|
104
|
+
const scopeLabel = item.scope === "global"
|
|
105
|
+
? chalk.dim("global")
|
|
106
|
+
: chalk.dim(item.scope.replace("project:", ""));
|
|
107
|
+
console.log(` ${chalk.bold(String(index + 1).padStart(2, " "))}. ${chalk.cyan(item.agent)} ${item.file_path} ${scopeLabel}`);
|
|
108
|
+
console.log(chalk.gray(` deleted ${formatAge(item.deleted_at)} · recoverable until ${item.content_expires_at ? new Date(item.content_expires_at).toLocaleString() : "expired"}`));
|
|
109
|
+
});
|
|
110
|
+
console.log();
|
|
111
|
+
}
|
|
112
|
+
export async function restoreDeletedAgentConfigsCommand() {
|
|
113
|
+
let deleted;
|
|
114
|
+
try {
|
|
115
|
+
deleted = await getClient().configs.listDeleted();
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
console.error(chalk.red(` Failed to fetch deleted configs: ${err.message}\n`));
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (!deleted.configs || deleted.configs.length === 0) {
|
|
122
|
+
console.log(chalk.gray(" No recoverable deleted configs.\n"));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
console.log(chalk.bold("\n Recover Deleted Configs\n"));
|
|
126
|
+
deleted.configs.forEach((item, index) => {
|
|
127
|
+
const scopeLabel = item.scope === "global" ? "global" : item.scope.replace("project:", "");
|
|
128
|
+
console.log(` ${index + 1}. ${item.agent}/${item.file_path} ${chalk.gray(scopeLabel)}`);
|
|
129
|
+
});
|
|
130
|
+
const raw = await ask(chalk.gray("\n Enter indexes to restore (comma-separated): "));
|
|
131
|
+
const indexes = raw
|
|
132
|
+
.split(",")
|
|
133
|
+
.map((value) => Number.parseInt(value.trim(), 10))
|
|
134
|
+
.filter((value) => Number.isInteger(value) && value > 0 && value <= deleted.configs.length);
|
|
135
|
+
if (indexes.length === 0) {
|
|
136
|
+
console.log(chalk.gray(" Cancelled.\n"));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const cwd = process.cwd();
|
|
140
|
+
const deviceID = getOrCreateDeviceID();
|
|
141
|
+
const currentProjectScope = getProjectScope(cwd);
|
|
142
|
+
let restored = 0;
|
|
143
|
+
for (const index of indexes) {
|
|
144
|
+
const item = deleted.configs[index - 1];
|
|
145
|
+
try {
|
|
146
|
+
const writePath = resolveAgentConfigWritePath(item.agent, item.file_path, item.scope, {
|
|
147
|
+
cwd,
|
|
148
|
+
home: homedir(),
|
|
149
|
+
currentProjectScope,
|
|
150
|
+
findClaudeProjectDir,
|
|
151
|
+
});
|
|
152
|
+
const config = await getClient().configs.restore({
|
|
153
|
+
agent: item.agent,
|
|
154
|
+
file_path: item.file_path,
|
|
155
|
+
scope: item.scope,
|
|
156
|
+
device_id: deviceID,
|
|
157
|
+
local_path: writePath ?? undefined,
|
|
158
|
+
});
|
|
159
|
+
if (writePath && !existsSync(writePath)) {
|
|
160
|
+
mkdirSync(dirname(writePath), { recursive: true });
|
|
161
|
+
writeFileSync(writePath, config.content);
|
|
162
|
+
await getClient().configs.ack({
|
|
163
|
+
device_id: deviceID,
|
|
164
|
+
configs: [
|
|
165
|
+
{
|
|
166
|
+
agent: item.agent,
|
|
167
|
+
file_path: item.file_path,
|
|
168
|
+
scope: item.scope,
|
|
169
|
+
content_hash: config.content_hash,
|
|
170
|
+
version: config.version,
|
|
171
|
+
local_path: writePath,
|
|
172
|
+
},
|
|
173
|
+
],
|
|
174
|
+
});
|
|
175
|
+
console.log(chalk.green(` ✓ ${item.file_path}`), chalk.gray("restored to cloud and local machine"));
|
|
176
|
+
}
|
|
177
|
+
else if (writePath && existsSync(writePath)) {
|
|
178
|
+
console.log(chalk.yellow(` - ${item.file_path}`), chalk.gray("restored to cloud; local file already exists"));
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
console.log(chalk.yellow(` - ${item.file_path}`), chalk.gray("restored to cloud; no safe local path on this machine"));
|
|
182
|
+
}
|
|
183
|
+
restored++;
|
|
184
|
+
}
|
|
185
|
+
catch (err) {
|
|
186
|
+
console.log(chalk.red(` ✗ ${item.file_path}`), chalk.gray(err.message));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
console.log(chalk.gray(`\n ${restored} config${restored === 1 ? "" : "s"} restored.\n`));
|
|
190
|
+
}
|
|
191
|
+
export function classifyAgentConfigPlacement(agent, filePath, scope, options = {}) {
|
|
192
|
+
const key = `${agent}|${normalizeFilePath(filePath)}|${scope}`;
|
|
193
|
+
const existing = options.localByKey?.get(key);
|
|
194
|
+
if (existing) {
|
|
195
|
+
return {
|
|
196
|
+
kind: "present",
|
|
197
|
+
path: existing.path,
|
|
198
|
+
reason: "present locally",
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
const cwd = options.cwd ?? process.cwd();
|
|
202
|
+
const currentProjectScope = options.currentProjectScope ?? getProjectScope(cwd);
|
|
203
|
+
if (scope.startsWith("project:") && scope !== currentProjectScope) {
|
|
204
|
+
return {
|
|
205
|
+
kind: "different_project",
|
|
206
|
+
reason: `belongs to ${scope.replace(/^project:/, "")}`,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
const path = resolveAgentConfigWritePath(agent, filePath, scope, options);
|
|
210
|
+
if (path) {
|
|
211
|
+
return {
|
|
212
|
+
kind: "restorable",
|
|
213
|
+
path,
|
|
214
|
+
reason: "safe restore path available",
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
kind: "unresolved",
|
|
219
|
+
reason: "no safe restore path on this machine",
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
export async function doctorAgentConfigsCommand() {
|
|
223
|
+
const cwd = process.cwd();
|
|
224
|
+
const home = homedir();
|
|
225
|
+
const deviceID = getOrCreateDeviceID();
|
|
226
|
+
const project = resolveProjectScope(cwd);
|
|
227
|
+
const memaxYml = readMemaxYmlConfig(cwd);
|
|
228
|
+
const locations = discoverAgentConfigs();
|
|
229
|
+
const localConfigs = locations.filter((loc) => {
|
|
230
|
+
if (!existsSync(loc.path))
|
|
231
|
+
return false;
|
|
232
|
+
try {
|
|
233
|
+
const stat = statSync(loc.path);
|
|
234
|
+
return stat.isFile() && stat.size > 0;
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
const localByKey = new Map();
|
|
241
|
+
for (const loc of localConfigs) {
|
|
242
|
+
localByKey.set(`${loc.agent}|${loc.filePath}|${loc.scope}`, loc);
|
|
243
|
+
}
|
|
244
|
+
let cloudConfigs = null;
|
|
245
|
+
try {
|
|
246
|
+
cloudConfigs = await getClient().configs.list();
|
|
247
|
+
}
|
|
248
|
+
catch (err) {
|
|
249
|
+
console.error(chalk.red(` Failed to fetch cloud configs: ${err.message}\n`));
|
|
250
|
+
}
|
|
251
|
+
const scopeSource = project.source === "memax_yml"
|
|
252
|
+
? ".memax.yml project_id"
|
|
253
|
+
: project.source === "git_remote"
|
|
254
|
+
? "git origin"
|
|
255
|
+
: "no canonical project identity";
|
|
256
|
+
console.log(chalk.bold("\n Memax Agent Config Doctor\n"));
|
|
257
|
+
console.log(chalk.white(" Device"));
|
|
258
|
+
console.log(` ID ${chalk.bold(deviceID)}`);
|
|
259
|
+
console.log(` Home ${chalk.gray(home)}`);
|
|
260
|
+
console.log();
|
|
261
|
+
console.log(chalk.white(" Project"));
|
|
262
|
+
console.log(` CWD ${chalk.gray(cwd)}`);
|
|
263
|
+
console.log(` Scope ${chalk.bold(project.scope)}`);
|
|
264
|
+
console.log(` Source ${chalk.gray(scopeSource)}`);
|
|
265
|
+
if (memaxYml?.hub) {
|
|
266
|
+
console.log(` Hub ${chalk.gray(memaxYml.hub)}`);
|
|
267
|
+
}
|
|
268
|
+
if (memaxYml?.project_id) {
|
|
269
|
+
console.log(` project_id ${chalk.gray(memaxYml.project_id)}`);
|
|
270
|
+
}
|
|
271
|
+
if (project.warning) {
|
|
272
|
+
console.log(` Warning ${chalk.yellow(project.warning)}`);
|
|
273
|
+
}
|
|
274
|
+
if (project.scope === "project") {
|
|
275
|
+
console.log(` Note ${chalk.yellow("project-scoped cross-device restore is disabled until git origin or .memax.yml project_id is available")}`);
|
|
276
|
+
}
|
|
277
|
+
console.log();
|
|
278
|
+
const byAgent = new Map();
|
|
279
|
+
for (const loc of localConfigs) {
|
|
280
|
+
const group = byAgent.get(loc.agent) ?? [];
|
|
281
|
+
group.push(loc);
|
|
282
|
+
byAgent.set(loc.agent, group);
|
|
283
|
+
}
|
|
284
|
+
console.log(chalk.white(" Local Discovery"));
|
|
285
|
+
if (localConfigs.length === 0) {
|
|
286
|
+
console.log(` ${chalk.gray("No local agent configs discovered.")}`);
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
for (const [agent, group] of byAgent) {
|
|
290
|
+
console.log(` ${chalk.cyan(formatAgentName(agent))}`);
|
|
291
|
+
for (const loc of group) {
|
|
292
|
+
const scopeLabel = loc.scope === "global"
|
|
293
|
+
? "global"
|
|
294
|
+
: loc.scope.replace(/^project:/, "");
|
|
295
|
+
console.log(` • ${loc.filePath} ${chalk.gray(scopeLabel)} ${chalk.gray(loc.path)}`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
console.log();
|
|
300
|
+
if (!cloudConfigs) {
|
|
301
|
+
console.log(chalk.gray(" Cloud inspection skipped because cloud config fetch failed.\n"));
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
const placements = cloudConfigs.configs.map((config) => ({
|
|
305
|
+
config,
|
|
306
|
+
placement: classifyAgentConfigPlacement(config.agent, config.file_path, config.scope, {
|
|
307
|
+
cwd,
|
|
308
|
+
home,
|
|
309
|
+
currentProjectScope: project.scope,
|
|
310
|
+
localByKey,
|
|
311
|
+
findClaudeProjectDir,
|
|
312
|
+
}),
|
|
313
|
+
}));
|
|
314
|
+
const present = placements.filter((p) => p.placement.kind === "present");
|
|
315
|
+
const restorable = placements.filter((p) => p.placement.kind === "restorable");
|
|
316
|
+
const differentProject = placements.filter((p) => p.placement.kind === "different_project");
|
|
317
|
+
const unresolved = placements.filter((p) => p.placement.kind === "unresolved");
|
|
318
|
+
console.log(chalk.white(" Cloud Coverage"));
|
|
319
|
+
console.log(` Present locally ${chalk.bold(String(present.length))}`);
|
|
320
|
+
console.log(` Restorable here ${chalk.bold(String(restorable.length))}`);
|
|
321
|
+
console.log(` Other project ${chalk.bold(String(differentProject.length))}`);
|
|
322
|
+
console.log(` Unresolved here ${chalk.bold(String(unresolved.length))}`);
|
|
323
|
+
console.log();
|
|
324
|
+
printPlacementSection(" Restorable Here", chalk.cyan, restorable, (item) => [
|
|
325
|
+
item.config.agent,
|
|
326
|
+
item.config.file_path,
|
|
327
|
+
item.placement.path ?? "",
|
|
328
|
+
]);
|
|
329
|
+
printPlacementSection(" Different Project", chalk.yellow, differentProject, (item) => [item.config.agent, item.config.file_path, item.placement.reason]);
|
|
330
|
+
printPlacementSection(" Unresolved", chalk.magenta, unresolved, (item) => [
|
|
331
|
+
item.config.agent,
|
|
332
|
+
item.config.file_path,
|
|
333
|
+
item.placement.reason,
|
|
334
|
+
]);
|
|
335
|
+
console.log(chalk.gray(" Use this command to verify what sync can restore safely on this machine.\n"));
|
|
336
|
+
}
|
|
337
|
+
export async function syncAgentsCommand(options) {
|
|
338
|
+
await syncAgentMemoryCommand(options);
|
|
339
|
+
await syncAgentSessionsCommand(options);
|
|
340
|
+
}
|
|
341
|
+
export function registerSyncCommands(program) {
|
|
342
|
+
const syncCmd = program
|
|
343
|
+
.command("sync [directory]")
|
|
344
|
+
.description("Sync a directory or agent configs to your Memax workspace")
|
|
345
|
+
.option("-w, --watch", "Watch for changes (coming soon)")
|
|
346
|
+
.option("-b, --boundary <level>", "Visibility level: private, team, org", "private")
|
|
347
|
+
.option("-c, --category <category>", "Default category (auto-detected if omitted)")
|
|
348
|
+
.option("--ignore <patterns>", "Comma-separated directories to ignore")
|
|
349
|
+
.option("-y, --yes", "Skip confirmation for large syncs")
|
|
350
|
+
.action(syncCommand);
|
|
351
|
+
syncCmd
|
|
352
|
+
.command("status")
|
|
353
|
+
.description("Show registered memory sync sources and last run status")
|
|
354
|
+
.action(syncStatusCommand);
|
|
355
|
+
syncCmd
|
|
356
|
+
.command("agents")
|
|
357
|
+
.description("Sync agent configs and sessions (alias for: memax agents sync)")
|
|
358
|
+
.option("--push", "Force push local configs to cloud (overwrite)")
|
|
359
|
+
.option("--pull", "Force pull cloud configs to local (overwrite)")
|
|
360
|
+
.action(syncAgentsCommand);
|
|
361
|
+
}
|
|
362
|
+
export function registerAgentConfigCommands(agentsCmd) {
|
|
363
|
+
const agentConfigsCmd = agentsCmd
|
|
364
|
+
.command("configs")
|
|
365
|
+
.description("Manage synced agent config files");
|
|
366
|
+
agentConfigsCmd
|
|
367
|
+
.command("sync")
|
|
368
|
+
.description("Sync agent config files bidirectionally with Memax cloud")
|
|
369
|
+
.option("--push", "Force push local configs to cloud (overwrite)")
|
|
370
|
+
.option("--pull", "Force pull cloud configs to local (overwrite)")
|
|
371
|
+
.action(syncAgentMemoryCommand);
|
|
372
|
+
agentConfigsCmd
|
|
373
|
+
.command("list")
|
|
374
|
+
.description("List all synced agent configs in the cloud")
|
|
375
|
+
.action(listAgentConfigsCommand);
|
|
376
|
+
agentConfigsCmd
|
|
377
|
+
.command("deleted")
|
|
378
|
+
.description("List recoverable deleted configs retained in cloud")
|
|
379
|
+
.action(listDeletedAgentConfigsCommand);
|
|
380
|
+
agentConfigsCmd
|
|
381
|
+
.command("restore")
|
|
382
|
+
.description("Restore deleted configs retained in cloud")
|
|
383
|
+
.action(restoreDeletedAgentConfigsCommand);
|
|
384
|
+
agentConfigsCmd
|
|
385
|
+
.command("delete")
|
|
386
|
+
.description("Interactively select and delete synced configs")
|
|
387
|
+
.action(deleteAgentConfigsCommand);
|
|
388
|
+
agentConfigsCmd
|
|
389
|
+
.command("doctor")
|
|
390
|
+
.description("Explain config sync identity, discovery, and safe restore behavior on this machine")
|
|
391
|
+
.action(doctorAgentConfigsCommand);
|
|
392
|
+
}
|
|
393
|
+
export async function deleteAgentConfigsCommand() {
|
|
394
|
+
let configs;
|
|
395
|
+
try {
|
|
396
|
+
const result = await getClient().configs.list();
|
|
397
|
+
configs = result.configs;
|
|
398
|
+
}
|
|
399
|
+
catch (err) {
|
|
400
|
+
console.error(chalk.red(` Failed to fetch configs: ${err.message}\n`));
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (!configs || configs.length === 0) {
|
|
404
|
+
console.log(chalk.yellow(" No synced configs to delete.\n"));
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
// Display numbered list grouped by agent
|
|
408
|
+
const items = [];
|
|
409
|
+
let currentAgent = "";
|
|
410
|
+
for (const c of configs) {
|
|
411
|
+
if (c.agent !== currentAgent) {
|
|
412
|
+
if (currentAgent)
|
|
413
|
+
console.log();
|
|
414
|
+
console.log(` ${chalk.cyan(c.agent)}`);
|
|
415
|
+
currentAgent = c.agent;
|
|
416
|
+
}
|
|
417
|
+
items.push({
|
|
418
|
+
id: c.id,
|
|
419
|
+
agent: c.agent,
|
|
420
|
+
filePath: c.file_path,
|
|
421
|
+
scope: c.scope,
|
|
422
|
+
version: c.version,
|
|
423
|
+
});
|
|
424
|
+
const idx = chalk.dim(`${items.length}.`);
|
|
425
|
+
const scopeTag = c.scope === "global"
|
|
426
|
+
? chalk.dim("global")
|
|
427
|
+
: chalk.dim(c.scope.replace("project:", ""));
|
|
428
|
+
console.log(` ${idx} ${c.file_path} ${scopeTag}`);
|
|
429
|
+
}
|
|
430
|
+
console.log();
|
|
431
|
+
const answer = await ask(" Select configs to delete (comma-separated numbers, or 'q' to quit): ");
|
|
432
|
+
if (!answer || answer.trim().toLowerCase() === "q") {
|
|
433
|
+
console.log(chalk.gray(" Cancelled.\n"));
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
const indices = answer
|
|
437
|
+
.split(",")
|
|
438
|
+
.map((s) => parseInt(s.trim(), 10))
|
|
439
|
+
.filter((n) => !isNaN(n) && n >= 1 && n <= items.length);
|
|
440
|
+
if (indices.length === 0) {
|
|
441
|
+
console.log(chalk.gray(" No valid selections.\n"));
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const modeAnswer = await ask(" Delete from [l] this device only, [e] everywhere, or [s] skip? ");
|
|
445
|
+
const mode = modeAnswer.trim().toLowerCase();
|
|
446
|
+
if (mode !== "l" && mode !== "e") {
|
|
447
|
+
console.log(chalk.gray(" Cancelled.\n"));
|
|
46
448
|
return;
|
|
47
449
|
}
|
|
450
|
+
console.log();
|
|
451
|
+
for (const i of indices) {
|
|
452
|
+
const item = items[i - 1];
|
|
453
|
+
console.log(chalk.yellow(` ${item.agent}/${item.filePath}`));
|
|
454
|
+
}
|
|
455
|
+
const ok = await confirm(mode === "e"
|
|
456
|
+
? `\n Delete ${indices.length} config${indices.length > 1 ? "s" : ""} everywhere? (y/N) `
|
|
457
|
+
: `\n Remove ${indices.length} config${indices.length > 1 ? "s" : ""} from this device only? (y/N) `);
|
|
458
|
+
if (!ok) {
|
|
459
|
+
console.log(chalk.gray(" Cancelled.\n"));
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
const deviceID = getOrCreateDeviceID();
|
|
463
|
+
const cwd = process.cwd();
|
|
464
|
+
const currentProjectScope = getProjectScope(cwd);
|
|
465
|
+
const locations = discoverAgentConfigs();
|
|
466
|
+
const locByKey = new Map();
|
|
467
|
+
for (const loc of locations) {
|
|
468
|
+
locByKey.set(`${loc.agent}|${loc.filePath}|${loc.scope}`, loc);
|
|
469
|
+
}
|
|
470
|
+
const resolveDeletePath = (agent, filePath, scope) => {
|
|
471
|
+
const loc = locByKey.get(`${agent}|${filePath}|${scope}`);
|
|
472
|
+
if (loc)
|
|
473
|
+
return loc.path;
|
|
474
|
+
return resolveAgentConfigWritePath(agent, filePath, scope, {
|
|
475
|
+
cwd,
|
|
476
|
+
home: homedir(),
|
|
477
|
+
currentProjectScope,
|
|
478
|
+
});
|
|
479
|
+
};
|
|
480
|
+
let deleted = 0;
|
|
481
|
+
for (const i of indices) {
|
|
482
|
+
const item = items[i - 1];
|
|
483
|
+
try {
|
|
484
|
+
const localPath = resolveDeletePath(item.agent, item.filePath, item.scope);
|
|
485
|
+
if (mode === "l") {
|
|
486
|
+
await getClient().configs.localDelete({
|
|
487
|
+
device_id: deviceID,
|
|
488
|
+
agent: item.agent,
|
|
489
|
+
file_path: item.filePath,
|
|
490
|
+
scope: item.scope,
|
|
491
|
+
local_path: localPath ?? undefined,
|
|
492
|
+
});
|
|
493
|
+
if (localPath && existsSync(localPath)) {
|
|
494
|
+
moveFileToTrash(localPath, "agent-configs");
|
|
495
|
+
}
|
|
496
|
+
console.log(chalk.green(` \u2713 ${item.agent}/${item.filePath}`), chalk.gray("removed from this device (moved to Memax trash)"));
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
await getClient().configs.delete(item.id);
|
|
500
|
+
if (localPath && existsSync(localPath)) {
|
|
501
|
+
moveFileToTrash(localPath, "agent-configs");
|
|
502
|
+
}
|
|
503
|
+
await getClient().configs.ack({
|
|
504
|
+
device_id: deviceID,
|
|
505
|
+
configs: [
|
|
506
|
+
{
|
|
507
|
+
agent: item.agent,
|
|
508
|
+
file_path: item.filePath,
|
|
509
|
+
scope: item.scope,
|
|
510
|
+
version: item.version + 1,
|
|
511
|
+
local_path: localPath ?? undefined,
|
|
512
|
+
deleted: true,
|
|
513
|
+
},
|
|
514
|
+
],
|
|
515
|
+
});
|
|
516
|
+
console.log(chalk.green(` \u2713 ${item.agent}/${item.filePath}`), chalk.gray("deleted everywhere (local copy moved to Memax trash)"));
|
|
517
|
+
}
|
|
518
|
+
deleted++;
|
|
519
|
+
}
|
|
520
|
+
catch (err) {
|
|
521
|
+
console.log(chalk.red(` \u2717 ${item.agent}/${item.filePath}`), chalk.gray(err.message));
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
console.log(chalk.gray(mode === "e"
|
|
525
|
+
? `\n ${deleted} config${deleted > 1 ? "s" : ""} deleted everywhere.\n`
|
|
526
|
+
: `\n ${deleted} config${deleted > 1 ? "s" : ""} removed from this device.\n`));
|
|
527
|
+
}
|
|
528
|
+
function formatAge(dateStr) {
|
|
529
|
+
const ms = Date.now() - new Date(dateStr).getTime();
|
|
530
|
+
const mins = Math.floor(ms / 60000);
|
|
531
|
+
if (mins < 60)
|
|
532
|
+
return `${mins}m ago`;
|
|
533
|
+
const hours = Math.floor(mins / 60);
|
|
534
|
+
if (hours < 24)
|
|
535
|
+
return `${hours}h ago`;
|
|
536
|
+
const days = Math.floor(hours / 24);
|
|
537
|
+
return `${days}d ago`;
|
|
538
|
+
}
|
|
539
|
+
export async function syncCommand(directory, options) {
|
|
48
540
|
const dir = directory ?? ".";
|
|
541
|
+
const syncRoot = resolve(dir);
|
|
49
542
|
const customIgnore = options.ignore
|
|
50
543
|
? new Set(options.ignore.split(",").map((s) => s.trim()))
|
|
51
544
|
: new Set();
|
|
52
545
|
const ignoreSet = new Set([...DEFAULT_IGNORE, ...customIgnore]);
|
|
546
|
+
const ignorePatterns = [...customIgnore].sort();
|
|
547
|
+
const projectContext = detectProjectContext(syncRoot);
|
|
53
548
|
console.log(chalk.blue("Scanning"), dir);
|
|
54
|
-
const files = walkDir(
|
|
549
|
+
const files = walkDir(syncRoot, ignoreSet);
|
|
55
550
|
if (files.length === 0) {
|
|
56
551
|
console.log(chalk.yellow("No supported files found."));
|
|
552
|
+
updateSyncSourceRun(syncRoot, {
|
|
553
|
+
ignorePatterns,
|
|
554
|
+
defaultCategory: options.category,
|
|
555
|
+
defaultBoundary: options.boundary,
|
|
556
|
+
mode: options.watch ? "watch" : "manual",
|
|
557
|
+
scanCount: 0,
|
|
558
|
+
pushed: 0,
|
|
559
|
+
skipped: 0,
|
|
560
|
+
errors: 0,
|
|
561
|
+
});
|
|
57
562
|
return;
|
|
58
563
|
}
|
|
59
564
|
console.log(chalk.gray(`Found ${files.length} files to sync`));
|
|
565
|
+
// Confirm if many files (>10) unless -y is passed
|
|
566
|
+
if (files.length > 10 && !options.yes) {
|
|
567
|
+
console.log(chalk.yellow(`\n This will push ${files.length} files. Continue? (y/N) `));
|
|
568
|
+
const confirmed = await confirm(" ");
|
|
569
|
+
if (!confirmed) {
|
|
570
|
+
console.log(chalk.gray(" Cancelled.\n"));
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
60
574
|
console.log();
|
|
61
575
|
let pushed = 0;
|
|
576
|
+
let skipped = 0;
|
|
62
577
|
let errors = 0;
|
|
63
578
|
for (const file of files) {
|
|
64
|
-
const result = await pushFile(file, options);
|
|
579
|
+
const result = await pushFile(file, syncRoot, projectContext, options);
|
|
65
580
|
if (result === "pushed")
|
|
66
581
|
pushed++;
|
|
582
|
+
else if (result === "skipped")
|
|
583
|
+
skipped++;
|
|
67
584
|
else if (result === "error")
|
|
68
585
|
errors++;
|
|
69
586
|
}
|
|
587
|
+
updateSyncSourceRun(syncRoot, {
|
|
588
|
+
ignorePatterns,
|
|
589
|
+
defaultCategory: options.category,
|
|
590
|
+
defaultBoundary: options.boundary,
|
|
591
|
+
mode: options.watch ? "watch" : "manual",
|
|
592
|
+
scanCount: files.length,
|
|
593
|
+
pushed,
|
|
594
|
+
skipped,
|
|
595
|
+
errors,
|
|
596
|
+
});
|
|
70
597
|
console.log();
|
|
71
|
-
console.log(chalk.blue(`Synced ${pushed} files`), errors > 0 ? chalk.red(`(${errors} errors)`) : "");
|
|
598
|
+
console.log(chalk.blue(`Synced ${pushed} files`), skipped > 0 ? chalk.gray(`(${skipped} skipped)`) : "", errors > 0 ? chalk.red(`(${errors} errors)`) : "");
|
|
599
|
+
console.log(chalk.gray(" Missing local files are retained in Memax until removed explicitly."));
|
|
72
600
|
if (options.watch) {
|
|
73
|
-
const resolvedDir = resolve(dir);
|
|
74
601
|
console.log(chalk.cyan(`\nWatching ${dir} for changes... (Ctrl+C to stop)`));
|
|
75
602
|
let debounceTimer = null;
|
|
76
603
|
const pendingChanges = new Set();
|
|
77
|
-
watch(
|
|
604
|
+
watch(syncRoot, { recursive: true }, (_eventType, filename) => {
|
|
78
605
|
if (!filename)
|
|
79
606
|
return;
|
|
80
|
-
const fullPath = join(
|
|
607
|
+
const fullPath = join(syncRoot, filename);
|
|
81
608
|
if (!isSupportedFile(filename) || isIgnored(filename, ignoreSet))
|
|
82
609
|
return;
|
|
83
610
|
pendingChanges.add(fullPath);
|
|
@@ -86,37 +613,37 @@ export async function syncCommand(directory, options) {
|
|
|
86
613
|
debounceTimer = setTimeout(async () => {
|
|
87
614
|
for (const file of pendingChanges) {
|
|
88
615
|
if (!existsSync(file)) {
|
|
89
|
-
console.log(chalk.gray(" ~"),
|
|
616
|
+
console.log(chalk.gray(" ~"), buildSyncSourcePath(syncRoot, file), chalk.gray("[deleted locally, retained in Memax]"));
|
|
90
617
|
continue;
|
|
91
618
|
}
|
|
92
|
-
await pushFile(file, options);
|
|
619
|
+
await pushFile(file, syncRoot, projectContext, options);
|
|
93
620
|
}
|
|
94
621
|
pendingChanges.clear();
|
|
95
622
|
}, 500);
|
|
96
623
|
});
|
|
97
624
|
}
|
|
98
625
|
}
|
|
99
|
-
async function pushFile(file, options) {
|
|
626
|
+
async function pushFile(file, syncRoot, projectContext, options) {
|
|
100
627
|
try {
|
|
101
628
|
const content = readFileSync(file, "utf-8");
|
|
102
629
|
if (!content.trim())
|
|
103
630
|
return "skipped";
|
|
104
|
-
const relPath =
|
|
631
|
+
const relPath = buildSyncSourcePath(syncRoot, file);
|
|
105
632
|
const ext = extname(file);
|
|
106
633
|
const contentType = ext === ".md"
|
|
107
634
|
? "markdown"
|
|
108
635
|
: ext === ".json" || ext === ".yaml" || ext === ".yml"
|
|
109
636
|
? "structured"
|
|
110
637
|
: "code";
|
|
111
|
-
const
|
|
112
|
-
content,
|
|
638
|
+
const memory = await getClient().push(content, {
|
|
113
639
|
title: relPath,
|
|
114
640
|
category: options.category ?? guessCategory(relPath),
|
|
115
641
|
source: "sync",
|
|
116
|
-
|
|
117
|
-
|
|
642
|
+
sourcePath: relPath,
|
|
643
|
+
contentType,
|
|
644
|
+
projectContext,
|
|
118
645
|
});
|
|
119
|
-
console.log(chalk.green(" +"), relPath, chalk.gray(`[${
|
|
646
|
+
console.log(chalk.green(" +"), relPath, chalk.gray(`[${memory.category}]`));
|
|
120
647
|
return "pushed";
|
|
121
648
|
}
|
|
122
649
|
catch (err) {
|
|
@@ -124,6 +651,46 @@ async function pushFile(file, options) {
|
|
|
124
651
|
return "error";
|
|
125
652
|
}
|
|
126
653
|
}
|
|
654
|
+
export function buildSyncSourcePath(syncRoot, file) {
|
|
655
|
+
const relativePath = relative(syncRoot, file);
|
|
656
|
+
if (!relativePath ||
|
|
657
|
+
relativePath.startsWith("..") ||
|
|
658
|
+
relativePath === "." ||
|
|
659
|
+
resolve(syncRoot, relativePath) !== resolve(file)) {
|
|
660
|
+
throw new Error("file is outside sync root");
|
|
661
|
+
}
|
|
662
|
+
return normalizeFilePath(relativePath);
|
|
663
|
+
}
|
|
664
|
+
export function syncStatusCommand() {
|
|
665
|
+
const sources = listSyncSources();
|
|
666
|
+
console.log(chalk.bold("\n Memax Sync Status\n"));
|
|
667
|
+
if (sources.length === 0) {
|
|
668
|
+
console.log(chalk.gray(" No sync sources recorded yet."));
|
|
669
|
+
console.log(chalk.gray(" Run: memax sync <dir>\n"));
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
for (const source of sources) {
|
|
673
|
+
console.log(` ${chalk.cyan(source.root_path)}`);
|
|
674
|
+
console.log(` kind ${chalk.gray(source.kind)}`);
|
|
675
|
+
console.log(` deletion policy ${chalk.gray("retain missing files in Memax")}`);
|
|
676
|
+
if (source.default_category) {
|
|
677
|
+
console.log(` category ${chalk.gray(source.default_category)}`);
|
|
678
|
+
}
|
|
679
|
+
if (source.default_boundary) {
|
|
680
|
+
console.log(` boundary ${chalk.gray(source.default_boundary)}`);
|
|
681
|
+
}
|
|
682
|
+
if (source.ignore_patterns.length > 0) {
|
|
683
|
+
console.log(` ignore ${chalk.gray(source.ignore_patterns.join(", "))}`);
|
|
684
|
+
}
|
|
685
|
+
if (source.last_sync_at) {
|
|
686
|
+
console.log(` last sync ${chalk.gray(formatAge(source.last_sync_at))} (${chalk.gray(source.last_mode ?? "manual")})`);
|
|
687
|
+
}
|
|
688
|
+
if (source.last_scan_count !== undefined) {
|
|
689
|
+
console.log(` last run ${chalk.gray(`${source.last_scan_count} scanned, ${source.last_pushed ?? 0} pushed, ${source.last_skipped ?? 0} skipped, ${source.last_errors ?? 0} errors`)}`);
|
|
690
|
+
}
|
|
691
|
+
console.log();
|
|
692
|
+
}
|
|
693
|
+
}
|
|
127
694
|
function isSupportedFile(filename) {
|
|
128
695
|
return SUPPORTED_EXTENSIONS.has(extname(filename));
|
|
129
696
|
}
|
|
@@ -179,143 +746,756 @@ function guessCategory(path) {
|
|
|
179
746
|
return "reference/config";
|
|
180
747
|
return "daily/note";
|
|
181
748
|
}
|
|
182
|
-
function
|
|
749
|
+
function findClaudeProjectDir(scope) {
|
|
750
|
+
const home = homedir();
|
|
751
|
+
const claudeProjectsDir = join(home, ".claude", "projects");
|
|
752
|
+
if (!existsSync(claudeProjectsDir))
|
|
753
|
+
return null;
|
|
754
|
+
try {
|
|
755
|
+
for (const project of readdirSync(claudeProjectsDir)) {
|
|
756
|
+
const repoUrl = resolveClaudeProjectFolder(project);
|
|
757
|
+
if (repoUrl && scope === `project:${repoUrl}`) {
|
|
758
|
+
return join(claudeProjectsDir, project);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
catch {
|
|
763
|
+
// Permission denied — skip
|
|
764
|
+
}
|
|
765
|
+
return null;
|
|
766
|
+
}
|
|
767
|
+
function printPlacementSection(title, color, items, format) {
|
|
768
|
+
if (items.length === 0)
|
|
769
|
+
return;
|
|
770
|
+
console.log(chalk.white(title));
|
|
771
|
+
for (const item of items) {
|
|
772
|
+
const [agent, filePath, detail] = format(item);
|
|
773
|
+
console.log(` ${color(formatAgentName(agent))} ${filePath}`);
|
|
774
|
+
console.log(` ${chalk.gray(detail)}`);
|
|
775
|
+
}
|
|
776
|
+
console.log();
|
|
777
|
+
}
|
|
778
|
+
export function resolveAgentConfigWritePath(agent, filePath, scope, options = {}) {
|
|
779
|
+
const cwd = options.cwd ?? process.cwd();
|
|
780
|
+
const home = options.home ?? homedir();
|
|
781
|
+
const currentProjectScope = options.currentProjectScope ?? getProjectScope(cwd);
|
|
782
|
+
const normalizedFilePath = normalizeFilePath(filePath);
|
|
783
|
+
if (scope === "global") {
|
|
784
|
+
switch (agent) {
|
|
785
|
+
case "claude-code":
|
|
786
|
+
return join(home, ".claude", normalizedFilePath);
|
|
787
|
+
case "codex":
|
|
788
|
+
return join(home, ".codex", normalizedFilePath);
|
|
789
|
+
case "gemini":
|
|
790
|
+
return join(home, ".gemini", normalizedFilePath);
|
|
791
|
+
case "openclaw":
|
|
792
|
+
return join(home, ".openclaw", normalizedFilePath);
|
|
793
|
+
case "opencode":
|
|
794
|
+
return join(home, ".opencode", normalizedFilePath);
|
|
795
|
+
default:
|
|
796
|
+
return null;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
if (!scope.startsWith("project:") || scope !== currentProjectScope) {
|
|
800
|
+
return null;
|
|
801
|
+
}
|
|
802
|
+
switch (agent) {
|
|
803
|
+
case "claude-code":
|
|
804
|
+
if (normalizedFilePath === "CLAUDE.md" ||
|
|
805
|
+
normalizedFilePath === "MEMORY.md") {
|
|
806
|
+
return join(cwd, ".claude", normalizedFilePath);
|
|
807
|
+
}
|
|
808
|
+
if (normalizedFilePath.startsWith(".claude/")) {
|
|
809
|
+
return join(cwd, normalizedFilePath);
|
|
810
|
+
}
|
|
811
|
+
if (normalizedFilePath.startsWith("memory/")) {
|
|
812
|
+
const projectDir = options.findClaudeProjectDir?.(scope);
|
|
813
|
+
if (projectDir) {
|
|
814
|
+
return join(projectDir, normalizedFilePath);
|
|
815
|
+
}
|
|
816
|
+
const mangledCwd = cwd.replace(/\//g, "-");
|
|
817
|
+
return join(home, ".claude", "projects", mangledCwd, normalizedFilePath);
|
|
818
|
+
}
|
|
819
|
+
return null;
|
|
820
|
+
case "cursor":
|
|
821
|
+
if (normalizedFilePath === ".cursorrules" ||
|
|
822
|
+
normalizedFilePath.startsWith(".cursor/")) {
|
|
823
|
+
return join(cwd, normalizedFilePath);
|
|
824
|
+
}
|
|
825
|
+
return null;
|
|
826
|
+
case "codex":
|
|
827
|
+
if (normalizedFilePath === "instructions.md") {
|
|
828
|
+
return join(cwd, ".codex", "instructions.md");
|
|
829
|
+
}
|
|
830
|
+
if (normalizedFilePath.startsWith(".codex/")) {
|
|
831
|
+
return join(cwd, normalizedFilePath);
|
|
832
|
+
}
|
|
833
|
+
return null;
|
|
834
|
+
case "gemini":
|
|
835
|
+
if (normalizedFilePath === "GEMINI.md") {
|
|
836
|
+
return join(cwd, "GEMINI.md");
|
|
837
|
+
}
|
|
838
|
+
return null;
|
|
839
|
+
case "copilot":
|
|
840
|
+
if (normalizedFilePath === "copilot-instructions.md") {
|
|
841
|
+
return join(cwd, ".github", "copilot-instructions.md");
|
|
842
|
+
}
|
|
843
|
+
if (normalizedFilePath.startsWith(".github/")) {
|
|
844
|
+
return join(cwd, normalizedFilePath);
|
|
845
|
+
}
|
|
846
|
+
return null;
|
|
847
|
+
case "windsurf":
|
|
848
|
+
if (normalizedFilePath === ".windsurfrules" ||
|
|
849
|
+
normalizedFilePath.startsWith(".windsurf/")) {
|
|
850
|
+
return join(cwd, normalizedFilePath);
|
|
851
|
+
}
|
|
852
|
+
return null;
|
|
853
|
+
case "opencode":
|
|
854
|
+
if (normalizedFilePath.startsWith(".opencode/")) {
|
|
855
|
+
return join(cwd, normalizedFilePath);
|
|
856
|
+
}
|
|
857
|
+
return join(cwd, ".opencode", normalizedFilePath);
|
|
858
|
+
case "generic":
|
|
859
|
+
if (normalizedFilePath === "AGENTS.md" ||
|
|
860
|
+
normalizedFilePath === "CLAUDE.md" ||
|
|
861
|
+
normalizedFilePath === "GEMINI.md") {
|
|
862
|
+
return join(cwd, normalizedFilePath);
|
|
863
|
+
}
|
|
864
|
+
return null;
|
|
865
|
+
default:
|
|
866
|
+
return null;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
function discoverAgentConfigs() {
|
|
183
870
|
const home = homedir();
|
|
184
871
|
const cwd = process.cwd();
|
|
872
|
+
const projectScope = getProjectScope(cwd);
|
|
873
|
+
const canonicalProjectScope = isCanonicalProjectScope(projectScope)
|
|
874
|
+
? projectScope
|
|
875
|
+
: null;
|
|
185
876
|
const locations = [];
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
877
|
+
const add = (agent, label, path, filePath, scope = "global") => locations.push({
|
|
878
|
+
agent,
|
|
879
|
+
label,
|
|
880
|
+
path,
|
|
881
|
+
filePath: normalizeFilePath(filePath),
|
|
882
|
+
scope,
|
|
883
|
+
});
|
|
884
|
+
// Claude Code — global
|
|
885
|
+
add("claude-code", "~/.claude/CLAUDE.md", join(home, ".claude", "CLAUDE.md"), "CLAUDE.md");
|
|
886
|
+
add("claude-code", "~/.claude/MEMORY.md", join(home, ".claude", "MEMORY.md"), "MEMORY.md");
|
|
887
|
+
// Claude Code — per-project memories: ~/.claude/projects/*/memory/*.md
|
|
888
|
+
// The folder name is the absolute project path with "/" replaced by "-"
|
|
889
|
+
// (e.g., "-workspaces-memax"). We resolve it to a git repo URL so the
|
|
890
|
+
// same project's memories match across machines regardless of clone path.
|
|
190
891
|
const claudeProjectsDir = join(home, ".claude", "projects");
|
|
191
892
|
if (existsSync(claudeProjectsDir)) {
|
|
192
893
|
try {
|
|
193
894
|
for (const project of readdirSync(claudeProjectsDir)) {
|
|
194
895
|
const memoryDir = join(claudeProjectsDir, project, "memory");
|
|
195
|
-
if (existsSync(memoryDir))
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
896
|
+
if (!existsSync(memoryDir))
|
|
897
|
+
continue;
|
|
898
|
+
// Try to resolve mangled folder → git repo → canonical scope
|
|
899
|
+
const repoUrl = resolveClaudeProjectFolder(project);
|
|
900
|
+
const memoryScope = repoUrl
|
|
901
|
+
? `project:${repoUrl}`
|
|
902
|
+
: undefined;
|
|
903
|
+
try {
|
|
904
|
+
for (const file of readdirSync(memoryDir)) {
|
|
905
|
+
if (!file.endsWith(".md"))
|
|
906
|
+
continue;
|
|
907
|
+
if (memoryScope) {
|
|
908
|
+
// Canonical: filePath is just "memory/<file>", scope identifies the project
|
|
909
|
+
add("claude-code", `~/.claude/projects/${project}/memory/${file}`, join(memoryDir, file), `memory/${file}`, memoryScope);
|
|
910
|
+
}
|
|
911
|
+
else {
|
|
912
|
+
// Fallback: can't resolve project → keep legacy format with folder name
|
|
913
|
+
add("claude-code", `~/.claude/projects/${project}/memory/${file}`, join(memoryDir, file), `projects/${project}/memory/${file}`);
|
|
204
914
|
}
|
|
205
915
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
916
|
+
}
|
|
917
|
+
catch {
|
|
918
|
+
// Permission denied — skip
|
|
209
919
|
}
|
|
210
920
|
}
|
|
211
921
|
}
|
|
212
922
|
catch {
|
|
213
|
-
// Permission denied
|
|
923
|
+
// Permission denied — skip
|
|
214
924
|
}
|
|
215
925
|
}
|
|
216
|
-
//
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
926
|
+
// --- Project-scoped configs (only when inside a git repo) ---
|
|
927
|
+
if (canonicalProjectScope) {
|
|
928
|
+
// Claude Code — project-level
|
|
929
|
+
add("claude-code", "./.claude/CLAUDE.md", join(cwd, ".claude", "CLAUDE.md"), "CLAUDE.md", canonicalProjectScope);
|
|
930
|
+
// Cursor (project-level)
|
|
931
|
+
add("cursor", "./.cursorrules", join(cwd, ".cursorrules"), ".cursorrules", canonicalProjectScope);
|
|
932
|
+
const cursorRulesDir = join(cwd, ".cursor", "rules");
|
|
933
|
+
if (existsSync(cursorRulesDir)) {
|
|
934
|
+
try {
|
|
935
|
+
for (const file of readdirSync(cursorRulesDir)) {
|
|
936
|
+
if (file.endsWith(".mdc")) {
|
|
937
|
+
add("cursor", `./.cursor/rules/${file}`, join(cursorRulesDir, file), `.cursor/rules/${file}`, canonicalProjectScope);
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
catch {
|
|
942
|
+
/* skip */
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
// Codex (project-level)
|
|
946
|
+
add("codex", "./.codex/instructions.md", join(cwd, ".codex", "instructions.md"), "instructions.md", canonicalProjectScope);
|
|
947
|
+
}
|
|
948
|
+
add("codex", "~/.codex/AGENTS.md", join(home, ".codex", "AGENTS.md"), "AGENTS.md");
|
|
949
|
+
// Gemini CLI — global
|
|
950
|
+
add("gemini", "~/.gemini/GEMINI.md", join(home, ".gemini", "GEMINI.md"), "GEMINI.md");
|
|
951
|
+
if (canonicalProjectScope) {
|
|
952
|
+
// Gemini CLI — project-level
|
|
953
|
+
add("gemini", "./GEMINI.md", join(cwd, "GEMINI.md"), "GEMINI.md", canonicalProjectScope);
|
|
954
|
+
// GitHub Copilot
|
|
955
|
+
add("copilot", "./.github/copilot-instructions.md", join(cwd, ".github", "copilot-instructions.md"), "copilot-instructions.md", canonicalProjectScope);
|
|
956
|
+
// Windsurf
|
|
957
|
+
add("windsurf", "./.windsurfrules", join(cwd, ".windsurfrules"), ".windsurfrules", canonicalProjectScope);
|
|
958
|
+
const windsurfRulesDir = join(cwd, ".windsurf", "rules");
|
|
959
|
+
if (existsSync(windsurfRulesDir)) {
|
|
960
|
+
try {
|
|
961
|
+
for (const file of readdirSync(windsurfRulesDir)) {
|
|
962
|
+
if (file.endsWith(".md")) {
|
|
963
|
+
add("windsurf", `./.windsurf/rules/${file}`, join(windsurfRulesDir, file), `.windsurf/rules/${file}`, canonicalProjectScope);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
catch {
|
|
968
|
+
/* skip */
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
// OpenClaw
|
|
973
|
+
const openclawMemoryDir = join(home, ".openclaw", "memory");
|
|
974
|
+
if (existsSync(openclawMemoryDir)) {
|
|
221
975
|
try {
|
|
222
|
-
for (const file of readdirSync(
|
|
223
|
-
if (file.endsWith(".
|
|
224
|
-
|
|
225
|
-
label: `./.cursor/rules/${file}`,
|
|
226
|
-
path: join(cursorRulesDir, file),
|
|
227
|
-
});
|
|
976
|
+
for (const file of readdirSync(openclawMemoryDir)) {
|
|
977
|
+
if (file.endsWith(".md") || file.endsWith(".json")) {
|
|
978
|
+
add("openclaw", `~/.openclaw/memory/${file}`, join(openclawMemoryDir, file), `memory/${file}`);
|
|
228
979
|
}
|
|
229
980
|
}
|
|
230
981
|
}
|
|
231
982
|
catch {
|
|
232
|
-
|
|
983
|
+
/* skip */
|
|
233
984
|
}
|
|
234
985
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
});
|
|
240
|
-
// Generic agent config files in current directory
|
|
241
|
-
locations.push({ label: "./AGENTS.md", path: join(cwd, "AGENTS.md") });
|
|
242
|
-
locations.push({ label: "./CLAUDE.md", path: join(cwd, "CLAUDE.md") });
|
|
243
|
-
return locations;
|
|
244
|
-
}
|
|
245
|
-
function formatFileSize(bytes) {
|
|
246
|
-
if (bytes < 1024)
|
|
247
|
-
return `${bytes} B`;
|
|
248
|
-
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
249
|
-
}
|
|
250
|
-
async function syncAgentMemory() {
|
|
251
|
-
console.log(chalk.blue("Scanning for agent memory files..."));
|
|
252
|
-
console.log();
|
|
253
|
-
const locations = discoverAgentMemoryFiles();
|
|
254
|
-
const found = [];
|
|
255
|
-
const notFound = [];
|
|
256
|
-
for (const loc of locations) {
|
|
257
|
-
if (existsSync(loc.path)) {
|
|
986
|
+
if (canonicalProjectScope) {
|
|
987
|
+
// OpenCode (project-level)
|
|
988
|
+
const opencodePath = join(cwd, ".opencode");
|
|
989
|
+
if (existsSync(opencodePath)) {
|
|
258
990
|
try {
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
else {
|
|
264
|
-
notFound.push(loc.label);
|
|
991
|
+
for (const file of readdirSync(opencodePath)) {
|
|
992
|
+
if (file.endsWith(".md")) {
|
|
993
|
+
add("opencode", `./.opencode/${file}`, join(opencodePath, file), file, canonicalProjectScope);
|
|
994
|
+
}
|
|
265
995
|
}
|
|
266
996
|
}
|
|
267
997
|
catch {
|
|
268
|
-
|
|
998
|
+
/* skip */
|
|
269
999
|
}
|
|
270
1000
|
}
|
|
271
|
-
|
|
272
|
-
|
|
1001
|
+
// Generic project-level agent files
|
|
1002
|
+
add("generic", "./AGENTS.md", join(cwd, "AGENTS.md"), "AGENTS.md", canonicalProjectScope);
|
|
1003
|
+
add("generic", "./CLAUDE.md", join(cwd, "CLAUDE.md"), "CLAUDE.md", canonicalProjectScope);
|
|
1004
|
+
}
|
|
1005
|
+
return locations;
|
|
1006
|
+
}
|
|
1007
|
+
async function syncAgentMemory(options = {}) {
|
|
1008
|
+
console.log(chalk.bold("\n Memax Config Sync\n"));
|
|
1009
|
+
const projectScopeResolution = resolveProjectScope();
|
|
1010
|
+
if (projectScopeResolution.warning) {
|
|
1011
|
+
console.log(chalk.yellow(` Warning: ${projectScopeResolution.warning}`));
|
|
1012
|
+
console.log(chalk.gray(" Using .memax.yml project_id as the canonical project identity.\n"));
|
|
1013
|
+
}
|
|
1014
|
+
// Discover local config files
|
|
1015
|
+
const locations = discoverAgentConfigs();
|
|
1016
|
+
const localConfigs = [];
|
|
1017
|
+
for (const loc of locations) {
|
|
1018
|
+
if (!existsSync(loc.path))
|
|
1019
|
+
continue;
|
|
1020
|
+
try {
|
|
1021
|
+
const stat = statSync(loc.path);
|
|
1022
|
+
if (!stat.isFile() || stat.size === 0)
|
|
1023
|
+
continue;
|
|
1024
|
+
const content = readFileSync(loc.path, "utf-8");
|
|
1025
|
+
if (!content.trim())
|
|
1026
|
+
continue;
|
|
1027
|
+
const hash = createHash("sha256").update(content).digest("hex");
|
|
1028
|
+
localConfigs.push({
|
|
1029
|
+
loc,
|
|
1030
|
+
content,
|
|
1031
|
+
hash,
|
|
1032
|
+
updatedAt: stat.mtime.toISOString(),
|
|
1033
|
+
});
|
|
273
1034
|
}
|
|
1035
|
+
catch {
|
|
1036
|
+
// Skip unreadable files
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
const isBootstrap = localConfigs.length === 0;
|
|
1040
|
+
const deviceID = getOrCreateDeviceID();
|
|
1041
|
+
if (isBootstrap) {
|
|
1042
|
+
console.log(chalk.gray(" No local agent configs found. Checking cloud for backups...\n"));
|
|
1043
|
+
}
|
|
1044
|
+
else {
|
|
1045
|
+
console.log(chalk.gray(` Found ${localConfigs.length} local config${localConfigs.length > 1 ? "s" : ""}. Syncing with cloud...\n`));
|
|
274
1046
|
}
|
|
275
|
-
|
|
276
|
-
|
|
1047
|
+
// Build manifest — may be empty on a new device (that's fine, we'll pull from cloud)
|
|
1048
|
+
const manifest = localConfigs.map((c) => ({
|
|
1049
|
+
agent: c.loc.agent,
|
|
1050
|
+
file_path: c.loc.filePath,
|
|
1051
|
+
scope: c.loc.scope,
|
|
1052
|
+
content_hash: c.hash,
|
|
1053
|
+
updated_at: c.updatedAt,
|
|
1054
|
+
local_path: c.loc.path,
|
|
1055
|
+
}));
|
|
1056
|
+
let actions;
|
|
1057
|
+
try {
|
|
1058
|
+
const plan = await getClient().configs.sync({
|
|
1059
|
+
device_id: deviceID,
|
|
1060
|
+
configs: manifest,
|
|
1061
|
+
});
|
|
1062
|
+
actions = plan.actions;
|
|
1063
|
+
}
|
|
1064
|
+
catch (err) {
|
|
1065
|
+
console.error(chalk.red(` Sync failed: ${err.message}\n`));
|
|
277
1066
|
return;
|
|
278
1067
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
1068
|
+
// Force modes: resolve ALL ambiguous actions in one direction.
|
|
1069
|
+
// This includes both "conflict" AND "delete_local" (tombstone-driven
|
|
1070
|
+
// deletions where the server decided local should be removed).
|
|
1071
|
+
// One-sided actions (local_only push, cloud_only pull) stay as-is.
|
|
1072
|
+
if (options.push) {
|
|
1073
|
+
actions = actions.map((a) => {
|
|
1074
|
+
if (a.action === "conflict")
|
|
1075
|
+
return { ...a, action: "push" };
|
|
1076
|
+
if (a.action === "delete_local")
|
|
1077
|
+
return { ...a, action: "push" };
|
|
1078
|
+
return a;
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
else if (options.pull) {
|
|
1082
|
+
actions = actions.map((a) => {
|
|
1083
|
+
if (a.action !== "conflict")
|
|
1084
|
+
return a;
|
|
1085
|
+
if (a.config_id)
|
|
1086
|
+
return { ...a, action: "pull" };
|
|
1087
|
+
return { ...a, action: "delete_local" };
|
|
1088
|
+
});
|
|
282
1089
|
}
|
|
283
|
-
|
|
284
|
-
|
|
1090
|
+
// Skip conflicts mode (used by setup): only process one-sided actions
|
|
1091
|
+
// (local_only push, cloud_only pull), skip all ambiguous/destructive actions.
|
|
1092
|
+
if (options.skipConflicts) {
|
|
1093
|
+
const skippedCount = actions.filter((a) => a.action === "conflict" || a.action === "delete_local").length;
|
|
1094
|
+
actions = actions.filter((a) => a.action !== "conflict" && a.action !== "delete_local");
|
|
1095
|
+
if (skippedCount > 0) {
|
|
1096
|
+
console.log(chalk.gray(` ${skippedCount} conflict${skippedCount > 1 ? "s" : ""} skipped (resolve with: memax sync agents)\n`));
|
|
1097
|
+
}
|
|
285
1098
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
1099
|
+
// Filter out project-scoped cloud-only configs that don't belong to the
|
|
1100
|
+
// current project. Without this, running `memax sync agents` from ~/
|
|
1101
|
+
// would dump project configs (like .cursorrules from repo X) into the
|
|
1102
|
+
// home directory.
|
|
1103
|
+
const currentProjectScope = projectScopeResolution.scope;
|
|
1104
|
+
actions = actions.filter((a) => {
|
|
1105
|
+
if (!a.scope.startsWith("project:"))
|
|
1106
|
+
return true; // global → always sync
|
|
1107
|
+
// Project-scoped: only sync if it matches the current project
|
|
1108
|
+
if (a.scope === currentProjectScope)
|
|
1109
|
+
return true;
|
|
1110
|
+
// Cloud-only configs for other projects → skip silently
|
|
1111
|
+
if (a.action === "pull" && a.reason === "cloud_only")
|
|
1112
|
+
return false;
|
|
1113
|
+
// Conflict/push for configs we have locally → keep (user is in the project)
|
|
1114
|
+
return true;
|
|
1115
|
+
});
|
|
1116
|
+
// Index local configs by (agent, file_path, scope) for quick lookup
|
|
1117
|
+
const localByKey = new Map();
|
|
1118
|
+
for (const c of localConfigs) {
|
|
1119
|
+
localByKey.set(`${c.loc.agent}|${c.loc.filePath}|${c.loc.scope}`, c);
|
|
1120
|
+
}
|
|
1121
|
+
// Index locations by (agent, file_path, scope) for pull path resolution
|
|
1122
|
+
const locByKey = new Map();
|
|
1123
|
+
for (const loc of locations) {
|
|
1124
|
+
locByKey.set(`${loc.agent}|${loc.filePath}|${loc.scope}`, loc);
|
|
1125
|
+
}
|
|
1126
|
+
// Resolve a local write path for any config — even ones not discovered locally.
|
|
1127
|
+
// This enables pulling configs to a brand-new device where agent dirs don't exist yet.
|
|
1128
|
+
const resolveWritePath = (agent, filePath, scope) => {
|
|
1129
|
+
const loc = locByKey.get(`${agent}|${filePath}|${scope}`);
|
|
1130
|
+
if (loc)
|
|
1131
|
+
return loc.path;
|
|
1132
|
+
return resolveAgentConfigWritePath(agent, filePath, scope, {
|
|
1133
|
+
cwd: process.cwd(),
|
|
1134
|
+
home: homedir(),
|
|
1135
|
+
currentProjectScope,
|
|
1136
|
+
findClaudeProjectDir,
|
|
1137
|
+
});
|
|
1138
|
+
};
|
|
1139
|
+
// Execute sync plan
|
|
1140
|
+
let pushed = 0;
|
|
1141
|
+
let pulled = 0;
|
|
1142
|
+
let deletedLocal = 0;
|
|
1143
|
+
let unchangedCount = 0;
|
|
1144
|
+
let skipped = 0;
|
|
289
1145
|
let errors = 0;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
1146
|
+
const ackConfigs = [];
|
|
1147
|
+
// Group actions by agent for display
|
|
1148
|
+
const byAgent = new Map();
|
|
1149
|
+
for (const action of actions) {
|
|
1150
|
+
const group = byAgent.get(action.agent) ?? [];
|
|
1151
|
+
group.push(action);
|
|
1152
|
+
byAgent.set(action.agent, group);
|
|
1153
|
+
}
|
|
1154
|
+
for (const [agent, agentActions] of byAgent) {
|
|
1155
|
+
console.log(chalk.white(` ${formatAgentName(agent)}`));
|
|
1156
|
+
for (const action of agentActions) {
|
|
1157
|
+
const key = `${action.agent}|${action.file_path}|${action.scope}`;
|
|
1158
|
+
if (action.action === "unchanged") {
|
|
1159
|
+
const local = localByKey.get(key);
|
|
1160
|
+
console.log(chalk.gray(` = ${action.file_path}`), chalk.gray("unchanged"));
|
|
1161
|
+
if (local && action.version) {
|
|
1162
|
+
ackConfigs.push({
|
|
1163
|
+
agent: action.agent,
|
|
1164
|
+
file_path: action.file_path,
|
|
1165
|
+
scope: action.scope,
|
|
1166
|
+
content_hash: local.hash,
|
|
1167
|
+
version: action.version,
|
|
1168
|
+
local_path: local.loc.path,
|
|
1169
|
+
});
|
|
1170
|
+
}
|
|
1171
|
+
unchangedCount++;
|
|
294
1172
|
continue;
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
1173
|
+
}
|
|
1174
|
+
if (action.action === "push") {
|
|
1175
|
+
const local = localByKey.get(key);
|
|
1176
|
+
if (!local) {
|
|
1177
|
+
console.log(chalk.red(` \u2717 ${action.file_path}`), chalk.gray("local file not found for push"));
|
|
1178
|
+
errors++;
|
|
1179
|
+
continue;
|
|
1180
|
+
}
|
|
1181
|
+
try {
|
|
1182
|
+
await getClient().configs.upsert({
|
|
1183
|
+
agent: action.agent,
|
|
1184
|
+
file_path: action.file_path,
|
|
1185
|
+
scope: action.scope,
|
|
1186
|
+
content: local.content,
|
|
1187
|
+
device_id: deviceID,
|
|
1188
|
+
local_path: local.loc.path,
|
|
1189
|
+
});
|
|
1190
|
+
console.log(chalk.green(` \u2191 ${action.file_path}`), chalk.gray(action.reason === "local_only"
|
|
1191
|
+
? "pushing (new)"
|
|
1192
|
+
: "pushing (local newer)"));
|
|
1193
|
+
pushed++;
|
|
1194
|
+
}
|
|
1195
|
+
catch (err) {
|
|
1196
|
+
console.log(chalk.red(` \u2717 ${action.file_path}`), chalk.gray(err.message));
|
|
1197
|
+
errors++;
|
|
1198
|
+
}
|
|
1199
|
+
continue;
|
|
1200
|
+
}
|
|
1201
|
+
if (action.action === "pull") {
|
|
1202
|
+
if (!action.config_id) {
|
|
1203
|
+
console.log(chalk.red(` \u2717 ${action.file_path}`), chalk.gray("missing config ID from server"));
|
|
1204
|
+
errors++;
|
|
1205
|
+
continue;
|
|
1206
|
+
}
|
|
1207
|
+
try {
|
|
1208
|
+
const writePath = resolveWritePath(action.agent, action.file_path, action.scope);
|
|
1209
|
+
if (!writePath) {
|
|
1210
|
+
console.log(chalk.yellow(` ? ${action.file_path}`), chalk.gray(action.scope !== "global" &&
|
|
1211
|
+
action.scope !== currentProjectScope
|
|
1212
|
+
? "different project \u2014 skipped"
|
|
1213
|
+
: "unknown agent \u2014 skipped"));
|
|
1214
|
+
skipped++;
|
|
1215
|
+
continue;
|
|
1216
|
+
}
|
|
1217
|
+
// For new files (not updates), ask user before writing
|
|
1218
|
+
const isNewLocally = action.reason === "cloud_only" && !existsSync(writePath);
|
|
1219
|
+
if (isNewLocally && !options.pull) {
|
|
1220
|
+
console.log(chalk.cyan(` New file: ${action.file_path}`));
|
|
1221
|
+
console.log(chalk.gray(` → ${writePath}`));
|
|
1222
|
+
const accept = await confirmDefault(` Download? [Y/n] `);
|
|
1223
|
+
if (!accept) {
|
|
1224
|
+
console.log(chalk.gray(` - ${action.file_path}`), chalk.gray("skipped"));
|
|
1225
|
+
skipped++;
|
|
1226
|
+
continue;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
const config = await getClient().configs.get(action.config_id);
|
|
1230
|
+
mkdirSync(dirname(writePath), { recursive: true });
|
|
1231
|
+
writeFileSync(writePath, config.content);
|
|
1232
|
+
if (action.version) {
|
|
1233
|
+
ackConfigs.push({
|
|
1234
|
+
agent: action.agent,
|
|
1235
|
+
file_path: action.file_path,
|
|
1236
|
+
scope: action.scope,
|
|
1237
|
+
content_hash: config.content_hash,
|
|
1238
|
+
version: action.version,
|
|
1239
|
+
local_path: writePath,
|
|
1240
|
+
});
|
|
1241
|
+
}
|
|
1242
|
+
console.log(chalk.cyan(` \u2193 ${action.file_path}`), chalk.gray(isNewLocally ? "restored" : "pulling (cloud newer)"));
|
|
1243
|
+
pulled++;
|
|
1244
|
+
}
|
|
1245
|
+
catch (err) {
|
|
1246
|
+
console.log(chalk.red(` \u2717 ${action.file_path}`), chalk.gray(err.message));
|
|
1247
|
+
errors++;
|
|
1248
|
+
}
|
|
1249
|
+
continue;
|
|
1250
|
+
}
|
|
1251
|
+
if (action.action === "delete_local") {
|
|
1252
|
+
if (!options.pull) {
|
|
1253
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
1254
|
+
console.log(chalk.yellow(` - ${action.file_path}`), chalk.gray("cloud deleted this config \u2014 skipped in non-interactive mode"));
|
|
1255
|
+
skipped++;
|
|
1256
|
+
continue;
|
|
1257
|
+
}
|
|
1258
|
+
const resolution = await promptCloudDeletion(action.file_path);
|
|
1259
|
+
if (resolution === "skip") {
|
|
1260
|
+
console.log(chalk.gray(` - ${action.file_path}`), chalk.gray("skipped"));
|
|
1261
|
+
skipped++;
|
|
1262
|
+
continue;
|
|
1263
|
+
}
|
|
1264
|
+
if (resolution === "local") {
|
|
1265
|
+
const local = localByKey.get(key);
|
|
1266
|
+
if (!local) {
|
|
1267
|
+
console.log(chalk.yellow(` - ${action.file_path}`), chalk.gray("local file missing \u2014 skipped"));
|
|
1268
|
+
skipped++;
|
|
1269
|
+
continue;
|
|
1270
|
+
}
|
|
1271
|
+
try {
|
|
1272
|
+
await getClient().configs.upsert({
|
|
1273
|
+
agent: action.agent,
|
|
1274
|
+
file_path: action.file_path,
|
|
1275
|
+
scope: action.scope,
|
|
1276
|
+
content: local.content,
|
|
1277
|
+
device_id: deviceID,
|
|
1278
|
+
local_path: local.loc.path,
|
|
1279
|
+
});
|
|
1280
|
+
console.log(chalk.green(` \u2191 ${action.file_path}`), chalk.gray("kept local and restored to cloud"));
|
|
1281
|
+
pushed++;
|
|
1282
|
+
}
|
|
1283
|
+
catch (err) {
|
|
1284
|
+
console.log(chalk.red(` \u2717 ${action.file_path}`), chalk.gray(err.message));
|
|
1285
|
+
errors++;
|
|
1286
|
+
}
|
|
1287
|
+
continue;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
try {
|
|
1291
|
+
const writePath = resolveWritePath(action.agent, action.file_path, action.scope);
|
|
1292
|
+
if (writePath && existsSync(writePath)) {
|
|
1293
|
+
moveFileToTrash(writePath, "agent-configs");
|
|
1294
|
+
}
|
|
1295
|
+
if (action.version) {
|
|
1296
|
+
ackConfigs.push({
|
|
1297
|
+
agent: action.agent,
|
|
1298
|
+
file_path: action.file_path,
|
|
1299
|
+
scope: action.scope,
|
|
1300
|
+
version: action.version,
|
|
1301
|
+
local_path: writePath ?? undefined,
|
|
1302
|
+
deleted: true,
|
|
1303
|
+
});
|
|
1304
|
+
}
|
|
1305
|
+
console.log(chalk.yellow(` - ${action.file_path}`), chalk.gray("deleted locally (moved to Memax trash)"));
|
|
1306
|
+
deletedLocal++;
|
|
1307
|
+
}
|
|
1308
|
+
catch (err) {
|
|
1309
|
+
console.log(chalk.red(` \u2717 ${action.file_path}`), chalk.gray(err.message));
|
|
1310
|
+
errors++;
|
|
1311
|
+
}
|
|
1312
|
+
continue;
|
|
1313
|
+
}
|
|
1314
|
+
if (action.action === "conflict") {
|
|
1315
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
1316
|
+
console.log(chalk.yellow(` - ${action.file_path}`), chalk.gray("conflict skipped in non-interactive mode"));
|
|
1317
|
+
skipped++;
|
|
1318
|
+
continue;
|
|
1319
|
+
}
|
|
1320
|
+
const resolution = await promptConflict(agent, action.file_path);
|
|
1321
|
+
if (resolution === "local") {
|
|
1322
|
+
const local = localByKey.get(key);
|
|
1323
|
+
if (local) {
|
|
1324
|
+
try {
|
|
1325
|
+
await getClient().configs.upsert({
|
|
1326
|
+
agent: action.agent,
|
|
1327
|
+
file_path: action.file_path,
|
|
1328
|
+
scope: action.scope,
|
|
1329
|
+
content: local.content,
|
|
1330
|
+
device_id: deviceID,
|
|
1331
|
+
local_path: local.loc.path,
|
|
1332
|
+
});
|
|
1333
|
+
console.log(chalk.green(` \u2191 ${action.file_path}`), chalk.gray("kept local"));
|
|
1334
|
+
pushed++;
|
|
1335
|
+
}
|
|
1336
|
+
catch (err) {
|
|
1337
|
+
console.log(chalk.red(` \u2717 ${action.file_path}`), chalk.gray(err.message));
|
|
1338
|
+
errors++;
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
else if (resolution === "cloud" && action.config_id) {
|
|
1343
|
+
try {
|
|
1344
|
+
const writePath = resolveWritePath(action.agent, action.file_path, action.scope);
|
|
1345
|
+
if (writePath) {
|
|
1346
|
+
const config = await getClient().configs.get(action.config_id);
|
|
1347
|
+
mkdirSync(dirname(writePath), { recursive: true });
|
|
1348
|
+
writeFileSync(writePath, config.content);
|
|
1349
|
+
if (action.version) {
|
|
1350
|
+
ackConfigs.push({
|
|
1351
|
+
agent: action.agent,
|
|
1352
|
+
file_path: action.file_path,
|
|
1353
|
+
scope: action.scope,
|
|
1354
|
+
content_hash: config.content_hash,
|
|
1355
|
+
version: action.version,
|
|
1356
|
+
local_path: writePath,
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
console.log(chalk.cyan(` \u2193 ${action.file_path}`), chalk.gray("used cloud"));
|
|
1360
|
+
pulled++;
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
catch (err) {
|
|
1364
|
+
console.log(chalk.red(` \u2717 ${action.file_path}`), chalk.gray(err.message));
|
|
1365
|
+
errors++;
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
else if (resolution === "merge" && action.config_id) {
|
|
1369
|
+
const local = localByKey.get(key);
|
|
1370
|
+
if (local) {
|
|
1371
|
+
try {
|
|
1372
|
+
const cloudConfig = await getClient().configs.get(action.config_id);
|
|
1373
|
+
console.log(chalk.gray(` Merging with LLM...`));
|
|
1374
|
+
const merged = await mergeConfigs(action.agent, action.file_path, local.content, cloudConfig.content);
|
|
1375
|
+
if (merged) {
|
|
1376
|
+
// Write merged to local
|
|
1377
|
+
const writePath = resolveWritePath(action.agent, action.file_path, action.scope);
|
|
1378
|
+
if (writePath) {
|
|
1379
|
+
mkdirSync(dirname(writePath), { recursive: true });
|
|
1380
|
+
writeFileSync(writePath, merged);
|
|
1381
|
+
}
|
|
1382
|
+
// Push merged to cloud
|
|
1383
|
+
await getClient().configs.upsert({
|
|
1384
|
+
agent: action.agent,
|
|
1385
|
+
file_path: action.file_path,
|
|
1386
|
+
scope: action.scope,
|
|
1387
|
+
content: merged,
|
|
1388
|
+
device_id: deviceID,
|
|
1389
|
+
local_path: writePath ?? undefined,
|
|
1390
|
+
});
|
|
1391
|
+
console.log(chalk.magenta(` \u2194 ${action.file_path}`), chalk.gray("merged (LLM)"));
|
|
1392
|
+
pushed++;
|
|
1393
|
+
}
|
|
1394
|
+
else {
|
|
1395
|
+
skipped++;
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
catch (err) {
|
|
1399
|
+
console.log(chalk.red(` \u2717 ${action.file_path}`), chalk.gray(err.message));
|
|
1400
|
+
errors++;
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
else {
|
|
1405
|
+
console.log(chalk.gray(` - ${action.file_path}`), chalk.gray("skipped"));
|
|
1406
|
+
skipped++;
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
if (ackConfigs.length > 0) {
|
|
1412
|
+
try {
|
|
1413
|
+
await getClient().configs.ack({
|
|
1414
|
+
device_id: deviceID,
|
|
1415
|
+
configs: ackConfigs,
|
|
303
1416
|
});
|
|
304
|
-
synced++;
|
|
305
1417
|
}
|
|
306
1418
|
catch (err) {
|
|
307
|
-
|
|
308
|
-
if (msg.includes("unchanged") ||
|
|
309
|
-
msg.includes("duplicate") ||
|
|
310
|
-
msg.includes("exists")) {
|
|
311
|
-
unchanged++;
|
|
312
|
-
}
|
|
313
|
-
else {
|
|
314
|
-
errors++;
|
|
315
|
-
console.log(chalk.red(" Error syncing"), f.label, chalk.gray(msg));
|
|
316
|
-
}
|
|
1419
|
+
console.log(chalk.yellow("\n Warning: failed to persist sync state"), chalk.gray(err.message));
|
|
317
1420
|
}
|
|
318
1421
|
}
|
|
319
|
-
|
|
1422
|
+
// Summary
|
|
1423
|
+
if (pushed === 0 &&
|
|
1424
|
+
pulled === 0 &&
|
|
1425
|
+
unchangedCount === 0 &&
|
|
1426
|
+
skipped === 0 &&
|
|
1427
|
+
errors === 0) {
|
|
1428
|
+
console.log(chalk.gray(" No configs in cloud yet. Push some first from a device that has them.\n"));
|
|
1429
|
+
return;
|
|
1430
|
+
}
|
|
1431
|
+
const parts = [];
|
|
1432
|
+
if (pushed > 0)
|
|
1433
|
+
parts.push(`${pushed} pushed`);
|
|
1434
|
+
if (pulled > 0)
|
|
1435
|
+
parts.push(`${pulled} restored`);
|
|
1436
|
+
if (deletedLocal > 0)
|
|
1437
|
+
parts.push(`${deletedLocal} deleted locally`);
|
|
1438
|
+
if (unchangedCount > 0)
|
|
1439
|
+
parts.push(`${unchangedCount} unchanged`);
|
|
1440
|
+
if (skipped > 0)
|
|
1441
|
+
parts.push(`${skipped} skipped`);
|
|
1442
|
+
if (errors > 0)
|
|
1443
|
+
parts.push(`${errors} errors`);
|
|
1444
|
+
console.log(chalk.bold(`\n Done: ${parts.join(", ")}`));
|
|
1445
|
+
if (pulled > 0) {
|
|
1446
|
+
console.log(chalk.gray(" Restart your agents for restored configs to take effect."));
|
|
1447
|
+
}
|
|
1448
|
+
console.log();
|
|
1449
|
+
}
|
|
1450
|
+
function formatAgentName(id) {
|
|
1451
|
+
const names = {
|
|
1452
|
+
"claude-code": "Claude Code",
|
|
1453
|
+
cursor: "Cursor",
|
|
1454
|
+
codex: "Codex",
|
|
1455
|
+
gemini: "Gemini CLI",
|
|
1456
|
+
copilot: "GitHub Copilot",
|
|
1457
|
+
windsurf: "Windsurf",
|
|
1458
|
+
openclaw: "OpenClaw",
|
|
1459
|
+
opencode: "OpenCode",
|
|
1460
|
+
generic: "Generic",
|
|
1461
|
+
};
|
|
1462
|
+
return names[id] ?? id;
|
|
1463
|
+
}
|
|
1464
|
+
async function promptConflict(_agent, filePath) {
|
|
1465
|
+
const answer = await ask(chalk.yellow(`\n ${filePath} has changes on both sides.\n` +
|
|
1466
|
+
` [l] Keep local [c] Use cloud [m] Merge (LLM) [s] Skip: `));
|
|
1467
|
+
const a = answer.toLowerCase();
|
|
1468
|
+
if (a === "l")
|
|
1469
|
+
return "local";
|
|
1470
|
+
if (a === "c")
|
|
1471
|
+
return "cloud";
|
|
1472
|
+
if (a === "m")
|
|
1473
|
+
return "merge";
|
|
1474
|
+
return "skip";
|
|
1475
|
+
}
|
|
1476
|
+
async function promptCloudDeletion(filePath) {
|
|
1477
|
+
const answer = await ask(chalk.yellow(`\n ${filePath} was deleted in cloud.\n` +
|
|
1478
|
+
` [d] Delete local copy [k] Keep local and restore cloud [s] Skip: `));
|
|
1479
|
+
const normalized = answer.toLowerCase();
|
|
1480
|
+
if (normalized === "d")
|
|
1481
|
+
return "delete";
|
|
1482
|
+
if (normalized === "k")
|
|
1483
|
+
return "local";
|
|
1484
|
+
return "skip";
|
|
1485
|
+
}
|
|
1486
|
+
async function mergeConfigs(agent, filePath, localContent, cloudContent) {
|
|
1487
|
+
try {
|
|
1488
|
+
const result = await getClient().configs.merge({
|
|
1489
|
+
local_content: localContent,
|
|
1490
|
+
cloud_content: cloudContent,
|
|
1491
|
+
file_path: filePath,
|
|
1492
|
+
agent,
|
|
1493
|
+
});
|
|
1494
|
+
return result.merged_content;
|
|
1495
|
+
}
|
|
1496
|
+
catch (err) {
|
|
1497
|
+
console.log(chalk.red(` Merge failed: ${err.message}`));
|
|
1498
|
+
return null;
|
|
1499
|
+
}
|
|
320
1500
|
}
|
|
321
1501
|
//# sourceMappingURL=sync.js.map
|