Heuristic:0xK3vin MegaMemory Idempotent Installation Marker
| Knowledge Sources | |
|---|---|
| Domains | Developer_Tools, Configuration_Management |
| Last Updated | 2026-10-27 14:00 GMT |
Overview
Mark managed files with a sentinel comment so the installer can safely overwrite them on re-run without clobbering user-customized files.
Description
The MegaMemory installer (`megamemory install`) writes configuration files, skill plugins, and slash commands to editor config directories. To ensure idempotency (running install multiple times is safe), every file the installer creates includes a sentinel marker comment: `MegaMemory-managed file. Safe for megamemory install to update.`
Before overwriting an existing file, the installer checks `isManagedInstallFile(content)`, which tests for the presence of this marker. If the existing file has the marker, it was created by a previous MegaMemory install and can be safely updated. If the file exists but lacks the marker, the installer warns and leaves it untouched — it may be user-customized.
For MCP server config files (JSON/TOML), the installer uses a read-merge-write pattern: it reads the existing config, parses it, injects/updates only the `megamemory` entry, and writes back — preserving all other user configuration.
Usage
Apply this heuristic when:
- Running `megamemory install` multiple times (it is safe and idempotent)
- Writing any tool that modifies user config files — use a sentinel marker
- Re-installing after a MegaMemory upgrade — managed files update, custom files stay
The Insight (Rule of Thumb)
- Action: Embed `MegaMemory-managed file. Safe for megamemory install to update.` as the first line of every managed file.
- Value: Before overwriting, check `content.includes(MANAGED_FILE_MARKER)`. Only overwrite if marker present or file is new.
- Trade-off: A user who manually edits a managed file loses changes on re-install. The alternative (never overwrite) means upgrades don't propagate. The marker signals consent.
- Config files: JSON/TOML configs don't use the marker — instead, merge by key (`mcp.megamemory` or `mcpServers.megamemory`), preserving other entries.
Reasoning
Code agents and editors have complex config ecosystems. Blindly overwriting a user's `opencode.json` or `.claude.json` would destroy their other MCP server configurations, agent settings, and customizations. The read-merge-write pattern ensures only the `megamemory` key is touched.
For non-config files (slash commands, skill plugins), the marker pattern provides a clear contract: "if this marker is present, MegaMemory owns this file and may update it." Without the marker, the file may have been hand-edited by the user, and overwriting it would destroy their work.
The installer also handles the `AGENTS.md`/`CLAUDE.md` instruction files differently: it checks for the `## Project Knowledge Graph` header marker (`AGENTS_MD_MARKER`) and appends the snippet if missing, rather than overwriting the whole file.
Code Evidence
Marker constant from `src/install.ts:52-53`:
const AGENTS_MD_MARKER = "## Project Knowledge Graph";
const MANAGED_FILE_MARKER = "MegaMemory-managed file. Safe for megamemory install to update.";
Marker check from `src/install.ts:179-181`:
function isManagedInstallFile(content: string): boolean {
return content.includes(MANAGED_FILE_MARKER);
}
Safe overwrite logic for tool plugin from `src/install.ts:280-301`:
if (fs.existsSync(dest)) {
const existing = fs.readFileSync(dest, "utf-8");
if (existing === sourceContent) {
skip(`Tool plugin already up to date`);
return;
}
if (!isManagedInstallFile(existing)) {
warn(
`Existing tool plugin at ${pc.dim(dest)} is not marked as MegaMemory-managed. Left it untouched.`
);
return;
}
fs.writeFileSync(dest, sourceContent);
success(`Updated tool plugin at ${pc.dim(dest)}`);
return;
}
Config merge pattern from `src/install.ts:337-364`:
async function setupOpencodeMcpConfig(runtime: CommandRuntime): Promise<void> {
fs.mkdirSync(OPENCODE_CONFIG_DIR, { recursive: true });
const config = readJsonConfigForMerge(OPENCODE_CONFIG_PATH, "mcp");
if (!config["$schema"]) {
config["$schema"] = "https://opencode.ai/config.json";
}
const mcp = isObject(config["mcp"]) ? config["mcp"] : {};
const existed = "megamemory" in mcp;
mcp["megamemory"] = {
type: "local",
command: runtime.opencodeCommand,
enabled: true,
};
config["mcp"] = mcp;
fs.writeFileSync(OPENCODE_CONFIG_PATH, JSON.stringify(config, null, 2) + "\n");
Read-merge safety from `src/install.ts:152-177`:
function readJsonConfigForMerge(
filePath: string,
nestedKey: string
): Record<string, unknown> {
if (!fs.existsSync(filePath)) {
return {};
}
let parsed: unknown;
try {
parsed = parseJsonc(fs.readFileSync(filePath, "utf-8"));
} catch (err) {
const reason = err instanceof Error ? `could not parse JSON (${err.message})` : "could not parse JSON";
throw formatConfigReadError(filePath, reason);
}
if (!isObject(parsed)) {
throw formatConfigReadError(filePath, "top-level JSON value is not an object");
}
if (nestedKey in parsed && !isObject(parsed[nestedKey])) {
throw formatConfigReadError(filePath, `'${nestedKey}' is present but is not an object`);
}
return parsed;
}