shipwright-cli 3.1.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (283) hide show
  1. package/.claude/agents/code-reviewer.md +2 -0
  2. package/.claude/agents/devops-engineer.md +2 -0
  3. package/.claude/agents/doc-fleet-agent.md +2 -0
  4. package/.claude/agents/pipeline-agent.md +2 -0
  5. package/.claude/agents/shell-script-specialist.md +2 -0
  6. package/.claude/agents/test-specialist.md +2 -0
  7. package/.claude/hooks/agent-crash-capture.sh +32 -0
  8. package/.claude/hooks/post-tool-use.sh +3 -2
  9. package/.claude/hooks/pre-tool-use.sh +35 -3
  10. package/README.md +22 -8
  11. package/claude-code/hooks/config-change.sh +18 -0
  12. package/claude-code/hooks/instructions-reloaded.sh +7 -0
  13. package/claude-code/hooks/worktree-create.sh +25 -0
  14. package/claude-code/hooks/worktree-remove.sh +20 -0
  15. package/config/code-constitution.json +130 -0
  16. package/config/defaults.json +25 -2
  17. package/config/policy.json +1 -1
  18. package/dashboard/middleware/auth.ts +134 -0
  19. package/dashboard/middleware/constants.ts +21 -0
  20. package/dashboard/public/index.html +8 -6
  21. package/dashboard/public/styles.css +176 -97
  22. package/dashboard/routes/auth.ts +38 -0
  23. package/dashboard/server.ts +117 -25
  24. package/dashboard/services/config.ts +26 -0
  25. package/dashboard/services/db.ts +118 -0
  26. package/dashboard/src/canvas/pixel-agent.ts +298 -0
  27. package/dashboard/src/canvas/pixel-sprites.ts +440 -0
  28. package/dashboard/src/canvas/shipyard-effects.ts +367 -0
  29. package/dashboard/src/canvas/shipyard-scene.ts +616 -0
  30. package/dashboard/src/canvas/submarine-layout.ts +267 -0
  31. package/dashboard/src/components/header.ts +8 -7
  32. package/dashboard/src/core/api.ts +5 -0
  33. package/dashboard/src/core/router.ts +1 -0
  34. package/dashboard/src/design/submarine-theme.ts +253 -0
  35. package/dashboard/src/main.ts +2 -0
  36. package/dashboard/src/types/api.ts +12 -1
  37. package/dashboard/src/views/activity.ts +2 -1
  38. package/dashboard/src/views/metrics.ts +69 -1
  39. package/dashboard/src/views/shipyard.ts +39 -0
  40. package/dashboard/types/index.ts +166 -0
  41. package/docs/plans/2026-02-28-compound-audit-and-shipyard-design.md +186 -0
  42. package/docs/plans/2026-02-28-skipper-shipwright-implementation-plan.md +1182 -0
  43. package/docs/plans/2026-02-28-skipper-shipwright-integration-design.md +531 -0
  44. package/docs/plans/2026-03-01-ai-powered-skill-injection-design.md +298 -0
  45. package/docs/plans/2026-03-01-ai-powered-skill-injection-plan.md +1109 -0
  46. package/docs/plans/2026-03-01-capabilities-cleanup-plan.md +658 -0
  47. package/docs/plans/2026-03-01-clean-architecture-plan.md +924 -0
  48. package/docs/plans/2026-03-01-compound-audit-cascade-design.md +191 -0
  49. package/docs/plans/2026-03-01-compound-audit-cascade-plan.md +921 -0
  50. package/docs/plans/2026-03-01-deep-integration-plan.md +851 -0
  51. package/docs/plans/2026-03-01-pipeline-audit-trail-design.md +145 -0
  52. package/docs/plans/2026-03-01-pipeline-audit-trail-plan.md +770 -0
  53. package/docs/plans/2026-03-01-refined-depths-brand-design.md +382 -0
  54. package/docs/plans/2026-03-01-refined-depths-implementation.md +599 -0
  55. package/docs/plans/2026-03-01-skipper-kernel-integration-design.md +203 -0
  56. package/docs/plans/2026-03-01-unified-platform-design.md +272 -0
  57. package/docs/plans/2026-03-07-claude-code-feature-integration-design.md +189 -0
  58. package/docs/plans/2026-03-07-claude-code-feature-integration-plan.md +1165 -0
  59. package/docs/research/BACKLOG_QUICK_REFERENCE.md +352 -0
  60. package/docs/research/CUTTING_EDGE_RESEARCH_2026.md +546 -0
  61. package/docs/research/RESEARCH_INDEX.md +439 -0
  62. package/docs/research/RESEARCH_SOURCES.md +440 -0
  63. package/docs/research/RESEARCH_SUMMARY.txt +275 -0
  64. package/docs/superpowers/specs/2026-03-10-pipeline-quality-revolution-design.md +341 -0
  65. package/package.json +2 -2
  66. package/scripts/lib/adaptive-model.sh +427 -0
  67. package/scripts/lib/adaptive-timeout.sh +316 -0
  68. package/scripts/lib/audit-trail.sh +309 -0
  69. package/scripts/lib/auto-recovery.sh +471 -0
  70. package/scripts/lib/bandit-selector.sh +431 -0
  71. package/scripts/lib/bootstrap.sh +104 -2
  72. package/scripts/lib/causal-graph.sh +455 -0
  73. package/scripts/lib/compat.sh +126 -0
  74. package/scripts/lib/compound-audit.sh +337 -0
  75. package/scripts/lib/constitutional.sh +454 -0
  76. package/scripts/lib/context-budget.sh +359 -0
  77. package/scripts/lib/convergence.sh +594 -0
  78. package/scripts/lib/cost-optimizer.sh +634 -0
  79. package/scripts/lib/daemon-adaptive.sh +14 -2
  80. package/scripts/lib/daemon-dispatch.sh +106 -17
  81. package/scripts/lib/daemon-failure.sh +34 -4
  82. package/scripts/lib/daemon-patrol.sh +25 -4
  83. package/scripts/lib/daemon-poll-github.sh +361 -0
  84. package/scripts/lib/daemon-poll-health.sh +299 -0
  85. package/scripts/lib/daemon-poll.sh +27 -611
  86. package/scripts/lib/daemon-state.sh +119 -66
  87. package/scripts/lib/daemon-triage.sh +10 -0
  88. package/scripts/lib/dod-scorecard.sh +442 -0
  89. package/scripts/lib/error-actionability.sh +300 -0
  90. package/scripts/lib/formal-spec.sh +461 -0
  91. package/scripts/lib/helpers.sh +180 -5
  92. package/scripts/lib/intent-analysis.sh +409 -0
  93. package/scripts/lib/loop-convergence.sh +350 -0
  94. package/scripts/lib/loop-iteration.sh +682 -0
  95. package/scripts/lib/loop-progress.sh +48 -0
  96. package/scripts/lib/loop-restart.sh +185 -0
  97. package/scripts/lib/memory-effectiveness.sh +506 -0
  98. package/scripts/lib/mutation-executor.sh +352 -0
  99. package/scripts/lib/outcome-feedback.sh +521 -0
  100. package/scripts/lib/pipeline-cli.sh +336 -0
  101. package/scripts/lib/pipeline-commands.sh +1216 -0
  102. package/scripts/lib/pipeline-detection.sh +101 -3
  103. package/scripts/lib/pipeline-execution.sh +897 -0
  104. package/scripts/lib/pipeline-github.sh +28 -3
  105. package/scripts/lib/pipeline-intelligence-compound.sh +431 -0
  106. package/scripts/lib/pipeline-intelligence-scoring.sh +407 -0
  107. package/scripts/lib/pipeline-intelligence-skip.sh +181 -0
  108. package/scripts/lib/pipeline-intelligence.sh +104 -1138
  109. package/scripts/lib/pipeline-quality-bash-compat.sh +182 -0
  110. package/scripts/lib/pipeline-quality-checks.sh +17 -711
  111. package/scripts/lib/pipeline-quality-gates.sh +563 -0
  112. package/scripts/lib/pipeline-stages-build.sh +730 -0
  113. package/scripts/lib/pipeline-stages-delivery.sh +965 -0
  114. package/scripts/lib/pipeline-stages-intake.sh +1133 -0
  115. package/scripts/lib/pipeline-stages-monitor.sh +407 -0
  116. package/scripts/lib/pipeline-stages-review.sh +1022 -0
  117. package/scripts/lib/pipeline-stages.sh +161 -2901
  118. package/scripts/lib/pipeline-state.sh +36 -5
  119. package/scripts/lib/pipeline-util.sh +487 -0
  120. package/scripts/lib/policy-learner.sh +438 -0
  121. package/scripts/lib/process-reward.sh +493 -0
  122. package/scripts/lib/project-detect.sh +649 -0
  123. package/scripts/lib/quality-profile.sh +334 -0
  124. package/scripts/lib/recruit-commands.sh +885 -0
  125. package/scripts/lib/recruit-learning.sh +739 -0
  126. package/scripts/lib/recruit-roles.sh +648 -0
  127. package/scripts/lib/reward-aggregator.sh +458 -0
  128. package/scripts/lib/rl-optimizer.sh +362 -0
  129. package/scripts/lib/root-cause.sh +427 -0
  130. package/scripts/lib/scope-enforcement.sh +445 -0
  131. package/scripts/lib/session-restart.sh +493 -0
  132. package/scripts/lib/skill-memory.sh +300 -0
  133. package/scripts/lib/skill-registry.sh +775 -0
  134. package/scripts/lib/spec-driven.sh +476 -0
  135. package/scripts/lib/test-helpers.sh +18 -7
  136. package/scripts/lib/test-holdout.sh +429 -0
  137. package/scripts/lib/test-optimizer.sh +511 -0
  138. package/scripts/shipwright-file-suggest.sh +45 -0
  139. package/scripts/skills/adversarial-quality.md +61 -0
  140. package/scripts/skills/api-design.md +44 -0
  141. package/scripts/skills/architecture-design.md +50 -0
  142. package/scripts/skills/brainstorming.md +43 -0
  143. package/scripts/skills/data-pipeline.md +44 -0
  144. package/scripts/skills/deploy-safety.md +64 -0
  145. package/scripts/skills/documentation.md +38 -0
  146. package/scripts/skills/frontend-design.md +45 -0
  147. package/scripts/skills/generated/.gitkeep +0 -0
  148. package/scripts/skills/generated/_refinements/.gitkeep +0 -0
  149. package/scripts/skills/generated/_refinements/adversarial-quality.patch.md +3 -0
  150. package/scripts/skills/generated/_refinements/architecture-design.patch.md +3 -0
  151. package/scripts/skills/generated/_refinements/brainstorming.patch.md +3 -0
  152. package/scripts/skills/generated/cli-version-management.md +29 -0
  153. package/scripts/skills/generated/collection-system-validation.md +99 -0
  154. package/scripts/skills/generated/large-scale-c-refactoring-coordination.md +97 -0
  155. package/scripts/skills/generated/pattern-matching-similarity-scoring.md +195 -0
  156. package/scripts/skills/generated/test-parallelization-detection.md +65 -0
  157. package/scripts/skills/observability.md +79 -0
  158. package/scripts/skills/performance.md +48 -0
  159. package/scripts/skills/pr-quality.md +49 -0
  160. package/scripts/skills/product-thinking.md +43 -0
  161. package/scripts/skills/security-audit.md +49 -0
  162. package/scripts/skills/systematic-debugging.md +40 -0
  163. package/scripts/skills/testing-strategy.md +47 -0
  164. package/scripts/skills/two-stage-review.md +52 -0
  165. package/scripts/skills/validation-thoroughness.md +55 -0
  166. package/scripts/sw +9 -3
  167. package/scripts/sw-activity.sh +9 -8
  168. package/scripts/sw-adaptive.sh +8 -7
  169. package/scripts/sw-adversarial.sh +2 -1
  170. package/scripts/sw-architecture-enforcer.sh +3 -1
  171. package/scripts/sw-auth.sh +12 -2
  172. package/scripts/sw-autonomous.sh +5 -1
  173. package/scripts/sw-changelog.sh +4 -1
  174. package/scripts/sw-checkpoint.sh +2 -1
  175. package/scripts/sw-ci.sh +15 -6
  176. package/scripts/sw-cleanup.sh +4 -26
  177. package/scripts/sw-code-review.sh +45 -20
  178. package/scripts/sw-connect.sh +2 -1
  179. package/scripts/sw-context.sh +2 -1
  180. package/scripts/sw-cost.sh +107 -5
  181. package/scripts/sw-daemon.sh +71 -11
  182. package/scripts/sw-dashboard.sh +3 -1
  183. package/scripts/sw-db.sh +71 -20
  184. package/scripts/sw-decide.sh +8 -2
  185. package/scripts/sw-decompose.sh +360 -17
  186. package/scripts/sw-deps.sh +4 -1
  187. package/scripts/sw-developer-simulation.sh +4 -1
  188. package/scripts/sw-discovery.sh +378 -5
  189. package/scripts/sw-doc-fleet.sh +4 -1
  190. package/scripts/sw-docs-agent.sh +3 -1
  191. package/scripts/sw-docs.sh +2 -1
  192. package/scripts/sw-doctor.sh +453 -2
  193. package/scripts/sw-dora.sh +4 -1
  194. package/scripts/sw-durable.sh +12 -7
  195. package/scripts/sw-e2e-orchestrator.sh +17 -16
  196. package/scripts/sw-eventbus.sh +13 -4
  197. package/scripts/sw-evidence.sh +364 -12
  198. package/scripts/sw-feedback.sh +550 -9
  199. package/scripts/sw-fix.sh +20 -1
  200. package/scripts/sw-fleet-discover.sh +6 -2
  201. package/scripts/sw-fleet-viz.sh +9 -4
  202. package/scripts/sw-fleet.sh +5 -1
  203. package/scripts/sw-github-app.sh +18 -4
  204. package/scripts/sw-github-checks.sh +3 -2
  205. package/scripts/sw-github-deploy.sh +3 -2
  206. package/scripts/sw-github-graphql.sh +18 -7
  207. package/scripts/sw-guild.sh +5 -1
  208. package/scripts/sw-heartbeat.sh +5 -30
  209. package/scripts/sw-hello.sh +67 -0
  210. package/scripts/sw-hygiene.sh +10 -3
  211. package/scripts/sw-incident.sh +273 -5
  212. package/scripts/sw-init.sh +18 -2
  213. package/scripts/sw-instrument.sh +10 -2
  214. package/scripts/sw-intelligence.sh +44 -7
  215. package/scripts/sw-jira.sh +5 -1
  216. package/scripts/sw-launchd.sh +2 -1
  217. package/scripts/sw-linear.sh +4 -1
  218. package/scripts/sw-logs.sh +4 -1
  219. package/scripts/sw-loop.sh +436 -1076
  220. package/scripts/sw-memory.sh +357 -3
  221. package/scripts/sw-mission-control.sh +6 -1
  222. package/scripts/sw-model-router.sh +483 -27
  223. package/scripts/sw-otel.sh +15 -4
  224. package/scripts/sw-oversight.sh +14 -5
  225. package/scripts/sw-patrol-meta.sh +334 -0
  226. package/scripts/sw-pipeline-composer.sh +7 -1
  227. package/scripts/sw-pipeline-vitals.sh +12 -6
  228. package/scripts/sw-pipeline.sh +54 -2653
  229. package/scripts/sw-pm.sh +16 -8
  230. package/scripts/sw-pr-lifecycle.sh +2 -1
  231. package/scripts/sw-predictive.sh +17 -5
  232. package/scripts/sw-prep.sh +185 -2
  233. package/scripts/sw-ps.sh +5 -25
  234. package/scripts/sw-public-dashboard.sh +17 -4
  235. package/scripts/sw-quality.sh +14 -6
  236. package/scripts/sw-reaper.sh +8 -25
  237. package/scripts/sw-recruit.sh +156 -2303
  238. package/scripts/sw-regression.sh +19 -12
  239. package/scripts/sw-release-manager.sh +3 -1
  240. package/scripts/sw-release.sh +4 -1
  241. package/scripts/sw-remote.sh +3 -1
  242. package/scripts/sw-replay.sh +7 -1
  243. package/scripts/sw-retro.sh +158 -1
  244. package/scripts/sw-review-rerun.sh +3 -1
  245. package/scripts/sw-scale.sh +14 -5
  246. package/scripts/sw-security-audit.sh +6 -1
  247. package/scripts/sw-self-optimize.sh +173 -6
  248. package/scripts/sw-session.sh +9 -3
  249. package/scripts/sw-setup.sh +3 -1
  250. package/scripts/sw-stall-detector.sh +406 -0
  251. package/scripts/sw-standup.sh +15 -7
  252. package/scripts/sw-status.sh +3 -1
  253. package/scripts/sw-strategic.sh +14 -6
  254. package/scripts/sw-stream.sh +13 -4
  255. package/scripts/sw-swarm.sh +20 -7
  256. package/scripts/sw-team-stages.sh +13 -6
  257. package/scripts/sw-templates.sh +7 -31
  258. package/scripts/sw-testgen.sh +17 -6
  259. package/scripts/sw-tmux-pipeline.sh +4 -1
  260. package/scripts/sw-tmux-role-color.sh +2 -0
  261. package/scripts/sw-tmux-status.sh +1 -1
  262. package/scripts/sw-tmux.sh +37 -1
  263. package/scripts/sw-trace.sh +3 -1
  264. package/scripts/sw-tracker-github.sh +3 -0
  265. package/scripts/sw-tracker-jira.sh +3 -0
  266. package/scripts/sw-tracker-linear.sh +3 -0
  267. package/scripts/sw-tracker.sh +3 -1
  268. package/scripts/sw-triage.sh +3 -2
  269. package/scripts/sw-upgrade.sh +3 -1
  270. package/scripts/sw-ux.sh +5 -2
  271. package/scripts/sw-webhook.sh +5 -2
  272. package/scripts/sw-widgets.sh +9 -4
  273. package/scripts/sw-worktree.sh +15 -3
  274. package/scripts/test-skill-injection.sh +1233 -0
  275. package/templates/pipelines/autonomous.json +27 -3
  276. package/templates/pipelines/cost-aware.json +34 -8
  277. package/templates/pipelines/deployed.json +12 -0
  278. package/templates/pipelines/enterprise.json +12 -0
  279. package/templates/pipelines/fast.json +6 -0
  280. package/templates/pipelines/full.json +27 -3
  281. package/templates/pipelines/hotfix.json +6 -0
  282. package/templates/pipelines/standard.json +12 -0
  283. package/templates/pipelines/tdd.json +12 -0
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env bash
2
+ # shellcheck disable=SC2034,SC2064 # config vars used by sourced scripts; traps expand at definition time
2
3
  # ╔═══════════════════════════════════════════════════════════════════════════╗
3
4
  # ║ shipwright self-optimize — Learning & Self-Tuning System ║
4
5
  # ║ Outcome analysis · Template tuning · Model routing · Memory evolution ║
@@ -6,7 +7,7 @@
6
7
  set -euo pipefail
7
8
  trap 'echo "ERROR: $BASH_SOURCE:$LINENO exited with status $?" >&2' ERR
8
9
 
9
- VERSION="3.1.0"
10
+ VERSION="3.3.0"
10
11
  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11
12
  REPO_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
12
13
 
@@ -29,7 +30,8 @@ fi
29
30
  if [[ "$(type -t emit_event 2>/dev/null)" != "function" ]]; then
30
31
  emit_event() {
31
32
  local event_type="$1"; shift; mkdir -p "${HOME}/.shipwright"
32
- local payload="{\"ts\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"type\":\"$event_type\""
33
+ local payload
34
+ payload="{\"ts\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"type\":\"$event_type\""
33
35
  while [[ $# -gt 0 ]]; do local key="${1%%=*}" val="${1#*=}"; payload="${payload},\"${key}\":\"${val}\""; shift; done
34
36
  echo "${payload}}" >> "${HOME}/.shipwright/events.jsonl"
35
37
  }
@@ -1181,6 +1183,162 @@ optimize_evolve_memory() {
1181
1183
  success "Memory evolved: pruned=$pruned, strengthened=$strengthened, promoted=$promoted"
1182
1184
  }
1183
1185
 
1186
+ # ═════════════════════════════════════════════════════════════════════════════
1187
+ # CONTEXT EFFICIENCY CLOSED LOOP
1188
+ # ═════════════════════════════════════════════════════════════════════════════
1189
+
1190
+ # optimize_tune_context_efficiency
1191
+ # Read loop.context_efficiency events and recommend context budget adjustments.
1192
+ # If avg budget_utilization > 90%, recommend increasing context_budget_chars.
1193
+ # If avg trim_ratio > 30%, recommend reducing verbose context sections.
1194
+ optimize_tune_context_efficiency() {
1195
+ local events_file="${EVENTS_FILE:-${HOME}/.shipwright/events.jsonl}"
1196
+
1197
+ if [[ ! -f "$events_file" ]]; then
1198
+ info "No events file found — skipping context efficiency tuning"
1199
+ return 0
1200
+ fi
1201
+
1202
+ ensure_optimization_dir
1203
+
1204
+ info "Analyzing context efficiency..."
1205
+
1206
+ # Extract recent loop.context_efficiency events (last 200)
1207
+ local ctx_events
1208
+ ctx_events=$(grep '"loop.context_efficiency"' "$events_file" 2>/dev/null | tail -200 || true)
1209
+
1210
+ if [[ -z "$ctx_events" ]]; then
1211
+ info "No context efficiency events found — skipping"
1212
+ return 0
1213
+ fi
1214
+
1215
+ local event_count
1216
+ event_count=$(echo "$ctx_events" | wc -l | tr -d ' ')
1217
+ event_count="${event_count:-0}"
1218
+
1219
+ if [[ "$event_count" -lt 3 ]]; then
1220
+ info "Insufficient context efficiency events ($event_count) — need at least 3"
1221
+ return 0
1222
+ fi
1223
+
1224
+ # Calculate averages using jq (handles both string and numeric JSON values)
1225
+ local stats
1226
+ stats=$(echo "$ctx_events" | jq -rs '
1227
+ if length == 0 then {u:0,r:0,raw:0,tr:0,b:0,n:0}
1228
+ else {
1229
+ u: ([.[] | .budget_utilization | tonumber] | add / length),
1230
+ r: ([.[] | .trim_ratio | tonumber] | add / length),
1231
+ raw: ([.[] | .raw_prompt_chars | tonumber] | add / length),
1232
+ tr: ([.[] | .trimmed_prompt_chars | tonumber] | add / length),
1233
+ b: ([.[] | .budget_chars | tonumber] | last // 0),
1234
+ n: length
1235
+ } end
1236
+ | "\(.u) \(.r) \(.raw) \(.tr) \(.b) \(.n)"
1237
+ ' 2>/dev/null || echo "0 0 0 0 0 0")
1238
+
1239
+ local avg_utilization avg_trim_ratio avg_raw avg_trimmed current_budget sample_count
1240
+ avg_utilization=$(echo "$stats" | awk '{printf "%.1f", $1}')
1241
+ avg_trim_ratio=$(echo "$stats" | awk '{printf "%.1f", $2}')
1242
+ avg_raw=$(echo "$stats" | awk '{printf "%.0f", $3}')
1243
+ avg_trimmed=$(echo "$stats" | awk '{printf "%.0f", $4}')
1244
+ current_budget=$(echo "$stats" | awk '{printf "%.0f", $5}')
1245
+ sample_count=$(echo "$stats" | awk '{printf "%d", $6}')
1246
+
1247
+ info "Context efficiency: avg_utilization=${avg_utilization}%, avg_trim_ratio=${avg_trim_ratio}%, samples=${sample_count}"
1248
+
1249
+ local recommendations=""
1250
+ local rec_count=0
1251
+
1252
+ # Rule 1: If avg budget_utilization > 90%, recommend increasing context_budget_chars
1253
+ if awk -v u="$avg_utilization" 'BEGIN { exit !(u > 90) }' 2>/dev/null; then
1254
+ local new_budget
1255
+ new_budget=$(awk -v b="$current_budget" 'BEGIN { printf "%.0f", b * 1.2 }')
1256
+ # Cap at 300000
1257
+ if awk -v nb="$new_budget" 'BEGIN { exit !(nb > 300000) }' 2>/dev/null; then
1258
+ new_budget=300000
1259
+ fi
1260
+ recommendations="${recommendations}increase_budget:${new_budget} "
1261
+ rec_count=$((rec_count + 1))
1262
+ warn "Budget utilization high (${avg_utilization}%) — recommend increasing context_budget_chars from ${current_budget} to ${new_budget}"
1263
+
1264
+ emit_event "optimize.context_recommendation" \
1265
+ "action=increase_budget" \
1266
+ "current=${current_budget}" \
1267
+ "recommended=${new_budget}" \
1268
+ "avg_utilization=${avg_utilization}" \
1269
+ "samples=${sample_count}"
1270
+ fi
1271
+
1272
+ # Rule 2: If avg trim_ratio > 30%, recommend reducing verbose context sections
1273
+ if awk -v r="$avg_trim_ratio" 'BEGIN { exit !(r > 30) }' 2>/dev/null; then
1274
+ local avg_discarded
1275
+ avg_discarded=$(awk -v raw="$avg_raw" -v trimmed="$avg_trimmed" 'BEGIN { printf "%.0f", raw - trimmed }')
1276
+ recommendations="${recommendations}reduce_verbose:${avg_discarded} "
1277
+ rec_count=$((rec_count + 1))
1278
+ warn "Trim ratio high (${avg_trim_ratio}%) — avg ${avg_discarded} chars discarded per iteration"
1279
+ warn "Recommend reducing verbose context: lower context_trim_memory_chars, context_trim_git_entries, or context_trim_test_lines"
1280
+
1281
+ emit_event "optimize.context_recommendation" \
1282
+ "action=reduce_verbose" \
1283
+ "avg_trim_ratio=${avg_trim_ratio}" \
1284
+ "avg_discarded=${avg_discarded}" \
1285
+ "samples=${sample_count}"
1286
+ fi
1287
+
1288
+ # Rule 3: If budget utilization is very low (<50%), recommend decreasing budget to save tokens
1289
+ if awk -v u="$avg_utilization" 'BEGIN { exit !(u < 50) }' 2>/dev/null && [[ "$sample_count" -ge 10 ]]; then
1290
+ local smaller_budget
1291
+ smaller_budget=$(awk -v b="$current_budget" 'BEGIN { printf "%.0f", b * 0.85 }')
1292
+ # Floor at 100000
1293
+ if awk -v sb="$smaller_budget" 'BEGIN { exit !(sb < 100000) }' 2>/dev/null; then
1294
+ smaller_budget=100000
1295
+ fi
1296
+ recommendations="${recommendations}decrease_budget:${smaller_budget} "
1297
+ rec_count=$((rec_count + 1))
1298
+ info "Budget utilization low (${avg_utilization}%) — recommend decreasing context_budget_chars from ${current_budget} to ${smaller_budget} to save tokens"
1299
+
1300
+ emit_event "optimize.context_recommendation" \
1301
+ "action=decrease_budget" \
1302
+ "current=${current_budget}" \
1303
+ "recommended=${smaller_budget}" \
1304
+ "avg_utilization=${avg_utilization}" \
1305
+ "samples=${sample_count}"
1306
+ fi
1307
+
1308
+ # Write context efficiency summary to optimization dir
1309
+ local summary_file="${OPTIMIZATION_DIR}/context-efficiency.json"
1310
+ local tmp_summary
1311
+ tmp_summary=$(mktemp "${summary_file}.tmp.XXXXXX")
1312
+ trap "rm -f '$tmp_summary'" RETURN
1313
+ jq -n \
1314
+ --argjson avg_utilization "$avg_utilization" \
1315
+ --argjson avg_trim_ratio "$avg_trim_ratio" \
1316
+ --argjson avg_raw "$avg_raw" \
1317
+ --argjson avg_trimmed "$avg_trimmed" \
1318
+ --argjson current_budget "${current_budget:-0}" \
1319
+ --argjson sample_count "$sample_count" \
1320
+ --argjson rec_count "$rec_count" \
1321
+ --arg recommendations "${recommendations}" \
1322
+ --arg updated "$(now_iso)" \
1323
+ '{
1324
+ avg_budget_utilization: $avg_utilization,
1325
+ avg_trim_ratio: $avg_trim_ratio,
1326
+ avg_raw_chars: $avg_raw,
1327
+ avg_trimmed_chars: $avg_trimmed,
1328
+ current_budget_chars: $current_budget,
1329
+ sample_count: $sample_count,
1330
+ recommendation_count: $rec_count,
1331
+ recommendations: $recommendations,
1332
+ updated_at: $updated
1333
+ }' > "$tmp_summary" && mv "$tmp_summary" "$summary_file" || rm -f "$tmp_summary"
1334
+
1335
+ if [[ "$rec_count" -eq 0 ]]; then
1336
+ success "Context efficiency is healthy (utilization: ${avg_utilization}%, trim: ${avg_trim_ratio}%)"
1337
+ else
1338
+ success "Context efficiency analysis complete — $rec_count recommendation(s)"
1339
+ fi
1340
+ }
1341
+
1184
1342
  # ═════════════════════════════════════════════════════════════════════════════
1185
1343
  # QUALITY INDEX (LONGITUDINAL TRACKING)
1186
1344
  # ═════════════════════════════════════════════════════════════════════════════
@@ -1206,10 +1364,14 @@ optimize_track_quality_index() {
1206
1364
  [[ -z "$recent" ]] && return 0
1207
1365
 
1208
1366
  local total success_count
1209
- total=$(echo "$recent" | wc -l | tr -d ' ')
1367
+ total=$(echo "$recent" | wc -l || true)
1368
+ total="${total:-0}"
1369
+ total=$(echo "$total" | tr -d ' ')
1210
1370
  [[ "$total" -lt 3 ]] && return 0
1211
1371
 
1212
- success_count=$(echo "$recent" | jq -c 'select(.result == "success" or .result == "completed")' 2>/dev/null | wc -l | tr -d ' ')
1372
+ success_count=$(echo "$recent" | jq -c 'select(.result == "success" or .result == "completed")' 2>/dev/null | wc -l || true)
1373
+ success_count="${success_count:-0}"
1374
+ success_count=$(echo "$success_count" | tr -d ' ')
1213
1375
  success_count="${success_count:-0}"
1214
1376
 
1215
1377
  local avg_iterations avg_quality
@@ -1229,13 +1391,15 @@ optimize_track_quality_index() {
1229
1391
  local quality_index
1230
1392
  quality_index=$(awk "BEGIN{printf \"%.0f\", ($success_rate * 0.4) + ($efficiency * 0.3) + ($avg_quality * 0.3)}" 2>/dev/null || echo "0")
1231
1393
 
1232
- local entry="{\"timestamp\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"window\":$window,\"total\":$total,\"success_rate\":$success_rate,\"avg_iterations\":$avg_iterations,\"avg_quality\":$avg_quality,\"efficiency\":$efficiency,\"quality_index\":$quality_index}"
1394
+ local entry
1395
+ entry="{\"timestamp\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"window\":$window,\"total\":$total,\"success_rate\":$success_rate,\"avg_iterations\":$avg_iterations,\"avg_quality\":$avg_quality,\"efficiency\":$efficiency,\"quality_index\":$quality_index}"
1233
1396
  echo "$entry" >> "$quality_file"
1234
1397
 
1235
1398
  # Detect trend
1236
1399
  if [[ -f "$quality_file" ]]; then
1237
1400
  local line_count
1238
- line_count=$(wc -l < "$quality_file" 2>/dev/null | tr -d ' ' || echo "0")
1401
+ line_count=$(wc -l < "$quality_file" 2>/dev/null | tr -d ' ' || true)
1402
+ line_count="${line_count:-0}"
1239
1403
  if [[ "$line_count" -ge 2 ]]; then
1240
1404
  local prev_index
1241
1405
  prev_index=$(tail -2 "$quality_file" | head -1 | jq -r '.quality_index // 0' 2>/dev/null || echo "0")
@@ -1295,6 +1459,7 @@ optimize_full_analysis() {
1295
1459
  optimize_route_models
1296
1460
  optimize_learn_risk_keywords
1297
1461
  optimize_evolve_memory
1462
+ optimize_tune_context_efficiency 2>/dev/null || true
1298
1463
  optimize_track_quality_index 2>/dev/null || true
1299
1464
  optimize_report >> "${OPTIMIZATION_DIR}/last-report.txt" 2>/dev/null || true
1300
1465
  optimize_adjust_audit_intensity 2>/dev/null || true
@@ -1489,6 +1654,7 @@ show_help() {
1489
1654
  echo " quality-index Show quality trend (last 10 snapshots)"
1490
1655
  echo " ingest-retro Ingest most recent retro into optimization loop"
1491
1656
  echo " evolve-memory Prune/strengthen/promote memory patterns"
1657
+ echo " context-efficiency Analyze context budget usage and recommend tuning"
1492
1658
  echo " help Show this help"
1493
1659
  echo ""
1494
1660
  echo -e "${CYAN}STORAGE${RESET}"
@@ -1513,6 +1679,7 @@ main() {
1513
1679
  report) optimize_report ;;
1514
1680
  quality-index) cmd_quality_index ;;
1515
1681
  evolve-memory) optimize_evolve_memory ;;
1682
+ context-efficiency) optimize_tune_context_efficiency ;;
1516
1683
  help|--help|-h) show_help ;;
1517
1684
  *) error "Unknown command: $cmd"; exit 1 ;;
1518
1685
  esac
@@ -8,7 +8,8 @@
8
8
  # ║ Supports --template to scaffold from a team template and --terminal ║
9
9
  # ║ to select a terminal adapter (tmux, iterm2, wezterm). ║
10
10
  # ╚═══════════════════════════════════════════════════════════════════════════╝
11
- VERSION="3.1.0"
11
+ # shellcheck disable=SC2034
12
+ VERSION="3.3.0"
12
13
  set -euo pipefail
13
14
  trap 'echo "ERROR: $BASH_SOURCE:$LINENO exited with status $?" >&2' ERR
14
15
 
@@ -33,6 +34,7 @@ fi
33
34
  if [[ "$(type -t emit_event 2>/dev/null)" != "function" ]]; then
34
35
  emit_event() {
35
36
  local event_type="$1"; shift; mkdir -p "${HOME}/.shipwright"
37
+ # shellcheck disable=SC2155
36
38
  local payload="{\"ts\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"type\":\"$event_type\""
37
39
  while [[ $# -gt 0 ]]; do local key="${1%%=*}" val="${1#*=}"; payload="${payload},\"${key}\":\"${val}\""; shift; done
38
40
  echo "${payload}}" >> "${HOME}/.shipwright/events.jsonl"
@@ -206,8 +208,12 @@ if [[ -n "$TEMPLATE_NAME" ]]; then
206
208
  case "$key" in
207
209
  description) TEMPLATE_DESC="$value" ;;
208
210
  layout) TEMPLATE_LAYOUT="$value" ;;
209
- layout_style) TEMPLATE_LAYOUT_STYLE="$value" ;;
210
- main_pane_percent) TEMPLATE_MAIN_PANE_PERCENT="$value" ;;
211
+ layout_style)
212
+ # shellcheck disable=SC2034
213
+ TEMPLATE_LAYOUT_STYLE="$value" ;;
214
+ main_pane_percent)
215
+ # shellcheck disable=SC2034
216
+ TEMPLATE_MAIN_PANE_PERCENT="$value" ;;
211
217
  esac
212
218
  ;;
213
219
  AGENT) [[ -n "$key" ]] && TEMPLATE_AGENTS+=("$key") ;;
@@ -10,9 +10,10 @@
10
10
  set -euo pipefail
11
11
  trap 'echo "ERROR: $BASH_SOURCE:$LINENO exited with status $?" >&2' ERR
12
12
 
13
- VERSION="3.1.0"
13
+ VERSION="3.3.0"
14
14
 
15
15
  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16
+ # shellcheck disable=SC2034
16
17
  REPO_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
17
18
 
18
19
  # ─── Cross-platform compatibility ──────────────────────────────────────────
@@ -34,6 +35,7 @@ fi
34
35
  if [[ "$(type -t emit_event 2>/dev/null)" != "function" ]]; then
35
36
  emit_event() {
36
37
  local event_type="$1"; shift; mkdir -p "${HOME}/.shipwright"
38
+ # shellcheck disable=SC2155
37
39
  local payload="{\"ts\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"type\":\"$event_type\""
38
40
  while [[ $# -gt 0 ]]; do local key="${1%%=*}" val="${1#*=}"; payload="${payload},\"${key}\":\"${val}\""; shift; done
39
41
  echo "${payload}}" >> "${HOME}/.shipwright/events.jsonl"