@juspay/neurolink 11.18.0 → 11.18.1

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/CHANGELOG.md CHANGED
@@ -1,9 +1,8 @@
1
- ## [11.18.0](https://github.com/juspay/neurolink/compare/v11.17.3...v11.18.0) (2026-08-22)
1
+ ## [11.18.1](https://github.com/juspay/neurolink/compare/v11.18.0...v11.18.1) (2026-08-22)
2
2
 
3
- ### Features
3
+ ### Bug Fixes
4
4
 
5
- - **(proxy):** add the Gemini CLI door ([9f754a9](https://github.com/juspay/neurolink/commit/9f754a9fec229c42033d35be034a75fbf037f292))
6
- - **(proxy):** attribute usage to the CLI that spent it ([d13d210](https://github.com/juspay/neurolink/commit/d13d21012086f2fb588b7e9df9b5d4ea9cee7496))
5
+ - **(proxy):** write client configs atomically ([49032fc](https://github.com/juspay/neurolink/commit/49032fc5b1df7b90bfda013d9be71423e358001e))
7
6
 
8
7
  ## [11.2.3](https://github.com/juspay/neurolink/compare/v11.2.2...v11.2.3) (2026-08-19)
9
8
 
@@ -8,7 +8,7 @@
8
8
  import { homedir } from "os";
9
9
  import { join } from "path";
10
10
  import { logger } from "../../utils/logger.js";
11
- import { isProxyOwnedValue, shouldCaptureSnapshot } from "./snapshot.js";
11
+ import { isProxyOwnedValue, shouldCaptureSnapshot, writeFileAtomic, } from "./snapshot.js";
12
12
  /**
13
13
  * Resolved per call rather than at module load so `detect()` and `apply()`
14
14
  * agree when HOME changes — under test, and on the `--dev` isolation path.
@@ -63,7 +63,7 @@ export async function setClaudeProxySettings(baseUrl) {
63
63
  ANTHROPIC_BASE_URL: baseUrl,
64
64
  ENABLE_TOOL_SEARCH: "true",
65
65
  };
66
- fs.writeFileSync(getClaudeSettingsPath(), JSON.stringify(settings, null, 2));
66
+ await writeFileAtomic(getClaudeSettingsPath(), JSON.stringify(settings, null, 2));
67
67
  }
68
68
  export async function clearClaudeProxySettings(expectedBaseUrl) {
69
69
  const fs = await import("fs");
@@ -128,7 +128,7 @@ export async function clearClaudeProxySettings(expectedBaseUrl) {
128
128
  else {
129
129
  settings.env = env;
130
130
  }
131
- fs.writeFileSync(getClaudeSettingsPath(), JSON.stringify(settings, null, 2));
131
+ await writeFileAtomic(getClaudeSettingsPath(), JSON.stringify(settings, null, 2));
132
132
  return hadBaseUrl || hadToolSearch;
133
133
  }
134
134
  export const claudeCodeConfigurator = {
@@ -7,6 +7,7 @@
7
7
  import { homedir } from "os";
8
8
  import { join } from "path";
9
9
  import { logger } from "../../utils/logger.js";
10
+ import { writeFileAtomic } from "./snapshot.js";
10
11
  //
11
12
  // Points the Codex CLI at the proxy by managing `~/.codex/config.toml`:
12
13
  // - appends a marker-delimited `[model_providers.neurolink]` table
@@ -92,7 +93,7 @@ export async function setCodexProxySettings(baseUrl) {
92
93
  ? providerMatch[0]
93
94
  : null;
94
95
  fs.mkdirSync(join(homedir(), ".neurolink"), { recursive: true });
95
- fs.writeFileSync(getCodexSnapshotPath(), JSON.stringify({ originalProviderLine }, null, 2), { mode: 0o600 });
96
+ await writeFileAtomic(getCodexSnapshotPath(), JSON.stringify({ originalProviderLine }, null, 2), 0o600);
96
97
  }
97
98
  let text = stripCodexManagedConfig(original);
98
99
  // Set the selector: replace an existing top-level model_provider or insert
@@ -113,7 +114,7 @@ export async function setCodexProxySettings(baseUrl) {
113
114
  text = `model_provider = "neurolink"\n${text}`;
114
115
  }
115
116
  const trimmed = text.replace(/\s*$/, "\n");
116
- fs.writeFileSync(getCodexConfigPath(), `${trimmed}\n${buildCodexProviderBlock(baseUrl)}`);
117
+ await writeFileAtomic(getCodexConfigPath(), `${trimmed}\n${buildCodexProviderBlock(baseUrl)}`);
117
118
  return true;
118
119
  }
119
120
  catch (error) {
@@ -182,7 +183,7 @@ export async function clearCodexProxySettings(expectedBaseUrl) {
182
183
  }
183
184
  return false;
184
185
  }
185
- fs.writeFileSync(getCodexConfigPath(), text.replace(/\s*$/, "\n"));
186
+ await writeFileAtomic(getCodexConfigPath(), text.replace(/\s*$/, "\n"));
186
187
  try {
187
188
  fs.rmSync(getCodexSnapshotPath(), { force: true });
188
189
  }
@@ -24,6 +24,7 @@
24
24
  import { homedir } from "os";
25
25
  import { join } from "path";
26
26
  import { logger } from "../../utils/logger.js";
27
+ import { writeFileAtomic } from "./snapshot.js";
27
28
  /**
28
29
  * Resolved per call rather than at module load so `detect()` and `apply()`
29
30
  * agree when HOME changes — under test, and on the `--dev` isolation path.
@@ -61,12 +62,11 @@ export async function setCopilotProxySettings(baseUrl, proxyKey) {
61
62
  try {
62
63
  const envPath = getCopilotEnvPath();
63
64
  fs.mkdirSync(join(homedir(), ".neurolink"), { recursive: true });
64
- fs.writeFileSync(envPath, buildCopilotEnvScript(baseUrl, proxyKey || "neurolink-proxy"), { mode: 0o600 });
65
- // The mode option above only applies when open() creates the file. On an
66
- // overwrite it is ignored, so a pre-existing file keeps whatever
67
- // permissions it had which for a file holding a proxy key could be
68
- // world-readable. Enforce it on both paths.
69
- fs.chmodSync(envPath, 0o600);
65
+ // 0600 is applied to the temp file before the rename, so the script never
66
+ // exists at the destination with wider permissions not even briefly.
67
+ // writeFileSync's own mode option would not do this: it applies only when
68
+ // open() creates the file, so an overwrite kept whatever mode was there.
69
+ await writeFileAtomic(envPath, buildCopilotEnvScript(baseUrl, proxyKey || "neurolink-proxy"), 0o600);
70
70
  return true;
71
71
  }
72
72
  catch (error) {
@@ -7,7 +7,7 @@
7
7
  import { homedir } from "os";
8
8
  import { join } from "path";
9
9
  import { logger } from "../../utils/logger.js";
10
- import { cloneForSnapshot, isProxyOwnedValue, shouldCaptureSnapshot, } from "./snapshot.js";
10
+ import { cloneForSnapshot, isProxyOwnedValue, shouldCaptureSnapshot, writeFileAtomic, } from "./snapshot.js";
11
11
  function getOpenCodeConfigDir() {
12
12
  // OpenCode resolves this with the unmodified `xdg-basedir` package —
13
13
  // `XDG_CONFIG_HOME || ~/.config` — on every platform, macOS included. There
@@ -82,7 +82,7 @@ export async function setOpenCodeProxySettings(baseUrl, proxyKey) {
82
82
  provider.neurolink = block;
83
83
  config[OPENCODE_WRITTEN_KEY] = cloneForSnapshot(block);
84
84
  config.provider = provider;
85
- fs.writeFileSync(getOpenCodeConfigPath(), JSON.stringify(config, null, 2));
85
+ await writeFileAtomic(getOpenCodeConfigPath(), JSON.stringify(config, null, 2));
86
86
  return true;
87
87
  }
88
88
  export async function clearOpenCodeProxySettings(expectedBaseUrl) {
@@ -146,7 +146,7 @@ export async function clearOpenCodeProxySettings(expectedBaseUrl) {
146
146
  return false;
147
147
  }
148
148
  config.provider = provider;
149
- fs.writeFileSync(getOpenCodeConfigPath(), JSON.stringify(config, null, 2));
149
+ await writeFileAtomic(getOpenCodeConfigPath(), JSON.stringify(config, null, 2));
150
150
  return hadNeurolink;
151
151
  }
152
152
  /**
@@ -20,7 +20,7 @@
20
20
  import { homedir } from "os";
21
21
  import { join } from "path";
22
22
  import { logger } from "../../utils/logger.js";
23
- import { cloneForSnapshot, isProxyOwnedValue, shouldCaptureSnapshot, } from "./snapshot.js";
23
+ import { cloneForSnapshot, isProxyOwnedValue, shouldCaptureSnapshot, writeFileAtomic, } from "./snapshot.js";
24
24
  /**
25
25
  * Resolved per call rather than at module load so `detect()` and `apply()`
26
26
  * agree when HOME changes — under test, and on the `--dev` isolation path.
@@ -87,7 +87,7 @@ export async function setQwenProxySettings(baseUrl, proxyKey) {
87
87
  security.auth = auth;
88
88
  settings.security = security;
89
89
  settings[QWEN_WRITTEN_KEY] = cloneForSnapshot(auth);
90
- fs.writeFileSync(getQwenSettingsPath(), JSON.stringify(settings, null, 2));
90
+ await writeFileAtomic(getQwenSettingsPath(), JSON.stringify(settings, null, 2));
91
91
  return true;
92
92
  }
93
93
  export async function clearQwenProxySettings(expectedBaseUrl) {
@@ -133,7 +133,7 @@ export async function clearQwenProxySettings(expectedBaseUrl) {
133
133
  delete settings[QWEN_ORIGINAL_KEY];
134
134
  delete settings[QWEN_WRITTEN_KEY];
135
135
  settings.security = security;
136
- fs.writeFileSync(getQwenSettingsPath(), JSON.stringify(settings, null, 2));
136
+ await writeFileAtomic(getQwenSettingsPath(), JSON.stringify(settings, null, 2));
137
137
  return true;
138
138
  }
139
139
  export const qwenCodeConfigurator = {
@@ -50,3 +50,54 @@ export declare function isProxyOwnedValue(args: {
50
50
  written: unknown;
51
51
  current: unknown;
52
52
  }): boolean;
53
+ /**
54
+ * Replace a file's contents without ever exposing a partial one.
55
+ *
56
+ * `writeFileSync` opens with `O_TRUNC`, so from the truncate until the last
57
+ * byte lands the user's config is short — and a real config spans several
58
+ * syscalls, not an instant. Anything reading concurrently, the CLI the config
59
+ * belongs to included, can load a truncated file; a crash in that window leaves
60
+ * it truncated permanently. Both Qwen and OpenCode keep live API keys there.
61
+ *
62
+ * Writing to a sibling temp file and renaming closes it: `rename(2)` within a
63
+ * directory is atomic, so a reader sees either the whole old file or the whole
64
+ * new one. The temp file must be a sibling — a rename across filesystems is a
65
+ * copy, which reintroduces exactly the window this removes.
66
+ *
67
+ * PERMISSIONS ARE THE SUBTLE PART, and getting them wrong here leaks API keys.
68
+ *
69
+ * Writing through a temp file changes who decides the destination's mode. A
70
+ * plain `writeFileSync` over an existing file leaves that file's mode alone, so
71
+ * a config the user had locked to 0600 stayed 0600. A rename replaces the inode,
72
+ * so the destination inherits the TEMP file's mode instead — and a temp file
73
+ * created without an explicit mode lands at 0666 minus umask, i.e. 0644 on a
74
+ * default system. Left unhandled, making the write atomic would have quietly
75
+ * widened every credential file it touched from 0600 to 0644.
76
+ *
77
+ * So when the caller does not specify a mode, the destination's current mode is
78
+ * carried over, which reproduces `writeFileSync`'s behaviour exactly; a file
79
+ * that does not exist yet starts at 0600 rather than whatever umask allows.
80
+ *
81
+ * The mode is applied at CREATE time, not after. `writeFileSync` followed by
82
+ * `chmodSync` would put the credential bytes on disk at 0644 first and tighten
83
+ * them a moment later — a window a local reader can win. The trailing `chmod`
84
+ * remains only to pin the exact mode, since umask masks the create mode; by
85
+ * then the file has never been readable more widely than its final mode.
86
+ *
87
+ * On Windows the guarantee holds but the failure mode differs, which is worth
88
+ * stating because the obvious worry there is the wrong one. Node's `renameSync`
89
+ * is `MoveFileExW` with `MOVEFILE_REPLACE_EXISTING`; replacing a file on the
90
+ * same volume is an atomic directory-entry update, so a concurrent reader still
91
+ * sees the whole old file or the whole new one and never a torn one. What
92
+ * Windows adds is that the rename can *fail* — `EPERM`/`EBUSY` when a reader
93
+ * holds the destination open — where POSIX would succeed. That path is safe:
94
+ * the catch below removes the temp file and rethrows, leaving the previous
95
+ * config intact for the caller to report on.
96
+ *
97
+ * The sibling rule above is what makes that true. `MOVEFILE_COPY_ALLOWED` is
98
+ * also set, so a cross-volume rename silently degrades to copy-then-delete and
99
+ * is NOT atomic. Moving the temp file to `os.tmpdir()` would look like a
100
+ * tidy-up and would quietly restore the exact window this function exists to
101
+ * close, on Windows only, where nobody here would see it.
102
+ */
103
+ export declare function writeFileAtomic(filePath: string, contents: string, mode?: number): Promise<void>;
@@ -95,4 +95,101 @@ export function isProxyOwnedValue(args) {
95
95
  }
96
96
  return valuesMatch(args.current, args.written);
97
97
  }
98
+ /** Distinguishes concurrent writers within one process. */
99
+ let atomicWriteCounter = 0;
100
+ /**
101
+ * Replace a file's contents without ever exposing a partial one.
102
+ *
103
+ * `writeFileSync` opens with `O_TRUNC`, so from the truncate until the last
104
+ * byte lands the user's config is short — and a real config spans several
105
+ * syscalls, not an instant. Anything reading concurrently, the CLI the config
106
+ * belongs to included, can load a truncated file; a crash in that window leaves
107
+ * it truncated permanently. Both Qwen and OpenCode keep live API keys there.
108
+ *
109
+ * Writing to a sibling temp file and renaming closes it: `rename(2)` within a
110
+ * directory is atomic, so a reader sees either the whole old file or the whole
111
+ * new one. The temp file must be a sibling — a rename across filesystems is a
112
+ * copy, which reintroduces exactly the window this removes.
113
+ *
114
+ * PERMISSIONS ARE THE SUBTLE PART, and getting them wrong here leaks API keys.
115
+ *
116
+ * Writing through a temp file changes who decides the destination's mode. A
117
+ * plain `writeFileSync` over an existing file leaves that file's mode alone, so
118
+ * a config the user had locked to 0600 stayed 0600. A rename replaces the inode,
119
+ * so the destination inherits the TEMP file's mode instead — and a temp file
120
+ * created without an explicit mode lands at 0666 minus umask, i.e. 0644 on a
121
+ * default system. Left unhandled, making the write atomic would have quietly
122
+ * widened every credential file it touched from 0600 to 0644.
123
+ *
124
+ * So when the caller does not specify a mode, the destination's current mode is
125
+ * carried over, which reproduces `writeFileSync`'s behaviour exactly; a file
126
+ * that does not exist yet starts at 0600 rather than whatever umask allows.
127
+ *
128
+ * The mode is applied at CREATE time, not after. `writeFileSync` followed by
129
+ * `chmodSync` would put the credential bytes on disk at 0644 first and tighten
130
+ * them a moment later — a window a local reader can win. The trailing `chmod`
131
+ * remains only to pin the exact mode, since umask masks the create mode; by
132
+ * then the file has never been readable more widely than its final mode.
133
+ *
134
+ * On Windows the guarantee holds but the failure mode differs, which is worth
135
+ * stating because the obvious worry there is the wrong one. Node's `renameSync`
136
+ * is `MoveFileExW` with `MOVEFILE_REPLACE_EXISTING`; replacing a file on the
137
+ * same volume is an atomic directory-entry update, so a concurrent reader still
138
+ * sees the whole old file or the whole new one and never a torn one. What
139
+ * Windows adds is that the rename can *fail* — `EPERM`/`EBUSY` when a reader
140
+ * holds the destination open — where POSIX would succeed. That path is safe:
141
+ * the catch below removes the temp file and rethrows, leaving the previous
142
+ * config intact for the caller to report on.
143
+ *
144
+ * The sibling rule above is what makes that true. `MOVEFILE_COPY_ALLOWED` is
145
+ * also set, so a cross-volume rename silently degrades to copy-then-delete and
146
+ * is NOT atomic. Moving the temp file to `os.tmpdir()` would look like a
147
+ * tidy-up and would quietly restore the exact window this function exists to
148
+ * close, on Windows only, where nobody here would see it.
149
+ */
150
+ export async function writeFileAtomic(filePath, contents, mode) {
151
+ const fs = await import("fs");
152
+ const { dirname, join, basename } = await import("path");
153
+ atomicWriteCounter += 1;
154
+ const tempPath = join(dirname(filePath), `.${basename(filePath)}.neurolink-${process.pid}-${atomicWriteCounter}.tmp`);
155
+ // Which step failed changes what the user should do about it: a failed write
156
+ // is usually a missing directory or a full disk and the config is untouched,
157
+ // while a failed rename is a locked destination and the config is intact but
158
+ // stale. The bare errno is the same shape for both, so the stage is recorded
159
+ // as it advances and named in the rethrow.
160
+ let stage = "write";
161
+ // Resolved before the first byte is written — see the permissions note above.
162
+ let effectiveMode = mode;
163
+ if (effectiveMode === undefined) {
164
+ try {
165
+ effectiveMode = fs.statSync(filePath).mode & 0o777;
166
+ }
167
+ catch {
168
+ effectiveMode = 0o600;
169
+ }
170
+ }
171
+ try {
172
+ fs.writeFileSync(tempPath, contents, { mode: effectiveMode });
173
+ stage = "chmod";
174
+ fs.chmodSync(tempPath, effectiveMode);
175
+ stage = "rename";
176
+ fs.renameSync(tempPath, filePath);
177
+ }
178
+ catch (error) {
179
+ // Never leave scratch in the user's config directory.
180
+ try {
181
+ fs.rmSync(tempPath, { force: true });
182
+ }
183
+ catch {
184
+ // best effort
185
+ }
186
+ const reason = error instanceof Error ? error.message : String(error);
187
+ // The original is attached as `cause`, not discarded: wrapping moves the
188
+ // errno off the thrown object, and `cause` is where anything that needs
189
+ // ENOENT/EPERM finds it. No caller reads it today — every call site either
190
+ // lets this propagate or swallows it — so nothing breaks, but a future one
191
+ // should not have to re-derive the syscall from a string.
192
+ throw new Error(`atomic write to ${filePath} failed at the ${stage} step: ${reason}`, { cause: error });
193
+ }
194
+ }
98
195
  //# sourceMappingURL=snapshot.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juspay/neurolink",
3
- "version": "11.18.0",
3
+ "version": "11.18.1",
4
4
  "packageManager": "pnpm@10.15.1",
5
5
  "description": "TypeScript AI SDK with 24+ LLM providers behind one consistent API. MCP-native (connect any MCP server), voice TTS/STT/realtime, RAG, agents, memory, context compaction. OpenAI · Anthropic · Gemini · Bedrock · Azure · Ollama · DeepSeek · NVIDIA NIM and more.",
6
6
  "author": {