alpha
Login
or
Join now
hotsocket.fyi
/
niri
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
This repository has no description
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
Overview
Issues
Pulls
Pipelines
summarize via llm during compacts
author
nekomimi.pet
date
3 months ago
(Apr 18, 2026, 7:28 AM -0400)
commit
ddc2254c
ddc2254c5993d072075cf03a10326f68b0c0a03b
parent
8bc5aad9
8bc5aad95b22285c213c040aa5497da4d788fde6
+44
-1
1 changed file
Expand all
Collapse all
Unified
Split
src
runner
loop.ts
+44
-1
src/runner/loop.ts
View file
Reviewed
···
15
15
import type { LoopHooks, LoopState } from "./types.js"
16
16
import {
17
17
API_BASE,
18
18
+
CONTEXT_COMPACT_TRIGGER_TOKENS,
18
19
FALLBACK_MODEL,
19
20
FALLBACK_TOOL_CHOICE,
20
21
FALLBACK_TOKEN_NUDGE_THRESHOLD,
···
36
37
shouldFallback,
37
38
summarizeConversationViaLLM,
38
39
} from "./util.js"
40
40
+
41
41
+
const LLM_POST_TURN_RECENT_MESSAGES = 40
39
42
40
43
type FunctionToolCall = OpenAI.Chat.ChatCompletionMessageToolCall & { type: "function" }
41
44
type ToolArgs = {
···
936
939
}
937
940
938
941
/**
942
942
+
* Post-turn compaction: when observed context crosses the compaction trigger,
943
943
+
* replace the middle of the conversation with a single LLM-generated summary so
944
944
+
* older history keeps its narrative/emotional texture instead of being reduced
945
945
+
* to bullet lines. Falls back to the heuristic compactor on failure.
946
946
+
*/
947
947
+
async function applyPostTurnCompaction(state: LoopState): Promise<boolean> {
948
948
+
if (state.contextSize >= CONTEXT_COMPACT_TRIGGER_TOKENS) {
949
949
+
const summaryClient = USE_FALLBACK ? fallbackClient : client
950
950
+
const summaryModel = USE_FALLBACK ? FALLBACK_MODEL : MODEL
951
951
+
if (summaryClient && summaryModel) {
952
952
+
const beforeCount = state.conversation.length
953
953
+
const beforeEstimate = estimatePromptTokens(state.conversation)
954
954
+
const summarized = await summarizeConversationViaLLM(
955
955
+
state.conversation,
956
956
+
summaryClient,
957
957
+
summaryModel,
958
958
+
{ recentKeep: LLM_POST_TURN_RECENT_MESSAGES },
959
959
+
)
960
960
+
if (summarized) {
961
961
+
const afterEstimate = estimatePromptTokens(summarized)
962
962
+
if (afterEstimate < beforeEstimate) {
963
963
+
state.conversation = summarized
964
964
+
state.contextSize = afterEstimate
965
965
+
console.log(
966
966
+
`[context] post-turn llm-summarized (${beforeCount} -> ${summarized.length} msgs, ${beforeEstimate} -> ${afterEstimate})`,
967
967
+
)
968
968
+
return true
969
969
+
}
970
970
+
console.warn(
971
971
+
`[context] post-turn llm summary not smaller (${beforeEstimate} -> ${afterEstimate}); falling back to heuristic`,
972
972
+
)
973
973
+
} else {
974
974
+
console.warn(`[context] post-turn llm summary unavailable; falling back to heuristic`)
975
975
+
}
976
976
+
}
977
977
+
}
978
978
+
return applyRollingCompaction(state, "post-turn")
979
979
+
}
980
980
+
981
981
+
/**
939
982
* Executes the main assistant/tool loop for an active conversation.
940
983
*
941
984
* The loop repeatedly:
···
957
1000
const outcome = await processAssistantTurn(convId, state, hooks)
958
1001
if (outcome === CycleOutcome.Rest) break
959
1002
960
960
-
applyRollingCompaction(state, "post-turn")
1003
1003
+
await applyPostTurnCompaction(state)
961
1004
if (outcome !== CycleOutcome.NoTools) {
962
1005
applyContextNudge(state)
963
1006
}