265 lines
15 KiB
Plaintext
265 lines
15 KiB
Plaintext
-------------------------- MODULE ApprovalRouting --------------------------
|
||
(***************************************************************************)
|
||
(* AnyBot「等你允許的卡片」:一個回答落在哪一張卡上。 *)
|
||
(* *)
|
||
(* 一個 Bot 一次只等一張卡:agent/steps.rs 的 Step::ask_person 送出 *)
|
||
(* NeedApproval,agent.rs 的 wait_answer 一直等到這張卡自己的 reply 有回答。*)
|
||
(* 大腦(app.rs 的 App)把卡放在 approvals[bot],每張新卡在 on_new_approval *)
|
||
(* 發一把 ask_keys[bot]。能回答的地方有四個: *)
|
||
(* desk 大腦自己的視窗(approval_dock/approval_body → answer()) *)
|
||
(* page 手機頁面上的卡(mobile/index.html renderDock、renderQuestion) *)
|
||
(* banner 手機通知上的「允許/拒絕」(mobile/sw.js,帶 d.ask = key) *)
|
||
(* screen 螢幕電腦(mirror:answer() 的 mir.on 分支) *)
|
||
(* desk 在同一個畫面格裡看到卡、處理按鍵、呼叫 answer();另外三個看到的是 *)
|
||
(* 某個時候抄過去的卡(和它的 key),回答要經過網路,到大腦時由 *)
|
||
(* remote_ui.rs 的 Cmd::Answer 決定收不收。手機頁面可能是更新前就開著的舊 *)
|
||
(* 頁面、螢幕電腦可能是舊版:它們的回答不帶 key。 *)
|
||
(* *)
|
||
(* 卡有兩種:「允許這個操作」(allow:Action、要開電腦工具…)和「需要處理」*)
|
||
(* (handoff:按鈕是「我完成了/跳過」,送出去也是 once/reject)。 *)
|
||
(* *)
|
||
(* 規則(cfg 的 INVARIANT): *)
|
||
(* AllowOnlyWhatWasSeen 「允許」「一律允許」(在需要處理的卡上是「我完成 *)
|
||
(* 了」)只會讓按的人當時看到的那張卡通過。 *)
|
||
(* RuleOnlyWhatWasSeen 「一律允許」記到 Bot 身上的類別,一定是按的人當 *)
|
||
(* 時看到的那張卡上寫的。 *)
|
||
(* NoticeNeverFinishesHandoff 手機通知上的按鈕只回答「允許這個操作」的 *)
|
||
(* 卡,絕不會變成「我完成了」。 *)
|
||
(* *)
|
||
(* 每個動作對應哪段程式碼:.claude/skills/verify-approval-routing/SKILL.md *)
|
||
(* 的「對照表」。程式碼一改,先照那張表重新對過,再相信這裡的結果。 *)
|
||
(***************************************************************************)
|
||
EXTENDS Naturals, FiniteSets
|
||
|
||
CONSTANTS
|
||
N, \* 這段時間 Bot 會問幾次(請求 1..N,一個問完才問下一個)
|
||
Surfaces, \* 有哪些地方能回答:{"desk", "page", "banner", "screen"} 的子集
|
||
\* 程式事實:每一條都對應程式裡的一行,見 SKILL.md「程式事實」。
|
||
FreshKeyPerCard, \* on_new_approval:每張新卡一把新的 ask_keys(random_hex)
|
||
KeyChecked, \* Cmd::Answer:帶 key 的回答,key 要是現在在等的那張卡的才算
|
||
KeylessRefused, \* Cmd::Answer:沒帶 key 的回答(舊頁面、舊版螢幕電腦)不算
|
||
NoticeFitsChecked, \* Cmd::Answer:通知上的回答只收 notice_answers 的卡、只收 once/reject
|
||
PageSendsKey, \* 手機頁面(renderDock、renderQuestion)送出 call = 那張卡的 key
|
||
ScreenSendsKey \* 螢幕電腦 answer()(mir.on)送出 call = 大腦給那張卡的 key
|
||
|
||
None == 0 \* 沒有卡/沒有 key
|
||
NoView == <<None, None>> \* 畫面上沒有卡;有卡時是 <<請求, 那張卡的 key>>
|
||
Grant(r) == r \* 每個請求的「一律允許」類別都不一樣(npm install、修改檔案…)
|
||
Kind(r) == IF r = N THEN "handoff" ELSE "allow" \* 最後一件是「需要處理」,其他是允許請求
|
||
Allow == {"once", "always"} \* 「允許」「一律允許」(需要處理的卡上 once 是「我完成了」)
|
||
|
||
\* 卡上的按鈕(大腦視窗 approval_body/handoff_card、手機 renderDock、螢幕電腦一樣):
|
||
Buttons(r) == IF Kind(r) = "handoff" THEN {"once", "reject"} ELSE {"once", "always", "reject"}
|
||
BannerButtons == {"once", "reject"} \* sw.js:allow → once、deny → reject
|
||
\* 手機頁面送出的 key:現在的頁面送那張卡的;更新前就開著的舊頁面什麼都不送。
|
||
PageKeys(v) == IF PageSendsKey THEN {v[2], None} ELSE {None}
|
||
\* 螢幕電腦送出的 key:現在的版本送大腦給的;舊版什麼都不送。
|
||
ScreenKeys(v) == IF ScreenSendsKey THEN {v[2], None} ELSE {None}
|
||
|
||
VARIABLES
|
||
bot, \* Bot 那一邊:"thinking" 下一步還沒問、"sent" NeedApproval 在路上、
|
||
\* "waiting" 在 wait_answer 等、"done" 不再問
|
||
asked, \* 問到第幾個請求了
|
||
card, \* 大腦的 approvals[bot]:正在等的請求,None 表示沒有
|
||
askKey, \* 大腦的 ask_keys[bot](卡被回答後不清掉)
|
||
keys, \* 發過幾把 key(新 key 就是下一個號碼)
|
||
page, \* 手機頁面上畫的卡(S.approvals,含 key)
|
||
screen, \* 螢幕電腦上畫的卡(它的 approvals 和 mir.keys,從大腦同步來)
|
||
banner, \* 手機通知上的卡(同一個 Bot 新的通知蓋掉舊的:tag "bot-<id>")
|
||
inflight, \* 已經送出、大腦還沒處理的回答
|
||
\* 記錄(程式裡沒有這些值,只拿來檢查規則;規則成立時一直是 FALSE,不讓狀態變多):
|
||
wrongAllow, \* 有沒有一個「允許」「一律允許」「我完成了」交給了按的人沒看到的卡
|
||
wrongRule, \* 有沒有一個「一律允許」記下了按的人沒看到的那一類
|
||
noticeOnHandoff, \* 有沒有手機通知上的按鈕回答了「需要處理」的卡
|
||
landedFrom \* 哪些地方(page/banner/screen)的回答真的交給過一張卡
|
||
|
||
vars == <<bot, asked, card, askKey, keys, page, screen, banner, inflight, wrongAllow, wrongRule, noticeOnHandoff, landedFrom>>
|
||
|
||
Views == (0..N) \X (0..N)
|
||
Answers == {"once", "always", "reject"}
|
||
Msg == [from: {"page", "banner", "screen"}, ans: Answers, key: 0..N, seen: 1..N, notice: BOOLEAN]
|
||
|
||
TypeOK ==
|
||
/\ bot \in {"thinking", "sent", "waiting", "done"}
|
||
/\ asked \in 0..N
|
||
/\ card \in 0..N
|
||
/\ askKey \in 0..N
|
||
/\ keys \in 0..N
|
||
/\ page \in Views /\ screen \in Views /\ banner \in Views
|
||
/\ inflight \subseteq Msg
|
||
/\ wrongAllow \in BOOLEAN /\ wrongRule \in BOOLEAN /\ noticeOnHandoff \in BOOLEAN
|
||
/\ landedFrom \subseteq {"page", "banner", "screen"}
|
||
|
||
Init ==
|
||
/\ bot = "thinking"
|
||
/\ asked = 0
|
||
/\ card = None
|
||
/\ askKey = None
|
||
/\ keys = 0
|
||
/\ page = NoView
|
||
/\ screen = NoView
|
||
/\ banner = NoView
|
||
/\ inflight = {}
|
||
/\ wrongAllow = FALSE
|
||
/\ wrongRule = FALSE
|
||
/\ noticeOnHandoff = FALSE
|
||
/\ landedFrom = {}
|
||
|
||
(* 大腦把回答交給「現在在等的那張卡」:app.rs answer() — *)
|
||
(* approvals.remove(&bot_id);Always 而且是 Action 就把 g 推進 *)
|
||
(* b.allow_rules;a.reply.send(answer)。Bot 的 wait_answer 收到後往下做。 *)
|
||
(* `seen`(按的人看到的請求)、`from`("desk" 是大腦自己的視窗)只拿來寫記錄。*)
|
||
Settle(ans, seen, from) ==
|
||
/\ wrongAllow' = (wrongAllow \/ (ans \in Allow /\ card # seen))
|
||
/\ wrongRule' = (wrongRule \/ (ans = "always" /\ Kind(card) = "allow" /\ Grant(card) # Grant(seen)))
|
||
/\ noticeOnHandoff' = (noticeOnHandoff \/ (from = "banner" /\ Kind(card) = "handoff"))
|
||
/\ landedFrom' = IF from = "desk" THEN landedFrom ELSE landedFrom \cup {from}
|
||
/\ card' = None
|
||
/\ bot' = IF asked = N THEN "done" ELSE "thinking"
|
||
|
||
(* Bot 要做一件要問的事:Step::ask_person 送出 ToolStatus(Waiting) 和 *)
|
||
(* NeedApproval(帶這張卡自己的 reply),然後進 wait_answer。 *)
|
||
BotAsks ==
|
||
/\ bot = "thinking"
|
||
/\ asked < N
|
||
/\ asked' = asked + 1
|
||
/\ bot' = "sent"
|
||
/\ UNCHANGED <<card, askKey, keys, page, screen, banner, inflight, wrongAllow, wrongRule, noticeOnHandoff, landedFrom>>
|
||
|
||
(* 大腦的 drain_events 拿到 NeedApproval:approvals.insert(id, a),接著 *)
|
||
(* on_new_approval → new_approval_keyed:ask_keys.insert(bot_id, 新 key); *)
|
||
(* 不在 App 前面時推一則手機通知——notice_answers 的卡(允許請求)才帶 *)
|
||
(* 這把 key 和「允許/拒絕」,其他的只是「打開聊天」。 *)
|
||
CardShows ==
|
||
/\ bot = "sent"
|
||
/\ bot' = "waiting"
|
||
/\ card' = asked
|
||
/\ IF FreshKeyPerCard \/ askKey = None
|
||
THEN /\ keys' = keys + 1
|
||
/\ askKey' = keys + 1
|
||
ELSE UNCHANGED <<keys, askKey>>
|
||
/\ banner' \in IF "banner" \in Surfaces /\ Kind(asked) = "allow" THEN {banner, <<asked, askKey'>>} ELSE {banner}
|
||
/\ UNCHANGED <<asked, page, screen, inflight, wrongAllow, wrongRule, noticeOnHandoff, landedFrom>>
|
||
|
||
(* 卡沒被回答就不見了:按停止、重新啟動 Bot、沒人在旁邊等超過 15 分鐘 *)
|
||
(* (watchdog)。approvals.remove 丟掉 reply,wait_answer 拿到 Err → Reject;*)
|
||
(* 還在路上的 NeedApproval 屬於舊的一輪,drain_events 不收。之後使用者再傳 *)
|
||
(* 一則訊息,Bot 繼續問下一件。 *)
|
||
TurnStops ==
|
||
/\ bot \in {"sent", "waiting"}
|
||
/\ card' = None
|
||
/\ bot' = IF asked = N THEN "done" ELSE "thinking"
|
||
/\ UNCHANGED <<asked, askKey, keys, page, screen, banner, inflight, wrongAllow, wrongRule, noticeOnHandoff, landedFrom>>
|
||
|
||
(* 大腦自己的視窗:approval_dock 畫出這張卡(approval_body,需要處理的是 *)
|
||
(* handoff_card),同一格裡的按鈕或 Enter/Esc 直接呼叫 answer()。看到的就是 *)
|
||
(* 現在的卡。 *)
|
||
YouAnswerOnDesk ==
|
||
/\ "desk" \in Surfaces
|
||
/\ card # None
|
||
/\ \E ans \in Buttons(card) : Settle(ans, card, "desk")
|
||
/\ UNCHANGED <<asked, askKey, keys, page, screen, banner, inflight>>
|
||
|
||
(* 手機頁面的長輪詢 api/state 帶回 approvals(approval_json,含這張卡的 *)
|
||
(* key),apply() → renderDock 重畫。 *)
|
||
PageRefreshes ==
|
||
/\ "page" \in Surfaces
|
||
/\ page' = IF card = None THEN NoView ELSE <<card, askKey>>
|
||
/\ UNCHANGED <<bot, asked, card, askKey, keys, screen, banner, inflight, wrongAllow, wrongRule, noticeOnHandoff, landedFrom>>
|
||
|
||
(* 手機頁面上按卡片的按鈕(允許/一律允許/拒絕、我完成了/跳過、問題卡的 *)
|
||
(* 送出/略過、「全部允許,之後直接操作」):清掉手機上的卡,POST *)
|
||
(* api/bots/{id}/answer {answer, call: a.key};舊頁面不帶 call。 *)
|
||
YouTapOnPage ==
|
||
/\ "page" \in Surfaces
|
||
/\ page # NoView
|
||
/\ \E ans \in Buttons(page[1]), k \in PageKeys(page) :
|
||
inflight' = inflight \cup {[from |-> "page", ans |-> ans, key |-> k, seen |-> page[1], notice |-> FALSE]}
|
||
/\ page' = NoView
|
||
/\ UNCHANGED <<bot, asked, card, askKey, keys, screen, banner, wrongAllow, wrongRule, noticeOnHandoff, landedFrom>>
|
||
|
||
(* 手機通知上按「允許」或「拒絕」:sw.js notificationclick 關掉通知, *)
|
||
(* 頁面 POST {answer, call: d.ask, from: "notice"}。 *)
|
||
YouTapBanner ==
|
||
/\ banner # NoView
|
||
/\ \E ans \in BannerButtons :
|
||
inflight' = inflight \cup {[from |-> "banner", ans |-> ans, key |-> banner[2], seen |-> banner[1], notice |-> TRUE]}
|
||
/\ banner' = NoView
|
||
/\ UNCHANGED <<bot, asked, card, askKey, keys, page, screen, wrongAllow, wrongRule, noticeOnHandoff, landedFrom>>
|
||
|
||
(* 螢幕電腦從大腦同步:mirror_ui.rs 把大腦的 approvals 換進來, *)
|
||
(* 每張卡的 key 記在 mir.keys。 *)
|
||
ScreenRefreshes ==
|
||
/\ "screen" \in Surfaces
|
||
/\ screen' = IF card = None THEN NoView ELSE <<card, askKey>>
|
||
/\ UNCHANGED <<bot, asked, card, askKey, keys, page, banner, inflight, wrongAllow, wrongRule, noticeOnHandoff, landedFrom>>
|
||
|
||
(* 螢幕電腦上回答:answer() 的 mir.on 分支 approvals.remove, *)
|
||
(* mirror_act {"bot", "action": "answer", "answer", "text", "call"} 送到 *)
|
||
(* 大腦的 api/sync/act;舊版螢幕電腦不帶 call。 *)
|
||
YouAnswerOnScreen ==
|
||
/\ "screen" \in Surfaces
|
||
/\ screen # NoView
|
||
/\ \E ans \in Buttons(screen[1]), k \in ScreenKeys(screen) :
|
||
inflight' = inflight \cup {[from |-> "screen", ans |-> ans, key |-> k, seen |-> screen[1], notice |-> FALSE]}
|
||
/\ screen' = NoView
|
||
/\ UNCHANGED <<bot, asked, card, askKey, keys, page, banner, wrongAllow, wrongRule, noticeOnHandoff, landedFrom>>
|
||
|
||
(* 大腦處理一個遠端回答:remote_ui.rs Cmd::Answer。 *)
|
||
(* this_card:有卡在等、帶了 key、key 是現在 ask_keys 的那把。 *)
|
||
(* fits:不是通知,或是 notice_answers 的卡而且是 once/reject。 *)
|
||
(* 兩個都成立才交給 answer();不然什麼都不做(有卡在等時寫「沒有用上」)。 *)
|
||
ThisCard(m) ==
|
||
/\ card # None
|
||
/\ IF m.key = None THEN ~KeylessRefused ELSE (~KeyChecked \/ askKey = m.key)
|
||
Fits(m) == ~m.notice \/ ~NoticeFitsChecked \/ (Kind(card) = "allow" /\ m.ans \in BannerButtons)
|
||
|
||
BrainTakesAnswer ==
|
||
/\ \E m \in inflight :
|
||
/\ inflight' = inflight \ {m}
|
||
/\ IF ThisCard(m) /\ Fits(m)
|
||
THEN Settle(m.ans, m.seen, m.from)
|
||
ELSE UNCHANGED <<bot, card, wrongAllow, wrongRule, noticeOnHandoff, landedFrom>>
|
||
/\ UNCHANGED <<asked, askKey, keys, page, screen, banner>>
|
||
|
||
(* 不再問了,什麼都不變(讓 TLC 不把結束當成卡死)。 *)
|
||
Finished ==
|
||
/\ bot = "done"
|
||
/\ UNCHANGED vars
|
||
|
||
Next ==
|
||
\/ BotAsks
|
||
\/ CardShows
|
||
\/ TurnStops
|
||
\/ YouAnswerOnDesk
|
||
\/ PageRefreshes
|
||
\/ YouTapOnPage
|
||
\/ YouTapBanner
|
||
\/ ScreenRefreshes
|
||
\/ YouAnswerOnScreen
|
||
\/ BrainTakesAnswer
|
||
\/ Finished
|
||
|
||
Spec == Init /\ [][Next]_vars
|
||
|
||
(* 界限(cfg 的 CONSTRAINT):每個地方同時最多一個回答在路上。程式沒有這 *)
|
||
(* 個限制(手機可以在上一個 POST 回來前又按一次),這是為了讓 TLC 跑得完。 *)
|
||
OneAnswerOnTheWayPerSurface ==
|
||
\A s \in {"page", "banner", "screen"} : Cardinality({m \in inflight : m.from = s}) <= 1
|
||
|
||
(* 「允許」「一律允許」(「我完成了」)只會讓按的人當時看到的那張卡通過。 *)
|
||
AllowOnlyWhatWasSeen == ~wrongAllow
|
||
|
||
(* 「一律允許」記下的類別,一定是按的人當時看到的那張卡上寫的。 *)
|
||
RuleOnlyWhatWasSeen == ~wrongRule
|
||
|
||
(* 手機通知上的按鈕只回答允許請求,絕不會變成需要處理的「我完成了/跳過」。*)
|
||
NoticeNeverFinishesHandoff == ~noticeOnHandoff
|
||
|
||
(* 檢查修法沒有把路堵死(CanAnswer.cfg,要「違反」):有一種走法,手機頁面、 *)
|
||
(* 手機通知、螢幕電腦的回答都各自真的交給了一張卡。哪天某個地方再也答不了 *)
|
||
(* (例如手機不送 key),這一條會「通過」,run.sh 會說不預期。 *)
|
||
SomePlaceCannotAnswer == ~({"page", "banner", "screen"} \subseteq landedFrom)
|
||
|
||
=============================================================================
|