slash-tokens 1.5.2 → 1.5.3

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/dist/cli.js CHANGED
@@ -28,10 +28,21 @@ function getInstance() {
28
28
  _instance = new WebAssembly.Instance(module);
29
29
  return _instance;
30
30
  }
31
+ var MAX_WASM_INPUT_BYTES = 32 * 1024 * 1024;
32
+ var WASM_PAGE_BYTES = 65536;
33
+ function ensureCapacity(memory, neededBytes) {
34
+ const cappedNeeded = Math.min(neededBytes, MAX_WASM_INPUT_BYTES);
35
+ if (memory.buffer.byteLength >= cappedNeeded)
36
+ return;
37
+ const additionalPages = Math.ceil((cappedNeeded - memory.buffer.byteLength) / WASM_PAGE_BYTES);
38
+ if (additionalPages > 0)
39
+ memory.grow(additionalPages);
40
+ }
31
41
  function writeToMemory(content) {
32
42
  const instance = getInstance();
33
43
  const bytes = encoder.encode(content);
34
44
  const memory = instance.exports.memory;
45
+ ensureCapacity(memory, WASM_INPUT_OFFSET + bytes.length + 1);
35
46
  const buffer = new Uint8Array(memory.buffer);
36
47
  const maxLen = Math.min(bytes.length, buffer.length - WASM_INPUT_OFFSET - 1);
37
48
  buffer.set(bytes.subarray(0, maxLen), WASM_INPUT_OFFSET);
package/dist/slash.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getInstance, writeToMemory } from './wasm.js';
1
+ import { getInstance, writeToMemory, ensureCapacity } from './wasm.js';
2
2
  const WASM_INPUT_OFFSET = 4096;
3
3
  /**
4
4
  * Per-model calibration factors.
@@ -140,6 +140,7 @@ export function slashBytes(bytes) {
140
140
  return 0;
141
141
  const instance = getInstance();
142
142
  const memory = instance.exports.memory;
143
+ ensureCapacity(memory, WASM_INPUT_OFFSET + bytes.length + 1);
143
144
  const buffer = new Uint8Array(memory.buffer);
144
145
  const maxLen = Math.min(bytes.length, buffer.length - WASM_INPUT_OFFSET - 1);
145
146
  buffer.set(bytes.subarray(0, maxLen), WASM_INPUT_OFFSET);
package/dist/wasm.d.ts CHANGED
@@ -1,2 +1,18 @@
1
1
  export declare function getInstance(): WebAssembly.Instance;
2
+ /**
3
+ * Grow WASM linear memory if needed so `neededBytes` of input (plus the
4
+ * offset reserved for Zig's stack/globals) fits without truncation.
5
+ *
6
+ * Fixed 2026-08-23: writeToMemory() used to silently truncate any input
7
+ * larger than the WASM module's initial memory (~1.06MB usable) BEFORE
8
+ * estimate_tokens ever ran — no error, no flag, just a token count
9
+ * computed over a truncated prefix. This defeated the "never
10
+ * under-report" invariant exactly where it matters most: calls near a
11
+ * model's real context ceiling. WebAssembly.Memory is growable at
12
+ * runtime (confirmed: `memory.grow()` works on this module, 17 pages ->
13
+ * 117 pages tested directly) — growing does not move or invalidate
14
+ * existing data, it only appends new zeroed pages, so this is safe to
15
+ * do lazily on every call with no effect on correctness for small inputs.
16
+ */
17
+ export declare function ensureCapacity(memory: WebAssembly.Memory, neededBytes: number): void;
2
18
  export declare function writeToMemory(content: string): number;
package/dist/wasm.js CHANGED
@@ -23,10 +23,46 @@ export function getInstance() {
23
23
  _instance = new WebAssembly.Instance(module);
24
24
  return _instance;
25
25
  }
26
+ // Safety cap on how far we'll grow WASM linear memory for one input.
27
+ // 32 MB is a large margin over any real model's context window today
28
+ // (the biggest is 1M tokens, which even for dense multi-byte scripts
29
+ // like CJK doesn't approach 32MB of UTF-8 bytes) — so growth alone
30
+ // eliminates silent truncation for every realistic real-world call.
31
+ // Above this cap we still truncate (see below), but a truncated
32
+ // estimate at 32MB+ of input is still far larger than any model's
33
+ // context ceiling, so the overflow gate still fires correctly either
34
+ // way — the cap exists only to bound memory growth against a
35
+ // pathological/abusive input, not to protect estimate correctness.
36
+ const MAX_WASM_INPUT_BYTES = 32 * 1024 * 1024;
37
+ const WASM_PAGE_BYTES = 65536;
38
+ /**
39
+ * Grow WASM linear memory if needed so `neededBytes` of input (plus the
40
+ * offset reserved for Zig's stack/globals) fits without truncation.
41
+ *
42
+ * Fixed 2026-08-23: writeToMemory() used to silently truncate any input
43
+ * larger than the WASM module's initial memory (~1.06MB usable) BEFORE
44
+ * estimate_tokens ever ran — no error, no flag, just a token count
45
+ * computed over a truncated prefix. This defeated the "never
46
+ * under-report" invariant exactly where it matters most: calls near a
47
+ * model's real context ceiling. WebAssembly.Memory is growable at
48
+ * runtime (confirmed: `memory.grow()` works on this module, 17 pages ->
49
+ * 117 pages tested directly) — growing does not move or invalidate
50
+ * existing data, it only appends new zeroed pages, so this is safe to
51
+ * do lazily on every call with no effect on correctness for small inputs.
52
+ */
53
+ export function ensureCapacity(memory, neededBytes) {
54
+ const cappedNeeded = Math.min(neededBytes, MAX_WASM_INPUT_BYTES);
55
+ if (memory.buffer.byteLength >= cappedNeeded)
56
+ return;
57
+ const additionalPages = Math.ceil((cappedNeeded - memory.buffer.byteLength) / WASM_PAGE_BYTES);
58
+ if (additionalPages > 0)
59
+ memory.grow(additionalPages);
60
+ }
26
61
  export function writeToMemory(content) {
27
62
  const instance = getInstance();
28
63
  const bytes = encoder.encode(content);
29
64
  const memory = instance.exports.memory;
65
+ ensureCapacity(memory, WASM_INPUT_OFFSET + bytes.length + 1);
30
66
  const buffer = new Uint8Array(memory.buffer);
31
67
  const maxLen = Math.min(bytes.length, buffer.length - WASM_INPUT_OFFSET - 1);
32
68
  buffer.set(bytes.subarray(0, maxLen), WASM_INPUT_OFFSET);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slash-tokens",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "Token Optimization for Context Engineers. 4.8 KB WASM. Sub-millisecond. Zero dependencies.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",