Skip to content

[AI] Centralize default max_tokens#205

Closed
sjsjsjjs534 wants to merge 0 commit into
bitflicker64:mainfrom
sjsjsjjs534:codex/issue-192-default-max-tokens
Closed

[AI] Centralize default max_tokens#205
sjsjsjjs534 wants to merge 0 commit into
bitflicker64:mainfrom
sjsjsjjs534:codex/issue-192-default-max-tokens

Conversation

@sjsjsjjs534

@sjsjsjjs534 sjsjsjjs534 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR centralizes the hardcoded 1500 token limit used across five long-form AI generator functions into a single exported DEFAULT_MAX_TOKENS constant in termstory/ai.py, improving maintainability and ensuring consistent default behavior across all generators.

Changes

Core

  • termstory/ai.py: Added module-level constant DEFAULT_MAX_TOKENS: int = 1500 to centralize the default token limit for long-form AI generators.
  • termstory/ai.py: Updated _send_llm_request function's default parameter from max_tokens: int = 1500 to max_tokens: int = DEFAULT_MAX_TOKENS.
  • termstory/ai.py: Updated generate_timeframe_summary to use DEFAULT_MAX_TOKENS instead of hardcoded 1500 when calling _send_llm_request.
  • termstory/ai.py: Updated generate_wrapped_summary to use DEFAULT_MAX_TOKENS instead of hardcoded 1500 when calling _send_llm_request.
  • termstory/ai.py: Updated translate_git_anger to use DEFAULT_MAX_TOKENS instead of hardcoded 1500 when calling _send_llm_request.
  • termstory/ai.py: Updated predict_bugs_from_sessions to use DEFAULT_MAX_TOKENS instead of hardcoded 1500 when calling _send_llm_request.
  • termstory/ai.py: Updated generate_rpg_bio to use DEFAULT_MAX_TOKENS instead of hardcoded 1500 when calling _send_llm_request.

Tests

  • tests/test_ai.py: Added imports for sys, SimpleNamespace, and import termstory.ai as ai to support the new regression test.
  • tests/test_ai.py: Updated existing test test_timeframe_summary_max_tokens to reference ai.DEFAULT_MAX_TOKENS instead of the hardcoded value 1500 in the assertion.
  • tests/test_ai.py: Added new regression test test_long_form_generators_share_default_max_tokens that patches DEFAULT_MAX_TOKENS to 1234 and verifies all five long-form generators (generate_timeframe_summary, generate_wrapped_summary, translate_git_anger, predict_bugs_from_sessions, generate_rpg_bio) pick up the changed value.

Fixes

Breaking Changes

None

Testing

Run the AI test suite with isolated config directories:

XDG_CONFIG_HOME=/private/tmp/termstory-test-config XDG_DATA_HOME=/private/tmp/termstory-test-data .venv/bin/python -m pytest tests/test_ai.py -q

Run the full test suite:

XDG_CONFIG_HOME=/private/tmp/termstory-test-config XDG_DATA_HOME=/private/tmp/termstory-test-data .venv/bin/python -m pytest tests/ -q

Notes

The constant DEFAULT_MAX_TOKENS is exported at the module level, making it available for external configuration or testing purposes. The regression test uses monkeypatch.setattr with raising=False to override the constant at runtime, ensuring that all five generators consistently reference the shared default rather than hardcoded values.

@git-miku git-miku Bot added the do-not-merge/needs-star PR author has not starred this repo — run /check-star after starring label Jul 2, 2026
@git-miku

git-miku Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

👋 Thanks for the PR, @sjsjsjjs534!

To get this PR reviewed, please ⭐ star this repo — it's free and helps others discover it. Once starred, run :miku /check-star on this PR and I'll start helping out.

A do-not-merge/needs-star label has been applied to block merge until then.

@greptile-apps

greptile-apps Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR replaces five hardcoded 1500 token limits across long-form AI generators with a single exported DEFAULT_MAX_TOKENS = 1500 constant in termstory/ai.py, and updates the test suite to verify the shared constant is picked up dynamically.

  • termstory/ai.py: Adds DEFAULT_MAX_TOKENS: int = 1500 at module scope; updates _send_llm_request's default parameter and the five call sites in generate_timeframe_summary, generate_wrapped_summary, translate_git_anger, predict_bugs_from_sessions, and generate_rpg_bio.
  • tests/test_ai.py: Converts the existing test_timeframe_summary_max_tokens assertion to reference ai.DEFAULT_MAX_TOKENS and adds test_long_form_generators_share_default_max_tokens, which monkeypatches the constant to 1234 and verifies all five generators propagate the override end-to-end.

Confidence Score: 5/5

Safe to merge — purely mechanical replacement of five hardcoded literals with a named constant; no logic changes.

Every target call site was updated and verified against the diff. The two intentionally distinct callers (500 and 2000 tokens) are correctly left unchanged. The new regression test exercises dynamic module-level lookup for all five generators, confirming the constant is not baked in at compile time. No behaviour changes in production paths.

No files require special attention.

Important Files Changed

Filename Overview
termstory/ai.py Adds DEFAULT_MAX_TOKENS constant and updates all five target call sites; no remaining hardcoded 1500 in generator bodies; two intentionally distinct call sites (500 and 2000) are correctly left untouched.
tests/test_ai.py Existing max_tokens assertion updated to reference the constant; new regression test correctly verifies all five generators do a module-level lookup (not a compile-time bind) of DEFAULT_MAX_TOKENS.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    C[DEFAULT_MAX_TOKENS = 1500]
    C --> A[_send_llm_request\ndefault param]
    C --> B1[generate_timeframe_summary]
    C --> B2[generate_wrapped_summary]
    C --> B3[translate_git_anger]
    C --> B4[predict_bugs_from_sessions]
    C --> B5[generate_rpg_bio]

    B1 -->|max_tokens=DEFAULT_MAX_TOKENS| R[_send_llm_request]
    B2 -->|max_tokens=DEFAULT_MAX_TOKENS| R
    B3 -->|max_tokens=DEFAULT_MAX_TOKENS| R
    B4 -->|max_tokens=DEFAULT_MAX_TOKENS| R
    B5 -->|max_tokens=DEFAULT_MAX_TOKENS| R

    X1[generate_ai_summary] -->|max_tokens=500| R
    X2[generate_daily_chronicle] -->|max_tokens=2000| R
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    C[DEFAULT_MAX_TOKENS = 1500]
    C --> A[_send_llm_request\ndefault param]
    C --> B1[generate_timeframe_summary]
    C --> B2[generate_wrapped_summary]
    C --> B3[translate_git_anger]
    C --> B4[predict_bugs_from_sessions]
    C --> B5[generate_rpg_bio]

    B1 -->|max_tokens=DEFAULT_MAX_TOKENS| R[_send_llm_request]
    B2 -->|max_tokens=DEFAULT_MAX_TOKENS| R
    B3 -->|max_tokens=DEFAULT_MAX_TOKENS| R
    B4 -->|max_tokens=DEFAULT_MAX_TOKENS| R
    B5 -->|max_tokens=DEFAULT_MAX_TOKENS| R

    X1[generate_ai_summary] -->|max_tokens=500| R
    X2[generate_daily_chronicle] -->|max_tokens=2000| R
Loading

Reviews (3): Last reviewed commit: "Potential fix for pull request finding" | Re-trigger Greptile

Comment thread tests/test_ai.py Outdated
monkeypatch.setattr(urllib.request, "urlopen", mock_urlopen)
monkeypatch.setattr("termstory.config.load_config", lambda: {})
monkeypatch.setattr(ai, "_get_all_active_project_contexts", lambda: [])
monkeypatch.setattr(ai, "DEFAULT_MAX_TOKENS", 1234, raising=False)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The raising=False flag is not needed here since DEFAULT_MAX_TOKENS is definitively exported from termstory.ai. With raising=False, a typo in the attribute name (e.g. DEFAULT_MAX_TOKEN) would silently set a new attribute on the module instead of patching the existing one, the generators would still read the old 1500 value, and all five assertions would fail with 1500 != 1234 — but the monkeypatch itself would give no warning. Dropping the flag makes the intent clear and lets pytest raise immediately if the name is ever wrong.

Suggested change
monkeypatch.setattr(ai, "DEFAULT_MAX_TOKENS", 1234, raising=False)
monkeypatch.setattr(ai, "DEFAULT_MAX_TOKENS", 1234)

@bitflicker64

Copy link
Copy Markdown
Owner

miku /check-star

@bitflicker64 bitflicker64 reopened this Jul 2, 2026
@git-miku

git-miku Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

👋 Thanks for the PR, @sjsjsjjs534!

To get this PR reviewed, please ⭐ star this repo — it's free and helps others discover it. Once starred, run :miku /check-star on this PR and I'll start helping out.

A do-not-merge/needs-star label has been applied to block merge until then.

@bitflicker64

Copy link
Copy Markdown
Owner

/rewrite

@git-miku

git-miku Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

miku /rewrite

✅ PR description rewritten! Check the PR body above.

Preview

Summary

This PR centralizes the hardcoded 1500 token limit used across five long-form AI generator functions into a single exported DEFAULT_MAX_TOKENS constant in termstory/ai.py, improving maintainability and ensuring consistent default behavior across all generators.

Changes

Core

  • termstory/ai.py: Added module-level constant DEFAULT_MAX_TOKENS: int = 1500 to centralize the default token limit for long-form AI generators.
  • termstory/ai.py: Updated _send_llm_request function's default parameter from max_tokens: int = 1500 to max_tokens: int = DEFAULT_MAX_TOKENS.
  • termstory/ai.py: Updated generate_timeframe_summary to use DEFAULT_MAX_TOKENS instead of hardcoded 1500 when calling _send_llm_request.
  • termstory/ai.py: Updated generate_wrapped_summary to use DEFAULT_MAX_TOKENS instead of hardcoded 1500 when calling _send_llm_request.
  • termstory/ai.py: Updated translate_git_anger to use DEFAULT_MAX_TOKENS instead of hardcoded 1500 when calling _send_llm_request.
  • termstory/ai.py: Updated predict_bugs_from_sessions to use DEFAULT_MAX_TOKENS instead of hardcoded 1500 when calling _send_llm_request.
  • termstory/ai.py: Updated generate_rpg_bio to use DEFAULT_MAX_TOKENS instead of hardcoded 1500 when calling _send_llm_request.

Tests

  • tests/test_ai.py: Added imports for sys, SimpleNamespace, and import termstory.ai as ai to support the new regression test.
  • tests/test_ai.py: Updated existing test test_timeframe_summary_max_tokens to reference ai.DEFAULT_MAX_TOKENS instead of the hardcoded value 1500 in the assertion.
  • tests/test_ai.py: Added new regression test test_long_form_generators_share_default_max_tokens that patches DEFAULT_MAX_TOKENS to 1234 and verifies all five long-form generators (generate_timeframe_summary, generate_wrapped_summary, translate_git_anger, predict_bugs_from_sessions, generate_rpg_bio) pick up the changed value.

Fixes

Breaking Changes

None

Testing

Run the AI test suite with isolated config directories:

XDG_CONFIG_HOME=/private/tmp/termstory-test-config XDG_DATA_HOME=/private/tmp/termstory-test-data .venv/bin/python -m pytest tests/test_ai.py -q

Run the full test suite:

XDG_CONFIG_HOME=/private/tmp/termstory-test-config XDG_DATA_HOME=/private/tmp/termstory-test-data .venv/bin/python -m pytest tests/ -q

Notes

The constant DEFAULT_MAX_TOKENS is exported at the module level, making it available for external configuration or testing purposes. The regression test uses monkeypatch.setattr with raising=False to override the constant at runtime, ensuring that all five generators consistently reference the shared default rather than hardcoded values.

@bitflicker64

Copy link
Copy Markdown
Owner

/triage

@git-miku git-miku Bot added the triage/triaged This PR has been triaged by a maintainer label Jul 3, 2026
@git-miku

git-miku Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

miku /triage

✅ Marked as triaged (triage/triaged applied, needs-triage removed).

Comment thread termstory/ai.py
Comment thread termstory/ai.py

# Module-level defaults — used when config is unavailable.
DEFAULT_MAX_TOKENS: int = 1500
_DEFAULT_MAX_FAILURES: int = 3

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_DEFAULT_MAX_FAILURES: int = 3
# Module-level defaults — used when config is unavailable.
# Note: DEFAULT_MAX_TOKENS is public (no underscore prefix) because tests
# override it via monkeypatch.setattr. The other defaults stay private.
DEFAULT_MAX_TOKENS: int = 1500
_DEFAULT_MAX_FAILURES: int = 3
_DEFAULT_COOLDOWN_SECONDS: float = 60.0

The naming is inconsistent: DEFAULT_MAX_TOKENS (public) vs _DEFAULT_MAX_FAILURES (private). Either the public one should also be private, or the others should be public. Currently DEFAULT_MAX_TOKENS is the only public constant and the convention isn't clear.

Comment thread tests/test_ai.py
resp_payload = {"choices": [{"message": {"content": "ok"}}]}
return MockResponse(json.dumps(resp_payload).encode("utf-8"))

monkeypatch.setattr(urllib.request, "urlopen", mock_urlopen)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test test_long_form_generators_share_default_max_tokens (75 lines) overlaps with the existing test_default_max_tokens_propagates_to_request_body test, which already verifies that DEFAULT_MAX_TOKENS propagates to the request body. The new one exercises generate_rpg_bio and 4 other generators — that's useful but could be folded into the existing test as additional assertions rather than a separate test that mostly duplicates the assertion pattern.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR centralizes the long-form AI generators’ default max_tokens value by introducing a single exported constant (DEFAULT_MAX_TOKENS) in termstory/ai.py, then updates the generators and tests to reference it for consistency and easier tuning.

Changes:

  • Added DEFAULT_MAX_TOKENS in termstory/ai.py and replaced hardcoded 1500 at the five long-form generator call sites.
  • Updated _send_llm_request’s default max_tokens parameter to reference the new constant.
  • Updated tests/test_ai.py to assert against ai.DEFAULT_MAX_TOKENS and added a regression test intended to ensure all long-form generators share the same default.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
termstory/ai.py Introduces DEFAULT_MAX_TOKENS and updates long-form generator calls (and _send_llm_request default) to use it.
tests/test_ai.py Updates assertions to use the constant and adds a regression test for shared defaults.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread termstory/ai.py
Comment on lines 168 to 172
model_name: str,
provider: str,
max_tokens: int = 1500,
max_tokens: int = DEFAULT_MAX_TOKENS,
timeout: float = 30.0
) -> Optional[str]:
Comment thread tests/test_ai.py Outdated
@git-miku

git-miku Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

👋 Thanks for the PR, @sjsjsjjs534!

To get this PR reviewed, please ⭐ star this repo — it's free and helps others discover it. Once starred, run :miku /check-star on this PR and I'll start helping out.

A do-not-merge/needs-star label has been applied to block merge until then.

@bitflicker64

Copy link
Copy Markdown
Owner

@copilot resolve the merge conflicts in this pull request

@sjsjsjjs534 sjsjsjjs534 closed this Jul 7, 2026
@sjsjsjjs534 sjsjsjjs534 force-pushed the codex/issue-192-default-max-tokens branch from 948eed3 to 58363e0 Compare July 7, 2026 16:15
@greptile-apps

greptile-apps Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

No reviewable files after applying ignore patterns.

@git-miku

git-miku Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

👋 Thanks for the PR, @sjsjsjjs534!

To get this PR reviewed, please ⭐ star this repo — it's free and helps others discover it. Once starred, run :miku /check-star on this PR and I'll start helping out.

A do-not-merge/needs-star label has been applied to block merge until then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/needs-star PR author has not starred this repo — run /check-star after starring triage/triaged This PR has been triaged by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Easy] ai.py: Hardcoded max_tokens=1500 duplicated across 5 call sites

3 participants