Skip to content

feat: keep rtk's usage ledger writable inside the workspace (+ /rtk, composer pill) - #3

Closed
sikasjc wants to merge 2 commits into
DeepTrial:mainfrom
sikasjc:main
Closed

sikasjc wants to merge 2 commits into
DeepTrial:mainfrom
sikasjc:main

Conversation

@sikasjc

@sikasjc sikasjc commented Oct 21, 2026 •

Copy link
Copy Markdown

Description

装了这个插件之后,命令输出确实被 rtk 压缩了,但 rtk gain 一直是空的,一条记录都没有。

问题出在账本的位置:rtk 把 history.db 放在 ~/.local/share/rtk/,而 DSH 的 workspace-write 沙箱只允许写会话工作区、/tmp 和平台临时目录。Linux 上就是用 bwrap 把根目录挂成只读,再把工作区单独 bind 成可写,HOME 落在只读那边。rtk 每次往 HOME 记账都会失败,而且它不吭声,所以压缩照常、账本永远空着。界面上也没有地方能直接看到省了多少 token。

这个 PR 把账本放进会话工作区,再补上两个查看入口:聊天里的 /rtk 命令,和输入框下方的一颗统计徽章。

Reproduction

  1. 在 web profile 里装好插件,把 profile 的 cordis.patch.yml 改成 bash-sandbox: disabled: true、bash-rtk: disabled: false。
  2. 让 agent 跑一条白名单里的简单命令,比如 git status,输出会变成 rtk 的紧凑格式。
  3. 在 DSH 里执行 rtk gain。
  4. 也可以去 ~/.local/share/rtk/history.db 看看有没有新记录。

Expected Behavior

rtk gain 能看到当前工作区跑了多少条命令、省了多少 token。界面上最好也能直接看到,不用来回敲命令。

Actual Behavior

在 DSH 里跑 rtk gain 直接报错:

rtk: Failed to initialize tracking database: unable to open database file: Error code 14: Unable to open the database file

也见过 Read-only file system (os error 30)。在普通终端里打开数据库没问题,但里面只有终端跑过的命令;DSH 里被插件包装执行的命令一条都没记上。

Environment

  • Node.js version: v26.12.0
  • dsh-bash-rtk version: 0.1.2
  • rtk version: rtk 1.0.1
  • OS: Ubuntu 26.04.1 LTS (WSL2),kernel 6.18.33.2-microsoft-standard-WSL2;DSH 0.2.1-rc.2,web profile

Additional Context

DSH 沙箱的可写根写死成 [workspaceRoot, /tmp, os.tmpdir()],sandbox-policy 也只有 mode/workspaceRoot 两个配置,没有地方能额外加一个可写目录,所以我选择把账本挪进工作区。

具体改了这些:

  • 执行器在 workspace-write 会话里给命令注入 RTK_DB_PATH=<workspace>/.rtk/history.db。新增两个配置:rtkTracking 默认开,关掉就不注入;rtkDbPath 可以指定绝对路径或相对工作区的路径。外部环境里已经设了 RTK_DB_PATH 的话以它为准;read-only 和 danger-full-access 两种模式保持原样。
  • 新增 /rtk 命令(bash-rtk-command 行),在聊天里直接打印当前工作区的账本。
  • web profile 下多一个 bash-rtk-stats 行,提供只读的 GET /rtk/stats:默认返回 rtk gain --format json 的摘要,缓存 10 秒,加 &detail=1 返回文本报告。前端半侧(client/index.js,通过 dsh.client 声明)在 conversation.composer.dock 上注册一颗 rtk 统计徽章,tooltip 里是节省的数字,点开能看账本明细,没有数据就不显示。弹窗可以用右上角 ×、点背景或 Esc 关闭。

验证过的:tsc(src 和 tests)、43 个 vitest 用例、tsdown 构建、node --check client/index.js、dsh --profile web --help 冷启动都通过;本机起 web 后用 curl 'http://127.0.0.1:3080/rtk/stats?sessionId=<id>' 能拿到 {"ok":true,"summary":{...}}。

注意前端 bundle 是 host 启动时快照的,改完 client/index.js 需要重启 dsh web 并刷新页面才会生效。

几个已知限制:账本按工作区各存一份,不会跨工作区汇总;/rtk 和 /rtk/stats 目前只认 RTK_DB_PATH 和默认路径,还没读执行器配置里的 rtkDbPath。

同类插件的做法可以对照 @viogus/dsh-rtk(给 macOS Seatbelt 打补丁,放开 rtk 状态目录)和 @wingsbutterfly/dsh-rtk(给改写后的命令注入 RTK_DB_PATH)。

rtk records usage in its HOME ledger, which a workspace-write sandbox
cannot write, so rtk gain stayed empty. The executors now inject
RTK_DB_PATH pointing at the session workspace by default
(<workspace>/.rtk/history.db) unless rtkTracking is false, and
rtkDbPath accepts an absolute or workspace-relative override. The new
bash-rtk-command bundle row registers /rtk, which prints the current
workspace ledger from rtk gain.
Add a web-only bash-rtk-stats row exposing the read-only GET
/rtk/stats endpoint (rtk gain JSON summary with a 10s cache, and
&detail=1 for the text report) and a browser half declared through
dsh.client that registers an rtk savings pill in
conversation.composer.dock. The pill polls the route, opens the ledger
detail in a native dialog (close button, backdrop click and Escape all
dismiss it), and hides itself when there is no ledger data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant