@capybara-hq/kernel 0.1.0-rc.102 → 0.1.0-rc.104
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/bootstrap.json +11 -11
- package/dist/conversations/events.d.ts +11 -0
- package/dist/conversations/events.js +26 -2
- package/dist/evals/reconstruct.d.ts +9 -1
- package/dist/evals/reconstruct.js +7 -0
- package/dist/extensions/sync.d.ts +9 -5
- package/dist/extensions/sync.js +28 -22
- package/dist/runtime/compaction.service.d.ts +3 -2
- package/dist/runtime/compaction.service.js +4 -5
- package/dist/runtime/context/build.d.ts +29 -3
- package/dist/runtime/context/build.js +65 -7
- package/dist/runtime/context/compaction.d.ts +18 -4
- package/dist/runtime/context/compaction.js +110 -23
- package/dist/runtime/context/run-outcome.d.ts +10 -0
- package/dist/runtime/context/run-outcome.js +92 -0
- package/dist/runtime/model-call.d.ts +7 -0
- package/dist/runtime/model-call.js +157 -89
- package/dist/tools/builtins/platform.js +4 -1
- package/package.json +1 -1
|
@@ -5,7 +5,7 @@ import { ApiError } from "../errors.js";
|
|
|
5
5
|
import { ExtensionUnloadedError } from "../extensions/inflight.js";
|
|
6
6
|
import { hasInvocation, skillLoadOffered } from "../skills/catalog.js";
|
|
7
7
|
import { buildModelRequest, latestSummary, mediaRefs, planMedia, } from "./context/build.js";
|
|
8
|
-
import { acceptSummary, buildCompactionRequest, chunkTranscript, compactionBackoff, compactionChunkSize, compactionInputBudget, compactionPlan, compactionRequestOverhead, extractSummary, MIN_COMPACTION_CHUNK_TOKENS, nextTranscriptChunk, openingSeqs, shouldCompact, summarySavedTokens, } from "./context/compaction.js";
|
|
8
|
+
import { acceptSummary, buildCompactionRequest, chunkTranscript, compactionBackoff, compactionChunkSize, compactionInputBudget, compactionPlan, compactionRequestOverhead, extractSummary, MIN_COMPACTION_CHUNK_TOKENS, nextTranscriptChunk, openingSeqs, shouldCompact, summarySavedTokens, turnsSinceSummary, } from "./context/compaction.js";
|
|
9
9
|
import { applyFit, estimateAfterFit, planFit, planningBounds, planReactiveTrim, planSqueezedFit, sentOutputCap, } from "./context/fit.js";
|
|
10
10
|
import { guardToolPairs } from "./context/pairing.js";
|
|
11
11
|
import { hasProjectSkills, overlongProjectSkills } from "./context/project-root.js";
|
|
@@ -220,11 +220,10 @@ const idem = {
|
|
|
220
220
|
/**
|
|
221
221
|
* Usage of a compaction call: `n` is the compaction's ordinal in the step
|
|
222
222
|
* ({@link compactionOrdinal}). A chunked compaction sends one call per chunk, so the chunk index
|
|
223
|
-
* (1-based) is part of the key (context compaction spec §4.5)
|
|
223
|
+
* (1-based) is part of the key (context compaction spec §4.5); the one retry of a call cut off at
|
|
224
|
+
* the output token limit adds `:retry` (spec §4.5, issue #959).
|
|
224
225
|
*/
|
|
225
|
-
compactionUsage: (runId, step, n, chunk) => chunk === undefined
|
|
226
|
-
? `usage_compaction:${runId}:${step}:${n}`
|
|
227
|
-
: `usage_compaction:${runId}:${step}:${n}:${chunk}`,
|
|
226
|
+
compactionUsage: (runId, step, n, chunk, retry = false) => `usage_compaction:${runId}:${step}:${n}${chunk === undefined ? "" : `:${chunk}`}${retry ? ":retry" : ""}`,
|
|
228
227
|
/**
|
|
229
228
|
* The outcome of one compaction: `n` is its ordinal in the step ({@link compactionOrdinal}), so a
|
|
230
229
|
* proactive summary and a forced one in the same step are two events (spec §4.4, revision
|
|
@@ -233,6 +232,8 @@ const idem = {
|
|
|
233
232
|
*/
|
|
234
233
|
compactionSummary: (runId, step, n) => `compaction_summary:${runId}:${step}:${n}`,
|
|
235
234
|
compactionFailed: (runId, step, n) => `compaction_failed:${runId}:${step}:${n}`,
|
|
235
|
+
/** A manual compact run that found nothing to summarize: one per run (spec §4.7). */
|
|
236
|
+
compactionSkipped: (runId) => `compaction_skipped:${runId}`,
|
|
236
237
|
mediaExcluded: (runId, step, attempt, part) => `media_excluded:${runId}:${step}:${attempt}:${part}`,
|
|
237
238
|
};
|
|
238
239
|
/** The step result of a sub-agent whose budget a compaction call used up: no step request. */
|
|
@@ -746,6 +747,28 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
746
747
|
summarySeq: null,
|
|
747
748
|
failed: false,
|
|
748
749
|
});
|
|
750
|
+
/**
|
|
751
|
+
* A manual compact run's early end that the automatic paths keep silent: recorded as its
|
|
752
|
+
* outcome (`compaction_failed` with a fixed reason), else just skipped.
|
|
753
|
+
*/
|
|
754
|
+
const unavailable = async (reason) => {
|
|
755
|
+
if (opts.manual !== true)
|
|
756
|
+
return skipped(events);
|
|
757
|
+
const written = await appendCounted(deps, input, [
|
|
758
|
+
{
|
|
759
|
+
type: "compaction_failed",
|
|
760
|
+
runId: input.runId,
|
|
761
|
+
idemKey: idem.compactionFailed(input.runId, input.step, compactionOrdinal(events, input.runId, input.step)),
|
|
762
|
+
payload: {
|
|
763
|
+
attempt: consecutiveCompactionFailures(events) + 1,
|
|
764
|
+
step: input.step,
|
|
765
|
+
reason,
|
|
766
|
+
},
|
|
767
|
+
},
|
|
768
|
+
]);
|
|
769
|
+
const merged = await mergeWritten(deps.eventLog, input.conversationId, events, written);
|
|
770
|
+
return { events: merged, summarySeq: null, failed: true };
|
|
771
|
+
};
|
|
749
772
|
const modelRef = opts.ref ?? input.agent.model;
|
|
750
773
|
let provider;
|
|
751
774
|
let info;
|
|
@@ -754,7 +777,7 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
754
777
|
}
|
|
755
778
|
catch (err) {
|
|
756
779
|
deps.log.warn("compaction skipped: model unavailable", { model: modelRef, err });
|
|
757
|
-
return
|
|
780
|
+
return unavailable("the model is unavailable");
|
|
758
781
|
}
|
|
759
782
|
const limits = mediaLimits(deps);
|
|
760
783
|
try {
|
|
@@ -769,7 +792,7 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
769
792
|
conversationId: input.conversationId,
|
|
770
793
|
err,
|
|
771
794
|
});
|
|
772
|
-
return
|
|
795
|
+
return unavailable("attachment storage is unavailable");
|
|
773
796
|
}
|
|
774
797
|
const { request: built, estimate: raw } = buildRequest(limits, events, input, info, attemptParams(input.agent, info, opts.fallback === true).value, runtime, tools, false);
|
|
775
798
|
const estimate = calibrate(raw, lastActual(events));
|
|
@@ -792,8 +815,30 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
792
815
|
// prompt and tool definitions every request carries
|
|
793
816
|
const tailBudget = bounds.target - estimateTokens([], built.system, built.tools);
|
|
794
817
|
const plan = compactionPlan(events, compaction, tailBudget);
|
|
795
|
-
if (!plan)
|
|
796
|
-
|
|
818
|
+
if (!plan) {
|
|
819
|
+
if (opts.manual !== true)
|
|
820
|
+
return skipped(events);
|
|
821
|
+
// a manual compaction that finds nothing to summarize says so (display only, never
|
|
822
|
+
// model-visible): every user turn since the last summary is in the kept tail
|
|
823
|
+
const written = await appendCounted(deps, input, [
|
|
824
|
+
{
|
|
825
|
+
type: "compaction_skipped",
|
|
826
|
+
runId: input.runId,
|
|
827
|
+
idemKey: idem.compactionSkipped(input.runId),
|
|
828
|
+
payload: {
|
|
829
|
+
reason: "too_few_turns",
|
|
830
|
+
keepRecentTurns: compaction.keepRecentTurns,
|
|
831
|
+
turns: turnsSinceSummary(events),
|
|
832
|
+
step: input.step,
|
|
833
|
+
},
|
|
834
|
+
},
|
|
835
|
+
]);
|
|
836
|
+
return {
|
|
837
|
+
events: await mergeWritten(deps.eventLog, input.conversationId, events, written),
|
|
838
|
+
summarySeq: null,
|
|
839
|
+
failed: false,
|
|
840
|
+
};
|
|
841
|
+
}
|
|
797
842
|
// decided now and recorded in the summary (spec §4.2): the request never depends on later events
|
|
798
843
|
const opening = openingSeqs(events, input.runId, plan.coversUpToSeq, input.absorbedUpToSeq);
|
|
799
844
|
const { runId, step, signal, conversationId } = input;
|
|
@@ -854,7 +899,7 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
854
899
|
let summary = "";
|
|
855
900
|
/** Whether the compaction really took more than one call. */
|
|
856
901
|
let multiChunk = false;
|
|
857
|
-
/** The chunk
|
|
902
|
+
/** The chunks sent (a retried chunk counts once); the total is known only when the compaction ends. */
|
|
858
903
|
let attempted = 0;
|
|
859
904
|
const usages = [];
|
|
860
905
|
for (;;) {
|
|
@@ -901,90 +946,113 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
901
946
|
// call 1 summarises chunk 1; call i carries the running summary before its chunk, which the
|
|
902
947
|
// instruction merges (compactionPlan formats a previous summary the same way)
|
|
903
948
|
const callInput = summary === "" ? chunkText : `[Earlier summary]\n${summary}\n\n${chunkText}`;
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
949
|
+
// A call that stops at the output token limit returns a cut-off summary that extractSummary
|
|
950
|
+
// would still pass on (an unclosed <analysis> or a dangling <summary> is stripped): it is a
|
|
951
|
+
// failed attempt, asked once more for a shorter summary from the start; a second cut-off fails
|
|
952
|
+
// the compaction (spec §4.5, issue #959)
|
|
953
|
+
let retry = false;
|
|
954
|
+
for (;;) {
|
|
955
|
+
const request = buildCompactionRequest(callInput, info.id, summaryCap, { retry });
|
|
956
|
+
if (hookBase && deps.hooks?.has("model.request")) {
|
|
957
|
+
let verdict;
|
|
958
|
+
try {
|
|
959
|
+
verdict = await deps.hooks.run("model.request", { ...hookBase, attempt: retry ? 2 : 1, request: hookRequestOf(request, deps.log) }, signal);
|
|
960
|
+
}
|
|
961
|
+
catch (e) {
|
|
962
|
+
// run() rejects only when the run's signal aborted: an abort, never a deny
|
|
963
|
+
if (signal.aborted) {
|
|
964
|
+
// an aborted attempt leaves no partial summary: drop the progress key now rather than
|
|
965
|
+
// leaving the writer-local throttle entry behind until the TTL (folded review Minor)
|
|
966
|
+
deps.compactionProgress?.clear(runId);
|
|
967
|
+
throw new StepAborted();
|
|
968
|
+
}
|
|
969
|
+
throw e;
|
|
970
|
+
}
|
|
971
|
+
if (!verdict.allowed) {
|
|
972
|
+
denial = { verdict, chunk: chunkNo };
|
|
973
|
+
break;
|
|
917
974
|
}
|
|
918
|
-
throw e;
|
|
919
|
-
}
|
|
920
|
-
if (!verdict.allowed) {
|
|
921
|
-
denial = { verdict, chunk: chunkNo };
|
|
922
|
-
break;
|
|
923
975
|
}
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
976
|
+
const sentAt = Date.now();
|
|
977
|
+
try {
|
|
978
|
+
let partial = "";
|
|
979
|
+
const out = await consumeModelStream(provider, request, info.streamIdleTimeoutMs ?? runtime.streamIdleMs, signal, (ev) => {
|
|
980
|
+
if (ev.type !== "text_delta")
|
|
981
|
+
return;
|
|
982
|
+
partial += ev.text;
|
|
983
|
+
// throttled by the sink (≤ once per 200 ms); real characters, never a percentage
|
|
984
|
+
progressOf({
|
|
985
|
+
phase: "summarizing",
|
|
986
|
+
step,
|
|
987
|
+
startedAt,
|
|
988
|
+
...(chunked ? { chunk: chunkNo, chunks: plannedChunks } : {}),
|
|
989
|
+
chars: partial.length,
|
|
990
|
+
});
|
|
939
991
|
});
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
992
|
+
usages.push({ chunk: multiChunk ? chunkNo : undefined, retry, usage: out.usage });
|
|
993
|
+
if (hookBase && deps.hooks) {
|
|
994
|
+
fireObserve(deps.hooks, "model.response", {
|
|
995
|
+
...hookBase,
|
|
996
|
+
attempt: retry ? 2 : 1,
|
|
997
|
+
outcome: "ok",
|
|
998
|
+
content: out.content,
|
|
999
|
+
stopReason: out.stopReason,
|
|
1000
|
+
usage: out.usage,
|
|
1001
|
+
latencyMs: Date.now() - sentAt,
|
|
1002
|
+
}, signal, deps.log);
|
|
1003
|
+
}
|
|
1004
|
+
if (out.stopReason === "max_tokens") {
|
|
1005
|
+
if (!retry) {
|
|
1006
|
+
deps.log.warn("compaction summary cut off at the output token limit; asking again", {
|
|
1007
|
+
conversationId,
|
|
1008
|
+
...(multiChunk ? { chunk: chunkNo } : {}),
|
|
1009
|
+
});
|
|
1010
|
+
retry = true;
|
|
1011
|
+
continue;
|
|
1012
|
+
}
|
|
1013
|
+
failure = `summary cut off at the output token limit (${summaryCap} tokens), also when asked again for a shorter one`;
|
|
1014
|
+
failedChunk = multiChunk ? chunkNo : null;
|
|
1015
|
+
break;
|
|
1016
|
+
}
|
|
1017
|
+
const final = out.content.flatMap((b) => (b.type === "text" ? [b.text] : [])).join("");
|
|
1018
|
+
// store only the <summary> block: the <analysis> scratchpad must not enter later requests
|
|
1019
|
+
summary = extractSummary(final || partial);
|
|
1020
|
+
// an intermediate call with nothing to carry forward would silently drop every chunk before
|
|
1021
|
+
// it: fail the compaction instead (spec §4.5), no partial summary
|
|
1022
|
+
if (remaining !== null && summary === "") {
|
|
1023
|
+
failure = "a chunk produced no summary";
|
|
1024
|
+
failedChunk = multiChunk ? chunkNo : null;
|
|
1025
|
+
break;
|
|
1026
|
+
}
|
|
1027
|
+
// the last call's text is the summary, accepted against the full transcript (spec §4.5)
|
|
1028
|
+
if (remaining === null && !acceptSummary(summary, plan.transcript)) {
|
|
1029
|
+
failure = `summary too short (${summary.length} chars)`;
|
|
1030
|
+
failedChunk = multiChunk ? chunkNo : null;
|
|
1031
|
+
}
|
|
951
1032
|
}
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
1033
|
+
catch (err) {
|
|
1034
|
+
if (err instanceof StepAborted) {
|
|
1035
|
+
// aborted mid-call: clear the progress key here (the success and failure paths clear at
|
|
1036
|
+
// the end; the abort path throws past them)
|
|
1037
|
+
deps.compactionProgress?.clear(runId);
|
|
1038
|
+
throw err;
|
|
1039
|
+
}
|
|
1040
|
+
if (hookBase && deps.hooks) {
|
|
1041
|
+
fireObserve(deps.hooks, "model.response", {
|
|
1042
|
+
...hookBase,
|
|
1043
|
+
attempt: retry ? 2 : 1,
|
|
1044
|
+
outcome: "failed",
|
|
1045
|
+
content: [],
|
|
1046
|
+
stopReason: null,
|
|
1047
|
+
usage: null,
|
|
1048
|
+
latencyMs: Date.now() - sentAt,
|
|
1049
|
+
errorCode: classify(err).code,
|
|
1050
|
+
}, signal, deps.log);
|
|
1051
|
+
}
|
|
1052
|
+
failure = err instanceof Error ? err.message : String(err);
|
|
959
1053
|
failedChunk = multiChunk ? chunkNo : null;
|
|
960
1054
|
break;
|
|
961
1055
|
}
|
|
962
|
-
// the last call's text is the summary, accepted against the full transcript (spec §4.5)
|
|
963
|
-
if (remaining === null && !acceptSummary(summary, plan.transcript)) {
|
|
964
|
-
failure = `summary too short (${summary.length} chars)`;
|
|
965
|
-
failedChunk = multiChunk ? chunkNo : null;
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
catch (err) {
|
|
969
|
-
if (err instanceof StepAborted) {
|
|
970
|
-
// aborted mid-call: clear the progress key here (the success and failure paths clear at
|
|
971
|
-
// the end; the abort path throws past them)
|
|
972
|
-
deps.compactionProgress?.clear(runId);
|
|
973
|
-
throw err;
|
|
974
|
-
}
|
|
975
|
-
if (hookBase && deps.hooks) {
|
|
976
|
-
fireObserve(deps.hooks, "model.response", {
|
|
977
|
-
...hookBase,
|
|
978
|
-
outcome: "failed",
|
|
979
|
-
content: [],
|
|
980
|
-
stopReason: null,
|
|
981
|
-
usage: null,
|
|
982
|
-
latencyMs: Date.now() - sentAt,
|
|
983
|
-
errorCode: classify(err).code,
|
|
984
|
-
}, signal, deps.log);
|
|
985
|
-
}
|
|
986
|
-
failure = err instanceof Error ? err.message : String(err);
|
|
987
|
-
failedChunk = multiChunk ? chunkNo : null;
|
|
988
1056
|
break;
|
|
989
1057
|
}
|
|
990
1058
|
if (failure !== null || denial !== null)
|
|
@@ -1001,10 +1069,10 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
1001
1069
|
/** The chunk a failure stopped at, for the reason (empty when the compaction was not chunked). */
|
|
1002
1070
|
const chunkNote = (chunk) => (chunk === null ? "" : ` (chunk ${chunk})`);
|
|
1003
1071
|
/** One usage event per completed call (spec §4.5); a chunked call carries its chunk index. */
|
|
1004
|
-
const usageEvents = () => usages.map(({ chunk, usage }) => ({
|
|
1072
|
+
const usageEvents = () => usages.map(({ chunk, retry, usage }) => ({
|
|
1005
1073
|
type: "usage",
|
|
1006
1074
|
runId,
|
|
1007
|
-
idemKey: idem.compactionUsage(runId, step, ordinal, chunk),
|
|
1075
|
+
idemKey: idem.compactionUsage(runId, step, ordinal, chunk, retry),
|
|
1008
1076
|
payload: {
|
|
1009
1077
|
step,
|
|
1010
1078
|
provider: ref.provider,
|
|
@@ -324,7 +324,10 @@ export function platformTool(deps) {
|
|
|
324
324
|
'"read" for how-to documentation (plugin format, skills, extension points).',
|
|
325
325
|
// the fixed domain list, the same on every replica and in every org
|
|
326
326
|
inputSchema: platformInputSchema(PLATFORM_DOMAINS.map((d) => d.id)),
|
|
327
|
-
|
|
327
|
+
// The row shows the first key argument present: the operation a call or describe names (a
|
|
328
|
+
// running row reads "Platform conversations.delete"), a search's query, a browsed domain;
|
|
329
|
+
// else the action (the first string input).
|
|
330
|
+
keyArgs: ["operation", "query", "domain"],
|
|
328
331
|
};
|
|
329
332
|
const facts = (ctx) => deps.actor.facts(ctx);
|
|
330
333
|
async function callPolicy(raw, ctx) {
|
package/package.json
CHANGED