@capybara-hq/kernel 0.1.0-rc.114 → 0.1.0-rc.115
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 +7 -7
- package/dist/admin/admin.schemas.d.ts +1 -0
- package/dist/conversations/events.d.ts +8 -0
- package/dist/conversations/events.js +11 -0
- package/dist/db/kernel-schema.d.ts +3 -0
- package/dist/identity/settings.d.ts +5 -0
- package/dist/identity/settings.js +4 -0
- package/dist/models/purposes.js +5 -0
- package/dist/orgs/settings.service.d.ts +2 -0
- package/dist/platform/ops/org-settings.ops.js +1 -1
- package/dist/runtime/compaction-model.d.ts +57 -0
- package/dist/runtime/compaction-model.js +87 -0
- package/dist/runtime/compaction.service.d.ts +23 -8
- package/dist/runtime/compaction.service.js +15 -10
- package/dist/runtime/context/compaction.d.ts +19 -2
- package/dist/runtime/context/compaction.js +23 -2
- package/dist/runtime/context/idle-compaction-rules.d.ts +17 -2
- package/dist/runtime/context/idle-compaction-rules.js +20 -5
- package/dist/runtime/idle-arm.d.ts +12 -2
- package/dist/runtime/idle-arm.js +66 -18
- package/dist/runtime/idle-compaction-jobs.d.ts +24 -3
- package/dist/runtime/idle-compaction.d.ts +11 -3
- package/dist/runtime/idle-compaction.js +61 -30
- package/dist/runtime/model-call.d.ts +52 -16
- package/dist/runtime/model-call.js +71 -24
- package/dist/runtime/run-finish.d.ts +12 -3
- package/dist/runtime/run-finish.js +14 -7
- package/dist/runtime/run-queue.d.ts +5 -2
- package/dist/runtime/run-queue.js +7 -4
- package/dist/runtime/run-steps.d.ts +4 -1
- package/dist/runtime/run-steps.js +9 -2
- package/dist/runtime/run-worker.js +4 -2
- package/dist/runtime/runtime.module.js +3 -0
- package/package.json +1 -1
- package/src/migrations/0200_runs_idle_mode.sql +4 -0
|
@@ -3,10 +3,11 @@ import type { AgentDefinition, ModelRef } from "../agents/definition.js";
|
|
|
3
3
|
import type { KernelConfig } from "../config/schema.js";
|
|
4
4
|
import type { AppendEvent, EventLog } from "../conversations/event-log.js";
|
|
5
5
|
import { type ConversationEvent } from "../conversations/events.js";
|
|
6
|
-
import type { KernelSchemaDb } from "../db/kernel-schema.js";
|
|
6
|
+
import type { IdleMode, KernelSchemaDb } from "../db/kernel-schema.js";
|
|
7
7
|
import type { CaseSnapshot } from "../evals/snapshot.js";
|
|
8
8
|
import type { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
9
9
|
import type { ToolRegistry, UnavailableTool } from "../tools/registry.js";
|
|
10
|
+
import { type CompactionModelPort, type Summarizer } from "./compaction-model.js";
|
|
10
11
|
import type { CompactionProgressSink } from "./compaction-progress.js";
|
|
11
12
|
import type { CompactionSettings, CompactionSettingsService } from "./compaction-settings.service.js";
|
|
12
13
|
import { type BuildModelRequestInput, type MediaBytes, type MediaInfo, type ModelCallMeta, type RequestTool } from "./context/build.js";
|
|
@@ -97,6 +98,13 @@ export interface ModelStepDeps {
|
|
|
97
98
|
* Absent in eval replays and single-step replays, which have no live conversation to show it on.
|
|
98
99
|
*/
|
|
99
100
|
compactionProgress?: CompactionProgressSink;
|
|
101
|
+
/**
|
|
102
|
+
* The org's `compaction` purpose (compaction model spec §4.2): read once per compaction, after
|
|
103
|
+
* the trigger passed and a plan exists. Absent = unmapped (test stubs, single-step replays): every
|
|
104
|
+
* compaction summarizes on the conversation model by today's rules. Live runs get it
|
|
105
|
+
* (`RunSteps`); full-run replays inherit it and resolve C through their billed registry.
|
|
106
|
+
*/
|
|
107
|
+
compactionModel?: CompactionModelPort;
|
|
100
108
|
/** The kernel logger (its `debug` level is optional here: SDK loggers have none). */
|
|
101
109
|
log: Logger & {
|
|
102
110
|
debug?(msg: string, fields?: Record<string, unknown>): void;
|
|
@@ -401,11 +409,15 @@ export interface CompactOptions {
|
|
|
401
409
|
*/
|
|
402
410
|
transcriptOnly?: boolean;
|
|
403
411
|
/**
|
|
404
|
-
* The idle compact run (idle compaction spec §4.3 check 9
|
|
405
|
-
* compaction with nothing written; never the transcript
|
|
406
|
-
* (
|
|
412
|
+
* The idle compact run's mode (idle compaction spec §4.3 check 9, compaction model spec §6.3).
|
|
413
|
+
* `cache`: an in-context skip ends the compaction with nothing written; never the transcript
|
|
414
|
+
* request (a compaction model mapped since arming skips the in-context build, so it writes
|
|
415
|
+
* nothing either: spec issue C3). `compaction_model`: the summarizer must be the compaction
|
|
416
|
+
* model, else the compaction ends with nothing written (`no_compaction_model`); never V. Implies
|
|
417
|
+
* nothing about `manual` (ruling R13: a manual compaction is transcript-only, so the idle run
|
|
418
|
+
* never sets it).
|
|
407
419
|
*/
|
|
408
|
-
|
|
420
|
+
idle?: IdleMode;
|
|
409
421
|
}
|
|
410
422
|
/** What one compaction execution wrote: the events, the summary's seq and whether it failed. */
|
|
411
423
|
export interface CompactionOutcome {
|
|
@@ -443,7 +455,12 @@ export type InContextFallback = InContextSkip
|
|
|
443
455
|
* Input the model has not answered follows the last reply (ruling R9, {@link
|
|
444
456
|
* unansweredAfterReply}): a tool result, or an absorbed message other than a run opener.
|
|
445
457
|
*/
|
|
446
|
-
| "pending_input"
|
|
458
|
+
| "pending_input"
|
|
459
|
+
/**
|
|
460
|
+
* A configured, usable, eligible compaction model summarizes instead (compaction model spec
|
|
461
|
+
* §4.4, decision Q2): always the transcript.
|
|
462
|
+
*/
|
|
463
|
+
| "compaction_model";
|
|
447
464
|
/** The in-context summary call of one compaction. */
|
|
448
465
|
export interface InContextSummary {
|
|
449
466
|
request: ModelRequest;
|
|
@@ -460,13 +477,18 @@ export interface InContextSummary {
|
|
|
460
477
|
* compaction spec §4.3 checks 8 and 9).
|
|
461
478
|
*/
|
|
462
479
|
export interface PreparedCompaction {
|
|
463
|
-
|
|
480
|
+
/** The conversation model V: what the next request is built for (`opts.ref` or the agent's). */
|
|
464
481
|
info: ModelInfo;
|
|
465
|
-
/**
|
|
482
|
+
/** V's ref (`opts.ref` or the agent's main model): the estimate, the plan and the fit use it. */
|
|
466
483
|
ref: ModelRef;
|
|
484
|
+
/**
|
|
485
|
+
* Who writes the summary (compaction model spec §3): the compaction model C, or V. The call
|
|
486
|
+
* part uses only it (request, hooks, usage, stream, chunk budget).
|
|
487
|
+
*/
|
|
488
|
+
summarizer: Summarizer;
|
|
467
489
|
/** The calibrated estimate of the next step's request (what the trigger compares). */
|
|
468
490
|
estimate: number;
|
|
469
|
-
/**
|
|
491
|
+
/** V's context window. */
|
|
470
492
|
window: number;
|
|
471
493
|
/** The estimated tokens of the plan's transcript: what no summary covers yet. */
|
|
472
494
|
growthTokens: number;
|
|
@@ -476,7 +498,7 @@ export interface PreparedCompaction {
|
|
|
476
498
|
};
|
|
477
499
|
/** Whether the manual rule planned it (`opts.manual`). */
|
|
478
500
|
manual: boolean;
|
|
479
|
-
/** The summary call's output cap (context compaction spec §4.8). */
|
|
501
|
+
/** The summary call's output cap (context compaction spec §4.8): `summarizer.cap`. */
|
|
480
502
|
summaryCap: number;
|
|
481
503
|
/** The in-context summary request, or why the compaction would summarize the transcript. */
|
|
482
504
|
inContext: InContextSummary | {
|
|
@@ -503,14 +525,24 @@ export type CompactionPrepareSkip =
|
|
|
503
525
|
| {
|
|
504
526
|
skipped: "unavailable";
|
|
505
527
|
reason: string;
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* `opts.idle === "compaction_model"` and the summarizer is not the compaction model (unmapped,
|
|
531
|
+
* unavailable or too small now): the idle run never falls back to V (compaction model spec §6.3).
|
|
532
|
+
*/
|
|
533
|
+
| {
|
|
534
|
+
skipped: "no_compaction_model";
|
|
506
535
|
};
|
|
507
536
|
/**
|
|
508
537
|
* The preparation of one compaction (idle compaction spec §4.3): resolve the model, load the media,
|
|
509
538
|
* build the next step's request and calibrate its estimate, apply the trigger and the backoff (unless
|
|
510
|
-
* `opts.force`), plan,
|
|
511
|
-
*
|
|
512
|
-
*
|
|
513
|
-
*
|
|
539
|
+
* `opts.force`), plan, choose the summarizer ({@link chooseSummarizer}: the org's compaction model
|
|
540
|
+
* when it is usable and eligible, else V), and build the in-context summary request
|
|
541
|
+
* ({@link inContextSummary}; skipped when the compaction model summarizes). It writes nothing and
|
|
542
|
+
* sends no model call; the outcomes a manual compaction records (`compaction_skipped`,
|
|
543
|
+
* `compaction_failed` for an unavailable model) are {@link runCompaction}'s. `hooks.planned` runs
|
|
544
|
+
* once the summarizer is chosen (an idle run in compaction-model mode without one ends before it),
|
|
545
|
+
* before the in-context build reads the live snapshot.
|
|
514
546
|
*/
|
|
515
547
|
export declare function prepareCompaction(deps: ModelStepDeps, input: ModelStepInput, events: ConversationEvent[], runtime: Runtime, compaction: CompactionSettings, tools: StepTools, opts?: CompactOptions, hooks?: {
|
|
516
548
|
planned?: () => void;
|
|
@@ -526,8 +558,12 @@ export declare function prepareCompaction(deps: ModelStepDeps, input: ModelStepI
|
|
|
526
558
|
*
|
|
527
559
|
* `opts.force` (the reactive path, spec §4.4, and a manual compact run, spec §4.7) bypasses the
|
|
528
560
|
* trigger, `enabled` and the backoff, and `opts.ref` makes it summarise with the rung's model rather
|
|
529
|
-
* than the agent's main model. `opts.
|
|
530
|
-
*
|
|
561
|
+
* than the agent's main model. `opts.idle` (the idle compact run, idle compaction spec §4.3)
|
|
562
|
+
* ends the compaction with nothing written when its summarizer does not apply: the in-context
|
|
563
|
+
* summary in `cache` mode, the compaction model in `compaction_model` mode. The summary calls go
|
|
564
|
+
* to the prepared summarizer (compaction model spec §3): the org's compaction model when one is
|
|
565
|
+
* usable and eligible, else the conversation model; a failed or denied call on it is never
|
|
566
|
+
* retried on the other (decision Q1).
|
|
531
567
|
*
|
|
532
568
|
* The one execution every path runs: the automatic step path through {@link maybeCompact} and a
|
|
533
569
|
* compact run (manual or idle) through `CompactionService.force`. The preparation is
|
|
@@ -4,6 +4,7 @@ import { MEDIA_EXCLUDED_MAX_IDS } from "../conversations/events.js";
|
|
|
4
4
|
import { ApiError } from "../errors.js";
|
|
5
5
|
import { ExtensionUnloadedError } from "../extensions/inflight.js";
|
|
6
6
|
import { hasInvocation, skillLoadOffered } from "../skills/catalog.js";
|
|
7
|
+
import { chooseSummarizer } from "./compaction-model.js";
|
|
7
8
|
import { buildModelRequest, latestSummary, mediaRefs, planMedia, } from "./context/build.js";
|
|
8
9
|
import { selectCacheTtl } from "./context/cache-ttl.js";
|
|
9
10
|
import { acceptSummary, buildCompactionRequest, chunkTranscript, compactionBackoff, compactionChunkSize, compactionInputBudget, compactionPlan, compactionRequestOverhead, extractSummary, MIN_COMPACTION_CHUNK_TOKENS, manualOpeningSeqs, manualTailBudget, nextTranscriptChunk, openingSeqs, shouldCompact, summarySavedTokens, turnsSinceSummary, } from "./context/compaction.js";
|
|
@@ -843,10 +844,13 @@ async function inContextSummary(deps, input, events, ref, info, tools, settings,
|
|
|
843
844
|
/**
|
|
844
845
|
* The preparation of one compaction (idle compaction spec §4.3): resolve the model, load the media,
|
|
845
846
|
* build the next step's request and calibrate its estimate, apply the trigger and the backoff (unless
|
|
846
|
-
* `opts.force`), plan,
|
|
847
|
-
*
|
|
848
|
-
*
|
|
849
|
-
*
|
|
847
|
+
* `opts.force`), plan, choose the summarizer ({@link chooseSummarizer}: the org's compaction model
|
|
848
|
+
* when it is usable and eligible, else V), and build the in-context summary request
|
|
849
|
+
* ({@link inContextSummary}; skipped when the compaction model summarizes). It writes nothing and
|
|
850
|
+
* sends no model call; the outcomes a manual compaction records (`compaction_skipped`,
|
|
851
|
+
* `compaction_failed` for an unavailable model) are {@link runCompaction}'s. `hooks.planned` runs
|
|
852
|
+
* once the summarizer is chosen (an idle run in compaction-model mode without one ends before it),
|
|
853
|
+
* before the in-context build reads the live snapshot.
|
|
850
854
|
*/
|
|
851
855
|
export async function prepareCompaction(deps, input, events, runtime, compaction, tools, opts = {}, hooks = {}) {
|
|
852
856
|
const modelRef = opts.ref ?? input.agent.model;
|
|
@@ -906,18 +910,40 @@ export async function prepareCompaction(deps, input, events, runtime, compaction
|
|
|
906
910
|
: compactionPlan(events, compaction, tailBudget);
|
|
907
911
|
if (!plan)
|
|
908
912
|
return { skipped: "no_plan", estimate, window: info.contextWindow };
|
|
913
|
+
// compaction model spec §4.2: the purpose is read only now (the trigger passed, a plan exists),
|
|
914
|
+
// so a step that does not compact pays nothing; the plan above is V's either way
|
|
915
|
+
let summarizer;
|
|
916
|
+
try {
|
|
917
|
+
({ summarizer } = await chooseSummarizer(deps, input.orgId, { ref: modelRef, provider, info }, compaction));
|
|
918
|
+
}
|
|
919
|
+
catch (err) {
|
|
920
|
+
// the purpose could not be read (not "unconfigured": that is V by today's rules): the
|
|
921
|
+
// compaction ends as an unavailable model does, never on V, and the step continues
|
|
922
|
+
deps.log.warn("compaction skipped: the compaction model is unavailable", {
|
|
923
|
+
context: "Compaction",
|
|
924
|
+
conversationId: input.conversationId,
|
|
925
|
+
err,
|
|
926
|
+
});
|
|
927
|
+
return { skipped: "unavailable", reason: "the compaction model is unavailable" };
|
|
928
|
+
}
|
|
929
|
+
if (opts.idle === "compaction_model" && summarizer.source !== "compaction_model") {
|
|
930
|
+
return { skipped: "no_compaction_model" };
|
|
931
|
+
}
|
|
909
932
|
hooks.planned?.();
|
|
910
|
-
// the summary call's output cap: the org's effective setting, never above
|
|
911
|
-
// (context compaction spec §4.8)
|
|
912
|
-
const summaryCap =
|
|
933
|
+
// the summary call's output cap: the org's effective setting, never above V's own cap nor, on
|
|
934
|
+
// the compaction model, above its cap (context compaction spec §4.8, compaction model spec §5.1)
|
|
935
|
+
const summaryCap = summarizer.cap;
|
|
913
936
|
// idle compaction spec §9.4: an automatic compaction tries the summary from the conversation's
|
|
914
937
|
// last request first (its cached prefix); a skip (always for a manual one, ruling R13)
|
|
915
|
-
// summarizes the transcript exactly as before, or ends
|
|
916
|
-
|
|
938
|
+
// summarizes the transcript exactly as before, or ends a cache-mode idle one. A compaction
|
|
939
|
+
// model always summarizes the transcript (compaction model spec §4.4, Q2).
|
|
940
|
+
const inContext = summarizer.source === "compaction_model"
|
|
941
|
+
? { skip: "compaction_model" }
|
|
942
|
+
: await inContextSummary(deps, input, events, modelRef, info, tools, { reserveTokens: compaction.reserveTokens, summaryCap }, plan, opts);
|
|
917
943
|
return {
|
|
918
|
-
provider,
|
|
919
944
|
info,
|
|
920
945
|
ref: modelRef,
|
|
946
|
+
summarizer,
|
|
921
947
|
estimate,
|
|
922
948
|
window: info.contextWindow,
|
|
923
949
|
growthTokens: textTokens(plan.transcript),
|
|
@@ -938,8 +964,12 @@ export async function prepareCompaction(deps, input, events, runtime, compaction
|
|
|
938
964
|
*
|
|
939
965
|
* `opts.force` (the reactive path, spec §4.4, and a manual compact run, spec §4.7) bypasses the
|
|
940
966
|
* trigger, `enabled` and the backoff, and `opts.ref` makes it summarise with the rung's model rather
|
|
941
|
-
* than the agent's main model. `opts.
|
|
942
|
-
*
|
|
967
|
+
* than the agent's main model. `opts.idle` (the idle compact run, idle compaction spec §4.3)
|
|
968
|
+
* ends the compaction with nothing written when its summarizer does not apply: the in-context
|
|
969
|
+
* summary in `cache` mode, the compaction model in `compaction_model` mode. The summary calls go
|
|
970
|
+
* to the prepared summarizer (compaction model spec §3): the org's compaction model when one is
|
|
971
|
+
* usable and eligible, else the conversation model; a failed or denied call on it is never
|
|
972
|
+
* retried on the other (decision Q1).
|
|
943
973
|
*
|
|
944
974
|
* The one execution every path runs: the automatic step path through {@link maybeCompact} and a
|
|
945
975
|
* compact run (manual or idle) through `CompactionService.force`. The preparation is
|
|
@@ -975,8 +1005,9 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
975
1005
|
return { events: merged, summarySeq: null, failed: true };
|
|
976
1006
|
};
|
|
977
1007
|
// §4.6 progress: from the plan on the compaction is certain, so the conversation may show a row.
|
|
978
|
-
// It is display-only (Redis + a bus message, never the log); a lost update costs nothing.
|
|
979
|
-
// compaction is certain only once its in-context request exists, so it shows the
|
|
1008
|
+
// It is display-only (Redis + a bus message, never the log); a lost update costs nothing. A
|
|
1009
|
+
// cache-mode idle compaction is certain only once its in-context request exists, so it shows the
|
|
1010
|
+
// row after it; a compaction-model one once its summarizer is chosen (the planned hook).
|
|
980
1011
|
let startedAt = 0;
|
|
981
1012
|
const progressOf = (progress) => {
|
|
982
1013
|
deps.compactionProgress?.update({
|
|
@@ -992,13 +1023,16 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
992
1023
|
};
|
|
993
1024
|
const prepared = await prepareCompaction(deps, input, events, runtime, compaction, tools, opts, {
|
|
994
1025
|
planned: () => {
|
|
995
|
-
if (opts.
|
|
1026
|
+
if (opts.idle !== "cache")
|
|
996
1027
|
preparing();
|
|
997
1028
|
},
|
|
998
1029
|
});
|
|
999
1030
|
if ("skipped" in prepared) {
|
|
1000
1031
|
if (prepared.skipped === "unavailable")
|
|
1001
1032
|
return unavailable(prepared.reason);
|
|
1033
|
+
// an idle run in compaction-model mode without the compaction model: nothing written
|
|
1034
|
+
if (prepared.skipped === "no_compaction_model")
|
|
1035
|
+
return skipped(events);
|
|
1002
1036
|
if (prepared.skipped !== "no_plan" || opts.manual !== true)
|
|
1003
1037
|
return skipped(events);
|
|
1004
1038
|
// a manual compaction that finds nothing to summarize says so (display only, never
|
|
@@ -1021,8 +1055,9 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
1021
1055
|
failed: false,
|
|
1022
1056
|
};
|
|
1023
1057
|
}
|
|
1024
|
-
|
|
1025
|
-
const
|
|
1058
|
+
// the call part uses only the summarizer: the plan above was V's (compaction model spec §5.1)
|
|
1059
|
+
const { summarizer, plan, manual, summaryCap, inContext } = prepared;
|
|
1060
|
+
const { provider, info } = summarizer;
|
|
1026
1061
|
const ic = "skip" in inContext ? null : inContext;
|
|
1027
1062
|
if ("skip" in inContext) {
|
|
1028
1063
|
deps.log.debug?.("in-context summary skipped", {
|
|
@@ -1030,16 +1065,18 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
1030
1065
|
conversationId: input.conversationId,
|
|
1031
1066
|
reason: inContext.skip,
|
|
1032
1067
|
});
|
|
1033
|
-
// the idle compact run never falls back to the transcript request (idle compaction
|
|
1034
|
-
// §4.3 check 9): it ends with nothing written
|
|
1035
|
-
if (opts.
|
|
1068
|
+
// the cache-mode idle compact run never falls back to the transcript request (idle compaction
|
|
1069
|
+
// spec §4.3 check 9): it ends with nothing written
|
|
1070
|
+
if (opts.idle === "cache")
|
|
1036
1071
|
return skipped(events);
|
|
1037
1072
|
}
|
|
1038
|
-
else if (opts.
|
|
1073
|
+
else if (opts.idle === "cache") {
|
|
1039
1074
|
preparing();
|
|
1040
1075
|
}
|
|
1041
1076
|
const { runId, step, signal, conversationId } = input;
|
|
1042
|
-
const ref =
|
|
1077
|
+
const ref = summarizer.ref;
|
|
1078
|
+
/** The model a summary or a failed call names (compaction model spec §7.1; not model-visible). */
|
|
1079
|
+
const writtenBy = { provider: ref.provider, model: ref.model };
|
|
1043
1080
|
// review M6: the preparation awaited the snapshot read; an abort meanwhile sends no call
|
|
1044
1081
|
if (signal.aborted) {
|
|
1045
1082
|
deps.compactionProgress?.clear(runId);
|
|
@@ -1153,7 +1190,10 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
1153
1190
|
? retry
|
|
1154
1191
|
? ic.retryRequest
|
|
1155
1192
|
: ic.request
|
|
1156
|
-
: buildCompactionRequest(callInput, info.id, summaryCap, {
|
|
1193
|
+
: buildCompactionRequest(callInput, info.id, summaryCap, {
|
|
1194
|
+
retry,
|
|
1195
|
+
params: summarizer.params,
|
|
1196
|
+
});
|
|
1157
1197
|
if (hookBase && deps.hooks?.has("model.request")) {
|
|
1158
1198
|
let verdict;
|
|
1159
1199
|
try {
|
|
@@ -1313,6 +1353,7 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
1313
1353
|
attempt: attemptNo,
|
|
1314
1354
|
step,
|
|
1315
1355
|
reason: `compaction request blocked by the "${verdict.plugin}" policy hook (${verdict.cause})${chunkNote(multiChunk ? denial.chunk : null)}`,
|
|
1356
|
+
model: writtenBy,
|
|
1316
1357
|
},
|
|
1317
1358
|
};
|
|
1318
1359
|
deps.log.warn("compaction blocked by a policy hook", {
|
|
@@ -1357,13 +1398,19 @@ export async function runCompaction(deps, input, events, runtime, compaction, to
|
|
|
1357
1398
|
savedTokens: summarySavedTokens(savedFrom, summary),
|
|
1358
1399
|
method: ic !== null ? "in_context" : "transcript",
|
|
1359
1400
|
step,
|
|
1401
|
+
model: writtenBy,
|
|
1360
1402
|
},
|
|
1361
1403
|
}
|
|
1362
1404
|
: {
|
|
1363
1405
|
type: "compaction_failed",
|
|
1364
1406
|
runId,
|
|
1365
1407
|
idemKey: idem.compactionFailed(runId, step, ordinal),
|
|
1366
|
-
payload: {
|
|
1408
|
+
payload: {
|
|
1409
|
+
attempt: attemptNo,
|
|
1410
|
+
step,
|
|
1411
|
+
reason: `${failure}${chunkNote(failedChunk)}`,
|
|
1412
|
+
model: writtenBy,
|
|
1413
|
+
},
|
|
1367
1414
|
};
|
|
1368
1415
|
// the compaction calls' usage (I-1): logged with the outcome in one append (one transaction),
|
|
1369
1416
|
// marked `purpose: "compaction"` so step consumers (calibration, trajectory steps, replay plans)
|
|
@@ -28,13 +28,22 @@ export interface FinishRunArgs {
|
|
|
28
28
|
*/
|
|
29
29
|
idleCompaction?: IdleCompactionIntent;
|
|
30
30
|
}
|
|
31
|
-
/**
|
|
32
|
-
|
|
31
|
+
/**
|
|
32
|
+
* What {@link FinishRunArgs.idleCompaction} carries (`runAt` and `expiresAt` in epoch ms), by the
|
|
33
|
+
* job's mode (compaction model spec §6.2): a cache-mode job carries the armed call and its expiry,
|
|
34
|
+
* a compaction-model job only its due time.
|
|
35
|
+
*/
|
|
36
|
+
export type IdleCompactionIntent = {
|
|
33
37
|
orgId: string;
|
|
38
|
+
mode: "cache";
|
|
34
39
|
cacheCallSeq: number;
|
|
35
40
|
expiresAt: number;
|
|
36
41
|
runAt: number;
|
|
37
|
-
}
|
|
42
|
+
} | {
|
|
43
|
+
orgId: string;
|
|
44
|
+
mode: "compaction_model";
|
|
45
|
+
runAt: number;
|
|
46
|
+
};
|
|
38
47
|
/**
|
|
39
48
|
* Writes runs' terminal states (one instance; the run worker, the run queue's orphan abort, the
|
|
40
49
|
* `run-orphan-check`, the waits and the eval replays finish runs through it).
|
|
@@ -181,21 +181,28 @@ let RunFinish = class RunFinish {
|
|
|
181
181
|
await oweEnsureRun(this.outbox, a.conversationId, seq, conv.org_id);
|
|
182
182
|
}
|
|
183
183
|
// the idle compaction job armed for this run's end (idle compaction spec §4.2, S4): due before
|
|
184
|
-
// the prompt cache expires
|
|
184
|
+
// the prompt cache expires (cache mode) or `idleDelayMinutes` after the run (compaction-model
|
|
185
|
+
// mode), stale as soon as anything but a run-less side-call usage follows
|
|
185
186
|
if (a.idleCompaction !== undefined &&
|
|
186
187
|
conv?.org_live &&
|
|
187
188
|
isLive(conv.deleted_at) &&
|
|
188
189
|
conv.kind === "chat" &&
|
|
189
190
|
a.idleCompaction.orgId === conv.org_id) {
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
orgId,
|
|
191
|
+
const intent = a.idleCompaction;
|
|
192
|
+
const job = {
|
|
193
|
+
orgId: intent.orgId,
|
|
193
194
|
conversationId: a.conversationId,
|
|
194
195
|
runId: a.runId,
|
|
195
196
|
armedSeq: seq,
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
197
|
+
};
|
|
198
|
+
await oweIdleCompaction(this.outbox, intent.mode === "cache"
|
|
199
|
+
? {
|
|
200
|
+
...job,
|
|
201
|
+
mode: "cache",
|
|
202
|
+
cacheCallSeq: intent.cacheCallSeq,
|
|
203
|
+
expiresAt: intent.expiresAt,
|
|
204
|
+
}
|
|
205
|
+
: { ...job, mode: "compaction_model" }, intent.runAt);
|
|
199
206
|
}
|
|
200
207
|
if (conv?.org_live)
|
|
201
208
|
await this.oweTreeWork(a, conv.kind, conv.org_id);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Logger } from "@capybara-hq/plugin-sdk";
|
|
2
2
|
import { EventLog } from "../conversations/event-log.js";
|
|
3
|
+
import type { IdleMode } from "../db/kernel-schema.js";
|
|
3
4
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
4
5
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
5
6
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
@@ -93,9 +94,11 @@ export declare class RunQueue {
|
|
|
93
94
|
* after `armedSeq`, and that the conversation is not busy: no active run (queued, running or
|
|
94
95
|
* waiting), no unconsumed message, no queue head. The run is a `compact` run with
|
|
95
96
|
* `compact_origin = 'idle'`, run as the conversation's owner, its boundary the current
|
|
96
|
-
* `consumed_seq`, exactly as a manual one.
|
|
97
|
+
* `consumed_seq`, exactly as a manual one. `mode` is the mode the job was armed in, stored as
|
|
98
|
+
* `idle_mode` so the run worker runs it in that mode (compaction model spec §6.3: a run never
|
|
99
|
+
* switches modes between arm, fire and run).
|
|
97
100
|
*/
|
|
98
|
-
startIdleCompaction(conversationId: string, armedSeq: number): Promise<string | null>;
|
|
101
|
+
startIdleCompaction(conversationId: string, armedSeq: number, mode: IdleMode): Promise<string | null>;
|
|
99
102
|
/** {@link startIdleCompaction}'s transaction: the re-checks, the run row and its owed job. */
|
|
100
103
|
private startIdleCompactionIn;
|
|
101
104
|
/** {@link ensureRun}'s transaction: what it decided goes back through `st`. */
|
|
@@ -229,11 +229,13 @@ let RunQueue = class RunQueue {
|
|
|
229
229
|
* after `armedSeq`, and that the conversation is not busy: no active run (queued, running or
|
|
230
230
|
* waiting), no unconsumed message, no queue head. The run is a `compact` run with
|
|
231
231
|
* `compact_origin = 'idle'`, run as the conversation's owner, its boundary the current
|
|
232
|
-
* `consumed_seq`, exactly as a manual one.
|
|
232
|
+
* `consumed_seq`, exactly as a manual one. `mode` is the mode the job was armed in, stored as
|
|
233
|
+
* `idle_mode` so the run worker runs it in that mode (compaction model spec §6.3: a run never
|
|
234
|
+
* switches modes between arm, fire and run).
|
|
233
235
|
*/
|
|
234
|
-
async startIdleCompaction(conversationId, armedSeq) {
|
|
236
|
+
async startIdleCompaction(conversationId, armedSeq, mode) {
|
|
235
237
|
try {
|
|
236
|
-
return await this.startIdleCompactionIn(conversationId, armedSeq);
|
|
238
|
+
return await this.startIdleCompactionIn(conversationId, armedSeq, mode);
|
|
237
239
|
}
|
|
238
240
|
catch (err) {
|
|
239
241
|
this.log.warn("idle compaction run not started", { conversationId, err });
|
|
@@ -244,7 +246,7 @@ let RunQueue = class RunQueue {
|
|
|
244
246
|
// org-filter: the idle-compaction job's kernel-written conversation id (owed by the run finish
|
|
245
247
|
// that armed it), run in runJob under the job's org; the conversation's own org is read under its
|
|
246
248
|
// row lock
|
|
247
|
-
async startIdleCompactionIn(conversationId, armedSeq) {
|
|
249
|
+
async startIdleCompactionIn(conversationId, armedSeq, mode) {
|
|
248
250
|
const t = this.db;
|
|
249
251
|
const conv = await t
|
|
250
252
|
.selectFrom("conversations")
|
|
@@ -322,6 +324,7 @@ let RunQueue = class RunQueue {
|
|
|
322
324
|
job_id: jobId.recorded,
|
|
323
325
|
compact: true,
|
|
324
326
|
compact_origin: "idle",
|
|
327
|
+
idle_mode: mode,
|
|
325
328
|
})
|
|
326
329
|
.execute();
|
|
327
330
|
return runId;
|
|
@@ -4,6 +4,7 @@ import { ConversationMedia } from "../files/conversation-media.js";
|
|
|
4
4
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
5
5
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
6
6
|
import type { KernelLogger } from "../log.js";
|
|
7
|
+
import { ModelPurposesService } from "../models/purposes.js";
|
|
7
8
|
import { ToolPipeline } from "../tools/pipeline.js";
|
|
8
9
|
import { ToolRegistry } from "../tools/registry.js";
|
|
9
10
|
import { KyselyCheckpointSaver } from "./checkpointer.js";
|
|
@@ -40,7 +41,9 @@ export declare class RunSteps {
|
|
|
40
41
|
readonly deps: LoopDeps;
|
|
41
42
|
constructor(kdb: KernelDb, eventLog: EventLog, stepLog: StepLog, registry: ProviderRegistry, tools: ToolRegistry, config: KernelConfigService, log: KernelLogger, toolPipeline: ToolPipeline, host: ExtensionHost, subagents: SubagentsService,
|
|
42
43
|
/** The conversation's media: library files it refers to, and `read` copies. */
|
|
43
|
-
media: ConversationMedia, compactionSettings: CompactionSettingsService, compactionProgress: CompactionProgressService, permissionContext: PermissionContextRecorder, writeBase: WriteBaseService
|
|
44
|
+
media: ConversationMedia, compactionSettings: CompactionSettingsService, compactionProgress: CompactionProgressService, permissionContext: PermissionContextRecorder, writeBase: WriteBaseService,
|
|
45
|
+
/** The org's `compaction` purpose (compaction model spec §4.2). */
|
|
46
|
+
purposes: ModelPurposesService);
|
|
44
47
|
}
|
|
45
48
|
/**
|
|
46
49
|
* The live request boundary of a run's steps (spec B2): every agent-history request construction
|
|
@@ -15,9 +15,11 @@ import { ConversationMedia } from "../files/conversation-media.js";
|
|
|
15
15
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
16
16
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
17
17
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
18
|
+
import { ModelPurposesService } from "../models/purposes.js";
|
|
18
19
|
import { ToolPipeline } from "../tools/pipeline.js";
|
|
19
20
|
import { ToolRegistry } from "../tools/registry.js";
|
|
20
21
|
import { KyselyCheckpointSaver } from "./checkpointer.js";
|
|
22
|
+
import { compactionModelPort } from "./compaction-model.js";
|
|
21
23
|
import { CompactionProgressService } from "./compaction-progress.js";
|
|
22
24
|
import { CompactionSettingsService } from "./compaction-settings.service.js";
|
|
23
25
|
import { LangGraphLoop } from "./langgraph-loop.js";
|
|
@@ -58,7 +60,9 @@ let RunSteps = class RunSteps {
|
|
|
58
60
|
deps;
|
|
59
61
|
constructor(kdb, eventLog, stepLog, registry, tools, config, log, toolPipeline, host, subagents,
|
|
60
62
|
/** The conversation's media: library files it refers to, and `read` copies. */
|
|
61
|
-
media, compactionSettings, compactionProgress, permissionContext, writeBase
|
|
63
|
+
media, compactionSettings, compactionProgress, permissionContext, writeBase,
|
|
64
|
+
/** The org's `compaction` purpose (compaction model spec §4.2). */
|
|
65
|
+
purposes) {
|
|
62
66
|
this.deps = {
|
|
63
67
|
get db() {
|
|
64
68
|
return kdb.db;
|
|
@@ -71,6 +75,8 @@ let RunSteps = class RunSteps {
|
|
|
71
75
|
compactionSettings,
|
|
72
76
|
// ephemeral, display-only: the run's progress on its own conversation's stream
|
|
73
77
|
compactionProgress,
|
|
78
|
+
// the summarizer of every compaction: the org's compaction model when it is usable
|
|
79
|
+
compactionModel: compactionModelPort(purposes),
|
|
74
80
|
log,
|
|
75
81
|
toolPipeline,
|
|
76
82
|
// plugin hooks for chat runs; the full-run replay drops them (P-18)
|
|
@@ -108,7 +114,8 @@ RunSteps = __decorate([
|
|
|
108
114
|
__param(11, Inject(CompactionSettingsService)),
|
|
109
115
|
__param(12, Inject(CompactionProgressService)),
|
|
110
116
|
__param(13, Inject(PermissionContextRecorder)),
|
|
111
|
-
__param(14, Inject(WriteBaseService))
|
|
117
|
+
__param(14, Inject(WriteBaseService)),
|
|
118
|
+
__param(15, Inject(ModelPurposesService))
|
|
112
119
|
], RunSteps);
|
|
113
120
|
export { RunSteps };
|
|
114
121
|
/**
|
|
@@ -422,6 +422,7 @@ let RunWorker = class RunWorker {
|
|
|
422
422
|
"budget_run_id",
|
|
423
423
|
"compact",
|
|
424
424
|
"compact_origin",
|
|
425
|
+
"idle_mode",
|
|
425
426
|
])
|
|
426
427
|
.where("id", "=", runId)
|
|
427
428
|
.executeTakeFirst();
|
|
@@ -1042,8 +1043,9 @@ let RunWorker = class RunWorker {
|
|
|
1042
1043
|
absorbedUpToSeq: Number(consumed_seq),
|
|
1043
1044
|
signal: ac.signal,
|
|
1044
1045
|
},
|
|
1045
|
-
// R13: an idle run
|
|
1046
|
-
|
|
1046
|
+
// R13: an idle run never sets `manual`; it runs in the mode it was armed in (a row from
|
|
1047
|
+
// before 0200 has none: cache, the in-context summary or nothing)
|
|
1048
|
+
origin === "idle" ? { origin, idleMode: run.idle_mode ?? "cache" } : { origin });
|
|
1047
1049
|
}
|
|
1048
1050
|
catch (err) {
|
|
1049
1051
|
if (state.abandoned)
|
|
@@ -22,6 +22,7 @@ import { IdentityModule } from "../identity/identity.module.js";
|
|
|
22
22
|
import { LockService } from "../infra/lock/lock.service.js";
|
|
23
23
|
import { QueueModule } from "../infra/queue/queue.module.js";
|
|
24
24
|
import { REDIS } from "../infra/tokens.js";
|
|
25
|
+
import { ModelsModule } from "../models/models.module.js";
|
|
25
26
|
import { OrgSettingsModule } from "../orgs/settings.service.js";
|
|
26
27
|
import { OperationAuthorityModule } from "../sandbox/operation-authority.module.js";
|
|
27
28
|
import { PermissionAuthorityModule } from "../sandbox/permission-authority.module.js";
|
|
@@ -90,6 +91,8 @@ RuntimeModule = __decorate([
|
|
|
90
91
|
PermissionProfileStoreModule,
|
|
91
92
|
GitModule,
|
|
92
93
|
ProvidersModule,
|
|
94
|
+
// the `compaction` purpose the run steps summarize with (compaction model spec §4.2)
|
|
95
|
+
ModelsModule,
|
|
93
96
|
RunJobsModule,
|
|
94
97
|
SubagentsModule,
|
|
95
98
|
UsageReportModule,
|
package/package.json
CHANGED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
-- Compaction model (compaction model spec §6.1): the mode of an idle compact run, decided when
|
|
2
|
+
-- its job was armed; null on every other run.
|
|
3
|
+
ALTER TABLE runs ADD COLUMN IF NOT EXISTS idle_mode text NULL
|
|
4
|
+
CONSTRAINT runs_idle_mode_check CHECK (idle_mode IN ('cache', 'compaction_model'));
|