sessioncost 1.1.4 → 1.2.1

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.
Files changed (2) hide show
  1. package/bin/sessioncost.js +43 -21
  2. package/package.json +1 -1
@@ -102,16 +102,50 @@ function installSteps(venv, what, which = onPath, runner = run) {
102
102
  [py, ["-m", "pip", "install", "--quiet", "--disable-pip-version-check", "--force-reinstall", "--no-deps", what]]];
103
103
  }
104
104
 
105
- function main(argv = process.argv.slice(2), env = process.env) {
105
+ // Where this version lives and what its stamp must say. The editor extension asks the same (installed, below).
106
+ function where(env = process.env, here = __dirname) {
106
107
  const dir = home(env);
107
108
  const venv = path.join(dir, "venv");
108
- const stamp = path.join(dir, "installed.txt"); // what is in the environment: changes when this package does
109
- const what = spec(env);
110
- const want = `${VERSION} ${what}`;
111
- const py = venvPython(venv);
109
+ const what = spec(env, here);
110
+ return { dir, venv, py: venvPython(venv), what, want: `${VERSION} ${what}`,
111
+ stamp: path.join(dir, "installed.txt") }; // what is in the environment: changes when this package does
112
+ }
113
+
114
+ // This version is in place and ready to start: its environment exists and its stamp matches.
115
+ function installed(env = process.env, here = __dirname) {
116
+ const w = where(env, here);
112
117
  let have = "";
113
- try { have = fs.readFileSync(stamp, "utf8").trim(); } catch (e) { /* first run */ }
114
- if (have !== want || !fs.existsSync(py)) {
118
+ try { have = fs.readFileSync(w.stamp, "utf8").trim(); } catch (e) { /* not installed yet */ }
119
+ return have === w.want && fs.existsSync(w.py);
120
+ }
121
+
122
+ // Runs the install steps (installSteps); true when this version is in place. An older version already there is
123
+ // upgraded in place: its environment is never deleted, because `setup` wrote its Python into every agent's guard
124
+ // hooks (deleting it first left the guard off while installing, and for good when the install failed).
125
+ function install(w, steps, runner = run) {
126
+ const older = fs.existsSync(w.py);
127
+ if (!older) {
128
+ fs.rmSync(w.venv, { recursive: true, force: true }); // a half-made one from an earlier failed try
129
+ fs.mkdirSync(w.dir, { recursive: true });
130
+ }
131
+ for (const [cmd, args] of older ? steps.slice(1) : steps) { // step 1 makes the environment
132
+ const r = runner(cmd, args, INSTALL_MS);
133
+ if (!r.ok) {
134
+ console.error(`SessionCost could not be installed: ${path.basename(cmd)} ${args.join(" ")}\n${r.err || ""}`.trim());
135
+ console.error(older ? "The version you had keeps working. Check your internet connection, then run npx sessioncost again."
136
+ : "Check your internet connection, then run npx sessioncost again.");
137
+ if (!older) fs.rmSync(w.venv, { recursive: true, force: true });
138
+ return false; // the old stamp stays: the next run tries again
139
+ }
140
+ }
141
+ fs.writeFileSync(w.stamp, w.want + "\n");
142
+ return true;
143
+ }
144
+
145
+ function main(argv = process.argv.slice(2), env = process.env) {
146
+ const w = where(env);
147
+ const { venv, what, py } = w;
148
+ if (!installed(env)) {
115
149
  let steps = installSteps(venv, what);
116
150
  if (!steps) {
117
151
  console.error("SessionCost runs on Python, and this computer has neither Python nor uv: installing uv " +
@@ -125,19 +159,7 @@ function main(argv = process.argv.slice(2), env = process.env) {
125
159
  return 1;
126
160
  }
127
161
  console.error(`SessionCost ${VERSION}: installing once into ${venv} …`);
128
- fs.rmSync(venv, { recursive: true, force: true });
129
- fs.mkdirSync(dir, { recursive: true });
130
- fs.rmSync(stamp, { force: true });
131
- for (const [cmd, args] of steps) {
132
- const r = run(cmd, args, INSTALL_MS);
133
- if (!r.ok) {
134
- console.error(`SessionCost could not be installed: ${path.basename(cmd)} ${args.join(" ")}\n${r.err}`.trim());
135
- console.error("Check your internet connection, then run npx sessioncost again.");
136
- fs.rmSync(venv, { recursive: true, force: true });
137
- return 1;
138
- }
139
- }
140
- fs.writeFileSync(stamp, want + "\n");
162
+ if (!install(w, steps)) return 1;
141
163
  }
142
164
  const r = spawnSync(py, ["-m", "sessioncost", ...argv], { stdio: "inherit" });
143
165
  if (r.error) {
@@ -147,6 +169,6 @@ function main(argv = process.argv.slice(2), env = process.env) {
147
169
  return r.status === null ? 1 : r.status;
148
170
  }
149
171
 
150
- module.exports = { spec, findPython, fetchUv, installSteps, venvPython, home, main, VERSION };
172
+ module.exports = { spec, findPython, fetchUv, installSteps, install, venvPython, home, where, installed, main, VERSION };
151
173
 
152
174
  if (require.main === module) process.exitCode = main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sessioncost",
3
- "version": "1.1.4",
3
+ "version": "1.2.1",
4
4
  "description": "See how many tokens each turn of your coding-agent chat sends, why, and what to cut. Runs the SessionCost Python tool (installs it once, privately).",
5
5
  "license": "MIT",
6
6
  "author": "kish21",