Skip to content

Commit e6839bc

Browse files
committed
merge: bring main into terminology branch
2 parents 071ab37 + b7d33af commit e6839bc

113 files changed

Lines changed: 5265 additions & 636 deletions

File tree

Some content is hidden

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

‎.github/workflows/release-desktop.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ jobs:
478478
shell: bash
479479
run: |
480480
sudo apt-get update
481-
sudo apt-get install -y libsecret-1-dev pkg-config
481+
sudo apt-get install -y libsecret-1-dev pkg-config libgtk-3-0t64 libnss3 libasound2t64 libgbm1
482482
if ! command -v magick >/dev/null 2>&1 && ! command -v convert >/dev/null 2>&1; then
483483
sudo apt-get install -y imagemagick
484484
fi

‎apps/desktop/src/electron/ElectronShell.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,67 @@ describe("ElectronShell", () => {
5454
}).pipe(Effect.provide(ElectronShell.layer)),
5555
);
5656

57+
it.effect("opens local editor file deep links", () =>
58+
Effect.gen(function* () {
59+
openExternalMock.mockResolvedValue(undefined);
60+
61+
const urls = [
62+
"cursor://file/Users/me/repo/file.md:186",
63+
"vscode://file/Users/me/repo/file.md:3:7",
64+
"vscode-insiders://file/Users/me/repo/file.md",
65+
"vscodium://file/C:/Users/me/repo/file.md:3",
66+
];
67+
const electronShell = yield* ElectronShell.ElectronShell;
68+
const results = yield* Effect.forEach(urls, (url) => electronShell.openExternal(url));
69+
70+
assert.deepEqual(results, [true, true, true, true]);
71+
assert.deepEqual(
72+
openExternalMock.mock.calls,
73+
urls.map((url) => [url]),
74+
);
75+
}).pipe(Effect.provide(ElectronShell.layer)),
76+
);
77+
78+
it.effect("normalizes the scheme case of local editor file deep links", () =>
79+
Effect.gen(function* () {
80+
openExternalMock.mockResolvedValue(undefined);
81+
82+
const electronShell = yield* ElectronShell.ElectronShell;
83+
const result = yield* electronShell.openExternal("VSCode://file/Users/me/repo/file.md:3");
84+
85+
assert.equal(result, true);
86+
assert.deepEqual(openExternalMock.mock.calls, [["vscode://file/Users/me/repo/file.md:3"]]);
87+
}).pipe(Effect.provide(ElectronShell.layer)),
88+
);
89+
90+
it.effect("does not open editor URLs that are not absolute local file links", () =>
91+
Effect.gen(function* () {
92+
openExternalMock.mockResolvedValue(undefined);
93+
94+
const electronShell = yield* ElectronShell.ElectronShell;
95+
const results = yield* Effect.forEach(
96+
[
97+
"cursor://user@file/Users/me/repo/file.md",
98+
"cursor://:secret@file/Users/me/repo/file.md",
99+
"cursor://file:8080/Users/me/repo/file.md",
100+
"cursor://anysphere.cursor-deeplink/prompt?text=hi",
101+
"vscode://vscode.git/clone?url=https%3A%2F%2Fexample.com%2Frepo.git",
102+
"cursor://file",
103+
"cursor://file/",
104+
"cursor:file/Users/me/repo/file.md",
105+
"foo://file/Users/me/repo/file.md",
106+
"zed://file/Users/me/repo/file.md",
107+
"jetbrains://file/Users/me/repo/file.md",
108+
"javascript:alert(1)",
109+
],
110+
(url) => electronShell.openExternal(url),
111+
);
112+
113+
assert.deepEqual(results, Array(12).fill(false));
114+
assert.equal(openExternalMock.mock.calls.length, 0);
115+
}).pipe(Effect.provide(ElectronShell.layer)),
116+
);
117+
57118
it.effect("does not open editor URLs that mix up link shapes", () =>
58119
Effect.gen(function* () {
59120
openExternalMock.mockResolvedValue(undefined);

‎apps/desktop/src/electron/ElectronShell.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MAC_PERMISSION_SETTINGS_URLS } from "../permissions/MacPermission.ts";
22
import {
3+
isLocalEditorFileUrl,
34
REMOTE_CAPABLE_EDITOR_IDS,
45
remoteSchemeForEditor,
56
type SystemSettingsPane,
@@ -11,7 +12,7 @@ import * as Option from "effect/Option";
1112

1213
import * as Electron from "electron";
1314

14-
// Remote editor deep links must reach the OS handler.
15+
// Remote editor deep links and local editor file links must reach the OS handler.
1516
const SAFE_WEB_PROTOCOLS = new Set(["http:", "https:"]);
1617
const REMOTE_EDITOR_PROTOCOLS = new Set(
1718
REMOTE_CAPABLE_EDITOR_IDS.flatMap((id) => {
@@ -44,7 +45,9 @@ export function parseSafeExternalUrl(rawUrl: unknown): Option.Option<string> {
4445

4546
try {
4647
const url = new URL(rawUrl);
47-
return SAFE_WEB_PROTOCOLS.has(url.protocol) || isRemoteEditorUrl(url)
48+
return SAFE_WEB_PROTOCOLS.has(url.protocol) ||
49+
isRemoteEditorUrl(url) ||
50+
isLocalEditorFileUrl(url)
4851
? Option.some(url.href)
4952
: Option.none();
5053
} catch {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"platforms": ["apple"],
3+
"apple": { "modules": ["T3WebSocketModule"] }
4+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Pod::Spec.new do |s|
2+
s.name = 'T3WebSocket'
3+
s.version = '1.0.0'
4+
s.summary = 'WebSocket client with permessage-deflate for T3 Code mobile.'
5+
s.description = 'React Native ships SocketRocket on iOS, which never negotiates compression. This module dials the T3 server through URLSessionWebSocketTask, which offers permessage-deflate on its own.'
6+
s.author = 'T3 Tools'
7+
s.homepage = 'https://t3tools.com'
8+
s.platform = :ios, '18.0'
9+
s.source = { :path => '.' }
10+
s.static_framework = true
11+
s.dependency 'ExpoModulesCore'
12+
s.source_files = '*.swift'
13+
end
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
import ExpoModulesCore
2+
import Foundation
3+
4+
private final class Connection {
5+
let id: Int
6+
var task: URLSessionWebSocketTask?
7+
var finished = false
8+
var requestedCloseCode: Int?
9+
var failure: String?
10+
11+
init(id: Int) {
12+
self.id = id
13+
}
14+
}
15+
16+
/// A WebSocket client that offers permessage-deflate. React Native's SocketRocket never does,
17+
/// so a large snapshot reached an iPhone uncompressed. URLSession offers the extension on every
18+
/// handshake with no public switch, which is what we want here.
19+
public final class T3WebSocketModule: Module {
20+
private var connections: [Int: Connection] = [:]
21+
private var nextId = 1
22+
private let lock = NSLock()
23+
private var session: URLSession?
24+
private let delegateQueue: OperationQueue = {
25+
let queue = OperationQueue()
26+
queue.maxConcurrentOperationCount = 1
27+
return queue
28+
}()
29+
30+
public func definition() -> ModuleDefinition {
31+
Name("T3WebSocket")
32+
Events("open", "message", "close", "error")
33+
34+
Function("connect") { (url: URL, protocols: [String]) -> Int in
35+
let connection = self.withLock { () -> Connection in
36+
let connection = Connection(id: self.nextId)
37+
self.nextId += 1
38+
self.connections[connection.id] = connection
39+
return connection
40+
}
41+
let session = self.session ?? self.makeSession()
42+
self.session = session
43+
let task = protocols.isEmpty
44+
? session.webSocketTask(with: url)
45+
: session.webSocketTask(with: url, protocols: protocols)
46+
// The app splits frames at 48 KiB, but older servers and the outdated-host probe send whole
47+
// messages, so stay well above URLSession's 1 MiB default.
48+
task.maximumMessageSize = 128 * 1024 * 1024
49+
self.withLock { connection.task = task }
50+
task.resume()
51+
self.receive(on: connection, task: task)
52+
return connection.id
53+
}
54+
55+
Function("sendText") { (id: Int, text: String) in
56+
guard let connection = self.connection(id), let task = connection.task else { return }
57+
task.send(.string(text)) { [weak self] error in
58+
if let error { self?.fail(connection, task: task, error: error) }
59+
}
60+
}
61+
62+
Function("sendBinary") { (id: Int, bytes: Uint8Array) in
63+
guard let connection = self.connection(id), let task = connection.task else { return }
64+
let data = Data(bytes: bytes.rawPointer, count: bytes.byteLength)
65+
task.send(.data(data)) { [weak self] error in
66+
if let error { self?.fail(connection, task: task, error: error) }
67+
}
68+
}
69+
70+
Function("close") { (id: Int, code: Int, reason: String) in
71+
guard let connection = self.connection(id) else { return }
72+
// URLSession can only send the codes it names; anything else goes out as a normal closure.
73+
let closeCode = URLSessionWebSocketTask.CloseCode(rawValue: code).flatMap { $0 == .invalid ? nil : $0 } ?? .normalClosure
74+
let task = self.withLock { () -> URLSessionWebSocketTask? in
75+
connection.requestedCloseCode = closeCode.rawValue
76+
return connection.task
77+
}
78+
task?.cancel(with: closeCode, reason: reason.isEmpty ? nil : reason.data(using: .utf8))
79+
}
80+
81+
OnDestroy {
82+
self.session?.invalidateAndCancel()
83+
}
84+
}
85+
86+
private func makeSession() -> URLSession {
87+
let configuration = URLSessionConfiguration.default
88+
// Dev clients register URLProtocol classes that intercept the upgrade and drop the socket.
89+
configuration.protocolClasses = []
90+
return URLSession(configuration: configuration, delegate: SessionDelegate(module: self), delegateQueue: delegateQueue)
91+
}
92+
93+
private func connection(_ id: Int) -> Connection? {
94+
withLock { connections[id] }
95+
}
96+
97+
private func connection(for task: URLSessionTask) -> Connection? {
98+
withLock { connections.values.first { $0.task === task } }
99+
}
100+
101+
private func withLock<T>(_ body: () -> T) -> T {
102+
lock.lock()
103+
defer { lock.unlock() }
104+
return body()
105+
}
106+
107+
private func receive(on connection: Connection, task: URLSessionWebSocketTask) {
108+
task.receive { [weak self] result in
109+
guard let self else { return }
110+
switch result {
111+
case .success(.string(let text)):
112+
self.sendEvent("message", ["id": connection.id, "text": text])
113+
self.receive(on: connection, task: task)
114+
case .success(.data(let data)):
115+
self.sendEvent("message", ["id": connection.id, "data": data])
116+
self.receive(on: connection, task: task)
117+
case .success:
118+
self.receive(on: connection, task: task)
119+
case .failure(let error):
120+
self.fail(connection, task: task, error: error)
121+
}
122+
}
123+
}
124+
125+
/// Every ending is reported once, from the delegate. A receive or send failure on a task that is
126+
/// still running (an oversized message, for one) gets no delegate callback until it is cancelled.
127+
private func fail(_ connection: Connection, task: URLSessionWebSocketTask, error: Error) {
128+
let live = withLock { () -> Bool in
129+
guard !connection.finished else { return false }
130+
if connection.failure == nil { connection.failure = error.localizedDescription }
131+
return true
132+
}
133+
guard live, task.state == .running else { return }
134+
task.cancel()
135+
}
136+
137+
fileprivate func didOpen(task: URLSessionWebSocketTask) {
138+
guard let connection = connection(for: task) else { return }
139+
sendEvent("open", ["id": connection.id])
140+
}
141+
142+
fileprivate func didClose(task: URLSessionWebSocketTask, code: URLSessionWebSocketTask.CloseCode, reason: Data?) {
143+
guard let connection = connection(for: task) else { return }
144+
finish(connection, code: code.rawValue, reason: reason.flatMap { String(data: $0, encoding: .utf8) } ?? "", clean: true)
145+
}
146+
147+
fileprivate func didComplete(task: URLSessionTask, error: Error?) {
148+
guard let connection = connection(for: task) else { return }
149+
let (requested, failure) = withLock { (connection.requestedCloseCode, connection.failure) }
150+
if requested == nil, let message = failure ?? error?.localizedDescription {
151+
sendEvent("error", ["id": connection.id, "message": message])
152+
}
153+
// A task that ends without a close frame is an abnormal closure, as in browsers; one we
154+
// cancelled ourselves reports the code we asked for.
155+
finish(connection, code: requested ?? (error == nil && failure == nil ? 1005 : 1006), reason: "", clean: requested != nil)
156+
}
157+
158+
private func finish(_ connection: Connection, code: Int, reason: String, clean: Bool) {
159+
let first = withLock { () -> Bool in
160+
if connection.finished { return false }
161+
connection.finished = true
162+
connections.removeValue(forKey: connection.id)
163+
return true
164+
}
165+
guard first else { return }
166+
sendEvent("close", ["id": connection.id, "code": code, "reason": reason, "wasClean": clean])
167+
}
168+
}
169+
170+
private final class SessionDelegate: NSObject, URLSessionWebSocketDelegate {
171+
weak var module: T3WebSocketModule?
172+
173+
init(module: T3WebSocketModule) {
174+
self.module = module
175+
}
176+
177+
func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol protocol: String?) {
178+
module?.didOpen(task: webSocketTask)
179+
}
180+
181+
func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didCloseWith closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?) {
182+
module?.didClose(task: webSocketTask, code: closeCode, reason: reason)
183+
}
184+
185+
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
186+
module?.didComplete(task: task, error: error)
187+
}
188+
}

‎apps/mobile/src/connection/environment-cache-store.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,21 @@ describe("mobile SQLite environment cache store", () => {
498498
Effect.provideService(MobileDatabase.MobileDatabase, memory.database),
499499
);
500500

501-
yield* store.saveShell(ENVIRONMENT_ID, SHELL_SNAPSHOT);
501+
const origin = {
502+
serverVersion: "1.0.0",
503+
databaseMigration: 70,
504+
omitIdleSubagents: true,
505+
omitOlderSettled: false,
506+
snapshotAt: 1_800_000_000_000,
507+
};
508+
yield* store.saveShell(ENVIRONMENT_ID, SHELL_SNAPSHOT, origin);
502509
yield* store.saveThread(ENVIRONMENT_ID, THREAD_SNAPSHOT);
503510

504511
const shell = Option.getOrThrow(yield* store.loadShell(ENVIRONMENT_ID));
505512
const thread = Option.getOrThrow(yield* store.loadThread(ENVIRONMENT_ID, THREAD_ID));
506513

514+
expect(shell.origin).toEqual(origin);
515+
507516
expect(DateTime.formatIso(shell.threads[0]!.updatedAt)).toBe("2026-07-29T12:00:00.000Z");
508517
expect(DateTime.formatIso(shell.threads[0]!.latestUserMessageAt!)).toBe(
509518
"2026-07-29T12:00:00.000Z",

‎apps/mobile/src/connection/environment-cache-store.ts‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
106106
operation: "load-shell",
107107
decode: decodeStoredShellSnapshot,
108108
select: (stored) =>
109-
stored.environmentId === environmentId ? Option.some(stored.snapshot) : Option.none(),
109+
stored.environmentId === environmentId
110+
? Option.some(
111+
stored.origin === undefined
112+
? stored.snapshot
113+
: { ...stored.snapshot, origin: stored.origin },
114+
)
115+
: Option.none(),
110116
}).pipe(
111117
Effect.zipWith(
112118
Effect.promise(() => projectFaviconDatabaseCache.hydrate()),
@@ -115,16 +121,25 @@ export const make = Effect.fn("MobileEnvironmentCacheStore.make")(function* () {
115121
),
116122
),
117123
),
118-
saveShell: Effect.fn("MobileEnvironmentCache.saveShell")(function* (environmentId, snapshot) {
119-
const payload = yield* encodeStoredShellSnapshot({
120-
schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
121-
environmentId,
122-
snapshot,
123-
}).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
124-
yield* database
125-
.saveCache(environmentId, "shell", "snapshot", ORCHESTRATION_CACHE_SCHEMA_VERSION, payload)
126-
.pipe(Effect.mapError(mapDatabaseError("save-shell")));
127-
}),
124+
saveShell: Effect.fn("MobileEnvironmentCache.saveShell")(
125+
function* (environmentId, snapshot, origin) {
126+
const payload = yield* encodeStoredShellSnapshot({
127+
schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION,
128+
environmentId,
129+
snapshot,
130+
...(origin === undefined ? {} : { origin }),
131+
}).pipe(Effect.mapError((cause) => persistenceError("save-shell", cause)));
132+
yield* database
133+
.saveCache(
134+
environmentId,
135+
"shell",
136+
"snapshot",
137+
ORCHESTRATION_CACHE_SCHEMA_VERSION,
138+
payload,
139+
)
140+
.pipe(Effect.mapError(mapDatabaseError("save-shell")));
141+
},
142+
),
128143
loadThread: Effect.fn("MobileEnvironmentCache.loadThread")((environmentId, threadId) =>
129144
loadDecodedCache({
130145
database,

0 commit comments

Comments
 (0)