Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions cmux-linux/src/socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ const methods = .{
.{ "surface.health", handleSurfaceHealth },
.{ "surface.trigger_flash", handleSurfaceTriggerFlash },
.{ "surface.clear_history", handleSurfaceClearHistory },
.{ "surface.action", handleSurfaceAction },
.{ "tab.action", handleSurfaceAction },
.{ "pane.list", handlePaneList },
.{ "pane.focus", handlePaneFocus },
.{ "pane.create", handlePaneCreate },
Expand Down Expand Up @@ -1238,6 +1240,126 @@ fn handleSurfaceClearHistory(_: Allocator, _: json.Value) []const u8 {
return "{}"; // Terminal scrollback clear stub
}

/// surface.action / tab.action — apply a tab-level action to a surface.
///
/// Mirrors macOS `v2TabAction` (Sources/TerminalController.swift). Linux
/// implements the trivial property-mutation actions (rename, clear_name,
/// pin, unpin, mark_read, mark_unread). Tab-relative close/new actions
/// (close_left, close_right, close_others, new_terminal_right,
/// new_browser_right, reload, duplicate) are not yet wired and will return
/// an `unsupported` error so callers can detect parity gaps.
fn handleSurfaceAction(alloc: Allocator, params: json.Value) []const u8 {
const tm = getTabManager() orelse return "{\"error\":\"no tab manager\"}";

// Resolve workspace (param or current)
const ws = if (getParamString(params, "workspace_id")) |id_str|
if (findWorkspaceById(tm, id_str)) |found| found.ws else return "{\"error\":\"workspace not found\"}"
else
tm.selectedWorkspace() orelse return "{\"error\":\"no workspace\"}";

const action = getParamString(params, "action") orelse return "{\"error\":\"missing action\"}";

// Resolve target surface (surface_id, tab_id, or focused)
const target_id = if (getParamString(params, "surface_id")) |id_str|
findSurfaceInWorkspace(ws, id_str) orelse return "{\"error\":\"invalid surface_id\"}"
else if (getParamString(params, "tab_id")) |id_str|
findSurfaceInWorkspace(ws, id_str) orelse return "{\"error\":\"invalid tab_id\"}"
else
ws.focused_panel_id orelse return "{\"error\":\"no focused surface\"}";

const panel = ws.panels.get(target_id) orelse return "{\"error\":\"surface not found\"}";
const panel_hex = formatId(target_id);
const panel_id_slice: []const u8 = &panel_hex;

if (std.mem.eql(u8, action, "rename")) {
const title = getParamString(params, "title") orelse return "{\"error\":\"missing title\"}";
if (panel.custom_title) |old| ws.alloc.free(old);
panel.custom_title = ws.alloc.dupe(u8, title) catch return "{\"error\":\"alloc failed\"}";
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
if (window.getSidebar()) |sb| sb.refresh();
return std.fmt.allocPrint(
alloc,
"{{\"action\":\"rename\",\"surface_id\":\"{s}\",\"title\":\"{s}\"}}",
.{ panel_id_slice, title },
) catch "{}";
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}

if (std.mem.eql(u8, action, "clear_name")) {
if (panel.custom_title) |old| {
ws.alloc.free(old);
panel.custom_title = null;
}
if (window.getSidebar()) |sb| sb.refresh();
return std.fmt.allocPrint(
alloc,
"{{\"action\":\"clear_name\",\"surface_id\":\"{s}\"}}",
.{panel_id_slice},
) catch "{}";
}

if (std.mem.eql(u8, action, "pin")) {
panel.is_pinned = true;
if (window.getSidebar()) |sb| sb.refresh();
return std.fmt.allocPrint(
alloc,
"{{\"action\":\"pin\",\"surface_id\":\"{s}\",\"pinned\":true}}",
.{panel_id_slice},
) catch "{}";
}

if (std.mem.eql(u8, action, "unpin")) {
panel.is_pinned = false;
if (window.getSidebar()) |sb| sb.refresh();
return std.fmt.allocPrint(
alloc,
"{{\"action\":\"unpin\",\"surface_id\":\"{s}\",\"pinned\":false}}",
.{panel_id_slice},
) catch "{}";
}

if (std.mem.eql(u8, action, "mark_read")) {
panel.is_manually_unread = false;
if (window.getSidebar()) |sb| sb.refresh();
return std.fmt.allocPrint(
alloc,
"{{\"action\":\"mark_read\",\"surface_id\":\"{s}\"}}",
.{panel_id_slice},
) catch "{}";
}

if (std.mem.eql(u8, action, "mark_unread")) {
panel.is_manually_unread = true;
if (window.getSidebar()) |sb| sb.refresh();
return std.fmt.allocPrint(
alloc,
"{{\"action\":\"mark_unread\",\"surface_id\":\"{s}\"}}",
.{panel_id_slice},
) catch "{}";
}

// Recognized but not yet implemented on Linux. Returning a structured
// error lets callers distinguish "wrong call" from "platform gap".
if (std.mem.eql(u8, action, "close_left") or
std.mem.eql(u8, action, "close_right") or
std.mem.eql(u8, action, "close_others") or
std.mem.eql(u8, action, "new_terminal_right") or
std.mem.eql(u8, action, "new_browser_right") or
std.mem.eql(u8, action, "reload") or
std.mem.eql(u8, action, "duplicate"))
{
return std.fmt.allocPrint(
alloc,
"{{\"error\":\"action not implemented on linux\",\"action\":\"{s}\"}}",
.{action},
) catch "{\"error\":\"action not implemented on linux\"}";
}

return std.fmt.allocPrint(
alloc,
"{{\"error\":\"unsupported action\",\"action\":\"{s}\",\"supported\":[\"rename\",\"clear_name\",\"pin\",\"unpin\",\"mark_read\",\"mark_unread\"]}}",
.{action},
) catch "{\"error\":\"unsupported action\"}";
}

// ── Batch 3: Additional Pane Operations ─────────────────────────────────

fn handlePaneFocus(_: Allocator, params: json.Value) []const u8 {
Expand Down
204 changes: 204 additions & 0 deletions tests_v2/test_surface_action_rename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
#!/usr/bin/env python3
"""Socket API: surface.action rename / clear_name / pin / unpin / mark_read / mark_unread.

Pure socket round-trip test — no GUI, no CLI binary, no platform-specific
filesystem assumptions. Designed to pass on both macOS and Linux daemons.
"""

import os
import sys
import time
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent))
from cmux import cmux, cmuxError

SOCKET_PATH = os.environ.get("CMUX_SOCKET", "/tmp/cmux-debug.sock")


def _surfaces_in(c: cmux, workspace_id: str) -> list[dict]:
"""Return raw surface.list rows (dicts) for the given workspace."""
res = c._call("surface.list", {"workspace_id": workspace_id}) or {}
return list(res.get("surfaces") or [])


def _find_surface(rows: list[dict], surface_id: str) -> dict:
for row in rows:
if str(row.get("id")) == surface_id:
return row
raise cmuxError(f"surface {surface_id} missing from list: {rows}")


def main() -> int:
created_workspace: str | None = None
with cmux(SOCKET_PATH) as c:
# Create an isolated workspace so the test cannot disturb the user's
# current workspace state.
created_workspace = c.new_workspace()
if not created_workspace:
raise cmuxError("workspace.create returned no id")
c.select_workspace(created_workspace)
time.sleep(0.05)

# Locate the focused surface in the new workspace.
rows = _surfaces_in(c, created_workspace)
if not rows:
raise cmuxError(f"new workspace has no surfaces: {rows}")
focused = next((r for r in rows if r.get("focused")), rows[0])
surface_id = str(focused["id"])
original_title = str(focused.get("title", ""))

unique_title = f"renamed-via-action-{int(time.time() * 1000) % 100000}"

# ── rename ────────────────────────────────────────────────────────
result = c._call(
"surface.action",
{
"workspace_id": created_workspace,
"surface_id": surface_id,
"action": "rename",
"title": unique_title,
},
) or {}
if result.get("action") != "rename":
raise cmuxError(f"rename: unexpected response: {result}")
if str(result.get("title")) != unique_title:
raise cmuxError(f"rename: title not echoed: {result}")

# Reflected in surface.list?
renamed = _find_surface(_surfaces_in(c, created_workspace), surface_id)
if str(renamed.get("title")) != unique_title:
raise cmuxError(
f"rename not reflected in surface.list: expected {unique_title!r}, "
f"got {renamed.get('title')!r}"
)

# ── tab.action alias should hit the same handler ──────────────────
alias_title = f"{unique_title}-alias"
alias_result = c._call(
"tab.action",
{
"workspace_id": created_workspace,
"tab_id": surface_id,
"action": "rename",
"title": alias_title,
},
) or {}
if alias_result.get("action") != "rename":
raise cmuxError(f"tab.action alias: unexpected response: {alias_result}")
aliased = _find_surface(_surfaces_in(c, created_workspace), surface_id)
if str(aliased.get("title")) != alias_title:
raise cmuxError(
f"tab.action alias not reflected: expected {alias_title!r}, "
f"got {aliased.get('title')!r}"
)

# ── clear_name ────────────────────────────────────────────────────
cleared_resp = c._call(
"surface.action",
{
"workspace_id": created_workspace,
"surface_id": surface_id,
"action": "clear_name",
},
) or {}
if cleared_resp.get("action") != "clear_name":
raise cmuxError(f"clear_name: unexpected response: {cleared_resp}")
cleared = _find_surface(_surfaces_in(c, created_workspace), surface_id)
if str(cleared.get("title")) == alias_title:
raise cmuxError(
f"clear_name did not drop custom title: still {cleared.get('title')!r}"
)
# Title falls back to process title or "Terminal"; we just assert it
# is no longer the custom alias.

# ── pin / unpin ───────────────────────────────────────────────────
pin_resp = c._call(
"surface.action",
{
"workspace_id": created_workspace,
"surface_id": surface_id,
"action": "pin",
},
) or {}
if pin_resp.get("action") != "pin" or pin_resp.get("pinned") is not True:
raise cmuxError(f"pin: unexpected response: {pin_resp}")

unpin_resp = c._call(
"surface.action",
{
"workspace_id": created_workspace,
"surface_id": surface_id,
"action": "unpin",
},
) or {}
if unpin_resp.get("action") != "unpin" or unpin_resp.get("pinned") is not False:
raise cmuxError(f"unpin: unexpected response: {unpin_resp}")

# ── mark_read / mark_unread ───────────────────────────────────────
for action in ("mark_unread", "mark_read"):
resp = c._call(
"surface.action",
{
"workspace_id": created_workspace,
"surface_id": surface_id,
"action": action,
},
) or {}
if resp.get("action") != action:
raise cmuxError(f"{action}: unexpected response: {resp}")

# ── unsupported action returns an error structure ─────────────────
try:
c._call(
"surface.action",
{
"workspace_id": created_workspace,
"surface_id": surface_id,
"action": "definitely-not-a-real-action",
},
)
# Some daemons may return a JSON body with an "error" field while
# still marking the response ok=true. Either is acceptable.
except cmuxError:
pass # expected

# ── focused-surface fallback (no surface_id provided) ─────────────
focus_fallback_title = f"{unique_title}-focused"
fallback_resp = c._call(
"surface.action",
{
"workspace_id": created_workspace,
"action": "rename",
"title": focus_fallback_title,
},
) or {}
if fallback_resp.get("action") != "rename":
raise cmuxError(
f"focused-surface fallback: unexpected response: {fallback_resp}"
)

# Reset to clear the custom name we leaked into the workspace.
c._call(
"surface.action",
{
"workspace_id": created_workspace,
"surface_id": surface_id,
"action": "clear_name",
},
)

# Cleanup
c.close_workspace(created_workspace)
created_workspace = None

print("PASS: surface.action rename / clear_name / pin / unpin / mark_read / mark_unread")
return 0

Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

if __name__ == "__main__":
try:
raise SystemExit(main())
except cmuxError as exc:
print(f"FAIL: {exc}", file=sys.stderr)
raise SystemExit(1)
Loading