@chosengeneration/light-code 0.132.0 → 0.133.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/dist/cli.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/dist/cli.cjs
3
- size: 49594454 bytes
4
- sha256: 6a5cfce656108346df75662a968ad43932fe79a85050392ef2f53a0d96847b53
3
+ size: 49612094 bytes
4
+ sha256: 7f11230ff131683d685c4a7a3e9bc657b74d3b397ea355519af84f187555676e
package/dist/server.cjs CHANGED
@@ -74785,12 +74785,14 @@ var UsageLog = class {
74785
74785
  storageDir;
74786
74786
  queueDir;
74787
74787
  manifestFile;
74788
+ rolledFile;
74788
74789
  legacyFile;
74789
74790
  session;
74790
74791
  constructor(storageDir, session) {
74791
74792
  this.storageDir = storageDir;
74792
74793
  this.queueDir = import_node_path43.default.join(storageDir, "usage", "queue");
74793
74794
  this.manifestFile = import_node_path43.default.join(storageDir, "usage", "pushed.json");
74795
+ this.rolledFile = import_node_path43.default.join(storageDir, "usage", "rolled.json");
74794
74796
  this.legacyFile = import_node_path43.default.join(storageDir, "usage-events.jsonl");
74795
74797
  this.session = session ?? (0, import_node_crypto12.randomBytes)(6).toString("hex");
74796
74798
  }
@@ -74883,6 +74885,22 @@ var UsageLog = class {
74883
74885
  const days = { ...await this.pushed(target), [day]: signature };
74884
74886
  await writeFileAtomically(this.manifestFile, JSON.stringify({ target, days }));
74885
74887
  }
74888
+ /** Months this install has rolled up in `target` (`usage/rollup.ts`): their late days go into the month. */
74889
+ async rolledMonths(target) {
74890
+ try {
74891
+ const saved = JSON.parse(await import_node_fs6.promises.readFile(this.rolledFile, "utf8"));
74892
+ return new Set(saved.target === target && Array.isArray(saved.months) ? saved.months.filter((month) => typeof month === "string") : []);
74893
+ } catch {
74894
+ return /* @__PURE__ */ new Set();
74895
+ }
74896
+ }
74897
+ async markRolled(target, month) {
74898
+ const months = await this.rolledMonths(target);
74899
+ if (months.has(month))
74900
+ return;
74901
+ months.add(month);
74902
+ await writeFileAtomically(this.rolledFile, JSON.stringify({ target, months: [...months].sort() }));
74903
+ }
74886
74904
  /** Deletes whole files of days older than `keepDays` that were pushed as they are. */
74887
74905
  async prune(target, keepDays, now = Date.now()) {
74888
74906
  const cutoff = usageDay(now - keepDays * 864e5);
@@ -74956,12 +74974,45 @@ function parseShardName(name) {
74956
74974
  const match = /^(\d{4}-\d{2}-\d{2})__([A-Za-z0-9]+)\.jsonl$/.exec(name);
74957
74975
  return match === null ? void 0 : { day: match[1], instance: match[2] };
74958
74976
  }
74977
+ function parseMonthName(name) {
74978
+ const match = /^(\d{4}-\d{2})__([A-Za-z0-9]+)\.jsonl$/.exec(name);
74979
+ return match === null ? void 0 : { month: match[1], instance: match[2] };
74980
+ }
74981
+ function shardFileName(shard) {
74982
+ return `${shard.day ?? shard.month ?? ""}__${shard.instance}.jsonl`;
74983
+ }
74984
+ function selectShards(files, fromDay, toDay) {
74985
+ const rolled = /* @__PURE__ */ new Set();
74986
+ for (const file2 of files) {
74987
+ const month = parseMonthName(file2.name);
74988
+ if (month !== void 0)
74989
+ rolled.add(`${file2.user}|${month.month}|${month.instance}`);
74990
+ }
74991
+ return files.filter((file2) => {
74992
+ const month = parseMonthName(file2.name);
74993
+ if (month !== void 0)
74994
+ return month.month >= fromDay.slice(0, 7) && month.month <= toDay.slice(0, 7);
74995
+ const day = parseShardName(file2.name);
74996
+ if (day === void 0 || day.day < fromDay || day.day > toDay)
74997
+ return false;
74998
+ return !rolled.has(`${file2.user}|${day.day.slice(0, 7)}|${day.instance}`);
74999
+ });
75000
+ }
75001
+ function ownShards(files, instance) {
75002
+ const own = [];
75003
+ for (const file2 of files) {
75004
+ const day = parseShardName(file2.name);
75005
+ if (day !== void 0 && day.instance === instance)
75006
+ own.push({ user: file2.user, day: day.day, instance });
75007
+ const month = parseMonthName(file2.name);
75008
+ if (month !== void 0 && month.instance === instance)
75009
+ own.push({ user: file2.user, month: month.month, instance });
75010
+ }
75011
+ return own;
75012
+ }
74959
75013
  function shardText(events) {
74960
75014
  return events.map((event) => JSON.stringify(event)).join("\n") + "\n";
74961
75015
  }
74962
- function inRange(day, fromDay, toDay) {
74963
- return day >= fromDay && day <= toDay;
74964
- }
74965
75016
  async function pooled(items, width, work) {
74966
75017
  const results = new Array(items.length);
74967
75018
  let next = 0;
@@ -75020,6 +75071,16 @@ var FolderShardStore = class {
75020
75071
  }
75021
75072
  return (Array.isArray(answer["events"]) ? answer["events"] : []).filter(isUsageEvent);
75022
75073
  }
75074
+ const files = await this.listFiles();
75075
+ const wanted = selectShards(files, fromDay, toDay);
75076
+ const texts = await pooled(wanted, 8, (file2) => import_node_fs7.promises.readFile(import_node_path44.default.join(this.root, file2.user, file2.name), "utf8").catch((error51) => {
75077
+ this.lastProblems.push(`${import_node_path44.default.join(this.root, file2.user, file2.name)}: ${error51 instanceof Error ? error51.message : String(error51)}`);
75078
+ return "";
75079
+ }));
75080
+ return texts.flatMap(parseUsageLines);
75081
+ }
75082
+ /** Every shard file, as `{ user, name }` - the person's folder and the file in it. */
75083
+ async listFiles() {
75023
75084
  let users;
75024
75085
  try {
75025
75086
  users = (await import_node_fs7.promises.readdir(this.root, { withFileTypes: true })).filter((entry) => entry.isDirectory() && entry.name !== "_profiles").map((entry) => entry.name);
@@ -75038,17 +75099,29 @@ var FolderShardStore = class {
75038
75099
  names = [];
75039
75100
  this.lastProblems.push(`${folder}: ${error51 instanceof Error ? error51.message : String(error51)}`);
75040
75101
  }
75041
- for (const name of names) {
75042
- const parsed = parseShardName(name);
75043
- if (parsed !== void 0 && inRange(parsed.day, fromDay, toDay))
75044
- files.push(import_node_path44.default.join(folder, name));
75045
- }
75102
+ for (const name of names)
75103
+ if (parseShardName(name) !== void 0 || parseMonthName(name) !== void 0)
75104
+ files.push({ user, name });
75046
75105
  }
75047
- const texts = await pooled(files, 8, (file2) => import_node_fs7.promises.readFile(file2, "utf8").catch((error51) => {
75048
- this.lastProblems.push(`${file2}: ${error51 instanceof Error ? error51.message : String(error51)}`);
75049
- return "";
75050
- }));
75051
- return texts.flatMap(parseUsageLines);
75106
+ return files;
75107
+ }
75108
+ async listOwn(instance) {
75109
+ return ownShards(await this.listFiles(), instance);
75110
+ }
75111
+ async readShard(shard) {
75112
+ try {
75113
+ return parseUsageLines(await import_node_fs7.promises.readFile(import_node_path44.default.join(this.root, shardSegment(shard.user), shardFileName(shard)), "utf8"));
75114
+ } catch (error51) {
75115
+ if (error51.code === "ENOENT")
75116
+ return void 0;
75117
+ throw error51;
75118
+ }
75119
+ }
75120
+ async putMonth(shard, events) {
75121
+ await this.writeAtomically(import_node_path44.default.join(this.root, shardSegment(shard.user), shardFileName(shard)), shardText(events));
75122
+ }
75123
+ async removeShard(shard) {
75124
+ await import_node_fs7.promises.rm(import_node_path44.default.join(this.root, shardSegment(shard.user), shardFileName(shard)), { force: true });
75052
75125
  }
75053
75126
  async putProfile(user, profile) {
75054
75127
  this.profilesFromRead = void 0;
@@ -75091,15 +75164,45 @@ var S3ShardStore = class {
75091
75164
  const key2 = `${this.base}${shardSegment(shard.user)}/${shard.day}__${shard.instance}.jsonl`;
75092
75165
  await this.client.put(key2, Buffer.from(shardText(events), "utf8"), "application/x-ndjson");
75093
75166
  }
75094
- async read(fromDay, toDay) {
75167
+ /** Every shard object, as `{ user, name, key }`. */
75168
+ async listFiles() {
75095
75169
  const objects = await this.client.list(this.base, 2e5);
75096
- const wanted = objects.filter((object3) => {
75097
- const parsed = parseShardName(object3.key.slice(object3.key.lastIndexOf("/") + 1));
75098
- return parsed !== void 0 && inRange(parsed.day, fromDay, toDay);
75099
- });
75100
- const texts = await pooled(wanted, 8, async (object3) => (await this.client.get(object3.key)).toString("utf8"));
75170
+ return objects.map((object3) => {
75171
+ const rest = object3.key.slice(this.base.length);
75172
+ const slash = rest.indexOf("/");
75173
+ return { user: rest.slice(0, slash), name: rest.slice(slash + 1), key: object3.key };
75174
+ }).filter((file2) => file2.user.length > 0 && file2.user !== "_profiles" && !file2.name.includes("/"));
75175
+ }
75176
+ async read(fromDay, toDay) {
75177
+ const wanted = selectShards(await this.listFiles(), fromDay, toDay);
75178
+ const texts = await pooled(wanted, 8, async (file2) => (await this.client.get(file2.key)).toString("utf8"));
75101
75179
  return texts.flatMap(parseUsageLines);
75102
75180
  }
75181
+ keyOf(shard) {
75182
+ return `${this.base}${shardSegment(shard.user)}/${shardFileName(shard)}`;
75183
+ }
75184
+ async listOwn(instance) {
75185
+ return ownShards(await this.listFiles(), instance);
75186
+ }
75187
+ async readShard(shard) {
75188
+ try {
75189
+ return parseUsageLines((await this.client.get(this.keyOf(shard))).toString("utf8"));
75190
+ } catch (error51) {
75191
+ if (/\b404\b|NoSuchKey|not found/i.test(error51 instanceof Error ? error51.message : String(error51)))
75192
+ return void 0;
75193
+ throw error51;
75194
+ }
75195
+ }
75196
+ async putMonth(shard, events) {
75197
+ await this.client.put(this.keyOf(shard), Buffer.from(shardText(events), "utf8"), "application/x-ndjson");
75198
+ }
75199
+ /**
75200
+ * The second caller of `S3Client.remove` (§13 kept it to `s3/remove.ts`): only this install's own usage
75201
+ * day shards, only after the month that replaces them was written and read back complete.
75202
+ */
75203
+ async removeShard(shard) {
75204
+ await this.client.remove(this.keyOf(shard));
75205
+ }
75103
75206
  async putProfile(user, profile) {
75104
75207
  await this.client.put(`${this.base}_profiles/${shardSegment(user)}.json`, Buffer.from(JSON.stringify(cleanProfile(profile)), "utf8"), "application/json");
75105
75208
  }
@@ -75160,14 +75263,37 @@ var VectorShardStore = class {
75160
75263
  }
75161
75264
  ]);
75162
75265
  }
75163
- async read(fromDay, toDay) {
75164
- const events = [];
75266
+ /** Every shard document, as `{ user, name, text }` from its `usage/<user>/<name>` path. */
75267
+ async listFiles() {
75268
+ const files = [];
75165
75269
  await this.scanAll((document) => {
75166
- const parsed = parseShardName(document.path.slice(document.path.lastIndexOf("/") + 1));
75167
- if (parsed !== void 0 && inRange(parsed.day, fromDay, toDay))
75168
- events.push(...parseUsageLines(document.text));
75270
+ const match = /^usage\/([^/]+)\/([^/]+)$/.exec(document.path);
75271
+ if (match === null || match[1] === "_profiles")
75272
+ return;
75273
+ files.push({ user: match[1], name: match[2], text: document.text });
75169
75274
  });
75170
- return events;
75275
+ return files;
75276
+ }
75277
+ async read(fromDay, toDay) {
75278
+ return selectShards(await this.listFiles(), fromDay, toDay).flatMap((file2) => parseUsageLines(file2.text));
75279
+ }
75280
+ async listOwn(instance) {
75281
+ return ownShards(await this.listFiles(), instance);
75282
+ }
75283
+ async readShard(shard) {
75284
+ const name = shardFileName(shard);
75285
+ const found2 = (await this.listFiles()).find((file2) => file2.user === shardSegment(shard.user) && file2.name === name);
75286
+ return found2 === void 0 ? void 0 : parseUsageLines(found2.text);
75287
+ }
75288
+ async putMonth(shard, events) {
75289
+ await this.ensure();
75290
+ const user = shardSegment(shard.user);
75291
+ await this.writer.upsert(this.collection, [
75292
+ { id: `usage-month:${user}:${shard.month}:${shard.instance}`, text: shardText(events), path: `usage/${user}/${shardFileName(shard)}`, startLine: 0, endLine: 0, vector: [1, 0], owner: shard.user }
75293
+ ]);
75294
+ }
75295
+ async removeShard(shard) {
75296
+ await this.writer.deleteByPaths(this.collection, [`usage/${shardSegment(shard.user)}/${shardFileName(shard)}`]);
75171
75297
  }
75172
75298
  async putProfile(user, profile) {
75173
75299
  await this.ensure();
@@ -75196,6 +75322,7 @@ async function pushUsage(options) {
75196
75322
  const pushedDays = /* @__PURE__ */ new Set();
75197
75323
  const again = new Set(options.recheck ?? []);
75198
75324
  let events = 0;
75325
+ const rolled = await options.log.rolledMonths(options.target);
75199
75326
  for (let round2 = 0; round2 < (options.maxRounds ?? 5); round2++) {
75200
75327
  const days = await options.log.days();
75201
75328
  const pushed = await options.log.pushed(options.target);
@@ -75208,8 +75335,16 @@ async function pushUsage(options) {
75208
75335
  const byUser = /* @__PURE__ */ new Map();
75209
75336
  for (const event of ofDay)
75210
75337
  byUser.set(event.u, [...byUser.get(event.u) ?? [], event]);
75211
- for (const [user, userEvents] of byUser)
75212
- await options.store.put({ user, day, instance: options.instance }, userEvents);
75338
+ const month = day.slice(0, 7);
75339
+ for (const [user, userEvents] of byUser) {
75340
+ if (rolled.has(month)) {
75341
+ const shard = { user, month, instance: options.instance };
75342
+ const kept = (await options.store.readShard(shard) ?? []).filter((event) => usageDay(event.t) !== day);
75343
+ await options.store.putMonth(shard, [...kept, ...userEvents].sort((a, b) => a.t - b.t));
75344
+ } else {
75345
+ await options.store.put({ user, day, instance: options.instance }, userEvents);
75346
+ }
75347
+ }
75213
75348
  const after = await options.log.daySignature(day);
75214
75349
  if (after === before) {
75215
75350
  await options.log.markPushed(options.target, day, after);
@@ -75226,6 +75361,70 @@ async function pushUsage(options) {
75226
75361
  return { pushedDays: pushedDays.size, events };
75227
75362
  }
75228
75363
 
75364
+ // ../../packages/core/dist/usage/rollup.js
75365
+ async function rollUpMonths(options) {
75366
+ const now = options.now ?? Date.now();
75367
+ const grace = (options.graceDays ?? 3) * 864e5;
75368
+ const currentMonth = usageDay(now).slice(0, 7);
75369
+ const monthEnd = (month) => {
75370
+ const [year, number5] = month.split("-").map(Number);
75371
+ return Date.UTC(year, number5, 1);
75372
+ };
75373
+ const eligible = (month) => month < currentMonth && now >= monthEnd(month) + grace;
75374
+ const pushed = await options.log.pushed(options.target);
75375
+ const blocked = /* @__PURE__ */ new Set();
75376
+ for (const [day, entry] of await options.log.days())
75377
+ if (pushed[day] !== entry.signature)
75378
+ blocked.add(day.slice(0, 7));
75379
+ const groups = /* @__PURE__ */ new Map();
75380
+ for (const shard of await options.store.listOwn(options.instance)) {
75381
+ const month = shard.month ?? shard.day?.slice(0, 7);
75382
+ if (month === void 0)
75383
+ continue;
75384
+ const key2 = `${shard.user}|${month}`;
75385
+ const group = groups.get(key2) ?? { user: shard.user, month, days: [] };
75386
+ if (shard.month !== void 0)
75387
+ group.monthShard = shard;
75388
+ else
75389
+ group.days.push(shard);
75390
+ groups.set(key2, group);
75391
+ }
75392
+ let months = 0;
75393
+ let removed = 0;
75394
+ const problems = [];
75395
+ for (const group of groups.values()) {
75396
+ if (group.monthShard !== void 0)
75397
+ await options.log.markRolled(options.target, group.month);
75398
+ if (group.days.length === 0 || !eligible(group.month) || blocked.has(group.month))
75399
+ continue;
75400
+ const monthShard = { user: group.user, month: group.month, instance: options.instance };
75401
+ const covered = /* @__PURE__ */ new Set();
75402
+ const fromDays = [];
75403
+ for (const shard of group.days) {
75404
+ const events = await options.store.readShard(shard);
75405
+ if (events === void 0)
75406
+ continue;
75407
+ covered.add(shard.day);
75408
+ fromDays.push(...events);
75409
+ }
75410
+ const existing = await options.store.readShard(monthShard) ?? [];
75411
+ const merged = [...existing.filter((event) => !covered.has(usageDay(event.t))), ...fromDays].sort((a, b) => a.t - b.t);
75412
+ await options.store.putMonth(monthShard, merged);
75413
+ const back = await options.store.readShard(monthShard);
75414
+ if (back === void 0 || back.length !== merged.length) {
75415
+ problems.push(`${group.user} ${group.month}: the month read back with ${String(back?.length ?? 0)} of ${String(merged.length)} events, so its days were kept.`);
75416
+ continue;
75417
+ }
75418
+ await options.log.markRolled(options.target, group.month);
75419
+ for (const shard of group.days) {
75420
+ await options.store.removeShard(shard);
75421
+ removed++;
75422
+ }
75423
+ months++;
75424
+ }
75425
+ return { months, removed, problems };
75426
+ }
75427
+
75229
75428
  // ../../packages/core/dist/usage/dashboard.js
75230
75429
  var HOUR = 36e5;
75231
75430
  var DAY = 24 * HOUR;
@@ -76443,6 +76642,7 @@ ${preview.text}` } : {}
76443
76642
  let cachedUsageChoice;
76444
76643
  let usageKicked = false;
76445
76644
  let usageRechecked = false;
76645
+ let usageLastRollUp = 0;
76446
76646
  let cachedProfileFresh;
76447
76647
  let directoryChecked = false;
76448
76648
  let lastToolNames;
@@ -82424,6 +82624,14 @@ ${redact(shown2, cachedSecretValues)}` }
82424
82624
  ...usageRechecked ? {} : { recheck: [usageDay(Date.now()), usageDay(Date.now() - 864e5)] }
82425
82625
  });
82426
82626
  usageRechecked = true;
82627
+ if (Date.now() - usageLastRollUp > 864e5) {
82628
+ usageLastRollUp = Date.now();
82629
+ const rolled = await rollUpMonths({ log: usageLog, store: resolved.store, target: resolved.target, instance: usageInstance });
82630
+ for (const problem of rolled.problems)
82631
+ logger.warn(`usage: roll-up: ${problem}`);
82632
+ if (rolled.months > 0)
82633
+ logger.info(`usage: rolled up ${String(rolled.months)} month(s), removing ${String(rolled.removed)} day files`);
82634
+ }
82427
82635
  usageLastProblem = void 0;
82428
82636
  void enrichFromDirectory(namesToEnrich(await usageLog.all()));
82429
82637
  await usageLog.prune(resolved.target, 14);
@@ -87581,8 +87789,13 @@ own user.
87581
87789
 
87582
87790
  **In a shared folder the statistics are plain files**: one JSON Lines file per person, per day, per
87583
87791
  install (\`<folder>\\<user>\\2026-10-10__<install>.jsonl\`, one event per line) and one small JSON file
87584
- per person in \`_profiles\`. No database and no locks: each file is written whole and swapped in,
87585
- so readers never see half of one, and nothing is ever deleted.
87792
+ per person in \`_profiles\`. No database and no locks: each file is written whole and swapped in, so
87793
+ readers never see half of one. **Each month is rolled up**: three days after a month ends, each machine
87794
+ merges its own day files for it into one month file (\`2026-09__<install>.jsonl\`), reads it back to
87795
+ check every event is there, and only then deletes those day files - so a year is about a dozen files per
87796
+ person and machine rather than a few hundred. Only a machine's own files are ever merged or deleted. In a
87797
+ bucket the layout is the same under \`usage/\`; in OpenSearch, Qdrant or Chroma each file is one document
87798
+ (its text the events, its vector a placeholder - nothing is searched by meaning there).
87586
87799
 
87587
87800
  **What is collected**: the skill or tool name, who, when, the project, the author named on the skill or
87588
87801
  tool, whether a tool call failed and how long it took. Never arguments, file contents or results. A
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chosengeneration/light-code",
3
- "version": "0.132.0",
3
+ "version": "0.133.0",
4
4
  "description": "A minimal agentic coding assistant, served to your browser from a local Node server",
5
5
  "license": "MIT",
6
6
  "type": "module",