The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 18:30 UTC
perf(server): uploading a large document with seven notebooks finishes 1.8x faster (median 823 ms -> 461 ms) (#33946)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Andrei committed
Nov 13, 2026 at 07:03 UTC
e253b88371496d8d2137762de6cc61b4f41bcb2b
2 files changed
+119
-4
apps/server/src/provider/DocumentText.ts
+10
-4
@@ -1,6 +1,7 @@
1
// @effect-diagnostics nodeBuiltinImport:off -- the isolated parser process is an external runtime boundary.
2
import * as NodeChildProcess from "node:child_process";
3
import * as NodeURL from "node:url";
4
+import * as Cache from "effect/Cache";
5
import * as Clock from "effect/Clock";
6
import * as Context from "effect/Context";
7
import * as Effect from "effect/Effect";
@@ -48,13 +49,17 @@ const make = Effect.gen(function* () {
49
const isExecutable = yield* HostProcess.IsExecutable;
50
const environment = yield* HostProcess.Environment;
51
const clock = yield* Clock.Clock;
51
- const lock = yield* Semaphore.make(1);
52
+ // A large DOCX worker peaks near 270 MiB RSS, so two slots bound parser memory near 540 MiB.
53
+ const lock = yield* Semaphore.make(2);
54
const failed = new Map<string, number>();
55
+ // A cancelled extraction can still be publishing while its retry runs in the other slot.
56
+ let attempt = 0;
57
// In a bundle import.meta.url is bin.mjs; development and tests use this source module.
58
const entry = NodeURL.fileURLToPath(
59
new URL(import.meta.url.endsWith(".ts") ? "../bin.ts" : "./bin.mjs", import.meta.url),
60
);
57
- const extract = Effect.fn("DocumentText.extract")(function* (path: string, extension: string) {
61
+ const extract = Effect.fn("DocumentText.extract")(function* (path: string) {
62
+ const extension = path.split(".").at(-1)!.toLowerCase();
63
const sidecar = `${path}.txt`;
64
if (yield* fs.exists(sidecar)) return sidecar;
65
if ((failed.get(path) ?? 0) > clock.currentTimeMillisUnsafe()) return;
@@ -94,7 +99,7 @@ const make = Effect.gen(function* () {
99
return Effect.logWarning("Document text extraction failed.", { extension });
100
}),
101
);
97
- const pending = `${sidecar}.part`;
102
+ const pending = `${sidecar}.${attempt++}.part`;
103
yield* fs
104
.writeFileString(pending, content)
105
.pipe(
@@ -104,13 +109,14 @@ const make = Effect.gen(function* () {
109
);
110
return sidecar;
111
}, lock.withPermits(1));
112
+ const extracting = yield* Cache.make({ capacity: Infinity, timeToLive: 0, lookup: extract });
113
const ensure = Effect.fn("DocumentText.ensure")(
114
function* (path: string) {
115
const extension = path.split(".").at(-1)?.toLowerCase();
116
if (!extension || !extensions.has(extension)) return;
117
const sidecar = `${path}.txt`;
118
if (yield* fs.exists(sidecar)) return sidecar;
113
- return yield* extract(path, extension);
119
+ return yield* Cache.get(extracting, path);
120
},
121
Effect.orElseSucceed(() => undefined),
122
);
apps/server/src/provider/userInputAttachments.test.ts
+109
@@ -7,6 +7,7 @@ import { describe, expect, it } from "@effect/vitest";
7
import * as Effect from "effect/Effect";
8
import * as Deferred from "effect/Deferred";
9
import * as Fiber from "effect/Fiber";
10
+import * as Queue from "effect/Queue";
11
import * as FileSystem from "effect/FileSystem";
12
import * as Layer from "effect/Layer";
13
import * as TestClock from "effect/testing/TestClock";
@@ -88,6 +89,114 @@ describe("question answer paths", () => {
89
}).pipe(Effect.provide(layer)),
90
);
91
92
+ it.effect.each(["interrupted", "failed"])(
93
+ "extracts two documents at once and frees a slot when one is %s",
94
+ (outcome) =>
95
+ Effect.gen(function* () {
96
+ const config = yield* ServerConfig.ServerConfig;
97
+ const fs = yield* FileSystem.FileSystem;
98
+ const [first, second, third] = ["first", "second", "third"].map((name) =>
99
+ NodePath.join(config.attachmentsDir, `${name}.rtf`),
100
+ );
101
+ NodeFS.writeFileSync(first!, String.raw`{\rtf1 First}`);
102
+ NodeFS.writeFileSync(second!, "invalid document");
103
+ NodeFS.writeFileSync(third!, String.raw`{\rtf1 Third}`);
104
+ const started = yield* Queue.unbounded<string>();
105
+ const releases = new Map<string, Deferred.Deferred<void>>();
106
+ for (const path of [first!, second!, third!])
107
+ releases.set(path, yield* Deferred.make<void>());
108
+ yield* Effect.gen(function* () {
109
+ const documents = yield* DocumentText.DocumentText;
110
+ const leaving = yield* documents.ensure(first!).pipe(Effect.forkChild);
111
+ const staying = yield* documents.ensure(first!).pipe(Effect.forkChild);
112
+ const freed = yield* documents.ensure(second!).pipe(Effect.forkChild);
113
+ expect(new Set([yield* Queue.take(started), yield* Queue.take(started)])).toEqual(
114
+ new Set([first, second]),
115
+ );
116
+ const waiting = yield* documents.ensure(third!).pipe(Effect.forkChild);
117
+ yield* Fiber.interrupt(leaving);
118
+ if (outcome === "interrupted") yield* Fiber.interrupt(freed);
119
+ else {
120
+ yield* Deferred.succeed(releases.get(second!)!, undefined);
121
+ expect(yield* Fiber.join(freed)).toBeUndefined();
122
+ }
123
+ expect(yield* Queue.take(started)).toBe(third);
124
+ yield* Deferred.succeed(releases.get(first!)!, undefined);
125
+ yield* Deferred.succeed(releases.get(third!)!, undefined);
126
+ expect(yield* Fiber.join(staying)).toBe(first + ".txt");
127
+ expect(yield* Fiber.join(waiting)).toBe(third + ".txt");
128
+ expect(yield* Queue.size(started)).toBe(0);
129
+ expect(NodeFS.existsSync(second + ".txt")).toBe(false);
130
+ expect(
131
+ NodeFS.readdirSync(config.attachmentsDir).filter((file) => file.endsWith(".part")),
132
+ ).toEqual([]);
133
+ }).pipe(
134
+ Effect.provide(Layer.fresh(DocumentText.layer)),
135
+ Effect.provideService(FileSystem.FileSystem, {
136
+ ...fs,
137
+ stat: (file) =>
138
+ Effect.gen(function* () {
139
+ yield* Queue.offer(started, file);
140
+ yield* Deferred.await(releases.get(file)!);
141
+ return yield* fs.stat(file);
142
+ }),
143
+ }),
144
+ );
145
+ }).pipe(Effect.provide(layer)),
146
+ );
147
+
148
+ it.effect("retries a document while its cancelled extraction is still publishing", () =>
149
+ Effect.gen(function* () {
150
+ const config = yield* ServerConfig.ServerConfig;
151
+ const fs = yield* FileSystem.FileSystem;
152
+ const path = NodePath.join(config.attachmentsDir, "retried.rtf");
153
+ NodeFS.writeFileSync(path, String.raw`{\rtf1 Retried}`);
154
+ const writing = yield* Deferred.make<void>();
155
+ const writeReleased = yield* Deferred.make<void>();
156
+ const renaming = yield* Deferred.make<void>();
157
+ const renameReleased = yield* Deferred.make<void>();
158
+ let writes = 0;
159
+ let renames = 0;
160
+ yield* Effect.gen(function* () {
161
+ const documents = yield* DocumentText.DocumentText;
162
+ const cancelled = yield* documents.ensure(path).pipe(Effect.forkChild);
163
+ yield* Deferred.await(writing);
164
+ const interrupting = yield* Fiber.interrupt(cancelled).pipe(Effect.forkChild);
165
+ const retry = yield* documents.ensure(path).pipe(Effect.forkChild);
166
+ yield* Deferred.await(renaming);
167
+ yield* Deferred.succeed(writeReleased, undefined);
168
+ yield* Fiber.join(interrupting);
169
+ yield* Deferred.succeed(renameReleased, undefined);
170
+ expect(yield* Fiber.join(retry)).toBe(path + ".txt");
171
+ expect(NodeFS.readFileSync(path + ".txt", "utf8")).toBe("Retried");
172
+ expect(
173
+ NodeFS.readdirSync(config.attachmentsDir).filter((file) => file.endsWith(".part")),
174
+ ).toEqual([]);
175
+ }).pipe(
176
+ Effect.provide(Layer.fresh(DocumentText.layer)),
177
+ Effect.provideService(FileSystem.FileSystem, {
178
+ ...fs,
179
+ writeFileString: (file, data) =>
180
+ Effect.gen(function* () {
181
+ if (++writes === 1) {
182
+ yield* Deferred.succeed(writing, undefined);
183
+ yield* Deferred.await(writeReleased);
184
+ }
185
+ return yield* fs.writeFileString(file, data);
186
+ }),
187
+ rename: (from, to) =>
188
+ Effect.gen(function* () {
189
+ if (++renames === 1) {
190
+ yield* Deferred.succeed(renaming, undefined);
191
+ yield* Deferred.await(renameReleased);
192
+ }
193
+ return yield* fs.rename(from, to);
194
+ }),
195
+ }),
196
+ );
197
+ }).pipe(Effect.provide(layer)),
198
+ );
199
+
200
it.effect.each(["spawn", "read"])("retries after a transient %s failure expires", (failure) =>
201
Effect.acquireUseRelease(
202
Effect.promise(async () => {