Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

- Share Discord's whole-tree Expand/Collapse and paging controls between Error
details and Explore data. Keep error inspection inside the original reply,
preserve its answer/media/files/footer and restore it exactly when hidden.
Inspect only frozen public diagnostics; never rerun a failed tool or model.

- Reserve anonymous durable AI usage rows before model HTTP requests and Gemini
CLI spawn. Refuse unrecorded inference, retain completed results if final
ledger accounting fails, and never retry paid work for bookkeeping. Cover
Expand Down
11 changes: 7 additions & 4 deletions doc/PUBLIC_RESULTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ a page indicator remains visible. There is no rediscovery or execution during
navigation. Opening another pane uses the first page as its restoration base;
paging returns to the frozen document and replaces the temporary pane.

Navigation is public and read-only. Controls select bounded snapshot depth,
page or diagnostic node IDs, never arbitrary paths or command arguments. Each
Navigation is public and read-only. Controls select bounded snapshot depth
or page, never arbitrary paths or command arguments. Each
inline view requires the actual tracked bot reply, channel/thread, frozen
snapshot token and current revision.

Expand All @@ -105,8 +105,11 @@ preserve depth. Depth and paging buttons are disabled at their bounds. There
is no one-field dropdown or separately posted viewer. Hiding restores the exact
original card; reopening starts at the two-level overview.

**Error details** expands the tracked original reply in place. Its navigation
also stays inline, and **Hide error details** restores the exact original card.
**Error details** uses the same two-level overview, whole-tree **Expand** and
**Collapse**, and depth-preserving Previous/Next controls as **Explore data**.
It reads only frozen public diagnostics, not the result-data snapshot. There is
no per-field dropdown. **Hide error details** restores the exact original card.
Navigation never retries a failed tool or makes another model request.
Only JSON-only outbound public presentation is retained for restoration, never
upload streams or raw result data. Restoration is capped at the ordinary
30-reply limit; overflow notices do not offer unrestorable inline controls.
Expand Down
16 changes: 9 additions & 7 deletions src/yetibot/core/adapters/discord.clj
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@
:data (components/interaction-payload text)))

(defn- inline-details! [conn event entry expected
{:keys [action key token node page depth]}]
{:keys [action key token page depth]}]
(binding [*delivery-deadline* (+ (monotonic-time) 2500000000)]
(with-command-lock (:lock entry)
(let [state (:inspection entry)
Expand Down Expand Up @@ -535,10 +535,12 @@
state key (:inspection-base reply) depth page)
"page" (inspection/inline-text-payload
state key (:inspection-base reply) page)
(inspection/inline-error-payload
state key (:inspection-base reply)
(or node 0) (or page 0)
(get-in event [:data :values])))]
"error" (inspection/inline-error-payload
state key (:inspection-base reply) nil 0)
"error-tree" (inspection/inline-error-payload
state key (:inspection-base reply)
depth page)
nil)]
;; Source revisions can change before obtaining this same lock.
;; Recheck immediately before IO; never retry an unknown update.
(when (current?)
Expand Down Expand Up @@ -610,7 +612,7 @@
(get-in @conn [:commands key :inspection]))
(or (not (contains? #{"view" "error-hide" "details"
"details-hide" "data" "data-hide"
"tree" "page"} action))
"tree" "error-tree" "page"} action))
(= token (:token @(:inspection entry))))
(not (and (= action "view") (= kind "d")))
(or (not confirm?)
Expand All @@ -627,7 +629,7 @@
"This command's buttons have expired. Please send the command again.")

(or (contains? #{"error" "error-hide" "details" "details-hide"
"data" "data-hide" "tree" "page"} action)
"data" "data-hide" "tree" "error-tree" "page"} action)
(and (= action "view") (= kind "e")))
(when-not (inline-details! conn event entry expected control)
(interaction-reply! conn event
Expand Down
96 changes: 34 additions & 62 deletions src/yetibot/core/adapters/discord_inspection.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"Bounded opt-in public snapshots and read-only Discord navigation."
(:require [clojure.data.json :as json]
[clojure.string :as str]
[clojure.walk :as walk]
[yetibot.core.adapters.discord-components :as components]))

(defn initial []
Expand Down Expand Up @@ -142,11 +141,11 @@
id)]
{:action "page" :key [channel message] :token token
:page (Long/parseLong page)})
(when-let [[_ channel message token _operation depth page]
(when-let [[_ action channel message token _operation depth page]
(re-matches
#"yb:tree:([0-9]{1,20}):([0-9]{1,20}):([a-f0-9]{16}):([ecpn]):([1-8]):([0-9]{1,2})"
#"yb:(tree|error-tree):([0-9]{1,20}):([0-9]{1,20}):([a-f0-9]{16}):([ecpn]):([1-8]):([0-9]{1,2})"
id)]
{:action "tree" :key [channel message] :token token
{:action action :key [channel message] :token token
:depth (Long/parseLong depth) :page (Long/parseLong page)})
(when-let [[_ channel message token kind node page]
(re-matches
Expand Down Expand Up @@ -318,16 +317,21 @@
(inc newline) (count prefix))]
(recur (subs remaining end) (conj pages (subs remaining 0 end)))))))

(defn inline-data-payload
"Show the frozen public tree at up to two levels initially. Depth/page
controls are stateless and bounded; no paths, providers or commands run."
[state key base depth page]
(let [s @state value (root-value s "d")
(defn- inline-tree-payload
"Render either frozen public snapshot with shared depth and paging controls."
[state key base kind depth page]
(let [s @state
error? (= kind "e")
value (root-value s kind)
action (if error? "error-tree" "tree")
panel (if error? "error" "data")
title (if error? "Error details" "Explore data")
max-depth (reduce max 1 (map #(count (:path %)) (tree value)))
depth (or depth (min 2 max-depth))
page (or page 0)]
(when (and (:sealed? s) (:data? s) (integer? depth)
(<= 1 depth max-depth) (integer? page) (<= 0 page 99))
(when (and (:sealed? s) (get s (if error? :error? :data?))
(integer? depth) (<= 1 depth max-depth)
(integer? page) (<= 0 page 99))
(let [encoded (-> (with-out-str
(json/pprint (depth-preview value depth)
:escape-unicode false))
Expand All @@ -336,14 +340,18 @@
token (:token s)
button (fn [label operation level page disabled?]
{:type 2 :style 2 :label label :disabled disabled?
:custom_id (str "yb:tree:" (str/join ":" key) ":" token
":" operation ":" level ":" page)})]
:custom_id (str "yb:" action ":" (str/join ":" key) ":"
token ":" operation ":" level ":" page)})]
(when-let [preview (get pages page)]
(components/inline-pane-payload
base
[{:type 10 :content
(str "**Explore data**\nDepth " depth " · Page " (inc page)
(str "**" title "**\nDepth " depth " · Page " (inc page)
" of " (count pages) "\n\n```json\n" preview "\n```"
(when error?
(str "\nOnly retained public diagnostics are shown."
" Raw responses and private data were not retained"
" for this view."))
"\n-# Retained public snapshot · collapsed branches show type/count."
" Large values may be shortened.")}
{:type 1 :components
Expand All @@ -354,53 +362,17 @@
(conj (button "Previous" "p" depth (max 0 (dec page)) (zero? page))
(button "Next" "n" depth (min (dec (count pages)) (inc page))
(= page (dec (count pages))))))}]
(str (components/button-id "data" key) ":" token)
(str (components/button-id "data-hide" key) ":" token) "Hide data"))))))
(cond-> (components/button-id panel key)
(not error?) (str ":" token))
(str (components/button-id (str panel "-hide") key) ":" token)
(if error? "Hide error details" "Hide data")))))))

(defn inline-data-payload
"Inspect retained public data without providers or command execution."
[state key base depth page]
(inline-tree-payload state key base "d" depth page))

(defn inline-error-payload
"Expand reviewed diagnostics inside a retained public reply. The base is
JSON-only outbound presentation, not raw command/provider data. Never upload
files or clear attachments; collapsing restores the exact original card."
[state key base node page selected]
(when-let [pane (view-payload state key "e" node page selected
{:preview-limit 1600 :inline? true})]
(when (and (= 1 (count (:components base)))
(= 17 (get-in base [:components 0 :type])))
(let [children (get-in base [:components 0 :components])
elapsed-index
(last (keep-indexed
(fn [i c]
(when (and (= 10 (:type c))
(re-matches #"-# [0-9]+[.][0-9]s"
(or (:content c) ""))) i)) children))
elapsed (when elapsed-index (get children elapsed-index))
children
(into []
(keep-indexed
(fn [i c]
(when (not= i elapsed-index)
(if (and (= 10 (:type c)) (> (count (:content c)) 1400))
;; Leave room for even a long closing Markdown fence.
(update c :content
#(str (components/answer-preview % 650)
"\n-# Error message shortened while details are open."
" Hide details to restore it."))
c))) children))
hide-id (str "yb:error-hide:" (str/join ":" key) ":" (:token @state))
children (walk/postwalk
(fn [c]
(if (and (map? c)
(= (components/button-id "error" key)
(:custom_id c)))
(assoc c :label "Hide error details" :custom_id hide-id)
c)) children)
children (cond-> (into (conj children {:type 14 :divider true :spacing 1})
(get-in pane [:components 0 :components]))
elapsed (conj elapsed))
result (assoc-in base [:components 0 :components] children)
nodes (filter map? (tree-seq coll? seq result))]
(when (and (<= (reduce + 0 (keep #(when (= 10 (:type %))
(count (:content %))) nodes))
components/max-text-length)
(<= (count (filter :type nodes)) 40))
result)))))
"Inspect retained public diagnostics using the same controls as public data."
[state key base depth page]
(inline-tree-payload state key base "e" depth page))
64 changes: 44 additions & 20 deletions test/yetibot/core/test/adapters/discord_inspection.clj
Original file line number Diff line number Diff line change
Expand Up @@ -247,28 +247,52 @@
(count (filter #(= :create (first %)) @calls)) => 1
@executions => 1))))

(fact "inline error navigation preserves the answer and rejects stale or foreign cards"
(session {:result/error "Weather unavailable"
:result/public-error {:http-status 503 :stage "weather lookup"}}
(fn [calls _ event! click!]
(fact "error trees share data depth controls and reject stale or foreign cards"
(session {:result/error "One model failed"
:result/public-error
{:reason "partial-result"
:calls [{:model "example-model" :outcome "error"
:diagnostic {:code "request-rejected" :http_status 400}}]}}
(fn [calls conn event! click!]
(await! (click! {:custom-id (:custom_id
(button (last (last @calls)) "Error details"))}))
(let [menu (some #(when (= 3 (:type %)) %) (nodes (last (last @calls))))
data {:custom-id (:custom_id menu)
:values [(:value (first (:options menu)))]}]
(await! (click! data))
(str/includes? (text (last (last @calls))) "Weather unavailable") => true
(some? (button (last (last @calls)) "Back")) => true
(await! (click! data {:message {:id "another-bot-message"
:author {:id "bot"}}}))
(second (last @calls)) => 4
(str/includes? (text (last (last @calls))) "expired") => true
(await! (event! :message-update
(assoc source :content "!inspect-fixture edited")))
(await! (click! data))
(second (last @calls)) => 4
(str/includes? (text (last (last @calls))) "expired") => true
@executions => 2))))
(let [initial (last (last @calls))
expand (button initial "Expand")]
(str/includes? (text initial) "Depth 2") => true
(some #(when (= 3 (:type %)) %) (nodes initial)) => nil
(some? expand) => true
(when expand
(let [data {:custom-id (:custom_id expand)}
token (:token @(get-in @conn
[:commands ["200" "100"] :inspection]))]
(await! (click! data))
(second (last @calls)) => 7
(str/includes? (text (last (last @calls))) "Depth 3") => true
(await! (click! {:custom-id (:custom_id
(button (last (last @calls)) "Expand"))}))
(str/includes? (text (last (last @calls))) "request-rejected") => true
(str/includes? (text (last (last @calls))) "One model failed") => true
(await! (click! {:custom-id (:custom_id
(button (last (last @calls)) "Collapse"))}))
(str/includes? (text (last (last @calls))) "Depth 3") => true
(doseq [overrides [{:channel-id "another-channel"}
{:message {:id "another-bot-message"
:author {:id "bot"}}}
{:message {:id "answer"
:author {:id "other"}}}]]
(await! (click! data overrides))
(second (last @calls)) => 4)
(doseq [id [(str/replace (:custom-id data) token "0000000000000000")
(str/replace (:custom-id data) #":[0-9]+$" ":99")
(str "yb:error-tree:200:100:" token ":e:8:0")]]
(await! (click! {:custom-id id}))
(second (last @calls)) => 4)
(await! (event! :message-update
(assoc source :content "!inspect-fixture edited")))
(await! (click! data))
(second (last @calls)) => 4
(str/includes? (text (last (last @calls))) "expired") => true
@executions => 2))))))

(fact "overflow replies do not offer unrestorable inline controls"
(let [state (atom (assoc (inspection/initial) :sealed? true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
(defn- page [state depth n]
(inspect/inline-data-payload state ["200" "100"] (base state "Answer") depth n))
(defn- preview [data]
(some #(when (str/starts-with? (or (:content %) "") "**Explore data**")
(some #(when (some (fn [title]
(str/starts-with? (or (:content %) "") title))
["**Explore data**" "**Error details**"])
(second (re-find #"(?s)```json\n(.*?)\n```" (:content %)))) (maps data)))

(fact "public capture does not mutate pipeline data or trust serialized tags"
Expand Down Expand Up @@ -196,3 +198,48 @@
(contains? expanded :attachments) => false
(some? (button expanded "Hide data")) => true
(button original "Hide data") => nil)))

(fact "error depth and paging keep the complete retained JSON and typed controls"
(let [state (atom (inspect/initial))
note (apply str (repeat 80 "🙂`\""))
value {:calls (mapv (fn [i]
{:call i :outcome "error"
:diagnostic {:code "request-rejected"
:note (str i note)}})
(range 7))}]
(binding [public-result/*capture* #(inspect/capture! state %)]
(public-result/observe!
(public-result/reviewed {:result/error "Some calls failed"
:result/public-error value})))
(swap! state assoc :sealed? true)
(let [original (-> (components/payload
{:content "Five answers retained" :error? true
:command-key ["200" "100"] :elapsed "1.2s"})
(dissoc :allowed-mentions))
page #(inspect/inline-error-payload state ["200" "100"] original %1 %2)
initial (page nil 0)
pages (loop [n 0 pages []]
(let [view (page 4 n)
pages (conj pages (preview view))]
(some? view) => true
(<= (count (text view)) 4000) => true
(:disabled (button view "Expand")) => true
(if (:disabled (button view "Next")) pages
(recur (inc n) pages))))]
(> (count pages) 1) => true
(json/read-str (apply str pages)) => (first (:errors @state))
(str/includes? (text initial) "Depth 2") => true
(str/includes? (text initial) "not retained") => true
(:disabled (button (page 1 0) "Collapse")) => true
(:action (inspect/parse-control (:custom_id (button initial "Expand"))))
=> "error-tree"
(:action (inspect/parse-control (:custom_id (button (page 4 0) "Next"))))
=> "error-tree"
(some? (button initial "Hide error details")) => true
(button initial "Hide data") => nil
(page 0 0) => nil
(page 5 0) => nil
(page 2 99) => nil
(inspect/inline-data-payload state ["200" "100"] original 2 0) => nil
(swap! state assoc :error? false)
(page 2 0) => nil)))