@bonesofspring/ai-rules 0.2.90 → 0.2.92

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 (111) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/docs/OPENCODE-COMPATIBILITY.md +2 -1
  3. package/docs/OPENCODE.md +20 -2
  4. package/docs/TEAM-DB.md +2 -0
  5. package/package.json +1 -1
  6. package/presets/_shared/ai-local/scripts/team-db-api.mjs +33 -15
  7. package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +15 -0
  8. package/presets/_shared/ai-local/scripts/team-db-project.mjs +41 -2
  9. package/presets/_shared/ai-local/scripts/team-db-schema.mjs +23 -0
  10. package/presets/_shared/core/agent-team/agent-task-metrics.md +16 -1
  11. package/presets/_shared/hooks/chain-team-phases.sh.tmpl +87 -6
  12. package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +87 -6
  13. package/presets/claude/go/hooks/chain-team-phases.sh +87 -6
  14. package/presets/claude/ios-swift/hooks/chain-team-phases.sh +87 -6
  15. package/presets/claude/java/hooks/chain-team-phases.sh +87 -6
  16. package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +87 -6
  17. package/presets/claude/mol/hooks/chain-team-phases.sh +87 -6
  18. package/presets/claude/next/hooks/chain-team-phases.sh +87 -6
  19. package/presets/claude/nuxt/hooks/chain-team-phases.sh +87 -6
  20. package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +87 -6
  21. package/presets/claude/php-laravel/hooks/chain-team-phases.sh +87 -6
  22. package/presets/claude/svelte/hooks/chain-team-phases.sh +87 -6
  23. package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +87 -6
  24. package/presets/cursor/go/hooks/chain-team-phases.sh +87 -6
  25. package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +87 -6
  26. package/presets/cursor/java/hooks/chain-team-phases.sh +87 -6
  27. package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +87 -6
  28. package/presets/cursor/mol/hooks/chain-team-phases.sh +87 -6
  29. package/presets/cursor/next/hooks/chain-team-phases.sh +87 -6
  30. package/presets/cursor/nuxt/hooks/chain-team-phases.sh +87 -6
  31. package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +87 -6
  32. package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +87 -6
  33. package/presets/cursor/svelte/hooks/chain-team-phases.sh +87 -6
  34. package/presets/mavis/android-kotlin/adapter-map.json +1 -1
  35. package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +87 -6
  36. package/presets/mavis/go/adapter-map.json +1 -1
  37. package/presets/mavis/go/hooks/chain-team-phases.sh +87 -6
  38. package/presets/mavis/ios-swift/adapter-map.json +1 -1
  39. package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +87 -6
  40. package/presets/mavis/java/adapter-map.json +1 -1
  41. package/presets/mavis/java/hooks/chain-team-phases.sh +87 -6
  42. package/presets/mavis/mcp-ts/adapter-map.json +1 -1
  43. package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +87 -6
  44. package/presets/mavis/mol/adapter-map.json +1 -1
  45. package/presets/mavis/mol/hooks/chain-team-phases.sh +87 -6
  46. package/presets/mavis/next/adapter-map.json +1 -1
  47. package/presets/mavis/next/hooks/chain-team-phases.sh +87 -6
  48. package/presets/mavis/nuxt/adapter-map.json +1 -1
  49. package/presets/mavis/nuxt/hooks/chain-team-phases.sh +87 -6
  50. package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
  51. package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +87 -6
  52. package/presets/mavis/php-laravel/adapter-map.json +1 -1
  53. package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +87 -6
  54. package/presets/mavis/svelte/adapter-map.json +1 -1
  55. package/presets/mavis/svelte/hooks/chain-team-phases.sh +87 -6
  56. package/presets/opencode/android-kotlin/adapter-map.json +1 -1
  57. package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +87 -6
  58. package/presets/opencode/go/adapter-map.json +1 -1
  59. package/presets/opencode/go/hooks/chain-team-phases.sh +87 -6
  60. package/presets/opencode/ios-swift/adapter-map.json +1 -1
  61. package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +87 -6
  62. package/presets/opencode/java/adapter-map.json +1 -1
  63. package/presets/opencode/java/hooks/chain-team-phases.sh +87 -6
  64. package/presets/opencode/mcp-ts/adapter-map.json +1 -1
  65. package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +87 -6
  66. package/presets/opencode/mol/adapter-map.json +1 -1
  67. package/presets/opencode/mol/hooks/chain-team-phases.sh +87 -6
  68. package/presets/opencode/next/adapter-map.json +1 -1
  69. package/presets/opencode/next/hooks/chain-team-phases.sh +87 -6
  70. package/presets/opencode/nuxt/adapter-map.json +1 -1
  71. package/presets/opencode/nuxt/hooks/chain-team-phases.sh +87 -6
  72. package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
  73. package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +87 -6
  74. package/presets/opencode/php-laravel/adapter-map.json +1 -1
  75. package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +87 -6
  76. package/presets/opencode/plugins/output-guard/README.md +4 -3
  77. package/presets/opencode/plugins/output-guard/index.ts +4 -3
  78. package/presets/opencode/plugins/output-guard/rewrite.ts +1 -2
  79. package/presets/opencode/plugins/rule-attach/README.md +1 -1
  80. package/presets/opencode/plugins/rule-attach/index.ts +23 -4
  81. package/presets/opencode/plugins/settings-guard/README.md +3 -0
  82. package/presets/opencode/plugins/settings-guard/index.ts +1 -1
  83. package/presets/opencode/plugins/settings-guard/policy.mjs +1 -1
  84. package/presets/opencode/plugins/task-status/README.md +4 -4
  85. package/presets/opencode/plugins/task-status/deepseek.ts +27 -8
  86. package/presets/opencode/plugins/task-status/task-view.ts +8 -4
  87. package/presets/opencode/plugins/task-status/tui.tsx +12 -3
  88. package/presets/opencode/svelte/adapter-map.json +1 -1
  89. package/presets/opencode/svelte/hooks/chain-team-phases.sh +87 -6
  90. package/presets/zcode/android-kotlin/adapter-map.json +1 -1
  91. package/presets/zcode/android-kotlin/hooks/chain-team-phases.sh +87 -6
  92. package/presets/zcode/go/adapter-map.json +1 -1
  93. package/presets/zcode/go/hooks/chain-team-phases.sh +87 -6
  94. package/presets/zcode/ios-swift/adapter-map.json +1 -1
  95. package/presets/zcode/ios-swift/hooks/chain-team-phases.sh +87 -6
  96. package/presets/zcode/java/adapter-map.json +1 -1
  97. package/presets/zcode/java/hooks/chain-team-phases.sh +87 -6
  98. package/presets/zcode/mcp-ts/adapter-map.json +1 -1
  99. package/presets/zcode/mcp-ts/hooks/chain-team-phases.sh +87 -6
  100. package/presets/zcode/mol/adapter-map.json +1 -1
  101. package/presets/zcode/mol/hooks/chain-team-phases.sh +87 -6
  102. package/presets/zcode/next/adapter-map.json +1 -1
  103. package/presets/zcode/next/hooks/chain-team-phases.sh +87 -6
  104. package/presets/zcode/nuxt/adapter-map.json +1 -1
  105. package/presets/zcode/nuxt/hooks/chain-team-phases.sh +87 -6
  106. package/presets/zcode/php-hexagonal/adapter-map.json +1 -1
  107. package/presets/zcode/php-hexagonal/hooks/chain-team-phases.sh +87 -6
  108. package/presets/zcode/php-laravel/adapter-map.json +1 -1
  109. package/presets/zcode/php-laravel/hooks/chain-team-phases.sh +87 -6
  110. package/presets/zcode/svelte/adapter-map.json +1 -1
  111. package/presets/zcode/svelte/hooks/chain-team-phases.sh +87 -6
@@ -105,7 +105,20 @@ function writeSessionLease(id, value) {
105
105
  const temporary = `${file}.${process.pid}.tmp`;
106
106
  const now = new Date().toISOString();
107
107
  const firstSeenAt = firstSeenAtOf(file, value, now);
108
- fs.writeFileSync(temporary, `${JSON.stringify({ slug: value, sessionId: id, firstSeenAt, updatedAt: now })}\n`);
108
+ let model = null;
109
+ try {
110
+ if (Date.now() - fs.statSync(file).mtimeMs <= LEASE_TTL_MS) {
111
+ const previous = JSON.parse(fs.readFileSync(file, 'utf8'));
112
+ const previousTool = previous && typeof previous.tool === 'string' ? previous.tool : '';
113
+ const keepModel = previous && previous.slug === value && (previousTool === '' || previousTool === PLATFORM);
114
+ if (keepModel && typeof previous.model === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:@/#-]{0,119}$/.test(previous.model)) {
115
+ model = previous.model;
116
+ }
117
+ }
118
+ } catch {}
119
+ const body = { slug: value, sessionId: id, firstSeenAt, updatedAt: now, tool: PLATFORM };
120
+ if (model) body.model = model;
121
+ fs.writeFileSync(temporary, `${JSON.stringify(body)}\n`);
109
122
  fs.renameSync(temporary, file);
110
123
  for (const name of fs.readdirSync(LEASE_ROOT)) {
111
124
  if (!name.endsWith('.json')) continue;
@@ -196,8 +209,6 @@ if (slugSource === 'active-task.json') {
196
209
  if (slugSource !== 'TEAM_TASK_SLUG') {
197
210
  process.stderr.write(`WARNING: ${slug}: chain hook resolved the slug from ${slugSource}; pass TEAM_TASK_SLUG=<slug> on every hook call so parallel sessions cannot no-op each other.\n`);
198
211
  }
199
- if (slugSource === 'TEAM_TASK_SLUG' && sessionId) writeSessionLease(sessionId, slug);
200
-
201
212
  const taskDir = path.join(TEAM_ROOT, 'tasks', slug);
202
213
  const statusPath = path.join(taskDir, 'status.json');
203
214
  const pipelinePath = path.join(taskDir, 'pipeline.json');
@@ -216,6 +227,7 @@ let status;
216
227
  const now = new Date().toISOString();
217
228
  const STACK = 'java';
218
229
  const PLATFORM = 'claude';
230
+ if (slugSource === 'TEAM_TASK_SLUG' && sessionId) writeSessionLease(sessionId, slug);
219
231
 
220
232
  let metricsLockToken = null;
221
233
  function readMetricsLockToken() {
@@ -512,6 +524,7 @@ function reclaimStaleAttempts(scope) {
512
524
  type: 'attempt_finished',
513
525
  at: now,
514
526
  attemptId,
527
+ ...closingExecution(entry, Array.isArray(steps) ? steps[entry.stepIndex] : null),
515
528
  stepIndex: entry.stepIndex,
516
529
  stepLabel: (Array.isArray(steps) && steps[entry.stepIndex] && steps[entry.stepIndex].label) || null,
517
530
  agent: entry.agent,
@@ -578,6 +591,59 @@ function saveWriteScopeBaseline(step) {
578
591
  .split('\n').map((line) => line.trim()).filter(Boolean)[0] || `exit ${result ? result.status : 'unknown'}`;
579
592
  process.stderr.write(`[chain-team-phases] ${slug}: write-scope baseline for step ${stepId} not saved (${reason}); the audit will skip that step.\n`);
580
593
  }
594
+ const MODEL_TIERS = new Set(['cheap', 'standard', 'strong']);
595
+ const MODEL_SOURCES = new Set(['host', 'lease', 'tier-map', 'session']);
596
+ function boundedModel(value) {
597
+ if (typeof value !== 'string') return null;
598
+ const text = value.trim();
599
+ return /^[A-Za-z0-9][A-Za-z0-9._:@/#-]{0,119}$/.test(text) ? text : null;
600
+ }
601
+ function readTierMapModel(tier) {
602
+ try {
603
+ const parsed = JSON.parse(fs.readFileSync(path.join(process.cwd(), '.ai.local', 'team', 'model-tiers.json'), 'utf8'));
604
+ const tiers = parsed && parsed.tiers;
605
+ if (!tiers || typeof tiers !== 'object') return null;
606
+ const approvedBy = typeof parsed.approvedBy === 'string' ? parsed.approvedBy.trim() : '';
607
+ const filled = ['cheap', 'standard', 'strong'].some((key) => typeof tiers[key] === 'string' && tiers[key].trim() !== '');
608
+ if (approvedBy === '' && !filled) return null;
609
+ return boundedModel(tiers[tier]);
610
+ } catch {
611
+ return null;
612
+ }
613
+ }
614
+ function observedModel() {
615
+ const execution = hookInput && hookInput.execution;
616
+ if (execution && typeof execution === 'object') {
617
+ const model = boundedModel(execution.model);
618
+ if (model) return { model, modelSource: MODEL_SOURCES.has(execution.modelSource) ? execution.modelSource : 'host' };
619
+ }
620
+ const direct = boundedModel(hookInput && hookInput.model);
621
+ return direct ? { model: direct, modelSource: 'host' } : null;
622
+ }
623
+ function leaseModel() {
624
+ if (!sessionId) return null;
625
+ const lease = readSessionLease(sessionId);
626
+ if (!lease || lease.tool !== PLATFORM) return null;
627
+ const model = boundedModel(lease.model);
628
+ return model ? { model, modelSource: 'lease' } : null;
629
+ }
630
+ function executionForStep(step) {
631
+ const modelTier = step && MODEL_TIERS.has(step.model) ? step.model : null;
632
+ const observed = observedModel() || leaseModel();
633
+ if (observed) return { tool: PLATFORM, modelTier, model: observed.model, modelSource: observed.modelSource };
634
+ const mapped = modelTier ? readTierMapModel(modelTier) : null;
635
+ if (mapped) return { tool: PLATFORM, modelTier, model: mapped, modelSource: 'tier-map' };
636
+ return { tool: PLATFORM, modelTier, model: null, modelSource: null };
637
+ }
638
+ function closingExecution(entry, step) {
639
+ const model = boundedModel(entry && entry.model);
640
+ const armedTier = entry && MODEL_TIERS.has(entry.modelTier) ? entry.modelTier : null;
641
+ const armedSource = model && entry && MODEL_SOURCES.has(entry.modelSource) ? entry.modelSource : null;
642
+ const observed = observedModel();
643
+ const modelTier = (step && MODEL_TIERS.has(step.model) ? step.model : null) || armedTier;
644
+ if (observed) return { tool: PLATFORM, modelTier, model: observed.model, modelSource: observed.modelSource };
645
+ return { tool: PLATFORM, modelTier, model, modelSource: model ? armedSource : null };
646
+ }
581
647
  function activateAttempts(step, stepIndex, waveId = null, agentsOverride = null) {
582
648
  const agents = Array.isArray(agentsOverride) && agentsOverride.length > 0 ? agentsOverride : getStepAgents(step);
583
649
  const parallelGroupId = step.parallel && getStepAgents(step).length > 1 ? `${status.metricsRunId || slug}:step-${stepIndex}` : null;
@@ -587,8 +653,9 @@ function activateAttempts(step, stepIndex, waveId = null, agentsOverride = null)
587
653
  const attempt = nextAttemptOrdinal(stepIndex, agent); const runId = status.metricsRunId || `${slug}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
588
654
  status.metricsRunId = runId; const attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
589
655
  if (active[attemptId]) continue;
590
- active[attemptId] = { agent, stepIndex, attempt, startedAt: now, parallelGroupId, waveId: waveId || null };
591
- appendMetric({ type: 'attempt_started', at: now, attemptId, stepIndex, stepLabel: step.label || null, agent, role: roleFor(agent), attempt, parallelGroupId, waveId: waveId || null, source: 'hook', attributionConfidence: 'high' });
656
+ const execution = executionForStep(step);
657
+ active[attemptId] = { agent, stepIndex, attempt, startedAt: now, parallelGroupId, waveId: waveId || null, ...execution };
658
+ appendMetric({ type: 'attempt_started', at: now, attemptId, stepIndex, stepLabel: step.label || null, agent, role: roleFor(agent), attempt, parallelGroupId, waveId: waveId || null, source: 'hook', attributionConfidence: 'high', ...execution });
592
659
  }
593
660
  status.activeAttempts = active;
594
661
  saveWriteScopeBaseline(step);
@@ -1592,10 +1659,12 @@ function finishActiveAttempt(forced) {
1592
1659
  const idleMs = wallMs === null ? null : Math.max(0, wallMs - (activeMs ?? 0));
1593
1660
  const outcome = ['completed', 'blocked', 'changes_requested', 'validation_failed'].includes(status.state) ? status.state : 'unknown';
1594
1661
  const usage = sanitizeUsage(hookInput.usage);
1662
+ const execution = closingExecution(resolved.entry, Array.isArray(steps) ? steps[resolved.entry.stepIndex] : null);
1595
1663
  appendMetric({
1596
1664
  type: 'attempt_finished',
1597
1665
  at: now,
1598
1666
  attemptId: resolved.attemptId,
1667
+ ...execution,
1599
1668
  stepIndex: typeof resolved.entry.stepIndex === 'number' ? resolved.entry.stepIndex : null,
1600
1669
  agent: resolved.entry.agent || null,
1601
1670
  outcome,
@@ -2941,9 +3010,10 @@ function armResumedAttempt(step, stepIndex, agent) {
2941
3010
  0,
2942
3011
  ) + 1;
2943
3012
  attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
3013
+ const execution = executionForStep(step);
2944
3014
  status.activeAttempts = {
2945
3015
  ...(status.activeAttempts || {}),
2946
- [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null },
3016
+ [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null, ...execution },
2947
3017
  };
2948
3018
  events.push({
2949
3019
  eventId: `${runId}:${Date.now()}:${Math.random().toString(16).slice(2)}`,
@@ -2959,6 +3029,7 @@ function armResumedAttempt(step, stepIndex, agent) {
2959
3029
  waveId: null,
2960
3030
  source: 'hook',
2961
3031
  attributionConfidence: 'high',
3032
+ ...execution,
2962
3033
  });
2963
3034
  });
2964
3035
  return attemptId;
@@ -3134,12 +3205,17 @@ if (hookInput.action === 'out_of_band_start') {
3134
3205
  const agent = typeof hookInput.agent === 'string' ? hookInput.agent : 'unknown';
3135
3206
  const reason = typeof hookInput.reason === 'string' ? hookInput.reason : null;
3136
3207
  const outOfBandId = `${status.metricsRunId || slug}:oob:${Date.now()}:${Math.random().toString(16).slice(2, 8)}`;
3208
+ const observed = observedModel();
3137
3209
  appendMetric({
3138
3210
  type: 'out_of_band_started',
3139
3211
  outOfBandId,
3140
3212
  agent,
3141
3213
  reason,
3142
3214
  at: now,
3215
+ tool: PLATFORM,
3216
+ modelTier: null,
3217
+ model: observed ? observed.model : null,
3218
+ modelSource: observed ? observed.modelSource : null,
3143
3219
  source: 'orchestrator',
3144
3220
  attributionConfidence: 'high',
3145
3221
  });
@@ -3174,6 +3250,7 @@ if (hookInput.action === 'out_of_band_end') {
3174
3250
  }
3175
3251
  const durationMs = Math.max(0, Date.parse(now) - Date.parse(started.at));
3176
3252
  const outcome = typeof hookInput.outcome === 'string' ? hookInput.outcome : 'completed';
3253
+ const finishedModel = boundedModel(started.model);
3177
3254
  appendMetric({
3178
3255
  type: 'out_of_band_finished',
3179
3256
  outOfBandId: started.outOfBandId,
@@ -3182,6 +3259,10 @@ if (hookInput.action === 'out_of_band_end') {
3182
3259
  outcome,
3183
3260
  durationMs,
3184
3261
  at: now,
3262
+ tool: PLATFORM,
3263
+ modelTier: MODEL_TIERS.has(started.modelTier) ? started.modelTier : null,
3264
+ model: finishedModel,
3265
+ modelSource: finishedModel && MODEL_SOURCES.has(started.modelSource) ? started.modelSource : null,
3185
3266
  source: 'orchestrator',
3186
3267
  attributionConfidence: 'high',
3187
3268
  });
@@ -107,7 +107,20 @@ function writeSessionLease(id, value) {
107
107
  const temporary = `${file}.${process.pid}.tmp`;
108
108
  const now = new Date().toISOString();
109
109
  const firstSeenAt = firstSeenAtOf(file, value, now);
110
- fs.writeFileSync(temporary, `${JSON.stringify({ slug: value, sessionId: id, firstSeenAt, updatedAt: now })}\n`);
110
+ let model = null;
111
+ try {
112
+ if (Date.now() - fs.statSync(file).mtimeMs <= LEASE_TTL_MS) {
113
+ const previous = JSON.parse(fs.readFileSync(file, 'utf8'));
114
+ const previousTool = previous && typeof previous.tool === 'string' ? previous.tool : '';
115
+ const keepModel = previous && previous.slug === value && (previousTool === '' || previousTool === PLATFORM);
116
+ if (keepModel && typeof previous.model === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:@/#-]{0,119}$/.test(previous.model)) {
117
+ model = previous.model;
118
+ }
119
+ }
120
+ } catch {}
121
+ const body = { slug: value, sessionId: id, firstSeenAt, updatedAt: now, tool: PLATFORM };
122
+ if (model) body.model = model;
123
+ fs.writeFileSync(temporary, `${JSON.stringify(body)}\n`);
111
124
  fs.renameSync(temporary, file);
112
125
  for (const name of fs.readdirSync(LEASE_ROOT)) {
113
126
  if (!name.endsWith('.json')) continue;
@@ -198,8 +211,6 @@ if (slugSource === 'active-task.json') {
198
211
  if (slugSource !== 'TEAM_TASK_SLUG') {
199
212
  process.stderr.write(`WARNING: ${slug}: chain hook resolved the slug from ${slugSource}; pass TEAM_TASK_SLUG=<slug> on every hook call so parallel sessions cannot no-op each other.\n`);
200
213
  }
201
- if (slugSource === 'TEAM_TASK_SLUG' && sessionId) writeSessionLease(sessionId, slug);
202
-
203
214
  const taskDir = path.join(TEAM_ROOT, 'tasks', slug);
204
215
  const statusPath = path.join(taskDir, 'status.json');
205
216
  const pipelinePath = path.join(taskDir, 'pipeline.json');
@@ -218,6 +229,7 @@ let status;
218
229
  const now = new Date().toISOString();
219
230
  const STACK = 'mcp-ts';
220
231
  const PLATFORM = 'claude';
232
+ if (slugSource === 'TEAM_TASK_SLUG' && sessionId) writeSessionLease(sessionId, slug);
221
233
 
222
234
  let metricsLockToken = null;
223
235
  function readMetricsLockToken() {
@@ -500,6 +512,7 @@ function reclaimStaleAttempts(scope) {
500
512
  type: 'attempt_finished',
501
513
  at: now,
502
514
  attemptId,
515
+ ...closingExecution(entry, Array.isArray(steps) ? steps[entry.stepIndex] : null),
503
516
  stepIndex: entry.stepIndex,
504
517
  stepLabel: (Array.isArray(steps) && steps[entry.stepIndex] && steps[entry.stepIndex].label) || null,
505
518
  agent: entry.agent,
@@ -566,6 +579,59 @@ function saveWriteScopeBaseline(step) {
566
579
  .split('\n').map((line) => line.trim()).filter(Boolean)[0] || `exit ${result ? result.status : 'unknown'}`;
567
580
  process.stderr.write(`[chain-team-phases] ${slug}: write-scope baseline for step ${stepId} not saved (${reason}); the audit will skip that step.\n`);
568
581
  }
582
+ const MODEL_TIERS = new Set(['cheap', 'standard', 'strong']);
583
+ const MODEL_SOURCES = new Set(['host', 'lease', 'tier-map', 'session']);
584
+ function boundedModel(value) {
585
+ if (typeof value !== 'string') return null;
586
+ const text = value.trim();
587
+ return /^[A-Za-z0-9][A-Za-z0-9._:@/#-]{0,119}$/.test(text) ? text : null;
588
+ }
589
+ function readTierMapModel(tier) {
590
+ try {
591
+ const parsed = JSON.parse(fs.readFileSync(path.join(process.cwd(), '.ai.local', 'team', 'model-tiers.json'), 'utf8'));
592
+ const tiers = parsed && parsed.tiers;
593
+ if (!tiers || typeof tiers !== 'object') return null;
594
+ const approvedBy = typeof parsed.approvedBy === 'string' ? parsed.approvedBy.trim() : '';
595
+ const filled = ['cheap', 'standard', 'strong'].some((key) => typeof tiers[key] === 'string' && tiers[key].trim() !== '');
596
+ if (approvedBy === '' && !filled) return null;
597
+ return boundedModel(tiers[tier]);
598
+ } catch {
599
+ return null;
600
+ }
601
+ }
602
+ function observedModel() {
603
+ const execution = hookInput && hookInput.execution;
604
+ if (execution && typeof execution === 'object') {
605
+ const model = boundedModel(execution.model);
606
+ if (model) return { model, modelSource: MODEL_SOURCES.has(execution.modelSource) ? execution.modelSource : 'host' };
607
+ }
608
+ const direct = boundedModel(hookInput && hookInput.model);
609
+ return direct ? { model: direct, modelSource: 'host' } : null;
610
+ }
611
+ function leaseModel() {
612
+ if (!sessionId) return null;
613
+ const lease = readSessionLease(sessionId);
614
+ if (!lease || lease.tool !== PLATFORM) return null;
615
+ const model = boundedModel(lease.model);
616
+ return model ? { model, modelSource: 'lease' } : null;
617
+ }
618
+ function executionForStep(step) {
619
+ const modelTier = step && MODEL_TIERS.has(step.model) ? step.model : null;
620
+ const observed = observedModel() || leaseModel();
621
+ if (observed) return { tool: PLATFORM, modelTier, model: observed.model, modelSource: observed.modelSource };
622
+ const mapped = modelTier ? readTierMapModel(modelTier) : null;
623
+ if (mapped) return { tool: PLATFORM, modelTier, model: mapped, modelSource: 'tier-map' };
624
+ return { tool: PLATFORM, modelTier, model: null, modelSource: null };
625
+ }
626
+ function closingExecution(entry, step) {
627
+ const model = boundedModel(entry && entry.model);
628
+ const armedTier = entry && MODEL_TIERS.has(entry.modelTier) ? entry.modelTier : null;
629
+ const armedSource = model && entry && MODEL_SOURCES.has(entry.modelSource) ? entry.modelSource : null;
630
+ const observed = observedModel();
631
+ const modelTier = (step && MODEL_TIERS.has(step.model) ? step.model : null) || armedTier;
632
+ if (observed) return { tool: PLATFORM, modelTier, model: observed.model, modelSource: observed.modelSource };
633
+ return { tool: PLATFORM, modelTier, model, modelSource: model ? armedSource : null };
634
+ }
569
635
  function activateAttempts(step, stepIndex, waveId = null, agentsOverride = null) {
570
636
  const agents = Array.isArray(agentsOverride) && agentsOverride.length > 0 ? agentsOverride : getStepAgents(step);
571
637
  const parallelGroupId = step.parallel && getStepAgents(step).length > 1 ? `${status.metricsRunId || slug}:step-${stepIndex}` : null;
@@ -575,8 +641,9 @@ function activateAttempts(step, stepIndex, waveId = null, agentsOverride = null)
575
641
  const attempt = nextAttemptOrdinal(stepIndex, agent); const runId = status.metricsRunId || `${slug}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
576
642
  status.metricsRunId = runId; const attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
577
643
  if (active[attemptId]) continue;
578
- active[attemptId] = { agent, stepIndex, attempt, startedAt: now, parallelGroupId, waveId: waveId || null };
579
- appendMetric({ type: 'attempt_started', at: now, attemptId, stepIndex, stepLabel: step.label || null, agent, role: roleFor(agent), attempt, parallelGroupId, waveId: waveId || null, source: 'hook', attributionConfidence: 'high' });
644
+ const execution = executionForStep(step);
645
+ active[attemptId] = { agent, stepIndex, attempt, startedAt: now, parallelGroupId, waveId: waveId || null, ...execution };
646
+ appendMetric({ type: 'attempt_started', at: now, attemptId, stepIndex, stepLabel: step.label || null, agent, role: roleFor(agent), attempt, parallelGroupId, waveId: waveId || null, source: 'hook', attributionConfidence: 'high', ...execution });
580
647
  }
581
648
  status.activeAttempts = active;
582
649
  saveWriteScopeBaseline(step);
@@ -1580,10 +1647,12 @@ function finishActiveAttempt(forced) {
1580
1647
  const idleMs = wallMs === null ? null : Math.max(0, wallMs - (activeMs ?? 0));
1581
1648
  const outcome = ['completed', 'blocked', 'changes_requested', 'validation_failed'].includes(status.state) ? status.state : 'unknown';
1582
1649
  const usage = sanitizeUsage(hookInput.usage);
1650
+ const execution = closingExecution(resolved.entry, Array.isArray(steps) ? steps[resolved.entry.stepIndex] : null);
1583
1651
  appendMetric({
1584
1652
  type: 'attempt_finished',
1585
1653
  at: now,
1586
1654
  attemptId: resolved.attemptId,
1655
+ ...execution,
1587
1656
  stepIndex: typeof resolved.entry.stepIndex === 'number' ? resolved.entry.stepIndex : null,
1588
1657
  agent: resolved.entry.agent || null,
1589
1658
  outcome,
@@ -2888,9 +2957,10 @@ function armResumedAttempt(step, stepIndex, agent) {
2888
2957
  0,
2889
2958
  ) + 1;
2890
2959
  attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
2960
+ const execution = executionForStep(step);
2891
2961
  status.activeAttempts = {
2892
2962
  ...(status.activeAttempts || {}),
2893
- [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null },
2963
+ [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null, ...execution },
2894
2964
  };
2895
2965
  events.push({
2896
2966
  eventId: `${runId}:${Date.now()}:${Math.random().toString(16).slice(2)}`,
@@ -2906,6 +2976,7 @@ function armResumedAttempt(step, stepIndex, agent) {
2906
2976
  waveId: null,
2907
2977
  source: 'hook',
2908
2978
  attributionConfidence: 'high',
2979
+ ...execution,
2909
2980
  });
2910
2981
  });
2911
2982
  return attemptId;
@@ -3081,12 +3152,17 @@ if (hookInput.action === 'out_of_band_start') {
3081
3152
  const agent = typeof hookInput.agent === 'string' ? hookInput.agent : 'unknown';
3082
3153
  const reason = typeof hookInput.reason === 'string' ? hookInput.reason : null;
3083
3154
  const outOfBandId = `${status.metricsRunId || slug}:oob:${Date.now()}:${Math.random().toString(16).slice(2, 8)}`;
3155
+ const observed = observedModel();
3084
3156
  appendMetric({
3085
3157
  type: 'out_of_band_started',
3086
3158
  outOfBandId,
3087
3159
  agent,
3088
3160
  reason,
3089
3161
  at: now,
3162
+ tool: PLATFORM,
3163
+ modelTier: null,
3164
+ model: observed ? observed.model : null,
3165
+ modelSource: observed ? observed.modelSource : null,
3090
3166
  source: 'orchestrator',
3091
3167
  attributionConfidence: 'high',
3092
3168
  });
@@ -3121,6 +3197,7 @@ if (hookInput.action === 'out_of_band_end') {
3121
3197
  }
3122
3198
  const durationMs = Math.max(0, Date.parse(now) - Date.parse(started.at));
3123
3199
  const outcome = typeof hookInput.outcome === 'string' ? hookInput.outcome : 'completed';
3200
+ const finishedModel = boundedModel(started.model);
3124
3201
  appendMetric({
3125
3202
  type: 'out_of_band_finished',
3126
3203
  outOfBandId: started.outOfBandId,
@@ -3129,6 +3206,10 @@ if (hookInput.action === 'out_of_band_end') {
3129
3206
  outcome,
3130
3207
  durationMs,
3131
3208
  at: now,
3209
+ tool: PLATFORM,
3210
+ modelTier: MODEL_TIERS.has(started.modelTier) ? started.modelTier : null,
3211
+ model: finishedModel,
3212
+ modelSource: finishedModel && MODEL_SOURCES.has(started.modelSource) ? started.modelSource : null,
3132
3213
  source: 'orchestrator',
3133
3214
  attributionConfidence: 'high',
3134
3215
  });
@@ -104,7 +104,20 @@ function writeSessionLease(id, value) {
104
104
  const temporary = `${file}.${process.pid}.tmp`;
105
105
  const now = new Date().toISOString();
106
106
  const firstSeenAt = firstSeenAtOf(file, value, now);
107
- fs.writeFileSync(temporary, `${JSON.stringify({ slug: value, sessionId: id, firstSeenAt, updatedAt: now })}\n`);
107
+ let model = null;
108
+ try {
109
+ if (Date.now() - fs.statSync(file).mtimeMs <= LEASE_TTL_MS) {
110
+ const previous = JSON.parse(fs.readFileSync(file, 'utf8'));
111
+ const previousTool = previous && typeof previous.tool === 'string' ? previous.tool : '';
112
+ const keepModel = previous && previous.slug === value && (previousTool === '' || previousTool === PLATFORM);
113
+ if (keepModel && typeof previous.model === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:@/#-]{0,119}$/.test(previous.model)) {
114
+ model = previous.model;
115
+ }
116
+ }
117
+ } catch {}
118
+ const body = { slug: value, sessionId: id, firstSeenAt, updatedAt: now, tool: PLATFORM };
119
+ if (model) body.model = model;
120
+ fs.writeFileSync(temporary, `${JSON.stringify(body)}\n`);
108
121
  fs.renameSync(temporary, file);
109
122
  for (const name of fs.readdirSync(LEASE_ROOT)) {
110
123
  if (!name.endsWith('.json')) continue;
@@ -195,8 +208,6 @@ if (slugSource === 'active-task.json') {
195
208
  if (slugSource !== 'TEAM_TASK_SLUG') {
196
209
  process.stderr.write(`WARNING: ${slug}: chain hook resolved the slug from ${slugSource}; pass TEAM_TASK_SLUG=<slug> on every hook call so parallel sessions cannot no-op each other.\n`);
197
210
  }
198
- if (slugSource === 'TEAM_TASK_SLUG' && sessionId) writeSessionLease(sessionId, slug);
199
-
200
211
  const taskDir = path.join(TEAM_ROOT, 'tasks', slug);
201
212
  const statusPath = path.join(taskDir, 'status.json');
202
213
  const pipelinePath = path.join(taskDir, 'pipeline.json');
@@ -215,6 +226,7 @@ let status;
215
226
  const now = new Date().toISOString();
216
227
  const STACK = 'mol';
217
228
  const PLATFORM = 'claude';
229
+ if (slugSource === 'TEAM_TASK_SLUG' && sessionId) writeSessionLease(sessionId, slug);
218
230
 
219
231
  let metricsLockToken = null;
220
232
  function readMetricsLockToken() {
@@ -513,6 +525,7 @@ function reclaimStaleAttempts(scope) {
513
525
  type: 'attempt_finished',
514
526
  at: now,
515
527
  attemptId,
528
+ ...closingExecution(entry, Array.isArray(steps) ? steps[entry.stepIndex] : null),
516
529
  stepIndex: entry.stepIndex,
517
530
  stepLabel: (Array.isArray(steps) && steps[entry.stepIndex] && steps[entry.stepIndex].label) || null,
518
531
  agent: entry.agent,
@@ -579,6 +592,59 @@ function saveWriteScopeBaseline(step) {
579
592
  .split('\n').map((line) => line.trim()).filter(Boolean)[0] || `exit ${result ? result.status : 'unknown'}`;
580
593
  process.stderr.write(`[chain-team-phases] ${slug}: write-scope baseline for step ${stepId} not saved (${reason}); the audit will skip that step.\n`);
581
594
  }
595
+ const MODEL_TIERS = new Set(['cheap', 'standard', 'strong']);
596
+ const MODEL_SOURCES = new Set(['host', 'lease', 'tier-map', 'session']);
597
+ function boundedModel(value) {
598
+ if (typeof value !== 'string') return null;
599
+ const text = value.trim();
600
+ return /^[A-Za-z0-9][A-Za-z0-9._:@/#-]{0,119}$/.test(text) ? text : null;
601
+ }
602
+ function readTierMapModel(tier) {
603
+ try {
604
+ const parsed = JSON.parse(fs.readFileSync(path.join(process.cwd(), '.ai.local', 'team', 'model-tiers.json'), 'utf8'));
605
+ const tiers = parsed && parsed.tiers;
606
+ if (!tiers || typeof tiers !== 'object') return null;
607
+ const approvedBy = typeof parsed.approvedBy === 'string' ? parsed.approvedBy.trim() : '';
608
+ const filled = ['cheap', 'standard', 'strong'].some((key) => typeof tiers[key] === 'string' && tiers[key].trim() !== '');
609
+ if (approvedBy === '' && !filled) return null;
610
+ return boundedModel(tiers[tier]);
611
+ } catch {
612
+ return null;
613
+ }
614
+ }
615
+ function observedModel() {
616
+ const execution = hookInput && hookInput.execution;
617
+ if (execution && typeof execution === 'object') {
618
+ const model = boundedModel(execution.model);
619
+ if (model) return { model, modelSource: MODEL_SOURCES.has(execution.modelSource) ? execution.modelSource : 'host' };
620
+ }
621
+ const direct = boundedModel(hookInput && hookInput.model);
622
+ return direct ? { model: direct, modelSource: 'host' } : null;
623
+ }
624
+ function leaseModel() {
625
+ if (!sessionId) return null;
626
+ const lease = readSessionLease(sessionId);
627
+ if (!lease || lease.tool !== PLATFORM) return null;
628
+ const model = boundedModel(lease.model);
629
+ return model ? { model, modelSource: 'lease' } : null;
630
+ }
631
+ function executionForStep(step) {
632
+ const modelTier = step && MODEL_TIERS.has(step.model) ? step.model : null;
633
+ const observed = observedModel() || leaseModel();
634
+ if (observed) return { tool: PLATFORM, modelTier, model: observed.model, modelSource: observed.modelSource };
635
+ const mapped = modelTier ? readTierMapModel(modelTier) : null;
636
+ if (mapped) return { tool: PLATFORM, modelTier, model: mapped, modelSource: 'tier-map' };
637
+ return { tool: PLATFORM, modelTier, model: null, modelSource: null };
638
+ }
639
+ function closingExecution(entry, step) {
640
+ const model = boundedModel(entry && entry.model);
641
+ const armedTier = entry && MODEL_TIERS.has(entry.modelTier) ? entry.modelTier : null;
642
+ const armedSource = model && entry && MODEL_SOURCES.has(entry.modelSource) ? entry.modelSource : null;
643
+ const observed = observedModel();
644
+ const modelTier = (step && MODEL_TIERS.has(step.model) ? step.model : null) || armedTier;
645
+ if (observed) return { tool: PLATFORM, modelTier, model: observed.model, modelSource: observed.modelSource };
646
+ return { tool: PLATFORM, modelTier, model, modelSource: model ? armedSource : null };
647
+ }
582
648
  function activateAttempts(step, stepIndex, waveId = null, agentsOverride = null) {
583
649
  const agents = Array.isArray(agentsOverride) && agentsOverride.length > 0 ? agentsOverride : getStepAgents(step);
584
650
  const parallelGroupId = step.parallel && getStepAgents(step).length > 1 ? `${status.metricsRunId || slug}:step-${stepIndex}` : null;
@@ -588,8 +654,9 @@ function activateAttempts(step, stepIndex, waveId = null, agentsOverride = null)
588
654
  const attempt = nextAttemptOrdinal(stepIndex, agent); const runId = status.metricsRunId || `${slug}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
589
655
  status.metricsRunId = runId; const attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
590
656
  if (active[attemptId]) continue;
591
- active[attemptId] = { agent, stepIndex, attempt, startedAt: now, parallelGroupId, waveId: waveId || null };
592
- appendMetric({ type: 'attempt_started', at: now, attemptId, stepIndex, stepLabel: step.label || null, agent, role: roleFor(agent), attempt, parallelGroupId, waveId: waveId || null, source: 'hook', attributionConfidence: 'high' });
657
+ const execution = executionForStep(step);
658
+ active[attemptId] = { agent, stepIndex, attempt, startedAt: now, parallelGroupId, waveId: waveId || null, ...execution };
659
+ appendMetric({ type: 'attempt_started', at: now, attemptId, stepIndex, stepLabel: step.label || null, agent, role: roleFor(agent), attempt, parallelGroupId, waveId: waveId || null, source: 'hook', attributionConfidence: 'high', ...execution });
593
660
  }
594
661
  status.activeAttempts = active;
595
662
  saveWriteScopeBaseline(step);
@@ -1593,10 +1660,12 @@ function finishActiveAttempt(forced) {
1593
1660
  const idleMs = wallMs === null ? null : Math.max(0, wallMs - (activeMs ?? 0));
1594
1661
  const outcome = ['completed', 'blocked', 'changes_requested', 'validation_failed'].includes(status.state) ? status.state : 'unknown';
1595
1662
  const usage = sanitizeUsage(hookInput.usage);
1663
+ const execution = closingExecution(resolved.entry, Array.isArray(steps) ? steps[resolved.entry.stepIndex] : null);
1596
1664
  appendMetric({
1597
1665
  type: 'attempt_finished',
1598
1666
  at: now,
1599
1667
  attemptId: resolved.attemptId,
1668
+ ...execution,
1600
1669
  stepIndex: typeof resolved.entry.stepIndex === 'number' ? resolved.entry.stepIndex : null,
1601
1670
  agent: resolved.entry.agent || null,
1602
1671
  outcome,
@@ -2943,9 +3012,10 @@ function armResumedAttempt(step, stepIndex, agent) {
2943
3012
  0,
2944
3013
  ) + 1;
2945
3014
  attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
3015
+ const execution = executionForStep(step);
2946
3016
  status.activeAttempts = {
2947
3017
  ...(status.activeAttempts || {}),
2948
- [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null },
3018
+ [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null, ...execution },
2949
3019
  };
2950
3020
  events.push({
2951
3021
  eventId: `${runId}:${Date.now()}:${Math.random().toString(16).slice(2)}`,
@@ -2961,6 +3031,7 @@ function armResumedAttempt(step, stepIndex, agent) {
2961
3031
  waveId: null,
2962
3032
  source: 'hook',
2963
3033
  attributionConfidence: 'high',
3034
+ ...execution,
2964
3035
  });
2965
3036
  });
2966
3037
  return attemptId;
@@ -3136,12 +3207,17 @@ if (hookInput.action === 'out_of_band_start') {
3136
3207
  const agent = typeof hookInput.agent === 'string' ? hookInput.agent : 'unknown';
3137
3208
  const reason = typeof hookInput.reason === 'string' ? hookInput.reason : null;
3138
3209
  const outOfBandId = `${status.metricsRunId || slug}:oob:${Date.now()}:${Math.random().toString(16).slice(2, 8)}`;
3210
+ const observed = observedModel();
3139
3211
  appendMetric({
3140
3212
  type: 'out_of_band_started',
3141
3213
  outOfBandId,
3142
3214
  agent,
3143
3215
  reason,
3144
3216
  at: now,
3217
+ tool: PLATFORM,
3218
+ modelTier: null,
3219
+ model: observed ? observed.model : null,
3220
+ modelSource: observed ? observed.modelSource : null,
3145
3221
  source: 'orchestrator',
3146
3222
  attributionConfidence: 'high',
3147
3223
  });
@@ -3176,6 +3252,7 @@ if (hookInput.action === 'out_of_band_end') {
3176
3252
  }
3177
3253
  const durationMs = Math.max(0, Date.parse(now) - Date.parse(started.at));
3178
3254
  const outcome = typeof hookInput.outcome === 'string' ? hookInput.outcome : 'completed';
3255
+ const finishedModel = boundedModel(started.model);
3179
3256
  appendMetric({
3180
3257
  type: 'out_of_band_finished',
3181
3258
  outOfBandId: started.outOfBandId,
@@ -3184,6 +3261,10 @@ if (hookInput.action === 'out_of_band_end') {
3184
3261
  outcome,
3185
3262
  durationMs,
3186
3263
  at: now,
3264
+ tool: PLATFORM,
3265
+ modelTier: MODEL_TIERS.has(started.modelTier) ? started.modelTier : null,
3266
+ model: finishedModel,
3267
+ modelSource: finishedModel && MODEL_SOURCES.has(started.modelSource) ? started.modelSource : null,
3187
3268
  source: 'orchestrator',
3188
3269
  attributionConfidence: 'high',
3189
3270
  });