Skip to content

Commit 129481f

Browse files
committed
Update max_tokens to 32000, add dynamic patch length generation, and docs
1 parent f9da3a8 commit 129481f

4 files changed

Lines changed: 26 additions & 17 deletions

File tree

src/codestory/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class GlobalConfig:
4949
api_key: str | None = None
5050
api_base: str | None = None
5151
temperature: float = 0
52-
max_tokens: int | None = 4096
52+
max_tokens: int | None = 32000
5353
relevance_filtering: bool = False
5454
relevance_filter_similarity_threshold: float = 0.75
5555
secret_scanner_aggression: Literal["safe", "standard", "strict", "none"] = "safe"

src/codestory/core/semantic_analysis/summarization/chunk_summarizer.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
INITIAL_SUMMARY_USER,
4141
)
4242
from codestory.core.semantic_analysis.summarization.summarizer_utils import (
43-
DEFAULT_PATCH_CUTOFF_CHARS,
4443
generate_annotated_patch,
4544
)
4645

@@ -86,23 +85,20 @@ def __init__(
8685
context_manager: ContextManager,
8786
patch_generator: PatchGenerator,
8887
batching_strategy: Literal["auto", "requests", "prompt"] = "auto",
89-
max_tokens: int = 4096,
90-
patch_cutoff_chars: int = DEFAULT_PATCH_CUTOFF_CHARS,
88+
max_tokens: int = 32000,
9189
):
9290
"""Initialize the ChunkSummarizer.
9391
9492
Args:
9593
codestory_adapter: The CodeStoryAdapter for LLM invocation
9694
batching_strategy: Strategy for batching LLM requests
9795
max_tokens: Maximum tokens per request
98-
patch_cutoff_chars: Maximum characters per patch before truncation
9996
"""
10097
self.model = codestory_adapter
10198
self.context_manager = context_manager
10299
self.patch_generator = patch_generator
103100
self.batching_strategy = batching_strategy
104101
self.max_tokens = max_tokens
105-
self.patch_cutoff_chars = patch_cutoff_chars
106102

107103
def summarize_containers(
108104
self,
@@ -130,7 +126,7 @@ def summarize_containers(
130126
container=container,
131127
context_manager=self.context_manager,
132128
patch_generator=self.patch_generator,
133-
patch_cutoff_chars=self.patch_cutoff_chars,
129+
max_tokens=self.max_tokens,
134130
)
135131
annotated_patches.append(patch)
136132

src/codestory/core/semantic_analysis/summarization/summarizer_utils.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,19 @@
4141
)
4242

4343

44-
DEFAULT_PATCH_CUTOFF_CHARS = 1000
45-
46-
4744
def generate_annotated_patches(
4845
containers: list[AtomicContainer],
4946
context_manager: ContextManager,
5047
patch_generator: PatchGenerator,
51-
patch_cutoff_chars: int = DEFAULT_PATCH_CUTOFF_CHARS,
48+
max_tokens: int | None = None,
5249
) -> list[str]:
5350
"""Generate annotated patches for a list of containers as XML strings.
5451
5552
Args:
5653
containers: List of AtomicContainer objects
5754
context_manager: ContextManager for semantic analysis
5855
patch_generator: PatchGenerator for patch generation
59-
patch_cutoff_chars: Maximum characters per patch before truncation
56+
max_tokens: Maximum tokens for the model to use for dynamic cutoff calculation
6057
6158
Returns:
6259
List of XML-formatted annotated patches, one per container
@@ -77,7 +74,7 @@ def generate_annotated_patches(
7774
container=container,
7875
context_manager=context_manager,
7976
patch_generator=patch_generator,
80-
patch_cutoff_chars=patch_cutoff_chars,
77+
max_tokens=max_tokens,
8178
)
8279
patches.append(patch)
8380
return patches
@@ -87,7 +84,7 @@ def generate_annotated_patch(
8784
container: AtomicContainer,
8885
context_manager: ContextManager,
8986
patch_generator: PatchGenerator,
90-
patch_cutoff_chars: int = DEFAULT_PATCH_CUTOFF_CHARS,
87+
max_tokens: int | None = None,
9188
) -> str:
9289
"""Generate an XML-formatted annotated patch for a single container."""
9390
merged_container = merge_container(container)
@@ -98,7 +95,9 @@ def generate_annotated_patch(
9895
)
9996

10097
return generate_annotated_chunk_patch(
101-
annotated_container, patch_generator, patch_cutoff_chars
98+
annotated_container,
99+
patch_generator,
100+
max_tokens=max_tokens,
102101
)
103102

104103

@@ -115,7 +114,7 @@ def prioritize_longer_fqns(fqns: set[TypedFQN]) -> list[TypedFQN]:
115114
def generate_annotated_chunk_patch(
116115
annotated_container: AnnotatedContainer,
117116
patch_generator: PatchGenerator,
118-
patch_cutoff_chars: int,
117+
max_tokens: int | None = None,
119118
) -> str:
120119
"""Generate an XML-formatted annotated patch with semantic information per file.
121120
@@ -142,6 +141,20 @@ def generate_annotated_chunk_patch(
142141
path = chunk.canonical_path()
143142
groups[path].append((chunk, sig))
144143

144+
# Dynamically calculate patch cutoff if max_tokens is provided
145+
num_paths = len(groups)
146+
patch_cutoff_chars = 1000 # Default fallback
147+
if max_tokens is not None and num_paths > 0:
148+
# Use a safe offset for prompt overhead (system prompt, metadata, etc.)
149+
# The user suggests 500-1000 tokens.
150+
token_offset = 1000
151+
# Estimate 3 characters per token (consistent with chunk_summarizer estimation)
152+
chars_per_token = 3
153+
154+
tokens_per_path = (max_tokens // num_paths) - token_offset
155+
# Ensure we have a reasonable minimum per path
156+
patch_cutoff_chars = max(tokens_per_path * chars_per_token, 1000)
157+
145158
file_sections = []
146159

147160
# Sort paths for consistency

src/docs/configuration/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Below are the key configuration options available in Codestory CLI:
5858
| `api_key` | API key for the LLM provider | `None` |
5959
| `api_base` | Custom API base URL for the LLM provider (optional) | `None` |
6060
| `temperature` | Temperature for LLM responses (0.0-1.0) | `0` |
61-
| `max_tokens` | Maximum tokens to send per llm request | `4096` |
61+
| `max_tokens` | Maximum tokens to send per llm request | `32000` |
6262
| `relevance_filtering` | Whether to filter changes by relevance to your intent (`cst commit` only) | `false` |
6363
| `relevance_filter_similarity_threshold` | How similar do changes have to be to your intent to be included | `0.75` |
6464
| `secret_scanner_aggression` | How aggressively to scan for secrets (`safe`, `standard`, `strict`, `none`) | `safe` |

0 commit comments

Comments
 (0)