From e2e73d63e934d5f61454e9f1d6e4d1649af89edc Mon Sep 17 00:00:00 2001 From: clover caruso Date: Tue, 13 Oct 2026 16:08:34 -0700 Subject: [PATCH] feat(workspaces): return structured workspace creation results MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit T3 Code needs identities from the existing Raycast workspace creation flow so it can navigate the resulting chat itself. ws new --json returns workspacePath, projectId, threadId, and environmentId; ordinary Raycast stdout remains the path. --no-open keeps desktop launch under the caller’s control, and --background returns before fetching or dependency preparation. Verified real synchronous and background JSON creation against the isolated V2 CLI with JJ/APFS/pnpm; confirmed plain background stdout and guarded --no-open against launching the desktop app. Assisted-by: gpt-6.1-sol --- users/clover/packages/workspaces/README.md | 4 +- .../clover/packages/workspaces/workspaces.mjs | 34 ++++++++++------ .../packages/workspaces/workspaces.test.mjs | 39 +++++++++++++++++-- 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/users/clover/packages/workspaces/README.md b/users/clover/packages/workspaces/README.md index 3e6f4776e50c2e38085c1012956bfbdbe2bd1bc9..7598e3680165258db2683aef14e0411c6cdaa657 100644 --- a/users/clover/packages/workspaces/README.md +++ b/users/clover/packages/workspaces/README.md @@ -19,7 +19,9 @@ project followed by ` | `, with the same logo. Additional chats in that project use the same workspace folder. `ws new` and `ws open` accept `--no-open` to bind the chat without navigating -the desktop app. Standard output is the workspace path; setup progress goes to +the desktop app. `ws new --json` returns `{workspacePath, projectId, threadId, +environmentId}` for callers that navigate the resulting chat themselves. By +default, standard output is the workspace path; setup progress goes to standard error. The chat opens in an empty folder before fetching or creating the JJ workspace; `--background` returns without waiting for preparation. Claude and Codex startup and prompt hooks wait for the workspace to be ready. diff --git a/users/clover/packages/workspaces/workspaces.mjs b/users/clover/packages/workspaces/workspaces.mjs index bfa53800b753e404987236b5babb828c78f1fc5e..4a344e545f4731356f87d70eeabb281a52a8edca 100644 --- a/users/clover/packages/workspaces/workspaces.mjs +++ b/users/clover/packages/workspaces/workspaces.mjs @@ -526,10 +526,10 @@ async function openChat(id, config, client, open = true) { "Add the source repository to T3 Code before creating a workspace chat.", ); } - const previous = [...snapshot.threads, ...snapshot.archivedThreads].find( + let thread = [...snapshot.threads, ...snapshot.archivedThreads].find( (thread) => thread.id === record.threadId, ); - if (!previous) { + if (!thread) { let project = snapshot.projects.find((project) => canonical(project.workspaceRoot) === directory ); @@ -577,19 +577,20 @@ async function openChat(id, config, client, open = true) { creationSource: "web", }); const confirmed = await client.snapshot(); - if (!confirmed.threads.some((thread) => thread.id === threadId)) { + thread = confirmed.threads.find((thread) => thread.id === threadId); + if (!thread) { throw new Error( "T3 Code has not confirmed the chat yet. Run ws open again.", ); } } - if (previous?.archivedAt) { - await client.dispatch({ type: "thread.unarchive", threadId: previous.id }); + if (thread.archivedAt) { + await client.dispatch({ type: "thread.unarchive", threadId: thread.id }); } - if (previous?.settledOverride === "settled") { + if (thread.settledOverride === "settled") { await client.dispatch({ type: "thread.unsettle", - threadId: previous.id, + threadId: thread.id, reason: "user", }); } @@ -602,7 +603,12 @@ async function openChat(id, config, client, open = true) { `t3code://threads/${environmentId}/${record.threadId}`, ]); } - console.log(directory); + return { + workspacePath: directory, + projectId: thread.projectId, + threadId: thread.id, + environmentId, + }; } export async function main(args) { @@ -647,7 +653,7 @@ export async function main(args) { } if (!command || command === "--help") { console.log( - "ws setup \nws new [name] [-r revision] [--no-open] [--background]\nws open [--no-open]\nws deps \nws gc [--dry-run]", + "ws setup \nws new [name] [-r revision] [--no-open] [--background] [--json]\nws open [--no-open]\nws deps \nws gc [--dry-run]", ); return; } @@ -719,8 +725,10 @@ export async function main(args) { const client = await t3(); const open = ![argument, ...rest].includes("--no-open"); const background = [argument, ...rest].includes("--background"); + const json = [argument, ...rest].includes("--json"); const options = [argument, ...rest].filter((value) => - value !== undefined && value !== "--no-open" && value !== "--background" + value !== undefined && value !== "--no-open" && value !== "--background" && + value !== "--json" ); const id = options[0]?.startsWith("-") ? undefined : options.shift(); const revision = options.length === 0 @@ -749,7 +757,8 @@ export async function main(args) { prepared: false, revision, }); - await openChat(name, config, client, open); + const created = await openChat(name, config, client, open); + console.log(json ? JSON.stringify(created) : created.workspacePath); const logs = path.join(home, "Library/Logs"); fs.mkdirSync(logs, { recursive: true }); const output = background @@ -785,7 +794,8 @@ export async function main(args) { if (rest.length > 1 || (rest[0] && rest[0] !== "--no-open")) { throw new Error("Run ws open [--no-open]."); } - await openChat(argument, config, await t3(), rest[0] !== "--no-open"); + const opened = await openChat(argument, config, await t3(), rest[0] !== "--no-open"); + console.log(opened.workspacePath); } else if (command === "gc") { if (rest.length || (argument && argument !== "--dry-run")) { throw new Error("Run ws gc [--dry-run]."); diff --git a/users/clover/packages/workspaces/workspaces.test.mjs b/users/clover/packages/workspaces/workspaces.test.mjs index 25af8ab78bf61fdcfbf5afeb8615ad40f9eec703..43a104c32a06100a5c12bc31cdb0904f0c189441 100644 --- a/users/clover/packages/workspaces/workspaces.test.mjs +++ b/users/clover/packages/workspaces/workspaces.test.mjs @@ -225,7 +225,15 @@ process.exit(result.status ?? 1); }), (error) => /working copy is stale/.test(error.stderr.toString())); await main(["setup", projectWorkspace]); await main(["setup", source]); - await main(["new", "second", "--no-open"]); + const creationOutput = []; + const creationLog = mock.method(console, "log", (value) => creationOutput.push(value)); + const noOpen = mock.method(childProcess, "execFileSync", (command, args, options) => { + assert.notEqual(command, "/usr/bin/open", "--no-open never launches the desktop app"); + return run(command, args, options); + }); + await main(["new", "second", "--no-open", "--json"]); + noOpen.mock.restore(); + creationLog.mock.restore(); const created = (await client.snapshot()).threads.find((thread) => thread.title === "Fixture | second" ); @@ -234,6 +242,13 @@ process.exit(result.status ?? 1); "approval-required", "new chats retain the source chat's permission mode", ); + assert.equal(creationOutput.length, 1, "JSON creation writes one structured result"); + assert.deepEqual(JSON.parse(creationOutput[0]), { + workspacePath: path.join(directory, "workspaces/second"), + projectId: created.projectId, + threadId: created.id, + environmentId: client.environment.environmentId, + }); const first = path.join(directory, "workspaces/first"); const second = path.join(directory, "workspaces/second"); const projects = (await client.snapshot()).projects; @@ -337,19 +352,33 @@ exec "$WORKSPACE_TEST_PNPM" "$@" path.join(directory, ".local/state/workspaces/creation-lock"), process.execPath, script, - "new", "background", "--no-open", "--background", + "new", "background", "--no-open", "--background", "--json", ], { env: preparationEnv, stdio: ["ignore", "pipe", "pipe"], }); let creatorOutput = ""; - creator.stdout.on("data", (chunk) => { creatorOutput += chunk; }); + let creatorStdout = ""; + creator.stdout.on("data", (chunk) => { + creatorOutput += chunk; + creatorStdout += chunk; + }); creator.stderr.on("data", (chunk) => { creatorOutput += chunk; }); const [creatorStatus] = await once(creator, "exit"); assert.equal(creatorStatus, 0, creatorOutput); const backgroundRecord = path.join( directory, ".local/state/workspaces/workspaces/background.json", ); + const backgroundCreation = JSON.parse(creatorStdout); + const backgroundThread = (await client.snapshot()).threads.find( + (thread) => thread.id === backgroundCreation.threadId, + ); + assert.deepEqual(backgroundCreation, { + workspacePath: background, + projectId: backgroundThread.projectId, + threadId: backgroundThread.id, + environmentId: client.environment.environmentId, + }); assert.equal(JSON.parse(fs.readFileSync(backgroundRecord)).prepared, false); for (let attempt = 0; !fs.existsSync(fetchStarted); attempt++) { assert.ok(attempt < 200, "background preparation starts fetching main"); @@ -374,9 +403,13 @@ exec "$WORKSPACE_TEST_PNPM" "$@" process.execPath, script, "new", "queued", "--no-open", "--background", ], { env: preparationEnv, stdio: "pipe" }); let queuedOutput = ""; + let queuedStdout = ""; + queued.stdout.on("data", (chunk) => { queuedStdout += chunk; }); queued.stderr.on("data", (chunk) => { queuedOutput += chunk; }); const [queuedStatus] = await once(queued, "exit"); assert.equal(queuedStatus, 0, queuedOutput); + assert.equal(queuedStdout.trim(), path.join(directory, "workspaces/queued"), + "default creation stdout remains the plain workspace path"); const queuedRecord = path.join(directory, ".local/state/workspaces/workspaces/queued.json"); assert.ok(JSON.parse(fs.readFileSync(queuedRecord)).threadId, "another chat can be created while a preparation worker holds the lock"); -- 2.54.1