From 82ac4194b450fbc33ae25b96ee91d4a274ba7565 Mon Sep 17 00:00:00 2001 From: pigmagicfly <2831850183@qq.com> Date: Thu, 26 Nov 2026 14:12:50 +0800 Subject: [PATCH 1/3] =?UTF-8?q?[1345]=20=E5=BC=82=E6=AD=A5=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E7=BA=AF=E6=96=87=E6=9C=AC=20Diff=20=E5=92=8C?= =?UTF-8?q?=E6=9C=AC=E8=BD=AE=E5=87=80=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/liii/pi/app/interactive/app-diff.scm | 194 ++++++++++++ app/liii/pi/app/interactive/app-events.scm | 25 +- app/liii/pi/app/interactive/app-history.scm | 1 + app/liii/pi/app/interactive/app.scm | 7 +- app/liii/pi/app/interactive/diff-capture.scm | 293 ++++++++++++++++++ app/liii/pi/app/interactive/diff-worker.scm | 195 ++++++++++++ app/liii/pi/app/interactive/renderers.scm | 2 +- app/liii/pi/app/interactive/run.scm | 1 + app/liii/pi/app/interactive/state.scm | 6 +- app/liii/pi/app/interactive/tool-diff.scm | 259 ++++++++++++++++ .../pi/app/interactive/views/tool-card.scm | 75 ++++- app/liii/pi/app/interactive/worker.scm | 121 ++++---- .../interactive/app-diff/app-diff-test.scm | 150 +++++++++ .../app-diff/diff-history-test.scm | 79 +++++ .../diff-capture/diff-capture-test.scm | 148 +++++++++ .../diff-worker/diff-performance-test.scm | 133 ++++++++ .../diff-worker/diff-worker-test.scm | 88 ++++++ .../interactive/tool-diff/tool-diff-test.scm | 57 ++++ 18 files changed, 1749 insertions(+), 85 deletions(-) create mode 100644 app/liii/pi/app/interactive/app-diff.scm create mode 100644 app/liii/pi/app/interactive/diff-capture.scm create mode 100644 app/liii/pi/app/interactive/diff-worker.scm create mode 100644 app/liii/pi/app/interactive/tool-diff.scm create mode 100644 app/tests/liii/pi/app/interactive/app-diff/app-diff-test.scm create mode 100644 app/tests/liii/pi/app/interactive/app-diff/diff-history-test.scm create mode 100644 app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm create mode 100644 app/tests/liii/pi/app/interactive/diff-worker/diff-performance-test.scm create mode 100644 app/tests/liii/pi/app/interactive/diff-worker/diff-worker-test.scm create mode 100644 app/tests/liii/pi/app/interactive/tool-diff/tool-diff-test.scm diff --git a/app/liii/pi/app/interactive/app-diff.scm b/app/liii/pi/app/interactive/app-diff.scm new file mode 100644 index 00000000..c974b76c --- /dev/null +++ b/app/liii/pi/app/interactive/app-diff.scm @@ -0,0 +1,194 @@ +;; Copyright (C) 2026 The Goldfish Scheme Authors +;; Licensed under the MIT License + +(define-library (liii pi app interactive app-diff) + (export %pi-app-handle-diff-event! %pi-app-apply-diff! %pi-app-settle-diff! + %pi-app-reset-diff! + ) ;export + (import (scheme base) + (liii string) + (liii hash-table) + (liii tui) + (liii pi app interactive state) + (liii pi app interactive renderers) + (liii pi app interactive tool-diff) + (liii pi app interactive views tool-card) + (liii pi app interactive status-card) + ) ;import + (begin + (define-record-type + (%make-app-diff-run id early settled? summary) + %app-diff-run? + (id %app-diff-run-id) + (early %app-diff-run-early) + (settled? %app-diff-run-settled? %app-diff-run-settled-set!) + (summary %app-diff-run-summary %app-diff-run-summary-set!) + ) ;define-record-type + + (define (%pi-app-reset-diff! app) + ;; Dispatch retires the old live index even if the optional run marker + ;; is dropped by a saturated event channel. + (%pi-app-tool-cards-set! app '()) + (%pi-app-diff-run-set! app #f) + ) ;define + + (define (%pi-app-apply-diff! app id card) + (let ((run (%pi-app-diff-run app))) + (when run + (let* ((early (%app-diff-run-early run)) (result (hash-table-ref early id))) + (when (pair? result) + (let ((applied (%tool-card-apply-diff! card (car result) (cadr result) (caddr result)) + ) ;applied + ) ; + (when applied + (hash-table-set! early id 'consumed) + ) ;when + (when (eq? applied 'changed) + (scroll-view-invalidate! (pi-app-scroll-view app)) + ) ;when + ) ;let + ) ;when + ) ;let* + ) ;when + ) ;let + ) ;define + + (define (%pi-app-show-diff-summary! app run) + (let ((entries (%app-diff-run-summary run))) + (when (and (%app-diff-run-settled? run) (list? entries)) + (%app-diff-run-summary-set! run 'shown) + (unless (null? entries) + (let ((box (make-container))) + (container-add! box (make-status-card "Turn changes")) + (for-each + (lambda (entry) + (let ((path (%clean-tool-display-text (car entry)))) + (container-add! box + (make-status-card (string-append (string-join (string-split path "\n") " ") + " " + (%tool-diff-counts (cadr entry) (caddr entry)) + ) ;string-append + ) ;make-status-card + ) ;container-add! + ) ;let + ) ;lambda + entries + ) ;for-each + (container-add! (%pi-app-chat app) box) + (scroll-view-invalidate! (pi-app-scroll-view app)) + ) ;let + ) ;unless + ) ;when + ) ;let + ) ;define + + (define (%pi-app-settle-diff! app) + (let ((run (%pi-app-diff-run app))) + (when run + (%app-diff-run-settled-set! run #t) + (%pi-app-show-diff-summary! app run) + ) ;when + ) ;let + ) ;define + + (define (%diff-startable? event) + (and (= (length event) 2) (exact-integer? (cadr event)) (> (cadr event) 0)) + ) ;define + + (define (%diff-line-count? n) + (and (exact-integer? n) (<= 0 n %diff-lines-count-limit)) + ) ;define + + (define (%diff-summary-entry? entry) + (and (list? entry) + (= (length entry) 3) + (string? (car entry)) + (%diff-line-count? (cadr entry)) + (%diff-line-count? (caddr entry)) + (> (+ (cadr entry) (caddr entry)) 0) + ) ;and + ) ;define + + (define (%diff-summary-entries? entries) + (and (list? entries) + (<= (length entries) 16) + (let loop + ((rest entries)) + (or (null? rest) (and (%diff-summary-entry? (car rest)) (loop (cdr rest)))) + ) ;let + ) ;and + ) ;define + + (define (%diff-result? event) + (and (= (length event) 8) + (string? (caddr event)) + (let ((text (list-ref event 4)) + (added (list-ref event 5)) + (removed (list-ref event 6)) + ) ; + (or (and (not text) (not added) (not removed)) + (and (or (not text) (string? text)) + (%diff-line-count? added) + (%diff-line-count? removed) + ) ;and + ) ;or + ) ;let + ) ;and + ) ;define + + (define (%pi-app-handle-diff-event! app event) + ;; Only internal events reach this boundary; ignore malformed payloads + ;; rather than letting optional display work interrupt the event loop. + (when (and (list? event) (pair? event) (pair? (cdr event))) + (%pi-app-dispatch-diff! app event) + ) ;when + ) ;define + + (define (%pi-app-dispatch-diff! app event) + (let ((id (cadr event)) (run (%pi-app-diff-run app))) + (cond + ((and (eq? (car event) 'run_started) (%diff-startable? event)) + ;; Agent events are ordered on their channel. A reused call ID + ;; in a live run must not resolve to an earlier/history card. + (%pi-app-tool-cards-set! app '()) + (%pi-app-diff-run-set! app (%make-app-diff-run id (make-hash-table) #f #f)) + ) ; + ((and run (equal? id (%app-diff-run-id run))) + (case (car event) + ((diff_result) + (when (%diff-result? event) + (%pi-app-handle-diff-result! app event run) + ) ;when + ) ; + ((turn_diff_summary) + (when (and (not (%app-diff-run-summary run)) + (= (length event) 3) + (%diff-summary-entries? (caddr event)) + ) ;and + (%app-diff-run-summary-set! run (caddr event)) + (%pi-app-show-diff-summary! app run) + ) ;when + ) ; + ) ;case + ) ; + ) ;cond + ) ;let + ) ;define + (define (%pi-app-handle-diff-result! app event run) + (let* ((call-id (caddr event)) + (early (%app-diff-run-early run)) + (card (assoc call-id (%pi-app-tool-cards app))) + ) ; + (unless (hash-table-ref early call-id) + (hash-table-set! early + call-id + (list (list-ref event 4) (list-ref event 5) (list-ref event 6)) + ) ;hash-table-set! + (when card + (%pi-app-apply-diff! app call-id (cdr card)) + ) ;when + ) ;unless + ) ;let* + ) ;define + ) ;begin +) ;define-library diff --git a/app/liii/pi/app/interactive/app-events.scm b/app/liii/pi/app/interactive/app-events.scm index ba6c3464..dbec6b9e 100644 --- a/app/liii/pi/app/interactive/app-events.scm +++ b/app/liii/pi/app/interactive/app-events.scm @@ -24,6 +24,7 @@ (liii pi app interactive views) (liii pi app interactive working) (liii pi app interactive app-bash) + (liii pi app interactive app-diff) (liii pi app interactive bash) (liii pi app interactive app-name) (liii pi app interactive app-queue) @@ -150,7 +151,8 @@ (if (not msg) rendered (begin - ;; 新任务开始:关闭上一轮引用,已显示的 thinking 与正文内容保留 + ;; 新任务不接受上一轮迟到的展示事件;历史卡片仍留在聊天区。 + (%pi-app-reset-diff! app) (%pi-app-close-thinking-card! app) (%pi-app-close-assistant-card! app) ;; 1. 出队并正式上屏为深蓝用户消息卡片 @@ -256,15 +258,14 @@ (duration (and (> (length ev) 6) (list-ref ev 6))) (card (%pi-app-find-tool-card app id)) ) ; - (if card - (tool-card-set-complete! card raw-res is-err? duration) - ;; 兼容未收到 start 的恢复/测试路径 - (let ((new-card (make-tool-card name args 'pending))) - (tool-card-set-expanded! new-card (pi-app-tools-expanded? app)) - (tool-card-set-complete! new-card raw-res is-err? duration) - (container-add! (%pi-app-chat app) new-card) - ) ;let - ) ;if + (unless card + (set! card (make-tool-card name args 'pending)) + (tool-card-set-expanded! card (pi-app-tools-expanded? app)) + (%pi-app-tool-cards-set! app (cons (cons id card) (%pi-app-tool-cards app))) + (container-add! (%pi-app-chat app) card) + ) ;unless + (tool-card-set-complete! card raw-res is-err? duration) + (%pi-app-apply-diff! app id card) ) ;let* ;; 旧自定义 handler:(end name args is-error? result) (let ((name (cadr ev)) @@ -315,6 +316,9 @@ ;; Kimi Code OAuth 设备码登录:device_code 上屏、done 落盘/报错 ((kimi_oauth_device_code) (pi-app-handle-oauth-device-code! app ev)) ((kimi_oauth_done) (pi-app-handle-oauth-done! app ev)) + ((run_started diff_result turn_diff_summary) + (%pi-app-handle-diff-event! app ev) + ) ; ((tool_execution_start) (%pi-app-close-assistant-card! app) (%pi-app-handle-tool-start! app ev) @@ -404,6 +408,7 @@ (pi-app-add-assistant-text! app "Aborted") ) ;when (pi-queue-finish-task! q) + (%pi-app-settle-diff! app) (settle!) ) ; ((error) diff --git a/app/liii/pi/app/interactive/app-history.scm b/app/liii/pi/app/interactive/app-history.scm index 21338f3e..d75e5929 100644 --- a/app/liii/pi/app/interactive/app-history.scm +++ b/app/liii/pi/app/interactive/app-history.scm @@ -321,6 +321,7 @@ ) ; ) ; ;; 全部转换成功后再替换界面 + (%pi-app-diff-run-set! app #f) (container-clear! (%pi-app-chat app)) (for-each (lambda (card) (container-add! (%pi-app-chat app) card)) cards) ;; 复位旧会话的引用:工具卡片索引、thinking 引用与文本选区 diff --git a/app/liii/pi/app/interactive/app.scm b/app/liii/pi/app/interactive/app.scm index 57fed046..b0348dd8 100644 --- a/app/liii/pi/app/interactive/app.scm +++ b/app/liii/pi/app/interactive/app.scm @@ -45,6 +45,7 @@ (liii pi app interactive thinking) (liii pi app interactive working) (liii pi app interactive worker) + (liii pi app interactive diff-worker) (liii pi app interactive bash) (liii pi app interactive app-bash) (liii pi app interactive app-tool-timer) @@ -301,6 +302,7 @@ (event-chan (make-chan 32)) (abort-chan (make-chan 1)) (bash-jobs-chan (make-chan 16)) + (display-jobs (make-chan 16)) (bash-abort-chan (make-chan 1)) (steering-chan (make-chan 16)) (follow-up-chan (make-chan 16)) @@ -369,6 +371,8 @@ #f request-selection-chan #f + display-jobs + #f ) ;%make-pi-app ) ;app (ed (%make-pi-editor app tl)) @@ -388,8 +392,9 @@ (pi-app-rebuild-history! app session-file) ;; 无自定义 handler 时启动后台 agent worker (unless handler + (go (%pi-display-worker display-jobs event-chan)) (go (pi-tui-agent-worker jobs-chan event-chan abort-chan session-file - steering-chan follow-up-chan request-selection-chan + steering-chan follow-up-chan request-selection-chan display-jobs ) ;pi-tui-agent-worker ) ;go ) ;unless diff --git a/app/liii/pi/app/interactive/diff-capture.scm b/app/liii/pi/app/interactive/diff-capture.scm new file mode 100644 index 00000000..bf5b13ab --- /dev/null +++ b/app/liii/pi/app/interactive/diff-capture.scm @@ -0,0 +1,293 @@ +;; Copyright (C) 2026 The Goldfish Scheme Authors +;; Licensed under the MIT License + +(define-library (liii pi app interactive diff-capture) + (export %pi-make-diff-capture %pi-diff-executor %pi-diff-emit %pi-diff-finish! + %pi-diff-config %diff-capture-id %diff-capture-done %diff-capture-events + %diff-capture-jobs %diff-capture-call %diff-capture-call-set! + %diff-capture-count %diff-capture-count-set! %diff-capture-misses + %diff-capture-misses-set! %diff-tool? %diff-read-snapshot %pi-diff-start + %diff-without-nul? %diff-text-snapshot %diff-cwd %diff-try-send! + %diff-enqueue! %pi-with-turn-diff %diff-written-size? %diff-valid-path? + %diff-read-bounded + ) ;export + (import (scheme base) + (scheme file) + (only (liii base) byte-vector->string) + (liii go) + (liii os) + (liii path stat) + (liii pi ai config runtime) + (liii pi ai tools result) + (liii pi code core tools read) + (liii pi app interactive tool-diff) + ) ;import + (begin + (define-record-type + (%make-diff-capture id jobs events done call count misses) + %diff-capture? + (id %diff-capture-id) + (jobs %diff-capture-jobs) + (events %diff-capture-events) + (done %diff-capture-done) + (call %diff-capture-call %diff-capture-call-set!) + (count %diff-capture-count %diff-capture-count-set!) + (misses %diff-capture-misses %diff-capture-misses-set!) + ) ;define-record-type + + (define (%pi-make-diff-capture id jobs events) + (%make-diff-capture id jobs events (make-chan 1) #f 0 '()) + ) ;define + + (define (%diff-tool? name) + (or (equal? name "edit") (equal? name "write")) + ) ;define + + (define (%diff-without-nul? text) + (let ((size (string-length text))) + (let loop + ((i 0)) + (or (= i size) (and (not (char=? (string-ref text i) #\nul)) (loop (+ i 1)))) + ) ;let + ) ;let + ) ;define + + (define (%diff-text-snapshot text) + (and (string? text) + (<= (string-length text) %diff-snapshot-bytes) + (%diff-without-nul? text) + text + ) ;and + ) ;define + + (define (%diff-read-bounded absolute expected) + ;; On Windows, use a binary port so CRT text mode cannot translate CRLF. + ;; On POSIX a raw read-string is a single fread rather than a Scheme byte loop. + (let* ((binary? (os-windows?)) + (port (if binary? (open-binary-input-file absolute) (open-input-file absolute))) + ) ; + (dynamic-wind (lambda () #t) + (lambda () + (let* ((limit (+ expected 1)) + (raw (if binary? (read-bytevector limit port) (read-string limit port))) + (text (if (bytevector? raw) (byte-vector->string raw) raw)) + ) ; + (cond ((eof-object? text) (and (zero? expected) "")) + ((= (string-length text) expected) (%diff-text-snapshot text)) + (else #f) + ) ;cond + ) ;let* + ) ;lambda + (lambda () (close-input-port port)) + ) ;dynamic-wind + ) ;let* + ) ;define + + (define (%diff-read-snapshot absolute missing-ok?) + (catch 'file-not-found-error + (lambda () + (let ((st (path-stat absolute))) + (and (path-stat-file? st) + (<= (path-stat-size st) %diff-snapshot-bytes) + (%diff-read-bounded absolute (path-stat-size st)) + ) ;and + ) ;let + ) ;lambda + (lambda args (and missing-ok? "")) + ) ;catch + ) ;define + + (define (%diff-cwd opts) + (let loop + ((rest opts)) + (cond ((not (pair? rest)) (getcwd)) + ((not (pair? (cdr rest))) (getcwd)) + ((eq? (car rest) :cwd) (cadr rest)) + (else (loop (cddr rest))) + ) ;cond + ) ;let + ) ;define + + ;; Never wait to enqueue optional display data. Closed channels on shutdown + ;; and optional snapshot errors must not log file contents or fail the tool. + (define (%diff-try-send! ch message) + (and ch (catch #t (lambda () (chan-try-send! ch message)) (lambda args #f))) + ) ;define + + (define (%diff-enqueue! run id path absolute before after) + (if + (and absolute + (%diff-try-send! (%diff-capture-jobs run) + (list 'diff_job + (%diff-capture-id run) + id + path + absolute + before + after + (%diff-capture-done run) + ) ;list + ) ;%diff-try-send! + ) ;and + (%diff-capture-count-set! run (+ 1 (%diff-capture-count run))) + (let ((misses (%diff-capture-misses run))) + ;; Remember dropped paths so an earlier snapshot cannot produce a + ;; partial net count. If even this list overflows, omit the summary. + (when misses + (%diff-capture-misses-set! run + (cond ((not absolute) #f) + ((member absolute misses) misses) + ((< (length misses) 16) (cons absolute misses)) + (else #f) + ) ;cond + ) ;%diff-capture-misses-set! + ) ;when + (%diff-try-send! (%diff-capture-events run) + (list 'diff_result (%diff-capture-id run) id path #f #f #f #f) + ) ;%diff-try-send! + ) ;let + ) ;if + ) ;define + + (define (%diff-valid-path? p) + (and (string? p) (<= 1 (string-length p) 4096) (%diff-without-nul? p)) + ) ;define + + (define (%diff-written-size? absolute text) + ;; write's original result can report success even when the filesystem + ;; rejects a write. Check metadata without re-reading the content. + (and text + (let ((st (path-stat absolute))) + (and (path-stat-file? st) (= (path-stat-size st) (string-length text))) + ) ;let + ) ;and + ) ;define + + (define (%pi-diff-executor run execute) + (lambda (name args . opts) + (let ((call (%diff-capture-call run))) + (if + (not (and call (equal? (cdr call) name))) + (apply execute name args opts) + (let ((absolute #f) + (display-path #f) + (before #f) + (write-after #f) + (write? (equal? name "write")) + ) ; + (catch #t + (lambda () + (let ((p (pi-arg-ref args "path"))) + (when (%diff-valid-path? p) + (set! display-path p) + (set! absolute (pi-resolve-path p (%diff-cwd opts))) + (when (> (string-length absolute) 4096) + (set! absolute #f) + ) ;when + (when write? + (set! write-after (%diff-text-snapshot (pi-arg-ref args "content"))) + ) ;when + (when (and absolute (or (not write?) write-after)) + (set! before (%diff-read-snapshot absolute write?)) + ) ;when + ) ;when + ) ;let + ) ;lambda + (lambda args #f) + ) ;catch + ;; Keep the originals outside the observation catches: execute + ;; once, propagate its exceptions, and return its exact result. + (let ((result (apply execute name args opts))) + (when (and (pi-tool-result? result) (not (pi-tool-result-is-error result))) + (let ((after + (and before + (catch #t + (lambda () + (if write? + (and (%diff-written-size? absolute write-after) write-after) + (%diff-read-snapshot absolute #f) + ) ;if + ) ;lambda + (lambda args #f) + ) ;catch + ) ;and + ) ;after + ) ; + (catch #t + (lambda () (%diff-enqueue! run (car call) display-path absolute before after)) + (lambda args #f) + ) ;catch + ) ;let + ) ;when + result + ) ;let + ) ;let + ) ;if + ) ;let + ) ;lambda + ) ;define + + (define (%pi-diff-config run cfg) + (let ((execute (and run (pi-cfg-ref cfg 'execute-tool-call)))) + (if (procedure? execute) + (pi-cfg-merge cfg + (list (cons 'execute-tool-call (%pi-diff-executor run execute))) + ) ;pi-cfg-merge + cfg + ) ;if + ) ;let + ) ;define + + ;; This is an agent-worker-local observer; the existing make-tui-emit and + ;; tool/extension event contracts do not change. Tools execute serially. + (define (%pi-diff-emit run emit) + (lambda (kind . args) + (case kind + ((tool_execution_start) + (let ((id (car args)) (name (cadr args))) + (%diff-capture-call-set! run + (and (%diff-tool? name) (string? id) (cons id name)) + ) ;%diff-capture-call-set! + ) ;let + ) ; + ((tool_execution_end) (%diff-capture-call-set! run #f)) + ) ;case + (apply emit kind args) + ) ;lambda + ) ;define + + (define (%pi-diff-start run emit) + ;; Optional events must not add backpressure to the agent. If the start + ;; marker cannot be sent, disable capture for this run rather than orphaning results. + (if + (and run + (%diff-try-send! (%diff-capture-events run) + (list 'run_started (%diff-capture-id run)) + ) ;%diff-try-send! + ) ;and + (%pi-diff-emit run emit) + emit + ) ;if + ) ;define + + (define (%pi-diff-finish! run) + ;; A private one-slot control channel cannot be full on first finish, + ;; and doesn't compete for space with snapshots in the shared job queue. + (%diff-try-send! (%diff-capture-done run) + (cons (%diff-capture-count run) (%diff-capture-misses run)) + ) ;%diff-try-send! + (chan-close! (%diff-capture-done run)) + ) ;define + (define (%pi-with-turn-diff run cfg emit execute) + (if run + (let ((observed-emit (%pi-diff-start run emit))) + (dynamic-wind (lambda () #t) + (lambda () (execute (%pi-diff-config run cfg) observed-emit)) + (lambda () (%pi-diff-finish! run)) + ) ;dynamic-wind + ) ;let + (execute cfg emit) + ) ;if + ) ;define + ) ;begin +) ;define-library diff --git a/app/liii/pi/app/interactive/diff-worker.scm b/app/liii/pi/app/interactive/diff-worker.scm new file mode 100644 index 00000000..26f5a77f --- /dev/null +++ b/app/liii/pi/app/interactive/diff-worker.scm @@ -0,0 +1,195 @@ +;; Copyright (C) 2026 The Goldfish Scheme Authors +;; Licensed under the MIT License + +(define-library (liii pi app interactive diff-worker) + ;; All procedures reached by go must be visible in its separate session. + (export %pi-display-worker %diff-record-file! %diff-job! %diff-compute + %diff-summary %make-diff-turn %diff-turn-id %diff-turn-done %diff-turn-files + %diff-turn-control %diff-turn-control-set! %diff-turn-count + %diff-turn-count-set! %diff-finish-turn %make-diff-file %diff-file-key + %diff-file-path %diff-file-before %diff-file-after %diff-file-after-set! + %diff-publish! + ) ;export + (import (scheme base) + (liii go) + (liii pi app interactive tool-diff) + (only (liii unicode) utf8-replace-invalid) + ) ;import + (begin + (define-record-type + (%make-diff-file key path before after) + %diff-file? + (key %diff-file-key) + (path %diff-file-path) + (before %diff-file-before) + (after %diff-file-after %diff-file-after-set!) + ) ;define-record-type + + (define-record-type + (%make-diff-turn id done files control count) + %diff-turn? + (id %diff-turn-id) + (done %diff-turn-done) + (files %diff-turn-files) + (control %diff-turn-control %diff-turn-control-set!) + (count %diff-turn-count %diff-turn-count-set!) + ) ;define-record-type + + (define (%diff-record-file! files key path before after) + (when key + ;; Sixteen slots cap retained baseline/after strings at 8 MiB. + ;; A slot with #f after is permanently invalid; do not start a new + ;; baseline after a successful write with no usable snapshot. + (let loop + ((i 0)) + (when (< i (vector-length files)) + (let ((file (vector-ref files i))) + (cond + ((not file) + (vector-set! files i (%make-diff-file key path before (and before after))) + ) ; + ((equal? key (%diff-file-key file)) + (%diff-file-after-set! file (and (%diff-file-after file) before after)) + ) ; + (else (loop (+ i 1))) + ) ;cond + ) ;let + ) ;when + ) ;let + ) ;when + ) ;define + + (define (%diff-compute before after with-text? jobs) + (and (not (chan-closed? jobs)) + (string? before) + (string? after) + ;; Only derived display data is affected by a computation fault. + (catch #t + (lambda () + (and (eq? before (utf8-replace-invalid before)) + (eq? after (utf8-replace-invalid after)) + (%tool-diff before after with-text? (lambda () (chan-closed? jobs))) + ) ;and + ) ;lambda + (lambda args #f) + ) ;catch + ) ;and + ) ;define + + ;; Optional results cannot wait for room in the UI event channel. + (define (%diff-publish! events event) + (catch #t (lambda () (chan-try-send! events event)) (lambda args #f)) + ) ;define + + (define (%diff-job! turn job events jobs) + (let ((id (caddr job)) + (path (list-ref job 3)) + (key (list-ref job 4)) + (before (list-ref job 5)) + (after (list-ref job 6)) + ) ; + (%diff-record-file! (%diff-turn-files turn) key path before after) + (%diff-turn-count-set! turn (+ 1 (%diff-turn-count turn))) + (let ((diff (%diff-compute before after #t jobs))) + (%diff-publish! events + (append (list 'diff_result (%diff-turn-id turn) id path) + (or diff '(#f #f #f #f)) + ) ;append + ) ;%diff-publish! + ) ;let + ) ;let + ) ;define + + (define (%diff-summary files missed jobs) + (and missed + (let loop + ((i 0) (entries '())) + (if (= i (vector-length files)) + (reverse entries) + (let* ((file (vector-ref files i)) + (diff + (and file + (not (member (%diff-file-key file) missed)) + (%diff-compute (%diff-file-before file) (%diff-file-after file) #f jobs) + ) ;and + ) ;diff + ) ; + (loop (+ i 1) + (if + (and diff (> (+ (cadr diff) (caddr diff)) 0)) + (cons (list (%diff-file-path file) (cadr diff) (caddr diff)) entries) + entries + ) ;if + ) ;loop + ) ;let* + ) ;if + ) ;let + ) ;and + ) ;define + + (define (%diff-finish-turn turn events jobs) + (let ((control (or (%diff-turn-control turn) (chan-try-recv! (%diff-turn-done turn) #f)) + ) ;control + ) ; + (cond ((eof-object? control) #f) + ((and (pair? control) (= (car control) (%diff-turn-count turn))) + (%diff-publish! events + (list 'turn_diff_summary + (%diff-turn-id turn) + (or (%diff-summary (%diff-turn-files turn) (cdr control) jobs) '()) + ) ;list + ) ;%diff-publish! + #f + ) ; + (else (%diff-turn-control-set! turn control) turn) + ) ;cond + ) ;let + ) ;define + + (define (%pi-display-worker jobs events) + (let loop + ((turn #f)) + (unless (chan-closed? jobs) + (let* ((current (and turn (%diff-finish-turn turn events jobs))) + (job + (if (and current (not (%diff-turn-control current))) + (select ((chan-recv! jobs val) val) + ((chan-recv! (%diff-turn-done current) val) + (%diff-turn-control-set! current val) + #f + ) ; + ) ;select + (chan-recv! jobs) + ) ;if + ) ;job + ) ; + (cond ((or (eof-object? job) (chan-closed? jobs)) #f) + ((not job) (loop current)) + ((eq? (car job) 'diff_job) + (let ((active + (if (and current (equal? (%diff-turn-id current) (cadr job))) + current + (begin + ;; FIFO jobs and a serial producer: the previous + ;; control is available by the first job of a new run. + (when current + (%diff-finish-turn current events jobs) + ) ;when + (%make-diff-turn (cadr job) (list-ref job 7) (make-vector 16 #f) #f 0) + ) ;begin + ) ;if + ) ;active + ) ; + (%diff-job! active job events jobs) + (loop active) + ) ;let + ) ; + ;; Reserved for additional display work types. + (else (loop current)) + ) ;cond + ) ;let* + ) ;unless + ) ;let + ) ;define + ) ;begin +) ;define-library diff --git a/app/liii/pi/app/interactive/renderers.scm b/app/liii/pi/app/interactive/renderers.scm index fd814341..380cf088 100644 --- a/app/liii/pi/app/interactive/renderers.scm +++ b/app/liii/pi/app/interactive/renderers.scm @@ -7,7 +7,7 @@ (define-library (liii pi app interactive renderers) (export format-tool-call format-tool-call-segments format-tool-result-lines extract-result-text format-bash-tool-result-lines format-tool-duration - get-tool-arg + get-tool-arg %clean-tool-display-text ) ;export (import (scheme base) (liii string) diff --git a/app/liii/pi/app/interactive/run.scm b/app/liii/pi/app/interactive/run.scm index b06e1195..f0bc912f 100644 --- a/app/liii/pi/app/interactive/run.scm +++ b/app/liii/pi/app/interactive/run.scm @@ -323,6 +323,7 @@ ;; 退出清理(顺序重要): ;; 1. 先中止在途 agent 任务:worker 线程阻塞在 LLM HTTP 请求上, ;; 进程退出会 join go 线程池,不中止就会等到 provider 超时才退出 + (chan-close! (%pi-app-display-jobs app)) (let ((settled (pi-app-abort-inflight! app))) ;; 2. 关闭鼠标事件上报(与进入时对称), ;; 再离开备用屏(主屏内容自动恢复), diff --git a/app/liii/pi/app/interactive/state.scm b/app/liii/pi/app/interactive/state.scm index 35353c9f..e85bac02 100644 --- a/app/liii/pi/app/interactive/state.scm +++ b/app/liii/pi/app/interactive/state.scm @@ -44,7 +44,8 @@ %pi-app-recovery-card-set! pi-app-tool-timer-last-tick %pi-app-tool-timer-last-tick-set! pi-app-tool-timer-last-tick-set! pi-app-request-selection-chan pi-app-exit-requested? - %pi-app-exit-requested-set! + %pi-app-exit-requested-set! %pi-app-display-jobs %pi-app-diff-run + %pi-app-diff-run-set! ) ;export (import (scheme base) (liii date-time) (liii pi app interactive selection)) (begin @@ -66,6 +67,7 @@ rendered-frame dock-length working-border-row skills enable-skill-commands scroll-hint-rect thinking-level-explicit recovery-card tool-timer-last-tick request-selection-chan exit-requested + display-jobs diff-run ) ;%make-pi-app pi-app? (term pi-app-term) @@ -79,6 +81,8 @@ (footer %pi-app-footer) (queue pi-app-queue) (handler %pi-app-handler) + (display-jobs %pi-app-display-jobs) + (diff-run %pi-app-diff-run %pi-app-diff-run-set!) (jobs-chan pi-app-jobs-chan) (event-chan pi-app-event-chan) ;; ESC 中断信号通道:主线程发送,后台 worker 接收探测 diff --git a/app/liii/pi/app/interactive/tool-diff.scm b/app/liii/pi/app/interactive/tool-diff.scm new file mode 100644 index 00000000..21f055ae --- /dev/null +++ b/app/liii/pi/app/interactive/tool-diff.scm @@ -0,0 +1,259 @@ +;; Copyright (C) 2026 The Goldfish Scheme Authors +;; Licensed under the MIT License + +(define-library (liii pi app interactive tool-diff) + ;; Export the private helpers for the independent go session. + (export %tool-diff %diff-snapshot-bytes %diff-lines %diff-common-edge + %diff-lines-count-limit %diff-matrix %diff-operations %diff-hunks + %diff-hunk-header %diff-preview + ) ;export + (import (scheme base) + (scheme write) + (only (liii base) call-with-output-string) + (liii string) + ) ;import + (begin + (define %diff-snapshot-bytes (* 256 1024)) + (define %diff-lines-count-limit 8192) + + ;; Keep the newline in the comparison key so a missing final newline is + ;; visible as a line change. Fold CRLF and bare CR like path-read-text. + (define (%diff-lines text) + (let ((size (string-length text))) + (let loop + ((i 0) (start 0) (lines '()) (count 0)) + (cond + ((= i size) + (cond ((= i start) (list->vector (reverse lines))) + ((< count %diff-lines-count-limit) + (list->vector (reverse (cons (substring text start i) lines))) + ) ; + (else #f) + ) ;cond + ) ; + ((char=? (string-ref text i) #\return) + (and (< count %diff-lines-count-limit) + (let ((next + (if + (and (< (+ i 1) size) (char=? (string-ref text (+ i 1)) #\newline)) + (+ i 2) + (+ i 1) + ) ;if + ) ;next + (line (string-append (substring text start i) "\n")) + ) ; + (loop next next (cons line lines) (+ count 1)) + ) ;let + ) ;and + ) ; + ((char=? (string-ref text i) #\newline) + (and (< count %diff-lines-count-limit) + (let ((next (+ i 1))) + (loop next next (cons (substring text start next) lines) (+ count 1)) + ) ;let + ) ;and + ) ; + (else (loop (+ i 1) start lines count)) + ) ;cond + ) ;let + ) ;let + ) ;define + + (define (%diff-common-edge old new start end step limit) + (let loop + ((i start) (j end) (n 0)) + (if (and (< n limit) (string=? (vector-ref old i) (vector-ref new j))) + (loop (+ i step) (+ j step) (+ n 1)) + n + ) ;if + ) ;let + ) ;define + + ;; LCS of suffixes in the unmatched middle; index 0 is its LCS length. + (define (%diff-matrix old new start n m stride cancelled?) + (let ((cells (make-vector (* (+ n 1) stride) 0))) + (do ((i (- n 1) (- i 1))) + ((or (< i 0) (cancelled?)) (and (< i 0) cells)) + (let ((a (vector-ref old (+ start i)))) + (do ((j (- m 1) (- j 1))) + ((< j 0)) + (let* ((k (+ (* i stride) j)) (down (+ k stride))) + (vector-set! cells + k + (if (string=? a (vector-ref new (+ start j))) + (+ 1 (vector-ref cells (+ down 1))) + (max (vector-ref cells down) (vector-ref cells (+ k 1))) + ) ;if + ) ;vector-set! + ) ;let* + ) ;do + ) ;let + ) ;do + ) ;let + ) ;define + + ;; Each op's two zero-based positions are counts *before* consuming a line. + (define (%diff-operations old new start n m stride cells) + (let loop + ((i (- start)) (j (- start)) (ops '())) + (let ((x (+ start i)) (y (+ start j))) + (cond + ((< i 0) (loop (+ i 1) (+ j 1) (cons (list #\space x y) ops))) + ((and (>= i n) (>= j m)) + (if (= x (vector-length old)) + (list->vector (reverse ops)) + (loop (+ i 1) (+ j 1) (cons (list #\space x y) ops)) + ) ;if + ) ; + ((and (< i n) (< j m) (string=? (vector-ref old x) (vector-ref new y))) + (loop (+ i 1) (+ j 1) (cons (list #\space x y) ops)) + ) ; + ((or (= j m) + (and (< i n) + (>= + (vector-ref cells (+ (* (+ i 1) stride) j)) + (vector-ref cells (+ (* i stride) j 1)) + ) ;>= + ) ;and + ) ;or + (loop (+ i 1) j (cons (list #\- x y) ops)) + ) ; + (else (loop i (+ j 1) (cons (list #\+ x y) ops))) + ) ;cond + ) ;let + ) ;let + ) ;define + + (define (%diff-hunks ops) + (let ((size (vector-length ops))) + (let loop + ((i 0) (start #f) (end 0) (hunks '())) + (cond + ((= i size) (reverse (if start (cons (cons start end) hunks) hunks))) + ((char=? (car (vector-ref ops i)) #\space) (loop (+ i 1) start end hunks)) + (else + (let ((lo (max 0 (- i 3))) (hi (min size (+ i 4)))) + (if (and start (<= lo end)) + (loop (+ i 1) start hi hunks) + (loop (+ i 1) lo hi (if start (cons (cons start end) hunks) hunks)) + ) ;if + ) ;let + ) ;else + ) ;cond + ) ;let + ) ;let + ) ;define + + (define (%diff-hunk-header first last) + (let* ((tag (car last)) + (x (cadr first)) + (y (caddr first)) + (n (+ (- (cadr last) x) (if (char=? tag #\+) 0 1))) + (m (+ (- (caddr last) y) (if (char=? tag #\-) 0 1))) + ) ; + (string-append "@@ -" + (number->string (if (zero? n) x (+ x 1))) + "," + (number->string n) + " +" + (number->string (if (zero? m) y (+ y 1))) + "," + (number->string m) + " @@" + ) ;string-append + ) ;let* + ) ;define + + (define (%diff-preview old new ops) + (let ((text + (call-with-output-string + (lambda (out) + (define (write-line text) + (display text out) + (newline out) + ) ;define + (for-each + (lambda (hunk) + (let ((start (car hunk)) (end (cdr hunk))) + (write-line (%diff-hunk-header (vector-ref ops start) (vector-ref ops (- end 1))) + ) ;write-line + (do ((i start (+ i 1))) + ((= i end)) + (let* ((op (vector-ref ops i)) + (tag (car op)) + (line (if (char=? tag #\+) (vector-ref new (caddr op)) (vector-ref old (cadr op))) + ) ;line + (end (- (string-length line) 1)) + (terminated? (char=? (string-ref line end) #\newline)) + ) ; + (write-char tag out) + (write-line (if terminated? (substring line 0 end) line)) + (unless terminated? + (write-line "\\ No newline at end of file") + ) ;unless + ) ;let* + ) ;do + ) ;let + ) ;lambda + (%diff-hunks ops) + ) ;for-each + ) ;lambda + ) ;call-with-output-string + ) ;text + ) ; + (and (<= (string-length text) (+ 1 (* 48 1024))) + (string-trim-right text #\newline) + ) ;and + ) ;let + ) ;define + + (define* (%tool-diff before after (with-text? #t) (cancelled? (lambda () #f))) + (unless (and (string? before) (string? after)) + (error 'type-error "%tool-diff: expected before/after text strings") + ) ;unless + (and (<= (max (string-length before) (string-length after)) %diff-snapshot-bytes) + (if (string=? before after) + '(#f 0 0 #f) + (let ((old (%diff-lines before)) (new (%diff-lines after))) + (and old + new + (let* ((old-size (vector-length old)) + (new-size (vector-length new)) + (limit (min old-size new-size)) + (start (%diff-common-edge old new 0 0 1 limit)) + (tail (%diff-common-edge old new (- old-size 1) (- new-size 1) -1 (- limit start)) + ) ;tail + (n (- old-size start tail)) + (m (- new-size start tail)) + (stride (+ m 1)) + ) ; + (and + (or (zero? n) (zero? m) (<= (* (+ n 1) stride) 250000)) + (let* ((matrix? (and (> n 0) (> m 0))) + (cells (and matrix? (%diff-matrix old new start n m stride cancelled?))) + (shared (if cells (vector-ref cells 0) 0)) + (added (- m shared)) + (removed (- n shared)) + (changed (+ added removed)) + ) ; + (and (or (not matrix?) cells) + (not (cancelled?)) + (list (and with-text? + (<= 1 changed 200) + (%diff-preview old new (%diff-operations old new start n m stride cells)) + ) ;and + added + removed + (and (> changed 0) (+ start 1)) + ) ;list + ) ;and + ) ;let* + ) ;and + ) ;let* + ) ;and + ) ;let + ) ;if + ) ;and + ) ;define* + ) ;begin +) ;define-library diff --git a/app/liii/pi/app/interactive/views/tool-card.scm b/app/liii/pi/app/interactive/views/tool-card.scm index f435884b..e721eb64 100644 --- a/app/liii/pi/app/interactive/views/tool-card.scm +++ b/app/liii/pi/app/interactive/views/tool-card.scm @@ -8,7 +8,7 @@ (export make-tool-card tool-card? tool-card-component? tool-card-expanded? tool-card-set-expanded! tool-card-set-output! tool-card-set-complete! tool-card-set-started-at! tool-card-started-at tool-card-pending-bash? - %tool-now-ms + %tool-now-ms %tool-card-apply-diff! %tool-diff-counts ) ;export (import (scheme base) (scheme time) @@ -26,7 +26,8 @@ ;; 工具调用卡片的可变状态。pending 卡片在 start/update/end 事件间复用。 (define-record-type (%make-tool-card tool-name args status result expanded? started-at - ended-at duration-ms render-cache card-render-cache + ended-at duration-ms render-cache card-render-cache diff-status + diff-text added-lines removed-lines ) ;%make-tool-card tool-card? (tool-name %tool-card-name) @@ -40,6 +41,10 @@ ;; 原始 bash 输出缓存与完整卡片渲染缓存分别保存,避免互相覆盖。 (render-cache %tool-card-render-cache %tool-card-render-cache-set!) (card-render-cache %tool-card-card-render-cache) + (diff-status %tool-card-diff-status %tool-card-diff-status-set!) + (diff-text %tool-card-diff-text %tool-card-diff-text-set!) + (added-lines %tool-card-added-lines %tool-card-added-lines-set!) + (removed-lines %tool-card-removed-lines %tool-card-removed-lines-set!) ) ;define-record-type (define (%tool-now-ms) @@ -89,6 +94,33 @@ ) ;let ) ;define + ;; #f requests deferred application; terminal failures and duplicate results + ;; are ignored rather than replacing the body of an already-finished card. + (define (%tool-card-apply-diff! card text added removed) + (let ((st (component-data card))) + (cond + ((or (not (member (%tool-card-name st) '("write" "edit"))) + (%tool-card-diff-status st) + ) ;or + 'ignored + ) ; + ((eq? (%tool-card-status st) 'pending) #f) + ((not (eq? (%tool-card-status st) 'success)) 'ignored) + (added (%tool-card-diff-status-set! st 'ready) + (%tool-card-diff-text-set! st text) + (%tool-card-added-lines-set! st added) + (%tool-card-removed-lines-set! st removed) + 'changed + ) ;added + (else (%tool-card-diff-status-set! st 'skipped) 'ignored) + ) ;cond + ) ;let + ) ;define + + (define (%tool-diff-counts added removed) + (string-append "+" (number->string added) " -" (number->string removed)) + ) ;define + ;; 与上游每秒刷新一致:Working 动画可更频繁重绘,但时间只按秒推进 (define (%tool-elapsed-text started) (let ((elapsed @@ -227,11 +259,16 @@ ;; 上下各一行背景空行;pending/绿/红背景把命令与输出整个包住; ;; 标题先分段着色再折行,超宽时保持分段样式(issue #1484); ;; 展开态不受预览行数限制,折叠态按工具取预览行数(Ctrl+O 全局切换) - (define (render-tool-card-lines tool-name args status result width . maybe-expanded?) - (let* ((expanded? (if (pair? maybe-expanded?) (car maybe-expanded?) #f)) - (bg (%tool-bg status)) + (define (render-tool-card-lines tool-name args status result width expanded? counts) + (let* ((bg (%tool-bg status)) (content-width (max 1 (- width 2))) - (segs (format-tool-call-segments tool-name args)) + (base-segs (format-tool-call-segments tool-name args)) + (segs + (if counts + (append base-segs (list (cons 'muted (string-append " " counts)))) + base-segs + ) ;if + ) ;segs (title-rows (map (lambda (line) (apply-card-bg (string-append " " line) width bg "")) (wrap-text (%style-segments segs) content-width) @@ -278,6 +315,10 @@ width (%tool-card-expanded? st) timer + (%tool-card-diff-status st) + (%tool-card-diff-text st) + (%tool-card-added-lines st) + (%tool-card-removed-lines st) ) ;list ) ;define @@ -294,13 +335,17 @@ (let ((tool-name (%tool-card-name st))) (cond ((<= width 0) '()) ((string=? tool-name "bash") (%render-bash-tool-card st width)) - (else (render-tool-card-lines tool-name - (%tool-card-args st) - (%tool-card-status st) - (%tool-card-result st) - width - (%tool-card-expanded? st) - ) ;render-tool-card-lines + (else + (render-tool-card-lines tool-name + (%tool-card-args st) + (%tool-card-status st) + (or (%tool-card-diff-text st) (%tool-card-result st)) + width + (%tool-card-expanded? st) + (and (eq? (%tool-card-diff-status st) 'ready) + (%tool-diff-counts (%tool-card-added-lines st) (%tool-card-removed-lines st)) + ) ;and + ) ;render-tool-card-lines ) ;else ) ;cond ) ;let @@ -333,6 +378,10 @@ duration #f (%make-render-cache #f '()) + #f + #f + #f + #f ) ;%make-tool-card ) ;st (card diff --git a/app/liii/pi/app/interactive/worker.scm b/app/liii/pi/app/interactive/worker.scm index 39a7a534..0ff92ca7 100644 --- a/app/liii/pi/app/interactive/worker.scm +++ b/app/liii/pi/app/interactive/worker.scm @@ -9,6 +9,7 @@ pi-cfg-with-thinking pi-cfg-with-model %worker-job-cfg %cfg-override pi-worker-ensure-session-file! pi-worker-handle-job %assistant-error-text %poll-one-injected %pi-worker-poll-cfg %worker-cfg-with-session-id + %job-explicit ) ;export (import (scheme base) (scheme write) @@ -33,6 +34,7 @@ (liii pi ai retry) (liii pi code core extension-notify) (liii pi code core extensions) + (liii pi app interactive diff-capture) (liii pi app interactive worker-emit) (liii pi app interactive worker-model) (liii pi code modes flow) @@ -149,35 +151,42 @@ ) ;let ) ;define - (define* (pi-tui-run-worker parsed cfg user-text event-chan . opts) - (let* ((session-file (and (pair? opts) (car opts))) - (opt-window-getter - (if (and (pair? opts) (pair? (cdr opts))) (cadr opts) #f) - ) ;opt-window-getter - ) ; - (catch #t - (lambda () - (%worker-assert-cred! cfg) - (run-session-flow parsed - (%worker-cfg-with-session-id cfg session-file) - user-text - (make-tui-emit event-chan (or opt-window-getter (%cfg-context-window cfg))) - (lambda (res-msgs) #t) - session-file - '() - #f - ) ;run-session-flow - ) ;lambda - (lambda (tag . err-args) - (pi-tui-send-event! event-chan - (list 'error - (string-append (object->string tag) ": " (object->string err-args)) - ) ;list - ) ;pi-tui-send-event! - (pi-tui-send-event! event-chan (list 'agent_settled #f)) - ) ;lambda - ) ;catch - ) ;let* + (define* (pi-tui-run-worker parsed + cfg + user-text + event-chan + (session-file #f) + (window #f) + (diff #f) + ) ;pi-tui-run-worker + (%pi-with-turn-diff diff + cfg + (make-tui-emit event-chan (or window (%cfg-context-window cfg))) + (lambda (run-cfg emit) + (catch #t + (lambda () + (%worker-assert-cred! run-cfg) + (run-session-flow parsed + (%worker-cfg-with-session-id run-cfg session-file) + user-text + emit + (lambda (res-msgs) #t) + session-file + '() + #f + ) ;run-session-flow + ) ;lambda + (lambda (tag . err-args) + (pi-tui-send-event! event-chan + (list 'error + (string-append (object->string tag) ": " (object->string err-args)) + ) ;list + ) ;pi-tui-send-event! + (pi-tui-send-event! event-chan (list 'agent_settled #f)) + ) ;lambda + ) ;catch + ) ;lambda + ) ;%pi-with-turn-diff ) ;define* (define (pi-worker-ensure-session-file! parsed cfg sfile-box) @@ -219,20 +228,20 @@ ) ;let* ) ;define - (define (pi-worker-handle-job job parsed cfg sfile-box event-chan abort-chan - . opt-chans - ) ; - (let* ((steering-chan (if (pair? opt-chans) (car opt-chans) #f)) - (follow-up-chan - (if (and (pair? opt-chans) (pair? (cdr opt-chans))) (cadr opt-chans) #f) - ) ;follow-up-chan - (eff-box - (if (and (pair? opt-chans) (>= (length opt-chans) 3)) (list-ref opt-chans 2) #f) - ) ;eff-box - (req-sel-chan - (if (and (pair? opt-chans) (>= (length opt-chans) 4)) (list-ref opt-chans 3) #f) - ) ;req-sel-chan - (cfg (%pi-worker-poll-cfg cfg steering-chan follow-up-chan event-chan)) + (define* (pi-worker-handle-job job + parsed + cfg + sfile-box + event-chan + abort-chan + (steering-chan #f) + (follow-up-chan #f) + (eff-box #f) + (req-sel-chan #f) + (display-jobs #f) + (run-id #f) + ) ;pi-worker-handle-job + (let* ((cfg (%pi-worker-poll-cfg cfg steering-chan follow-up-chan event-chan)) (effective-cfg cfg) ) ; (cond @@ -360,8 +369,7 @@ ) ; (else (pi-abort-arm-chan! abort-chan) (let* ((job-cfg (%worker-job-cfg cfg job parsed)) - ;; 新消息初始化 effective-cfg 为本任务的初始快照; - ;; 运行中切模由 request-selection-chan 在每次请求前覆盖 + ;; /model 或 /thinking 变更由 prepare-request 在请求前读取。 (eff-local (or eff-box (pi-make-effective-cfg-box job-cfg))) ) ; (when eff-box @@ -369,14 +377,8 @@ ) ;when (let* ((prepare (pi-worker-make-prepare-request eff-local req-sel-chan parsed)) (cfg-with-prepare (pi-cfg-merge job-cfg (list (cons 'prepare-request prepare)))) - ;; 事件桥读取本次响应对应的有效窗口: - ;; all-in-one 实际节点窗口优先,否则按 effective-cfg 的模型窗口 - (window-getter - (lambda () - (let* ((cur (pi-effective-cfg-ref eff-local)) (cw (%cfg-context-window cur))) - cw - ) ;let* - ) ;lambda + ;; 优先使用 all-in-one 节点窗口,否则使用 effective-cfg。 + (window-getter (lambda () (%cfg-context-window (pi-effective-cfg-ref eff-local))) ) ;window-getter ) ; (set! effective-cfg job-cfg) @@ -386,6 +388,7 @@ event-chan (pi-worker-ensure-session-file! parsed cfg sfile-box) window-getter + (and display-jobs (%pi-make-diff-capture run-id display-jobs event-chan)) ) ;pi-tui-run-worker ) ;let* ) ;let* @@ -393,7 +396,7 @@ ) ;cond effective-cfg ) ;let* - ) ;define + ) ;define* (define* (pi-tui-agent-worker jobs-chan event-chan @@ -402,9 +405,9 @@ (steering-chan #f) (follow-up-chan #f) (request-selection-chan #f) + (display-jobs #f) ) ;pi-tui-agent-worker - ;; 扩展加载须早于 parse-pi-args(扩展 flag 才能参与解析);initializer - ;; 已按 discovery 仲裁一次性加载全部层级,不可再补加载用户层致重复挂载。 + ;; 按 discovery 仲裁加载扩展,再解析扩展 flag,避免重复挂载。 (default-extension-registry) (let* ((parsed (parse-pi-args (cdr (command-line)))) (cfg (augment-pi-config (resolve-pi-config parsed (get-environment-variables)) @@ -412,7 +415,6 @@ ) ;augment-pi-config ) ;cfg (sfile-box (vector initial-session-file)) - ;; 贯穿本 worker 生命周期的 effective-cfg:切模后保留最新选择 (eff-box (pi-make-effective-cfg-box cfg)) ) ; (set-pi-retry-logger! (lambda (msg) (pi-tui-send-event! event-chan (list 'retry msg))) @@ -422,13 +424,14 @@ (pi-extension-notify-set! (lambda (msg) (pi-tui-send-event! event-chan (list 'tps_report msg)) #t) ) ;pi-extension-notify-set! (let loop - ((current-cfg cfg)) + ((current-cfg cfg) (run-id 1)) (let ((job (chan-recv! jobs-chan))) (when (and job (not (eof-object? job))) (loop (pi-worker-handle-job job parsed current-cfg sfile-box event-chan abort-chan steering-chan follow-up-chan eff-box - request-selection-chan + request-selection-chan display-jobs run-id ) ;pi-worker-handle-job + (+ run-id 1) ) ;loop ) ;when ) ;let diff --git a/app/tests/liii/pi/app/interactive/app-diff/app-diff-test.scm b/app/tests/liii/pi/app/interactive/app-diff/app-diff-test.scm new file mode 100644 index 00000000..34dcb1a5 --- /dev/null +++ b/app/tests/liii/pi/app/interactive/app-diff/app-diff-test.scm @@ -0,0 +1,150 @@ +;; %pi-app-handle-diff-event! applies runtime display events without waiting. +;; (pi-app-poll-events! app) consumes the same events used by the real workers. +;; No return value is required; stale/duplicate results and failures are ignored. +(import (scheme base) + (liii check) + (liii go) + (liii string) + (liii tui) + (liii pi agent queue) + (liii pi app interactive app) + (liii pi app interactive views tool-card) + (liii pi app interactive state) +) ;import + +(check-set-mode! 'report-failed) + +(define app + (make-pi-app + :terminal (make-virtual-terminal :width 80 :height 24) + :handler (lambda args #f) + :bash-handler (lambda args (values "" 0)) + ) ;make-pi-app +) ;define + +(define (emit . events) + (for-each (lambda (ev) (chan-send! (pi-app-event-chan app) ev 100)) events) + (pi-app-poll-events! app) +) ;define + +(define (transcript) + (string-join (map strip-ansi (pi-app-transcript-lines app)) "\n") +) ;define + +(define (shows? text) + (string-contains? (transcript) text) +) ;define + +(define args '(("path" . "sample.txt"))) + +(define (start id) + (list 'tool_execution_start id "write" args) +) ;define + +(define (end id ok?) + (list 'tool_execution_end + id + "write" + args + (not ok?) + (if ok? "Saved!" "Write failed!") + ) ;list +) ;define + +(emit '(run_started 1) + (start "early") + '(diff_result 1 "early" "sample.txt" "+early" 1 0 1) +) ;emit +(check-false (shows? "+early")) +(emit (end "early" #t)) +(check-true (shows? "+early")) +(check-true (shows? "sample.txt +1 -0")) +(emit '(diff_result 1 "early" "sample.txt" "+duplicate" 8 8 1) + (start "late") + (end "late" #t) +) ;emit +(check-true (shows? "Saved!")) +(check-false (shows? "+duplicate")) +(emit '(diff_result 1 "late" "sample.txt" "+late" 2 1 1) + (start "error") + (end "error" #f) + '(diff_result 1 "error" "sample.txt" "+false-success" 2 1 1) +) ;emit +(check-true (shows? "+late")) +(check-true (shows? "Write failed!")) +(check-false (shows? "+false-success")) + +(emit '(turn_diff_summary 1 (("sample.txt" 2 0)))) +(check-false (shows? "Turn changes")) +(emit '(assistant_text "Final answer") '(agent_settled #f)) +(check-true (shows? "Turn changes")) +(check-true + (string-contains? (car (string-split (transcript) "Turn changes")) + "Final answer" + ) ;string-contains? +) ;check-true +(emit '(turn_diff_summary 1 (("duplicate-summary.txt" 2 1)))) +(check-false (shows? "duplicate-summary.txt")) + +;; Run 2 reuses a tool ID. Completion and another task can proceed without diff. +(emit '(run_started 2) (start "early") (end "early" #t) '(agent_settled #f)) +(check-true (pi-queue-idle? (pi-app-queue app))) +(emit '(run_started 3) + (start "early") + '(diff_result 2 "early" "sample.txt" "+stale" 9 9 1) + '(turn_diff_summary 2 (("stale-summary.txt" 9 9))) + '(diff_result 3 "early" "sample.txt" "+fresh" 1 0 1) + (end "early" #t) + '(agent_settled #f) +) ;emit +(check-false (shows? "+stale")) +(check-false (shows? "stale-summary.txt")) +(check-true (shows? "+fresh")) +(emit '(turn_diff_summary 3 (("late-summary.txt" 1 0)))) +(check-true (shows? "late-summary.txt +1 -0")) + +;; Start-less completion also supports an early result. +(emit '(run_started 4) + '(diff_result 4 "nostart" "sample.txt" "+nostart" 1 0 1) + (end "nostart" #t) +) ;emit +(check-true (shows? "+nostart")) +(emit '(diff_result 4 "bad-payload") + '(turn_diff_summary 4 (("not-a-count.txt" "1" 0))) +) ;emit +(emit '(agent_settled #f)) +(check-false (shows? "not-a-count.txt")) + +;; Keep the same card, invalidate its render cache, and reuse the existing +;; sanitation and Ctrl+O folding path for plain-text diff output. +(emit '(run_started 5) (start "fold") (end "fold" #t)) +(let* ((card (cdr (assoc "fold" (%pi-app-tool-cards app)))) + (before (component-render card 80)) + (preview (string-append (apply string-append (make-list 12 "+line\n")) + "+\t\x1b;[33m最后一行\x1b;[0m" + ) ;string-append + ) ;preview + ) ; + (emit (list 'diff_result 5 "fold" "sample.txt" preview 13 0 1)) + (check + (eq? card (cdr (assoc "fold" (%pi-app-tool-cards app)))) + => + #t + ) ;check + (check-false (eq? before (component-render card 80))) + (check-true (shows? "ctrl+o to expand")) + (check-false (shows? "最后一行")) + (pi-app-toggle-tool-output! app) + (check-true (shows? "+ 最后一行")) +) ;let* + +;; The actual queue dispatch, not just run_started, closes the previous turn. +(pi-app-submit! app "the next task") +(emit '(turn_diff_summary 5 (("after-dispatch.txt" 1 0)))) +(check-false (shows? "after-dispatch.txt")) +(check-false (assoc "fold" (%pi-app-tool-cards app))) +;; If run_started was dropped, a reused ID still creates a new plain card. +(emit (start "fold") (end "fold" #t)) +(check-true (shows? "Saved!")) +(check-true (shows? "+ 最后一行")) +(check-report) diff --git a/app/tests/liii/pi/app/interactive/app-diff/diff-history-test.scm b/app/tests/liii/pi/app/interactive/app-diff/diff-history-test.scm new file mode 100644 index 00000000..b8ecf04f --- /dev/null +++ b/app/tests/liii/pi/app/interactive/app-diff/diff-history-test.scm @@ -0,0 +1,79 @@ +;; pi-app-rebuild-history! must not retain runtime diff state across a resume. +;; Given an app and a readable session path, it returns #t after replacing the +;; chat with historical messages and leaves the underlying session unchanged. +(import (scheme base) + (scheme file) + (liii check) + (liii go) + (liii path) + (liii string) + (liii tempfile) + (liii tui) + (liii pi code core session-manager) + (liii pi app interactive app) + (liii pi app interactive app-history) +) ;import + +(check-set-mode! 'report-failed) + +(let* ((tmp (make-temp-dir "gf-pi-diff-history-")) + (file (path->string (path-join tmp "history.jsonl"))) + (date "2026-11-04T08:00:00.000Z") + (app (make-pi-app + :terminal (make-virtual-terminal 100 30) + :handler (lambda args #f) + :bash-handler (lambda args (values "" 0)) + ) ;make-pi-app + ) ;app + (events (pi-app-event-chan app)) + ) ; + (define (emit . items) + (for-each (lambda (event) (chan-send! events event 100)) items) + (pi-app-poll-events! app) + ) ;define + (define (chat) + (string-join (map strip-ansi (pi-app-transcript-lines app)) "\n") + ) ;define + (dynamic-wind (lambda () #t) + (lambda () + (save-session-header! file "diff-resume" date tmp) + (save-session-message! file + (string-append "{\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"History reply\"}," + "{\"type\":\"toolCall\",\"id\":\"same\",\"name\":\"edit\",\"arguments\":{\"path\":\"never-read.txt\"," + "\"edits\":[{\"oldText\":\"old\",\"newText\":\"new\"}]}}]}" + ) ;string-append + "m1" + #f + date + ) ;save-session-message! + (save-session-message! file + (string-append "{\"role\":\"toolResult\",\"toolCallId\":\"same\",\"toolName\":\"edit\"," + "\"content\":[{\"type\":\"text\",\"text\":\"Historical tool result\"}],\"isError\":false}" + ) ;string-append + "m2" + "m1" + date + ) ;save-session-message! + (let ((original (path-read-text file))) + (emit '(run_started 1) + '(diff_result 1 "same" "never-read.txt" "+old-live" 1 1 1) + '(turn_diff_summary 1 (("never-read.txt" 1 1))) + ) ;emit + (check-true (pi-app-rebuild-history! app file)) + (emit '(diff_result 1 "same" "never-read.txt" "+late-live" 1 1 1) + '(turn_diff_summary 1 (("never-read.txt" 1 1))) + '(agent_settled #f) + ) ;emit + (check-true (string-contains? (chat) "History reply")) + (check-true (string-contains? (chat) "Historical tool result")) + (check-false (string-contains? (chat) "Turn changes")) + (check-false (string-contains? (chat) "+1 -1")) + (check-false (string-contains? (chat) "-live")) + (check (path-read-text file) => original) + ) ;let + ) ;lambda + (lambda () (when (file-exists? file) (delete-file file)) (path-rmdir tmp)) + ) ;dynamic-wind +) ;let* + +(check-report) diff --git a/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm b/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm new file mode 100644 index 00000000..e6be21cf --- /dev/null +++ b/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm @@ -0,0 +1,148 @@ +;; %pi-diff-executor and %pi-diff-emit decorate the executor and event bridge +;; with one run's runtime state. They preserve the original result and events. +;; %pi-diff-finish! closes the run without waiting for the display worker. +(import (scheme base) + (scheme file) + (liii check) + (liii go) + (liii path) + (liii tempfile) + (liii pi ai tools result) + (liii pi code core tools) + (liii pi code core tools read) + (liii pi app interactive diff-capture) +) ;import + +(check-set-mode! 'report-failed) + +(define tmp (make-temp-dir "gf-pi-diff-capture-")) + +(define target (path->string (path-join tmp "sample.txt"))) + +(define jobs (make-chan 2)) + +(define run (%pi-make-diff-capture 1 jobs #f)) + +(define calls 0) + +(define original #f) + +(define emit (%pi-diff-emit run (lambda args args))) + +(define exec + (%pi-diff-executor run + (lambda args + (set! calls (+ calls 1)) + (set! original (apply execute-tool-call args)) + original + ) ;lambda + ) ;%pi-diff-executor +) ;define + +(define (write-args body) + (list (cons "path" "sample.txt") (cons "content" body)) +) ;define + +(define (run-tool id name args) + (check (emit 'tool_execution_start id name args) + => + (list 'tool_execution_start id name args) + ) ;check + (let ((result (exec name args :cwd tmp))) + (check (eq? result original) => #t) + (emit 'tool_execution_end id name result (pi-tool-result-is-error result)) + result + ) ;let +) ;define + +(dynamic-wind (lambda () #t) + (lambda () + (check-false (pi-tool-result-is-error (run-tool "write" "write" (write-args "one\n"))) + ) ;check-false + (check-false + (pi-tool-result-is-error (run-tool "edit" + "edit" + '(("path" . "./sample.txt") + ("edits" + . #((("oldText" . "one") + ("newText" . "two")) + ) ;# + )) + ) ;run-tool + ) ;pi-tool-result-is-error + ) ;check-false + (check-true + (pi-tool-result-is-error (run-tool "bad" "write" '(("path" . #f) + ("content" . "no"))) + ) ;pi-tool-result-is-error + ) ;check-true + (let ((write-job (chan-try-recv! jobs #f)) (edit-job (chan-try-recv! jobs #f))) + (check (list-tail write-job 3) + => + (list "sample.txt" + (pi-resolve-path target tmp) + "" + "one\n" + (%diff-capture-done run) + ) ;list + ) ;check + (check (list-tail edit-job 3) + => + (list "./sample.txt" + (pi-resolve-path target tmp) + "one\n" + "two\n" + (%diff-capture-done run) + ) ;list + ) ;check + (check (chan-try-recv! jobs #f) => #f) + (check (path-read-text target) => "two\n") + ) ;let + ;; Goldfish can report success from write on a directory; no stats should + ;; be fabricated in that case, regardless of the original result's status. + (let ((directory-write + (run-tool "directory" "write" `(("path" . ,tmp) ("content" . "bad"))) + ) ;directory-write + ) ; + (unless (pi-tool-result-is-error directory-write) + (check (list-tail (chan-try-recv! jobs #f) 5) + => + (list #f #f (%diff-capture-done run)) + ) ;check + ) ;unless + ) ;let + (check (%diff-text-snapshot (string #\a #\nul #\b)) => #f) + ;; A large snapshot does not change the original result. + (check-false + (pi-tool-result-is-error (run-tool "large" "write" (write-args (make-string 262145 #\z))) + ) ;pi-tool-result-is-error + ) ;check-false + (check (list-tail (chan-try-recv! jobs #f) 5) + => + (list #f #f (%diff-capture-done run)) + ) ;check + (check-false (pi-tool-result-is-error (run-tool "overwrite" "write" (write-args "small\n"))) + ) ;check-false + (check (list-tail (chan-try-recv! jobs #f) 5) + => + (list #f #f (%diff-capture-done run)) + ) ;check + ;; Saturate the job queue: no worker is consuming while the tool completes. + (chan-try-send! jobs 'busy) + (chan-try-send! jobs 'busy) + (check-false (pi-tool-result-is-error (run-tool "full" "write" (write-args "full\n"))) + ) ;check-false + (%pi-diff-finish! run) + (check calls => 7) + (check (path-read-text target) => "full\n") + ) ;lambda + (lambda () + (chan-close! jobs) + (when (file-exists? target) + (delete-file target) + ) ;when + (path-rmdir tmp) + ) ;lambda +) ;dynamic-wind + +(check-report) diff --git a/app/tests/liii/pi/app/interactive/diff-worker/diff-performance-test.scm b/app/tests/liii/pi/app/interactive/diff-worker/diff-performance-test.scm new file mode 100644 index 00000000..32b0f60a --- /dev/null +++ b/app/tests/liii/pi/app/interactive/diff-worker/diff-performance-test.scm @@ -0,0 +1,133 @@ +;; %diff-performance: measure bounded capture and background diff separately. +;; This test takes no arguments. A gated display worker must not block the tool, +;; queue settlement, or the next dispatch; no timing threshold is machine-specific. +(import (scheme base) + (scheme file) + (scheme time) + (scheme write) + (liii check) + (liii go) + (liii path) + (liii tempfile) + (liii tui) + (liii pi ai tools result) + (liii pi agent queue) + (liii pi code core tools) + (liii pi app interactive app) + (liii pi app interactive diff-capture) + (liii pi app interactive diff-worker) +) ;import + +(check-set-mode! 'report-failed) + +(define (record-diff-time jobs events elapsed gate) + (when (eq? (chan-recv! gate 3000 #f) 'start) + (let ((t0 (monotonic-nanosecond))) + (%pi-display-worker jobs events) + (chan-try-send! elapsed (- (monotonic-nanosecond) t0)) + ) ;let + ) ;when +) ;define + +(define (lines line n) + (apply string-append (make-list n line)) +) ;define + +(define (measure label thunk n) + (let ((t0 (monotonic-nanosecond))) + (do ((i 0 (+ i 1))) + ((= i n)) + (thunk) + ) ;do + (display label) + (display (/ (- (monotonic-nanosecond) t0) (* 1000000.0 n))) + (display " ms\n") + ) ;let +) ;define + +(let* ((tmp (make-temp-dir "gf-pi-diff-perf-")) + (file (path->string (path-join tmp "perf.txt"))) + (small "first\nsecond\n") + (before (lines "before line 1345\n" 450)) + (after (lines "after line 1345\n" 450)) + (jobs (make-chan 16)) + (results (make-chan 4)) + (gate (make-chan 1)) + (elapsed (make-chan 1)) + (dispatched #f) + (app (make-pi-app + :terminal (make-virtual-terminal 100 30) + :handler (lambda (_ msg) (set! dispatched msg)) + :bash-handler (lambda args (values "" 0)) + ) ;make-pi-app + ) ;app + (events (pi-app-event-chan app)) + (queue (pi-app-queue app)) + (run (%pi-make-diff-capture 1 jobs events)) + (emit + (%pi-diff-emit run + (lambda (kind . args) (chan-try-send! events (cons kind args))) + ) ;%pi-diff-emit + ) ;emit + (execute (%pi-diff-executor run execute-tool-call)) + (args `(("path" . ,file) ("content" . ,after))) + ) ; + (go (record-diff-time jobs results elapsed gate)) + (dynamic-wind (lambda () #t) + (lambda () + (path-write-text file small) + (measure "bounded snapshot, 13 bytes (mean of 40): " + (lambda () (%diff-read-snapshot file #f)) + 40 + ) ;measure + (path-write-text file before) + (measure "bounded snapshot, 450 lines (mean of 20): " + (lambda () (%diff-read-snapshot file #f)) + 20 + ) ;measure + (pi-queue-enqueue! queue "running") + (check (pi-queue-dequeue-next! queue) => "running") + (chan-try-send! events '(run_started 1)) + (emit 'tool_execution_start "write-perf" "write" args) + (let ((res (execute "write" args))) + (check-false (pi-tool-result-is-error res)) + (emit 'tool_execution_end "write-perf" "write" args #f res) + ) ;let + (emit 'assistant_text "Final reply") + (emit 'agent_settled #f) + (%pi-diff-finish! run) + (pi-app-poll-events! app) + (check-true (pi-queue-idle? queue)) + (check (path-read-text file) => after) + (pi-app-submit! app "next run need not wait") + (check dispatched => "next run need not wait") + (check-true (pi-queue-idle? queue)) + (check (chan-try-recv! results #f) => #f) + (chan-try-send! gate 'start) + (let* ((t0 (monotonic-nanosecond)) (ev (chan-recv! results 3000 #f))) + (check (and ev (list-tail ev 4)) => '(#f 450 450 1)) + (display "worker start to single-card result, 450x450: ") + (display (/ (- (monotonic-nanosecond) t0) 1000000.0)) + (display " ms\n") + ) ;let* + (check (chan-recv! results 3000 #f) + => + (list 'turn_diff_summary 1 (list (list file 450 450))) + ) ;check + (chan-close! jobs) + (check-true (number? (chan-recv! elapsed 3000 #f))) + ) ;lambda + (lambda () + (chan-close! gate) + (chan-close! jobs) + (chan-close! results) + (chan-close! elapsed) + (when (file-exists? file) + (delete-file file) + ) ;when + (path-rmdir tmp) + ) ;lambda + ) ;dynamic-wind +) ;let* + +(check-report) diff --git a/app/tests/liii/pi/app/interactive/diff-worker/diff-worker-test.scm b/app/tests/liii/pi/app/interactive/diff-worker/diff-worker-test.scm new file mode 100644 index 00000000..6688cc48 --- /dev/null +++ b/app/tests/liii/pi/app/interactive/diff-worker/diff-worker-test.scm @@ -0,0 +1,88 @@ +;; %pi-display-worker (jobs events) consumes serialized diff jobs in a go +;; session and sends runtime diff_result and turn_diff_summary events. Unavailable +;; snapshots/budget overflow produce skipped results; no file I/O is performed. +(import (scheme base) + (liii check) + (only (liii base) byte-vector->string) + (liii go) + (liii string) + (liii pi app interactive diff-worker) +) ;import + +(check-set-mode! 'report-failed) + +(define jobs (make-chan 16)) + +(define events (make-chan 16)) + +(define (send id call path key before after done) + (check-true (chan-send! jobs (list 'diff_job id call path key before after done) 100) + ) ;check-true +) ;define + +(define (next-event) + (chan-recv! events 3000 'timeout) +) ;define + +;; Queue the snapshots first, but do not signal completion yet. Tools can +;; finish and the next run can be queued without waiting for these results. +(let ((first (make-chan 1)) + (second (make-chan 1)) + (large-a (apply string-append (make-list 1000 "old\n"))) + (large-b (apply string-append (make-list 1000 "new\n"))) + ) ; + (send 1 "a" "same.txt" "/same.txt" "one\n" "two\n" first) + (send 1 "b" "./same.txt" "/same.txt" "two\n" "one\n" first) + (send 1 "c" "net.txt" "/net.txt" "a\n" "a\nb\n" first) + (send 1 "d" "net.txt" "/net.txt" "a\nb\n" "b\n" first) + (send 1 "e" "missed.txt" "/missed.txt" "a\n" "b\n" first) + (send 1 "f" "oversized.txt" "/oversized.txt" large-a large-b first) + (send 1 "g" "incomplete.txt" "/incomplete.txt" #f #f first) + (send 1 "h" "incomplete.txt" "/incomplete.txt" "a\n" "b\n" first) + (send 2 "a" "next.txt" "/next.txt" "" "end\n" second) + (check (chan-try-recv! events #f) => #f) + (chan-send! first '(8 "/missed.txt") 100) + (chan-close! first) + (chan-send! second '(1) 100) + (chan-close! second) + (go (%pi-display-worker jobs events)) + (check (next-event) + => + '(diff_result 1 "a" "same.txt" "@@ -1,1 +1,1 @@\n-one\n+two" 1 1 1) + ) ;check + (check (next-event) + => + '(diff_result 1 "b" "./same.txt" "@@ -1,1 +1,1 @@\n-two\n+one" 1 1 1) + ) ;check + (do ((i 0 (+ i 1))) + ((= i 3)) + (check (car (next-event)) => 'diff_result) + ) ;do + (check (next-event) => '(diff_result 1 "f" "oversized.txt" #f #f #f #f)) + (check (next-event) => '(diff_result 1 "g" "incomplete.txt" #f #f #f #f)) + (check (car (next-event)) => 'diff_result) + (check (next-event) => '(turn_diff_summary 1 (("net.txt" 1 1)))) + (check (next-event) + => + '(diff_result 2 "a" "next.txt" "@@ -0,0 +1,1 @@\n+end" 1 0 1) + ) ;check + (check (next-event) => '(turn_diff_summary 2 (("next.txt" 1 0)))) +) ;let +(let ((third (make-chan 1))) + (send 3 + "invalid" + "binary.dat" + "/binary.dat" + (byte-vector->string (bytevector 255)) + "text\n" + third + ) ;send + (chan-send! third '(1) 100) + (chan-close! third) + (check (next-event) => '(diff_result 3 "invalid" "binary.dat" #f #f #f #f)) + (check (next-event) => '(turn_diff_summary 3 ())) +) ;let +(chan-close! jobs) +(chan-close! events) + +(check-report) diff --git a/app/tests/liii/pi/app/interactive/tool-diff/tool-diff-test.scm b/app/tests/liii/pi/app/interactive/tool-diff/tool-diff-test.scm new file mode 100644 index 00000000..6b2eebc4 --- /dev/null +++ b/app/tests/liii/pi/app/interactive/tool-diff/tool-diff-test.scm @@ -0,0 +1,57 @@ +;; %tool-diff +;; (%tool-diff before after [with-text?]) takes UTF-8 strings and returns +;; (diff-text added removed first-changed-line), or #f when the input exceeds +;; a calculation budget. Omitted previews and absent line positions are #f. +;; CRLF is equivalent to LF; a changed final newline changes one logical line. +(import (scheme base) + (liii check) + (liii string) + (liii pi app interactive tool-diff) +) ;import + +(check-set-mode! 'report-failed) + +(check (%tool-diff "" "") => '(#f 0 0 #f)) +(check (%tool-diff "" "甲\n乙\n") => '("@@ -0,0 +1,2 @@\n+甲\n+乙" 2 0 1)) +(check (%tool-diff "one\n\n" "") => '("@@ -1,2 +0,0 @@\n-one\n-" 0 2 1)) +(check (%tool-diff "one\r\ntwo\r\n" "one\ntwo\n") => '(#f 0 0 #f)) +(check (%tool-diff "one\rtwo\r" "one\ntwo\n") => '(#f 0 0 #f)) +(check-catch 'type-error (%tool-diff 1 "")) +(check (%tool-diff "a\nb" "x\ny" #t (lambda () #t)) => #f) +(check (%tool-diff "one\n" "one") + => + '("@@ -1,1 +1,1 @@\n-one\n+one\n\\ No newline at end of file" 1 1 1) +) ;check +(check (%tool-diff "a\nx\nx\nb\n" "a\nx\nb\nx\n" #f) => '(#f 1 1 3)) +(check (%tool-diff "a\nc\n" "a\nb\nc\n") + => + '("@@ -1,2 +1,3 @@\n a\n+b\n c" 1 0 2) +) ;check +(check (%tool-diff "a\nx\nb\nc\nd\ne\nf\ng\nh\ny\nz\n" + "a\nX\nb\nc\nd\ne\nf\ng\nh\nY\nz\n" + ) ;%tool-diff + => + '("@@ -1,5 +1,5 @@\n a\n-x\n+X\n b\n c\n d\n@@ -7,5 +7,5 @@\n f\n g\n h\n-y\n+Y\n z" + 2 2 2) +) ;check + +(define (repeat-line line n) + (apply string-append (make-list n line)) +) ;define + +;; A disjoint matrix is declined, but identical edges are trimmed first. +(check (%tool-diff (repeat-line "a\n" 1000) (repeat-line "b\n" 1000)) => #f) +(let ((common (repeat-line "a\n" 1000))) + (check (%tool-diff (string-append common "before\n") + (string-append common "after\n") + #f + ) ;%tool-diff + => + '(#f 1 1 1001) + ) ;check +) ;let +;; Large output can retain exact counts without creating a huge UI payload. +(check (%tool-diff "" (repeat-line "a\n" 1000)) => '(#f 1000 0 1)) +(check (%tool-diff "" (repeat-line "\n" 9000)) => #f) +(check (%tool-diff (make-string 262145 #\a) "") => #f) +(check-report) From 4aac12f7edd5f83765325ecccd735b883c3271f8 Mon Sep 17 00:00:00 2001 From: pigmagicfly <2831850183@qq.com> Date: Thu, 26 Nov 2026 14:27:56 +0800 Subject: [PATCH 2/3] =?UTF-8?q?[1345]=20=E4=BF=AE=E5=A4=8D=E7=BA=AF?= =?UTF-8?q?=E6=96=87=E6=9C=AC=20Diff=20=E7=9A=84=20CI=20=E9=9D=99=E6=80=81?= =?UTF-8?q?=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/liii/pi/app/interactive/diff-capture.scm | 120 +++++------ app/liii/pi/app/interactive/tool-diff.scm | 186 ++++++++++-------- .../pi/app/interactive/app-diff/README.md | 13 ++ .../interactive/app-diff/app-diff-test.scm | 18 +- .../app-diff/diff-history-test.scm | 17 +- .../pi/app/interactive/diff-capture/README.md | 11 ++ .../diff-capture/diff-capture-test.scm | 18 +- .../pi/app/interactive/diff-worker/README.md | 12 ++ .../diff-worker/diff-performance-test.scm | 18 +- .../diff-worker/diff-worker-test.scm | 17 +- .../pi/app/interactive/tool-diff/README.md | 11 ++ .../interactive/tool-diff/tool-diff-test.scm | 20 +- 12 files changed, 305 insertions(+), 156 deletions(-) create mode 100644 app/tests/liii/pi/app/interactive/app-diff/README.md create mode 100644 app/tests/liii/pi/app/interactive/diff-capture/README.md create mode 100644 app/tests/liii/pi/app/interactive/diff-worker/README.md create mode 100644 app/tests/liii/pi/app/interactive/tool-diff/README.md diff --git a/app/liii/pi/app/interactive/diff-capture.scm b/app/liii/pi/app/interactive/diff-capture.scm index bf5b13ab..4c6be89a 100644 --- a/app/liii/pi/app/interactive/diff-capture.scm +++ b/app/liii/pi/app/interactive/diff-capture.scm @@ -9,7 +9,8 @@ %diff-capture-misses-set! %diff-tool? %diff-read-snapshot %pi-diff-start %diff-without-nul? %diff-text-snapshot %diff-cwd %diff-try-send! %diff-enqueue! %pi-with-turn-diff %diff-written-size? %diff-valid-path? - %diff-read-bounded + %diff-read-bounded %diff-snapshot-before %diff-snapshot-after + %diff-observe-tool ) ;export (import (scheme base) (scheme file) @@ -163,65 +164,72 @@ ) ;and ) ;define + ;; Return the resolved path and the bounded input snapshot. An unreadable + ;; file still has a key so it can be excluded from the run's net summary. + (define (%diff-snapshot-before args opts write?) + (let ((display-path (pi-arg-ref args "path"))) + (and (%diff-valid-path? display-path) + (let* ((resolved (pi-resolve-path display-path (%diff-cwd opts))) + (absolute (and (<= (string-length resolved) 4096) resolved)) + (write-after (and write? (%diff-text-snapshot (pi-arg-ref args "content")))) + (before + (and absolute + (or (not write?) write-after) + (catch #t (lambda () (%diff-read-snapshot absolute write?)) (lambda args #f)) + ) ;and + ) ;before + ) ; + (list display-path absolute before write-after) + ) ;let* + ) ;and + ) ;let + ) ;define + + (define (%diff-snapshot-after absolute before write? write-after) + (and before + (catch #t + (lambda () + (if write? + (and (%diff-written-size? absolute write-after) write-after) + (%diff-read-snapshot absolute #f) + ) ;if + ) ;lambda + (lambda args #f) + ) ;catch + ) ;and + ) ;define + + (define (%diff-observe-tool run id execute name args opts) + (let* ((write? (equal? name "write")) + (snapshot (catch #t (lambda () (%diff-snapshot-before args opts write?)) (lambda args #f)) + ) ;snapshot + ;; Keep the original call outside the observer catches: execute + ;; once, propagate exceptions, and return the exact result. + (result (apply execute name args opts)) + ) ; + (when (and (pi-tool-result? result) (not (pi-tool-result-is-error result))) + (let* ((path (and snapshot (car snapshot))) + (absolute (and snapshot (cadr snapshot))) + (before (and snapshot (caddr snapshot))) + (after (%diff-snapshot-after absolute before write? (and snapshot (cadddr snapshot))) + ) ;after + ) ; + (catch #t + (lambda () (%diff-enqueue! run id path absolute before after)) + (lambda args #f) + ) ;catch + ) ;let* + ) ;when + result + ) ;let* + ) ;define + (define (%pi-diff-executor run execute) (lambda (name args . opts) (let ((call (%diff-capture-call run))) - (if - (not (and call (equal? (cdr call) name))) + (if (and call (equal? (cdr call) name)) + (%diff-observe-tool run (car call) execute name args opts) (apply execute name args opts) - (let ((absolute #f) - (display-path #f) - (before #f) - (write-after #f) - (write? (equal? name "write")) - ) ; - (catch #t - (lambda () - (let ((p (pi-arg-ref args "path"))) - (when (%diff-valid-path? p) - (set! display-path p) - (set! absolute (pi-resolve-path p (%diff-cwd opts))) - (when (> (string-length absolute) 4096) - (set! absolute #f) - ) ;when - (when write? - (set! write-after (%diff-text-snapshot (pi-arg-ref args "content"))) - ) ;when - (when (and absolute (or (not write?) write-after)) - (set! before (%diff-read-snapshot absolute write?)) - ) ;when - ) ;when - ) ;let - ) ;lambda - (lambda args #f) - ) ;catch - ;; Keep the originals outside the observation catches: execute - ;; once, propagate its exceptions, and return its exact result. - (let ((result (apply execute name args opts))) - (when (and (pi-tool-result? result) (not (pi-tool-result-is-error result))) - (let ((after - (and before - (catch #t - (lambda () - (if write? - (and (%diff-written-size? absolute write-after) write-after) - (%diff-read-snapshot absolute #f) - ) ;if - ) ;lambda - (lambda args #f) - ) ;catch - ) ;and - ) ;after - ) ; - (catch #t - (lambda () (%diff-enqueue! run (car call) display-path absolute before after)) - (lambda args #f) - ) ;catch - ) ;let - ) ;when - result - ) ;let - ) ;let ) ;if ) ;let ) ;lambda diff --git a/app/liii/pi/app/interactive/tool-diff.scm b/app/liii/pi/app/interactive/tool-diff.scm index 21f055ae..adc63317 100644 --- a/app/liii/pi/app/interactive/tool-diff.scm +++ b/app/liii/pi/app/interactive/tool-diff.scm @@ -5,7 +5,8 @@ ;; Export the private helpers for the independent go session. (export %tool-diff %diff-snapshot-bytes %diff-lines %diff-common-edge %diff-lines-count-limit %diff-matrix %diff-operations %diff-hunks - %diff-hunk-header %diff-preview + %diff-hunk-header %diff-preview %diff-write-hunk %diff-write-op + %diff-compare-lines %diff-compare-middle %diff-middle-result %diff-cr-end ) ;export (import (scheme base) (scheme write) @@ -16,6 +17,15 @@ (define %diff-snapshot-bytes (* 256 1024)) (define %diff-lines-count-limit 8192) + (define (%diff-cr-end text cr size) + (let ((next (+ cr 1))) + (if (and (< next size) (char=? (string-ref text next) #\newline)) + (+ next 1) + next + ) ;if + ) ;let + ) ;define + ;; Keep the newline in the comparison key so a missing final newline is ;; visible as a line change. Fold CRLF and bare CR like path-read-text. (define (%diff-lines text) @@ -33,13 +43,7 @@ ) ; ((char=? (string-ref text i) #\return) (and (< count %diff-lines-count-limit) - (let ((next - (if - (and (< (+ i 1) size) (char=? (string-ref text (+ i 1)) #\newline)) - (+ i 2) - (+ i 1) - ) ;if - ) ;next + (let ((next (%diff-cr-end text i size)) (line (string-append (substring text start i) "\n")) ) ; (loop next next (cons line lines) (+ count 1)) @@ -164,47 +168,102 @@ ) ;let* ) ;define + (define (%diff-write-op old new op out) + (let* ((tag (car op)) + (line (if (char=? tag #\+) (vector-ref new (caddr op)) (vector-ref old (cadr op))) + ) ;line + (end (- (string-length line) 1)) + (terminated? (char=? (string-ref line end) #\newline)) + ) ; + (write-char tag out) + (display (if terminated? (substring line 0 end) line) out) + (newline out) + (unless terminated? + (display "\\ No newline at end of file" out) + (newline out) + ) ;unless + ) ;let* + ) ;define + + (define (%diff-write-hunk old new ops hunk out) + (let ((start (car hunk)) (end (cdr hunk))) + (display (%diff-hunk-header (vector-ref ops start) (vector-ref ops (- end 1))) + out + ) ;display + (newline out) + (do ((i start (+ i 1))) + ((= i end)) + (%diff-write-op old new (vector-ref ops i) out) + ) ;do + ) ;let + ) ;define + (define (%diff-preview old new ops) - (let ((text - (call-with-output-string - (lambda (out) - (define (write-line text) - (display text out) - (newline out) - ) ;define - (for-each - (lambda (hunk) - (let ((start (car hunk)) (end (cdr hunk))) - (write-line (%diff-hunk-header (vector-ref ops start) (vector-ref ops (- end 1))) - ) ;write-line - (do ((i start (+ i 1))) - ((= i end)) - (let* ((op (vector-ref ops i)) - (tag (car op)) - (line (if (char=? tag #\+) (vector-ref new (caddr op)) (vector-ref old (cadr op))) - ) ;line - (end (- (string-length line) 1)) - (terminated? (char=? (string-ref line end) #\newline)) - ) ; - (write-char tag out) - (write-line (if terminated? (substring line 0 end) line)) - (unless terminated? - (write-line "\\ No newline at end of file") - ) ;unless - ) ;let* - ) ;do - ) ;let - ) ;lambda - (%diff-hunks ops) - ) ;for-each - ) ;lambda - ) ;call-with-output-string - ) ;text - ) ; + (let* ((hunks (%diff-hunks ops)) + (text + (call-with-output-string + (lambda (out) + (for-each (lambda (hunk) (%diff-write-hunk old new ops hunk out)) hunks) + ) ;lambda + ) ;call-with-output-string + ) ;text + ) ; (and (<= (string-length text) (+ 1 (* 48 1024))) (string-trim-right text #\newline) ) ;and - ) ;let + ) ;let* + ) ;define + + (define (%diff-middle-result old new start n m stride cells with-text?) + (let* ((shared (if cells (vector-ref cells 0) 0)) + (added (- m shared)) + (removed (- n shared)) + (changed (+ added removed)) + (ops (and with-text? + (<= 1 changed 200) + (%diff-operations old new start n m stride cells) + ) ;and + ) ;ops + ) ; + (list (and ops (%diff-preview old new ops)) + added + removed + (and (> changed 0) (+ start 1)) + ) ;list + ) ;let* + ) ;define + + (define (%diff-compare-middle old new start n m with-text? cancelled?) + (let* ((stride (+ m 1)) (matrix? (and (> n 0) (> m 0)))) + (and + (or (not matrix?) (<= (* (+ n 1) stride) 250000)) + (let ((cells (and matrix? (%diff-matrix old new start n m stride cancelled?)))) + (and (or (not matrix?) cells) + (not (cancelled?)) + (%diff-middle-result old new start n m stride cells with-text?) + ) ;and + ) ;let + ) ;and + ) ;let* + ) ;define + + (define (%diff-compare-lines old new with-text? cancelled?) + (let* ((old-size (vector-length old)) + (new-size (vector-length new)) + (limit (min old-size new-size)) + (start (%diff-common-edge old new 0 0 1 limit)) + (tail (%diff-common-edge old new (- old-size 1) (- new-size 1) -1 (- limit start)) + ) ;tail + ) ; + (%diff-compare-middle old + new + start + (- old-size start tail) + (- new-size start tail) + with-text? + cancelled? + ) ;%diff-compare-middle + ) ;let* ) ;define (define* (%tool-diff before after (with-text? #t) (cancelled? (lambda () #f))) @@ -215,42 +274,7 @@ (if (string=? before after) '(#f 0 0 #f) (let ((old (%diff-lines before)) (new (%diff-lines after))) - (and old - new - (let* ((old-size (vector-length old)) - (new-size (vector-length new)) - (limit (min old-size new-size)) - (start (%diff-common-edge old new 0 0 1 limit)) - (tail (%diff-common-edge old new (- old-size 1) (- new-size 1) -1 (- limit start)) - ) ;tail - (n (- old-size start tail)) - (m (- new-size start tail)) - (stride (+ m 1)) - ) ; - (and - (or (zero? n) (zero? m) (<= (* (+ n 1) stride) 250000)) - (let* ((matrix? (and (> n 0) (> m 0))) - (cells (and matrix? (%diff-matrix old new start n m stride cancelled?))) - (shared (if cells (vector-ref cells 0) 0)) - (added (- m shared)) - (removed (- n shared)) - (changed (+ added removed)) - ) ; - (and (or (not matrix?) cells) - (not (cancelled?)) - (list (and with-text? - (<= 1 changed 200) - (%diff-preview old new (%diff-operations old new start n m stride cells)) - ) ;and - added - removed - (and (> changed 0) (+ start 1)) - ) ;list - ) ;and - ) ;let* - ) ;and - ) ;let* - ) ;and + (and old new (%diff-compare-lines old new with-text? cancelled?)) ) ;let ) ;if ) ;and diff --git a/app/tests/liii/pi/app/interactive/app-diff/README.md b/app/tests/liii/pi/app/interactive/app-diff/README.md new file mode 100644 index 00000000..62ee27a7 --- /dev/null +++ b/app/tests/liii/pi/app/interactive/app-diff/README.md @@ -0,0 +1,13 @@ +# (liii pi app interactive app-diff) 模块函数索引 + +本模块管理 TUI 当前轮的 diff 展示状态。通过 `(import (liii pi app interactive app-diff))` 在应用层接入;它不读写会话文件。 + +- `%pi-app-handle-diff-event!`:关联运行编号和工具调用编号,记录早到结果。 +- `%pi-app-apply-diff!`:工具完成后原地更新匹配的卡片。 +- `%pi-app-settle-diff!` / `%pi-app-reset-diff!`:结算时展示汇总,或在新一轮派发时丢弃旧状态。 + +`app-diff-test.scm` 覆盖事件顺序和渲染,`diff-history-test.scm` 覆盖会话恢复隔离。 + +```bash +gf test app/tests/liii/pi/app/interactive/app-diff +``` diff --git a/app/tests/liii/pi/app/interactive/app-diff/app-diff-test.scm b/app/tests/liii/pi/app/interactive/app-diff/app-diff-test.scm index 34dcb1a5..5e323d3d 100644 --- a/app/tests/liii/pi/app/interactive/app-diff/app-diff-test.scm +++ b/app/tests/liii/pi/app/interactive/app-diff/app-diff-test.scm @@ -1,6 +1,17 @@ -;; %pi-app-handle-diff-event! applies runtime display events without waiting. -;; (pi-app-poll-events! app) consumes the same events used by the real workers. -;; No return value is required; stale/duplicate results and failures are ignored. +;; pi-app-poll-events! +;; 消费展示事件,验证早到、迟到、重复结果和本轮汇总的顺序。 +;; +;; 语法 +;; ---- +;; (pi-app-poll-events! app) +;; +;; 参数 +;; ---- +;; app : pi-app?,已装配事件通道的 TUI 实例。 +;; +;; 返回值 +;; ---- +;; 不使用返回值;仅修改当前轮的匹配卡片,忽略过期或非法事件。 (import (scheme base) (liii check) (liii go) @@ -147,4 +158,5 @@ (emit (start "fold") (end "fold" #t)) (check-true (shows? "Saved!")) (check-true (shows? "+ 最后一行")) + (check-report) diff --git a/app/tests/liii/pi/app/interactive/app-diff/diff-history-test.scm b/app/tests/liii/pi/app/interactive/app-diff/diff-history-test.scm index b8ecf04f..d192fbb2 100644 --- a/app/tests/liii/pi/app/interactive/app-diff/diff-history-test.scm +++ b/app/tests/liii/pi/app/interactive/app-diff/diff-history-test.scm @@ -1,6 +1,17 @@ -;; pi-app-rebuild-history! must not retain runtime diff state across a resume. -;; Given an app and a readable session path, it returns #t after replacing the -;; chat with historical messages and leaves the underlying session unchanged. +;; pi-app-rebuild-history! +;; 成功恢复历史后,隔离恢复前的 diff 和汇总事件。 +;; +;; 语法 +;; ---- +;; (pi-app-rebuild-history! app session-file) +;; +;; 参数 +;; ---- +;; app : pi-app?,交互式应用实例;session-file : string?,会话 JSONL 路径。 +;; +;; 返回值 +;; ---- +;; boolean?。成功替换聊天记录返回 #t,失败返回 #f;不改写会话文件。 (import (scheme base) (scheme file) (liii check) diff --git a/app/tests/liii/pi/app/interactive/diff-capture/README.md b/app/tests/liii/pi/app/interactive/diff-capture/README.md new file mode 100644 index 00000000..326c3b15 --- /dev/null +++ b/app/tests/liii/pi/app/interactive/diff-capture/README.md @@ -0,0 +1,11 @@ +# (liii pi app interactive diff-capture) 模块函数索引 + +本模块只在 TUI agent worker 中观察 `edit` / `write`。通过 `(import (liii pi app interactive diff-capture))` 接入;原工具结果不变。 + +- `%pi-make-diff-capture`:创建本轮快照状态。 +- `%pi-diff-executor` / `%pi-diff-emit`:捕获有界快照,并按工具调用编号发送任务。 +- `%pi-diff-finish!` / `%pi-with-turn-diff`:在任务结束后释放本轮观察状态,不等待计算结果。 + +```bash +gf test app/tests/liii/pi/app/interactive/diff-capture +``` diff --git a/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm b/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm index e6be21cf..ba6a3b74 100644 --- a/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm +++ b/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm @@ -1,6 +1,18 @@ -;; %pi-diff-executor and %pi-diff-emit decorate the executor and event bridge -;; with one run's runtime state. They preserve the original result and events. -;; %pi-diff-finish! closes the run without waiting for the display worker. +;; %pi-diff-executor +;; 包装原工具执行器,并按同一轮的工具事件捕获有界快照。 +;; +;; 语法 +;; ---- +;; (%pi-diff-executor run execute) +;; +;; 参数 +;; ---- +;; run : 本轮快照状态,由 %pi-make-diff-capture 构造。 +;; execute : 原工具执行过程,接收工具名、参数和可选项。 +;; +;; 返回值 +;; ---- +;; procedure?。原样返回工具结果并传播原工具异常;快照不可用时跳过 diff。 (import (scheme base) (scheme file) (liii check) diff --git a/app/tests/liii/pi/app/interactive/diff-worker/README.md b/app/tests/liii/pi/app/interactive/diff-worker/README.md new file mode 100644 index 00000000..92f8f8f2 --- /dev/null +++ b/app/tests/liii/pi/app/interactive/diff-worker/README.md @@ -0,0 +1,12 @@ +# (liii pi app interactive diff-worker) 模块函数索引 + +本模块在独立的 go 会话中处理 TUI 展示任务。通过 `(import (liii pi app interactive diff-worker))` 接入。 + +- `%pi-display-worker`:消费有界快照,从内存计算单卡结果和本轮净变化,不读取磁盘。 +- `%diff-job!` / `%diff-finish-turn`:处理单个任务,结算后尝试发送汇总。 + +测试覆盖净变化、无效输入、预算降级和延迟放行 worker 时的队列调度。 + +```bash +gf test app/tests/liii/pi/app/interactive/diff-worker +``` diff --git a/app/tests/liii/pi/app/interactive/diff-worker/diff-performance-test.scm b/app/tests/liii/pi/app/interactive/diff-worker/diff-performance-test.scm index 32b0f60a..2f4041c2 100644 --- a/app/tests/liii/pi/app/interactive/diff-worker/diff-performance-test.scm +++ b/app/tests/liii/pi/app/interactive/diff-worker/diff-performance-test.scm @@ -1,6 +1,18 @@ -;; %diff-performance: measure bounded capture and background diff separately. -;; This test takes no arguments. A gated display worker must not block the tool, -;; queue settlement, or the next dispatch; no timing threshold is machine-specific. +;; %pi-display-worker +;; 用受控通道延迟展示线程,验证工具、结算和下一轮不会等待 diff。 +;; +;; 语法 +;; ---- +;; (%pi-display-worker jobs events) +;; +;; 参数 +;; ---- +;; jobs : 展示任务通道;events : 展示结果通道。 +;; +;; 返回值 +;; ---- +;; 不使用返回值;通过 events 发送结果,jobs 关闭时退出。 +;; 测试还分别记录有界快照和后台计算耗时,不以某个耗时值作为门槛。 (import (scheme base) (scheme file) (scheme time) diff --git a/app/tests/liii/pi/app/interactive/diff-worker/diff-worker-test.scm b/app/tests/liii/pi/app/interactive/diff-worker/diff-worker-test.scm index 6688cc48..c23249d7 100644 --- a/app/tests/liii/pi/app/interactive/diff-worker/diff-worker-test.scm +++ b/app/tests/liii/pi/app/interactive/diff-worker/diff-worker-test.scm @@ -1,6 +1,17 @@ -;; %pi-display-worker (jobs events) consumes serialized diff jobs in a go -;; session and sends runtime diff_result and turn_diff_summary events. Unavailable -;; snapshots/budget overflow produce skipped results; no file I/O is performed. +;; %pi-display-worker +;; 在 go 会话内消费有界快照并汇总本轮净变化,不读盘。 +;; +;; 语法 +;; ---- +;; (%pi-display-worker jobs events) +;; +;; 参数 +;; ---- +;; jobs : 展示任务通道;events : 接收 diff_result 和 turn_diff_summary 的通道。 +;; +;; 返回值 +;; ---- +;; 不使用返回值;通过 events 发送结果。预算超限或快照无效时跳过该 diff。 (import (scheme base) (liii check) (only (liii base) byte-vector->string) diff --git a/app/tests/liii/pi/app/interactive/tool-diff/README.md b/app/tests/liii/pi/app/interactive/tool-diff/README.md new file mode 100644 index 00000000..f10f22ba --- /dev/null +++ b/app/tests/liii/pi/app/interactive/tool-diff/README.md @@ -0,0 +1,11 @@ +# (liii pi app interactive tool-diff) 模块函数索引 + +本模块提供有预算限制的行级 diff,通过 `(import (liii pi app interactive tool-diff))` 在展示 worker 中使用。 + +- `%tool-diff`:统计增删行、生成可选的纯文本上下文块。预算超限时不估算统计。 +- `%diff-lines` / `%diff-matrix`:规范化换行并计算最长公共子序列。 +- `%diff-hunks` / `%diff-preview`:将变更分组成有界的上下文预览。 + +```bash +gf test app/tests/liii/pi/app/interactive/tool-diff +``` diff --git a/app/tests/liii/pi/app/interactive/tool-diff/tool-diff-test.scm b/app/tests/liii/pi/app/interactive/tool-diff/tool-diff-test.scm index 6b2eebc4..ccef2fd0 100644 --- a/app/tests/liii/pi/app/interactive/tool-diff/tool-diff-test.scm +++ b/app/tests/liii/pi/app/interactive/tool-diff/tool-diff-test.scm @@ -1,8 +1,19 @@ ;; %tool-diff -;; (%tool-diff before after [with-text?]) takes UTF-8 strings and returns -;; (diff-text added removed first-changed-line), or #f when the input exceeds -;; a calculation budget. Omitted previews and absent line positions are #f. -;; CRLF is equivalent to LF; a changed final newline changes one logical line. +;; 规范化换行并计算精确的行级差异;末尾换行变化计为一行替换。 +;; +;; 语法 +;; ---- +;; (%tool-diff before after [with-text? [cancelled?]]) +;; +;; 参数 +;; ---- +;; before、after : string?,比较前后的 UTF-8 文本。 +;; with-text? : boolean?,是否生成预览;cancelled? : 可选的零参数取消谓词。 +;; +;; 返回值 +;; ---- +;; (diff-text added removed first-changed-line),无预览或位置时相应字段为 #f; +;; 超出计算预算或取消时返回 #f。非字符串输入抛出 type-error。 (import (scheme base) (liii check) (liii string) @@ -54,4 +65,5 @@ (check (%tool-diff "" (repeat-line "a\n" 1000)) => '(#f 1000 0 1)) (check (%tool-diff "" (repeat-line "\n" 9000)) => #f) (check (%tool-diff (make-string 262145 #\a) "") => #f) + (check-report) From b6bf83d0d9a24216a0d3619044b971363c832204 Mon Sep 17 00:00:00 2001 From: pigmagicfly <2831850183@qq.com> Date: Thu, 26 Nov 2026 15:15:52 +0800 Subject: [PATCH 3/3] =?UTF-8?q?[1345]=20=E7=B2=BE=E7=AE=80=E7=BA=AF?= =?UTF-8?q?=E6=96=87=E6=9C=AC=20Diff=20=E7=9A=84=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/liii/pi/app/interactive/app-diff.scm | 2 +- app/liii/pi/app/interactive/diff-capture.scm | 34 ++++--------------- .../diff-capture/diff-capture-test.scm | 4 +++ 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/app/liii/pi/app/interactive/app-diff.scm b/app/liii/pi/app/interactive/app-diff.scm index c974b76c..e7d792da 100644 --- a/app/liii/pi/app/interactive/app-diff.scm +++ b/app/liii/pi/app/interactive/app-diff.scm @@ -64,7 +64,7 @@ (lambda (entry) (let ((path (%clean-tool-display-text (car entry)))) (container-add! box - (make-status-card (string-append (string-join (string-split path "\n") " ") + (make-status-card (string-append (string-replace path "\n" " ") " " (%tool-diff-counts (cadr entry) (caddr entry)) ) ;string-append diff --git a/app/liii/pi/app/interactive/diff-capture.scm b/app/liii/pi/app/interactive/diff-capture.scm index 4c6be89a..683cc9fc 100644 --- a/app/liii/pi/app/interactive/diff-capture.scm +++ b/app/liii/pi/app/interactive/diff-capture.scm @@ -7,10 +7,9 @@ %diff-capture-jobs %diff-capture-call %diff-capture-call-set! %diff-capture-count %diff-capture-count-set! %diff-capture-misses %diff-capture-misses-set! %diff-tool? %diff-read-snapshot %pi-diff-start - %diff-without-nul? %diff-text-snapshot %diff-cwd %diff-try-send! - %diff-enqueue! %pi-with-turn-diff %diff-written-size? %diff-valid-path? - %diff-read-bounded %diff-snapshot-before %diff-snapshot-after - %diff-observe-tool + %diff-text-snapshot %diff-try-send! %diff-enqueue! %pi-with-turn-diff + %diff-written-size? %diff-valid-path? %diff-read-bounded + %diff-snapshot-before %diff-snapshot-after %diff-observe-tool ) ;export (import (scheme base) (scheme file) @@ -18,6 +17,7 @@ (liii go) (liii os) (liii path stat) + (only (liii string) string-index) (liii pi ai config runtime) (liii pi ai tools result) (liii pi code core tools read) @@ -44,19 +44,10 @@ (or (equal? name "edit") (equal? name "write")) ) ;define - (define (%diff-without-nul? text) - (let ((size (string-length text))) - (let loop - ((i 0)) - (or (= i size) (and (not (char=? (string-ref text i) #\nul)) (loop (+ i 1)))) - ) ;let - ) ;let - ) ;define - (define (%diff-text-snapshot text) (and (string? text) (<= (string-length text) %diff-snapshot-bytes) - (%diff-without-nul? text) + (not (string-index text #\nul)) text ) ;and ) ;define @@ -98,17 +89,6 @@ ) ;catch ) ;define - (define (%diff-cwd opts) - (let loop - ((rest opts)) - (cond ((not (pair? rest)) (getcwd)) - ((not (pair? (cdr rest))) (getcwd)) - ((eq? (car rest) :cwd) (cadr rest)) - (else (loop (cddr rest))) - ) ;cond - ) ;let - ) ;define - ;; Never wait to enqueue optional display data. Closed channels on shutdown ;; and optional snapshot errors must not log file contents or fail the tool. (define (%diff-try-send! ch message) @@ -151,7 +131,7 @@ ) ;define (define (%diff-valid-path? p) - (and (string? p) (<= 1 (string-length p) 4096) (%diff-without-nul? p)) + (and (string? p) (<= 1 (string-length p) 4096) (not (string-index p #\nul))) ) ;define (define (%diff-written-size? absolute text) @@ -169,7 +149,7 @@ (define (%diff-snapshot-before args opts write?) (let ((display-path (pi-arg-ref args "path"))) (and (%diff-valid-path? display-path) - (let* ((resolved (pi-resolve-path display-path (%diff-cwd opts))) + (let* ((resolved (pi-resolve-path display-path (pi-keyword-value :cwd opts (getcwd)))) (absolute (and (<= (string-length resolved) 4096) resolved)) (write-after (and write? (%diff-text-snapshot (pi-arg-ref args "content")))) (before diff --git a/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm b/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm index ba6a3b74..fc111a5c 100644 --- a/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm +++ b/app/tests/liii/pi/app/interactive/diff-capture/diff-capture-test.scm @@ -123,7 +123,11 @@ ) ;check ) ;unless ) ;let + (check (%diff-text-snapshot "") => "") + (check (%diff-text-snapshot "中文") => "中文") + (check (%diff-text-snapshot (string #\nul #\a)) => #f) (check (%diff-text-snapshot (string #\a #\nul #\b)) => #f) + (check (%diff-text-snapshot (string-append "中文" (string #\nul))) => #f) ;; A large snapshot does not change the original result. (check-false (pi-tool-result-is-error (run-tool "large" "write" (write-args (make-string 262145 #\z)))