@codyswann/lisa 7.0.4 → 7.1.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 (175) hide show
  1. package/all/copy-overwrite/scripts/lib/work-item-estimate-calibration.mjs +131 -0
  2. package/all/copy-overwrite/scripts/lib/work-item-estimate-check.mjs +15 -5
  3. package/all/copy-overwrite/scripts/lisa-estimate.mjs +29 -3
  4. package/dist/core/lisa-owned-hash-ledger.d.ts.map +1 -1
  5. package/dist/core/lisa-owned-hash-ledger.js +18 -0
  6. package/dist/core/lisa-owned-hash-ledger.js.map +1 -1
  7. package/dist/core/nightly-e2e-guard-behavior-certificate.js +2 -2
  8. package/dist/core/upstream-evidence-manifest.d.ts.map +1 -1
  9. package/dist/core/upstream-evidence-manifest.js +42 -17
  10. package/dist/core/upstream-evidence-manifest.js.map +1 -1
  11. package/dist/utils/usage-accounting.d.ts.map +1 -1
  12. package/dist/utils/usage-accounting.js +3 -1
  13. package/dist/utils/usage-accounting.js.map +1 -1
  14. package/package.json +4 -4
  15. package/plugins/lisa/.claude-plugin/plugin.json +1 -1
  16. package/plugins/lisa/.codex-plugin/plugin.json +1 -1
  17. package/plugins/lisa/.codex-plugin/skills/lisa-github-validate-issue/SKILL.md +2 -0
  18. package/plugins/lisa/.codex-plugin/skills/lisa-jira-validate-ticket/SKILL.md +2 -0
  19. package/plugins/lisa/.codex-plugin/skills/lisa-linear-validate-issue/SKILL.md +2 -0
  20. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/SKILL.md +45 -15
  21. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/cli-runtime.mjs +1 -0
  22. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/cli-usage.mjs +10 -3
  23. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/cli-workers.mjs +52 -1
  24. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/orphan-sweep.mjs +2 -20
  25. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/session-manager.mjs +5 -1
  26. package/plugins/lisa/.codex-plugin/skills/lisa-session-manager/scripts/take-check.mjs +110 -0
  27. package/plugins/lisa/.codex-plugin/skills/lisa-track/SKILL.md +11 -1
  28. package/plugins/lisa/.codex-plugin/skills/lisa-tracker-claim/SKILL.md +1 -0
  29. package/plugins/lisa/.codex-plugin/skills/lisa-usage-accounting/references/estimate.md +25 -2
  30. package/plugins/lisa/rules/reference/work-item-definition-of-ready.md +7 -2
  31. package/plugins/lisa/skills/lisa-github-validate-issue/SKILL.md +2 -0
  32. package/plugins/lisa/skills/lisa-jira-validate-ticket/SKILL.md +2 -0
  33. package/plugins/lisa/skills/lisa-linear-validate-issue/SKILL.md +2 -0
  34. package/plugins/lisa/skills/lisa-session-manager/SKILL.md +45 -15
  35. package/plugins/lisa/skills/lisa-session-manager/scripts/cli-runtime.mjs +1 -0
  36. package/plugins/lisa/skills/lisa-session-manager/scripts/cli-usage.mjs +10 -3
  37. package/plugins/lisa/skills/lisa-session-manager/scripts/cli-workers.mjs +52 -1
  38. package/plugins/lisa/skills/lisa-session-manager/scripts/orphan-sweep.mjs +2 -20
  39. package/plugins/lisa/skills/lisa-session-manager/scripts/session-manager.mjs +5 -1
  40. package/plugins/lisa/skills/lisa-session-manager/scripts/take-check.mjs +110 -0
  41. package/plugins/lisa/skills/lisa-track/SKILL.md +11 -1
  42. package/plugins/lisa/skills/lisa-tracker-claim/SKILL.md +1 -0
  43. package/plugins/lisa/skills/lisa-usage-accounting/references/estimate.md +25 -2
  44. package/plugins/lisa-agy/plugin.json +1 -1
  45. package/plugins/lisa-agy/skills/lisa-github-validate-issue/SKILL.md +2 -0
  46. package/plugins/lisa-agy/skills/lisa-jira-validate-ticket/SKILL.md +2 -0
  47. package/plugins/lisa-agy/skills/lisa-linear-validate-issue/SKILL.md +2 -0
  48. package/plugins/lisa-agy/skills/lisa-session-manager/SKILL.md +45 -15
  49. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/cli-runtime.mjs +1 -0
  50. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/cli-usage.mjs +10 -3
  51. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/cli-workers.mjs +52 -1
  52. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/orphan-sweep.mjs +2 -20
  53. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/session-manager.mjs +5 -1
  54. package/plugins/lisa-agy/skills/lisa-session-manager/scripts/take-check.mjs +110 -0
  55. package/plugins/lisa-agy/skills/lisa-track/SKILL.md +11 -1
  56. package/plugins/lisa-agy/skills/lisa-tracker-claim/SKILL.md +1 -0
  57. package/plugins/lisa-agy/skills/lisa-usage-accounting/references/estimate.md +25 -2
  58. package/plugins/lisa-cdk/.claude-plugin/plugin.json +1 -1
  59. package/plugins/lisa-cdk/.codex-plugin/plugin.json +1 -1
  60. package/plugins/lisa-cdk-agy/plugin.json +1 -1
  61. package/plugins/lisa-cdk-copilot/.claude-plugin/plugin.json +1 -1
  62. package/plugins/lisa-cdk-cursor/.claude-plugin/plugin.json +1 -1
  63. package/plugins/lisa-cdk-devin/.claude-plugin/plugin.json +1 -1
  64. package/plugins/lisa-cdk-devin/.devin-plugin/plugin.json +1 -1
  65. package/plugins/lisa-copilot/.claude-plugin/plugin.json +1 -1
  66. package/plugins/lisa-copilot/rules/reference/work-item-definition-of-ready.md +7 -2
  67. package/plugins/lisa-copilot/skills/lisa-github-validate-issue/SKILL.md +2 -0
  68. package/plugins/lisa-copilot/skills/lisa-jira-validate-ticket/SKILL.md +2 -0
  69. package/plugins/lisa-copilot/skills/lisa-linear-validate-issue/SKILL.md +2 -0
  70. package/plugins/lisa-copilot/skills/lisa-session-manager/SKILL.md +45 -15
  71. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/cli-runtime.mjs +1 -0
  72. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/cli-usage.mjs +10 -3
  73. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/cli-workers.mjs +52 -1
  74. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/orphan-sweep.mjs +2 -20
  75. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/session-manager.mjs +5 -1
  76. package/plugins/lisa-copilot/skills/lisa-session-manager/scripts/take-check.mjs +110 -0
  77. package/plugins/lisa-copilot/skills/lisa-track/SKILL.md +11 -1
  78. package/plugins/lisa-copilot/skills/lisa-tracker-claim/SKILL.md +1 -0
  79. package/plugins/lisa-copilot/skills/lisa-usage-accounting/references/estimate.md +25 -2
  80. package/plugins/lisa-cursor/.claude-plugin/plugin.json +1 -1
  81. package/plugins/lisa-cursor/rules/work-item-definition-of-ready-reference.mdc +7 -2
  82. package/plugins/lisa-cursor/skills/lisa-github-validate-issue/SKILL.md +2 -0
  83. package/plugins/lisa-cursor/skills/lisa-jira-validate-ticket/SKILL.md +2 -0
  84. package/plugins/lisa-cursor/skills/lisa-linear-validate-issue/SKILL.md +2 -0
  85. package/plugins/lisa-cursor/skills/lisa-session-manager/SKILL.md +45 -15
  86. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/cli-runtime.mjs +1 -0
  87. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/cli-usage.mjs +10 -3
  88. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/cli-workers.mjs +52 -1
  89. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/orphan-sweep.mjs +2 -20
  90. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/session-manager.mjs +5 -1
  91. package/plugins/lisa-cursor/skills/lisa-session-manager/scripts/take-check.mjs +110 -0
  92. package/plugins/lisa-cursor/skills/lisa-track/SKILL.md +11 -1
  93. package/plugins/lisa-cursor/skills/lisa-tracker-claim/SKILL.md +1 -0
  94. package/plugins/lisa-cursor/skills/lisa-usage-accounting/references/estimate.md +25 -2
  95. package/plugins/lisa-devin/.claude-plugin/plugin.json +1 -1
  96. package/plugins/lisa-devin/.devin-plugin/plugin.json +1 -1
  97. package/plugins/lisa-devin/rules/reference/work-item-definition-of-ready.md +7 -2
  98. package/plugins/lisa-devin/skills/lisa-github-validate-issue/SKILL.md +2 -0
  99. package/plugins/lisa-devin/skills/lisa-jira-validate-ticket/SKILL.md +2 -0
  100. package/plugins/lisa-devin/skills/lisa-linear-validate-issue/SKILL.md +2 -0
  101. package/plugins/lisa-devin/skills/lisa-session-manager/SKILL.md +45 -15
  102. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/cli-runtime.mjs +1 -0
  103. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/cli-usage.mjs +10 -3
  104. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/cli-workers.mjs +52 -1
  105. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/orphan-sweep.mjs +2 -20
  106. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/session-manager.mjs +5 -1
  107. package/plugins/lisa-devin/skills/lisa-session-manager/scripts/take-check.mjs +110 -0
  108. package/plugins/lisa-devin/skills/lisa-track/SKILL.md +11 -1
  109. package/plugins/lisa-devin/skills/lisa-tracker-claim/SKILL.md +1 -0
  110. package/plugins/lisa-devin/skills/lisa-usage-accounting/references/estimate.md +25 -2
  111. package/plugins/lisa-expo/.claude-plugin/plugin.json +1 -1
  112. package/plugins/lisa-expo/.codex-plugin/plugin.json +1 -1
  113. package/plugins/lisa-expo-agy/plugin.json +1 -1
  114. package/plugins/lisa-expo-copilot/.claude-plugin/plugin.json +1 -1
  115. package/plugins/lisa-expo-cursor/.claude-plugin/plugin.json +1 -1
  116. package/plugins/lisa-expo-devin/.claude-plugin/plugin.json +1 -1
  117. package/plugins/lisa-expo-devin/.devin-plugin/plugin.json +1 -1
  118. package/plugins/lisa-harper-fabric/.claude-plugin/plugin.json +1 -1
  119. package/plugins/lisa-harper-fabric/.codex-plugin/plugin.json +1 -1
  120. package/plugins/lisa-harper-fabric-agy/plugin.json +1 -1
  121. package/plugins/lisa-harper-fabric-copilot/.claude-plugin/plugin.json +1 -1
  122. package/plugins/lisa-harper-fabric-cursor/.claude-plugin/plugin.json +1 -1
  123. package/plugins/lisa-harper-fabric-devin/.claude-plugin/plugin.json +1 -1
  124. package/plugins/lisa-harper-fabric-devin/.devin-plugin/plugin.json +1 -1
  125. package/plugins/lisa-nestjs/.claude-plugin/plugin.json +1 -1
  126. package/plugins/lisa-nestjs/.codex-plugin/plugin.json +1 -1
  127. package/plugins/lisa-nestjs-agy/plugin.json +1 -1
  128. package/plugins/lisa-nestjs-copilot/.claude-plugin/plugin.json +1 -1
  129. package/plugins/lisa-nestjs-cursor/.claude-plugin/plugin.json +1 -1
  130. package/plugins/lisa-nestjs-devin/.claude-plugin/plugin.json +1 -1
  131. package/plugins/lisa-nestjs-devin/.devin-plugin/plugin.json +1 -1
  132. package/plugins/lisa-phaser/.claude-plugin/plugin.json +1 -1
  133. package/plugins/lisa-phaser/.codex-plugin/plugin.json +1 -1
  134. package/plugins/lisa-phaser-agy/plugin.json +1 -1
  135. package/plugins/lisa-phaser-copilot/.claude-plugin/plugin.json +1 -1
  136. package/plugins/lisa-phaser-cursor/.claude-plugin/plugin.json +1 -1
  137. package/plugins/lisa-phaser-devin/.claude-plugin/plugin.json +1 -1
  138. package/plugins/lisa-phaser-devin/.devin-plugin/plugin.json +1 -1
  139. package/plugins/lisa-rails/.claude-plugin/plugin.json +1 -1
  140. package/plugins/lisa-rails/.codex-plugin/plugin.json +1 -1
  141. package/plugins/lisa-rails-agy/plugin.json +1 -1
  142. package/plugins/lisa-rails-copilot/.claude-plugin/plugin.json +1 -1
  143. package/plugins/lisa-rails-cursor/.claude-plugin/plugin.json +1 -1
  144. package/plugins/lisa-rails-devin/.claude-plugin/plugin.json +1 -1
  145. package/plugins/lisa-rails-devin/.devin-plugin/plugin.json +1 -1
  146. package/plugins/lisa-typescript/.claude-plugin/plugin.json +1 -1
  147. package/plugins/lisa-typescript/.codex-plugin/plugin.json +1 -1
  148. package/plugins/lisa-typescript-agy/plugin.json +1 -1
  149. package/plugins/lisa-typescript-copilot/.claude-plugin/plugin.json +1 -1
  150. package/plugins/lisa-typescript-cursor/.claude-plugin/plugin.json +1 -1
  151. package/plugins/lisa-typescript-devin/.claude-plugin/plugin.json +1 -1
  152. package/plugins/lisa-typescript-devin/.devin-plugin/plugin.json +1 -1
  153. package/plugins/lisa-wiki/.claude-plugin/plugin.json +1 -1
  154. package/plugins/lisa-wiki/.codex-plugin/plugin.json +1 -1
  155. package/plugins/lisa-wiki-agy/plugin.json +1 -1
  156. package/plugins/lisa-wiki-copilot/.claude-plugin/plugin.json +1 -1
  157. package/plugins/lisa-wiki-cursor/.claude-plugin/plugin.json +1 -1
  158. package/plugins/lisa-wiki-devin/.claude-plugin/plugin.json +1 -1
  159. package/plugins/lisa-wiki-devin/.devin-plugin/plugin.json +1 -1
  160. package/plugins/src/base/rules/reference/work-item-definition-of-ready.md +7 -2
  161. package/plugins/src/base/skills/lisa-github-validate-issue/SKILL.md +2 -0
  162. package/plugins/src/base/skills/lisa-jira-validate-ticket/SKILL.md +2 -0
  163. package/plugins/src/base/skills/lisa-linear-validate-issue/SKILL.md +2 -0
  164. package/plugins/src/base/skills/lisa-session-manager/SKILL.md +45 -15
  165. package/plugins/src/base/skills/lisa-session-manager/scripts/cli-runtime.mjs +1 -0
  166. package/plugins/src/base/skills/lisa-session-manager/scripts/cli-usage.mjs +10 -3
  167. package/plugins/src/base/skills/lisa-session-manager/scripts/cli-workers.mjs +52 -1
  168. package/plugins/src/base/skills/lisa-session-manager/scripts/orphan-sweep.mjs +2 -20
  169. package/plugins/src/base/skills/lisa-session-manager/scripts/session-manager.mjs +5 -1
  170. package/plugins/src/base/skills/lisa-session-manager/scripts/take-check.mjs +110 -0
  171. package/plugins/src/base/skills/lisa-track/SKILL.md +11 -1
  172. package/plugins/src/base/skills/lisa-tracker-claim/SKILL.md +1 -0
  173. package/plugins/src/base/skills/lisa-usage-accounting/references/estimate.md +25 -2
  174. package/scripts/two-channel-couplings.json +1 -1
  175. package/typescript/copy-overwrite/.prettierignore +1 -0
@@ -5,7 +5,9 @@
5
5
  * `spawn` is the one that starts yolo-mode sessions, so its checks run in a
6
6
  * fixed order and each has its own exit code the manager skill branches on:
7
7
  * the repo must be a Lisa repo (2), there must be capacity (3), and no live
8
- * worker may already hold the ref (4). Only then does it run
8
+ * worker may already hold the ref (4), and the item must be taken: its
9
+ * read-back assignees (`--assignees`) name "me" or an operator and nobody
10
+ * else (5, take-check.mjs). Only then does it run
9
11
  * `claude --bg`, with the cwd set to the routed repo and every `TELEGRAM_*`
10
12
  * variable removed from the worker's environment. A repo whose workspace
11
13
  * trust was never accepted is exit 2 too (see worker-launch.mjs). So is a
@@ -25,6 +27,8 @@ import { createHash } from "node:crypto";
25
27
  import { readFile } from "node:fs/promises";
26
28
 
27
29
  import { computeCapacity, readMachineStats } from "./capacity.mjs";
30
+ import { parseJson } from "./json-input.mjs";
31
+ import { judgeTake } from "./take-check.mjs";
28
32
  import {
29
33
  CliError,
30
34
  EXIT,
@@ -260,6 +264,7 @@ export async function spawn(ctx) {
260
264
  }
261
265
 
262
266
  const ledger = await readLedger(ctx.root);
267
+ refuseUntaken(ctx, config, ledger, ref);
263
268
  const repo = key ?? ".";
264
269
  refuseSetupGap(ledger, repo);
265
270
  // A spawn no longer clears a pending question (lisa#4571), and a worker
@@ -299,6 +304,52 @@ export async function spawn(ctx) {
299
304
  return { json: { ref, name, sessionId, repo, cwd: repoDir, firstPrompt } };
300
305
  }
301
306
 
307
+ /**
308
+ * Refuse, with exit 5, an item that was not taken: the assign-on-take rule.
309
+ * `--assignees` is the JSON array of the item's live assignees as the
310
+ * manager read them back (`[]` when nobody is assigned): GitHub
311
+ * `{login, id}` objects, Jira accountIds, Linear user ids. Missing or not an
312
+ * array is exit 2, so no caller can skip the check by leaving it out.
313
+ * @param {object} ctx - CLI context
314
+ * @param {{operatorIds?: unknown}} config - Session-manager config
315
+ * @param {Awaited<ReturnType<typeof readLedger>>} ledger - Ledger
316
+ * @param {string} ref - Item ref
317
+ * @returns {void}
318
+ */
319
+ function refuseUntaken(ctx, config, ledger, ref) {
320
+ const raw = ctx.values.assignees;
321
+ const assignees =
322
+ raw === undefined ? undefined : parseJson(raw, "--assignees");
323
+ if (!Array.isArray(assignees)) {
324
+ throw new CliError(
325
+ `spawn needs --assignees: the JSON array of ${ref}'s live assignees ` +
326
+ "as read back from the tracker ([] when unassigned).",
327
+ EXIT.SETUP
328
+ );
329
+ }
330
+ const me = ledger.control.me ?? null;
331
+ const project = loadProject(ctx.root);
332
+ const verdict = judgeTake(assignees, {
333
+ me,
334
+ operatorIds: config.operatorIds,
335
+ tracker: project.local?.tracker ?? project.global?.tracker,
336
+ });
337
+ if (verdict.ok) return;
338
+ const message =
339
+ verdict.reason === "unassigned"
340
+ ? `${ref} is unassigned; nothing was spawned. Assign it to "me" ` +
341
+ `(${me ?? "not recorded: run ledger control --me <id>"}), read it ` +
342
+ "back, and spawn again."
343
+ : `${ref} is assigned to someone other than "me" or an operator ` +
344
+ `(${verdict.foreign.join(", ")}); nothing was spawned. Leave it.`;
345
+ throw new CliError(message, EXIT.NOT_TAKEN, {
346
+ ref,
347
+ reason: verdict.reason,
348
+ me,
349
+ foreign: verdict.foreign,
350
+ });
351
+ }
352
+
302
353
  /**
303
354
  * The ledger row for `ref`, or exit 2.
304
355
  * @param {Awaited<ReturnType<typeof readLedger>>} ledger - Ledger
@@ -22,11 +22,11 @@
22
22
  * @module session-manager/orphan-sweep
23
23
  */
24
24
  import {
25
- entryMatches,
26
25
  idComparableValues,
27
26
  parseIdEntries,
28
27
  } from "../../../scripts/operator-id-kinds.mjs";
29
28
  import { holdDigest, judgeHold } from "./orphan-holds.mjs";
29
+ import { assigneeFields, isMeOrOperator } from "./take-check.mjs";
30
30
  import { FINISH_RETRY_HOURS, classifyOrphan } from "./orphan-buckets.mjs";
31
31
 
32
32
  export {
@@ -62,22 +62,6 @@ function idOrNull(value) {
62
62
  return Number.isSafeInteger(value) ? String(value) : null;
63
63
  }
64
64
 
65
- /**
66
- * The fields one assignee is compared on, the way `queue` compares them: a
67
- * plain string is the tracker's normalized id (GitHub login, Jira accountId,
68
- * Linear user id); a `{login, id}` object adds the kind-keyed fields.
69
- * @param {unknown} assignee - One assignee as the tracker gave it
70
- * @returns {{plain: string|null, login: string|null, id: string|null}} Fields
71
- */
72
- function assigneeFields(assignee) {
73
- if (assignee !== null && typeof assignee === "object") {
74
- const login = idOrNull(assignee.login);
75
- const id = idOrNull(assignee.id ?? assignee.accountId);
76
- return { plain: login ?? id, login, id };
77
- }
78
- return { plain: idOrNull(assignee), login: null, id: null };
79
- }
80
-
81
65
  /**
82
66
  * Whether any assignee is neither "me" nor an operator.
83
67
  * @param {unknown} assignees - The item's assignees
@@ -87,9 +71,7 @@ function assigneeFields(assignee) {
87
71
  function heldBySomeoneElse(assignees, who) {
88
72
  return (Array.isArray(assignees) ? assignees : []).some(assignee => {
89
73
  const fields = assigneeFields(assignee);
90
- if (fields.plain === null) return false;
91
- if (who.me !== null && fields.plain === who.me) return false;
92
- return !who.operators.some(entry => entryMatches(entry, fields));
74
+ return fields.plain !== null && !isMeOrOperator(fields, who);
93
75
  });
94
76
  }
95
77
 
@@ -69,7 +69,11 @@ const MEASURE = { "max-workers": text, meminfo: text };
69
69
  /** Each subcommand: handler, accepted flags, and how many positionals. */
70
70
  const COMMANDS = {
71
71
  capacity: { run: capacity, options: MEASURE, positionals: 0 },
72
- spawn: { run: spawn, options: { ...MEASURE, repo: text }, positionals: 1 },
72
+ spawn: {
73
+ run: spawn,
74
+ options: { ...MEASURE, repo: text, assignees: text },
75
+ positionals: 1,
76
+ },
73
77
  resume: { run: resume, options: {}, positionals: 1 },
74
78
  classify: { run: classify, options: {}, positionals: 1 },
75
79
  status: { run: status, options: {}, positionals: 0 },
@@ -0,0 +1,110 @@
1
+ /**
2
+ * The assign-on-take rule (CodySwannGT/lisa#5029), as a check `spawn` runs
3
+ * before it launches anything.
4
+ *
5
+ * The rule (runbook "Fixed facts", "Taking an item"): every item the manager
6
+ * takes is assigned to "me" when it has no assignee, and nothing is ever
7
+ * dispatched unassigned. The claim step used to do the assigning implicitly,
8
+ * and the dispatch step accepted the claim's word for it, so an item the
9
+ * manager released, re-dispatched or drove itself could reach a worker with
10
+ * nobody named on it. `spawn` now needs the assignees the manager read back
11
+ * from the tracker (`--assignees`) and refuses, with exit 5, an item that is
12
+ * unassigned or held by anyone other than "me" or an operator.
13
+ *
14
+ * An assignee is compared exactly as the queue and the orphan sweep compare
15
+ * one: a plain string is the tracker's normalized id (GitHub login, Jira
16
+ * accountId, Linear user id), and a `{login, id}` object (GitHub, with the
17
+ * REST numeric id) also matches `login:` and `id:` operator entries.
18
+ * Pure: no I/O.
19
+ * @module session-manager/take-check
20
+ */
21
+ import {
22
+ entryMatches,
23
+ parseIdEntries,
24
+ } from "../../../scripts/operator-id-kinds.mjs";
25
+
26
+ /**
27
+ * A trimmed id string, or null. Numeric ids become strings.
28
+ * @param {unknown} value - Raw id
29
+ * @returns {string | null} Comparable id
30
+ */
31
+ function idOrNull(value) {
32
+ if (typeof value === "string" && value.trim() !== "") return value.trim();
33
+ return Number.isSafeInteger(value) ? String(value) : null;
34
+ }
35
+
36
+ /**
37
+ * The fields one assignee is compared on: a plain string is the tracker's
38
+ * normalized id (GitHub login, Jira accountId, Linear user id); a
39
+ * `{login, id}` object adds the kind-keyed fields.
40
+ * @param {unknown} assignee - One assignee as the tracker gave it
41
+ * @returns {{plain: string|null, login: string|null, id: string|null}} Fields
42
+ */
43
+ export function assigneeFields(assignee) {
44
+ if (assignee !== null && typeof assignee === "object") {
45
+ const login = idOrNull(assignee.login);
46
+ const id = idOrNull(assignee.id ?? assignee.accountId);
47
+ return { plain: login ?? id, login, id };
48
+ }
49
+ return { plain: idOrNull(assignee), login: null, id: null };
50
+ }
51
+
52
+ /**
53
+ * The fields `spawn` compares one read-back assignee on. A plain string means
54
+ * what the tracker's normalized id is: on GitHub a login (so `login:` entries
55
+ * match it and `id:` entries never do, since anyone can pick an all-digit
56
+ * login), and on Jira and Linear an account id (so `id:` entries match it).
57
+ * With no known tracker it keeps `assigneeFields`' plain-only reading.
58
+ * @param {unknown} assignee - One assignee as read back
59
+ * @param {string | undefined} tracker - `github`, `jira` or `linear`
60
+ * @returns {{plain: string|null, login: string|null, id: string|null}} Fields
61
+ */
62
+ function takenFields(assignee, tracker) {
63
+ const fields = assigneeFields(assignee);
64
+ if (typeof assignee !== "string" || fields.plain === null) return fields;
65
+ if (tracker === "github") return { ...fields, login: fields.plain };
66
+ if (tracker === "jira" || tracker === "linear") {
67
+ return { ...fields, id: fields.plain };
68
+ }
69
+ return fields;
70
+ }
71
+
72
+ /**
73
+ * Whether one assignee is "me" or an operator.
74
+ * @param {{plain: string|null, login: string|null, id: string|null}} fields - Assignee fields
75
+ * @param {{me: string|null, operators: object[]}} who - Identities
76
+ * @returns {boolean} True for "me" or an operator
77
+ */
78
+ export function isMeOrOperator(fields, who) {
79
+ if (fields.plain === null) return false;
80
+ if (who.me !== null && fields.plain === who.me) return true;
81
+ return who.operators.some(entry => entryMatches(entry, fields));
82
+ }
83
+
84
+ /**
85
+ * Judge whether an item may be dispatched under the assign-on-take rule.
86
+ * @param {unknown} assignees - The item's live assignees, as read back
87
+ * @param {{me?: string|null, operatorIds?: unknown, tracker?: string}} identities - "Me"
88
+ * (`ledger control.me`), `sessionManager.operatorIds`, and the project's
89
+ * tracker, which says what a plain assignee string is
90
+ * @returns {{ok: true} | {ok: false, reason: "unassigned"|"foreign", foreign: string[]}}
91
+ * `unassigned`: nobody is named, so assign "me" and read it back first;
92
+ * `foreign`: someone other than "me" or an operator holds it, so leave it
93
+ */
94
+ export function judgeTake(assignees, identities) {
95
+ const who = {
96
+ me: idOrNull(identities.me),
97
+ operators: parseIdEntries(identities.operatorIds),
98
+ };
99
+ const named = (Array.isArray(assignees) ? assignees : [])
100
+ .map(assignee => takenFields(assignee, identities.tracker))
101
+ .filter(fields => fields.plain !== null);
102
+ if (named.length === 0)
103
+ return { ok: false, reason: "unassigned", foreign: [] };
104
+ const foreign = named
105
+ .filter(fields => !isMeOrOperator(fields, who))
106
+ .map(fields => fields.plain);
107
+ return foreign.length === 0
108
+ ? { ok: true }
109
+ : { ok: false, reason: "foreign", foreign };
110
+ }
@@ -125,7 +125,15 @@ Otherwise:
125
125
 
126
126
  3. Read the binding back through `node scripts/lisa-work-item.mjs current` and require it to equal the canonical reference. If binding fails, stop before durable project work.
127
127
  - A detached-HEAD worktree is valid at this stage: the binding records `branch: null` as a pending state. Create the feature branch only after the gate succeeds, then run `node scripts/lisa-work-item.mjs attach-branch`. Commit preparation and validation fail closed until that attachment succeeds.
128
- 4. **Ignore guard before any write.** A host can receive these skills before its shipped `.gitignore` entry, so prove the path is ignored first, from the worktree root: `git check-ignore -q .lisa/work-item-context.md || printf '%s\n' '.lisa/work-item-context.md' >> "$(git rev-parse --git-common-dir)/info/exclude"`, then re-run `git check-ignore -q .lisa/work-item-context.md`. If it is still not ignored, stop and report — do not write the bundle. If an `.easignore` exists and `grep -qxF '.lisa/work-item-context.md' .easignore` fails, stop and report instead of writing: EAS uploads read `.easignore`, which is derived from `.gitignore`, not from `info/exclude`. Then write the full resolved work-item context — the live vendor read bundle, unedited, every comment included — to the worktree-local, git-ignored `<worktree root>/.lisa/work-item-context.md`, then return this structured result (the file is the context; a summary in the reply never substitutes for it). When a `caller_bundle_path` was forwarded for this work item and the file already exists, do not overwrite it — it holds the caller's bundle (or a previous run's) and is left for the resolver step. Otherwise this is the first write; when `lisa-implement` forwarded a `caller_bundle_path`, its resolver step then keeps the bundle at that path as the base and appends only what this live read adds, and in every case finishes it with the `## Comment inventory` section:
128
+ 4. **Token estimate before any work** (`work-item-definition-of-ready`, gate S21; contract in the `lisa-usage-accounting` skill's `references/estimate.md`). Write the item's live body to a scratch file and run `node scripts/lisa-estimate.mjs check --phase claim --body-file <file>`. Exit 0 (`PASS`, or `N/A` because `workItem.estimate.required` is `false`) → continue. Exit 1 because the estimate is missing → estimate it now, yourself; never ask a person and never stop for one:
129
+ 1. Pick a first token range and a recommended model from `workItem.estimate.models` (`node scripts/lisa-estimate.mjs config` prints them) by reading the item: scope, files it touches, type, how much verification it needs.
130
+ 2. Calibrate it. Fetch the bodies of up to 20 comparable finished items in this repository — same type, closed or done, carrying a `## Lisa Estimate` section — as a JSON array (GitHub: `gh issue list --repo <org>/<repo> --state closed --label <type label> --search '"Lisa Estimate" in:body' --limit 20 --json body`. Jira and Linear projects can serve several repositories, so scope those searches to this one by its `repo:<name>` label (the repo marker in the `config-resolution` rule; `<name>` is this repository's short name). Jira: through `lisa-atlassian-access`, `operation: search-issues jql:"project = <KEY> AND labels = \"repo:<name>\" AND issuetype = <type> AND statusCategory = Done AND text ~ \"Lisa Estimate\" ORDER BY resolved DESC"`, the first 20. Linear: through `lisa-linear-access`, `operation: list-issues team:<ID> label:repo:<name> state_type:["completed"]`. A listing may not carry full descriptions and has no body search, so read each candidate with `lisa-linear-access` `operation: get-issue id:<ID>` and keep the first 20 that carry the item's type label and a `## Lisa Estimate` section in that full description. Save each item's description as markdown text in `[{"body": "..."}]`). Run `node scripts/lisa-estimate.mjs calibrate --tokens-low <n> --tokens-high <n> --history-file <file>` and use the range and `basis` it prints. With no history it says so; keep your range.
131
+ 3. Render and write it: `node scripts/lisa-estimate.mjs render --tokens-low <n> --tokens-high <n> --model <id> --basis "<basis>" --body-file <file>` prints the whole body with the section in place. Write that body back through `lisa-tracker-write` (update), read the item back, and rerun the claim-phase check on the stored body; it must exit 0.
132
+
133
+ Exit 1 because a present estimate is malformed → re-render it the same way.
134
+
135
+ An estimate failure never blocks the work. The item is already claimed, and stopping here would strand it in the claimed state with no trace of why. When a check exits 2, calibration or rendering fails, or the written body does not read back with a passing claim-phase check, continue to step 5 with `estimate_outcome: failed` and `estimate_failure: <reason>`, and post one comment on the item through `lisa-tracker-write` that a non-technical reader can follow, for example: "Lisa could not record a cost estimate for this item before starting work, because the project's estimate settings could not be read. The work is going ahead without one. What it actually cost will still be recorded when it is done." Name the real reason in plain words, not an exit code. Record `estimate_outcome: present|written|failed|n/a` in the result.
136
+ 5. **Ignore guard before any write.** A host can receive these skills before its shipped `.gitignore` entry, so prove the path is ignored first, from the worktree root: `git check-ignore -q .lisa/work-item-context.md || printf '%s\n' '.lisa/work-item-context.md' >> "$(git rev-parse --git-common-dir)/info/exclude"`, then re-run `git check-ignore -q .lisa/work-item-context.md`. If it is still not ignored, stop and report — do not write the bundle. If an `.easignore` exists and `grep -qxF '.lisa/work-item-context.md' .easignore` fails, stop and report instead of writing: EAS uploads read `.easignore`, which is derived from `.gitignore`, not from `info/exclude`. Then write the full resolved work-item context — the live vendor read bundle, unedited, every comment included — to the worktree-local, git-ignored `<worktree root>/.lisa/work-item-context.md`, then return this structured result (the file is the context; a summary in the reply never substitutes for it). When a `caller_bundle_path` was forwarded for this work item and the file already exists, do not overwrite it — it holds the caller's bundle (or a previous run's) and is left for the resolver step. Otherwise this is the first write; when `lisa-implement` forwarded a `caller_bundle_path`, its resolver step then keeps the bundle at that path as the base and appends only what this live read adds, and in every case finishes it with the `## Comment inventory` section:
129
137
 
130
138
  ```text
131
139
  tracker_provider: jira|github|linear
@@ -136,6 +144,8 @@ Otherwise:
136
144
  gate_reason: <the act a person must perform>|null
137
145
  claim_outcome: claimed|reused|held-by-gate|blocked-ci-loop
138
146
  binding_outcome: verified|skipped-human-gate|skipped-ci-loop
147
+ estimate_outcome: present|written|failed|n/a
148
+ estimate_failure: <plain-English reason>|null
139
149
  route: implement|ci-rework pr=<n>|blocked pr=<n>
140
150
  failing_checks: <comma-separated check names from the marker comment>|none
141
151
  head_ref: <the pull request's head branch>|null
@@ -33,3 +33,4 @@ Thin dispatcher. `$ARGUMENTS` must contain exactly one canonical work-item refer
33
33
  - A fresh claim uses the same idempotency lock as the three build-intake Phase 3b flows: move configured ready to configured claimed, or place an unlaned backlog leaf directly in claimed; assign to the authenticated user only when unassigned; never replace an existing owner.
34
34
  - Fail closed if the mutation or post-write read cannot prove the claimed-or-later state. A tracker outage is a blocker, not permission to work untracked.
35
35
  - This skill claims existing work only. It never creates a ticket and never writes the worktree binding.
36
+ - A claim does not satisfy the token-estimate gate (S21). `lisa-track` step 4 runs the claim-phase check right after the claim and, when the item has no estimate, writes one before any work begins. Every dispatch path (direct `lisa-implement`, the build-intake flows, the session manager) reaches work through `lisa-track`, so that step is the single owner.
@@ -64,8 +64,31 @@ stored bytes.
64
64
  file items they cannot estimate. Validator gate S21 (`lisa-estimate.mjs check`) refuses only an
65
65
  estimate that is present and wrong: no parseable token, an inverted range, a model outside
66
66
  `models`, or a dollar range that disagrees with the price table the token names.
67
- - **Before work begins:** required. The claiming agent estimates a claimed item that lacks one and
68
- writes it before doing any work. It never asks a person.
67
+ - **Before work begins:** required. `lisa-track` step 4, which every dispatch path runs right
68
+ after the claim, checks with `lisa-estimate.mjs check --phase claim`. A claimed item without an
69
+ estimate fails that check, and the claiming agent estimates it and writes it before doing any
70
+ work. It never asks a person.
71
+ If estimating itself fails, the work still goes ahead: the agent records
72
+ `estimate_outcome: failed` and posts a plain-English comment on the item saying why.
69
73
  - **When the config is off:** nothing about estimates blocks; the check reports `N/A`.
70
74
 
75
+ ## Calibration
76
+
77
+ Estimates improve by learning from finished work. Before rendering, the claiming agent fetches up
78
+ to 20 comparable finished items (same repository, same type, carrying a `## Lisa Estimate`
79
+ section) as a JSON array of bodies and runs:
80
+
81
+ ```bash
82
+ node scripts/lisa-estimate.mjs calibrate --tokens-low 1M --tokens-high 2M --history-file <items.json>
83
+ ```
84
+
85
+ For each item with both an estimate and a complete actual (its `## Lisa Usage` rollup
86
+ `total_tokens`, counted only when every run in the ledger has a full token count), the ratio is
87
+ the actual over the estimate's midpoint. The median ratio scales the new range, and the printed
88
+ `basis` (for example "calibrated x2 (median actual/estimate) from 3 comparable finished item(s)")
89
+ goes into the section. Items whose actual is unknown or partial — no ledger, any run with
90
+ `unavailable` telemetry, or a `measured-subset` entry — are skipped, never counted as zero or as
91
+ the sum of only the known runs. With
92
+ no usable history the range is unchanged and the basis says "no calibration history".
93
+
71
94
  The recommended model is recorded only. It does not choose which model or agent runs the work.
@@ -169,7 +169,7 @@
169
169
  "workflows": 27,
170
170
  "steps": 890,
171
171
  "couplings": 50,
172
- "inventory": 251
172
+ "inventory": 252
173
173
  },
174
174
  "counts": {
175
175
  "package-backed": 11,
@@ -270,6 +270,7 @@ scripts/lib/usage-effectiveness.mjs
270
270
  scripts/lib/usage-ledger-integrity.mjs
271
271
  scripts/lib/usage-ledger.mjs
272
272
  scripts/lib/windows-process-job.mjs
273
+ scripts/lib/work-item-estimate-calibration.mjs
273
274
  scripts/lib/work-item-estimate-check.mjs
274
275
  scripts/lib/work-item-estimate-config.mjs
275
276
  scripts/lib/work-item-estimate.mjs