From bd5878ee128020c6011d4b02f1e4de64d088b267 Mon Sep 17 00:00:00 2001 From: loopway-bot Date: Mon, 16 Nov 2026 09:53:13 -0500 Subject: [PATCH 1/4] A chat message or a reply on any subject answers a thread's question A blocked thread now says what it waits on in `loopway threads`, --json and the TUI. Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 2 + README.md | 6 ++- docs/developing.md | 2 +- packages/cli/package.json | 2 +- packages/cli/src/asks.ts | 10 ++++ packages/cli/src/format.ts | 21 ++++++--- packages/cli/src/sessions.ts | 16 ++++++- packages/cli/src/threads.ts | 24 +++++++++- packages/cli/src/tui-main.tsx | 3 ++ packages/cli/src/tui-thread.tsx | 6 ++- packages/cli/test/asks.test.ts | 73 +++++++++++++++++++++++++++--- packages/cli/test/files.test.ts | 22 +++++++++ packages/cli/test/sessions.test.ts | 4 +- 13 files changed, 168 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2ea57b7..8682a590 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ loopway goes local. Run `loopway` in a directory with a `.loopway/` folder and i ### Added +- A question answered in chat, or by a comment on any of the thread's subjects (the pull request, say), unblocks the thread, so its queue goes out. A blocked thread says what it waits on in `loopway threads`, `--json` and the TUI. + - Bare `loopway`, with `--json`. Bots are `.loopway/.md` files; state is JSON per project, in `~/.loopway/state/`. - Sessions: each open thread's queued updates go to a Claude Code session that acts as the bot user and talks back through loopway's own MCP server (`comment`). `--max N` caps runs in flight. Bot files gain `model` and `harness`. Each thread has a `session`, the `sent` messages and a `.log`. diff --git a/README.md b/README.md index bd50c905..68b1516a 100644 --- a/README.md +++ b/README.md @@ -80,8 +80,11 @@ The bare `loopway` adds thread lines to its output, after the line of the event ``` 3f9a1c2e dev open owner/repo#12 2 pending 8c01d7b4 review closed owner/repo#13 1 pending +24edcfc4 dev open owner/repo#14 3 pending blocked: question on owner/repo#14 https://github.com/owner/repo/issues/14#issuecomment-1 ``` +A thread waiting on an ask says so: `blocked: on ` for its oldest open ask (`(+N more)` when there are several). `--json` has `blocked: { kind, subject, url }` for it, and nothing for a thread that isn't waiting. The TUI shows the same on the main view's row, and the thread view's Pending section says its updates wait on the answer. + ## Sessions Each open thread with queued updates and no run in flight gets all of them, oldest first, in one run of Claude Code (`claude -p --permission-mode auto --output-format stream-json`) in the directory `loopway` runs in. A new session gets the bot's prompt plus the updates; later batches run with `--resume `. `--max N` (default 3) caps the runs in flight. @@ -133,7 +136,8 @@ A session can also ask a question itself: `ask(subject, question)` posts it (mar How an ask is answered: - **A reaction.** After every pass over the known subjects (every 30 s) loopway reads the reactions on its open permission asks. A ๐Ÿ‘ or ๐Ÿ‘Ž from your `gh` user, and nobody else, answers. A ๐Ÿ‘ resumes the session with `approved: ` and "Retry it.", and the approval is remembered (`session.approved`), so the same call passes next time. A ๐Ÿ‘Ž resumes it with `denied`. If you left both, the ๐Ÿ‘Ž wins. It costs one conditional request per open ask, and a 304 is free. -- **A reply.** A comment from your `gh` user on the question's subject, after the question, answers it. It is queued like any update, and the ask is marked answered. +- **A reply.** A comment from your `gh` user on any open subject of the thread, after the question, answers it, so a reply on the pull request answers a question asked on its issue. It is queued like any update, and the ask is marked answered. +- **A chat message.** Anything you send in the TUI's chat answers the thread's open questions (`answered ... chatted`), since you are talking to the session. The queue it held back goes out on the run after the chat block ends. A permission ask is not answered by chat: it still needs a ๐Ÿ‘ or ๐Ÿ‘Ž. - Either way the session leaves `blocked` and its next run gets the answer. A blocked session is not run for other updates meanwhile: they wait in the queue. Open asks live in the thread's JSON, so a restart keeps waiting on them. The log (`loopway`, `--json`) has `asked` and `answered` lines, and the TUI's thread view lists what the thread is waiting on, with links; `blocked` shows as the session status on the main view. diff --git a/docs/developing.md b/docs/developing.md index f22ed63e..65e39c28 100644 --- a/docs/developing.md +++ b/docs/developing.md @@ -46,7 +46,7 @@ When CI passes on a push to `main` and `packages/cli` changed since the last `cl | `sessions.ts` | Delivers each thread's queue to its session (`--max`, resume, requeue on failure, usage-limit hold), and records what it sends. Also the TUI's chat blocks: messages from the owner go to the session in order, the queue waits, and the end of the block tells the session to report | | `claude.ts`, `transcript.ts` | The Claude Code harness: `claude -p` args, spawn, how a run ended, and the stream turned into log lines | | `mcp.ts` | loopway's MCP server (HTTP, one path per thread) with the `comment`, `ask`, `create_issue`, `create_pr` and `permission` tools | -| `asks.ts` | Asks: the allow/deny rule matching, the comments loopway posts to ask, and reading the owner's reactions | +| `asks.ts` | Asks: what a thread is blocked on, the allow/deny rule matching, the comments loopway posts to ask, and reading the owner's reactions | | `config.ts`, `state.ts` | `~/.loopway/config.json`'s `github` block, and `stateDir`, where `state.json` and the threads live | | `tui.tsx`, `tui-main.tsx`, `tui-thread.tsx`, `tui-text.ts` | The [Ink](https://github.com/vadimdemedes/ink) TUI: the main view, the thread view, and the subject filter. Reads threads from the in-process store and the session log from disk | | `format.ts` | The printed line, and `--json` | diff --git a/packages/cli/package.json b/packages/cli/package.json index e5b0ceac..fc1282f1 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -1,6 +1,6 @@ { "name": "@loopway/cli", - "version": "2.0.0", + "version": "2.0.1", "private": true, "license": "MIT", "description": "loopway: polls GitHub and prints every new subject and every update to one you know.", diff --git a/packages/cli/src/asks.ts b/packages/cli/src/asks.ts index 7eef9eb3..b8e796d0 100644 --- a/packages/cli/src/asks.ts +++ b/packages/cli/src/asks.ts @@ -29,6 +29,16 @@ export interface Ask { export const openAsks = (thread: Thread): Ask[] => (thread.asks ?? []).filter((a) => !a.answeredAt); +/** What a thread's session waits on: its oldest open ask, and how many more there are. */ +export function blockedOn(thread: Thread): (Ask & { more: number }) | undefined { + const [first, ...rest] = openAsks(thread); + return first && { ...first, more: rest.length }; +} + +/** `blocked: question on owner/repo#12 `, with `(+N more)` when it waits on several. */ +export const blockedText = (ask: Ask & { more: number }) => + `blocked: ${ask.kind} on ${ask.subject} ${ask.commentUrl}${ask.more ? ` (+${ask.more} more)` : ""}`; + /** The same call: the same tool with the same input. */ export function callKey(tool: string, input: unknown): string { // Claude words a Bash call's `description` differently each time it retries; the rest of the input is the call. diff --git a/packages/cli/src/format.ts b/packages/cli/src/format.ts index c084f0c4..d4abd434 100644 --- a/packages/cli/src/format.ts +++ b/packages/cli/src/format.ts @@ -1,3 +1,4 @@ +import { blockedOn, blockedText } from "./asks.js"; import type { Subject, Update } from "./source.js"; import type { Thread } from "./thread.js"; @@ -24,7 +25,7 @@ export type ThreadEvent = | { type: "queued"; subject: string; update: Update; thread: string } | { type: "ignored"; subject: string; update: Update; thread: string } | { type: "thread_closed"; thread: Thread } - | { type: "answered"; at: string; thread: string; what: string; how: "approved" | "denied" | "replied" }; + | { type: "answered"; at: string; thread: string; what: string; how: "approved" | "denied" | "replied" | "chatted" }; const pad = (n: number) => String(n).padStart(2, "0"); @@ -147,17 +148,25 @@ export function formatJson(event: LoopwayEvent): string { } } -/** One row of `loopway threads`: id, bot, status, subjects, pending count. */ -export const formatThread = (t: Thread) => - `${t.id} ${t.bot.padEnd(8)} ${t.status.padEnd(6)} ${subjectKeys(t)} ${t.queue.length} pending`; +/** One row of `loopway threads`: id, bot, status, subjects, pending count, and what it is blocked on if it is. */ +export const formatThread = (t: Thread) => { + const blocked = blockedOn(t); + return ( + `${t.id} ${t.bot.padEnd(8)} ${t.status.padEnd(6)} ${subjectKeys(t)} ${t.queue.length} pending` + + (blocked ? ` ${blockedText(blocked)}` : "") + ); +}; -export const formatThreadJson = (t: Thread) => - JSON.stringify({ +export const formatThreadJson = (t: Thread) => { + const blocked = blockedOn(t); + return JSON.stringify({ id: t.id, bot: t.bot, status: t.status, subjects: t.subjects.map((s) => s.key), pending: t.queue.length, + ...(blocked ? { blocked: { kind: blocked.kind, subject: blocked.subject, url: blocked.commentUrl } } : {}), createdAt: t.createdAt, latestAt: t.latestAt, }); +}; diff --git a/packages/cli/src/sessions.ts b/packages/cli/src/sessions.ts index d7b6381f..bbd4a021 100644 --- a/packages/cli/src/sessions.ts +++ b/packages/cli/src/sessions.ts @@ -18,7 +18,7 @@ import { } from "./asks.js"; import type { Bot } from "./bots.js"; import type { RunAgent, RunOutcome } from "./claude.js"; -import type { SessionEvent } from "./format.js"; +import type { SessionEvent, ThreadEvent } from "./format.js"; import { abortableSleep } from "./poller.js"; import type { IssueOptions, Made, Subject } from "./source.js"; import { markerOf, repoOf, type Pending, type Session, type Thread } from "./thread.js"; @@ -95,7 +95,7 @@ export interface SessionsOptions { }; /** Told after a thread created a subject, once it is recorded as sent: the rest of loopway takes it in. */ created: (thread: string, subject: Subject, subscribe: boolean) => Promise; - emit: (event: SessionEvent) => void; + emit: (event: SessionEvent | ThreadEvent) => void; now?: () => number; newSessionId?: () => string; newAskId?: () => string; @@ -164,9 +164,21 @@ export class Sessions { chat.inbox.push(text); chat.said = true; this.log(thread, `you> ${text}`); + this.answerQuestions(thread); this.kick(); } + /** The owner said something in chat: the questions the thread was blocked on are answered. */ + private answerQuestions(thread: Thread): void { + const events = this.o.threads.answerByChat(thread); + if (events.length === 0) return; + void this.o.threads.save(thread); + for (const event of events) { + if (event.type === "answered") this.log(thread, `answered by chat: ${event.what}`); + this.o.emit(event); + } + } + /** Ends the chat block. If anything was said, the session is told so, and reports it to the ticket if it should. */ endChat(threadId: string): void { const chat = this.chats.get(threadId); diff --git a/packages/cli/src/threads.ts b/packages/cli/src/threads.ts index 7abb3a61..17a32aa2 100644 --- a/packages/cli/src/threads.ts +++ b/packages/cli/src/threads.ts @@ -141,17 +141,37 @@ export class Threads { events.push({ type: "queued", subject, update, thread: thread.id }); } - /** The owner's comment on a subject after a question was asked there answers it. It is queued like any update. */ + /** + * The owner's comment on any open subject of the thread, after a question was asked, answers it: the question may + * have been asked on the issue and the answer given on the pull request. It is queued like any update. + */ private answerQuestions(thread: Thread, subject: string, update: Update, events: ThreadEvent[]) { if (update.kind !== "comment" || update.actor.toLowerCase() !== this.o.owner?.toLowerCase()) return; + if (thread.subjects.find((s) => s.key === subject)?.status !== "open") return; for (const ask of openAsks(thread)) { - if (ask.kind !== "question" || ask.subject !== subject || ask.at >= update.at) continue; + if (ask.kind !== "question" || ask.at >= update.at) continue; ask.answeredAt = update.at; events.push({ type: "answered", at: update.at, thread: thread.id, what: askLabel(ask), how: "replied" }); } this.unblock(thread); } + /** + * A chat message from the owner answers the thread's open questions: they are talking to the session, so it need not + * wait for a comment. A permission ask still needs its ๐Ÿ‘ or ๐Ÿ‘Ž. The queue it held back goes out on the next run. + * The caller saves the thread. + */ + answerByChat(thread: Thread): ThreadEvent[] { + const at = new Date(this.now()).toISOString(); + const events: ThreadEvent[] = []; + for (const ask of openAsks(thread).filter((a) => a.kind === "question")) { + ask.answeredAt = at; + events.push({ type: "answered", at, thread: thread.id, what: askLabel(ask), how: "chatted" }); + } + if (events.length > 0) this.unblock(thread); + return events; + } + /** * The owner's ๐Ÿ‘ or ๐Ÿ‘Ž on a permission ask. A ๐Ÿ‘ is remembered, so the same call passes next time. Either way the * session is told on its queue and is no longer blocked. diff --git a/packages/cli/src/tui-main.tsx b/packages/cli/src/tui-main.tsx index 390ef615..ef3790e8 100644 --- a/packages/cli/src/tui-main.tsx +++ b/packages/cli/src/tui-main.tsx @@ -1,6 +1,7 @@ import { Box, Text, useInput } from "ink"; import { stamp, subjectKeys } from "./format.js"; import type { Thread } from "./thread.js"; +import { blockedOn, blockedText } from "./asks.js"; import { sessionColor, sessionText } from "./tui-text.js"; export interface MainViewProps { @@ -60,6 +61,7 @@ export function MainView(props: MainViewProps) { /** bot, subjects, thread status, session status, pending count, time of the latest message. Closed threads are dim. */ function Row({ thread, selected }: { thread: Thread; selected: boolean }) { const closed = thread.status === "closed"; + const blocked = blockedOn(thread); return ( {selected ? "โ–ธ " : " "} @@ -67,6 +69,7 @@ function Row({ thread, selected }: { thread: Thread; selected: boolean }) { {sessionText(thread).padEnd(14)} {" "} {thread.queue.length} pending {stamp(thread.latestAt)} {subjectKeys(thread)} + {blocked && {blockedText(blocked)}} ); } diff --git a/packages/cli/src/tui-thread.tsx b/packages/cli/src/tui-thread.tsx index 45c58128..f8c9b7d6 100644 --- a/packages/cli/src/tui-thread.tsx +++ b/packages/cli/src/tui-thread.tsx @@ -93,7 +93,11 @@ export function ThreadView({ thread, log, onBack, onFilter, chat }: ThreadViewPr ))} -