@bonesofspring/ai-rules 0.2.91 → 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 (99) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/docs/TEAM-DB.md +2 -0
  3. package/package.json +1 -1
  4. package/presets/_shared/ai-local/scripts/team-db-api.mjs +33 -15
  5. package/presets/_shared/ai-local/scripts/team-db-openapi.mjs +15 -0
  6. package/presets/_shared/ai-local/scripts/team-db-project.mjs +41 -2
  7. package/presets/_shared/ai-local/scripts/team-db-schema.mjs +23 -0
  8. package/presets/_shared/core/agent-team/agent-task-metrics.md +16 -1
  9. package/presets/_shared/hooks/chain-team-phases.sh.tmpl +87 -6
  10. package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +87 -6
  11. package/presets/claude/go/hooks/chain-team-phases.sh +87 -6
  12. package/presets/claude/ios-swift/hooks/chain-team-phases.sh +87 -6
  13. package/presets/claude/java/hooks/chain-team-phases.sh +87 -6
  14. package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +87 -6
  15. package/presets/claude/mol/hooks/chain-team-phases.sh +87 -6
  16. package/presets/claude/next/hooks/chain-team-phases.sh +87 -6
  17. package/presets/claude/nuxt/hooks/chain-team-phases.sh +87 -6
  18. package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +87 -6
  19. package/presets/claude/php-laravel/hooks/chain-team-phases.sh +87 -6
  20. package/presets/claude/svelte/hooks/chain-team-phases.sh +87 -6
  21. package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +87 -6
  22. package/presets/cursor/go/hooks/chain-team-phases.sh +87 -6
  23. package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +87 -6
  24. package/presets/cursor/java/hooks/chain-team-phases.sh +87 -6
  25. package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +87 -6
  26. package/presets/cursor/mol/hooks/chain-team-phases.sh +87 -6
  27. package/presets/cursor/next/hooks/chain-team-phases.sh +87 -6
  28. package/presets/cursor/nuxt/hooks/chain-team-phases.sh +87 -6
  29. package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +87 -6
  30. package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +87 -6
  31. package/presets/cursor/svelte/hooks/chain-team-phases.sh +87 -6
  32. package/presets/mavis/android-kotlin/adapter-map.json +1 -1
  33. package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +87 -6
  34. package/presets/mavis/go/adapter-map.json +1 -1
  35. package/presets/mavis/go/hooks/chain-team-phases.sh +87 -6
  36. package/presets/mavis/ios-swift/adapter-map.json +1 -1
  37. package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +87 -6
  38. package/presets/mavis/java/adapter-map.json +1 -1
  39. package/presets/mavis/java/hooks/chain-team-phases.sh +87 -6
  40. package/presets/mavis/mcp-ts/adapter-map.json +1 -1
  41. package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +87 -6
  42. package/presets/mavis/mol/adapter-map.json +1 -1
  43. package/presets/mavis/mol/hooks/chain-team-phases.sh +87 -6
  44. package/presets/mavis/next/adapter-map.json +1 -1
  45. package/presets/mavis/next/hooks/chain-team-phases.sh +87 -6
  46. package/presets/mavis/nuxt/adapter-map.json +1 -1
  47. package/presets/mavis/nuxt/hooks/chain-team-phases.sh +87 -6
  48. package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
  49. package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +87 -6
  50. package/presets/mavis/php-laravel/adapter-map.json +1 -1
  51. package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +87 -6
  52. package/presets/mavis/svelte/adapter-map.json +1 -1
  53. package/presets/mavis/svelte/hooks/chain-team-phases.sh +87 -6
  54. package/presets/opencode/android-kotlin/adapter-map.json +1 -1
  55. package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +87 -6
  56. package/presets/opencode/go/adapter-map.json +1 -1
  57. package/presets/opencode/go/hooks/chain-team-phases.sh +87 -6
  58. package/presets/opencode/ios-swift/adapter-map.json +1 -1
  59. package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +87 -6
  60. package/presets/opencode/java/adapter-map.json +1 -1
  61. package/presets/opencode/java/hooks/chain-team-phases.sh +87 -6
  62. package/presets/opencode/mcp-ts/adapter-map.json +1 -1
  63. package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +87 -6
  64. package/presets/opencode/mol/adapter-map.json +1 -1
  65. package/presets/opencode/mol/hooks/chain-team-phases.sh +87 -6
  66. package/presets/opencode/next/adapter-map.json +1 -1
  67. package/presets/opencode/next/hooks/chain-team-phases.sh +87 -6
  68. package/presets/opencode/nuxt/adapter-map.json +1 -1
  69. package/presets/opencode/nuxt/hooks/chain-team-phases.sh +87 -6
  70. package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
  71. package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +87 -6
  72. package/presets/opencode/php-laravel/adapter-map.json +1 -1
  73. package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +87 -6
  74. package/presets/opencode/plugins/task-status/README.md +2 -2
  75. package/presets/opencode/plugins/task-status/deepseek.ts +8 -4
  76. package/presets/opencode/svelte/adapter-map.json +1 -1
  77. package/presets/opencode/svelte/hooks/chain-team-phases.sh +87 -6
  78. package/presets/zcode/android-kotlin/adapter-map.json +1 -1
  79. package/presets/zcode/android-kotlin/hooks/chain-team-phases.sh +87 -6
  80. package/presets/zcode/go/adapter-map.json +1 -1
  81. package/presets/zcode/go/hooks/chain-team-phases.sh +87 -6
  82. package/presets/zcode/ios-swift/adapter-map.json +1 -1
  83. package/presets/zcode/ios-swift/hooks/chain-team-phases.sh +87 -6
  84. package/presets/zcode/java/adapter-map.json +1 -1
  85. package/presets/zcode/java/hooks/chain-team-phases.sh +87 -6
  86. package/presets/zcode/mcp-ts/adapter-map.json +1 -1
  87. package/presets/zcode/mcp-ts/hooks/chain-team-phases.sh +87 -6
  88. package/presets/zcode/mol/adapter-map.json +1 -1
  89. package/presets/zcode/mol/hooks/chain-team-phases.sh +87 -6
  90. package/presets/zcode/next/adapter-map.json +1 -1
  91. package/presets/zcode/next/hooks/chain-team-phases.sh +87 -6
  92. package/presets/zcode/nuxt/adapter-map.json +1 -1
  93. package/presets/zcode/nuxt/hooks/chain-team-phases.sh +87 -6
  94. package/presets/zcode/php-hexagonal/adapter-map.json +1 -1
  95. package/presets/zcode/php-hexagonal/hooks/chain-team-phases.sh +87 -6
  96. package/presets/zcode/php-laravel/adapter-map.json +1 -1
  97. package/presets/zcode/php-laravel/hooks/chain-team-phases.sh +87 -6
  98. package/presets/zcode/svelte/adapter-map.json +1 -1
  99. package/presets/zcode/svelte/hooks/chain-team-phases.sh +87 -6
@@ -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 = 'android-kotlin';
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,
@@ -2946,9 +3015,10 @@ function armResumedAttempt(step, stepIndex, agent) {
2946
3015
  0,
2947
3016
  ) + 1;
2948
3017
  attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
3018
+ const execution = executionForStep(step);
2949
3019
  status.activeAttempts = {
2950
3020
  ...(status.activeAttempts || {}),
2951
- [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null },
3021
+ [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null, ...execution },
2952
3022
  };
2953
3023
  events.push({
2954
3024
  eventId: `${runId}:${Date.now()}:${Math.random().toString(16).slice(2)}`,
@@ -2964,6 +3034,7 @@ function armResumedAttempt(step, stepIndex, agent) {
2964
3034
  waveId: null,
2965
3035
  source: 'hook',
2966
3036
  attributionConfidence: 'high',
3037
+ ...execution,
2967
3038
  });
2968
3039
  });
2969
3040
  return attemptId;
@@ -3139,12 +3210,17 @@ if (hookInput.action === 'out_of_band_start') {
3139
3210
  const agent = typeof hookInput.agent === 'string' ? hookInput.agent : 'unknown';
3140
3211
  const reason = typeof hookInput.reason === 'string' ? hookInput.reason : null;
3141
3212
  const outOfBandId = `${status.metricsRunId || slug}:oob:${Date.now()}:${Math.random().toString(16).slice(2, 8)}`;
3213
+ const observed = observedModel();
3142
3214
  appendMetric({
3143
3215
  type: 'out_of_band_started',
3144
3216
  outOfBandId,
3145
3217
  agent,
3146
3218
  reason,
3147
3219
  at: now,
3220
+ tool: PLATFORM,
3221
+ modelTier: null,
3222
+ model: observed ? observed.model : null,
3223
+ modelSource: observed ? observed.modelSource : null,
3148
3224
  source: 'orchestrator',
3149
3225
  attributionConfidence: 'high',
3150
3226
  });
@@ -3179,6 +3255,7 @@ if (hookInput.action === 'out_of_band_end') {
3179
3255
  }
3180
3256
  const durationMs = Math.max(0, Date.parse(now) - Date.parse(started.at));
3181
3257
  const outcome = typeof hookInput.outcome === 'string' ? hookInput.outcome : 'completed';
3258
+ const finishedModel = boundedModel(started.model);
3182
3259
  appendMetric({
3183
3260
  type: 'out_of_band_finished',
3184
3261
  outOfBandId: started.outOfBandId,
@@ -3187,6 +3264,10 @@ if (hookInput.action === 'out_of_band_end') {
3187
3264
  outcome,
3188
3265
  durationMs,
3189
3266
  at: now,
3267
+ tool: PLATFORM,
3268
+ modelTier: MODEL_TIERS.has(started.modelTier) ? started.modelTier : null,
3269
+ model: finishedModel,
3270
+ modelSource: finishedModel && MODEL_SOURCES.has(started.modelSource) ? started.modelSource : null,
3190
3271
  source: 'orchestrator',
3191
3272
  attributionConfidence: 'high',
3192
3273
  });
@@ -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 = 'go';
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
  });
@@ -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 = 'ios-swift';
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,
@@ -2946,9 +3015,10 @@ function armResumedAttempt(step, stepIndex, agent) {
2946
3015
  0,
2947
3016
  ) + 1;
2948
3017
  attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
3018
+ const execution = executionForStep(step);
2949
3019
  status.activeAttempts = {
2950
3020
  ...(status.activeAttempts || {}),
2951
- [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null },
3021
+ [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null, ...execution },
2952
3022
  };
2953
3023
  events.push({
2954
3024
  eventId: `${runId}:${Date.now()}:${Math.random().toString(16).slice(2)}`,
@@ -2964,6 +3034,7 @@ function armResumedAttempt(step, stepIndex, agent) {
2964
3034
  waveId: null,
2965
3035
  source: 'hook',
2966
3036
  attributionConfidence: 'high',
3037
+ ...execution,
2967
3038
  });
2968
3039
  });
2969
3040
  return attemptId;
@@ -3139,12 +3210,17 @@ if (hookInput.action === 'out_of_band_start') {
3139
3210
  const agent = typeof hookInput.agent === 'string' ? hookInput.agent : 'unknown';
3140
3211
  const reason = typeof hookInput.reason === 'string' ? hookInput.reason : null;
3141
3212
  const outOfBandId = `${status.metricsRunId || slug}:oob:${Date.now()}:${Math.random().toString(16).slice(2, 8)}`;
3213
+ const observed = observedModel();
3142
3214
  appendMetric({
3143
3215
  type: 'out_of_band_started',
3144
3216
  outOfBandId,
3145
3217
  agent,
3146
3218
  reason,
3147
3219
  at: now,
3220
+ tool: PLATFORM,
3221
+ modelTier: null,
3222
+ model: observed ? observed.model : null,
3223
+ modelSource: observed ? observed.modelSource : null,
3148
3224
  source: 'orchestrator',
3149
3225
  attributionConfidence: 'high',
3150
3226
  });
@@ -3179,6 +3255,7 @@ if (hookInput.action === 'out_of_band_end') {
3179
3255
  }
3180
3256
  const durationMs = Math.max(0, Date.parse(now) - Date.parse(started.at));
3181
3257
  const outcome = typeof hookInput.outcome === 'string' ? hookInput.outcome : 'completed';
3258
+ const finishedModel = boundedModel(started.model);
3182
3259
  appendMetric({
3183
3260
  type: 'out_of_band_finished',
3184
3261
  outOfBandId: started.outOfBandId,
@@ -3187,6 +3264,10 @@ if (hookInput.action === 'out_of_band_end') {
3187
3264
  outcome,
3188
3265
  durationMs,
3189
3266
  at: now,
3267
+ tool: PLATFORM,
3268
+ modelTier: MODEL_TIERS.has(started.modelTier) ? started.modelTier : null,
3269
+ model: finishedModel,
3270
+ modelSource: finishedModel && MODEL_SOURCES.has(started.modelSource) ? started.modelSource : null,
3190
3271
  source: 'orchestrator',
3191
3272
  attributionConfidence: 'high',
3192
3273
  });