Merge accepted local main into PR #184 working branch

This commit is contained in:
amosblomqvist
2026-10-27 18:12:09 +02:00
28 changed files with 1464 additions and 377 deletions
+3 -3
View File
@@ -272,9 +272,9 @@ export function newCompactionMarker(summary: string, details: CompactionDetails)
};
}
/** The ACTIVE compaction marker: the newest one in the record (chained
* compactions — the last one rules; older markers fall inside the
* dropped span and never render). */
/** The ACTIVE compaction marker: the newest one with a retained-message
* id. Its summary supersedes older markers even when their stored
* positions remain inside its retained tail. */
export function activeCompactionOf(messages: readonly LhMessage[]): (LhMarkerMessage & { details: CompactionDetails }) | null {
for (let i = messages.length - 1; i >= 0; i--) {
const m = messages[i]!;
+7 -7
View File
@@ -563,8 +563,9 @@ export function toLlmMessages(messages: readonly AgentMessage[]): Message[] {
// THE COMPACTION CUT (owner round 14, 2026-10-05): the ACTIVE marker
// (the newest — chained compactions rule last) names the first kept
// message; everything before it is replaced by the marker's content
// (header + summary) rendered ONCE, up top. The marker itself is then
// skipped in place — it must never render twice. A cut naming a
// (header + summary) rendered ONCE, up top. All compaction markers
// in the retained tail are then skipped, including older summaries
// whose stored positions still lie after the cut. A cut naming a
// missing id degrades honestly (everything renders — never throw).
const lhMessages = messages as readonly LhMessage[];
const active = activeCompactionOf(lhMessages);
@@ -605,11 +606,10 @@ export function toLlmMessages(messages: readonly AgentMessage[]): Message[] {
// UI-only markers: plan blocks, and problem dismissals (the
// synthesized conclude pair carries the model's face).
if (marker.customType === "plan-block" || marker.customType === "problem-dismissed") continue;
// The ACTIVE compaction marker already rendered as the head —
// never twice. (Stale markers before the cut are unreachable; a
// marker whose cut could not resolve has no head and renders as
// its user text — honest degradation.)
if (marker.customType === "compaction" && head !== null && marker.id === active!.id) continue;
// The head supersedes every compaction marker, not only its own
// stored position. Without a resolved head, preserve the existing
// full-history fallback, including the summary text.
if (marker.customType === "compaction" && head !== null) continue;
out.push({
role: "user",
content: [{ type: "text", text: marker.content }],
+15 -2
View File
@@ -69,7 +69,7 @@ async function subagentSummarizer(prompt: string, engine: PiEngine): Promise<{ s
totalTokens: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
};
const { text } = await runSubagent([], {
const { text, newMessages } = await runSubagent([], {
// The engine override is always present (the chat's own conversation
// engine — owner ruling), so the role label never resolves an
// engine; "researcher" is the least-wrong existing ModelRole label
@@ -83,6 +83,12 @@ async function subagentSummarizer(prompt: string, engine: PiEngine): Promise<{ s
usage = u;
},
});
// A provider-declared incomplete response is not a usable checkpoint,
// even when it contains nonempty text. Match Pi's compaction failure
// semantics: leave the existing history and prior summary untouched.
if (newMessages.some((message) => message.role === "assistant" && message.stopReason === "length")) {
throw new Error("Compaction summary hit the output limit and is incomplete");
}
return { summary: text, usage };
}
@@ -201,7 +207,14 @@ async function compact(deps: CompactionDeps, chatId: string): Promise<void> {
spanStart = prevKept;
priorSummary = previous.content;
}
const spanText = serializeForSummary(messages.slice(spanStart, cut));
const span = messages.slice(spanStart, cut);
// The separately supplied prior summary replaces all summary markers
// in this span. Without a resolved prior boundary, retain the existing
// legacy fallback rather than silently discarding summary-only context.
const summaryMessages = priorSummary
? span.filter((m) => m.role !== "custom" || m.customType !== "compaction")
: span;
const spanText = serializeForSummary(summaryMessages);
const fullSpan = priorSummary ? `[SYSTEM NOTE — prior compaction summary, still authoritative where not superseded]\n${priorSummary}\n\n${spanText}` : spanText;
const prompt = buildCompactionPrompt({
+10 -4
View File
@@ -245,10 +245,17 @@ export function validateChatSettings(settings: ChatSettings): SettingsValidation
return { ok: true };
}
export function validateModelsSettings(settings: ModelsSettings): SettingsValidation {
/** Validate introduced/replaced model selections, not unchanged saved pins.
* A disconnected subscription loses its available catalog, but its stored
* selections must not prevent repairing another slot or editing thinking.
* Only the SAME slot's provider+id can pass through; runtime admission stays
* separate. Callers without a stored section retain strict catalog checking. */
export function validateModelsSettings(settings: ModelsSettings, stored?: ModelsSettings): SettingsValidation {
for (const name of MODEL_SLOTS) {
const slot = settings[name];
if (!slot) continue;
const previous = stored?.[name]?.model;
if (slot.model && previous && slot.model.provider === previous.provider && slot.model.id === previous.id) continue;
const verdict = validateSlotSpec(name, slot);
if (!verdict.ok) return verdict;
}
@@ -280,9 +287,8 @@ function validateSlotSpec(name: ModelSlotName, slot: { model?: { provider: strin
// runtime — and the clamped value is what lands in the record. The
// server is the truth: the record shows the clamped level after write.
/** Clamp one level against one model (identity when the model is unknown
* or the level already supported — the existence gate rejects unknown
* models separately, so this only ever sees real ones in practice). */
/** Clamp one level against one model. Preserve the preference when metadata
* is unavailable for an unchanged saved pin; runtime clamps after reconnect. */
function clampLevelFor(provider: string, id: string, level: ThinkingLevel): ThinkingLevel {
const model = connectionModel(provider, id);
return model ? clampThinkingLevel(model, level) : level;