-
Notifications
You must be signed in to change notification settings - Fork 0
fix: correct usage analytics and plan detection #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arafa-dev
wants to merge
8
commits into
main
Choose a base branch
from
codex/usage-analytics-correctness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8efc59e
fix(scanner): dedup usage by message.id, not per-line uuid
arafa-dev 37f3c14
fix(storage): merge duplicate events keeping max output_tokens
arafa-dev 48e950a
feat(storage): schema v3 resets events to rebuild deduped usage
arafa-dev a94d942
feat(plandetect): detect plan tier from local Claude config
arafa-dev b2cd761
feat(quotawire): auto-detect plan tier, overriding manual config
arafa-dev 785ced3
feat(cli): split cache out of usage --quota headline tokens
arafa-dev aa158eb
fix(storage): preserve session ownership during v3 usage reset
arafa-dev 5a769d6
fix(run): satisfy process launcher lint
arafa-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package plandetect | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| type claudeConfig struct { | ||
| OAuthAccount struct { | ||
| OrganizationType string `json:"organizationType"` | ||
| OrganizationRateLimit string `json:"organizationRateLimitTier"` | ||
| } `json:"oauthAccount"` | ||
| } | ||
|
|
||
| // Detect reads <configDir>/.claude.json and returns the ccx plan tier ("pro") | ||
| // inferred from oauthAccount.organizationType. ok is false when the file is | ||
| // missing/unreadable or the type is unrecognized, in which case the caller | ||
| // should fall back to manually configured limits. | ||
| func Detect(configDir string) (tier string, ok bool) { | ||
| if configDir == "" { | ||
| return "", false | ||
| } | ||
| // #nosec G304 -- configDir is a local Claude profile path selected by the user. | ||
| b, err := os.ReadFile(filepath.Join(configDir, ".claude.json")) | ||
| if err != nil { | ||
| return "", false | ||
| } | ||
| var cfg claudeConfig | ||
| if err := json.Unmarshal(b, &cfg); err != nil { | ||
| return "", false | ||
| } | ||
| return mapOrgType(cfg.OAuthAccount.OrganizationType, cfg.OAuthAccount.OrganizationRateLimit) | ||
| } | ||
|
|
||
| func mapOrgType(orgType, _ string) (string, bool) { | ||
| switch strings.ToLower(strings.TrimSpace(orgType)) { | ||
| case "claude_pro": | ||
| return "pro", true | ||
| default: | ||
| return "", false | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package plandetect | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func writeClaudeJSON(t *testing.T, dir, body string) { | ||
| t.Helper() | ||
| if err := os.WriteFile(filepath.Join(dir, ".claude.json"), []byte(body), 0o600); err != nil { | ||
| t.Fatalf("write: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestDetectPro(t *testing.T) { | ||
| dir := t.TempDir() | ||
| writeClaudeJSON(t, dir, `{"oauthAccount":{"organizationType":"claude_pro","organizationRateLimitTier":"default_claude_ai"}}`) | ||
| tier, ok := Detect(dir) | ||
| if !ok || tier != "pro" { | ||
| t.Fatalf("want pro,true; got %q,%v", tier, ok) | ||
| } | ||
| } | ||
|
|
||
| func TestDetectUnknownFallsBack(t *testing.T) { | ||
| dir := t.TempDir() | ||
| writeClaudeJSON(t, dir, `{"oauthAccount":{"organizationType":"something_new"}}`) | ||
| if tier, ok := Detect(dir); ok { | ||
| t.Fatalf("unknown type must return ok=false; got %q,%v", tier, ok) | ||
| } | ||
| } | ||
|
|
||
| func TestDetectMissingFile(t *testing.T) { | ||
| if tier, ok := Detect(t.TempDir()); ok { | ||
| t.Fatalf("missing file must return ok=false; got %q,%v", tier, ok) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // Package plandetect resolves a profile's Anthropic plan tier from the local | ||
| // Claude Code config (<config_dir>/.claude.json) instead of manual setup. It | ||
| // is a leaf package: it reads a file and maps a string, with no sibling | ||
| // imports. | ||
| package plandetect |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
Detectperforms file I/O but does not accept acontext.Context.Detectreads.claude.jsonviaos.ReadFile, so per the project convention it should takectx context.Contextas its first parameter (and can short-circuit onctx.Err()before the read). This also lets thequotawirecaller propagate its request context.♻️ Add ctx to the signature and propagate from the caller
Add
"context"to imports. Downstream caller ininternal/quotawire/adapter.go(Line 48) becomes:and the tests in
detect_test.goneed thectxargument as well.As per coding guidelines: "Every I/O or blocking public function must take
ctx context.Contextas its first parameter".📝 Committable suggestion
🤖 Prompt for AI Agents