Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: stand the numCtx migration down when OLLAMA_CONTEXT_LENGTH is already set (#6191)

[._.]/ Adam Eivy99a009af33fc
diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md
index 839b66bb69..475e74f0fb 100644
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@ -151,6 +151,12 @@ next run, because a CLI/TUI harness talks to Ollama directly and nothing else
can raise it. `OLLAMA_CONTEXT_LENGTH` in the environment works as a machine-wide
fallback.
+The shipped `claude-ollama` / `claude-ollama-tui` records already pin 128K, so
+this usually bites the OpenCode Ollama harnesses or a record whose window was
+cleared. Note the precedence: a record's **Local num_ctx** wins over
+`OLLAMA_CONTEXT_LENGTH`, so on a machine where 128K does not fit, lower it on
+the record rather than only in the environment.
+
Two caveats:
- **Check the model still fits.** A larger window means a larger KV cache. Past
diff --git a/scripts/migrations/339-claude-ollama-num-ctx.js b/scripts/migrations/339-claude-ollama-num-ctx.js
index fa331931ea..fcf78fb1cd 100644
--- a/scripts/migrations/339-claude-ollama-num-ctx.js
+++ b/scripts/migrations/339-claude-ollama-num-ctx.js
@@ -15,7 +15,13 @@
* What this migration deliberately does NOT do:
*
* - **It never overwrites a window the user picked.** Only a record with no
- * usable `numCtx` is stamped.
+ * usable `numCtx` is stamped — and the whole migration stands down when
+ * `OLLAMA_CONTEXT_LENGTH` is set, because `resolveOllamaContextLength`
+ * ranks a record's `numCtx` ABOVE that env var. Stamping over a
+ * deliberately small machine-wide window is the one way this migration
+ * could make an install worse: past what VRAM allows Ollama does not fail,
+ * it offloads layers to CPU. For the same reason a small-VRAM install
+ * should lower this value rather than inherit it.
* - **It never touches a record repointed at another binary.** Keyed on the
* `ollamaBacked` marker plus the `claude` command, matching
* `isOllamaBackedProvider` + the harness these defaults describe, so a
@@ -37,6 +43,9 @@ import { readProvidersDoc, writeJsonAtomic } from './_lib.js';
const NUM_CTX = 131072;
const CLAUDE_COMMAND = 'claude';
+// Ollama's own machine-wide knob, spelled out rather than imported: a migration
+// is a frozen snapshot and must not drift when `lib/ollamaContext.js` changes.
+const OLLAMA_CONTEXT_ENV_VAR = 'OLLAMA_CONTEXT_LENGTH';
// Logged relative, never as `doc.path` — that is an absolute path carrying the
// operator's home directory, and a boot line is the wrong place to print it.
@@ -73,7 +82,15 @@ const needsNumCtx = (provider) => {
};
export default {
- async up({ rootDir }) {
+ async up({ rootDir, env = process.env }) {
+ // A set env var is the user having already chosen this machine's window.
+ // Checked before the read so the file is never touched in that case.
+ const ambient = usableNumCtx(env?.[OLLAMA_CONTEXT_ENV_VAR]);
+ if (ambient) {
+ console.log(`🪟 ${OLLAMA_CONTEXT_ENV_VAR}=${ambient} is already set — leaving the Claude Ollama records on that window`);
+ return { ok: true, reason: 'ambient-context-length', updated: 0 };
+ }
+
const doc = await readProvidersDoc({ rootDir });
if (!doc.ok) {
const why = SKIP_REASONS[doc.reason] ?? 'could not be read';
diff --git a/scripts/migrations/339-claude-ollama-num-ctx.test.js b/scripts/migrations/339-claude-ollama-num-ctx.test.js
index 77ab06ec10..bc9533873f 100644
--- a/scripts/migrations/339-claude-ollama-num-ctx.test.js
+++ b/scripts/migrations/339-claude-ollama-num-ctx.test.js
@@ -31,13 +31,13 @@ describe('migration 339 — claude-ollama context window pin', () => {
afterEach(() => rmSync(rootDir, { recursive: true, force: true }));
it('skips a fresh install with no providers.json (data.reference already ships the window)', async () => {
- await expect(migration.up({ rootDir })).resolves.toEqual({ ok: false, reason: 'no-file', updated: 0 });
+ await expect(migration.up({ rootDir, env: {} })).resolves.toEqual({ ok: false, reason: 'no-file', updated: 0 });
});
it('pins 131072 on a record that carries no numCtx', async () => {
writeProviders({ 'claude-ollama': { ...CLAUDE_OLLAMA } });
- await expect(migration.up({ rootDir })).resolves.toEqual({ ok: true, reason: 'updated', updated: 1 });
+ await expect(migration.up({ rootDir, env: {} })).resolves.toEqual({ ok: true, reason: 'updated', updated: 1 });
const provider = readProviders()['claude-ollama'];
expect(provider.numCtx).toBe(131072);
@@ -50,7 +50,7 @@ describe('migration 339 — claude-ollama context window pin', () => {
// every real install is in. Skipping it would make this migration a no-op.
writeProviders({ 'claude-ollama': { ...CLAUDE_OLLAMA, numCtx: null } });
- await expect(migration.up({ rootDir })).resolves.toEqual({ ok: true, reason: 'updated', updated: 1 });
+ await expect(migration.up({ rootDir, env: {} })).resolves.toEqual({ ok: true, reason: 'updated', updated: 1 });
expect(readProviders()['claude-ollama'].numCtx).toBe(131072);
});
@@ -66,7 +66,7 @@ describe('migration 339 — claude-ollama context window pin', () => {
ollama: { id: 'ollama', type: 'api', endpoint: 'http://localhost:11434/v1' },
});
- await expect(migration.up({ rootDir })).resolves.toEqual({ ok: true, reason: 'updated', updated: 3 });
+ await expect(migration.up({ rootDir, env: {} })).resolves.toEqual({ ok: true, reason: 'updated', updated: 3 });
const providers = readProviders();
for (const id of ['claude-ollama', 'claude-ollama-tui', 'claude-ollama-review']) {
@@ -83,7 +83,7 @@ describe('migration 339 — claude-ollama context window pin', () => {
'claude-ollama-tui': { ...CLAUDE_OLLAMA, id: 'claude-ollama-tui', type: 'tui', numCtx: 262144 },
});
- await expect(migration.up({ rootDir })).resolves.toEqual({
+ await expect(migration.up({ rootDir, env: {} })).resolves.toEqual({
ok: true, reason: 'already-current-or-custom', updated: 0,
});
@@ -95,7 +95,7 @@ describe('migration 339 — claude-ollama context window pin', () => {
it('leaves a record the user repointed at a different binary untouched', async () => {
writeProviders({ 'claude-ollama': { ...CLAUDE_OLLAMA, command: 'my-wrapper' } });
- await expect(migration.up({ rootDir })).resolves.toEqual({
+ await expect(migration.up({ rootDir, env: {} })).resolves.toEqual({
ok: true, reason: 'already-current-or-custom', updated: 0,
});
expect(readProviders()['claude-ollama'].numCtx).toBeUndefined();
@@ -103,17 +103,38 @@ describe('migration 339 — claude-ollama context window pin', () => {
it('is a no-op on a second run', async () => {
writeProviders({ 'claude-ollama': { ...CLAUDE_OLLAMA } });
- await migration.up({ rootDir });
+ await migration.up({ rootDir, env: {} });
- await expect(migration.up({ rootDir })).resolves.toEqual({
+ await expect(migration.up({ rootDir, env: {} })).resolves.toEqual({
ok: true, reason: 'already-current-or-custom', updated: 0,
});
});
+ it('stands down when OLLAMA_CONTEXT_LENGTH already names the machine window', async () => {
+ // `resolveOllamaContextLength` ranks a record's numCtx ABOVE the env var, so
+ // stamping here would silently override a window the user chose — and past
+ // what VRAM allows Ollama offloads to CPU rather than failing.
+ writeProviders({ 'claude-ollama': { ...CLAUDE_OLLAMA } });
+
+ await expect(migration.up({ rootDir, env: { OLLAMA_CONTEXT_LENGTH: '32768' } })).resolves.toEqual({
+ ok: true, reason: 'ambient-context-length', updated: 0,
+ });
+ expect(readProviders()['claude-ollama'].numCtx).toBeUndefined();
+ });
+
+ it('ignores a junk or non-positive OLLAMA_CONTEXT_LENGTH and stamps anyway', async () => {
+ writeProviders({ 'claude-ollama': { ...CLAUDE_OLLAMA } });
+
+ await expect(migration.up({ rootDir, env: { OLLAMA_CONTEXT_LENGTH: 'wide' } })).resolves.toEqual({
+ ok: true, reason: 'updated', updated: 1,
+ });
+ expect(readProviders()['claude-ollama'].numCtx).toBe(131072);
+ });
+
it('reports an unreadable providers.json instead of throwing', async () => {
mkdirSync(join(rootDir, 'data'), { recursive: true });
writeFileSync(providersPath, '{ not json');
- await expect(migration.up({ rootDir })).resolves.toEqual({ ok: false, reason: 'unreadable', updated: 0 });
+ await expect(migration.up({ rootDir, env: {} })).resolves.toEqual({ ok: false, reason: 'unreadable', updated: 0 });
});
});