@cohesivity/init 0.2.2 → 0.2.4

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/cli.js +38 -17
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -34,7 +34,7 @@ const flag = (f) => { const i = argv.indexOf(f); return i >= 0 ? argv[i + 1] : u
34
34
  if (has('--help') || has('-h')) { help(); process.exit(0); }
35
35
 
36
36
  // ── config ──────────────────────────────────────────────────────────────────
37
- const PKG_VERSION = '0.2.2';
37
+ const PKG_VERSION = '0.2.4';
38
38
  const BASE = (flag('--base') || process.env.COHESIVITY_BASE || 'https://cohesivity.ai').replace(/\/+$/, '');
39
39
 
40
40
  // Machine id: one per machine, stored outside any project. A project's
@@ -142,11 +142,18 @@ function inferHarness(chain) {
142
142
  function inferModel(harness) {
143
143
  if (!harness) return null;
144
144
  try {
145
- const roots = [
146
- join(homedir(), '.' + harness),
147
- join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), harness),
148
- join(process.env.XDG_DATA_HOME || join(homedir(), '.local', 'share'), harness),
149
- ];
145
+ // A harness's state dir is not always named exactly like its process: the
146
+ // binary is cursor-agent but the dir is ~/.cursor, the script is
147
+ // pi-coding-agent but the dir is ~/.pi. So the measured name is also tried
148
+ // truncated at each hyphen — a general string rule over the name, not a
149
+ // list of harnesses. Nonexistent paths cost nothing; the walk skips them.
150
+ const names = [];
151
+ for (let n = harness; n; n = n.includes('-') ? n.slice(0, n.lastIndexOf('-')) : '') {
152
+ if (!names.includes(n)) names.push(n);
153
+ }
154
+ const cfgHome = process.env.XDG_CONFIG_HOME || join(homedir(), '.config');
155
+ const dataHome = process.env.XDG_DATA_HOME || join(homedir(), '.local', 'share');
156
+ const roots = names.flatMap((n) => [join(homedir(), '.' + n), join(cfgHome, n), join(dataHome, n)]);
150
157
  const cutoff = Date.now() - 10 * 60 * 1000;
151
158
  const files = [];
152
159
  const walk = (dir, depth) => {
@@ -163,18 +170,32 @@ function inferModel(harness) {
163
170
  };
164
171
  for (const r of roots) walk(r, 0);
165
172
  files.sort((a, b) => b.m - a.m);
173
+ // Try candidates in order until one yields a model. Testing only the
174
+ // newest file is not enough: a harness touches lock, session-env and
175
+ // file-history files alongside its session log, so the most recently
176
+ // written file frequently carries no model — and reporting "none" while
177
+ // the answer sits in the next file is a loss, not a safe default.
166
178
  const cwdKey = CWD.split('/').pop();
167
- const pick = files.find((f) => cwdKey && f.p.includes(cwdKey)) || files[0];
168
- if (!pick) return null;
169
- const fd = openSync(pick.p, 'r');
170
- const len = Math.min(pick.s, 100000);
171
- const buf = Buffer.alloc(len);
172
- readSync(fd, buf, 0, len, Math.max(0, pick.s - len));
173
- closeSync(fd);
174
- const matches = buf.toString('utf8').match(/"model(_?[iI][dD])?"\s*:\s*"([^"]{2,50})"/g);
175
- if (!matches) return null;
176
- const value = matches[matches.length - 1].match(/"([^"]{2,50})"$/)[1];
177
- return /^[A-Za-z0-9][\w .,:=[\]-]{1,63}$/.test(value) ? value : null;
179
+ const ordered = [
180
+ ...files.filter((f) => cwdKey && f.p.includes(cwdKey)),
181
+ ...files,
182
+ ].slice(0, 20);
183
+ for (const pick of ordered) {
184
+ let text;
185
+ try {
186
+ const fd = openSync(pick.p, 'r');
187
+ const len = Math.min(pick.s, 100000);
188
+ const buf = Buffer.alloc(len);
189
+ readSync(fd, buf, 0, len, Math.max(0, pick.s - len));
190
+ closeSync(fd);
191
+ text = buf.toString('utf8');
192
+ } catch { continue; }
193
+ const matches = text.match(/"model(_?[iI][dD])?"\s*:\s*"([^"]{2,50})"/g);
194
+ if (!matches) continue;
195
+ const value = matches[matches.length - 1].match(/"([^"]{2,50})"$/)[1];
196
+ if (/^[A-Za-z0-9][\w .,:=[\]-]{1,63}$/.test(value)) return value;
197
+ }
198
+ return null;
178
199
  } catch { return null; }
179
200
  }
180
201
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cohesivity/init",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Set up Cohesivity in a project: install the agent skill, create a managed backend tenant, write the project config. Run with `npx @cohesivity/init`.",
5
5
  "type": "module",
6
6
  "bin": {