MCPcopy Index your code
hub / github.com/coder/coder / tryCompactOnExit

Function tryCompactOnExit

coderd/x/chatd/chatloop/chatloop.go:1803–1829  ·  view source on GitHub ↗

tryCompactOnExit runs compaction when the chatloop is about to exit early (e.g. via ErrDynamicToolCall). The normal inline and post-run compaction paths are unreachable in early-exit scenarios, so this ensures the context window doesn't grow unbounded.

(
	ctx context.Context,
	opts RunOptions,
	usage fantasy.Usage,
	metadata fantasy.ProviderMetadata,
)

Source from the content-addressed store, hash-verified

1801// early-exit scenarios, so this ensures the context window
1802// doesn't grow unbounded.
1803func tryCompactOnExit(
1804 ctx context.Context,
1805 opts RunOptions,
1806 usage fantasy.Usage,
1807 metadata fantasy.ProviderMetadata,
1808) {
1809 if opts.Compaction == nil || opts.ReloadMessages == nil {
1810 return
1811 }
1812 reloaded, err := opts.ReloadMessages(ctx)
1813 if err != nil {
1814 return
1815 }
1816 did, compactErr := tryCompact(
1817 ctx,
1818 opts.Model,
1819 opts.Compaction,
1820 opts.ContextLimitFallback,
1821 usage,
1822 metadata,
1823 reloaded,
1824 )
1825 opts.Metrics.RecordCompaction(opts.Model.Provider(), opts.Model.Model(), did, compactErr)
1826 if compactErr != nil && opts.Compaction.OnError != nil {
1827 opts.Compaction.OnError(compactErr)
1828 }
1829}
1830
1831func isToolActive(name string, activeTools []string) bool {
1832 return len(activeTools) == 0 || slices.Contains(activeTools, name)

Callers 2

RunFunction · 0.85
executeToolsForStepFunction · 0.85

Calls 4

tryCompactFunction · 0.85
RecordCompactionMethod · 0.80
ModelMethod · 0.65
ProviderMethod · 0.45

Tested by

no test coverage detected