agentus 0.15.2 → 0.15.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -57,7 +57,7 @@ background process comes back as a turn of its own), an **ACP session that write
57
57
  cockpit-side failure is diagnosable afterwards), and a **DeepSeek web backend** (search +
58
58
  extract on the model key) — come from the fork's integration build instead. That is a five-line install:
59
59
  [§ Which Hermes to run](docs/installation.md#which-hermes-to-run) — currently the tag
60
- `agentus/v0.22.0/merge-20261026-1237`, on Hermes `v0.22.0`.
60
+ `agentus/v0.22.0/merge-20261028-1850`, on Hermes `v0.22.0`.
61
61
 
62
62
  The first slot you start will use the Hermes home **your own agent already uses**
63
63
  (`~/.hermes`) unless the backend row names another one — see "HERMES_HOME" below.
package/README.zh-CN.md CHANGED
@@ -51,7 +51,7 @@ web bundle、配套 APK —— 然后直接退出,不启动任何东西)。
51
51
  以及 **DeepSeek 的 web 后端**(搜索 + 正文抓取,共用模型 key)—— 来自 fork 的集成构建(tag)。
52
52
  装法就五行:
53
53
  [§ 该跑哪份 Hermes](docs/installation.zh-CN.md#该跑哪份-hermes) —— 当前是 tag
54
- `agentus/v0.22.0/merge-20261026-1237`,基座 Hermes `v0.22.0`。
54
+ `agentus/v0.22.0/merge-20261028-1850`,基座 Hermes `v0.22.0`。
55
55
 
56
56
  你启动的第一个 slot 会使用**你自己的 agent 已经在用的**那个 Hermes home(`~/.hermes`),除非后端那一行
57
57
  指定了另一个 —— 见下面的「HERMES_HOME」。
@@ -60,7 +60,7 @@ ref and gets rebuilt, while a tag never moves — so pin the tag. The `agentus/`
60
60
  ours apart from upstream's own tags (`v0.22.0`, `v0.22.0+canary.*`), and the timestamp in the
61
61
  last segment sorts, so "newest" is a `sort -V`.
62
62
 
63
- **Current integration build: `agentus/v0.22.0/merge-20261026-1237`** — the newest tag on the fork,
63
+ **Current integration build: `agentus/v0.22.0/merge-20261028-1850`** — the newest tag on the fork,
64
64
  built on Hermes `v0.22.0`. This line is for orientation only, not the source of truth: the
65
65
  `TAG=$(…)` step below resolves the newest one on every run, so read it there if this sentence
66
66
  ever looks stale.
@@ -52,7 +52,7 @@ agentus/<hermes 内部版本>/merge-<YYYYMMDD-HHMM>
52
52
  tag。`agentus/` 这个前缀也把它和上游自己的 tag(`v0.22.0`、`v0.22.0+canary.*`)分开;最后一段是时间戳,
53
53
  可排序,所以「最新」就是一次 `sort -V`。
54
54
 
55
- **当前集成构建:`agentus/v0.22.0/merge-20261026-1237`** —— fork 上最新的那个 tag,基座 Hermes
55
+ **当前集成构建:`agentus/v0.22.0/merge-20261028-1850`** —— fork 上最新的那个 tag,基座 Hermes
56
56
  `v0.22.0`。这行只为一眼看个大概,不是权威:下面的 `TAG=$(…)` 每次都会自己解析出最新,觉得这行过期就
57
57
  以它为准。
58
58
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "agentus",
3
- "version": "0.15.2",
4
- "description": "Multi-session web cockpit for ACP-speaking coding agents (Qoder CLI / Hermes). UI has no intelligence \u2014 the agent lives in spawned CLI subprocesses.",
3
+ "version": "0.15.4",
4
+ "description": "Multi-session web cockpit for ACP-speaking coding agents (Qoder CLI / Hermes). UI has no intelligence — the agent lives in spawned CLI subprocesses.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
7
7
  "type": "git",
@@ -92,7 +92,9 @@
92
92
  "tus-mode-sweep": "node scripts/qa/tus-mode-sweep.mjs",
93
93
  "usage-row-sweep": "node scripts/qa/usage-row-sweep.mjs",
94
94
  "call-brief-sweep": "node scripts/qa/call-brief-sweep.mjs",
95
- "reply-on-done": "node scripts/qa/reply-on-done.mjs"
95
+ "reply-on-done": "node scripts/qa/reply-on-done.mjs",
96
+ "turn-usage": "tsx scripts/qa/turn-usage.mts",
97
+ "turn-usage-sweep": "node scripts/qa/turn-usage-sweep.mjs"
96
98
  },
97
99
  "engines": {
98
100
  "node": ">=22.5"
@@ -949,7 +949,29 @@ const agent = () => ({
949
949
  used: s.used,
950
950
  size: picked > 0 ? picked : Number(process.env.MOCK_USAGE_SIZE || 200_000),
951
951
  });
952
- return { stopReason: "end_turn" };
952
+ // The prompt response's `usage`, shaped like the hermes adapter's (issue #70, #71). ACP's
953
+ // semantics are "Total … across all turns", so the counters ACCUMULATE on the session state —
954
+ // a cockpit that forgot to diff would show the whole conversation on every turn, and a
955
+ // two-turn QA sweep is what proves it does not.
956
+ //
957
+ // Issue #71: `inputTokens` is the FULL prompt — hermes fills it from `prompt_tokens`, which
958
+ // its own accounting defines as fresh input + cache read + cache write. The mock therefore
959
+ // adds the cache buckets INTO the input counter, exactly as the real adapter does: a mock that
960
+ // kept them separate would let the old 「input + read + write」 denominator stay green.
961
+ // `cachedWriteTokens` is filled too, because the fork's adapter still omits it and the cockpit
962
+ // must not depend on that omission.
963
+ const outChars = words.join(" ").length + MOCK_MARKDOWN.length;
964
+ s.tokens = s.tokens || {
965
+ totalTokens: 0, inputTokens: 0, outputTokens: 0, thoughtTokens: 0,
966
+ cachedReadTokens: 0, cachedWriteTokens: 0,
967
+ };
968
+ s.tokens.inputTokens += text.length + 200 + 4096 + 128;
969
+ s.tokens.cachedReadTokens += 4096;
970
+ s.tokens.cachedWriteTokens += 128;
971
+ s.tokens.outputTokens += Math.ceil(outChars / 4);
972
+ s.tokens.thoughtTokens += 32;
973
+ s.tokens.totalTokens = s.tokens.inputTokens + s.tokens.outputTokens + s.tokens.thoughtTokens;
974
+ return { stopReason: "end_turn", usage: s.tokens };
953
975
  },
954
976
  });
955
977
 
@@ -49,6 +49,8 @@ import type {
49
49
  SessionModeState,
50
50
  StatusNoteItem,
51
51
  TurnTrace,
52
+ TurnUsage,
53
+ UsageTotals,
52
54
  } from "../../../shared/src/index.js";
53
55
  import { acceptsToolWrite, acceptsWriteFrom, clampExplanation, clampTitle, demoteInProgress, foldPlanUpdate, hasUnfinished, normalizeItems, v2PlanEnabled } from "../plan/plan.js";
54
56
  import { PLAN_GUIDANCE_ACTIVE, PLAN_GUIDANCE_INTRO, PLAN_GUIDANCE_NATIVE } from "../../../shared/src/index.js";
@@ -1564,13 +1566,30 @@ export class SessionManager {
1564
1566
  // The agent has the block (a cancelled turn still received it): NOW the boundary is spent.
1565
1567
  settleVoice();
1566
1568
  this.#finalizePlan(sessionId, terminalState);
1567
- // Turn END is the second and last ordering event of a turn (issue #50): start moved the session,
1568
- // this settles it where the conversation now is. Mid-turn rows deliberately do NOT touch — see
1569
- // #touch.
1569
+ const durationMs = Date.now() - turnStartedAt;
1570
+ // issue #70: the agent's CUMULATIVE token counters arrive on the prompt RESPONSE — a frame
1571
+ // nothing else reads — so the per-turn bill and the cache-hit rate were simply thrown away.
1572
+ // One row per turn, at the tail of the reply it bills: persisted, because a reload must show
1573
+ // the same numbers (the agent will never re-send them), and it sits AFTER the reply by seq.
1574
+ const bill = turnUsageOf(res?.usage, s.info.usage?.totals ?? null, durationMs);
1575
+ if (bill) {
1576
+ s.info.usage = {
1577
+ ...(s.info.usage ?? { used: 0, size: 0, cost: null, at: Date.now() }),
1578
+ totals: bill.totals,
1579
+ at: Date.now(),
1580
+ };
1581
+ }
1570
1582
  this.#touch(s);
1571
- this.#emit({ t: "turn-end", sessionId, stopReason: res?.stopReason, durationMs: Date.now() - turnStartedAt });
1583
+ const usageRow = this.#store.appendMessage({
1584
+ sessionId,
1585
+ kind: "turn-usage",
1586
+ payload: bill ? bill.bill : { available: false, durationMs },
1587
+ createdAt: Date.now(),
1588
+ });
1589
+ this.#emit({ t: "message", message: usageRow });
1590
+ this.#emit({ t: "turn-end", sessionId, stopReason: res?.stopReason, durationMs, ...(bill ? { usage: bill.bill } : {}) });
1572
1591
  // #59: 谁在等这一轮结束(跨会话 send --reply-on-done)
1573
- this.#settleTurnReplies(sessionId, mine, { stopReason: res?.stopReason ?? null, durationMs: Date.now() - turnStartedAt });
1592
+ this.#settleTurnReplies(sessionId, mine, { stopReason: res?.stopReason ?? null, durationMs });
1574
1593
  } catch (err) {
1575
1594
  this.#finalizePlan(sessionId, "failed");
1576
1595
  this.#touch(s);
@@ -2654,6 +2673,10 @@ export class SessionManager {
2654
2673
  size: Number.isFinite(size) ? size : 0,
2655
2674
  cost: uu.cost && typeof uu.cost.amount === "number" ? uu.cost.amount : null,
2656
2675
  at: Date.now(),
2676
+ // issue #70: this frame carries NO token breakdown, so the cumulative baseline that the
2677
+ // per-turn bills are subtracted from must survive it — replacing the object wholesale is
2678
+ // what would make the next bill report the whole session again.
2679
+ ...(live.info.usage?.totals ? { totals: live.info.usage.totals } : {}),
2657
2680
  };
2658
2681
  this.#updateSession(live);
2659
2682
  this.#emit({ t: "usage", sessionId: live.info.id, usage: live.info.usage });
@@ -3520,6 +3543,82 @@ function normConfigOptions(raw: unknown): ConfigOptionView[] {
3520
3543
  });
3521
3544
  }
3522
3545
 
3546
+ /** One turn's token bill, from the agent's CUMULATIVE counters (issue #70).
3547
+ *
3548
+ * ACP documents `PromptResponse.usage` as "Total … across all turns", so a turn's number is a
3549
+ * subtraction against the baseline kept beside the context gauge (`UsageView.totals`). Three
3550
+ * deliberate rules:
3551
+ * · a bucket the agent did not report stays ABSENT — a Qoder session must read 「未上报」, never a
3552
+ * made-up zero;
3553
+ * · a total that SHRANK means the agent's counters reset (a resumed process): the raw value is
3554
+ * then this turn's own number and the baseline resets, instead of emitting a negative bill;
3555
+ * · a turn that reported nothing at all returns null (the row still says 「未上报」).
3556
+ * The FIRST bill of a session that predates this baseline is therefore cumulative — one turn of
3557
+ * honest over-reporting beats inventing a difference. */
3558
+ export function turnUsageOf(
3559
+ raw: unknown, base: UsageTotals | null | undefined, durationMs: number,
3560
+ ): { bill: TurnUsage; totals: UsageTotals } | null {
3561
+ const u = (raw && typeof raw === "object" ? raw : null) as Record<string, unknown> | null;
3562
+ if (!u) return null;
3563
+ const num = (v: unknown): number | null =>
3564
+ (typeof v === "number" && Number.isFinite(v) ? Math.max(0, Math.round(v)) : null);
3565
+ const total = num(u.totalTokens);
3566
+ const input = num(u.inputTokens);
3567
+ const output = num(u.outputTokens);
3568
+ const thought = num(u.thoughtTokens);
3569
+ const cacheRead = num(u.cachedReadTokens);
3570
+ const cacheWrite = num(u.cachedWriteTokens);
3571
+ if ([total, input, output, thought, cacheRead, cacheWrite].every((x) => x === null)) return null;
3572
+ const cur: UsageTotals = {
3573
+ total: total ?? (input ?? 0) + (output ?? 0),
3574
+ input: input ?? 0,
3575
+ output: output ?? 0,
3576
+ thought: thought ?? 0,
3577
+ // issue #71: never `?? 0` here — the baseline is also what a persisted session reloads, and a
3578
+ // washed-in 0 is indistinguishable from a measured zero on the next diff.
3579
+ cacheRead,
3580
+ cacheWrite,
3581
+ };
3582
+ const delta = (now: number, before: number | undefined): number =>
3583
+ (before === undefined ? now : now >= before ? now - before : now);
3584
+ // Issue #71: a bucket the agent never reports stays NULL through the bill. 「写 0」 reads as
3585
+ // "this turn's cache writes were measured at zero", and cache writes are the billed kind
3586
+ // (Anthropic 1.25×). Omission is the honest shape; see tokens.ts. A null BASELINE means the
3587
+ // first reported value is the turn's own — undefined and null never had a prior count.
3588
+ const deltaOpt = (now: number | null, before: number | null | undefined): number | null =>
3589
+ (now === null ? null : delta(now, before ?? undefined));
3590
+ // Computed up front: the hit rate below divides by THIS turn's prompt delta, not the raw one.
3591
+ const billInput = delta(cur.input, base?.input);
3592
+ const billCacheRead = deltaOpt(cur.cacheRead, base?.cacheRead);
3593
+ const bill: TurnUsage = {
3594
+ total: delta(cur.total, base?.total),
3595
+ input: billInput,
3596
+ output: delta(cur.output, base?.output),
3597
+ thought: delta(cur.thought, base?.thought),
3598
+ cacheRead: billCacheRead,
3599
+ cacheWrite: deltaOpt(cur.cacheWrite, base?.cacheWrite),
3600
+ durationMs,
3601
+ // Issue #71 — the denominator is this turn's prompt total, i.e. the billed `input` DELTA.
3602
+ // ACP's Usage.inputTokens is the FULL prompt (hermes' adapter fills it with `prompt_tokens`,
3603
+ // which by definition = fresh input + cache read + cache write; see agent/usage_pricing.py in
3604
+ // the fork), so the cache buckets are a SUBSET of input, not peers beside it. Adding them
3605
+ // back counted every cached token twice and pinned the reading at cacheRead/(input+cacheRead)
3606
+ // ≈ 50% for any cache-heavy session — the exact defect #71 measured (real hits 91–100%, page
3607
+ // 50%). webui keeps one invariant across both of its sources: the denominator is a number
3608
+ // that already IS the full prompt (api/models.py passes input_tokens alone); its additive
3609
+ // form exists only for a rollup that stored uncached input separately. We read the first
3610
+ // source, so no addition. `min(100, …)` stays as a clamp against a skewed diff, never as the
3611
+ // thing that makes the ratio believable: with correct semantics normal data cannot exceed 100.
3612
+ cacheHitPercent: cacheRead === null && cacheWrite === null
3613
+ ? null
3614
+ : (billInput > 0 ? Math.min(100, Math.round(((billCacheRead ?? 0) / billInput) * 100)) : null),
3615
+ };
3616
+ // Average speed: this turn's OWN output over wall time (tool calls included, so a long tool
3617
+ // turn honestly reads slow rather than being dressed up as model speed).
3618
+ bill.tps = bill.output > 0 && durationMs > 0 ? Math.round((bill.output / (durationMs / 1000)) * 10) / 10 : null;
3619
+ return { bill, totals: cur };
3620
+ }
3621
+
3523
3622
  function errMessage(e: unknown): string {
3524
3623
  if (e && typeof e === "object" && "message" in e) return String((e as { message: unknown }).message);
3525
3624
  return String(e);
@@ -132,6 +132,46 @@ export interface UsageView {
132
132
  size: number;
133
133
  cost?: number | null;
134
134
  at: number;
135
+ /** SESSION-CUMULATIVE token counters from `PromptResponse.usage`, as the agent reported them
136
+ * (ACP's own semantics: "Total ... across all turns"). Kept beside the context gauge because
137
+ * that is the only place a reload can learn them again — `usage_update` carries no token
138
+ * breakdown, and the per-turn numbers are differences of these. Absent = the agent never
139
+ * reported any (Qoder), which reads as "not available", NEVER as measured zero. */
140
+ totals?: UsageTotals | null;
141
+ }
142
+
143
+ /** Cumulative token buckets a turn's usage is measured against (ACP `Usage`).
144
+ * `cacheRead`/`cacheWrite` are null = the agent never reports that bucket (issue #71); a
145
+ * baseline of null diffs the first REPORTED value as the turn's own, same as undefined. */
146
+ export interface UsageTotals {
147
+ total: number;
148
+ input: number;
149
+ output: number;
150
+ thought: number;
151
+ cacheRead: number | null;
152
+ cacheWrite: number | null;
153
+ }
154
+
155
+ /** One turn's token bill (issue #70) — the DELTA of the agent's cumulative counters.
156
+ *
157
+ * `cacheHitPercent` is cacheRead / input, where ACP's `inputTokens` is ALREADY the full prompt
158
+ * (fresh + cache read + cache write — issue #71; do not add the buckets back, it counts cached
159
+ * tokens twice and pins the reading near 50%). Null when the agent reports no cache buckets at
160
+ * all — a Qoder session must read as unavailable, not as a 0% hit rate. `tps` is the turn AVERAGE
161
+ * (output tokens over wall time, tool calls included); it is deliberately labelled as an average
162
+ * rather than sold as instantaneous speed. */
163
+ export interface TurnUsage {
164
+ input: number;
165
+ output: number;
166
+ thought: number;
167
+ /** NULL = the agent never reports this bucket (issue #71). A reported 0 is a real 0 — the
168
+ * two must not collapse into one number, or 「写 0」 lies about cache writes (1.25× billed). */
169
+ cacheRead: number | null;
170
+ cacheWrite: number | null;
171
+ total: number;
172
+ durationMs?: number;
173
+ cacheHitPercent?: number | null;
174
+ tps?: number | null;
135
175
  }
136
176
 
137
177
  export interface SessionModeState {
@@ -228,7 +268,10 @@ export type ServerEvent =
228
268
  /** Wall time from the moment the turn was announced to the moment the agent answered (ms).
229
269
  * Exists so a notification can say "用时 1m12s" instead of a protocol token like `end_turn`,
230
270
  * which is a fact about the wire, not about the operator's wait (absent when unknowable). */
231
- durationMs?: number }
271
+ durationMs?: number;
272
+ /** What this turn cost in tokens (issue #70), subtracted from the agent's cumulative
273
+ * counters. Absent when the agent reports none — the page then says 「未上报」, never 0. */
274
+ usage?: TurnUsage | null }
232
275
  /** per-turn trace: what model/effort/mode this turn is actually running with
233
276
  * (AionUi F-DISPLAY-11 lineage — "why did it behave differently?" ) */
234
277
  /** `pid` is the id of the prompt that started this turn, copied from the client's command (issue
@@ -491,8 +534,11 @@ export interface StoredMessage {
491
534
  * `approval` = an approval request as a transcript row (it asks a question, then carries the
492
535
  * decision), and `project-skills` = the agent reporting that this repo ships skills it is NOT
493
536
  * loading. Both are DUrable on purpose: a modal that vanishes leaves no record of what was
494
- * asked or what was answered. */
495
- kind: "user" | "agent" | "thought" | "tool" | "plan" | "meta" | "note" | "approval" | "project-skills";
537
+ * asked or what was answered. `turn-usage` = one turn's token bill (issue #70): it is written at
538
+ * turn-end, so it sits at the TAIL of the reply it bills, and it is persisted because a reload
539
+ * must show the same numbers instead of losing the one part of the turn the agent will never
540
+ * re-send. */
541
+ kind: "user" | "agent" | "thought" | "tool" | "plan" | "meta" | "note" | "approval" | "project-skills" | "turn-usage";
496
542
  payload: unknown; // raw ACP content/update fragment (kept lossless)
497
543
  toolCallId?: string; // set for kind=tool; upsert key
498
544
  createdAt: number;