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