Skip to content

Commit 3126894

Browse files
Extract collection performance refactors from mobile stack (#2854)
Co-authored-by: codex <codex@users.noreply.github.com>
1 parent 6b3050e commit 3126894

47 files changed

Lines changed: 646 additions & 455 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/server/src/auth/Layers/AuthControlPlane.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,16 @@ export const makeAuthControlPlane = Effect.gen(function* () {
6464

6565
const listPairingLinks: AuthControlPlaneShape["listPairingLinks"] = (input) =>
6666
bootstrapCredentials.listActive().pipe(
67-
Effect.map((pairingLinks) =>
68-
pairingLinks
69-
.filter((pairingLink) => (input?.role ? pairingLink.role === input.role : true))
70-
.filter((pairingLink) => !input?.excludeSubjects?.includes(pairingLink.subject))
71-
.map((pairingLink) =>
67+
Effect.map((pairingLinks) => {
68+
const activeLinks: Array<AuthPairingLink> = [];
69+
for (const pairingLink of pairingLinks) {
70+
if (input?.role && pairingLink.role !== input.role) {
71+
continue;
72+
}
73+
if (input?.excludeSubjects?.includes(pairingLink.subject)) {
74+
continue;
75+
}
76+
activeLinks.push(
7277
pairingLink.label
7378
? ({
7479
id: pairingLink.id,
@@ -87,11 +92,12 @@ export const makeAuthControlPlane = Effect.gen(function* () {
8792
createdAt: pairingLink.createdAt,
8893
expiresAt: pairingLink.expiresAt,
8994
} satisfies AuthPairingLink),
90-
)
91-
.toSorted(
92-
(left, right) => right.createdAt.epochMilliseconds - left.createdAt.epochMilliseconds,
93-
),
94-
),
95+
);
96+
}
97+
return activeLinks.toSorted(
98+
(left, right) => right.createdAt.epochMilliseconds - left.createdAt.epochMilliseconds,
99+
);
100+
}),
95101
Effect.mapError(toAuthControlPlaneError("Failed to list pairing links.")),
96102
);
97103

apps/server/src/git/remoteRefs.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
export function parseRemoteNamesInGitOrder(stdout: string): ReadonlyArray<string> {
2-
return stdout
3-
.split("\n")
4-
.map((line) => line.trim())
5-
.filter((line) => line.length > 0);
2+
const remoteNames: Array<string> = [];
3+
for (const line of stdout.split("\n")) {
4+
const remoteName = line.trim();
5+
if (remoteName.length > 0) {
6+
remoteNames.push(remoteName);
7+
}
8+
}
9+
return remoteNames;
610
}
711

812
export function parseRemoteNames(stdout: string): ReadonlyArray<string> {

apps/server/src/imageMime.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ export function parseBase64DataUrl(
3535
const match = /^data:([^,]+),([a-z0-9+/=\r\n ]+)$/i.exec(dataUrl.trim());
3636
if (!match) return null;
3737

38-
const headerParts = (match[1] ?? "")
39-
.split(";")
40-
.map((part) => part.trim())
41-
.filter((part) => part.length > 0);
38+
const headerParts: Array<string> = [];
39+
for (const part of (match[1] ?? "").split(";")) {
40+
const trimmed = part.trim();
41+
if (trimmed.length > 0) {
42+
headerParts.push(trimmed);
43+
}
44+
}
4245
if (headerParts.length < 2) {
4346
return null;
4447
}

apps/server/src/keybindings.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import * as Layer from "effect/Layer";
3232
import * as Option from "effect/Option";
3333
import * as Predicate from "effect/Predicate";
3434
import * as PubSub from "effect/PubSub";
35+
import * as Result from "effect/Result";
3536
import * as Schema from "effect/Schema";
3637
import * as SchemaIssue from "effect/SchemaIssue";
3738
import * as SchemaTransformation from "effect/SchemaTransformation";
@@ -538,9 +539,11 @@ const makeKeybindings = Effect.gen(function* () {
538539
return;
539540
}
540541

541-
const matchingDefaults = DEFAULT_KEYBINDINGS.filter((defaultRule) =>
542-
customConfig.some((entry) => isSameKeybindingRule(entry, defaultRule)),
543-
).map((rule) => rule.command);
542+
const matchingDefaults = Array.filterMap(DEFAULT_KEYBINDINGS, (defaultRule) =>
543+
customConfig.some((entry) => isSameKeybindingRule(entry, defaultRule))
544+
? Result.succeed(defaultRule.command)
545+
: Result.failVoid,
546+
);
544547
if (matchingDefaults.length > 0) {
545548
yield* Effect.logWarning("default keybinding rule already defined in user config", {
546549
path: keybindingsConfigPath,

apps/server/src/orchestration/Layers/CheckpointReactor.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
CommandId,
3+
type CheckpointRef,
34
EventId,
45
MessageId,
56
type ProjectId,
@@ -701,9 +702,12 @@ const make = Effect.gen(function* () {
701702
});
702703
}
703704

704-
const staleCheckpointRefs = thread.checkpoints
705-
.filter((checkpoint) => checkpoint.checkpointTurnCount > event.payload.turnCount)
706-
.map((checkpoint) => checkpoint.checkpointRef);
705+
const staleCheckpointRefs: Array<CheckpointRef> = [];
706+
for (const checkpoint of thread.checkpoints) {
707+
if (checkpoint.checkpointTurnCount > event.payload.turnCount) {
708+
staleCheckpointRefs.push(checkpoint.checkpointRef);
709+
}
710+
}
707711

708712
if (staleCheckpointRefs.length > 0) {
709713
yield* checkpointStore.deleteCheckpointRefs({

apps/server/src/orchestration/Layers/ProjectionPipeline.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,15 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
537537
projectionPendingApprovalRepository.listByThreadId({ threadId }),
538538
]);
539539

540-
const latestUserMessageAt =
541-
messages
542-
.filter((message) => message.role === "user")
543-
.map((message) => message.createdAt)
544-
.toSorted()
545-
.at(-1) ?? null;
540+
let latestUserMessageAt: string | null = null;
541+
for (const message of messages) {
542+
if (
543+
message.role === "user" &&
544+
(latestUserMessageAt === null || message.createdAt > latestUserMessageAt)
545+
) {
546+
latestUserMessageAt = message.createdAt;
547+
}
548+
}
546549

547550
const pendingApprovalCount = pendingApprovals.filter(
548551
(approval) => approval.status === "pending",

apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import {
2424
ProjectId,
2525
ThreadId,
2626
} from "@t3tools/contracts";
27+
import * as Arr from "effect/Array";
2728
import * as Effect from "effect/Effect";
2829
import * as Layer from "effect/Layer";
2930
import * as Option from "effect/Option";
31+
import * as Result from "effect/Result";
3032
import * as Schema from "effect/Schema";
3133
import * as Struct from "effect/Struct";
3234
import * as SqlClient from "effect/unstable/sql/SqlClient";
@@ -1488,34 +1490,36 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
14881490

14891491
const snapshot = {
14901492
snapshotSequence: computeSnapshotSequence(stateRows),
1491-
projects: projectRows
1492-
.filter((row) => row.deletedAt === null)
1493-
.map((row) =>
1494-
mapProjectShellRow(row, repositoryIdentities.get(row.projectId) ?? null),
1495-
),
1496-
threads: threadRows
1497-
.filter((row) => row.deletedAt === null)
1498-
.map(
1499-
(row): OrchestrationThreadShell => ({
1500-
id: row.threadId,
1501-
projectId: row.projectId,
1502-
title: row.title,
1503-
modelSelection: row.modelSelection,
1504-
runtimeMode: row.runtimeMode,
1505-
interactionMode: row.interactionMode,
1506-
branch: row.branch,
1507-
worktreePath: row.worktreePath,
1508-
latestTurn: latestTurnByThread.get(row.threadId) ?? null,
1509-
createdAt: row.createdAt,
1510-
updatedAt: row.updatedAt,
1511-
archivedAt: row.archivedAt,
1512-
session: sessionByThread.get(row.threadId) ?? null,
1513-
latestUserMessageAt: row.latestUserMessageAt,
1514-
hasPendingApprovals: row.pendingApprovalCount > 0,
1515-
hasPendingUserInput: row.pendingUserInputCount > 0,
1516-
hasActionableProposedPlan: row.hasActionableProposedPlan > 0,
1517-
}),
1518-
),
1493+
projects: Arr.filterMap(projectRows, (row) =>
1494+
row.deletedAt === null
1495+
? Result.succeed(
1496+
mapProjectShellRow(row, repositoryIdentities.get(row.projectId) ?? null),
1497+
)
1498+
: Result.failVoid,
1499+
),
1500+
threads: Arr.filterMap(threadRows, (row) =>
1501+
row.deletedAt === null
1502+
? Result.succeed({
1503+
id: row.threadId,
1504+
projectId: row.projectId,
1505+
title: row.title,
1506+
modelSelection: row.modelSelection,
1507+
runtimeMode: row.runtimeMode,
1508+
interactionMode: row.interactionMode,
1509+
branch: row.branch,
1510+
worktreePath: row.worktreePath,
1511+
latestTurn: latestTurnByThread.get(row.threadId) ?? null,
1512+
createdAt: row.createdAt,
1513+
updatedAt: row.updatedAt,
1514+
archivedAt: row.archivedAt,
1515+
session: sessionByThread.get(row.threadId) ?? null,
1516+
latestUserMessageAt: row.latestUserMessageAt,
1517+
hasPendingApprovals: row.pendingApprovalCount > 0,
1518+
hasPendingUserInput: row.pendingUserInputCount > 0,
1519+
hasActionableProposedPlan: row.hasActionableProposedPlan > 0,
1520+
} satisfies OrchestrationThreadShell)
1521+
: Result.failVoid,
1522+
),
15191523
updatedAt: updatedAt ?? "1970-01-01T00:00:00.000Z",
15201524
};
15211525

@@ -1621,11 +1625,13 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
16211625

16221626
const snapshot = {
16231627
snapshotSequence: computeSnapshotSequence(stateRows),
1624-
projects: projectRows
1625-
.filter((row) => row.deletedAt === null && activeProjectIds.has(row.projectId))
1626-
.map((row) =>
1627-
mapProjectShellRow(row, repositoryIdentities.get(row.projectId) ?? null),
1628-
),
1628+
projects: Arr.filterMap(projectRows, (row) =>
1629+
row.deletedAt === null && activeProjectIds.has(row.projectId)
1630+
? Result.succeed(
1631+
mapProjectShellRow(row, repositoryIdentities.get(row.projectId) ?? null),
1632+
)
1633+
: Result.failVoid,
1634+
),
16291635
threads: threadRows.map(
16301636
(row): OrchestrationThreadShell => ({
16311637
id: row.threadId,

apps/server/src/provider/Layers/ClaudeAdapter.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,13 @@ function toProcessError(
245245
function normalizeClaudeStreamMessages(
246246
cause: Cause.Cause<{ readonly message: string }>,
247247
): ReadonlyArray<string> {
248-
const errors = Cause.prettyErrors(cause)
249-
.map((error) => error.message.trim())
250-
.filter((message) => message.length > 0);
248+
const errors: Array<string> = [];
249+
for (const error of Cause.prettyErrors(cause)) {
250+
const message = error.message.trim();
251+
if (message.length > 0) {
252+
errors.push(message);
253+
}
254+
}
251255
if (errors.length > 0) {
252256
return errors;
253257
}

apps/server/src/provider/Layers/ClaudeProvider.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,13 @@ export function resolveClaudeApiModelId(modelSelection: ModelSelection): string
317317
}
318318

319319
function toTitleCaseWords(value: string): string {
320-
return value
321-
.split(/[\s_-]+/g)
322-
.filter(Boolean)
323-
.map((part) => part[0]!.toUpperCase() + part.slice(1).toLowerCase())
324-
.join(" ");
320+
const parts: Array<string> = [];
321+
for (const part of value.split(/[\s_-]+/g)) {
322+
if (part.length > 0) {
323+
parts.push(part[0]!.toUpperCase() + part.slice(1).toLowerCase());
324+
}
325+
}
326+
return parts.join(" ");
325327
}
326328

327329
function claudeSubscriptionLabel(subscriptionType: string | undefined): string | undefined {

apps/server/src/provider/Layers/CodexProvider.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,21 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
319319
} satisfies CodexAppServerProviderSnapshot;
320320
});
321321

322-
const emptyCodexModelsFromSettings = (codexSettings: CodexSettings): ServerProvider["models"] =>
323-
codexSettings.customModels
324-
.map((model) => model.trim())
325-
.filter((model, index, models) => model.length > 0 && models.indexOf(model) === index)
326-
.map((model) => ({
327-
slug: model,
328-
name: model,
329-
isCustom: true,
330-
capabilities: null,
331-
}));
322+
const emptyCodexModelsFromSettings = (codexSettings: CodexSettings): ServerProvider["models"] => {
323+
const models = new Set<string>();
324+
for (const model of codexSettings.customModels) {
325+
const trimmed = model.trim();
326+
if (trimmed.length > 0) {
327+
models.add(trimmed);
328+
}
329+
}
330+
return Array.from(models, (model) => ({
331+
slug: model,
332+
name: model,
333+
isCustom: true,
334+
capabilities: null,
335+
}));
336+
};
332337

333338
const makePendingCodexProvider = (
334339
codexSettings: CodexSettings,

0 commit comments

Comments
 (0)