emdash icon indicating copy to clipboard operation
emdash copied to clipboard

feat: support external session rename via hook server

Open OliPetry opened this issue 6 months ago β€’ 5 comments

Summary

Adds two new hook event types β€” rename-conversation and rename-task β€” that let CLI agents rename conversations (tab bar) and tasks (sidebar) through the existing hook server HTTP interface.

When an agent POSTs to /hook with X-Emdash-Event-Type: rename-conversation (or rename-task) and a JSON body {"title": "..."}, the handler:

  1. Resolves the conversation from the pty ID header
  2. Delegates to the existing renameConversation() / renameTask() service functions
  3. Emits IPC events so the renderer updates the UI in real time

Use case: Agents managing multiple sessions (orchestrators, task managers) can label conversations and tasks programmatically without needing direct DB access.

Files Changed

  • src/shared/events/taskEvents.ts β€” new taskRenamedChannel and conversationRenamedChannel IPC events
  • src/main/core/agent-hooks/rename-handler.ts β€” new handler that parses the request, looks up the conversation, and delegates to service functions
  • src/main/core/agent-hooks/agent-hook-service.ts β€” routes rename-conversation and rename-task event types before the generic agent event path
  • src/renderer/.../conversation-manager.ts β€” listens for conversationRenamedChannel and updates conversation title in the store
  • src/renderer/.../task-manager.ts β€” listens for taskRenamedChannel and updates task name in the store

Test Plan

  • [x] pnpm run typecheck passes
  • [x] pnpm run lint passes

OliPetry avatar May 19 '26 03:05 OliPetry

Greptile Summary

This PR adds rename-conversation and rename-task hook event types so CLI agents can relabel conversations and tasks via an HTTP POST to the existing hook server without direct DB access.

  • Main process (rename-handler.ts, agent-hook-service.ts): parses the request body, looks up the conversation by PTY ID, delegates to existing renameConversation/renameTask service functions, then emits IPC events β€” logic is correct.
  • Renderer (conversation-manager.ts): correctly subscribes, stores the unsubscribe handle, and tears it down in dispose(). task-manager.ts does the same subscription but discards the handle, so the listener accumulates on every store recreation without ever being removed.

Confidence Score: 3/5

The new rename endpoints are safe at the HTTP boundary, but the task-manager listener leaks on every store teardown and recreating it leaves stale handlers firing against disposed stores.

The taskRenamedChannel subscription in TaskManagerStore is wired in the constructor but its unsubscribe handle is discarded. Every project switch that tears down and rebuilds TaskManagerStore leaves the old listener alive, so the handler can fire against a stale or disposed store. The symmetric code in ConversationManagerStore handles teardown correctly, making the omission here clearly unintentional.

src/renderer/features/tasks/stores/task-manager.ts β€” the new taskRenamedChannel subscription and the dispose() method both need attention.

Important Files Changed

Filename Overview
src/renderer/features/tasks/stores/task-manager.ts Adds a taskRenamedChannel listener in the constructor but discards the unsubscribe handle β€” the listener is never removed in dispose(), leaking it whenever the store is torn down.
src/main/core/agent-hooks/rename-handler.ts New handler for conversation/task renames: parses body, looks up conversation by ptyId, delegates to existing service functions. Logic is correct; minor API naming inconsistency (both event types use a title key in the request body, while tasks internally use name).
src/main/core/agent-hooks/agent-hook-service.ts Routes two new event types before the generic agent-event path; emits IPC events on success. Error propagation is handled by HookServer's existing catch block. Safe.
src/renderer/features/tasks/conversations/conversation-manager.ts Adds conversationRenamedChannel listener, stores the unsubscribe handle in offConversationRenamed, and correctly calls it in dispose(). Pattern is correct.
src/shared/events/taskEvents.ts Adds two typed IPC event channel definitions for task and conversation renames; straightforward and consistent with existing channel definitions.

Sequence Diagram

sequenceDiagram
    participant Agent as CLI Agent
    participant HookServer as HookServer (HTTP)
    participant AgentHookService
    participant RenameHandler as rename-handler.ts
    participant DB as Database
    participant IPC as IPC events
    participant ConvMgr as ConversationManagerStore
    participant TaskMgr as TaskManagerStore

    Agent->>HookServer: "POST /hook rename-conversation {title}"
    HookServer->>AgentHookService: handler(raw)
    AgentHookService->>RenameHandler: handleConversationRename(raw)
    RenameHandler->>DB: "SELECT taskId, projectId WHERE id=conversationId"
    DB-->>RenameHandler: conv row
    RenameHandler->>RenameHandler: renameConversation(id, title)
    RenameHandler-->>AgentHookService: ConversationRenameResult
    AgentHookService->>IPC: emit conversationRenamedChannel
    IPC->>ConvMgr: handler (properly unsubscribed in dispose)
    ConvMgr->>ConvMgr: "store.data.title = title"
    HookServer-->>Agent: 200 OK

    Agent->>HookServer: "POST /hook rename-task {title}"
    HookServer->>AgentHookService: handler(raw)
    AgentHookService->>RenameHandler: handleTaskRename(raw)
    RenameHandler->>DB: "SELECT taskId, projectId WHERE id=conversationId"
    DB-->>RenameHandler: conv row
    RenameHandler->>RenameHandler: renameTask(projectId, taskId, title)
    RenameHandler-->>AgentHookService: TaskRenameResult
    AgentHookService->>IPC: emit taskRenamedChannel
    IPC->>TaskMgr: handler (handle discarded, never unsubscribed)
    TaskMgr->>TaskMgr: "store.data.name = name"
    HookServer-->>Agent: 200 OK

Comments Outside Diff (2)

  1. src/renderer/features/tasks/stores/task-manager.ts, line 132-135 (link)

    P1 The taskRenamedChannel subscription's unsubscribe handle is discarded, so it is never called in dispose(). Every time a TaskManagerStore is destroyed and recreated (e.g. switching projects), a new listener accumulates without the old one being removed. Compare how _unsubPrUpdated and _unsubProvisionProgress are handled β€” the same pattern should be followed here.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: src/renderer/features/tasks/stores/task-manager.ts
    Line: 132-135
    
    Comment:
    The `taskRenamedChannel` subscription's unsubscribe handle is discarded, so it is never called in `dispose()`. Every time a `TaskManagerStore` is destroyed and recreated (e.g. switching projects), a new listener accumulates without the old one being removed. Compare how `_unsubPrUpdated` and `_unsubProvisionProgress` are handled β€” the same pattern should be followed here.
    
    
    
    How can I resolve this? If you propose a fix, please make it concise.
    
  2. src/renderer/features/tasks/stores/task-manager.ts, line 636-645 (link)

    P1 Call the new unsubscribe handle in dispose() to match the fix above.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: src/renderer/features/tasks/stores/task-manager.ts
    Line: 636-645
    
    Comment:
    Call the new unsubscribe handle in `dispose()` to match the fix above.
    
    
    
    How can I resolve this? If you propose a fix, please make it concise.
    
Prompt To Fix All With AI
Fix the following 4 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 4
src/renderer/features/tasks/stores/task-manager.ts:132-135
The `taskRenamedChannel` subscription's unsubscribe handle is discarded, so it is never called in `dispose()`. Every time a `TaskManagerStore` is destroyed and recreated (e.g. switching projects), a new listener accumulates without the old one being removed. Compare how `_unsubPrUpdated` and `_unsubProvisionProgress` are handled β€” the same pattern should be followed here.

```suggestion
  private _unsubPrUpdated: (() => void) | null = null;
  private _unsubPrSyncProgress: (() => void) | null = null;
  private _unsubProvisionProgress: (() => void) | null = null;
  private _unsubTaskRenamed: (() => void) | null = null;
  private _disposeRepositoryReaction: (() => void) | null = null;
```

### Issue 2 of 4
src/renderer/features/tasks/stores/task-manager.ts:161-169
Store the return value so the listener can be removed in `dispose()`.

```suggestion
    this._unsubTaskRenamed = events.on(taskRenamedChannel, ({ taskId, projectId: evtProjectId, name }) => {
      if (evtProjectId !== this.projectId) return;
      const store = this.tasks.get(taskId);
      if (store && isRegistered(store)) {
        runInAction(() => {
          store.data.name = name;
        });
      }
    });
```

### Issue 3 of 4
src/renderer/features/tasks/stores/task-manager.ts:636-645
Call the new unsubscribe handle in `dispose()` to match the fix above.

```suggestion
  dispose(): void {
    this._unsubPrUpdated?.();
    this._unsubPrUpdated = null;
    this._unsubPrSyncProgress?.();
    this._unsubPrSyncProgress = null;
    this._unsubProvisionProgress?.();
    this._unsubProvisionProgress = null;
    this._unsubTaskRenamed?.();
    this._unsubTaskRenamed = null;
    this._disposeRepositoryReaction?.();
    this._disposeRepositoryReaction = null;
  }
```

### Issue 4 of 4
src/main/core/agent-hooks/rename-handler.ts:22-32
`parseTitle` reads the `title` key from the JSON body for both `rename-conversation` and `rename-task`. For task renames the underlying model field is `name`, so agents calling the hook must use `{"title": "…"}` rather than the more natural `{"name": "…"}`. This is an invisible API inconsistency that is hard to discover and easy to get wrong. Consider either accepting a `name` key for `rename-task`, or documenting the shared `title` convention explicitly.


Reviews (1): Last reviewed commit: "feat: support external session rename vi..." | Re-trigger Greptile

greptile-apps avatar May 19 '26 03:05 greptile-apps

Thanks for the PR, @OliPetry! We’re reviewing it now and will respond here with feedback/questions shortly.

arnestrickmann avatar May 19 '26 03:05 arnestrickmann

Looks interesting! We should definitely add a rename functionality like this. I am trying this out right now: Which agents are you using with this? How do you configure the agent to discover and use this endpoint?

Davidknp avatar May 21 '26 12:05 Davidknp

Thanks David! I use this with Claude Code (CLI) sessions β€” when you run multiple conversations or tasks in parallel, it's useful to label them programmatically so you can tell them apart in the tab bar / sidebar.

For discovery: fair question β€” right now the agent doesn't discover the endpoint dynamically. I have a small shell script that wraps the curl call to /hook with the right headers (X-Emdash-Event-Type: rename-conversation, X-Emdash-Pty-Id, auth token). The agent just calls that script early in a session to set a meaningful title based on what it's working on. I put the script on PATH and mention it in the agent's system prompt / instructions so it knows when to use it.

That said, I think discovery is the real question here β€” not just for rename but for the hook server capabilities in general. My setup works because I wrote the script and told the agent about it, but that doesn't scale to other users. A couple of options that might make sense:

  1. Documentation β€” document the available hook event types and their payloads somewhere agents can be pointed to (a AGENTS.md section, a man page, etc.)
  2. Skill / plugin β€” package hook server interactions into a bundled skill or MCP plugin that agents can discover and use out of the box, without needing a wrapper script or manual instructions

Option 2 seems like the more compelling long-term answer β€” agents could discover "I can rename sessions, emit events, etc." without users having to wire anything up.

Oli

OliPetry avatar May 21 '26 15:05 OliPetry

That sounds like a cool setup! The implementation via the rename channel also is aligned with the patterns we are currently using in the app.

As you're pointing out the feature currently requires the user to customise the system prompt and the shell script. Documentation can be a way to mitigate the user effort here however in this case I think this is such a fundamental and important feature that we should try to integrate it more natively without users having to configure it which is why I think Option 2 would be the right call here.

We are currently looking into leveraging the plugin infrastructure most agents are providing so that users would just install an '[agent] emdash plugin' (or we do it automatically somehow) and wire up the integration globally for the whole machine. The benefit of this would be that we could potentially use more advanced apis and the integration would be more transparent to the user compared to the currently sometimes unexpected modifications to agent settings in the users repo. Arguably this is also the less intrusive option.

For the rename feature specifically we could then, as you're suggesting, register an emdash MCP server that serves a rename endpoint which would let agents rename the conversations at points where the agent determines it makes sense.

Additionally we have to refactor the agent integrations in general in our codebase as well and centralise agent specific logic into 'agent integrations / plugins'. While reviewing your PR and trying out how we could enable this rename feature I started some experiments on here.

I would love to hear your thoughts on this!

David : )

Davidknp avatar May 22 '26 14:05 Davidknp