@theokit/sdk 4.17.0 → 4.18.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.
package/dist/eval.cjs CHANGED
@@ -5947,9 +5947,7 @@ async function hydrateSession(agentId, loc) {
5947
5947
  hydratedKeys.add(key);
5948
5948
  const persisted = await readSessionMessages(loc.store, agentId);
5949
5949
  if (persisted.length === 0) return;
5950
- if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
5951
- sessions.set(agentId, persisted);
5952
- }
5950
+ sessions.set(agentId, persisted);
5953
5951
  }
5954
5952
  async function flushSessionWrites() {
5955
5953
  while (pendingWrites.size > 0) {
@@ -6257,10 +6255,12 @@ __export(run_until_exports, {
6257
6255
  async function* runUntilImpl(agent, goal, options, deps) {
6258
6256
  const maxTurns = options?.maxTurns ?? 20;
6259
6257
  const maxFails = options?.maxConsecutiveJudgeFailures ?? 3;
6258
+ const tokenBudget = options?.tokenBudget;
6260
6259
  const signal = options?.signal;
6261
6260
  const isAborted = () => signal !== void 0 && signal.aborted;
6262
6261
  let turn = 0;
6263
6262
  let consecutiveFailures = 0;
6263
+ let tokensUsed = 0;
6264
6264
  let lastResponse = "";
6265
6265
  if (isAborted()) {
6266
6266
  yield {
@@ -6268,13 +6268,13 @@ async function* runUntilImpl(agent, goal, options, deps) {
6268
6268
  status: "paused",
6269
6269
  reason: "aborted via AbortSignal before first turn"
6270
6270
  };
6271
- return { status: "paused", turnsUsed: 0, finalResponse: void 0 };
6271
+ return { status: "paused", turnsUsed: 0, tokensUsed, finalResponse: void 0 };
6272
6272
  }
6273
6273
  yield { type: "status_change", status: "active", reason: "Goal started" };
6274
6274
  while (turn < maxTurns) {
6275
6275
  if (isAborted()) {
6276
6276
  yield { type: "status_change", status: "paused", reason: "aborted via AbortSignal" };
6277
- return { status: "paused", turnsUsed: turn, finalResponse: lastResponse || void 0 };
6277
+ return { status: "paused", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
6278
6278
  }
6279
6279
  turn += 1;
6280
6280
  yield { type: "turn_start", turn, goal };
@@ -6282,6 +6282,8 @@ async function* runUntilImpl(agent, goal, options, deps) {
6282
6282
  const run = await agent.send(continuationPrompt);
6283
6283
  const result = await run.wait();
6284
6284
  lastResponse = result.result ?? "";
6285
+ const turnTokens = result.usage?.totalTokens;
6286
+ if (typeof turnTokens === "number") tokensUsed += turnTokens;
6285
6287
  yield { type: "agent_response", turn, content: lastResponse };
6286
6288
  const judgeOpts = {};
6287
6289
  if (options?.judgeModel !== void 0) judgeOpts.judgeModel = options.judgeModel;
@@ -6307,6 +6309,7 @@ async function* runUntilImpl(agent, goal, options, deps) {
6307
6309
  return {
6308
6310
  status: "failed",
6309
6311
  turnsUsed: turn,
6312
+ tokensUsed,
6310
6313
  finalResponse: lastResponse || void 0
6311
6314
  };
6312
6315
  }
@@ -6315,7 +6318,7 @@ async function* runUntilImpl(agent, goal, options, deps) {
6315
6318
  }
6316
6319
  if (judgment.verdict === "done") {
6317
6320
  yield { type: "status_change", status: "completed", reason: judgment.reason };
6318
- return { status: "completed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
6321
+ return { status: "completed", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
6319
6322
  }
6320
6323
  if (judgment.verdict === "skipped") {
6321
6324
  yield {
@@ -6323,7 +6326,15 @@ async function* runUntilImpl(agent, goal, options, deps) {
6323
6326
  status: "completed",
6324
6327
  reason: `skipped: ${judgment.reason}`
6325
6328
  };
6326
- return { status: "completed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
6329
+ return { status: "completed", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
6330
+ }
6331
+ if (tokenBudget !== void 0 && tokensUsed >= tokenBudget) {
6332
+ yield {
6333
+ type: "status_change",
6334
+ status: "budget_limited",
6335
+ reason: `token budget (${tokenBudget}) reached: ${tokensUsed} used`
6336
+ };
6337
+ return { status: "budget_limited", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
6327
6338
  }
6328
6339
  yield { type: "continuation", turn, prompt: continuationPrompt };
6329
6340
  }
@@ -6332,13 +6343,32 @@ async function* runUntilImpl(agent, goal, options, deps) {
6332
6343
  status: "failed",
6333
6344
  reason: `max turns (${maxTurns}) exhausted`
6334
6345
  };
6335
- return { status: "failed", turnsUsed: turn, finalResponse: lastResponse || void 0 };
6346
+ return { status: "failed", turnsUsed: turn, tokensUsed, finalResponse: lastResponse || void 0 };
6336
6347
  }
6337
6348
  function composeContinuation(goal, lastResponse) {
6338
- return `Continue working toward the goal: ${goal}
6339
-
6340
- Your last response was:
6341
- ${lastResponse.slice(0, 1e3)}`;
6349
+ return [
6350
+ "Continue working toward the active goal.",
6351
+ "",
6352
+ `<objective>
6353
+ ${goal}
6354
+ </objective>`,
6355
+ "",
6356
+ "Continuation behavior:",
6357
+ "- This goal persists across turns. Keep the full objective intact; if it cannot be finished now,",
6358
+ " make concrete progress toward the real requested end state and do not redefine success around a",
6359
+ " smaller or easier task.",
6360
+ "",
6361
+ "Work from evidence:",
6362
+ "- Use the current worktree and external state as authoritative. Inspect the current state before",
6363
+ " relying on it. Improve, replace, or remove existing work as needed to satisfy the actual objective.",
6364
+ "",
6365
+ "Completion audit:",
6366
+ "- Before deciding the goal is achieved, treat completion as unproven and verify it against the actual",
6367
+ " current state, requirement by requirement. Treat uncertain or indirect evidence as not achieved.",
6368
+ "",
6369
+ `Your last response was:
6370
+ ${lastResponse.slice(0, 1e3)}`
6371
+ ].join("\n");
6342
6372
  }
6343
6373
  var init_run_until = __esm({
6344
6374
  "src/internal/runtime/lifecycle/run-until.ts"() {
@@ -18578,7 +18608,7 @@ function localAgentRunUntil(agent, goal, options) {
18578
18608
  status: "paused",
18579
18609
  reason: "runUntil() requires an explicit goal (durable objectives removed in v4.0)"
18580
18610
  };
18581
- return { status: "paused", turnsUsed: 0, finalResponse: void 0 };
18611
+ return { status: "paused", turnsUsed: 0, tokensUsed: 0, finalResponse: void 0 };
18582
18612
  }
18583
18613
  return yield* runUntilImpl2(agent, goal, options, await buildDeps());
18584
18614
  }