ecoportal-api-v2 3.3.2 → 3.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.ai-assistance/code/double_model_patterns.md +133 -0
- data/.ai-assistance/conventions/code-working-tree-protocol.md +176 -0
- data/.ai-assistance/scripts/token-logger.js +220 -0
- data/.ai-assistance/scripts/token-report.ts +158 -0
- data/.ai-assistance/scripts/token-session-start.js +66 -0
- data/.ai-assistance/skills/eP_AI_Manager/SKILL.md +262 -0
- data/.ai-assistance/token-budget.json +44 -0
- data/.claude/settings.json +107 -0
- data/.gitignore +2 -0
- data/CHANGELOG.md +11 -5
- data/lib/ecoportal/api/common/content/double_model/attributable/nesting/cascaded_callback.rb +1 -1
- data/lib/ecoportal/api/v2_version.rb +1 -1
- data/scripts/auto-worker-scheduler.sh +307 -0
- metadata +10 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f6d039e12072cb81b7229475b99b9f8d460375679130af1fe9272a2d2c1241ad
|
|
4
|
+
data.tar.gz: 347a85aa42ac89cffe29579abc90d35a4e30e16825a73b30b544cfe57abc3948
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f1a68e3d165c7fbb18d1136b423c79250561de8d29c87851a001a7cd7f431ccbef872d3c80b3b2ae4769faf491e985398cfb89962b71186c9efa22efe8ceda1f
|
|
7
|
+
data.tar.gz: 0a19c1a759679221430cf50897ba75da1c65b594684a35e06aa4e8ab1b357fbf267eca53aab4d5805c18ce161d7c5dea988ec957d7a627eadc50751529935b13
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# Code Spec: DoubleModel Patterns
|
|
2
|
+
|
|
3
|
+
**Scope:** Core patterns in `ecoportal-api-v2` that downstream gems (especially `ecoportal-api-graphql`) depend on.
|
|
4
|
+
**Last updated:** 2026-06-05
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## DoubleModel Architecture
|
|
9
|
+
|
|
10
|
+
`DoubleModel` is the base class for all model objects. It stores data in two hashes:
|
|
11
|
+
- `@doc` — current state (mutated by setters)
|
|
12
|
+
- `@original_doc` — snapshot at initialization time (never mutated)
|
|
13
|
+
|
|
14
|
+
Both are created via `JSON.parse(doc.to_json)` — meaning **keys are always STRING**.
|
|
15
|
+
This matters for any code that compares current vs original documents.
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
def initialize(doc = {}, parent: self, key: nil, read_only: ...)
|
|
19
|
+
@doc = JSON.parse(doc.to_json) # string keys
|
|
20
|
+
@original_doc = JSON.parse(@doc.to_json) # string keys, snapshot
|
|
21
|
+
end
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Key rule: `change_data` uses symbol keys for comparison
|
|
25
|
+
|
|
26
|
+
`HashDiffNesting#change_data` converts keys to symbols (`key.to_sym`) before comparing.
|
|
27
|
+
If you call it with hashes that have **string keys** (as `DoubleModel` docs do), the lookup
|
|
28
|
+
`b.key?(:name)` will ALWAYS be false (the hash has `'name'`, not `:name`).
|
|
29
|
+
|
|
30
|
+
**Consequence:** Always normalise both `curr` and `prev` to symbol keys before passing to
|
|
31
|
+
`change_data`. See `ecoportal-api-graphql`'s `DiffService#classic_diff` fix.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Field Definition Macros
|
|
36
|
+
|
|
37
|
+
| Macro | Effect |
|
|
38
|
+
|-------|--------|
|
|
39
|
+
| `passkey :fieldName` | ID field — registered as `key_method`, used for array lookups and `get_id` |
|
|
40
|
+
| `passthrough :fieldName` | Getter + setter mapping to `@doc['fieldName']` |
|
|
41
|
+
| `passboolean :fieldName` | Same as passthrough, coerces to Boolean |
|
|
42
|
+
| `passarray :fieldName` | Creates an `ArrayModel` wrapping `@doc['fieldName']` |
|
|
43
|
+
| `passdate :fieldName` | DateTime passthrough |
|
|
44
|
+
| `embeds_one :fieldName, klass:` | Lazy single-object embed; registers as cascaded attribute |
|
|
45
|
+
| `embeds_many :fieldName, klass:` | Lazy collection embed; registers as cascaded attribute |
|
|
46
|
+
| `root!` | Marks the class as a "root" (lookup-only) object — excluded from parent cascaded diffs |
|
|
47
|
+
| `read_only!` | Marks the class as read-only |
|
|
48
|
+
|
|
49
|
+
### Lazy embed initialisation (IMPORTANT)
|
|
50
|
+
|
|
51
|
+
`embeds_one` and `embeds_many` getters auto-initialize `@doc['fieldName']` to `{}` or `[]`
|
|
52
|
+
when accessed for the first time (`doc[obj_k] ||= multiple ? [] : {}`).
|
|
53
|
+
|
|
54
|
+
**Side effect:** Accessing any embedded field mutates `@doc`. Any code that calls `as_json`
|
|
55
|
+
or reads `@doc` AFTER a getter call will see auto-created empty objects, even if they were
|
|
56
|
+
not in the original hash. This is relevant when computing diffs.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Cascaded Attributes
|
|
61
|
+
|
|
62
|
+
Cascaded attributes are embedded fields registered via `embeds_one` / `embeds_many`.
|
|
63
|
+
They appear in `_cascaded_attributes` as `{method_name => doc_key}`.
|
|
64
|
+
|
|
65
|
+
`_cascaded_doc_keys` returns the string doc keys of all cascaded attributes — used by
|
|
66
|
+
`DiffService` to strip them from the flat diff before comparison.
|
|
67
|
+
|
|
68
|
+
### `cascaded_reduce` (v3.3.3+)
|
|
69
|
+
|
|
70
|
+
Iterates over the subject and all its cascaded attribute objects:
|
|
71
|
+
|
|
72
|
+
```ruby
|
|
73
|
+
subject.cascaded_reduce(init) do |result, obj, key, key_path, trace|
|
|
74
|
+
# key_path = [] for top-level subject
|
|
75
|
+
# key_path = ['fieldName'] for first-level embeds
|
|
76
|
+
...
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Ruby 3.x compatibility note:** The block parameter pattern in `_cascaded_attributes_trace`
|
|
81
|
+
was `|out, (attribute, obj_k)|` (WRONG — swapped) and was corrected to `|(attribute, obj_k), out|`
|
|
82
|
+
in commit `6a2b1b5`. If you see TypeError about the trace hash being passed to `send`,
|
|
83
|
+
check this method.
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## `as_update` on DoubleModel (ecoportal-api-v2)
|
|
88
|
+
|
|
89
|
+
`DoubleModel` in ecoportal-api-v2 has its own `as_update` (no kwargs). This is DIFFERENT
|
|
90
|
+
from `ecoportal-api-graphql`'s `Common::GraphQL::Model#as_update(**kargs)`.
|
|
91
|
+
|
|
92
|
+
When `DiffService#diff_reduce` calls `obj.as_update(ignore: [...])`, only call it on objects
|
|
93
|
+
that are `Common::GraphQL::Model` instances (the GraphQL gem's version with keyword args).
|
|
94
|
+
`ArrayModel` and other plain `DoubleModel` subclasses use the no-kwargs version and must be
|
|
95
|
+
skipped.
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## `ArrayModel` (created by `passarray`)
|
|
100
|
+
|
|
101
|
+
`passarray :fieldName` creates an `ArrayModel::FieldName` subclass inheriting from
|
|
102
|
+
`Common::Content::ArrayModel < DoubleModel`. It wraps an array of primitive values (IDs, strings).
|
|
103
|
+
|
|
104
|
+
`ArrayModel` instances:
|
|
105
|
+
- Respond to `as_update` (the ecoportal-api-v2 no-kwargs version)
|
|
106
|
+
- Are registered as cascaded attributes
|
|
107
|
+
- Should NOT be diff'd by `DiffService` (they're ID arrays, not model objects)
|
|
108
|
+
- Their changes are captured via the flat diff (`passarray` fields are scalar arrays)
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## `HashHelpers` (in DoubleModel)
|
|
113
|
+
|
|
114
|
+
Defines `dig_path?`, `dig_set!`, `dig_delete!` for nested hash navigation.
|
|
115
|
+
These are NOT included in plain service classes. If your class needs them, include
|
|
116
|
+
`Ecoportal::API::Common::Content::DoubleModel::HashHelpers::InstanceMethods` or
|
|
117
|
+
define them locally (see `ecoportal-api-graphql`'s `HashDiffNesting` for a copy).
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Known Issues / Gotchas
|
|
122
|
+
|
|
123
|
+
1. **String vs symbol keys**: `DoubleModel` always uses string keys in `@doc` / `@original_doc`.
|
|
124
|
+
Any comparison against symbol keys will silently fail to match. Always normalise before comparing.
|
|
125
|
+
|
|
126
|
+
2. **Lazy embed mutation**: Accessing `model.fieldName` on a model that didn't have that field
|
|
127
|
+
in the original doc adds `'fieldName' => {}` to `@doc`. This pollutes `as_json` output.
|
|
128
|
+
|
|
129
|
+
3. **`cascaded_reduce` block parameters**: Must use `|(element, obj_k), accumulator|` pattern
|
|
130
|
+
(not `|accumulator, (element, obj_k)|`). Ruby 3.x strict argument handling exposed this.
|
|
131
|
+
|
|
132
|
+
4. **`as_update` in ecoportal-api-v2 takes no kwargs**. Only `ecoportal-api-graphql`'s
|
|
133
|
+
`Common::GraphQL::Model#as_update` accepts `ignore:` keyword argument.
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# Code Working Tree Protocol
|
|
2
|
+
|
|
3
|
+
When Claude Code needs to make changes to files **outside** `bridge/inbox/`, it must
|
|
4
|
+
follow this protocol. This prevents Code's changes from mixing with in-progress CoWork
|
|
5
|
+
edits and ensures a clean, traceable commit history.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## When this applies
|
|
10
|
+
|
|
11
|
+
Any time Code intends to modify files in the working tree that are not bridge task files
|
|
12
|
+
(i.e., not `.ai-assistance/bridge/inbox/` or `.ai-assistance/bridge/outbox/`).
|
|
13
|
+
|
|
14
|
+
This includes: editing source files, updating documentation, changing scripts,
|
|
15
|
+
modifying capabilities files, etc.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Protocol
|
|
20
|
+
|
|
21
|
+
### 0. Check for a lock
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
cat .ai-assistance/bridge/LOCK 2>/dev/null || echo "NO_LOCK"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- **No lock:** proceed to step 1
|
|
28
|
+
- **Lock exists, EXPIRES is in the future:** stop. Tell the user:
|
|
29
|
+
> "Working tree is locked by [AGENT] ([USER]) since [ACQUIRED], working on: [INTENT].
|
|
30
|
+
> Expires at [EXPIRES]. Please wait or check if the other session is still active."
|
|
31
|
+
- **Lock exists, EXPIRES is in the past:** stale lock — safe to overwrite, proceed to step 1
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
### 1. Acquire the lock
|
|
36
|
+
|
|
37
|
+
Write `.ai-assistance/bridge/LOCK` with full watermark:
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
AGENT: code
|
|
41
|
+
USER: [git config user.name, lowercased]
|
|
42
|
+
ACQUIRED: [ISO 8601 now]
|
|
43
|
+
EXPIRES: [ISO 8601 now + 30 minutes]
|
|
44
|
+
INTENT: [one sentence — what you are about to change and why]
|
|
45
|
+
FILES: [comma-separated list of files you plan to modify]
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Example:
|
|
49
|
+
```
|
|
50
|
+
AGENT: code
|
|
51
|
+
USER: oscar
|
|
52
|
+
ACQUIRED: 2026-06-04T10:00:00Z
|
|
53
|
+
EXPIRES: 2026-06-04T10:30:00Z
|
|
54
|
+
INTENT: Update gitlab-mcp.md with new PAT scopes and rotation info
|
|
55
|
+
FILES: .ai-assistance/integrations/gitlab-mcp.md
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
### 2. Check for unstaged changes that overlap with your planned files
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
git status --short
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
If the working tree is clean, skip to step 3.
|
|
67
|
+
|
|
68
|
+
If there are unstaged/staged changes, compare them against the files listed in your LOCK:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
git diff --name-only HEAD
|
|
72
|
+
git diff --cached --name-only
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
- **No overlap with your FILES:** proceed — the changes are unrelated and won't pollute history
|
|
76
|
+
- **Overlap with one or more of your FILES:** commit the unstaged changes first.
|
|
77
|
+
Derive the commit message by running `git diff HEAD` on the overlapping files and
|
|
78
|
+
writing a short imperative summary of what actually changed — do not use a generic
|
|
79
|
+
message. Format: `wip: <what changed, e.g. "rename .claude to .ai-assistance across scripts">`
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
git add -A
|
|
83
|
+
git commit -m "wip: <derived from actual diff>"
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
This keeps Code's subsequent commit clean and ensures both sets of changes build
|
|
87
|
+
on the correct base. On a feature branch, `wip:` commits are fine — squash before MR.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
### 3. Apply your changes
|
|
92
|
+
|
|
93
|
+
Make the intended file edits. Stay within the scope declared in INTENT and FILES
|
|
94
|
+
when you acquired the lock. If scope expands, update the LOCK file before proceeding.
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
### 4. Commit your changes
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
git add -A
|
|
102
|
+
git commit -m "[descriptive message — what Code changed and why]"
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Commit message should be specific enough that a teammate can understand the change
|
|
106
|
+
without reading the diff. Example:
|
|
107
|
+
```
|
|
108
|
+
docs: update gitlab-mcp.md scopes and rotation info for new PAT (April 2027 expiry)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**Commit authorship — developer only by default:**
|
|
112
|
+
|
|
113
|
+
Commits are authored by the developer alone (git's `user.name` / `user.email` config).
|
|
114
|
+
Do NOT add `Co-Authored-By: Claude ...` to commit messages unless the developer
|
|
115
|
+
explicitly requests it.
|
|
116
|
+
|
|
117
|
+
Rationale: the commit history is the developer's professional record. Co-authorship is
|
|
118
|
+
opt-in, not opt-out. If the developer wants to attribute AI involvement, they can add
|
|
119
|
+
it themselves or ask Claude to include it for a specific commit.
|
|
120
|
+
|
|
121
|
+
Before adding any co-authorship attribution, ask:
|
|
122
|
+
> "Would you like to add AI co-authorship to this commit, or keep it as your commit alone?"
|
|
123
|
+
|
|
124
|
+
Default answer if not asked: **developer only**.
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
### 5. Release the lock
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
rm .ai-assistance/bridge/LOCK
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## Quick reference
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
# 0. Check lock
|
|
140
|
+
cat .ai-assistance/bridge/LOCK 2>/dev/null || echo "NO_LOCK"
|
|
141
|
+
|
|
142
|
+
# 1. Acquire lock
|
|
143
|
+
cat > .ai-assistance/bridge/LOCK << EOF
|
|
144
|
+
AGENT: code
|
|
145
|
+
USER: oscar
|
|
146
|
+
ACQUIRED: $(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
|
147
|
+
EXPIRES: $(date -u -d "+30 minutes" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date -u -v+30M +"%Y-%m-%dT%H:%M:%SZ")
|
|
148
|
+
INTENT: <what you are changing>
|
|
149
|
+
FILES: <files>
|
|
150
|
+
EOF
|
|
151
|
+
|
|
152
|
+
# 2. Check for overlapping unstaged changes
|
|
153
|
+
git diff --name-only HEAD && git diff --cached --name-only
|
|
154
|
+
# If any of those files overlap with your planned FILES → commit them first:
|
|
155
|
+
git add -A && git commit -m "wip: <description of CoWork's in-progress work>"
|
|
156
|
+
# If no overlap → skip, proceed directly
|
|
157
|
+
|
|
158
|
+
# 3. Apply changes
|
|
159
|
+
# ... make edits ...
|
|
160
|
+
|
|
161
|
+
# 4. Commit your changes
|
|
162
|
+
git add -A && git commit -m "<descriptive message>"
|
|
163
|
+
|
|
164
|
+
# 5. Release lock
|
|
165
|
+
rm .ai-assistance/bridge/LOCK
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## Notes
|
|
171
|
+
|
|
172
|
+
- If Code crashes mid-protocol, the LOCK will expire naturally (30 min timeout)
|
|
173
|
+
- The `wip:` commit prefix signals to teammates that this was an auto-committed
|
|
174
|
+
in-progress state — safe to squash or amend later
|
|
175
|
+
- This protocol does not apply to bridge task processing (reading inbox, writing outbox)
|
|
176
|
+
— those are read/write of bridge files only and don't touch the working tree
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* token-logger.js
|
|
4
|
+
*
|
|
5
|
+
* Claude Code Stop hook — fires after every AI response turn.
|
|
6
|
+
* Reads the session transcript, extracts token usage, accumulates weekly totals,
|
|
7
|
+
* and warns when approaching the project's budget allocation.
|
|
8
|
+
*
|
|
9
|
+
* Wired in .claude/settings.json:
|
|
10
|
+
* "Stop": [{ "type": "command", "command": "node .ai-assistance/scripts/token-logger.js" }]
|
|
11
|
+
*
|
|
12
|
+
* Reads: stdin (Stop event JSON with session_id, transcript_path, cwd)
|
|
13
|
+
* .ai-assistance/token-budget.json
|
|
14
|
+
* .ai-assistance/local/kpi/session-<id>.json (running session state)
|
|
15
|
+
* .ai-assistance/local/kpi/weekly-<YYYY-WNN>.json (weekly totals)
|
|
16
|
+
*
|
|
17
|
+
* Writes: .ai-assistance/local/kpi/session-<id>.json (updated state)
|
|
18
|
+
* .ai-assistance/local/kpi/weekly-<YYYY-WNN>.json (updated totals)
|
|
19
|
+
* .ai-assistance/local/kpi/sessions-<YYYY-WNN>.jsonl (completed turns)
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const fs = require("fs");
|
|
23
|
+
const path = require("path");
|
|
24
|
+
const os = require("os");
|
|
25
|
+
|
|
26
|
+
// ── Helpers ────────────────────────────────────────────────────────────────
|
|
27
|
+
|
|
28
|
+
function isoWeek(d) {
|
|
29
|
+
const jan4 = new Date(d.getFullYear(), 0, 4);
|
|
30
|
+
const startOfWeek = new Date(jan4);
|
|
31
|
+
startOfWeek.setDate(jan4.getDate() - ((jan4.getDay() + 6) % 7));
|
|
32
|
+
const weekNum = Math.ceil(((d - startOfWeek) / 86400000 + 1) / 7);
|
|
33
|
+
return `${d.getFullYear()}-W${String(weekNum).padStart(2, "0")}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function loadJson(p, fallback) {
|
|
37
|
+
try { return JSON.parse(fs.readFileSync(p, "utf8")); }
|
|
38
|
+
catch { return fallback; }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function saveJson(p, data) {
|
|
42
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
43
|
+
fs.writeFileSync(p, JSON.stringify(data, null, 2) + "\n", "utf8");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function appendJsonl(p, obj) {
|
|
47
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
48
|
+
fs.appendFileSync(p, JSON.stringify(obj) + "\n", "utf8");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ── Extract token usage from transcript JSONL ──────────────────────────────
|
|
52
|
+
|
|
53
|
+
function extractUsageFromTranscript(transcriptPath) {
|
|
54
|
+
if (!transcriptPath || !fs.existsSync(transcriptPath)) return null;
|
|
55
|
+
|
|
56
|
+
let inputTokens = 0, outputTokens = 0, cacheReadTokens = 0, cacheCreateTokens = 0;
|
|
57
|
+
let toolCalls = 0, turns = 0, found = false;
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
const lines = fs.readFileSync(transcriptPath, "utf8").split("\n").filter(Boolean);
|
|
61
|
+
for (const line of lines) {
|
|
62
|
+
try {
|
|
63
|
+
const entry = JSON.parse(line);
|
|
64
|
+
// Extract usage from any entry that has it
|
|
65
|
+
const usage = entry.usage || entry.message?.usage;
|
|
66
|
+
if (usage) {
|
|
67
|
+
inputTokens += usage.input_tokens || 0;
|
|
68
|
+
outputTokens += usage.output_tokens || 0;
|
|
69
|
+
cacheReadTokens += usage.cache_read_input_tokens || 0;
|
|
70
|
+
cacheCreateTokens+= usage.cache_creation_input_tokens|| 0;
|
|
71
|
+
found = true;
|
|
72
|
+
}
|
|
73
|
+
// Count tool uses
|
|
74
|
+
if (entry.type === "tool_use" || entry.tool_name) toolCalls++;
|
|
75
|
+
// Count assistant turns
|
|
76
|
+
if (entry.role === "assistant" || entry.type === "assistant") turns++;
|
|
77
|
+
} catch { /* skip malformed lines */ }
|
|
78
|
+
}
|
|
79
|
+
} catch { return null; }
|
|
80
|
+
|
|
81
|
+
if (!found) return null;
|
|
82
|
+
return { inputTokens, outputTokens, cacheReadTokens, cacheCreateTokens, toolCalls, turns };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ── Estimate tokens when transcript doesn't have usage data ───────────────
|
|
86
|
+
|
|
87
|
+
function estimateFromTranscript(transcriptPath) {
|
|
88
|
+
if (!transcriptPath || !fs.existsSync(transcriptPath)) {
|
|
89
|
+
return { inputTokens: 0, outputTokens: 0, toolCalls: 0, turns: 0, estimated: true };
|
|
90
|
+
}
|
|
91
|
+
let inputChars = 0, outputChars = 0, toolCalls = 0, turns = 0;
|
|
92
|
+
try {
|
|
93
|
+
const lines = fs.readFileSync(transcriptPath, "utf8").split("\n").filter(Boolean);
|
|
94
|
+
for (const line of lines) {
|
|
95
|
+
try {
|
|
96
|
+
const entry = JSON.parse(line);
|
|
97
|
+
const content = JSON.stringify(entry.content || entry.text || "");
|
|
98
|
+
if (entry.role === "user" || entry.type === "user") { inputChars += content.length; }
|
|
99
|
+
if (entry.role === "assistant" || entry.type === "assistant") { outputChars += content.length; turns++; }
|
|
100
|
+
if (entry.type === "tool_use" || entry.tool_name) { toolCalls++; inputChars += 500 * 4; }
|
|
101
|
+
} catch { /* skip */ }
|
|
102
|
+
}
|
|
103
|
+
} catch {}
|
|
104
|
+
return {
|
|
105
|
+
inputTokens: Math.round(inputChars / 4),
|
|
106
|
+
outputTokens: Math.round(outputChars / 4),
|
|
107
|
+
cacheReadTokens: 0, cacheCreateTokens: 0,
|
|
108
|
+
toolCalls, turns, estimated: true
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ── Main ───────────────────────────────────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
async function main() {
|
|
115
|
+
let event = {};
|
|
116
|
+
try {
|
|
117
|
+
const raw = fs.readFileSync("/dev/stdin", "utf8");
|
|
118
|
+
event = JSON.parse(raw);
|
|
119
|
+
} catch { /* no stdin or parse error — use empty event */ }
|
|
120
|
+
|
|
121
|
+
const cwd = event.cwd || process.cwd();
|
|
122
|
+
const sessionId = event.session_id || `unknown-${Date.now()}`;
|
|
123
|
+
const transcriptPath = event.transcript_path;
|
|
124
|
+
|
|
125
|
+
const budgetFile = path.join(cwd, ".ai-assistance", "token-budget.json");
|
|
126
|
+
const kpiDir = path.join(cwd, ".ai-assistance", "local", "kpi");
|
|
127
|
+
const weekId = isoWeek(new Date());
|
|
128
|
+
const sessionFile = path.join(kpiDir, `session-${sessionId}.json`);
|
|
129
|
+
const weeklyFile = path.join(kpiDir, `weekly-${weekId}.json`);
|
|
130
|
+
const turnLogFile = path.join(kpiDir, `sessions-${weekId}.jsonl`);
|
|
131
|
+
|
|
132
|
+
const budget = loadJson(budgetFile, {});
|
|
133
|
+
const project = (budget.project?.name || path.basename(cwd));
|
|
134
|
+
const priority= (budget.project?.priority || "medium");
|
|
135
|
+
const targetPct = (budget.weekly_quota?.target_utilization_pct || 75) / 100;
|
|
136
|
+
const warnAt = (budget.session_logging?.warn_at_pct || 80) / 100;
|
|
137
|
+
|
|
138
|
+
// Extract usage from transcript
|
|
139
|
+
const transcriptUsage = extractUsageFromTranscript(transcriptPath)
|
|
140
|
+
|| estimateFromTranscript(transcriptPath);
|
|
141
|
+
|
|
142
|
+
// Load previous session state (accumulate across turns in a session)
|
|
143
|
+
const prevSession = loadJson(sessionFile, {
|
|
144
|
+
session_id: sessionId, project, priority,
|
|
145
|
+
started_at: new Date().toISOString(),
|
|
146
|
+
week_id: weekId,
|
|
147
|
+
input_tokens: 0, output_tokens: 0,
|
|
148
|
+
cache_read_tokens: 0, cache_create_tokens: 0,
|
|
149
|
+
tool_calls: 0, turns: 0, estimated: false,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// Use transcript totals (they accumulate naturally) not deltas
|
|
153
|
+
const sessionNow = {
|
|
154
|
+
...prevSession,
|
|
155
|
+
input_tokens: transcriptUsage.inputTokens,
|
|
156
|
+
output_tokens: transcriptUsage.outputTokens,
|
|
157
|
+
cache_read_tokens: transcriptUsage.cacheReadTokens || 0,
|
|
158
|
+
cache_create_tokens:transcriptUsage.cacheCreateTokens || 0,
|
|
159
|
+
tool_calls: transcriptUsage.toolCalls,
|
|
160
|
+
turns: transcriptUsage.turns,
|
|
161
|
+
estimated: transcriptUsage.estimated || false,
|
|
162
|
+
last_updated_at: new Date().toISOString(),
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
saveJson(sessionFile, sessionNow);
|
|
166
|
+
|
|
167
|
+
// Update weekly totals — replace session contribution (re-compute from sessions)
|
|
168
|
+
const weekly = loadJson(weeklyFile, { week_id: weekId, projects: {}, total_tokens: 0 });
|
|
169
|
+
const sessionTotal = sessionNow.input_tokens + sessionNow.output_tokens;
|
|
170
|
+
const prevContrib = (weekly.projects[sessionId]?.tokens || 0);
|
|
171
|
+
weekly.projects[sessionId] = {
|
|
172
|
+
project, priority, tokens: sessionTotal,
|
|
173
|
+
tool_calls: sessionNow.tool_calls, turns: sessionNow.turns,
|
|
174
|
+
updated_at: new Date().toISOString()
|
|
175
|
+
};
|
|
176
|
+
weekly.total_tokens = Object.values(weekly.projects).reduce((s, p) => s + p.tokens, 0);
|
|
177
|
+
saveJson(weeklyFile, weekly);
|
|
178
|
+
|
|
179
|
+
// Log the turn to the weekly JSONL (for cross-session analysis)
|
|
180
|
+
appendJsonl(turnLogFile, {
|
|
181
|
+
ts: new Date().toISOString(), session_id: sessionId, project, priority, week_id: weekId,
|
|
182
|
+
turn_tokens: sessionTotal - prevContrib,
|
|
183
|
+
session_total_tokens: sessionTotal,
|
|
184
|
+
tool_calls: sessionNow.tool_calls,
|
|
185
|
+
estimated: sessionNow.estimated,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// ── Budget warnings ────────────────────────────────────────────────────
|
|
189
|
+
|
|
190
|
+
const totalTokens = budget.weekly_quota?.total_tokens;
|
|
191
|
+
if (totalTokens) {
|
|
192
|
+
const usedPct = weekly.total_tokens / totalTokens;
|
|
193
|
+
const targetTokens = totalTokens * targetPct;
|
|
194
|
+
|
|
195
|
+
// Priority-based soft allocation
|
|
196
|
+
const weights = budget.project_allocation?.priority_weights || { high: 50, medium: 30, low: 20 };
|
|
197
|
+
const myWeight = (weights[priority] || 30) / 100;
|
|
198
|
+
const myBudget = totalTokens * targetPct * myWeight;
|
|
199
|
+
const myUsed = Object.values(weekly.projects)
|
|
200
|
+
.filter(p => p.project === project)
|
|
201
|
+
.reduce((s, p) => s + p.tokens, 0);
|
|
202
|
+
const myPct = myBudget > 0 ? myUsed / myBudget : 0;
|
|
203
|
+
|
|
204
|
+
if (usedPct >= warnAt) {
|
|
205
|
+
process.stderr.write(
|
|
206
|
+
`\n[token-budget] ⚠ Week ${weekId}: ${Math.round(usedPct * 100)}% of quota used` +
|
|
207
|
+
` (${weekly.total_tokens.toLocaleString()}/${totalTokens.toLocaleString()} tokens)` +
|
|
208
|
+
` — target was ${Math.round(targetPct * 100)}%\n`
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
if (myPct >= warnAt) {
|
|
212
|
+
process.stderr.write(
|
|
213
|
+
`[token-budget] ⚠ Project "${project}" (${priority}): ${Math.round(myPct * 100)}% of allocation` +
|
|
214
|
+
` (${myUsed.toLocaleString()}/${Math.round(myBudget).toLocaleString()} tokens)\n`
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
main().catch(() => { /* never crash the hook */ });
|