ring-cli 0.17.3__tar.gz → 0.17.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ring_cli-0.17.3 → ring_cli-0.17.4}/PKG-INFO +1 -1
- {ring_cli-0.17.3 → ring_cli-0.17.4}/pyproject.toml +1 -1
- {ring_cli-0.17.3 → ring_cli-0.17.4}/pyproject.toml.orig +1 -1
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/cli.py +5 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/hook.py +144 -69
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/registry.py +12 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/sources/__init__.py +17 -1
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/tui.py +15 -5
- ring_cli-0.17.4/src/ring/waiting.py +104 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/LICENSE +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/README.en.md +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/__init__.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/__main__.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/agent_notify.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/commands/__init__.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/commands/_args.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/commands/completion.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/commands/digest.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/commands/doctor.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/commands/focus.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/commands/gc.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/commands/hook.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/commands/quiet.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/commands/stats.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/config.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/focus/__init__.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/focus/applescript.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/focus/base.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/focus/iterm2.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/focus/kitty.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/focus/linux_wm.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/focus/neovim.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/focus/terminal.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/focus/tmux.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/focus/trace.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/gc.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/hook_protocol.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/i18n.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/ipc.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/labels.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/locale/en/LC_MESSAGES/ring.mo +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/locale/en/LC_MESSAGES/ring.po +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/locale/ring.pot +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/notify/__init__.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/notify/base.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/notify/command.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/notify/notify_send.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/notify/ntfy.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/notify/osascript_notifier.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/notify/terminal_notifier.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/notify/webhook.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/notify_queue.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/osascript.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/pathutil.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/payload_log.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/permission.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/plugins.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/ps_parse.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/question_detect.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/sources/base.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/sources/claude_code.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/sources/codex.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/sources/hook_registry.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/sources/local_llm.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/stats.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/subproc.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/tmux_scan.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/transcript.py +0 -0
- {ring_cli-0.17.3 → ring_cli-0.17.4}/src/ring/watcher.py +0 -0
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# import 的 module(見 [tool.uv.build-backend] module-name)與 CLI 指令仍是 `ring`。
|
|
3
3
|
[project]
|
|
4
4
|
name = "ring-cli"
|
|
5
|
-
version = "0.17.
|
|
5
|
+
version = "0.17.4"
|
|
6
6
|
description = "RiNG — Realtime Instance Notification Grid for active agent CLI sessions."
|
|
7
7
|
readme = "README.en.md"
|
|
8
8
|
requires-python = ">=3.13"
|
|
@@ -284,6 +284,11 @@ def render_json(sessions: list[Session]) -> str:
|
|
|
284
284
|
"last_active": s.last_active,
|
|
285
285
|
"last_action": s.last_action,
|
|
286
286
|
"waiting_detail": s.waiting_detail,
|
|
287
|
+
"waiting_requests": [
|
|
288
|
+
{"id": request.request_id, "owner": request.owner, "kind": request.kind, "detail": request.detail}
|
|
289
|
+
for request in s.waiting_requests
|
|
290
|
+
if s.status is Status.WAITING
|
|
291
|
+
],
|
|
287
292
|
"todo": {"done": s.todo[0], "total": s.todo[1]} if s.todo else None,
|
|
288
293
|
"source": s.source,
|
|
289
294
|
}
|
|
@@ -35,7 +35,7 @@ import sys
|
|
|
35
35
|
import time
|
|
36
36
|
from collections.abc import Iterator
|
|
37
37
|
from contextlib import contextmanager
|
|
38
|
-
from dataclasses import dataclass, replace
|
|
38
|
+
from dataclasses import asdict, dataclass, replace
|
|
39
39
|
from pathlib import Path
|
|
40
40
|
from typing import Any
|
|
41
41
|
from urllib.parse import quote
|
|
@@ -55,6 +55,16 @@ from ring.registry import (
|
|
|
55
55
|
)
|
|
56
56
|
from ring.stats import log_transition
|
|
57
57
|
from ring.transcript import _extract_todo, _latest_action, _tail_records
|
|
58
|
+
from ring.waiting import (
|
|
59
|
+
FOREGROUND_OWNER,
|
|
60
|
+
UNKNOWN_OWNER,
|
|
61
|
+
WaitingRequest,
|
|
62
|
+
event_owner,
|
|
63
|
+
primary_wait,
|
|
64
|
+
read_waiting_requests,
|
|
65
|
+
renew_wait,
|
|
66
|
+
tool_request_id,
|
|
67
|
+
)
|
|
58
68
|
|
|
59
69
|
_HOOK_EVENTS = list(HOOK_EVENTS)
|
|
60
70
|
|
|
@@ -228,17 +238,19 @@ def _update_session_state(
|
|
|
228
238
|
"""持有 session lock 時合併並寫入;回傳需在鎖外發送的通知。"""
|
|
229
239
|
prev_row = _previous_row(path)
|
|
230
240
|
prev_status = str(prev_row["status"]) if "status" in prev_row else None
|
|
231
|
-
|
|
232
|
-
is_subagent_event =
|
|
233
|
-
isinstance(data.get(key), str) and data[key] for key in ("agent_type", "agentType")
|
|
234
|
-
)
|
|
241
|
+
owner = event_owner(data)
|
|
242
|
+
is_subagent_event = owner != FOREGROUND_OWNER
|
|
235
243
|
if is_subagent_event:
|
|
236
|
-
#
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
244
|
+
# Subagent SessionEnd 不是 host session 的結束。
|
|
245
|
+
if event.status is Status.ENDED:
|
|
246
|
+
raw_pending = prev_row.get("pending_permissions", [])
|
|
247
|
+
owns_pending = isinstance(raw_pending, list) and any(
|
|
248
|
+
isinstance(item, dict) and item.get("owner") == owner for item in raw_pending
|
|
249
|
+
)
|
|
250
|
+
if owner == UNKNOWN_OWNER or (owner not in read_waiting_requests(prev_row) and not owns_pending):
|
|
251
|
+
return None
|
|
252
|
+
# 只結束這個 agent 的待決事項;host 仍在工作,不能刪掉共用 registry。
|
|
253
|
+
event = replace(event, status=Status.WORKING)
|
|
242
254
|
if prev_row:
|
|
243
255
|
event = replace(
|
|
244
256
|
event,
|
|
@@ -246,16 +258,6 @@ def _update_session_state(
|
|
|
246
258
|
transcript_path=str(prev_row.get("source_path", "")),
|
|
247
259
|
)
|
|
248
260
|
unhide_session(event.session_id)
|
|
249
|
-
# 一般 Notification 只能升 🔴、不能降 🟡——閒置提醒不是「使用者回應了」的證據。
|
|
250
|
-
keep_waiting = _notification_keeps_waiting(event, prev_row)
|
|
251
|
-
if keep_waiting:
|
|
252
|
-
event = replace(
|
|
253
|
-
event,
|
|
254
|
-
status=Status.WAITING,
|
|
255
|
-
waiting_for=str(prev_row.get("waiting_for", "")),
|
|
256
|
-
waiting_kind=str(prev_row.get("waiting_kind", "")),
|
|
257
|
-
detail=str(prev_row.get("waiting_detail", "")),
|
|
258
|
-
)
|
|
259
261
|
if event.status is Status.ENDED:
|
|
260
262
|
path.unlink(missing_ok=True) # 乾淨離場:直接消失
|
|
261
263
|
if prev_status is not None and prev_status != Status.ENDED.value:
|
|
@@ -271,6 +273,46 @@ def _update_session_state(
|
|
|
271
273
|
# 查詢未知不是 session 換人;保留已取得的前景身分,成功的新 lookup 仍可重新綁定。
|
|
272
274
|
agent_pid = previous_pid
|
|
273
275
|
|
|
276
|
+
reset_waits = not is_subagent_event and (
|
|
277
|
+
event.event == "SessionStart" or (previous_pid is not None and agent_pid != previous_pid)
|
|
278
|
+
)
|
|
279
|
+
previous_state = {} if reset_waits else prev_row
|
|
280
|
+
requests = read_waiting_requests(previous_state)
|
|
281
|
+
keep_waiting = _notification_keeps_waiting(event, prev_row)
|
|
282
|
+
now = time.time()
|
|
283
|
+
pending, matched = _pending_permission_details(event, previous_state, data, owner, now)
|
|
284
|
+
request_id = tool_request_id(data)
|
|
285
|
+
if event.event == "Notification" and event.waiting_kind == "permission" and matched is not None:
|
|
286
|
+
# 無 agent ID 的 Notification 只有唯一候選時才可借用其來源與摘要。
|
|
287
|
+
owner = str(matched["owner"])
|
|
288
|
+
event = replace(event, detail=str(matched["detail"]))
|
|
289
|
+
request_id = str(matched["request_id"])
|
|
290
|
+
if event.status is Status.WAITING:
|
|
291
|
+
previous_wait = requests.get(owner)
|
|
292
|
+
if (
|
|
293
|
+
event.event == "Notification"
|
|
294
|
+
and event.waiting_kind == "permission"
|
|
295
|
+
and not request_id
|
|
296
|
+
and not pending
|
|
297
|
+
and previous_wait is not None
|
|
298
|
+
and previous_wait.kind == "permission"
|
|
299
|
+
and previous_wait.tool_use_id
|
|
300
|
+
):
|
|
301
|
+
# 暫存摘要 TTL 過後的泛用提醒不是另一筆請求,也不該蓋掉已知的工具摘要。
|
|
302
|
+
request_id = previous_wait.tool_use_id
|
|
303
|
+
event = replace(event, detail=previous_wait.detail)
|
|
304
|
+
requests[owner] = renew_wait(
|
|
305
|
+
previous_wait,
|
|
306
|
+
WaitingRequest(owner, event.waiting_kind, event.detail, event.waiting_for, now, tool_use_id=request_id),
|
|
307
|
+
)
|
|
308
|
+
elif not keep_waiting and owner != UNKNOWN_OWNER:
|
|
309
|
+
requests.pop(owner, None)
|
|
310
|
+
if owner == FOREGROUND_OWNER:
|
|
311
|
+
# 沒有 agent ID 的舊/匿名等待只能由前景的明確回應解除。
|
|
312
|
+
requests.pop(UNKNOWN_OWNER, None)
|
|
313
|
+
selected = primary_wait(requests) if requests else None
|
|
314
|
+
status = Status.WAITING if selected is not None else event.status
|
|
315
|
+
|
|
274
316
|
last_action, todo = event.last_action or "—", None
|
|
275
317
|
tp = event.transcript_path
|
|
276
318
|
if tp:
|
|
@@ -279,20 +321,23 @@ def _update_session_state(
|
|
|
279
321
|
last_action = _latest_action(records)
|
|
280
322
|
todo = _extract_todo(records)
|
|
281
323
|
|
|
282
|
-
now = time.time()
|
|
283
324
|
payload: dict[str, Any] = {
|
|
284
325
|
"session_id": event.session_id,
|
|
285
326
|
"provider": event.provider,
|
|
286
327
|
"cwd": event.cwd,
|
|
287
328
|
"origin_cwd": prev_row.get("origin_cwd", event.cwd) if is_subagent_event else event.cwd,
|
|
288
|
-
"status":
|
|
329
|
+
"status": status.value,
|
|
289
330
|
# 最後一個 hook 事件名:讀取側據此做 codex 的核可等待判定(最後事件是
|
|
290
331
|
# PermissionRequest 且靜默逾時 → 🔴),任何後續事件覆寫它就自然清紅。
|
|
291
332
|
"last_event": event.event,
|
|
333
|
+
"last_event_owner": owner,
|
|
334
|
+
"last_tool_use_id": request_id,
|
|
292
335
|
"last_active": now,
|
|
293
336
|
"heartbeat_at": now,
|
|
294
337
|
"last_action": last_action,
|
|
295
338
|
"hook_pid": os.getpid(),
|
|
339
|
+
"waiting_requests": {key: asdict(request) for key, request in requests.items()},
|
|
340
|
+
"pending_permissions": pending,
|
|
296
341
|
}
|
|
297
342
|
if agent_pid is not None:
|
|
298
343
|
payload["agent_pid"] = agent_pid
|
|
@@ -304,7 +349,9 @@ def _update_session_state(
|
|
|
304
349
|
payload["tmux_pane"] = tmux_pane
|
|
305
350
|
if todo:
|
|
306
351
|
payload["todo"] = list(todo)
|
|
307
|
-
if
|
|
352
|
+
if selected is not None and selected.waiting_for:
|
|
353
|
+
payload["waiting_for"] = selected.waiting_for
|
|
354
|
+
elif selected is None and event.waiting_for:
|
|
308
355
|
payload["waiting_for"] = event.waiting_for
|
|
309
356
|
|
|
310
357
|
# 裸 PermissionRequest(→ WORKING)帶著最具體的 tool/指令摘要;真的停下來等人時,
|
|
@@ -313,22 +360,17 @@ def _update_session_state(
|
|
|
313
360
|
# (還新鮮的話)拿它當 waiting_detail,看板與通知才看得到具體是哪條指令在等核可。
|
|
314
361
|
# Codex 沒有 Notification:這份暫存改由讀取側的靜默逾時判定拿去當 waiting_detail
|
|
315
362
|
# (見 registry._promote_codex_permission_wait)。
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
payload["
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
if
|
|
326
|
-
payload["
|
|
327
|
-
waiting_detail = event.detail
|
|
328
|
-
if event.event == "Notification" and event.waiting_kind == "permission" and pending_detail:
|
|
329
|
-
waiting_detail = pending_detail
|
|
330
|
-
if waiting_detail:
|
|
331
|
-
payload["waiting_detail"] = waiting_detail
|
|
363
|
+
if matched is not None:
|
|
364
|
+
payload["pending_permission_detail"] = matched["detail"]
|
|
365
|
+
payload["pending_permission_detail_at"] = matched["at"]
|
|
366
|
+
|
|
367
|
+
if selected is not None:
|
|
368
|
+
if selected.kind:
|
|
369
|
+
payload["waiting_kind"] = selected.kind
|
|
370
|
+
if selected.owner.startswith("agent:"):
|
|
371
|
+
payload["waiting_agent_id"] = selected.owner.removeprefix("agent:")
|
|
372
|
+
if selected.detail:
|
|
373
|
+
payload["waiting_detail"] = selected.detail
|
|
332
374
|
previous_tty = str(prev_row.get("tty", "")) if same_binding else ""
|
|
333
375
|
if is_subagent_event:
|
|
334
376
|
tty = previous_tty or (_pid_tty(agent_pid) if agent_pid is not None else "")
|
|
@@ -341,13 +383,12 @@ def _update_session_state(
|
|
|
341
383
|
# 的 waiting_notified_at(上次 waiting 通知的 epoch 時間戳)。離開 WAITING 也要把時間戳
|
|
342
384
|
# 帶進新 row(payload 每次重建,不帶會遺失),快速翻回 WAITING 時才判得出「還在冷卻期」。
|
|
343
385
|
# 舊 row 沒這欄位 = 從未通知過 → 照發。讀取與預約通知時間同樣受 session lock 保護。
|
|
344
|
-
last_notified = _as_epoch(
|
|
386
|
+
last_notified = _as_epoch(previous_state.get("waiting_notified_at"))
|
|
345
387
|
if last_notified is not None:
|
|
346
388
|
payload["waiting_notified_at"] = last_notified
|
|
347
389
|
should_ring = False
|
|
348
|
-
if event.status is Status.WAITING
|
|
349
|
-
#
|
|
350
|
-
# (重複提醒由 TUI 的提醒排程器負責,見 tui._ring_on_waiting_alerts)。
|
|
390
|
+
if event.status is Status.WAITING:
|
|
391
|
+
# 只通知本事件提出的等待;沿用其他 owner 的 🔴 不算新的等待事件。
|
|
351
392
|
should_ring = _waiting_ring_allowed(last_notified, now)
|
|
352
393
|
if should_ring:
|
|
353
394
|
payload["waiting_notified_at"] = now
|
|
@@ -356,11 +397,12 @@ def _update_session_state(
|
|
|
356
397
|
tmp.write_text(json.dumps(payload))
|
|
357
398
|
tmp.replace(path) # atomic
|
|
358
399
|
|
|
359
|
-
if prev_status !=
|
|
360
|
-
log_transition(event.session_id, event.provider, event.cwd,
|
|
400
|
+
if prev_status != status.value:
|
|
401
|
+
log_transition(event.session_id, event.provider, event.cwd, status.value)
|
|
361
402
|
|
|
362
403
|
if should_ring:
|
|
363
|
-
|
|
404
|
+
# 看板可能仍選著另一個等待者;即時通知要描述這次事件本身。
|
|
405
|
+
return event, payload | {"waiting_kind": event.waiting_kind, "waiting_detail": event.detail}, last_action
|
|
364
406
|
return None
|
|
365
407
|
|
|
366
408
|
|
|
@@ -376,8 +418,8 @@ def _notification_keeps_waiting(event: NormalizedHookEvent, prev_row: dict[str,
|
|
|
376
418
|
|
|
377
419
|
只有真的證明使用者已經回應的事件(UserPromptSubmit / PreToolUse / PostToolUse /
|
|
378
420
|
下一輪 Stop)才該清掉 WAITING;Notification 一律只能升 🔴、不能降 🟡。這裡回 True 時
|
|
379
|
-
|
|
380
|
-
|
|
421
|
+
由呼叫端保留 waiting_requests 的各來源內容(payload 每次重建,不帶就會遺失),
|
|
422
|
+
再選出摘要欄位,並且不重發通知。
|
|
381
423
|
"""
|
|
382
424
|
if event.event != "Notification" or event.status is not Status.IDLE:
|
|
383
425
|
return False
|
|
@@ -419,29 +461,62 @@ def _as_epoch(v: Any) -> float | None:
|
|
|
419
461
|
|
|
420
462
|
|
|
421
463
|
# 暫存的權限 detail 新鮮期(秒):permission_prompt Notification 通常在裸 PermissionRequest
|
|
422
|
-
# 後 ~6
|
|
464
|
+
# 後 ~6 秒到;超過這個窗口的暫存視為過期(多半屬於已解決的舊請求),不再拿來當 detail。
|
|
423
465
|
_PENDING_DETAIL_TTL = 120.0
|
|
424
466
|
|
|
425
467
|
# 這些事件代表權限已放行(工具跑完)或回合已推進(新輸入 / 回合結束)→ 清掉暫存。
|
|
426
|
-
_PENDING_DETAIL_CLEAR_EVENTS = {"PostToolUse", "UserPromptSubmit", "Stop"}
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
def
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
468
|
+
_PENDING_DETAIL_CLEAR_EVENTS = {"PostToolUse", "UserPromptSubmit", "Stop", "SessionEnd"}
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
def _pending_permission_details(
|
|
472
|
+
event: NormalizedHookEvent, prev_row: dict[str, Any], data: dict[str, Any], owner: str, now: float
|
|
473
|
+
) -> tuple[list[dict[str, Any]], dict[str, Any] | None]:
|
|
474
|
+
"""依來源/tool use ID 保存候選;沒有唯一配對時不向通知提供摘要。"""
|
|
475
|
+
raw = prev_row.get("pending_permissions")
|
|
476
|
+
if not isinstance(raw, list):
|
|
477
|
+
raw = [
|
|
478
|
+
{
|
|
479
|
+
"owner": FOREGROUND_OWNER,
|
|
480
|
+
"request_id": "",
|
|
481
|
+
"detail": prev_row.get("pending_permission_detail"),
|
|
482
|
+
"at": prev_row.get("pending_permission_detail_at"),
|
|
483
|
+
}
|
|
484
|
+
]
|
|
485
|
+
pending = [
|
|
486
|
+
item
|
|
487
|
+
for item in raw
|
|
488
|
+
if isinstance(item, dict)
|
|
489
|
+
and isinstance(item.get("owner"), str)
|
|
490
|
+
and isinstance(item.get("request_id"), str)
|
|
491
|
+
and isinstance(item.get("detail"), str)
|
|
492
|
+
and item["detail"]
|
|
493
|
+
and (at := _as_epoch(item.get("at"))) is not None
|
|
494
|
+
and 0 <= now - at <= _PENDING_DETAIL_TTL
|
|
495
|
+
]
|
|
496
|
+
request_id = tool_request_id(data)
|
|
497
|
+
if owner != UNKNOWN_OWNER:
|
|
498
|
+
if event.event in _PENDING_DETAIL_CLEAR_EVENTS:
|
|
499
|
+
ambiguous_completion = (
|
|
500
|
+
event.event == "PostToolUse" and not request_id and sum(item["owner"] == owner for item in pending) > 1
|
|
501
|
+
)
|
|
502
|
+
pending = [
|
|
503
|
+
item
|
|
504
|
+
for item in pending
|
|
505
|
+
if ambiguous_completion
|
|
506
|
+
or item["owner"] != owner
|
|
507
|
+
or (event.event == "PostToolUse" and request_id and item["request_id"] != request_id)
|
|
508
|
+
]
|
|
509
|
+
if event.event == "PermissionRequest" and event.status is Status.WORKING and event.detail:
|
|
510
|
+
pending = [item for item in pending if (item["owner"], item["request_id"]) != (owner, request_id)]
|
|
511
|
+
pending.append({"owner": owner, "request_id": request_id, "detail": event.detail, "at": now})
|
|
512
|
+
# 缺少來源的 Notification 可能來自背景請求;多個候選時不把最後一筆當答案。
|
|
513
|
+
unqualified = event.event == "Notification" and owner == FOREGROUND_OWNER
|
|
514
|
+
candidates = [
|
|
515
|
+
item
|
|
516
|
+
for item in pending
|
|
517
|
+
if (unqualified or item["owner"] == owner) and (not request_id or item["request_id"] == request_id)
|
|
518
|
+
]
|
|
519
|
+
return pending, candidates[0] if len(candidates) == 1 else None
|
|
445
520
|
|
|
446
521
|
|
|
447
522
|
def _waiting_ring_allowed(last_notified: float | None, now: float) -> bool:
|
|
@@ -55,6 +55,7 @@ from ring.transcript import (
|
|
|
55
55
|
_recent_actions,
|
|
56
56
|
_tail_records,
|
|
57
57
|
)
|
|
58
|
+
from ring.waiting import FOREGROUND_OWNER, WaitingRequest, read_waiting_requests
|
|
58
59
|
|
|
59
60
|
CLAUDE_PROJECTS = Path.home() / ".claude" / "projects"
|
|
60
61
|
RING_REGISTRY = Path.home() / ".config" / "ring" / "sessions"
|
|
@@ -359,6 +360,7 @@ class Session:
|
|
|
359
360
|
# 只是借一個「真實 session 的欄位」組出來的通知 payload,讓 notify_title / notify_message
|
|
360
361
|
# 改用彙總句式。session_id 本身維持真實值(不覆寫成 sentinel),點擊通知才能正確 focus。
|
|
361
362
|
is_summary: bool = field(default=False, repr=False, compare=False)
|
|
363
|
+
waiting_requests: tuple[WaitingRequest, ...] = ()
|
|
362
364
|
|
|
363
365
|
@property
|
|
364
366
|
def project(self) -> str:
|
|
@@ -1134,6 +1136,7 @@ def _hook_sessions(
|
|
|
1134
1136
|
provider=provider,
|
|
1135
1137
|
waiting_kind=str(data.get("waiting_kind", "")),
|
|
1136
1138
|
waiting_detail=str(data.get("waiting_detail", "")),
|
|
1139
|
+
waiting_requests=tuple(read_waiting_requests(data).values()),
|
|
1137
1140
|
origin_cwd=str(data.get("origin_cwd", "")),
|
|
1138
1141
|
)
|
|
1139
1142
|
if _promote_codex_permission_wait(
|
|
@@ -1151,6 +1154,15 @@ def _hook_sessions(
|
|
|
1151
1154
|
# 「下一個事件來時還新不新鮮」;這裡 hook 靜默 = 同一筆請求還掛著,
|
|
1152
1155
|
# 摘要必然還是它,直接沿用)。
|
|
1153
1156
|
row.waiting_detail = pending_detail
|
|
1157
|
+
row.waiting_requests = (
|
|
1158
|
+
WaitingRequest(
|
|
1159
|
+
str(data.get("last_event_owner", FOREGROUND_OWNER)),
|
|
1160
|
+
"permission",
|
|
1161
|
+
row.waiting_detail,
|
|
1162
|
+
since=row.last_active,
|
|
1163
|
+
tool_use_id=str(data.get("last_tool_use_id", "")),
|
|
1164
|
+
),
|
|
1165
|
+
)
|
|
1154
1166
|
if _is_bare_session_start_row(
|
|
1155
1167
|
row.tty or "",
|
|
1156
1168
|
str(data.get("last_event", "")),
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
|
|
8
8
|
from __future__ import annotations
|
|
9
9
|
|
|
10
|
+
from dataclasses import replace
|
|
11
|
+
|
|
10
12
|
import ring.registry as registry
|
|
11
13
|
import ring.tmux_scan as tmux_scan
|
|
12
14
|
from ring.registry import Session, Status
|
|
@@ -16,6 +18,7 @@ from ring.sources.codex import source as _codex
|
|
|
16
18
|
from ring.sources.hook_registry import source as _hook_registry
|
|
17
19
|
from ring.sources.local_llm import llama_cpp_source as _llama_cpp
|
|
18
20
|
from ring.sources.local_llm import ollama_source as _ollama
|
|
21
|
+
from ring.waiting import FOREGROUND_OWNER, primary_wait
|
|
19
22
|
|
|
20
23
|
# 註冊表(順序=彙整順序)。hook registry 先於 zero-config source,精準事件優先。
|
|
21
24
|
_SOURCES: list[SessionSource] = [_hook_registry, _claude_code, _codex, _ollama, _llama_cpp]
|
|
@@ -119,15 +122,28 @@ def _merge_duplicate_session(current: Session, candidate: Session) -> Session:
|
|
|
119
122
|
「使用者回應了」。這種 row 只由真人 prompt(tail "working")或後續 hook 事件清掉,
|
|
120
123
|
否則 Stop 之後任何一筆簿記寫入推進 mtime 都會讓紅色憑空消失。
|
|
121
124
|
"""
|
|
125
|
+
foreground_wait = next((r for r in current.waiting_requests if r.owner == FOREGROUND_OWNER), None)
|
|
126
|
+
# 背景 hook 會更新 row 的 last_active,但不能讓已解決的前景等待又蓋過 transcript 證據。
|
|
127
|
+
waiting_since = foreground_wait.since if foreground_wait is not None else current.last_active
|
|
122
128
|
if (
|
|
123
129
|
current.source == "hook"
|
|
124
130
|
and current.status is Status.WAITING
|
|
125
131
|
and candidate.provider == current.provider
|
|
126
132
|
and candidate.status is not Status.WAITING
|
|
127
|
-
and candidate.last_active >
|
|
133
|
+
and candidate.last_active > waiting_since
|
|
128
134
|
and candidate._tail_kind in {"working", "waiting"}
|
|
129
135
|
and not (current.waiting_kind == "question" and candidate._tail_kind == "waiting")
|
|
130
136
|
):
|
|
137
|
+
# Host transcript 只能證明前景已回應,不能代替另一個 subagent 解除等待。
|
|
138
|
+
remaining = {r.owner: r for r in current.waiting_requests if r.owner != FOREGROUND_OWNER}
|
|
139
|
+
if remaining:
|
|
140
|
+
selected = primary_wait(remaining)
|
|
141
|
+
return replace(
|
|
142
|
+
current,
|
|
143
|
+
waiting_requests=tuple(remaining.values()),
|
|
144
|
+
waiting_kind=selected.kind,
|
|
145
|
+
waiting_detail=selected.detail,
|
|
146
|
+
)
|
|
131
147
|
if not candidate.tty:
|
|
132
148
|
candidate.tty = current.tty
|
|
133
149
|
if not candidate.tmux_target:
|
|
@@ -477,7 +477,7 @@ class RingApp(App[None]):
|
|
|
477
477
|
acknowledged_revision = self._permission_acks.get(s.session_id)
|
|
478
478
|
if acknowledged_revision is None:
|
|
479
479
|
continue
|
|
480
|
-
if s.last_active > acknowledged_revision:
|
|
480
|
+
if s.last_active > acknowledged_revision or len(s.waiting_requests) > 1:
|
|
481
481
|
# 新 hook event(可能是下一個權限請求)已到,重新採信 provider 狀態。
|
|
482
482
|
del self._permission_acks[s.session_id]
|
|
483
483
|
continue
|
|
@@ -486,6 +486,7 @@ class RingApp(App[None]):
|
|
|
486
486
|
s.status = Status.WORKING
|
|
487
487
|
s.waiting_kind = ""
|
|
488
488
|
s.waiting_detail = ""
|
|
489
|
+
s.waiting_requests = ()
|
|
489
490
|
else:
|
|
490
491
|
# provider 已用同 revision 清掉 WAITING,不再需要本地 acknowledgment。
|
|
491
492
|
del self._permission_acks[s.session_id]
|
|
@@ -663,20 +664,29 @@ class RingApp(App[None]):
|
|
|
663
664
|
self._permission_replies_inflight.discard(s.session_id)
|
|
664
665
|
option = next((f"{n}. {text}" for n, text in dialog.options if n == number), str(number))
|
|
665
666
|
if outcome is permission.ReplyOutcome.OK:
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
667
|
+
# 畫面只證實一個對話框消失,無法證明其他 agent 的等待也已解除。
|
|
668
|
+
can_ack = len(s.waiting_requests) <= 1
|
|
669
|
+
if can_ack:
|
|
670
|
+
if s.session_id == self._focused_sid:
|
|
671
|
+
self._focused_sid = None # 已就地回覆,解除通知標記
|
|
672
|
+
self._permission_acks[s.session_id] = s.last_active
|
|
669
673
|
# 先直接更新現有資料,別為了一筆 acknowledgment 同步重掃所有 process。
|
|
670
674
|
# 下一次既有輪詢仍會正常全量同步、重排表格。
|
|
671
675
|
row = next(
|
|
672
676
|
((idx, current) for idx, current in enumerate(self._sessions) if current.session_id == s.session_id),
|
|
673
677
|
None,
|
|
674
678
|
)
|
|
675
|
-
if
|
|
679
|
+
if (
|
|
680
|
+
can_ack
|
|
681
|
+
and row is not None
|
|
682
|
+
and row[1].last_active <= s.last_active
|
|
683
|
+
and len(row[1].waiting_requests) <= 1
|
|
684
|
+
):
|
|
676
685
|
idx, current = row
|
|
677
686
|
current.status = Status.WORKING
|
|
678
687
|
current.waiting_kind = ""
|
|
679
688
|
current.waiting_detail = ""
|
|
689
|
+
current.waiting_requests = ()
|
|
680
690
|
self.query_one(DataTable).update_cell_at(Coordinate(idx, 0), self._status_cell(current))
|
|
681
691
|
self._toast(_("→ {project}:已回覆權限({option})", project=name, option=option), ok=True)
|
|
682
692
|
self._update_detail()
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"""Hook 等待來源的持久化格式;writer 與讀取/合併路徑共用。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
import math
|
|
8
|
+
from dataclasses import dataclass, replace
|
|
9
|
+
from typing import Any
|
|
10
|
+
from uuid import uuid4
|
|
11
|
+
|
|
12
|
+
FOREGROUND_OWNER = "foreground"
|
|
13
|
+
UNKNOWN_OWNER = "unknown"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def event_owner(data: dict[str, Any]) -> str:
|
|
17
|
+
for key in ("agent_id", "agentId"):
|
|
18
|
+
if isinstance(value := data.get(key), str) and value:
|
|
19
|
+
return f"agent:{value}"
|
|
20
|
+
if any(isinstance(data.get(key), str) and data[key] for key in ("agent_type", "agentType")):
|
|
21
|
+
return UNKNOWN_OWNER
|
|
22
|
+
return FOREGROUND_OWNER
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def tool_request_id(data: dict[str, Any]) -> str:
|
|
26
|
+
return next(
|
|
27
|
+
(value for key in ("tool_use_id", "toolUseId") if isinstance(value := data.get(key), str) and value), ""
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True)
|
|
32
|
+
class WaitingRequest:
|
|
33
|
+
owner: str
|
|
34
|
+
kind: str = ""
|
|
35
|
+
detail: str = ""
|
|
36
|
+
waiting_for: str = ""
|
|
37
|
+
since: float = 0.0
|
|
38
|
+
revision: str = ""
|
|
39
|
+
tool_use_id: str = ""
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def request_id(self) -> str:
|
|
43
|
+
"""跨輪詢穩定的不透明 ID;舊 row/讀取側升紅不可每次產生隨機值。"""
|
|
44
|
+
if self.revision:
|
|
45
|
+
return self.revision
|
|
46
|
+
identity = json.dumps([self.owner, self.since, self.tool_use_id])
|
|
47
|
+
return "legacy:" + hashlib.sha256(identity.encode()).hexdigest()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def renew_wait(previous: WaitingRequest | None, current: WaitingRequest) -> WaitingRequest:
|
|
51
|
+
"""同一工具的重複事件/相同提醒保留識別;明確的新請求才換 ID。"""
|
|
52
|
+
if previous is not None and previous.owner == current.owner and previous.kind == current.kind:
|
|
53
|
+
if previous.tool_use_id and current.tool_use_id:
|
|
54
|
+
same_request = previous.tool_use_id == current.tool_use_id
|
|
55
|
+
else:
|
|
56
|
+
same_request = (previous.detail, previous.waiting_for) == (current.detail, current.waiting_for)
|
|
57
|
+
if same_request:
|
|
58
|
+
return replace(
|
|
59
|
+
current,
|
|
60
|
+
since=previous.since,
|
|
61
|
+
revision=previous.request_id,
|
|
62
|
+
tool_use_id=current.tool_use_id or previous.tool_use_id,
|
|
63
|
+
)
|
|
64
|
+
return replace(current, revision=uuid4().hex)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def read_waiting_requests(row: dict[str, Any]) -> dict[str, WaitingRequest]:
|
|
68
|
+
"""舊單一 waiting row 視為一個來源;新格式的空集合不能復活舊欄位。"""
|
|
69
|
+
raw = row.get("waiting_requests")
|
|
70
|
+
if not isinstance(raw, dict):
|
|
71
|
+
if row.get("status") != "waiting":
|
|
72
|
+
return {}
|
|
73
|
+
owner = f"agent:{row['waiting_agent_id']}" if row.get("waiting_agent_id") else FOREGROUND_OWNER
|
|
74
|
+
raw = {
|
|
75
|
+
owner: {
|
|
76
|
+
"kind": row.get("waiting_kind", ""),
|
|
77
|
+
"detail": row.get("waiting_detail", ""),
|
|
78
|
+
"waiting_for": row.get("waiting_for", ""),
|
|
79
|
+
"since": row.get("last_active", 0.0),
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
requests = {}
|
|
83
|
+
for owner, value in raw.items():
|
|
84
|
+
if not isinstance(owner, str) or not isinstance(value, dict):
|
|
85
|
+
continue
|
|
86
|
+
if owner not in {FOREGROUND_OWNER, UNKNOWN_OWNER} and not owner.startswith("agent:"):
|
|
87
|
+
continue
|
|
88
|
+
since = value.get("since", 0.0)
|
|
89
|
+
if not isinstance(since, (int, float)) or isinstance(since, bool) or not math.isfinite(since):
|
|
90
|
+
since = 0.0
|
|
91
|
+
requests[owner] = WaitingRequest(
|
|
92
|
+
owner,
|
|
93
|
+
**{
|
|
94
|
+
key: value.get(key, "") if isinstance(value.get(key, ""), str) else ""
|
|
95
|
+
for key in ("kind", "detail", "waiting_for", "revision", "tool_use_id")
|
|
96
|
+
},
|
|
97
|
+
since=float(since),
|
|
98
|
+
)
|
|
99
|
+
return requests
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def primary_wait(requests: dict[str, WaitingRequest]) -> WaitingRequest:
|
|
103
|
+
"""優先顯示前景;其餘依加入順序,避免背景活動讓摘要一直換人。"""
|
|
104
|
+
return requests.get(FOREGROUND_OWNER) or next(iter(requests.values()))
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|