@linxin666/dsh-liangshen 0.4.14 → 0.4.16

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.
@@ -13,12 +13,13 @@
13
13
  * renders the current facts as one of its fields, republished only on change.
14
14
  *
15
15
  * The register itself is a list of single-line facts, newest last, rebuilt by
16
- * replaying `tool/call` events for this tool from the session log — exactly
17
- * the way the paging activation state replays. A fact is a string the model
18
- * writes; registering a new fact APPENDS, and registering one already present
19
- * is a no-op report. REVOCATION is append-only too: a `revoke` entry removes
20
- * every earlier fact carrying the same tag, so the fold stays deterministic
21
- * after resume and compaction without any process memory.
16
+ * replaying this tool's calls out of the session log — exactly the way the
17
+ * paging activation state replays, and from both wire shapes a session records
18
+ * them in ({@link factRegisterCall}). A fact is a string the model writes;
19
+ * registering a new fact APPENDS, and registering one already present is a
20
+ * no-op report. REVOCATION is append-only too: a `revoke` entry removes every
21
+ * earlier fact carrying the same tag, so the fold stays deterministic after
22
+ * resume and compaction without any process memory.
22
23
  *
23
24
  * The tool is deliberately small: it validates and reports, it does not edit
24
25
  * the context itself. The projection the model sees next step is the fold of
@@ -53,17 +54,52 @@ export const MAX_FACT_CHARS = 160
53
54
  /** Cap the register so the ledger cannot itself flood the local window. */
54
55
  export const MAX_FACTS = 12
55
56
 
57
+ /**
58
+ * The call one event contributes to the fold, or undefined when the event is
59
+ * not a `fact_register` call that took effect.
60
+ *
61
+ * The session records the call in one of two shapes, and which one depends on
62
+ * the presentation the session runs. `native` records a top-level `tool/call`
63
+ * whose `arguments` is the JSON string the model emitted. `ptc` — the shipped
64
+ * default — collapses the wire to `run_code` and reaches every other tool from
65
+ * inside a program, which the runtime records as `tool/ptc-dispatch`: its
66
+ * `arguments` is already an object, and a dispatch that failed carries its own
67
+ * `isError` on the dispatch record (`event.data.isError`), not on the event
68
+ * envelope. Reading only `tool/call` therefore folded an empty register in
69
+ * every factory-default session — pin and revoke both reported success while no
70
+ * `key facts` field ever rendered — which is the same gap `paging.mjs` replays
71
+ * both shapes to avoid.
72
+ *
73
+ * A dispatch that reported an error never took effect, so it contributes
74
+ * nothing: a failed pin must not pin, and a failed revoke must not drop a fact
75
+ * that still stands. `tool/ptc-dispatch-start` only announces a dispatch that
76
+ * has not settled yet, so the event-type check below leaves it out. The
77
+ * `native` half keeps its existing semantics untouched: a `tool/call` is read
78
+ * whatever its `tool/result` later reported, exactly as before this change.
79
+ */
80
+ function factRegisterCall(event) {
81
+ if (event?.type === 'tool/ptc-dispatch') {
82
+ const dispatch = event.data
83
+ if (dispatch?.name !== FACT_LEDGER_TOOL || dispatch?.isError === true) return undefined
84
+ return dispatch
85
+ }
86
+ if (event?.type !== 'tool/call' || event.data?.name !== FACT_LEDGER_TOOL) return undefined
87
+ return event.data
88
+ }
89
+
56
90
  /**
57
91
  * Fold the current fact register from the event stream: every successful
58
- * `fact_register` call appends its fact (or revokes by tag). Facts carry no
59
- * ids — revocation matches on the optional `tag`, and an untagged fact can
60
- * only be revoked by revoking its exact text as the tag of last resort.
92
+ * `fact_register` call appends its fact (or revokes by tag), whichever wire
93
+ * shape recorded it. Facts carry no ids — revocation matches on the optional
94
+ * `tag`, and an untagged fact can only be revoked by revoking its exact text as
95
+ * the tag of last resort.
61
96
  */
62
97
  export function foldFactLedger(events) {
63
98
  const facts = []
64
99
  for (const event of Array.isArray(events) ? events : []) {
65
- if (event?.type !== 'tool/call' || event.data?.name !== FACT_LEDGER_TOOL) continue
66
- let args = event.data?.arguments
100
+ const data = factRegisterCall(event)
101
+ if (data === undefined) continue
102
+ let args = data.arguments
67
103
  if (typeof args === 'string') {
68
104
  try { args = JSON.parse(args) } catch { args = undefined }
69
105
  }