Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix opencode duplicate tokens bug (#7792)

Shahmir Varqhabb7e7e01ff18
diff --git a/frontend/package.json b/frontend/package.json
index fb47beed1a..fde18488c7 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -173,7 +173,7 @@
"thememirror": "^2.0.1",
"timestring": "^7.0.0",
"typescript-memoize": "^1.1.1",
- "use-acp": "0.2.5",
+ "use-acp": "0.2.6",
"use-resize-observer": "^9.1.0",
"vega-lite": "6.3.1",
"vega-loader": "^5.1.0",
diff --git a/frontend/src/components/chat/acp/agent-panel.tsx b/frontend/src/components/chat/acp/agent-panel.tsx
index b6def7b337..a4ad43ad3d 100644
--- a/frontend/src/components/chat/acp/agent-panel.tsx
+++ b/frontend/src/components/chat/acp/agent-panel.tsx
@@ -677,7 +677,7 @@ const AgentPanel: React.FC = () => {
? getAgentWebSocketUrl(selectedTab.agentId)
: NO_WS_SET;
const { sendUpdateFile, sendFileDetails } = useRequestClient();
- const isCreatingNewSession = useRef(false);
+ const creatingOrResumingSession = useRef(false);
const acpClient = useAcpClient({
wsUrl,
@@ -716,6 +716,7 @@ const AgentPanel: React.FC = () => {
sessionMode,
activeSessionId,
agent,
+ clearNotifications,
} = acpClient;
useEffect(() => {
@@ -757,40 +758,42 @@ const AgentPanel: React.FC = () => {
return;
}
- // If there is an active session, we should stop it
- if (activeSessionId) {
- setActiveSessionId(null);
- await agent.cancel({ sessionId: activeSessionId }).catch((error) => {
- logger.error("Failed to cancel active session", { error });
- });
- }
+ creatingOrResumingSession.current = true;
+
+ try {
+ // If there is an active session, we should stop it
+ if (activeSessionId) {
+ await agent.cancel({ sessionId: activeSessionId }).catch((error) => {
+ logger.error("Failed to cancel active session", { error });
+ });
+ clearNotifications(activeSessionId);
+ setActiveSessionId(null);
+ }
- // Get the selected model from the current session state
- const currentModel = selectedTab?.selectedModel ?? null;
- logger.debug("Creating new agent session", { model: currentModel });
- isCreatingNewSession.current = true;
- const newSession = await agent
- .newSession({
+ // Get the selected model from the current session state
+ const currentModel = selectedTab?.selectedModel ?? null;
+ logger.debug("Creating new agent session", { model: currentModel });
+ const newSession = await agent.newSession({
cwd: getCwd(),
mcpServers: [],
_meta: currentModel ? { model: currentModel } : undefined,
- })
- .finally(() => {
- isCreatingNewSession.current = false;
});
- // Capture models from the response
- if (newSession.models) {
- logger.debug("Session models received", { models: newSession.models });
- setSessionModels(newSession.models);
- }
+ // Capture models from the response
+ if (newSession.models) {
+ logger.debug("Session models received", { models: newSession.models });
+ setSessionModels(newSession.models);
+ }
- setSessionState((prev) =>
- updateSessionExternalAgentSessionId(
- prev,
- newSession.sessionId as ExternalAgentSessionId,
- ),
- );
+ setSessionState((prev) =>
+ updateSessionExternalAgentSessionId(
+ prev,
+ newSession.sessionId as ExternalAgentSessionId,
+ ),
+ );
+ } finally {
+ creatingOrResumingSession.current = false;
+ }
});
const handleResumeSession = useEvent(
@@ -804,23 +807,28 @@ const AgentPanel: React.FC = () => {
if (!agent.loadSession) {
throw new Error("Agent does not support loading sessions");
}
- const loadedSession = await agent.loadSession({
- sessionId: previousSessionId,
- cwd: getCwd(),
- mcpServers: [],
- });
-
- // Capture models from the response if available
- if (loadedSession?.models) {
- logger.debug("Session models received", {
- models: loadedSession.models,
+ creatingOrResumingSession.current = true;
+ try {
+ const loadedSession = await agent.loadSession({
+ sessionId: previousSessionId,
+ cwd: getCwd(),
+ mcpServers: [],
});
- setSessionModels(loadedSession.models);
- }
- setSessionState((prev) =>
- updateSessionExternalAgentSessionId(prev, previousSessionId),
- );
+ // Capture models from the response if available
+ if (loadedSession?.models) {
+ logger.debug("Session models received", {
+ models: loadedSession.models,
+ });
+ setSessionModels(loadedSession.models);
+ }
+
+ setSessionState((prev) =>
+ updateSessionExternalAgentSessionId(prev, previousSessionId),
+ );
+ } finally {
+ creatingOrResumingSession.current = false;
+ }
},
);
@@ -838,6 +846,11 @@ const AgentPanel: React.FC = () => {
return;
}
+ // Prevent race conditions
+ if (creatingOrResumingSession.current) {
+ return;
+ }
+
// If there is an available session, resume it, otherwise create a new one
const createOrResumeSession = async () => {
const availableSession = tabLastActiveSessionId ?? activeSessionId;
diff --git a/pixi.lock b/pixi.lock
index 3cf28900fb..4aa7786221 100644
--- a/pixi.lock
+++ b/pixi.lock
@@ -119,9 +119,9 @@ environments:
- pypi: https://files.pythonhosted.org/packages/16/32/f8e3c85d1d5250232a5d3477a2a28cc291968ff175caeadaf3cc19ce0e4a/parso-0.8.5-py2.py3-none-any.whl
- pypi: https://files.pythonhosted.org/packages/29/f8/40e01c350ad9a2b3cb4e6adbcc8a83b17ee50dd5792102b6142385937db5/psutil-7.2.1-cp36-abi3-manylinux2010_x86_64.manylinux_2_12_x86_64.manylinux_2_28_x86_64.whl
- pypi: https://files.pythonhosted.org/packages/ea/10/47caf89cbb52e5bb764696fd52a8c591a2f0e851a93270c05a17f36000b5/pymdown_extensions-10.20-py3-none-any.whl
- - pypi: https://files.pythonhosted.org/packages/d9/52/1064f510b141bd54025f9b55105e26d1fa970b9be67ad766380a3c9b74b0/starlette-0.50.0-py3-none-any.whl
+ - pypi: https://files.pythonhosted.org/packages/18/c4/09985a03dba389d4fe16a9014147a7b02fa76ef3519bf5846462a485876d/starlette-0.51.0-py3-none-any.whl
- pypi: https://files.pythonhosted.org/packages/3d/d8/2083a1daa7439a66f3a48589a57d576aa117726762618f6bb09fe3798796/uvicorn-0.40.0-py3-none-any.whl
- - pypi: https://files.pythonhosted.org/packages/14/8f/aa61f528fba38578ec553c145857a181384c72b98156f858ca5c8e82d9d3/websockets-15.0.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
+ - pypi: https://files.pythonhosted.org/packages/18/29/71729b4671f21e1eaa5d6573031ab810ad2936c8175f03f97f3ff164c802/websockets-16.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
- pypi: ./
osx-arm64:
- conda: https://conda.anaconda.org/conda-forge/noarch/anyio-4.12.0-pyhcf101f3_0.conda
@@ -217,9 +217,9 @@ environments:
- pypi: https://files.pythonhosted.org/packages/16/32/f8e3c85d1d5250232a5d3477a2a28cc291968ff175caeadaf3cc19ce0e4a/parso-0.8.5-py2.py3-none-any.whl
- pypi: https://files.pythonhosted.org/packages/c5/2c/78e4a789306a92ade5000da4f5de3255202c534acdadc3aac7b5458fadef/psutil-7.2.1-cp36-abi3-macosx_11_0_arm64.whl
- pypi: https://files.pythonhosted.org/packages/ea/10/47caf89cbb52e5bb764696fd52a8c591a2f0e851a93270c05a17f36000b5/pymdown_extensions-10.20-py3-none-any.whl
- - pypi: https://files.pythonhosted.org/packages/d9/52/1064f510b141bd54025f9b55105e26d1fa970b9be67ad766380a3c9b74b0/starlette-0.50.0-py3-none-any.whl
+ - pypi: https://files.pythonhosted.org/packages/18/c4/09985a03dba389d4fe16a9014147a7b02fa76ef3519bf5846462a485876d/starlette-0.51.0-py3-none-any.whl
- pypi: https://files.pythonhosted.org/packages/3d/d8/2083a1daa7439a66f3a48589a57d576aa117726762618f6bb09fe3798796/uvicorn-0.40.0-py3-none-any.whl
- - pypi: https://files.pythonhosted.org/packages/3d/69/1a681dd6f02180916f116894181eab8b2e25b31e484c5d0eae637ec01f7c/websockets-15.0.1-cp312-cp312-macosx_11_0_arm64.whl
+ - pypi: https://files.pythonhosted.org/packages/40/1e/9771421ac2286eaab95b8575b0cb701ae3663abf8b5e1f64f1fd90d0a673/websockets-16.0-cp312-cp312-macosx_11_0_arm64.whl
- pypi: ./
packages:
- conda: https://conda.anaconda.org/conda-forge/linux-64/_libgcc_mutex-0.1-conda_forge.tar.bz2
@@ -1132,8 +1132,8 @@ packages:
timestamp: 1727801725384
- pypi: ./
name: marimo
- version: 0.19.0
- sha256: e3b89cad4941115582da49edd3ebfebc3505ef26c2462d9e226798d3e4d59d6c
+ version: 0.19.2
+ sha256: 90b3f019966fb4460ac05e6237db94ab2ca00f5a5f27aedaf5b41d4b4e109df2
requires_dist:
- click>=8.0,<9
- jedi>=0.18.0
@@ -1169,6 +1169,7 @@ packages:
- mcp>=1.0.0 ; extra == 'mcp'
- pydantic>2 ; extra == 'mcp'
requires_python: '>=3.10'
+ editable: true
- pypi: https://files.pythonhosted.org/packages/70/81/54e3ce63502cd085a0c556652a4e1b919c45a446bd1e5300e10c44c8c521/markdown-3.10-py3-none-any.whl
name: markdown
version: '3.10'
@@ -1792,10 +1793,10 @@ packages:
- pkg:pypi/sniffio?source=compressed-mapping
size: 15698
timestamp: 1762941572482
-- pypi: https://files.pythonhosted.org/packages/d9/52/1064f510b141bd54025f9b55105e26d1fa970b9be67ad766380a3c9b74b0/starlette-0.50.0-py3-none-any.whl
+- pypi: https://files.pythonhosted.org/packages/18/c4/09985a03dba389d4fe16a9014147a7b02fa76ef3519bf5846462a485876d/starlette-0.51.0-py3-none-any.whl
name: starlette
- version: 0.50.0
- sha256: 9e5391843ec9b6e472eed1365a78c8098cfceb7a74bfd4d6b1c0c0095efb3bca
+ version: 0.51.0
+ sha256: fb460a3d6fd3c958d729fdd96aee297f89a51b0181f16401fe8fd4cb6129165d
requires_dist:
- anyio>=3.6.2,<5
- typing-extensions>=4.10.0 ; python_full_version < '3.13'
@@ -1995,16 +1996,16 @@ packages:
- pkg:pypi/virtualenv?source=hash-mapping
size: 4401341
timestamp: 1761726489722
-- pypi: https://files.pythonhosted.org/packages/14/8f/aa61f528fba38578ec553c145857a181384c72b98156f858ca5c8e82d9d3/websockets-15.0.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
+- pypi: https://files.pythonhosted.org/packages/18/29/71729b4671f21e1eaa5d6573031ab810ad2936c8175f03f97f3ff164c802/websockets-16.0-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
name: websockets
- version: 15.0.1
- sha256: 64dee438fed052b52e4f98f76c5790513235efaa1ef7f3f2192c392cd7c91b65
- requires_python: '>=3.9'
-- pypi: https://files.pythonhosted.org/packages/3d/69/1a681dd6f02180916f116894181eab8b2e25b31e484c5d0eae637ec01f7c/websockets-15.0.1-cp312-cp312-macosx_11_0_arm64.whl
+ version: '16.0'
+ sha256: 9b5aca38b67492ef518a8ab76851862488a478602229112c4b0d58d63a7a4d5c
+ requires_python: '>=3.10'
+- pypi: https://files.pythonhosted.org/packages/40/1e/9771421ac2286eaab95b8575b0cb701ae3663abf8b5e1f64f1fd90d0a673/websockets-16.0-cp312-cp312-macosx_11_0_arm64.whl
name: websockets
- version: 15.0.1
- sha256: 0701bc3cfcb9164d04a14b149fd74be7347a530ad3bbf15ab2c678a2cd3dd9a2
- requires_python: '>=3.9'
+ version: '16.0'
+ sha256: 86890e837d61574c92a97496d590968b23c2ef0aeb8a9bc9421d174cd378ae39
+ requires_python: '>=3.10'
- conda: https://conda.anaconda.org/conda-forge/noarch/wheel-0.45.1-pyhd8ed1ab_1.conda
sha256: 1b34021e815ff89a4d902d879c3bd2040bc1bd6169b32e9427497fa05c55f1ce
md5: 75cb7132eb58d97896e173ef12ac9986
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ea1eeb4893..42566c05ee 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -494,8 +494,8 @@ importers:
specifier: ^1.1.1
version: 1.1.1
use-acp:
- specifier: 0.2.5
+ specifier: 0.2.6
use-resize-observer:
specifier: ^9.1.0
@@ -9800,8 +9800,8 @@ packages:
resolution: {integrity: sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==}
- resolution: {integrity: sha512-4TUEb4dlNzv1oOb0LboB5zYTNHBY6wSXS/3QCa1Fe9jLJ5x8xVY5rA+89FSzCnlDnd7XTwkLMJgaSXsQOOkoDA==}
+ resolution: {integrity: sha512-2D1vX0MSDIgx0f8bQp5s4ToVuWAZq1qKNyoNwXfag+1wKdIDCuRrVX0OehwRymCdv5Lmr4mJXji7ATxOnbXgvA==}
peerDependencies:
react: ^19.2.3
react-dom: ^19.2.3
@@ -21528,7 +21528,7 @@ snapshots:
querystringify: 2.2.0
requires-port: 1.0.0
dependencies:
'@agentclientprotocol/sdk': 0.4.9
react: 19.2.3