estimetry 0.1.1 → 0.2.0
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.
- package/CHANGELOG.md +13 -0
- package/README.md +45 -15
- package/dist/application/contracts/estimetry-command.js +34 -0
- package/dist/application/contracts/skill-mention.js +20 -0
- package/dist/application/estimetry.js +2 -0
- package/dist/application/hooks/context-limit.js +10 -2
- package/dist/application/hooks/hook-context.js +12 -4
- package/dist/application/hooks/receive-session-hook.js +15 -4
- package/dist/application/hooks/session-end-import.js +6 -7
- package/dist/application/ports/estimetry-runner.js +2 -0
- package/dist/application/project/init-defaults.js +17 -4
- package/dist/application/project/init-project.js +6 -2
- package/dist/application/project/install-copy.js +12 -3
- package/dist/application/project/show-init-choices.js +1 -1
- package/dist/application/project/tool-notices.js +8 -0
- package/dist/application/project/upgrade-plan.js +1 -1
- package/dist/application/task/import-session-logs.js +4 -1
- package/dist/bootstrap/container.js +38 -17
- package/dist/bootstrap/hook-receiver.js +7 -14
- package/dist/bootstrap/main.js +1 -1
- package/dist/domain/code/code-classes.js +1 -1
- package/dist/domain/progress/progress-record-parser.js +6 -0
- package/dist/domain/progress/session-log.js +2 -1
- package/dist/domain/usage/task-usage.js +24 -7
- package/dist/infrastructure/git/git-commit-hook-installer.js +16 -28
- package/dist/integrations/tools/claude-code/claude-code-adapter.js +32 -48
- package/dist/integrations/tools/claude-code/hook-events.js +4 -0
- package/dist/integrations/tools/claude-code/hook-translation.js +16 -34
- package/dist/integrations/tools/claude-code/importer/claude-code-logs.js +1 -1
- package/dist/integrations/tools/claude-code/skill-file.js +7 -19
- package/dist/integrations/tools/codex/codex-adapter.js +161 -0
- package/dist/integrations/tools/codex/hook-events.js +23 -0
- package/dist/integrations/tools/codex/hook-translation.js +76 -0
- package/dist/integrations/tools/codex/hook-trust.js +69 -0
- package/dist/integrations/tools/codex/importer/codex-logs.js +208 -0
- package/dist/integrations/tools/codex/importer/rollout-parser.js +163 -0
- package/dist/integrations/tools/codex/sandbox-rules.js +38 -0
- package/dist/integrations/tools/codex/skill-file.js +22 -0
- package/dist/integrations/tools/codex/trust-tables.js +96 -0
- package/dist/integrations/tools/content/commands.js +27 -20
- package/dist/integrations/tools/shared/generated-file.js +26 -0
- package/dist/integrations/tools/shared/hook-payload.js +33 -0
- package/dist/integrations/tools/{claude-code → shared}/hook-settings.js +40 -16
- package/dist/integrations/tools/shared/instructions-block.js +39 -0
- package/dist/integrations/tools/shared/skills-plan.js +39 -0
- package/dist/interface/cli/commands/hook-command.js +1 -1
- package/dist/interface/cli/commands/init-command.js +16 -7
- package/dist/interface/cli/commands/status-command.js +5 -2
- package/dist/interface/cli/presenters/text/closing-text.js +7 -3
- package/dist/interface/cli/presenters/text/notices-text.js +1 -0
- package/dist/interface/cli/presenters/text/task-text.js +1 -1
- package/dist/interface/cli/presenters/text-presenter.js +3 -3
- package/package.json +1 -1
- package/dist/integrations/tools/claude-code/claude-md.js +0 -28
- /package/dist/integrations/tools/{claude-code/importer → shared}/log-lines.js +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
1
|
+
import { existsSync, realpathSync } from 'node:fs';
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
3
|
import { homedir, userInfo } from 'node:os';
|
|
4
|
-
import { dirname, join, sep } from 'node:path';
|
|
4
|
+
import { basename, dirname, join, sep } from 'node:path';
|
|
5
5
|
import { StorageLayout } from '../infrastructure/fs/storage-layout.js';
|
|
6
6
|
import { FileErrorReporter } from '../infrastructure/system/file-error-reporter.js';
|
|
7
7
|
export function processEnvironment() {
|
|
@@ -10,26 +10,39 @@ export function processEnvironment() {
|
|
|
10
10
|
import { packageVersion } from './package-version.js';
|
|
11
11
|
export { packageVersion };
|
|
12
12
|
/**
|
|
13
|
-
* How this installation
|
|
14
|
-
*
|
|
13
|
+
* How hooks start this installation (estimetry-command): the entry point beside
|
|
14
|
+
* this file, the node of its npm prefix when it is a global npm install (a
|
|
15
15
|
* stable link such as Homebrew's `/opt/homebrew/bin/node`, which survives an
|
|
16
|
-
* upgrade of Node.js)
|
|
17
|
-
*
|
|
16
|
+
* upgrade of Node.js), else the running node, and the data directory. None on
|
|
17
|
+
* Windows, where Claude Code may run hooks without a POSIX shell, and none from
|
|
18
|
+
* the cache of `npx`, which is cleared: hooks then call `estimetry` from PATH.
|
|
18
19
|
*/
|
|
19
20
|
function estimetryRunner(layout) {
|
|
20
21
|
const main = fileURLToPath(new URL('./main.js', import.meta.url));
|
|
21
|
-
if (main.includes(`${sep}_npx${sep}`)) {
|
|
22
|
+
if (process.platform === 'win32' || main.includes(`${sep}_npx${sep}`)) {
|
|
22
23
|
return undefined;
|
|
23
24
|
}
|
|
24
|
-
// <prefix>/lib/node_modules/estimetry/dist/bootstrap/main.js → <prefix>/bin/node
|
|
25
|
-
const
|
|
26
|
-
|
|
25
|
+
// <prefix>/lib/node_modules/estimetry/dist/bootstrap/main.js → <prefix>/bin/node, only for that layout.
|
|
26
|
+
const pkg = dirname(dirname(dirname(main)));
|
|
27
|
+
const globalNpm = basename(pkg) === 'estimetry' && basename(dirname(pkg)) === 'node_modules' && basename(dirname(dirname(pkg))) === 'lib';
|
|
28
|
+
const prefixed = join(dirname(dirname(dirname(pkg))), 'bin', 'node');
|
|
29
|
+
return { node: globalNpm && existsSync(prefixed) ? prefixed : process.execPath, main, home: layout.root };
|
|
27
30
|
}
|
|
28
31
|
/** Claude Code keeps personal skills in `$CLAUDE_CONFIG_DIR`, else `~/.claude`. */
|
|
29
32
|
function claudeConfigDir(environment) {
|
|
30
33
|
const configured = environment.env['CLAUDE_CONFIG_DIR'];
|
|
31
34
|
return configured !== undefined && configured !== '' ? configured : join(environment.homeDir, '.claude');
|
|
32
35
|
}
|
|
36
|
+
/** Codex keeps the user's hooks and config in `$CODEX_HOME`, else `~/.codex`; its skills in `~/.agents/skills`. */
|
|
37
|
+
export function codexDirs(environment) {
|
|
38
|
+
const configured = environment.env['CODEX_HOME'];
|
|
39
|
+
const home = configured !== undefined && configured !== '' ? configured : join(environment.homeDir, '.codex');
|
|
40
|
+
return {
|
|
41
|
+
// Codex keys the trust of a hook by the canonical path of its file: a symlinked CODEX_HOME must give the same key.
|
|
42
|
+
home: existsSync(home) ? realpathSync(home) : home,
|
|
43
|
+
userSkills: join(environment.homeDir, '.agents', 'skills'),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
33
46
|
/** Composition root: the only place that knows which implementation backs which port. */
|
|
34
47
|
export function createContainer(environment, options = {}) {
|
|
35
48
|
const layout = StorageLayout.resolve(environment);
|
|
@@ -41,17 +54,21 @@ export function createContainer(environment, options = {}) {
|
|
|
41
54
|
app: () => (app ??= buildApp(layout, environment, errors)),
|
|
42
55
|
hooks: async () => {
|
|
43
56
|
const { buildHookReceiver } = await import('./hook-receiver.js');
|
|
44
|
-
const
|
|
57
|
+
const toolDirs = { claude: claudeConfigDir(environment), codex: codexDirs(environment).home };
|
|
58
|
+
const receiver = buildHookReceiver(layout, errors, options.startedAt ?? Date.now(), toolDirs);
|
|
45
59
|
return { receive: (call) => receiver.execute(call) };
|
|
46
60
|
},
|
|
47
61
|
};
|
|
48
62
|
}
|
|
49
63
|
async function toolRegistry(environment) {
|
|
50
|
-
const [{ ToolAdapterRegistry }, { ClaudeCodeAdapter }] = await Promise.all([
|
|
64
|
+
const [{ ToolAdapterRegistry }, { ClaudeCodeAdapter }, { CodexAdapter }] = await Promise.all([
|
|
51
65
|
import('../integrations/tools/registry.js'),
|
|
52
66
|
import('../integrations/tools/claude-code/claude-code-adapter.js'),
|
|
67
|
+
import('../integrations/tools/codex/codex-adapter.js'),
|
|
53
68
|
]);
|
|
54
|
-
return new ToolAdapterRegistry()
|
|
69
|
+
return new ToolAdapterRegistry()
|
|
70
|
+
.register(new ClaudeCodeAdapter(claudeConfigDir(environment)))
|
|
71
|
+
.register(new CodexAdapter(codexDirs(environment)));
|
|
55
72
|
}
|
|
56
73
|
async function installDependencies(environment, sharing, layout) {
|
|
57
74
|
const [tools, { FlowRegistry }, { openSpecFlow }, { noneFlow }, { StackDetectorRegistry }, { nodeDetector }, { dartDetector }, { mavenDetector, gradleDetector }, { pythonDetector }, { goDetector }, { composeDetector }, { NodeProjectFiles }, { GitCommitHookInstaller }, { GitExcludesFile }, { PathCommandLocator },] = await Promise.all([
|
|
@@ -71,6 +88,7 @@ async function installDependencies(environment, sharing, layout) {
|
|
|
71
88
|
import('../infrastructure/git/git-excludes-file.js'),
|
|
72
89
|
import('../infrastructure/system/path-command-locator.js'),
|
|
73
90
|
]);
|
|
91
|
+
const runner = estimetryRunner(layout);
|
|
74
92
|
return {
|
|
75
93
|
files: new NodeProjectFiles(),
|
|
76
94
|
registries: {
|
|
@@ -80,12 +98,12 @@ async function installDependencies(environment, sharing, layout) {
|
|
|
80
98
|
detectors: [nodeDetector, dartDetector, mavenDetector, gradleDetector, pythonDetector, goDetector, composeDetector].reduce((registry, detector) => registry.register(detector), new StackDetectorRegistry()),
|
|
81
99
|
},
|
|
82
100
|
git: {
|
|
83
|
-
commitHook: new GitCommitHookInstaller(
|
|
101
|
+
commitHook: new GitCommitHookInstaller(),
|
|
84
102
|
excludes: new GitExcludesFile(environment.env, environment.homeDir),
|
|
85
103
|
},
|
|
86
104
|
sharing,
|
|
87
105
|
commands: new PathCommandLocator(environment.env),
|
|
88
|
-
version: packageVersion(),
|
|
106
|
+
installation: { version: packageVersion(), ...(runner === undefined ? {} : { runner }) },
|
|
89
107
|
};
|
|
90
108
|
}
|
|
91
109
|
async function buildApp(layout, environment, errors) {
|
|
@@ -135,7 +153,7 @@ async function buildApp(layout, environment, errors) {
|
|
|
135
153
|
}
|
|
136
154
|
/** What closing a task needs besides the task: cards, calibration, proposals, the code volume, `HEAD` and the session logs. */
|
|
137
155
|
async function closingPorts(layout, environment) {
|
|
138
|
-
const [{ YamlCardStore }, { JsonCalibrationStore }, { FileCloseProposals }, { GitCodeMeasurer }, { FsGitHead }, { SessionLogReaderRegistry }, { ClaudeCodeLogs }, { YamlLogImportState },] = await Promise.all([
|
|
156
|
+
const [{ YamlCardStore }, { JsonCalibrationStore }, { FileCloseProposals }, { GitCodeMeasurer }, { FsGitHead }, { SessionLogReaderRegistry }, { ClaudeCodeLogs }, { CodexLogs }, { YamlLogImportState },] = await Promise.all([
|
|
139
157
|
import('../infrastructure/persistence/yaml-card-store.js'),
|
|
140
158
|
import('../infrastructure/persistence/json-calibration-store.js'),
|
|
141
159
|
import('../infrastructure/persistence/file-close-proposals.js'),
|
|
@@ -143,6 +161,7 @@ async function closingPorts(layout, environment) {
|
|
|
143
161
|
import('../infrastructure/git/fs-git-head.js'),
|
|
144
162
|
import('../integrations/tools/session-log-readers.js'),
|
|
145
163
|
import('../integrations/tools/claude-code/importer/claude-code-logs.js'),
|
|
164
|
+
import('../integrations/tools/codex/importer/codex-logs.js'),
|
|
146
165
|
import('../infrastructure/persistence/yaml-log-import-state.js'),
|
|
147
166
|
]);
|
|
148
167
|
return {
|
|
@@ -151,7 +170,9 @@ async function closingPorts(layout, environment) {
|
|
|
151
170
|
proposals: new FileCloseProposals(layout),
|
|
152
171
|
codeMeasurer: new GitCodeMeasurer(),
|
|
153
172
|
gitHead: new FsGitHead(),
|
|
154
|
-
logReaders: new SessionLogReaderRegistry()
|
|
173
|
+
logReaders: new SessionLogReaderRegistry()
|
|
174
|
+
.register(new ClaudeCodeLogs(claudeConfigDir(environment)))
|
|
175
|
+
.register(new CodexLogs(codexDirs(environment).home)),
|
|
155
176
|
logImportState: new YamlLogImportState(layout),
|
|
156
177
|
};
|
|
157
178
|
}
|
|
@@ -36,19 +36,11 @@ import { noneFlow } from '../integrations/flows/none/none-flow.js';
|
|
|
36
36
|
import { openSpecFlow } from '../integrations/flows/openspec/openspec-flow.js';
|
|
37
37
|
import { FlowRegistry } from '../integrations/flows/registry.js';
|
|
38
38
|
import { claudeCodeHooks } from '../integrations/tools/claude-code/hook-translation.js';
|
|
39
|
+
import { codexHooks } from '../integrations/tools/codex/hook-translation.js';
|
|
39
40
|
import { gitHooks } from '../integrations/tools/git/git-hooks.js';
|
|
40
41
|
import { HookAdapterRegistry } from '../integrations/tools/hook-adapters.js';
|
|
41
42
|
import { SessionWindow } from '../application/task/session-window.js';
|
|
42
|
-
|
|
43
|
-
* The hook receiver, apart from the composition of the facade: the container
|
|
44
|
-
* loads this module only for a hook, and a hook loads nothing of the other
|
|
45
|
-
* commands. What only one kind of call needs loads lazily within it.
|
|
46
|
-
*/
|
|
47
|
-
/**
|
|
48
|
-
* Only what a hook needs. The author comes from config.yaml alone: detecting it
|
|
49
|
-
* would run git, and init has written the profile already.
|
|
50
|
-
*/
|
|
51
|
-
export function buildHookReceiver(layout, errors, startedAt, claudeConfig) {
|
|
43
|
+
export function buildHookReceiver(layout, errors, startedAt, toolDirs) {
|
|
52
44
|
const config = new YamlConfigStore(layout, () => {
|
|
53
45
|
throw new Error(`no author profile in ${layout.configFile}; run estimetry init`);
|
|
54
46
|
});
|
|
@@ -75,8 +67,8 @@ export function buildHookReceiver(layout, errors, startedAt, claudeConfig) {
|
|
|
75
67
|
// Only the session start needs the fact, and only it loads the task repository and the calculator.
|
|
76
68
|
new SessionStartContext(() => hookFacts(layout, journal, config, plans), checkpoints, proposal, new UpgradeNotice(new YamlInstallVersions(layout), packageVersion)), new PromptContext(checkpoints, proposal),
|
|
77
69
|
// Only the session end parses a log, and only it loads the reader.
|
|
78
|
-
new SessionEndImport(bindings, tasks, () => hookLogs({ layout,
|
|
79
|
-
const adapters = new HookAdapterRegistry().register(claudeCodeHooks).register(gitHooks);
|
|
70
|
+
new SessionEndImport(bindings, tasks, () => hookLogs({ layout, toolDirs, journal, ids, config, errors })));
|
|
71
|
+
const adapters = new HookAdapterRegistry().register(claudeCodeHooks).register(codexHooks).register(gitHooks);
|
|
80
72
|
return new ReceiveHook(adapters, clock, new CommitRecords(locator, git, new CommitTask(bindings, tasks, recent), journal, meta), sessions);
|
|
81
73
|
}
|
|
82
74
|
/** The plan of a task for the text of a pending proposal to close it: percent done and unfinished groups. */
|
|
@@ -112,15 +104,16 @@ async function hookFacts(layout, journal, settings, plans) {
|
|
|
112
104
|
}
|
|
113
105
|
/** The importer of session logs for a session end. */
|
|
114
106
|
async function hookLogs(c) {
|
|
115
|
-
const [{ ImportSessionLogs }, { SessionLogReaderRegistry }, { ClaudeCodeLogs }, { YamlLogImportState }, { SystemClock }] = await Promise.all([
|
|
107
|
+
const [{ ImportSessionLogs }, { SessionLogReaderRegistry }, { ClaudeCodeLogs }, { CodexLogs }, { YamlLogImportState }, { SystemClock }] = await Promise.all([
|
|
116
108
|
import('../application/task/import-session-logs.js'),
|
|
117
109
|
import('../integrations/tools/session-log-readers.js'),
|
|
118
110
|
import('../integrations/tools/claude-code/importer/claude-code-logs.js'),
|
|
111
|
+
import('../integrations/tools/codex/importer/codex-logs.js'),
|
|
119
112
|
import('../infrastructure/persistence/yaml-log-import-state.js'),
|
|
120
113
|
import('../infrastructure/system/system-clock.js'),
|
|
121
114
|
]);
|
|
122
115
|
return new ImportSessionLogs({
|
|
123
|
-
readers: new SessionLogReaderRegistry().register(new ClaudeCodeLogs(c.
|
|
116
|
+
readers: new SessionLogReaderRegistry().register(new ClaudeCodeLogs(c.toolDirs.claude)).register(new CodexLogs(c.toolDirs.codex)),
|
|
124
117
|
state: new YamlLogImportState(c.layout),
|
|
125
118
|
journal: c.journal,
|
|
126
119
|
meta: new EventMetaSource(new SystemClock(), c.ids, c.config),
|
package/dist/bootstrap/main.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export const CODE_CLASSES = ['generated', 'tests', 'docs', 'config', 'production'];
|
|
6
6
|
const GENERATED_FILES = /(^|\/)(package-lock\.json|pnpm-lock\.yaml|go\.sum)$|\.lock$|\.lockb$|\.g\.dart$|\.freezed\.dart$|\.pb\.go$|_pb2\.py$|\.min\.js$|\.map$|\.snap$/;
|
|
7
|
-
const GENERATED_DIRS = /(^|\/)(node_modules|__snapshots__|generated)\/|^\.(estimetry|claude)\//;
|
|
7
|
+
const GENERATED_DIRS = /(^|\/)(node_modules|__snapshots__|generated)\/|^\.(estimetry|claude|codex|agents)\//;
|
|
8
8
|
const GENERATED_ROOT_DIRS = /^(dist|build)\//;
|
|
9
9
|
const TEST_DIRS = /(^|\/)(test|tests|__tests__|spec|integration_test)\/|(^|\/)src\/test\//;
|
|
10
10
|
const TEST_FILES = /\.(test|spec)\.[^/]+$|_test\.[^/]+$|(^|\/)test_[^/]*\.py$/;
|
|
@@ -156,6 +156,9 @@ function logSession(data) {
|
|
|
156
156
|
});
|
|
157
157
|
buckets(agent['tokens'], `data.agents[${i}].tokens`);
|
|
158
158
|
});
|
|
159
|
+
if (data['costReported'] !== undefined && typeof data['costReported'] !== 'boolean') {
|
|
160
|
+
throw new InvalidProgressRecord('data.costReported', 'must be true or false');
|
|
161
|
+
}
|
|
159
162
|
list(data['cost'], 'data.cost').forEach((item, i) => {
|
|
160
163
|
const total = object(item, `data.cost[${i}]`);
|
|
161
164
|
moment(total, 'startTime', `data.cost[${i}].`);
|
|
@@ -175,6 +178,9 @@ function buckets(value, path) {
|
|
|
175
178
|
for (const key of ['input', 'output', 'cacheRead', 'cacheWrite5m', 'cacheWrite1h']) {
|
|
176
179
|
count(bucket, key, `${path}[${i}].`);
|
|
177
180
|
}
|
|
181
|
+
if (bucket['cacheWrite'] !== undefined) {
|
|
182
|
+
count(bucket, 'cacheWrite', `${path}[${i}].`);
|
|
183
|
+
}
|
|
178
184
|
});
|
|
179
185
|
}
|
|
180
186
|
function moment(record, key, prefix) {
|
|
@@ -22,7 +22,8 @@ export function sessionLogs(records, closed = []) {
|
|
|
22
22
|
continue; // a write cut short: the next import reads the same stretch again
|
|
23
23
|
}
|
|
24
24
|
const { session, from } = first.data;
|
|
25
|
-
const log = merged.get(session) ?? { turns: [], agents: new Map(), cost: new Map() };
|
|
25
|
+
const log = merged.get(session) ?? { turns: [], agents: new Map(), cost: new Map(), reportsCost: false };
|
|
26
|
+
log.reportsCost ||= first.data.costReported !== false;
|
|
26
27
|
log.turns = [...log.turns.filter((turn) => turn.key < from), ...joinedTurns(parts.flatMap((part) => part.data.turns))];
|
|
27
28
|
for (const agent of joinedAgents(parts.flatMap((part) => part.data.agents))) {
|
|
28
29
|
log.agents.set(agent.agent, agent);
|
|
@@ -4,18 +4,26 @@
|
|
|
4
4
|
* session served the task come in as arguments.
|
|
5
5
|
*/
|
|
6
6
|
/**
|
|
7
|
-
* Relative weight of a token of each kind, the structure of Claude prices
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* Relative weight of a token of each kind (an input token is 1), the structure of Claude prices, the only tool that
|
|
8
|
+
* reports a cost: fitted without residue to the totals of the studied sessions; prices themselves are not in the
|
|
9
|
+
* code. A cache write without a lifetime weighs as the short one. The cost of a session stays exact whatever the
|
|
10
|
+
* weights: they only split it between tasks.
|
|
11
11
|
*/
|
|
12
|
-
export const TOKEN_WEIGHTS = {
|
|
12
|
+
export const TOKEN_WEIGHTS = {
|
|
13
|
+
input: 1,
|
|
14
|
+
output: 5,
|
|
15
|
+
cacheRead: 0.05,
|
|
16
|
+
cacheWrite5m: 1.25,
|
|
17
|
+
cacheWrite1h: 2,
|
|
18
|
+
cacheWrite: 1.25,
|
|
19
|
+
};
|
|
13
20
|
/** Undefined when no session of the task has a log. */
|
|
14
21
|
export function taskUsage(input) {
|
|
15
22
|
const tokens = new Map();
|
|
16
23
|
let cost = 0;
|
|
17
24
|
let approximate = false;
|
|
18
25
|
let withLogs = 0;
|
|
26
|
+
let withCost = 0;
|
|
19
27
|
for (const [session, spans] of input.spans) {
|
|
20
28
|
const log = input.logs.get(session);
|
|
21
29
|
if (log === undefined) {
|
|
@@ -32,6 +40,10 @@ export function taskUsage(input) {
|
|
|
32
40
|
if (own < all) {
|
|
33
41
|
approximate = true;
|
|
34
42
|
}
|
|
43
|
+
if (!log.reportsCost) {
|
|
44
|
+
continue; // its tokens count; its cost is not known to the tool, so it is no part of the cost
|
|
45
|
+
}
|
|
46
|
+
withCost += 1;
|
|
35
47
|
const totals = [...log.cost.values()];
|
|
36
48
|
if (totals.some((total) => total.unknown)) {
|
|
37
49
|
cost = null;
|
|
@@ -43,7 +55,7 @@ export function taskUsage(input) {
|
|
|
43
55
|
if (withLogs === 0) {
|
|
44
56
|
return undefined;
|
|
45
57
|
}
|
|
46
|
-
return { tokens, costUSD: cost, approximate, sessions: input.spans.size, withLogs };
|
|
58
|
+
return { tokens, costUSD: withCost === 0 ? null : cost, approximate, sessions: input.spans.size, withLogs, withCost };
|
|
47
59
|
}
|
|
48
60
|
function weight(buckets) {
|
|
49
61
|
return buckets.reduce((sum, b) => sum +
|
|
@@ -51,7 +63,8 @@ function weight(buckets) {
|
|
|
51
63
|
b.output * TOKEN_WEIGHTS.output +
|
|
52
64
|
b.cacheRead * TOKEN_WEIGHTS.cacheRead +
|
|
53
65
|
b.cacheWrite5m * TOKEN_WEIGHTS.cacheWrite5m +
|
|
54
|
-
b.cacheWrite1h * TOKEN_WEIGHTS.cacheWrite1h
|
|
66
|
+
b.cacheWrite1h * TOKEN_WEIGHTS.cacheWrite1h +
|
|
67
|
+
(b.cacheWrite ?? 0) * TOKEN_WEIGHTS.cacheWrite, 0);
|
|
55
68
|
}
|
|
56
69
|
function add(sum, bucket) {
|
|
57
70
|
return {
|
|
@@ -60,6 +73,9 @@ function add(sum, bucket) {
|
|
|
60
73
|
cacheRead: (sum?.cacheRead ?? 0) + bucket.cacheRead,
|
|
61
74
|
cacheWrite5m: (sum?.cacheWrite5m ?? 0) + bucket.cacheWrite5m,
|
|
62
75
|
cacheWrite1h: (sum?.cacheWrite1h ?? 0) + bucket.cacheWrite1h,
|
|
76
|
+
...(sum?.cacheWrite === undefined && bucket.cacheWrite === undefined
|
|
77
|
+
? {}
|
|
78
|
+
: { cacheWrite: (sum?.cacheWrite ?? 0) + (bucket.cacheWrite ?? 0) }),
|
|
63
79
|
};
|
|
64
80
|
}
|
|
65
81
|
/**
|
|
@@ -81,6 +97,7 @@ export function usageSummary(usage) {
|
|
|
81
97
|
approximate: usage.approximate,
|
|
82
98
|
sessions: usage.sessions,
|
|
83
99
|
withLogs: usage.withLogs,
|
|
100
|
+
withCost: usage.withCost,
|
|
84
101
|
};
|
|
85
102
|
}
|
|
86
103
|
//# sourceMappingURL=task-usage.js.map
|
|
@@ -2,6 +2,7 @@ import { execFileSync } from 'node:child_process';
|
|
|
2
2
|
import { readFileSync } from 'node:fs';
|
|
3
3
|
import { isAbsolute, join, normalize, relative, sep } from 'node:path';
|
|
4
4
|
import { realPathAhead } from '../fs/worktree.js';
|
|
5
|
+
import { estimetryCommand } from '../../application/contracts/estimetry-command.js';
|
|
5
6
|
export const BLOCK_START = '# >>> estimetry >>>';
|
|
6
7
|
export const BLOCK_END = '# <<< estimetry <<<';
|
|
7
8
|
/**
|
|
@@ -11,15 +12,15 @@ export const BLOCK_END = '# <<< estimetry <<<';
|
|
|
11
12
|
*/
|
|
12
13
|
export const BLOCK_COMMAND = 'if command -v estimetry >/dev/null 2>&1; then estimetry_sha=$(git rev-parse HEAD 2>/dev/null); printf \'%s\' "$estimetry_sha" | estimetry hook git post-commit >/dev/null 2>&1 & fi';
|
|
13
14
|
/**
|
|
14
|
-
* The personal hook
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* The personal hook runs this installation by its paths first, so neither the
|
|
16
|
+
* node a version manager picks for the project nor a program without the
|
|
17
|
+
* user's PATH (an IDE started from the Dock) stops it; else `estimetry` from
|
|
18
|
+
* PATH. A hook the project commits never holds the paths: they are this machine's.
|
|
18
19
|
*/
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
function personalCommand(runner) {
|
|
21
|
+
return ('estimetry_sha=$(git rev-parse HEAD 2>/dev/null); ' +
|
|
22
|
+
`printf '%s' "$estimetry_sha" | { ${estimetryCommand(runner, 'hook git post-commit')}; } >/dev/null 2>&1 &`);
|
|
23
|
+
}
|
|
23
24
|
const HUSKY_LOADER = '. "$(dirname "$0")/_/husky.sh"';
|
|
24
25
|
const SHELLS = /^#!.*\b(sh|bash|zsh|dash|ksh)\b/;
|
|
25
26
|
const MANAGERS = [
|
|
@@ -29,24 +30,20 @@ const MANAGERS = [
|
|
|
29
30
|
];
|
|
30
31
|
/** `post-commit` next to the hooks the repository already has: `core.hooksPath`, worktrees and husky. */
|
|
31
32
|
export class GitCommitHookInstaller {
|
|
32
|
-
runner;
|
|
33
|
-
constructor(runner) {
|
|
34
|
-
this.runner = runner;
|
|
35
|
-
}
|
|
36
33
|
installed(projectRoot) {
|
|
37
34
|
const layout = this.layout(projectRoot);
|
|
38
35
|
return layout !== undefined && (read(layout.path) ?? '').includes(BLOCK_START);
|
|
39
36
|
}
|
|
40
|
-
plan(projectRoot, enabled) {
|
|
37
|
+
plan(projectRoot, enabled, runner) {
|
|
41
38
|
const layout = this.layout(projectRoot);
|
|
42
39
|
if (layout === undefined) {
|
|
43
40
|
return enabled ? { changes: [], skipped: [], warnings: ['not a git repository, so --track-commits was skipped'] } : EMPTY;
|
|
44
41
|
}
|
|
45
42
|
const current = read(layout.path);
|
|
46
|
-
return enabled ? this.install(layout, current) : this.uninstall(layout, current);
|
|
43
|
+
return enabled ? this.install(layout, current, runner) : this.uninstall(layout, current);
|
|
47
44
|
}
|
|
48
|
-
install(layout, current) {
|
|
49
|
-
const block =
|
|
45
|
+
install(layout, current, runner) {
|
|
46
|
+
const block = blockFor(layout, runner);
|
|
50
47
|
const warnings = [];
|
|
51
48
|
if (layout.husky !== undefined) {
|
|
52
49
|
warnings.push(`${layout.shown} is part of the repository: the hook runs for colleagues who have estimetry installed`);
|
|
@@ -73,15 +70,6 @@ export class GitCommitHookInstaller {
|
|
|
73
70
|
}
|
|
74
71
|
return { changes: [{ path: layout.path, content: withBlock(lines, block), executable: true }], skipped: [], warnings };
|
|
75
72
|
}
|
|
76
|
-
/** The lines between the markers: with the paths of this installation only in a personal hook. */
|
|
77
|
-
block(layout) {
|
|
78
|
-
if (!layout.personal || this.runner === undefined) {
|
|
79
|
-
return [BLOCK_START, BLOCK_COMMAND, BLOCK_END];
|
|
80
|
-
}
|
|
81
|
-
const { node, main, home } = this.runner;
|
|
82
|
-
const paths = `estimetry_node=${quoted(node)}; estimetry_main=${quoted(main)}; estimetry_home=${quoted(home)}`;
|
|
83
|
-
return [BLOCK_START, paths, FALLBACK_COMMAND, BLOCK_END];
|
|
84
|
-
}
|
|
85
73
|
uninstall(layout, current) {
|
|
86
74
|
if (current === undefined || !current.includes(BLOCK_START)) {
|
|
87
75
|
return EMPTY;
|
|
@@ -124,9 +112,9 @@ function located(projectRoot, path, husky) {
|
|
|
124
112
|
const personal = husky === undefined && (outside || inside.split(sep)[0] === '.git');
|
|
125
113
|
return husky === undefined ? { path, shown, personal } : { path, shown, personal, husky };
|
|
126
114
|
}
|
|
127
|
-
/**
|
|
128
|
-
function
|
|
129
|
-
return
|
|
115
|
+
/** The lines between the markers: with the paths of this installation only in a personal hook. */
|
|
116
|
+
function blockFor(layout, runner) {
|
|
117
|
+
return [BLOCK_START, layout.personal && runner !== undefined ? personalCommand(runner) : BLOCK_COMMAND, BLOCK_END];
|
|
130
118
|
}
|
|
131
119
|
/** The block right after the shebang (and the husky loader), so an `exit` at the end cannot skip it. */
|
|
132
120
|
function withBlock(lines, block) {
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { join } from 'node:path';
|
|
2
|
-
import { COMMANDS, ESTIMETRY_SKILL } from '../content/commands.js';
|
|
3
|
-
import {
|
|
4
|
-
import { hasOurHooks, isBrokenJson, withHooks, withoutHooks } from '
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
2
|
+
import { COMMANDS, ESTIMATE_REMINDER_BLOCK, ESTIMETRY_SKILL } from '../content/commands.js';
|
|
3
|
+
import { estimetryCommand } from '../../../application/contracts/estimetry-command.js';
|
|
4
|
+
import { hasOurHooks, isBrokenJson, withHooks, withoutHooks } from '../shared/hook-settings.js';
|
|
5
|
+
import { instructionsPlan } from '../shared/instructions-block.js';
|
|
6
|
+
import { nameSkills } from '../../../application/contracts/skill-mention.js';
|
|
7
|
+
import { skillsPlan } from '../shared/skills-plan.js';
|
|
8
|
+
import { CLAUDE_CODE_HOOK_EVENTS, SYNCHRONOUS_HOOK_EVENTS, claudeCodeSkillRef } from './hook-events.js';
|
|
9
|
+
import { renderSkill } from './skill-file.js';
|
|
7
10
|
export { CLAUDE_CODE_HOOK_EVENTS } from './hook-events.js';
|
|
8
11
|
const SHARED_SETTINGS = join('.claude', 'settings.json');
|
|
9
12
|
const LOCAL_SETTINGS = join('.claude', 'settings.local.json');
|
|
10
13
|
const SKILLS = [ESTIMETRY_SKILL, ...COMMANDS];
|
|
14
|
+
/** Our hooks in the settings: `estimetry hook claude-code …`, in any form `estimetryCommand` gives. */
|
|
15
|
+
const OURS = 'hook claude-code';
|
|
16
|
+
const CLAUDE_MD_BLOCK = nameSkills(ESTIMATE_REMINDER_BLOCK, claudeCodeSkillRef);
|
|
11
17
|
/**
|
|
12
18
|
* Claude Code: hooks in the project settings, skills (which are also its slash
|
|
13
19
|
* commands) in the user's directory or, for a shared install, in the project.
|
|
@@ -18,6 +24,8 @@ export class ClaudeCodeAdapter {
|
|
|
18
24
|
aliases = ['claude'];
|
|
19
25
|
title = 'Claude Code';
|
|
20
26
|
hookEvents = CLAUDE_CODE_HOOK_EVENTS;
|
|
27
|
+
/** Claude Code runs the hooks in its settings as they are. */
|
|
28
|
+
asksTrust = false;
|
|
21
29
|
/** `configDir` is the user's Claude Code directory: `$CLAUDE_CONFIG_DIR` or `~/.claude`. */
|
|
22
30
|
constructor(configDir) {
|
|
23
31
|
this.configDir = configDir;
|
|
@@ -25,17 +33,20 @@ export class ClaudeCodeAdapter {
|
|
|
25
33
|
state(projectRoot, files) {
|
|
26
34
|
const sharedText = files.read(join(projectRoot, SHARED_SETTINGS));
|
|
27
35
|
const localText = files.read(join(projectRoot, LOCAL_SETTINGS));
|
|
28
|
-
const shared = hasOurHooks(sharedText);
|
|
36
|
+
const shared = hasOurHooks(sharedText, OURS);
|
|
29
37
|
const broken = [
|
|
30
38
|
...(isBrokenJson(sharedText) ? [SHARED_SETTINGS] : []),
|
|
31
39
|
...(isBrokenJson(localText) ? [LOCAL_SETTINGS] : []),
|
|
32
40
|
];
|
|
33
41
|
return {
|
|
34
|
-
installed: shared || hasOurHooks(localText),
|
|
42
|
+
installed: shared || hasOurHooks(localText, OURS),
|
|
35
43
|
shared,
|
|
36
44
|
...(broken.length === 0 ? {} : { unreadable: `${broken.join(' and ')} ${broken.length === 1 ? 'is' : 'are'} not valid JSON` }),
|
|
37
45
|
};
|
|
38
46
|
}
|
|
47
|
+
present(files) {
|
|
48
|
+
return files.isDirectory(this.configDir);
|
|
49
|
+
}
|
|
39
50
|
personalFiles() {
|
|
40
51
|
return [LOCAL_SETTINGS];
|
|
41
52
|
}
|
|
@@ -47,19 +58,21 @@ export class ClaudeCodeAdapter {
|
|
|
47
58
|
warnings: plans.flatMap((plan) => plan.warnings),
|
|
48
59
|
};
|
|
49
60
|
}
|
|
50
|
-
hooks({ projectRoot, files, shared }) {
|
|
61
|
+
hooks({ projectRoot, files, shared, runner }) {
|
|
51
62
|
const [target, previous] = shared ? [SHARED_SETTINGS, LOCAL_SETTINGS] : [LOCAL_SETTINGS, SHARED_SETTINGS];
|
|
63
|
+
// Personal settings run this installation by its paths; settings the team commits hold no paths of this machine.
|
|
64
|
+
const command = (event) => estimetryCommand(shared ? undefined : runner, `${OURS} ${event}`);
|
|
52
65
|
const changes = [
|
|
53
66
|
{
|
|
54
67
|
path: join(projectRoot, target),
|
|
55
|
-
content: withHooks(files.read(join(projectRoot, target)), this.hookEvents,
|
|
68
|
+
content: withHooks(files.read(join(projectRoot, target)), { ours: OURS, events: this.hookEvents, synchronous: SYNCHRONOUS_HOOK_EVENTS, command }, target),
|
|
56
69
|
},
|
|
57
70
|
];
|
|
58
71
|
const warnings = [];
|
|
59
72
|
const old = files.read(join(projectRoot, previous));
|
|
60
73
|
if (old !== undefined) {
|
|
61
74
|
try {
|
|
62
|
-
const cleaned = withoutHooks(old, previous);
|
|
75
|
+
const cleaned = withoutHooks(old, OURS, previous);
|
|
63
76
|
changes.push(cleaned === undefined
|
|
64
77
|
? { path: join(projectRoot, previous), remove: true }
|
|
65
78
|
: { path: join(projectRoot, previous), content: cleaned });
|
|
@@ -72,48 +85,19 @@ export class ClaudeCodeAdapter {
|
|
|
72
85
|
return { changes, skipped: [], warnings };
|
|
73
86
|
}
|
|
74
87
|
skills({ projectRoot, files, shared, force, version }) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (owner === 'foreign') {
|
|
84
|
-
skipped.push({ path, reason: 'not created by estimetry, left as it is' });
|
|
85
|
-
}
|
|
86
|
-
else if (owner === 'edited' && !force) {
|
|
87
|
-
skipped.push({ path, reason: 'edited after install; run init --force to overwrite it' });
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
changes.push({ path, content: renderSkill(skill, version) });
|
|
91
|
-
}
|
|
92
|
-
// A personal install takes the skills out of the project; a shared one keeps the personal ones for other projects.
|
|
93
|
-
if (!shared) {
|
|
94
|
-
const inProject = join(projectSkills, skill.id, 'SKILL.md');
|
|
95
|
-
const text = files.read(inProject);
|
|
96
|
-
if (text !== undefined && ownership(text) === 'ours') {
|
|
97
|
-
changes.push({ path: inProject, remove: true });
|
|
98
|
-
}
|
|
99
|
-
else if (text !== undefined && ownership(text) === 'edited') {
|
|
100
|
-
skipped.push({ path: inProject, reason: 'edited after install, so it stays in the project' });
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return { changes, skipped, warnings: [] };
|
|
88
|
+
return skillsPlan(SKILLS, {
|
|
89
|
+
files,
|
|
90
|
+
projectDir: join(projectRoot, '.claude', 'skills'),
|
|
91
|
+
userDir: join(this.configDir, 'skills'),
|
|
92
|
+
shared,
|
|
93
|
+
force,
|
|
94
|
+
render: (skill) => renderSkill(skill, version),
|
|
95
|
+
});
|
|
105
96
|
}
|
|
106
97
|
/** The reminder goes to the committed CLAUDE.md only with a shared install. */
|
|
107
98
|
claudeMd({ projectRoot, files, shared }) {
|
|
108
99
|
const path = join(projectRoot, 'CLAUDE.md');
|
|
109
|
-
|
|
110
|
-
if (shared) {
|
|
111
|
-
return { changes: [{ path, content: withBlock(current) }], skipped: [], warnings: [] };
|
|
112
|
-
}
|
|
113
|
-
if (current === undefined) {
|
|
114
|
-
return { changes: [], skipped: [], warnings: [] };
|
|
115
|
-
}
|
|
116
|
-
return { changes: [{ path, content: withoutBlock(current) }], skipped: [], warnings: [] };
|
|
100
|
+
return instructionsPlan(path, files.read(path), shared, CLAUDE_MD_BLOCK);
|
|
117
101
|
}
|
|
118
102
|
}
|
|
119
103
|
//# sourceMappingURL=claude-code-adapter.js.map
|
|
@@ -15,4 +15,8 @@ export const CLAUDE_CODE_HOOK_EVENTS = [
|
|
|
15
15
|
];
|
|
16
16
|
/** Hooks the agent must wait for: their context has to reach it in the same turn, or the session is ending. */
|
|
17
17
|
export const SYNCHRONOUS_HOOK_EVENTS = ['SessionStart', 'UserPromptSubmit', 'SessionEnd'];
|
|
18
|
+
/** How Claude Code calls a skill: as a slash command. One rule for the context of hooks and the bodies of skills. */
|
|
19
|
+
export function claudeCodeSkillRef(id) {
|
|
20
|
+
return `/${id}`;
|
|
21
|
+
}
|
|
18
22
|
//# sourceMappingURL=hook-events.js.map
|