From c6b1543dc8131431e57b593ac16e50bbac507a80 Mon Sep 17 00:00:00 2001 From: Qijun Date: Thu, 26 Nov 2026 02:16:37 -0500 Subject: [PATCH] feat: delete a message, in Messages and with jun tool message delete A message sent by mistake could only go when Jun quit. The user now deletes one from its context menu, or with Backspace or Delete on the row chosen; agents with `jun tool message delete `. Its number is not given again, and what it links to stays. The choice goes to the message below, or above for the last one. --- docs/agent-tools.md | 14 ++++++ docs/agents-view.md | 1 + internal/app/messages.go | 27 ++++++++++++ internal/app/messages_test.go | 83 +++++++++++++++++++++++++++++++++++ internal/app/messages_view.go | 34 ++++++++++---- internal/app/tools.go | 1 + internal/tool/cli.go | 7 ++- internal/tool/parse.go | 7 +++ internal/tool/protocol.go | 4 +- internal/tool/tool_test.go | 8 ++++ skills/jun-tools/SKILL.md | 4 +- 11 files changed, 179 insertions(+), 11 deletions(-) diff --git a/docs/agent-tools.md b/docs/agent-tools.md index 392479f..7d9902a 100644 --- a/docs/agent-tools.md +++ b/docs/agent-tools.md @@ -35,6 +35,7 @@ Or copy the folders in `skills/` to `~/.claude/skills/` for Claude Code (`$CLAUD | `jun tool send ` / `jun tool send -` | Submits the text to the agent in the terminal, or holds it while the user is typing there. Prints nothing, `sent, not started yet` or `held: …`. | | `jun tool message [--title ] [--kind <kind>] [--link <resource>] <text>`, or `-` for the text | Shows the text to the user, in [Messages](agents-view.md#messages). Prints the message's id. | | `jun tool message update <id> [--kind <kind>] [--task <id>] [<text>]`, or `-` for the text | Changes the message's kind or task, adds a reply, or any of them. Prints nothing. | +| `jun tool message delete <id>` | Takes the message out, as one sent by mistake. Prints nothing. | | `jun tool message` | The messages, as a JSON array, the latest first | | `jun tool goal [--all \| --state <state>…] [--match <text>]` / `goal <id>` | The [goals](#goal-and-task), as a JSON array, each followed by those under it / a goal with those above it and its tasks | | `jun tool goal add …` / `goal update <id> …` / `goal delete <id>` | Adds a goal and prints its id / changes it / takes it out. The others print nothing. | @@ -235,6 +236,19 @@ Opening another terminal gives it the focus, so you are then with that terminal: - **Who may update**: any terminal of Jun, whoever sent the message. Outside Jun's terminals it is refused. - **A message that does not exist** is status 1, as is one Jun no longer keeps. +#### Deleting a message + +`message delete` takes a message out of Messages, for one sent by mistake or by a loop gone wrong. The user can do the same in [Messages](agents-view.md#messages). + +```sh +"$JUN_BIN" tool message delete 12 +``` + +- **A matter handled is no mistake**: make its message `done`, for the user to read, rather than deleting it. +- **Its number** is not given again: the next message still gets the next number, and `update` or `delete` of `12` is then status 1, as for a message that does not exist. +- **Only the message goes**: the terminal, Diff, file or project it links to stays as it was, and so does its task. +- **Who may delete**: any terminal of Jun, whoever sent the message. Outside Jun's terminals it is refused. + #### Reading messages back `message` with nothing after it lists them, for instance to find what still waits for the user: diff --git a/docs/agents-view.md b/docs/agents-view.md index 08c988b..227ac6e 100644 --- a/docs/agents-view.md +++ b/docs/agents-view.md @@ -88,6 +88,7 @@ The **Agents** view, in Global, shows every coding agent running in Jun's termin - A long text shows four lines and a message its last three replies. **Show more** opens it where it is, and **Show less** folds it. - Messages, like Agents and Tasks, keep the size of Jun's own text, whatever the [text size](shortcuts.md#text-size) of terminals. - **Tab** from the title of Messages, past its ⋯ if it has one, reaches the list: **↑** and **↓** move through it, **↩** acts as a click, and **→** and **←** open and fold a long text. +- **Delete** in a message's context menu, or **⌫** on the row chosen, takes the message out at once, with no question: a message lasts only until Jun quits anyway. The choice goes to the message below, or above for the last one. What it links to stays, and its number is not given again. Agents delete one with [`jun tool message delete`](agent-tools.md#deleting-a-message). - Messages last until Jun quits. It keeps the last 500. Global shows its views together, without tabs: diff --git a/internal/app/messages.go b/internal/app/messages.go index b979bae..91b218a 100644 --- a/internal/app/messages.go +++ b/internal/app/messages.go @@ -113,6 +113,33 @@ func (a *app) toolUpdateMessage(req tool.Request, callers []int) error { return nil } +// toolDeleteMessage takes a message out, as the user does in Messages. +func (a *app) toolDeleteMessage(req tool.Request, callers []int) error { + if _, err := a.toolCaller(callers, req.Caller); err != nil { + return err + } + if !a.deleteMessage(req.Message) { + return fmt.Errorf("no message %d", req.Message) + } + return nil +} + +// deleteMessage takes the message out, and reports whether there was one. +// Its number stays used. Its row chosen, the choice goes to the next older +// row, or the newer one past the last; the list follows any other. +func (a *app) deleteMessage(id uint64) bool { + i := slices.IndexFunc(a.messages, func(m message) bool { return m.ID == id }) + if i < 0 { + return false + } + row := len(a.messages) - 1 - i + a.messages = slices.Delete(a.messages, i, i+1) + if row == a.messageSelected { + a.messagesList.Choose(min(row, len(a.messages)-1)) + } + return true +} + // checkTask tells whether the task a message is about is there: none is. func (a *app) checkTask(id int) error { if _, ok := a.goals.Task(id); id != 0 && !ok { diff --git a/internal/app/messages_test.go b/internal/app/messages_test.go index a1d7902..e007529 100644 --- a/internal/app/messages_test.go +++ b/internal/app/messages_test.go @@ -608,6 +608,89 @@ func TestARefusedUpdateChangesNothing(t *testing.T) { } } +func deleteMessage(a *app, from []int, id uint64) tool.Response { + return a.uiAnswers(tool.Request{Op: tool.OpDeleteMessage, Message: id}, from)[tool.OpDeleteMessage]() +} + +func TestAMessageSentByMistakeGoes(t *testing.T) { + a, _, sessions := agentsApp(t) + shell := shellOf(t, sessions[0]) + say(t, a, sessions[0], "Oldest") + mistake := say(t, a, sessions[0], "By mistake").Created + say(t, a, sessions[0], "Newest") + if resp := deleteMessage(a, nil, mistake); !strings.HasPrefix(resp.Error, "caller is not a terminal of this Jun") { + t.Fatalf("from outside Jun: %q", resp.Error) + } + if resp := deleteMessage(a, shell, mistake); resp.Error != "" { + t.Fatal(resp.Error) + } + if got := messageTexts(a); !slices.Equal(got, []string{"Newest", "Oldest"}) { + t.Fatalf("rows %v", got) + } + gone := fmt.Sprintf("no message %d", mistake) + if resp := deleteMessage(a, shell, mistake); resp.Error != gone { + t.Fatalf("deleted twice: %q", resp.Error) + } + if resp := update(t, a, sessions[0], tool.Request{Message: mistake, Text: "x"}); resp.Error != gone { + t.Fatalf("updated once gone: %q", resp.Error) + } + if id := say(t, a, sessions[0], "Next").Created; id != mistake+2 { + t.Fatalf("the next message is M%d", id) + } +} + +// The menu and ⌫ take a message out; the choice goes to the next older +// row, or the newer one past the last, and stays with a message that +// stays. +func TestTheUserDeletesAMessage(t *testing.T) { + a, tt, sessions := agentsApp(t) + a.state.GlobalVisible = true + for _, text := range []string{"Oldest", "Older", "Middle", "Newer", "Newest"} { + say(t, a, sessions[0], text) + } + tt.Frame() + if err := tt.RightClick("Newer"); err != nil { + t.Fatal(err) + } + if err := tt.ChooseMenuItem("Delete"); err != nil { + t.Fatalf("%v in %q", err, tt.Menu()) + } + tt.Frame() + if got := messageTexts(a); !slices.Equal(got, []string{"Newest", "Middle", "Older", "Oldest"}) { + t.Fatalf("rows %v", got) + } + a.focusRegion(a.state.Global.ID) + tt.Frame() + tt.Key(ui.Super, ui.Key3) + tt.Frame() + tt.Key(0, ui.KeyTab) + tt.Frame() + for range 2 { + tt.Key(0, ui.KeyDown) + tt.Frame() + } + tt.Key(0, ui.KeyBackspace) + tt.Frame() + if got := messageTexts(a); !slices.Equal(got, []string{"Newest", "Older", "Oldest"}) || a.messageSelected != 1 { + t.Fatalf("rows %v, row %d chosen", got, a.messageSelected) + } + shell := shellOf(t, sessions[0]) + if resp := deleteMessage(a, shell, a.messages[len(a.messages)-1].ID); resp.Error != "" { + t.Fatal(resp.Error) + } + tt.Frame() + if got := messageTexts(a); !slices.Equal(got, []string{"Older", "Oldest"}) || a.messageSelected != 0 { + t.Fatalf("the choice left its message: row %d", a.messageSelected) + } + tt.Key(0, ui.KeyDown) + tt.Frame() + tt.Key(0, ui.KeyDelete) + tt.Frame() + if got := messageTexts(a); !slices.Equal(got, []string{"Older"}) || a.messageSelected != 0 { + t.Fatalf("past the last: rows %v, row %d chosen", got, a.messageSelected) + } +} + func TestAMessageKeepsItsLatestReplies(t *testing.T) { a, _, sessions := agentsApp(t) id := say(t, a, sessions[0], "Busy").Created diff --git a/internal/app/messages_view.go b/internal/app/messages_view.go index 725cf6b..15cf7e1 100644 --- a/internal/app/messages_view.go +++ b/internal/app/messages_view.go @@ -40,7 +40,8 @@ func (a *app) messagesView(c *ui.Context) { } // messageList keeps the keyboard on the list: its rows take no focus, so -// ↩, → and ← always act on the row chosen, which a click chooses too. +// ↩, →, ← and ⌫ always act on the row chosen, which a click chooses too. +// A message goes once the list is built, whose rows it would move. func (a *app) messageList(c *ui.Context) { at := func(row int) message { return a.messages[len(a.messages)-1-row] } st := &a.messagesList @@ -53,16 +54,26 @@ func (a *app) messageList(c *ui.Context) { st.KeepOffset = first == 0 // the latest shows as it comes; scrolled away, the rows stay st.Label = func(row int) string { return at(row).name() } now := time.Now() - clicked := false + clicked, deleted := false, uint64(0) list := ui.List(c, st, len(a.messages), func(row int) { - if a.messageRow(c, at(row), now, row == a.messageSelected) { + m := at(row) + click, remove := a.messageRow(c, m, now, row == a.messageSelected) + if click { a.messageSelected, clicked = row, true } + if remove { + deleted = m.ID + } }).Gap(8).Dividers(1, a.theme.Border).Grow(1).FillWidth() if clicked { list.Focus() c.Invalidate() } + if deleted != 0 { + a.deleteMessage(deleted) + c.Invalidate() + return + } chosen := a.messageSelected if chosen < 0 || chosen >= len(a.messages) { return @@ -80,10 +91,15 @@ func (a *app) messageList(c *ui.Context) { a.setMessageWhole(m.ID, false) c.Invalidate() } + if list.Shortcut(0, ui.KeyBackspace) || list.Shortcut(0, ui.KeyDelete) { + a.deleteMessage(m.ID) + c.Invalidate() + } } -// messageRow reports a click on the row or on its fold control. -func (a *app) messageRow(c *ui.Context, m message, now time.Time, chosen bool) (clicked bool) { +// messageRow reports a click on the row or on its fold control, and Delete +// chosen in its menu. +func (a *app) messageRow(c *ui.Context, m message, now time.Time, chosen bool) (clicked, deleted bool) { width, size := a.messagesWidth-2*(messagePadding+messageBorder), a.messageSize() textWidth := width if m.Title == "" { @@ -95,7 +111,9 @@ func (a *app) messageRow(c *ui.Context, m message, now time.Time, chosen bool) ( if m.Link != nil && m.Link.Kind == tool.TargetTerminal && terminal.ID(m.Link.ID) == a.peek.id { border = a.theme.Accent } - row := ui.Column(c).FillWidth().Gap(4).Padding(6, messagePadding).Radius(6).Border(messageBorder, border) + row := ui.Column(c).FillWidth().Gap(4).Padding(6, messagePadding).Radius(6).Border(messageBorder, border).ContextMenu(func(menu *ui.Menu) { + deleted = menu.Item(a.text("Delete", "删除")).Chosen() + }) if m.Task != 0 && m.Task == a.selectedTask { row.Background(a.taskTint()) } @@ -121,9 +139,9 @@ func (a *app) messageRow(c *ui.Context, m message, now time.Time, chosen bool) ( } if live && row.Clicked() { a.openMessage(m, row.ClickModifiers()&ui.Super != 0) - return true + return true, deleted } - return clicked + return clicked, deleted } // messageFoot closes the row with its tag, fold control and age, and diff --git a/internal/app/tools.go b/internal/app/tools.go index ccab678..343b0cb 100644 --- a/internal/app/tools.go +++ b/internal/app/tools.go @@ -158,6 +158,7 @@ func (a *app) uiAnswers(req tool.Request, callers []int) map[tool.Op]func() tool }, tool.OpMessages: func() tool.Response { return tool.Response{Messages: a.toolMessages()} }, tool.OpUpdateMessage: func() tool.Response { return outcome(a.toolUpdateMessage(req, callers)) }, + tool.OpDeleteMessage: func() tool.Response { return outcome(a.toolDeleteMessage(req, callers)) }, tool.OpRun: func() tool.Response { return started(a.toolRun(req, callers)) }, tool.OpLoop: func() tool.Response { return started(a.toolLoop(req, callers)) }, tool.OpLoops: func() tool.Response { return tool.Response{Loops: a.toolLoops()} }, diff --git a/internal/tool/cli.go b/internal/tool/cli.go index a48b72f..09b8d5f 100644 --- a/internal/tool/cli.go +++ b/internal/tool/cli.go @@ -19,7 +19,7 @@ const usage = "usage: jun tool help | terminals | watch |\n" + " terminals update <id> [--name <name>] [--every <every>] [--at <HH:MM>] [--text <text>] [--no-timer] |\n" + " screen <id> [--lines N] | open <target> [--in <perspective>] [--peek] | close <target> | back | forward |\n" + " send <id> <text> | send <id> - | message [--title <title>] [--kind <kind>] [--link <resource>] [<text|->] |\n" + - " message | message update <id> [--kind <kind>] [--task <id>] [<text|->] |\n" + + " message | message update <id> [--kind <kind>] [--task <id>] [<text|->] | message delete <id> |\n" + " goal [--all | --state <state>…] [--match <text>] | goal <id> | goal add … <title> | goal update <id> … | goal delete <id> |\n" + " task [--all | --state <state>…] [--owner <role>] [--project <name>] [--goal <id>] [--match <text>] |\n" + " task <id> | task add --goal <id> … <title> | task update <id> … | task delete <id> |\n" + @@ -177,6 +177,11 @@ Sending: it, or any of them: ask becoming done tells the user it is handled. A reply moves the message to the top; a message keeps its last 50. Any terminal may, whoever sent it. No such message is status 1. Prints nothing. + jun tool message delete <id> + Takes the message out, for one sent by mistake. A matter handled + is no mistake: make its message done, for the user to read. Its + number is not given again. Any terminal may, whoever sent it. No + such message is status 1. Prints nothing. jun tool message The messages, the latest first, as a JSON array: id, time, kind, title, text, project, link, as --link takes it, task, and replies, diff --git a/internal/tool/parse.go b/internal/tool/parse.go index 5ace1e0..5c31d0e 100644 --- a/internal/tool/parse.go +++ b/internal/tool/parse.go @@ -139,6 +139,13 @@ func parseMessage(args []string, stdin io.Reader) (Request, error) { if args[0] == "update" && len(args) > 1 { return parseUpdateMessage(args[1:], stdin) } + if args[0] == "delete" && len(args) > 1 { + id, err := strconv.ParseUint(args[1], 10, 64) + if err != nil || id == 0 || len(args) > 2 { + return Request{}, errors.New("message delete takes one id, a positive integer") + } + return Request{Op: OpDeleteMessage, Message: id}, nil + } req := Request{Op: OpMessage} flags := map[string]func(string) error{ "--title": func(title string) error { diff --git a/internal/tool/protocol.go b/internal/tool/protocol.go index 20b472c..ff25b94 100644 --- a/internal/tool/protocol.go +++ b/internal/tool/protocol.go @@ -44,6 +44,7 @@ const ( // OpUpdateMessage adds a reply to the message of Request.Message, or // changes its kind, or both. OpUpdateMessage Op = "update-message" + OpDeleteMessage Op = "delete-message" // takes out the message of Request.Message OpRun Op = "run" OpLoop Op = "loop" // registers one OpLoops Op = "loops" // lists them @@ -269,7 +270,8 @@ type Request struct { Text string `json:"text,omitempty"` // send: to submit, its line breaks kept; message: to show; update-terminal: the timer's Title string `json:"title,omitempty"` // message Kind MessageKind `json:"kind,omitempty"` // message, none being MessageInfo; update-message, none leaving it - // Message is the ID of the message update-message changes. + // Message is the ID of the message update-message changes, or + // delete-message takes out. Message uint64 `json:"message,omitempty"` // Command is the shell command line run and loop run, in Dir, an // absolute path. diff --git a/internal/tool/tool_test.go b/internal/tool/tool_test.go index dc10ceb..76c4ced 100644 --- a/internal/tool/tool_test.go +++ b/internal/tool/tool_test.go @@ -77,6 +77,7 @@ func TestMainUsage(t *testing.T) { {"roles", "--open", "x"}, {"roles", "--team"}, {"roles", "--team", "a", "b"}, {"roles", "--team", "42"}, {"join"}, {"join", "0"}, {"join", "x"}, {"join", "1", "2"}, {"leave", "1"}, {"message", "--task", "0", "hi"}, {"message", "update", "3", "--task", "x"}, + {"message", "delete", "0"}, {"message", "delete", "x"}, {"message", "delete", "7", "8"}, {"goal", "0"}, {"goal", "x"}, {"goal", "--all", "--state", "open"}, {"goal", "--state", "later"}, {"goal", "--owner", "a"}, {"goal", "add"}, {"task", "update"}, {"goal", "add", "x"}, {"goal", "add", "--outcome", "o"}, {"goal", "add", "--outcome", "o", "a", "b"}, {"goal", "add", "--goal", "1", "--outcome", "o", "x"}, {"goal", "add", "--outcome", "a\nb", "x"}, {"goal", "add", "--outcome", "o", "--link", "terminal:3", "x"}, {"goal", "add", "--outcome", "o", "--parent", "-1", "x"}, @@ -324,6 +325,12 @@ func TestToolsAnswerOverTheSocket(t *testing.T) { if code, _, _ := run(path, "message", "update"); code != 0 { t.Fatalf("the one word update is a text: %d", code) } + if code, out, _ := run(path, "message", "delete", "7"); code != 0 || out != "" { + t.Fatalf("delete: %d %q", code, out) + } + if code, _, _ := run(path, "message", "delete"); code != 0 { + t.Fatalf("the one word delete is a text: %d", code) + } if code, _, _ := run(path, "message", "--title", "Done"); code != 0 { t.Fatalf("message with a title alone: %d", code) } @@ -399,6 +406,7 @@ func TestToolsAnswerOverTheSocket(t *testing.T) { {Op: OpMessage, Title: "Asks", Kind: MessageAsk, Target: &Target{Kind: TargetTerminal, ID: 3}, Text: "It waits"}, {Op: OpMessages}, {Op: OpUpdateMessage, Message: 7, Kind: MessageDone, Text: "Answered"}, {Op: OpUpdateMessage, Message: 7, Kind: MessageDone}, {Op: OpUpdateMessage, Message: 7, Text: "a\nb"}, {Op: OpMessage, Text: "update"}, + {Op: OpDeleteMessage, Message: 7}, {Op: OpMessage, Text: "delete"}, {Op: OpMessage, Title: "Done"}, {Op: OpMessage, Text: "-- deploy failed"}, {Op: OpMessage, Title: "Quiet"}, {Op: OpMessage, Text: "a\nb"}, {Op: OpRun, Command: "codex --full-auto", Dir: here, Open: true}, {Op: OpRun, Command: "codex", Project: "Web"}, {Op: OpRun, Command: "codex", Project: "web", Dir: filepath.Join(here, "sub")}, diff --git a/skills/jun-tools/SKILL.md b/skills/jun-tools/SKILL.md index b8384cb..d3f76f5 100644 --- a/skills/jun-tools/SKILL.md +++ b/skills/jun-tools/SKILL.md @@ -55,6 +55,7 @@ When a command fails, its error says what to do next: follow it. | `back` / `forward` | Steps through where the user has been | | `message [--title <t>] [--kind ask\|warn\|done\|info] [--link <resource>] <text>` / `… -` | Tells the user, in Messages in Global, without disturbing them, and prints the message's id; `-` reads standard input, and with a title the text may go | | `message update <id> [--kind <kind>] [--task <id>] [<text>]` / `… -` | Changes the message's kind or task, adds the text as a reply, or any of them | +| `message delete <id>` | Takes the message out of Messages, for one sent by mistake; its number is not given again | | `message` | The messages as JSON, the latest first: `id`, `time`, `kind`, `title`, `text`, `project`, `link`, `task`, `replies` | | `goal [--all \| --state <state>…] [--match <text>]` / `goal <id>` | The user's goals as JSON, each followed by those under it / a goal with those above it and its tasks | | `goal add [--parent <id>] --outcome <outcome> [--due <date>] [--period <period>] [--link <resource>] [--text <text>\|-] <title>` | Adds a goal; prints its id | @@ -174,11 +175,12 @@ Give `--link` whenever the message is about something: without it, the message i printf '%s\n' "$long_message" | "$JUN_BIN" tool message --link "file:report.md" - ``` -**Follow a message up.** `message` prints the id; the user sees `12` as `M12`, and may call it that. A message is one matter: `ask` means the user has something to do, so when they have done it, make it `done`. Add what happens next as replies, rather than new messages. A reply moves the message to the top. Any terminal may update any message. +**Follow a message up.** `message` prints the id; the user sees `12` as `M12`, and may call it that. A message is one matter: `ask` means the user has something to do, so when they have done it, make it `done`. Add what happens next as replies, rather than new messages. A reply moves the message to the top. Any terminal may update any message. Delete only one sent by mistake, such as a wrong link or a loop that repeated itself; a matter handled stays, as `done`, for the user to read. A deleted message's number is not given again. ```sh "$JUN_BIN" tool message update 12 --kind done 'You agreed; it goes on.' "$JUN_BIN" tool message update 12 'PR #53 is open, the tests pass.' +"$JUN_BIN" tool message delete 13 # sent by mistake "$JUN_BIN" tool message | jq -c '.[] | select(.kind == "ask")' # what still waits for the user ```