@namzu/sdk 64.3.0 → 64.4.0

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.
Files changed (87) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/advisory/executor.d.ts +2 -1
  3. package/dist/advisory/executor.d.ts.map +1 -1
  4. package/dist/advisory/executor.js +8 -2
  5. package/dist/advisory/executor.js.map +1 -1
  6. package/dist/compaction/context-window.d.ts.map +1 -1
  7. package/dist/compaction/context-window.js +9 -2
  8. package/dist/compaction/context-window.js.map +1 -1
  9. package/dist/eval/judge.d.ts.map +1 -1
  10. package/dist/eval/judge.js +6 -2
  11. package/dist/eval/judge.js.map +1 -1
  12. package/dist/manager/agent/lifecycle.d.ts +1 -0
  13. package/dist/manager/agent/lifecycle.d.ts.map +1 -1
  14. package/dist/manager/agent/lifecycle.js +20 -6
  15. package/dist/manager/agent/lifecycle.js.map +1 -1
  16. package/dist/manager/session/turn-recorder.d.ts.map +1 -1
  17. package/dist/manager/session/turn-recorder.js +11 -2
  18. package/dist/manager/session/turn-recorder.js.map +1 -1
  19. package/dist/prompt/coding-agent-doctrine.d.ts +1 -1
  20. package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
  21. package/dist/prompt/coding-agent-doctrine.js +7 -1
  22. package/dist/prompt/coding-agent-doctrine.js.map +1 -1
  23. package/dist/provider/instrumentation.js +1 -1
  24. package/dist/provider/instrumentation.js.map +1 -1
  25. package/dist/provider/side-request-reasoning.d.ts +32 -0
  26. package/dist/provider/side-request-reasoning.d.ts.map +1 -0
  27. package/dist/provider/side-request-reasoning.js +97 -0
  28. package/dist/provider/side-request-reasoning.js.map +1 -0
  29. package/dist/public-runtime.d.ts +3 -1
  30. package/dist/public-runtime.d.ts.map +1 -1
  31. package/dist/public-runtime.js +2 -1
  32. package/dist/public-runtime.js.map +1 -1
  33. package/dist/runtime/query/auto-review/classifier.d.ts.map +1 -1
  34. package/dist/runtime/query/auto-review/classifier.js +8 -2
  35. package/dist/runtime/query/auto-review/classifier.js.map +1 -1
  36. package/dist/runtime/query/callback-inference.d.ts.map +1 -1
  37. package/dist/runtime/query/callback-inference.js +14 -3
  38. package/dist/runtime/query/callback-inference.js.map +1 -1
  39. package/dist/runtime/query/iteration/index.d.ts +8 -0
  40. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  41. package/dist/runtime/query/iteration/index.js +35 -4
  42. package/dist/runtime/query/iteration/index.js.map +1 -1
  43. package/dist/runtime/query/prepare-turn.d.ts.map +1 -1
  44. package/dist/runtime/query/prepare-turn.js +3 -0
  45. package/dist/runtime/query/prepare-turn.js.map +1 -1
  46. package/dist/runtime/query/repeat-call.js +2 -2
  47. package/dist/runtime/query/repeat-call.js.map +1 -1
  48. package/dist/runtime/query/turn/stall-monitor.d.ts +26 -2
  49. package/dist/runtime/query/turn/stall-monitor.d.ts.map +1 -1
  50. package/dist/runtime/query/turn/stall-monitor.js +36 -4
  51. package/dist/runtime/query/turn/stall-monitor.js.map +1 -1
  52. package/dist/turn/evidence-query.d.ts.map +1 -1
  53. package/dist/turn/evidence-query.js +7 -1
  54. package/dist/turn/evidence-query.js.map +1 -1
  55. package/dist/types/common/index.d.ts +14 -0
  56. package/dist/types/common/index.d.ts.map +1 -1
  57. package/dist/types/common/index.js +7 -0
  58. package/dist/types/common/index.js.map +1 -1
  59. package/dist/types/provider/config.d.ts +6 -0
  60. package/dist/types/provider/config.d.ts.map +1 -1
  61. package/dist/types/session/prepare-step.d.ts +7 -0
  62. package/dist/types/session/prepare-step.d.ts.map +1 -1
  63. package/dist/utils/cost.d.ts +14 -0
  64. package/dist/utils/cost.d.ts.map +1 -1
  65. package/dist/utils/cost.js +20 -0
  66. package/dist/utils/cost.js.map +1 -1
  67. package/package.json +1 -1
  68. package/src/advisory/executor.ts +13 -2
  69. package/src/compaction/context-window.ts +9 -2
  70. package/src/eval/judge.ts +16 -9
  71. package/src/manager/agent/lifecycle.ts +19 -6
  72. package/src/manager/session/turn-recorder.ts +12 -1
  73. package/src/prompt/coding-agent-doctrine.ts +7 -1
  74. package/src/provider/instrumentation.ts +1 -1
  75. package/src/provider/side-request-reasoning.ts +121 -0
  76. package/src/public-runtime.ts +7 -0
  77. package/src/runtime/query/auto-review/classifier.ts +17 -9
  78. package/src/runtime/query/callback-inference.ts +13 -3
  79. package/src/runtime/query/iteration/index.ts +38 -2
  80. package/src/runtime/query/prepare-turn.ts +2 -0
  81. package/src/runtime/query/repeat-call.ts +2 -2
  82. package/src/runtime/query/turn/stall-monitor.ts +48 -4
  83. package/src/turn/evidence-query.ts +8 -1
  84. package/src/types/common/index.ts +22 -0
  85. package/src/types/provider/config.ts +6 -0
  86. package/src/types/session/prepare-step.ts +7 -0
  87. package/src/utils/cost.ts +24 -0
@@ -12,7 +12,7 @@ import type { ReasoningEffort } from '../../../types/provider/index.js'
12
12
 
13
13
  /** What the model is told when a step spent its whole reasoning allowance before acting. */
14
14
  export const REASONING_CAPPED_MESSAGE =
15
- 'Your thinking for that step reached its limit before you acted, so it was cut short. Act now on what you already know: make the first change, or run the one command that tells you what you still need. Refine as you go; do not plan the whole task again.'
15
+ 'Your thinking for that step reached its limit before you acted, so it was cut short. Act now on what you already know: make the first change, or, if you were asked a question, answer with what you have and say what is missing. Refine as you go; do not plan the whole task again.'
16
16
 
17
17
  /** What the model is told when a reply held thinking but no answer and no action. */
18
18
  export const REASONING_ONLY_MESSAGE =
@@ -20,7 +20,14 @@ export const REASONING_ONLY_MESSAGE =
20
20
 
21
21
  /** What the model is told when several steps passed with nothing changed. */
22
22
  export const STALL_NUDGE_MESSAGE =
23
- 'Several steps have passed and nothing has changed yet. Begin changing things now: make the first edit you are sure of, or run the one check that settles what you are unsure of. Reading more or planning again will not move the task.'
23
+ 'Several steps have passed and nothing has changed yet. Begin changing things now: make the first edit you are sure of. If you were asked a question, answer it now with what you have and say what is missing. Reading more or planning again will not move the task.'
24
+
25
+ /**
26
+ * What the model is told when it was asked a question and several steps have
27
+ * only looked. It names the way out as the answer, never as another command.
28
+ */
29
+ export const ANSWER_NOW_MESSAGE =
30
+ 'You were asked a question and the last steps only looked around. Answer it now, in the language the person used and within what they asked for, using what you have already seen. If something is still missing, say what it is instead of running another command. Do not open with an apology.'
24
31
 
25
32
  /**
26
33
  * Said again, on one request only, when a step must act but the provider
@@ -97,6 +104,14 @@ export interface StallConfig {
97
104
  readonly nudgeAfterReasoningChars?: number
98
105
  /** Further steps without a change after the nudge that make the next step an action step. Default 5. */
99
106
  readonly forceAfterSteps?: number
107
+ /**
108
+ * Steps that only looked, in a turn the person opened with a question,
109
+ * after which the model is told to answer. Default 6: half of the general
110
+ * threshold, because a question is usually settled by two or three looks,
111
+ * and six looks that settled nothing are a model circling rather than
112
+ * gathering. It is a nudge only; a question never forces a tool call.
113
+ */
114
+ readonly answerAfterSteps?: number
100
115
  }
101
116
 
102
117
  /** What the monitor wants done after a step. */
@@ -109,12 +124,32 @@ export type StallDirective =
109
124
  export interface StallObservation {
110
125
  readonly reasoningChars: number
111
126
  readonly changedState: boolean
127
+ /** The person's latest message asked a question, so the way out of a stall is an answer. */
128
+ readonly question?: boolean
129
+ }
130
+
131
+ /** The longest message still read as a question; a longer one is a brief. */
132
+ const QUESTION_MAX_CHARS = 400
133
+
134
+ /**
135
+ * Whether a person's message is mainly a question: short, and holding a
136
+ * question mark that ends a sentence. A mark inside a web address or a
137
+ * query string does not end one, and a long message is a task description
138
+ * that happens to ask something. A question without a mark is held to the
139
+ * general threshold.
140
+ */
141
+ export function asksQuestion(text: string | undefined): boolean {
142
+ if (text === undefined) return false
143
+ const trimmed = text.trim()
144
+ if (trimmed.length === 0 || trimmed.length > QUESTION_MAX_CHARS) return false
145
+ return /[?\uFF1F\u061F](?=$|[\s"'\u201D\u2019)\]])/.test(trimmed)
112
146
  }
113
147
 
114
148
  export class StallMonitor {
115
149
  private readonly nudgeAfterSteps: number
116
150
  private readonly nudgeAfterChars: number
117
151
  private readonly forceAfterSteps: number
152
+ private readonly answerAfterSteps: number
118
153
  private idleSteps = 0
119
154
  private idleChars = 0
120
155
  private nudged = false
@@ -124,6 +159,7 @@ export class StallMonitor {
124
159
  this.nudgeAfterSteps = config?.nudgeAfterSteps ?? 12
125
160
  this.nudgeAfterChars = config?.nudgeAfterReasoningChars ?? 80_000
126
161
  this.forceAfterSteps = config?.forceAfterSteps ?? 5
162
+ this.answerAfterSteps = config?.answerAfterSteps ?? 6
127
163
  }
128
164
 
129
165
  observe(step: StallObservation): StallDirective {
@@ -133,18 +169,26 @@ export class StallMonitor {
133
169
  }
134
170
  this.idleSteps += 1
135
171
  this.idleChars += step.reasoningChars
172
+ const question = step.question === true
136
173
  if (!this.nudged) {
174
+ if (question && this.idleSteps >= this.answerAfterSteps) {
175
+ this.nudged = true
176
+ this.stepsSinceNudge = 0
177
+ return { kind: 'nudge', text: ANSWER_NOW_MESSAGE }
178
+ }
137
179
  if (this.idleSteps >= this.nudgeAfterSteps || this.idleChars >= this.nudgeAfterChars) {
138
180
  this.nudged = true
139
181
  this.stepsSinceNudge = 0
140
- return { kind: 'nudge', text: STALL_NUDGE_MESSAGE }
182
+ return { kind: 'nudge', text: question ? ANSWER_NOW_MESSAGE : STALL_NUDGE_MESSAGE }
141
183
  }
142
184
  return { kind: 'none' }
143
185
  }
144
186
  this.stepsSinceNudge += 1
145
187
  if (this.stepsSinceNudge >= this.forceAfterSteps) {
146
188
  this.reset()
147
- return { kind: 'force' }
189
+ // A question is answered, not acted on: say it again instead of
190
+ // forcing a tool call, which would only buy another command.
191
+ return question ? { kind: 'nudge', text: ANSWER_NOW_MESSAGE } : { kind: 'force' }
148
192
  }
149
193
  return { kind: 'none' }
150
194
  }
@@ -344,7 +344,14 @@ export function createEvidenceQueryResolver() {
344
344
  maxTokens: 512,
345
345
  signal: context.signal,
346
346
  })
347
- .then(({ text }) => validateEvidenceQueryResolution(text, current, history))
347
+ .then(({ text, finishReason }) =>
348
+ // A planner that came back empty, or ran out of tokens before it
349
+ // finished, said nothing about the question. That is not a failed
350
+ // plan to report to the model: the lookup adds nothing this turn.
351
+ !text.trim() || finishReason === 'length'
352
+ ? null
353
+ : validateEvidenceQueryResolution(text, current, history),
354
+ )
348
355
  cached = { turnId: context.turnId, query, operator: identity, plan }
349
356
  return plan
350
357
  }
@@ -41,6 +41,21 @@ export interface TokenUsage {
41
41
  */
42
42
  reasoningTokens?: number
43
43
 
44
+ /**
45
+ * What the provider says this call cost, in US dollars, when it says.
46
+ *
47
+ * A BILLED figure the vendor reported, as opposed to one the kernel worked
48
+ * out from a rate card: it already reflects the route that served the call,
49
+ * the cache discount and any tier. Where it is present the kernel records it
50
+ * as the call's cost and does not price the tokens again (see
51
+ * {@link accumulateReportedCost}); where it is absent the rate card is the
52
+ * fallback. Summed across calls like {@link TokenUsage.webSearchRequests},
53
+ * since it is money spent; a sum over calls that did not all report is a
54
+ * floor. Absent means the provider did not say, not that the call was free;
55
+ * a reported `0` is a real zero.
56
+ */
57
+ reportedCostUsd?: number
58
+
44
59
  /**
45
60
  * How long the provider keeps the prompt cache this call used, in
46
61
  * milliseconds, as the call itself reported or requested it.
@@ -126,6 +141,9 @@ export function accumulateTokenUsage(current: TokenUsage, addition: TokenUsage):
126
141
  ...(current.webSearchRequests !== undefined || addition.webSearchRequests !== undefined
127
142
  ? { webSearchRequests: (current.webSearchRequests ?? 0) + (addition.webSearchRequests ?? 0) }
128
143
  : {}),
144
+ ...(current.reportedCostUsd !== undefined || addition.reportedCostUsd !== undefined
145
+ ? { reportedCostUsd: (current.reportedCostUsd ?? 0) + (addition.reportedCostUsd ?? 0) }
146
+ : {}),
129
147
  // `fullPromptTokens`, `cacheTtlMs`, `cacheWriteTtlTokens`, `speed`, `serviceTier`,
130
148
  // `inferenceGeo` and `cacheMiss` are per-call facts and are deliberately
131
149
  // not carried into a sum.
@@ -179,6 +197,10 @@ export function mergeTokenUsage(current: TokenUsage, next: TokenUsage): TokenUsa
179
197
  ...(next.webSearchRequests !== undefined || current.webSearchRequests !== undefined
180
198
  ? { webSearchRequests: Math.max(current.webSearchRequests ?? 0, next.webSearchRequests ?? 0) }
181
199
  : {}),
200
+ // Cumulative within one call, like the token counts.
201
+ ...(next.reportedCostUsd !== undefined || current.reportedCostUsd !== undefined
202
+ ? { reportedCostUsd: Math.max(current.reportedCostUsd ?? 0, next.reportedCostUsd ?? 0) }
203
+ : {}),
182
204
  ...(next.serviceTier !== undefined || current.serviceTier !== undefined
183
205
  ? { serviceTier: next.serviceTier ?? current.serviceTier }
184
206
  : {}),
@@ -164,6 +164,12 @@ export interface ProviderCapabilities {
164
164
  /** Whether the driver maps document blocks returned by tools onto its result wire. */
165
165
  supportsToolResultDocuments?: boolean
166
166
  maxOutputTokens?: number
167
+ /**
168
+ * Whether every call's usage carries the cost the provider billed
169
+ * (`TokenUsage.reportedCostUsd`). A cost limit is then measurable for a
170
+ * model no rate card names. Absent means the driver reports no cost.
171
+ */
172
+ reportsRequestCost?: boolean
167
173
  /**
168
174
  * Prompt-cache lifetimes the driver can be asked for through
169
175
  * `cacheControl.ttl`. Absent means the driver offers none (it has no cache,
@@ -33,6 +33,13 @@ export interface PreparationTextResult {
33
33
  readonly text: string
34
34
  readonly usage: TokenUsage
35
35
  readonly servedBy: StepProvenance
36
+ /**
37
+ * Why the provider stopped. `'length'` means the answer allowance ran out,
38
+ * which on a reasoning model can leave `text` empty or cut off mid-way; a
39
+ * caller that needs complete text treats it as no answer. Absent when the
40
+ * provider reported none.
41
+ */
42
+ readonly finishReason?: 'stop' | 'tool_calls' | 'length' | 'content_filter'
36
43
  }
37
44
 
38
45
  /**
package/src/utils/cost.ts CHANGED
@@ -241,6 +241,30 @@ export function accumulateUnpricedCost(current: CostInfo, additionalUsage: Token
241
241
  }
242
242
  }
243
243
 
244
+ /**
245
+ * Record a call whose cost the provider reported.
246
+ *
247
+ * The vendor's figure is what the account was charged, so it wins over a rate
248
+ * card: a card is an estimate of one model at one list price, while the report
249
+ * already includes the route that served the call, the cache discount and any
250
+ * tier. The rate fields go, as for unpriced tokens: no single card describes
251
+ * a total that holds a reported figure.
252
+ */
253
+ export function accumulateReportedCost(current: CostInfo, reportedCostUsd: number): CostInfo {
254
+ return {
255
+ totalCost: current.totalCost + reportedCostUsd,
256
+ cacheDiscount: current.cacheDiscount,
257
+ unpricedTokens: current.unpricedTokens,
258
+ }
259
+ }
260
+
261
+ /** Whether a usage frame carries a usable vendor-reported cost. */
262
+ export function hasReportedCost(
263
+ usage: TokenUsage,
264
+ ): usage is TokenUsage & { reportedCostUsd: number } {
265
+ return Number.isFinite(usage.reportedCostUsd) && (usage.reportedCostUsd as number) >= 0
266
+ }
267
+
244
268
  export function formatCost(usd: number): string {
245
269
  if (usd === 0) return '$0.00'
246
270
  if (usd < 0.01) return `$${usd.toFixed(4)}`