@agent-native/core 0.84.4 → 0.84.6

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 (39) hide show
  1. package/corpus/core/CHANGELOG.md +12 -0
  2. package/corpus/core/package.json +1 -1
  3. package/corpus/core/src/agent/production-agent.ts +97 -38
  4. package/corpus/core/src/agent/run-store.ts +17 -0
  5. package/corpus/core/src/cli/skills.ts +9 -7
  6. package/corpus/core/src/client/agent-chat-adapter.ts +13 -1
  7. package/corpus/core/src/client/sse-event-processor.ts +75 -1
  8. package/corpus/core/src/server/agent-chat-plugin.ts +57 -12
  9. package/corpus/templates/design/AGENTS.md +7 -6
  10. package/corpus/templates/design/actions/present-design-variants.ts +4 -2
  11. package/corpus/templates/design/app/hooks/use-question-flow.ts +2 -2
  12. package/corpus/templates/design/app/pages/DesignEditor.tsx +1 -1
  13. package/dist/agent/production-agent.d.ts +20 -0
  14. package/dist/agent/production-agent.d.ts.map +1 -1
  15. package/dist/agent/production-agent.js +65 -32
  16. package/dist/agent/production-agent.js.map +1 -1
  17. package/dist/agent/run-store.d.ts +14 -0
  18. package/dist/agent/run-store.d.ts.map +1 -1
  19. package/dist/agent/run-store.js +14 -0
  20. package/dist/agent/run-store.js.map +1 -1
  21. package/dist/cli/skills.d.ts.map +1 -1
  22. package/dist/cli/skills.js +9 -7
  23. package/dist/cli/skills.js.map +1 -1
  24. package/dist/client/agent-chat-adapter.d.ts.map +1 -1
  25. package/dist/client/agent-chat-adapter.js +8 -1
  26. package/dist/client/agent-chat-adapter.js.map +1 -1
  27. package/dist/client/sse-event-processor.d.ts.map +1 -1
  28. package/dist/client/sse-event-processor.js +63 -1
  29. package/dist/client/sse-event-processor.js.map +1 -1
  30. package/dist/collab/routes.d.ts +2 -2
  31. package/dist/notifications/routes.d.ts +1 -1
  32. package/dist/observability/routes.d.ts +5 -5
  33. package/dist/resources/handlers.d.ts +1 -1
  34. package/dist/server/agent-chat-plugin.d.ts +9 -0
  35. package/dist/server/agent-chat-plugin.d.ts.map +1 -1
  36. package/dist/server/agent-chat-plugin.js +20 -8
  37. package/dist/server/agent-chat-plugin.js.map +1 -1
  38. package/dist/server/agent-engine-api-key-route.d.ts +1 -1
  39. package/package.json +1 -1
@@ -3046,6 +3046,34 @@ export function shouldChainBackgroundContinuation(opts) {
3046
3046
  endsAtInternalContinuationBoundary(opts.run) &&
3047
3047
  opts.continuationCount < MAX_BACKGROUND_RUN_CONTINUATIONS);
3048
3048
  }
3049
+ export async function claimBackgroundWorkerRunEarly(opts) {
3050
+ const record = opts.deps?.recordRunDiagnostic ?? recordRunDiagnostic;
3051
+ const insert = opts.deps?.insertRun ?? insertRun;
3052
+ const claim = opts.deps?.claimBackgroundRun ?? claimBackgroundRun;
3053
+ const heartbeat = opts.deps?.updateRunHeartbeat ?? updateRunHeartbeat;
3054
+ const threadId = typeof opts.threadId === "string" && opts.threadId.trim()
3055
+ ? opts.threadId.trim()
3056
+ : opts.runId;
3057
+ const turnId = typeof opts.markerTurnId === "string" && opts.markerTurnId.trim()
3058
+ ? opts.markerTurnId.trim()
3059
+ : typeof opts.requestTurnId === "string" && opts.requestTurnId.trim()
3060
+ ? opts.requestTurnId.trim()
3061
+ : opts.runId;
3062
+ await record(opts.runId, RUN_DIAG_STAGE.workerEntered, `runsInBackgroundFunction=${opts.runsInBackgroundFunction} continuationCount=${opts.continuationCount}`).catch(() => { });
3063
+ if (opts.continuationCount > 0) {
3064
+ await insert(opts.runId, threadId, turnId, {
3065
+ dispatchMode: "background",
3066
+ }).catch(() => { });
3067
+ }
3068
+ const won = await claim(opts.runId);
3069
+ if (!won) {
3070
+ await record(opts.runId, RUN_DIAG_STAGE.workerClaimLost).catch(() => { });
3071
+ return { claimed: false, skipped: "already-claimed" };
3072
+ }
3073
+ await record(opts.runId, RUN_DIAG_STAGE.workerClaimed).catch(() => { });
3074
+ await heartbeat(opts.runId).catch(() => { });
3075
+ return { claimed: true };
3076
+ }
3049
3077
  function progressStepFromAgentChatEvent(event) {
3050
3078
  switch (event.type) {
3051
3079
  case "activity":
@@ -3164,6 +3192,23 @@ export function createProductionAgentHandler(options) {
3164
3192
  Number.isFinite(backgroundRunMarker.continuationCount)
3165
3193
  ? Math.max(0, Math.floor(backgroundRunMarker.continuationCount))
3166
3194
  : 0;
3195
+ let backgroundRunClaimedEarly = false;
3196
+ if (isBackgroundWorker && bgRunId) {
3197
+ const earlyClaim = await claimBackgroundWorkerRunEarly({
3198
+ runId: bgRunId,
3199
+ threadId,
3200
+ markerTurnId: typeof backgroundRunMarker?.turnId === "string"
3201
+ ? backgroundRunMarker.turnId
3202
+ : null,
3203
+ requestTurnId,
3204
+ continuationCount: backgroundContinuationCount,
3205
+ runsInBackgroundFunction,
3206
+ });
3207
+ if (!earlyClaim.claimed) {
3208
+ return { ok: true, skipped: earlyClaim.skipped };
3209
+ }
3210
+ backgroundRunClaimedEarly = true;
3211
+ }
3167
3212
  // The foreground POST decides whether to dispatch into a background
3168
3213
  // function. The background worker itself never re-dispatches.
3169
3214
  const dispatchToBackground = !isBackgroundWorker &&
@@ -4061,40 +4106,28 @@ export function createProductionAgentHandler(options) {
4061
4106
  }
4062
4107
  }
4063
4108
  : undefined;
4064
- // Background worker: claim the pre-inserted run idempotently before
4065
- // executing. A duplicate Netlify delivery loses the claim and no-ops here,
4066
- // so the run can never be double-executed. Bump the heartbeat immediately
4067
- // on entry so a slow cold-start doesn't leave the row looking stale to the
4068
- // reaper before startRun's 1.5s heartbeat timer takes over.
4109
+ // Background worker: the run was claimed immediately after the authenticated
4110
+ // `_process-run` body was parsed, before owner/model/prompt/tool setup. That
4111
+ // early claim is what lets the foreground subscribe to the real background
4112
+ // worker instead of racing slow setup and falling back to the 40s inline
4113
+ // path. This late block is a defensive fallback for older/custom callers
4114
+ // that somehow reach here without the early claim.
4069
4115
  if (isBackgroundWorker) {
4070
- // DIAGNOSTIC: the re-entered handler recognized itself as the background
4071
- // worker. Record the runtime regime too — `isInBackgroundFunctionRuntime()`
4072
- // reads a globalThis marker set by the bg-fn entry, which may NOT be set in
4073
- // this isolate; recording the ACTUAL resolved value reveals whether the
4074
- // worker is on the 13-min `-background` budget or the 40s clamp. This is
4075
- // the proof the worker reached its own code (vs. dying at auth before it).
4076
- await recordRunDiagnostic(runId, RUN_DIAG_STAGE.workerEntered, `runsInBackgroundFunction=${runsInBackgroundFunction} continuationCount=${backgroundContinuationCount}`).catch(() => { });
4077
- // A chained continuation chunk's runId was minted by the prior chunk and
4078
- // never inserted, so insert its background row now (idempotently — a
4079
- // duplicate Netlify delivery that already inserted it just PK-collides and
4080
- // the claim below dedups). The first chunk's row was inserted by the
4081
- // foreground, so skip the insert there.
4082
- if (isChainedBackgroundContinuation) {
4083
- await insertRun(runId, effectiveThreadId, effectiveTurnId, {
4084
- dispatchMode: "background",
4085
- }).catch(() => { });
4086
- }
4087
- const won = await claimBackgroundRun(runId);
4088
- if (!won) {
4089
- // Already claimed by an earlier delivery — return a benign ack so
4090
- // Netlify doesn't retry a successful handoff.
4091
- await recordRunDiagnostic(runId, RUN_DIAG_STAGE.workerClaimLost).catch(() => { });
4092
- return { ok: true, skipped: "already-claimed" };
4116
+ if (!backgroundRunClaimedEarly) {
4117
+ await recordRunDiagnostic(runId, RUN_DIAG_STAGE.workerEntered, `runsInBackgroundFunction=${runsInBackgroundFunction} continuationCount=${backgroundContinuationCount}`).catch(() => { });
4118
+ if (isChainedBackgroundContinuation) {
4119
+ await insertRun(runId, effectiveThreadId, effectiveTurnId, {
4120
+ dispatchMode: "background",
4121
+ }).catch(() => { });
4122
+ }
4123
+ const won = await claimBackgroundRun(runId);
4124
+ if (!won) {
4125
+ await recordRunDiagnostic(runId, RUN_DIAG_STAGE.workerClaimLost).catch(() => { });
4126
+ return { ok: true, skipped: "already-claimed" };
4127
+ }
4128
+ await recordRunDiagnostic(runId, RUN_DIAG_STAGE.workerClaimed).catch(() => { });
4129
+ await updateRunHeartbeat(runId).catch(() => { });
4093
4130
  }
4094
- // DIAGNOSTIC: this worker won the claim and now OWNS the run. If a run
4095
- // ever stalls at this stage it means the loop below failed to start.
4096
- await recordRunDiagnostic(runId, RUN_DIAG_STAGE.workerClaimed).catch(() => { });
4097
- await updateRunHeartbeat(runId).catch(() => { });
4098
4131
  }
4099
4132
  // DIAGNOSTIC-ONLY: build the pre-startRun setup-timing breakdown now (so the
4100
4133
  // marks reflect the work done BEFORE the loop), but EMIT it from inside