The best way to control Claude Code, Codex and more from any computer or phone
perf(cursor): long command output streams without copying its tail per chunk (#26934)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Hieu Le committed
Nov 14, 2026 at 11:31 UTC
6be1bd64722f048ec79c2c7d033fc1359381f6aa
2 files changed
+161
-4
packages/provider-cursor/src/server/adapter.test.ts
+151
@@ -1726,6 +1726,157 @@ describe("CursorAdapterV2", () => {
1726
),
1727
);
1728
1729
+ it.effect("keeps the last 256 KiB of streamed shell output when the result carries none", () =>
1730
+ Effect.gen(function* () {
1731
+ const fileSystem = yield* FileSystem.FileSystem;
1732
+ const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-v2-shell-" });
1733
+ const instanceId = ProviderInstanceId.make("cursor");
1734
+ const threadId = ThreadId.make("cursor-shell-thread");
1735
+ const modelSelection = { instanceId, model: "composer-2.5" };
1736
+ const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1737
+ runtimeMode: "full-access",
1738
+ interactionMode: "default",
1739
+ cwd: workspace,
1740
+ });
1741
+ const chunks = Array.from({ length: 3000 }, (_, index) => `${index}`.padEnd(255, ".") + "\n");
1742
+ const shell = {
1743
+ type: "shell" as const,
1744
+ args: { command: "yes", workingDirectory: workspace },
1745
+ };
1746
+ const adapter = yield* makeCursorAdapterV2({
1747
+ instanceId,
1748
+ environment: { HOME: workspace },
1749
+ }).pipe(
1750
+ Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, {
1751
+ assertComplete: Effect.void,
1752
+ open: () =>
1753
+ Effect.succeed({
1754
+ agentId: "native-cursor-shell",
1755
+ listMessages: Effect.succeed([]),
1756
+ close: Effect.void,
1757
+ send: (input) =>
1758
+ Effect.gen(function* () {
1759
+ yield* input.onDelta!({
1760
+ type: "tool-call-started",
1761
+ modelCallId: "model-call",
1762
+ callId: "shell-call",
1763
+ toolCall: shell,
1764
+ });
1765
+ for (const chunk of chunks) {
1766
+ yield* input.onDelta!({
1767
+ type: "shell-output-delta",
1768
+ event: { case: "stdout", value: { data: chunk }, toolCallId: "shell-call" },
1769
+ });
1770
+ }
1771
+ yield* input.onDelta!({
1772
+ type: "tool-call-completed",
1773
+ modelCallId: "model-call",
1774
+ callId: "shell-call",
1775
+ toolCall: {
1776
+ ...shell,
1777
+ result: {
1778
+ status: "success",
1779
+ value: {
1780
+ exitCode: 0,
1781
+ signal: "",
1782
+ stdout: "",
1783
+ stderr: "",
1784
+ executionTime: 0,
1785
+ },
1786
+ },
1787
+ },
1788
+ });
1789
+ return {
1790
+ agentId: "native-cursor-shell",
1791
+ runId: "native-cursor-run",
1792
+ wait: Effect.succeed({
1793
+ id: "native-cursor-run",
1794
+ requestId: "native-request",
1795
+ status: "finished" as const,
1796
+ durationMs: 1,
1797
+ }),
1798
+ cancel: Effect.void,
1799
+ };
1800
+ }).pipe(Effect.orDie),
1801
+ }),
1802
+ }),
1803
+ Effect.provide(TestProviderHost.layer({ cwd: workspace })),
1804
+ );
1805
+ const runtime = yield* adapter.openSession({
1806
+ threadId,
1807
+ providerSessionId: ProviderSessionId.make("cursor-shell-session"),
1808
+ modelSelection,
1809
+ runtimePolicy,
1810
+ });
1811
+ const providerThread = yield* runtime.ensureThread({
1812
+ threadId,
1813
+ modelSelection,
1814
+ runtimePolicy,
1815
+ });
1816
+ const now = yield* DateTime.now;
1817
+ yield* runtime.startTurn({
1818
+ threadId,
1819
+ providerThread,
1820
+ modelSelection,
1821
+ runtimePolicy,
1822
+ runId: RunId.make("cursor-shell-run"),
1823
+ runOrdinal: 1,
1824
+ providerTurnOrdinal: 1,
1825
+ attemptId: RunAttemptId.make("cursor-shell-attempt"),
1826
+ rootNodeId: NodeId.make("cursor-shell-root"),
1827
+ appThread: {
1828
+ id: threadId,
1829
+ projectId: ProjectId.make("cursor-shell-project"),
1830
+ createdBy: "user",
1831
+ creationSource: "web",
1832
+ title: "Cursor shell",
1833
+ providerInstanceId: instanceId,
1834
+ modelSelection,
1835
+ runtimeMode: "full-access",
1836
+ interactionMode: "default",
1837
+ branch: null,
1838
+ worktreePath: null,
1839
+ activeProviderThreadId: providerThread.id,
1840
+ lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
1841
+ forkedFrom: null,
1842
+ createdAt: now,
1843
+ updatedAt: now,
1844
+ archivedAt: null,
1845
+ settledOverride: null,
1846
+ settledAt: null,
1847
+ lastVisitedAt: null,
1848
+ deletedAt: null,
1849
+ },
1850
+ message: {
1851
+ messageId: MessageId.make("cursor-shell-message"),
1852
+ createdBy: "user",
1853
+ creationSource: "web",
1854
+ text: "hi",
1855
+ attachments: [],
1856
+ },
1857
+ });
1858
+ const events = yield* runtime.events.pipe(
1859
+ Stream.takeUntil((event) => event.type === "turn.terminal"),
1860
+ Stream.runCollect,
1861
+ );
1862
+ const output = events
1863
+ .flatMap((event) =>
1864
+ event.type === "turn_item.updated" &&
1865
+ event.turnItem.type === "command_execution" &&
1866
+ event.turnItem.status === "completed"
1867
+ ? [event.turnItem.output]
1868
+ : [],
1869
+ )
1870
+ .at(-1);
1871
+ assert.equal(output, chunks.join("").slice(-256 * 1024));
1872
+ }).pipe(
1873
+ Effect.scoped,
1874
+ Effect.provide(
1875
+ Layer.mergeAll(NodeServices.layer, IdAllocator.layer, McpProviderSessions.layer),
1876
+ ),
1877
+ ),
1878
+ );
1879
+
1880
it.effect("names a custom model Cursor's account catalog rejects", () =>
1881
Effect.gen(function* () {
1882
const fileSystem = yield* FileSystem.FileSystem;
packages/provider-cursor/src/server/adapter.ts
+10
-4
@@ -1364,7 +1364,9 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1364
| "completedAt"
1365
| "updatedAt"
1366
>;
1367
- const outputText = cursorToolOutputText(toolCall) || active.streamedOutput;
1367
+ const outputText =
1368
+ cursorToolOutputText(toolCall) ||
1369
+ active.streamedOutput.slice(-MAX_CURSOR_STREAMED_OUTPUT_CHARS);
1370
let turnItem: OrchestrationV2TurnItem;
1371
switch (toolCall.type) {
1372
case "shell":
@@ -2112,9 +2114,13 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2114
const chunk = shellOutputText(update.event);
2115
if (chunk.length === 0) return;
2116
// Persist output once; live hub deltas avoid quadratic event-store growth.
2115
- shell.streamedOutput = (shell.streamedOutput + chunk).slice(
2116
- -MAX_CURSOR_STREAMED_OUTPUT_CHARS,
2117
- );
2117
+ shell.streamedOutput += chunk;
2118
+ // Trimming on every chunk would flatten and copy the whole tail each time.
2119
+ if (shell.streamedOutput.length > 2 * MAX_CURSOR_STREAMED_OUTPUT_CHARS) {
2120
+ shell.streamedOutput = shell.streamedOutput.slice(
2121
+ -MAX_CURSOR_STREAMED_OUTPUT_CHARS,
2122
+ );
2123
+ }
2124
yield* emitProviderEvent({
2125
type: "command_output.delta",
2126
driver: CursorAgentSdk.CURSOR_PROVIDER,