rohitg00/agentmemory · warning
Could not update ~/.agentmemory/.env. Set AGENTMEMORY_INJECT
Error message
Could not update ~/.agentmemory/.env. Set AGENTMEMORY_INJECT_CONTEXT=true there to enable it.
What it means
During onboarding, when the user opts into context injection, agentmemory tries to write AGENTMEMORY_INJECT_CONTEXT=true into ~/.agentmemory/.env via enableInjectContextInEnv(). If that file cannot be read or written (permissions, lock, or malformed content), onboarding warns that it could not persist the setting and context injection remains off. The feature is not enabled until the variable exists in that env file.
Source
Thrown at src/cli/onboarding.ts:298
});
if (p.isCancel(enable)) {
p.cancel("Setup cancelled. Re-run any time with: agentmemory --reset");
process.exit(0);
}
p.log.info(
"Cost note: injection spends session tokens proportional to tool-call frequency. Default is off.",
);
writePrefs({ injectContextChosen: true });
if (enable === true) {
const wrote = enableInjectContextInEnv(envPath);
if (wrote) {
p.log.success("Context injection enabled (AGENTMEMORY_INJECT_CONTEXT=true).");
} else {
p.log.warn(
"Could not update ~/.agentmemory/.env. Set AGENTMEMORY_INJECT_CONTEXT=true there to enable it.",
);
}
} else {
p.log.info("Context injection left off. Set AGENTMEMORY_INJECT_CONTEXT=true later to enable.");
}
}
async function wireSelectedAgents(agents: string[]): Promise<void> {
p.note("Wire selected agents now?", "next step");
const confirmed = await p.confirm({
message: "Run `agentmemory connect <agent>` for each selected agent now? [Y/n]",
initialValue: true,
});
if (p.isCancel(confirmed) || confirmed === false) {
const cmds = agents.map((a) => ` agentmemory connect ${a}`);
p.note(["Wire later with:", ...cmds].join("\n"), "later");View on GitHub (pinned to e04ba88819)
Solutions
- Manually add AGENTMEMORY_INJECT_CONTEXT=true to ~/.agentmemory/.env (or create the file if missing).
- Fix permissions on ~/.agentmemory/.env (e.g. chown to your user, chmod u+w) and re-run onboarding.
- Re-run onboarding without sudo so the file is written as the normal user.
- Export AGENTMEMORY_INJECT_CONTEXT=true in your shell profile as a fallback if the env file cannot be used.
Example fix
// before: onboarding fails to write sudo agentmemory onboarding # .env owned by root // after sudo chown $(whoami) ~/.agentmemory/.env agentmemory onboarding # writes AGENTMEMORY_INJECT_CONTEXT=true successfully
Defensive patterns
Strategy: validation
Validate before calling
import { accessSync, constants } from "node:fs";
const envPath = `${process.env.HOME}/.agentmemory/.env`;
try {
accessSync(envPath, constants.W_OK);
} catch {
console.warn("~/.agentmemory/.env not writable; set AGENTMEMORY_INJECT_CONTEXT=true manually.");
} Prevention
- Never run onboarding with sudo; keep ~/.agentmemory owned by your user.
- Check .env writability before enabling context injection.
- Keep AGENTMEMORY_INJECT_CONTEXT=true in shell profile as backup.
When it happens
Trigger: Running `agentmemory` onboarding (runOnboarding → maybePromptContextInjection) and answering yes to enable context injection while enableInjectContextInEnv() returns false — e.g. ~/.agentmemory/.env is read-only, owned by another user, or unwritable.
Common situations: Home directory permissions changed after running the installer with sudo; .env file owned by root; disk full; .agentmemory directory created by a different user; corporate-managed machines with restricted write access.
Related errors
- Wiring ${name}… no adapter available (skipped).
- mem::smart-search: AGENTMEMORY_AGENT_SCOPE=isolated is set b
- Unknown tool: ${toolName} (local fallback supports only ${[.
- ${envName} must be a positive integer, got: ${override}
- COHERE_API_KEY is required
AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30).
Data as JSON: /api/errors/8bbd91d3d6790c54.
Report an issue: GitHub.