@albinocrabs/feynman 0.2.2 → 0.2.5

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.
@@ -0,0 +1,112 @@
1
+ # C4-Style Architecture and Request Flow
2
+
3
+ ## Question
4
+
5
+ > Sketch a clean C4-style view for an AI documentation tool and show the
6
+ > normal request flow. I want context, container split, component split, and a
7
+ > clear status view for blockers.
8
+
9
+ ## Without feynman
10
+
11
+ The tool has three users: author, reviewer, and operator. It includes a web
12
+ client, a prompt gateway API, a rules engine, a diagram renderer, and a
13
+ quality service that validates responses before returning them. There is
14
+ SSO-based auth, storage for templates, and a shared observability channel.
15
+ If the rules fail to load, the request still needs a deterministic fallback to
16
+ text mode so the user is not blocked.
17
+
18
+ ## With feynman
19
+
20
+ ### C4 context
21
+
22
+ ```
23
+ feynman-system
24
+ ├── actors
25
+ │ ├── [Document Author]
26
+ │ ├── [Reviewer]
27
+ │ └── [Operator]
28
+ ├── containers
29
+ │ ├── [Web Client]
30
+ │ ├── [Prompt Gateway API]
31
+ │ ├── [Rule Service]
32
+ │ ├── [Diagram Renderer]
33
+ │ └── [Quality Service]
34
+ └── external systems
35
+ ├── [SSO]
36
+ ├── [Template Storage]
37
+ ├── [Model Provider]
38
+ └── [Observability]
39
+ ```
40
+
41
+ ### C4 container run
42
+
43
+ ```
44
+ [Document Author] --> [Web Client]
45
+ |
46
+ v
47
+ [Reviewer] --> [Prompt Gateway API]
48
+ |
49
+ v
50
+ [Auth + Rate Limit]
51
+ |
52
+ +-- unauthorized --> [403 / 401]
53
+ |
54
+ +-- authorized
55
+ |
56
+ v
57
+ [Rule Service]
58
+ |
59
+ +-- rule set miss --> [Text fallback]
60
+ |
61
+ +-- rule set hit
62
+ |
63
+ v
64
+ [Diagram Renderer]
65
+ |
66
+ v
67
+ [Quality Service]
68
+ |
69
+ +-- blocked --> [Recovery Plan]
70
+ |
71
+ +-- pass --> [JSON response]
72
+ |
73
+ v
74
+ [Observability publish]
75
+ |
76
+ v
77
+ [Document Author/Reviewer]
78
+ ```
79
+
80
+ ### Architecture split
81
+
82
+ ```
83
+ criterion | Context (C1) | Containers (C2) | Components (C3)
84
+ ---------------|---------------------|----------------------|----------------------
85
+ Main question | Who talks to what | Who owns boundary | Who owns behavior
86
+ Primary risk | Missing actor path | Wrong trust boundary | Rule fallback bug
87
+ Owner now | Product + Ops | Backend + Security | Runtime rule authors
88
+ ```
89
+
90
+ ### Why this helps
91
+
92
+ ```
93
+ ┌─ Delivery readiness ────────────┐
94
+ context map done
95
+ container flow done
96
+ component split done
97
+ risk hotspots identified
98
+ └─────────────────────────────────┘
99
+ ```
100
+
101
+ ## Why this works
102
+
103
+ Without explicit structure, the explanation is a dense paragraph. With the C4
104
+ perspective, Feynman converts it into:
105
+ - actor/system decomposition,
106
+ - runtime sequence,
107
+ - boundary+fallback behavior,
108
+ - and explicit risk visibility.
109
+
110
+ The result is understandable quickly and can be reviewed or extended as a
111
+ single architecture baseline.
112
+
@@ -0,0 +1,77 @@
1
+ # Context Splitting: Product Initiative to Deploy a New Onboarding Assistant
2
+
3
+ ## Question
4
+
5
+ > We want to launch a new onboarding assistant next quarter. It touches web UI, backend,
6
+ > legal review, and customer support. How should we split work so leadership can
7
+ > understand risk, dependencies, and rollout order in one view?
8
+
9
+ ## Without feynman
10
+
11
+ The initiative includes multiple teams and timelines. First we should do discovery and
12
+ alignment between engineering, legal, and support. Then UI and backend work must start,
13
+ because both rely on design contracts. A pilot should run with 10% of new users, then
14
+ global rollout should happen after legal and reliability checks are complete.
15
+
16
+ ## With feynman
17
+
18
+ Scope decomposition:
19
+
20
+ ```
21
+ [Onboarding assistant]
22
+ ├── Product & UX
23
+ │ ├── onboarding copy
24
+ │ ├── microcopy guardrails
25
+ │ └── in-app hints
26
+ ├── Platform
27
+ │ ├── assistant API
28
+ │ ├── telemetry events
29
+ │ └── admin flags
30
+ ├── Legal & Compliance
31
+ │ ├── consent text
32
+ │ └── data retention policy
33
+ └── Operations
34
+ ├── runbook
35
+ ├── on-call drill
36
+ └── rollback script
37
+ ```
38
+
39
+ Cross-team launch flow:
40
+
41
+ ```
42
+ [Legal approves data flow] --> [Backend API contract ready]
43
+ |
44
+ v
45
+ [UX/Copy draft] --> [Integration tests] --> [10% pilot]
46
+ | |
47
+ fail --> [fix + retest] v
48
+ [60-day rollback check]
49
+ |
50
+ +--> [full rollout]
51
+ ```
52
+
53
+ Dependency safety frame:
54
+
55
+ - legal-ok: mandatory
56
+ - telemetry path: must emit onboarding_success and onboarding_fail
57
+ - fallback: always-on silent mode if latency > 300ms
58
+ - rollback: kill-switch <2 minutes
59
+
60
+ Priority lanes:
61
+
62
+ ```
63
+ ▲ high
64
+ legal/consent review
65
+ backend idempotency
66
+ kill-switch + rollback readiness
67
+ ▼ low
68
+ copy polishing
69
+ dashboard cosmetics
70
+ ```
71
+
72
+ ## Why this works
73
+
74
+ The question has nested uncertainty and cross-team constraints. The tree diagram makes
75
+ the decomposition explicit. The flow diagram shows execution order and rework loops.
76
+ The frame and priority lanes turn soft governance requirements into checkable launch
77
+ conditions.
@@ -0,0 +1,107 @@
1
+ # Feature Planning: Build Internal Search or Use Managed API
2
+
3
+ ## Question
4
+
5
+ > We need fast text search by title/body tags. Should we build it ourselves or use
6
+ > a managed search service? Compare the options by cost, speed, and maintenance.
7
+
8
+ ## Without feynman
9
+
10
+ Building search internally gives us full control over ranking but requires schema
11
+ indexing work, query tuning, and ongoing reliability engineering. A managed API
12
+ is faster to deliver and has better relevance out of the box, but it increases
13
+ vendor dependency and recurring cost. We can reduce risk by evaluating latency,
14
+ cost, and maintenance for a 6-month period and then revisiting.
15
+
16
+ ## With feynman
17
+
18
+ Decision matrix:
19
+
20
+ ```
21
+ Option | build-internal | managed-search-service
22
+ ------------------|--------------------------------|--------------------------
23
+ speed-to-market | 8-12 weeks | 1-2 weeks
24
+ query latency | 60-120ms (with cache) | 40-80ms
25
+ maintenance | high (2 engineers, on-call) | low
26
+ vendor lock-in | none | medium-high
27
+ relevance quality | custom control, tuning effort | high, pre-tuned
28
+ ```
29
+
30
+ Decision flow:
31
+
32
+ ```
33
+ [Need search by title/body now?] --> [Yes]
34
+ |
35
+ v
36
+ [Need search now?] --> [Evaluate managed by default]
37
+ |
38
+ v
39
+ [POC in 2 weeks]
40
+ |
41
+ +-------------------- +--------------------+
42
+ | |
43
+ v v
44
+ [Latency/cost ok] [No]
45
+ | |
46
+ +--> [Adopt] +--> [Re-open internal build path]
47
+ |
48
+ +--> [Plan v1 migration in 1 sprint]
49
+ ```
50
+
51
+ Governance priority:
52
+
53
+ ```
54
+ ▲ high
55
+ Vendor contract review (SLA, data residency)
56
+ Incident drill: provider outage fallback plan
57
+ ▼ low
58
+ UI polish in search result cards
59
+ Advanced synonym tuning
60
+ ```
61
+
62
+ Phased rollout map:
63
+
64
+ ```
65
+ [Decision]
66
+ |
67
+ v
68
+ [POC + telemetry]
69
+ |
70
+ +-- latency/cost tests fail? --+--> [Re-scope]
71
+ |
72
+ +-- latency/cost tests pass? --> [Fallback path] --> [Adoption path]
73
+ |
74
+ +--> [Cost optimization] --> [Quarterly review]
75
+ ```
76
+
77
+ Rollback frame:
78
+
79
+ ```
80
+ Managed API chosen:
81
+ - fail trigger: P95 > 2x baseline + cost ↑
82
+ - response: cut volume 50%, enable fallback
83
+ - rollback time: 45 min
84
+ - owner: on-call + search guild
85
+ ```
86
+
87
+ Context split before execution:
88
+
89
+ ```
90
+ [Business goal]
91
+ ├── [Performance]
92
+ │ ├── latency
93
+ │ └── availability
94
+ ├── [Economics]
95
+ │ ├── direct cost
96
+ │ └── hidden support cost
97
+ └── [Risk]
98
+ ├── lock-in
99
+ ├── security
100
+ └── reversibility
101
+ ```
102
+
103
+ ## Why this works
104
+
105
+ The plain comparison becomes explicit with columns, and the execution path becomes
106
+ operational through a flow diagram. This helps teams decide with one view of
107
+ trade-offs and controls.
@@ -0,0 +1,77 @@
1
+ # Incident Response: API Latency Spike and 502s
2
+
3
+ ## Question
4
+
5
+ > My API started returning intermittent 502s and latency jumped from 120ms to
6
+ > 4s. I have Redis, API pods, and DB replicas. How should we triage and restore
7
+ > service in the next 20 minutes?
8
+
9
+ ## Without feynman
10
+
11
+ The API has a latency issue. First verify if the issue is traffic-related or a
12
+ deployment regression, then inspect pods, then check cache hit rates, and then
13
+ inspect database query times. If the pods are overloaded, scale horizontally and
14
+ enable degraded mode. If Redis errors increase, fail over to DB reads with a strict
15
+ timeout. If replicas are behind replication lag, move read traffic back to primary
16
+ only and notify users of elevated error rates.
17
+
18
+ ## With feynman
19
+
20
+ Triage and restore sequence:
21
+
22
+ ```
23
+ [Alert: 502 + latency spike] --> [1) Confirm incident scope]
24
+ |
25
+ +--> [Critical path only]
26
+ | |
27
+ | +--> [Disable non-core features]
28
+ | |
29
+ | +--> [Enable queue fallback]
30
+ |
31
+ +--> [2) Isolate root layer]
32
+ |
33
+ [Cache regression?]
34
+ |
35
+ yes -->| |--> no
36
+ v v
37
+ [Scale API / limit RPS] [Deployment rollback?]
38
+ | |
39
+ pass fail
40
+ | |
41
+ v v
42
+ [Service partially stable]
43
+ | |
44
+ v v
45
+ [Resolve] [Rollback last deploy]
46
+ |
47
+ v
48
+ [Restore response rate]
49
+ ```
50
+
51
+ Live status summary:
52
+
53
+ ```
54
+ ┌─ Incident Runbook ────────────────────┐
55
+ step-1 in-progress
56
+ step-2 pending
57
+ step-3 pending
58
+ external-comm on-call-channel
59
+ └───────────────────────────────────────┘
60
+ ```
61
+
62
+ Priority gates:
63
+
64
+ ```
65
+ ▲ high
66
+ API error budget burn
67
+ Security-sensitive write path (stop writes if needed)
68
+ ▼ low
69
+ Nice-to-have dashboards
70
+ Cosmetic alert updates
71
+ ```
72
+
73
+ ## Why this works
74
+
75
+ This example combines sequential flow (triage stages), branch isolation, status
76
+ frames, and priority scale. It turns ad-hoc firefighting into an explicit,
77
+ readable execution plan before any action is taken.
@@ -0,0 +1,73 @@
1
+ # Release Readiness: Monthly Deployment to Production
2
+
3
+ ## Question
4
+
5
+ > We have a monthly release with API, web, and mobile changes. What gates and
6
+ > rollback triggers do we need before and after deploy?
7
+
8
+ ## Without feynman
9
+
10
+ Coordinate release notes, run migration checks, execute smoke tests on staging, and
11
+ run targeted regression tests. If deployment passes, monitor key metrics for one hour
12
+ and then announce success. Rollback should be triggered on increased errors or
13
+ critical latency regressions.
14
+
15
+ ## With feynman
16
+
17
+ ```
18
+ [Pre-release plan] --> [Staging deploy]
19
+ |
20
+ v
21
+ [Smoke + regression]
22
+ |
23
+ pass ----+---- fail
24
+ | |
25
+ v v
26
+ [Production] [Stop + fix]
27
+ |
28
+ v
29
+ [Observe 60 min]
30
+ |
31
+ pass ----+---- fail
32
+ | |
33
+ v v
34
+ [Done + announce]
35
+ [Rollback + incident]
36
+ ```
37
+
38
+ Readiness gates:
39
+
40
+ ```
41
+ criterion | gate type | owner | must be true
42
+ --------------------|-----------|----------------|----------------------
43
+ migration safety | schema | platform | dry-run + backward-compatible
44
+ auth/checkout smoke | functional| product qa | 100% critical paths
45
+ error budget | reliability| sre | error rate <= baseline + 0.2%
46
+ security regression | security | infosec | zero new critical findings
47
+ ```
48
+
49
+ Readiness status:
50
+
51
+ - staging-deploy: done
52
+ - smoke-tests: in progress
53
+ - security-gate: pending
54
+ - rollback-drill: ready
55
+ - comms-draft: in progress
56
+
57
+ Priority ladder:
58
+
59
+ ```
60
+ ▲ high
61
+ no data loss
62
+ idempotent migrations
63
+ auth + checkout paths
64
+ ▼ low
65
+ branding tweaks
66
+ help-center wording
67
+ ```
68
+
69
+ ## Why this works
70
+
71
+ The flow diagram shows what blocks progression to production and where rollback
72
+ branches back. The gate table gives objective pass criteria. The status frame and
73
+ priority scale separate urgent reliability conditions from polish work.
@@ -0,0 +1,72 @@
1
+ # Service Migration: Move Billing to a New Provider
2
+
3
+ ## Question
4
+
5
+ > We need to migrate billing from Provider A to Provider B. What is the safest rollout
6
+ > sequence with fallback and acceptance gates?
7
+
8
+ ## Without feynman
9
+
10
+ Start in read mode to keep both providers enabled, compare charge outcomes for a
11
+ sample of users, run a pilot for low-risk plans, then run a staged migration to
12
+ full traffic with manual approval and rollback checkpoints.
13
+
14
+ ## With feynman
15
+
16
+ Migration ladder:
17
+
18
+ ```
19
+ [Dual-run mode]
20
+ |
21
+ v
22
+ [Replay 30 days of traffic] --> [Compare divergence < 1%] --> [Pilot 5% users]
23
+ |
24
+ v
25
+ [Auto-scaling smoke] --> [Pilot 30% users] --> [100% rollout]
26
+ |
27
+ +--> [Error rate > threshold] --> [Disable 30% wave]
28
+ |
29
+ +--> [Critical incidents] --> [Fallback to A]
30
+ ```
31
+
32
+ Vendor comparison:
33
+
34
+ ```
35
+ Option A (keep) | Option B (move) | Hybrid (dual)
36
+ ------------------|----------------------|------------------
37
+ steady risk | new integration risk | controlled transition
38
+ low complexity | higher validation | higher ops cost
39
+ higher cost risk | lower long-term cost | medium monitoring
40
+ fast execution | phased execution | slower initial step
41
+ ```
42
+
43
+ Go/no-go gates:
44
+
45
+ - divergence_rate <= 1%
46
+ - failed callbacks < 0.2%
47
+ - latency_p95 <= baseline + 20ms
48
+ - reconciliation queue no growth
49
+
50
+ Fallback playbook:
51
+
52
+ - trigger: payment failure spike
53
+ - action: switch traffic to A
54
+ - deadline: < 10 minutes
55
+ - owner: on-call + fintech lead
56
+
57
+ Execution priority:
58
+
59
+ ```
60
+ ▲ high
61
+ reconciliation accuracy
62
+ callback reliability
63
+ ▼ low
64
+ brand messaging tweaks
65
+ custom dashboard layout
66
+ ```
67
+
68
+ ## Why this works
69
+
70
+ В миграции ключевая ценность — показывать не просто шаги, а условия перехода между
71
+ ступенями. Поток делает это явно: переход к следующей стадии возможен только при
72
+ прохождении метрик и готовности fallback.
@@ -31,18 +31,25 @@ process.stdin.on('end', () => {
31
31
  if (sessionId && /[/\\]|\.\./.test(sessionId)) process.exit(0);
32
32
 
33
33
  // Step 2: flag file + first-run bootstrap (D-05, D-07, bug #35713)
34
- // True first run: neither flag nor state exists bootstrap both, then fall through
35
- // Intentionally disabled: flag absent but state exists exit 0 (user ran /feynman off)
34
+ // True first run: neither flag nor state exists -> bootstrap default full mode.
35
+ // Intentionally disabled: flag absent + state.enabled=false -> exit 0.
36
36
  const flagExists = fs.existsSync(FLAG_PATH);
37
37
  const stateExists = fs.existsSync(STATE_PATH);
38
38
  if (!flagExists) {
39
39
  if (!stateExists) {
40
- // First install bootstrap everything and activate
40
+ // First install: bootstrap and activate full mode.
41
41
  fs.mkdirSync(FEYNMAN_DIR, { recursive: true });
42
42
  fs.writeFileSync(STATE_PATH, JSON.stringify(DEFAULT_STATE, null, 2));
43
43
  fs.writeFileSync(FLAG_PATH, DEFAULT_STATE.intensity);
44
44
  } else {
45
- process.exit(0); // disabled intentionally by user
45
+ let existingState;
46
+ try {
47
+ existingState = { ...DEFAULT_STATE, ...JSON.parse(fs.readFileSync(STATE_PATH, 'utf8')) };
48
+ } catch (_) {
49
+ process.exit(0);
50
+ }
51
+ if (!existingState.enabled) process.exit(0);
52
+ fs.writeFileSync(FLAG_PATH, existingState.intensity || DEFAULT_STATE.intensity);
46
53
  }
47
54
  }
48
55
 
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env node
2
+ // feynman — SessionStart hook — injects active diagram rules at session start.
3
+ // UserPromptSubmit still reinforces rules every turn; this primes fresh sessions.
4
+ 'use strict';
5
+
6
+ const fs = require('fs');
7
+ const path = require('path');
8
+ const os = require('os');
9
+
10
+ const HOME = os.homedir();
11
+ const CLIENT_HOME = process.env.FEYNMAN_HOME || path.join(HOME, '.claude');
12
+ const FEYNMAN_DIR = path.join(CLIENT_HOME, '.feynman');
13
+ const STATE_PATH = path.join(FEYNMAN_DIR, 'state.json');
14
+ const FLAG_PATH = path.join(CLIENT_HOME, '.feynman-active');
15
+ const RULES_PATH = path.join(__dirname, '..', 'rules', 'feynman-activate.md');
16
+
17
+ const DEFAULT_STATE = { enabled: true, intensity: 'full', injections: 0 };
18
+ const VALID_INTENSITIES = ['lite', 'full', 'ultra'];
19
+
20
+ function readState() {
21
+ try {
22
+ return { ...DEFAULT_STATE, ...JSON.parse(fs.readFileSync(STATE_PATH, 'utf8')) };
23
+ } catch (_) {
24
+ return { ...DEFAULT_STATE };
25
+ }
26
+ }
27
+
28
+ function writeState(state) {
29
+ fs.mkdirSync(FEYNMAN_DIR, { recursive: true });
30
+ fs.writeFileSync(STATE_PATH, JSON.stringify(state, null, 2));
31
+ }
32
+
33
+ function readRules(intensity) {
34
+ const rulesContent = fs.readFileSync(RULES_PATH, 'utf8');
35
+ const selected = VALID_INTENSITIES.includes(intensity) ? intensity : 'full';
36
+ const openMarker = '<!-- ' + selected + ' -->';
37
+ const closeMarker = '<!-- /' + selected + ' -->';
38
+ const i1 = rulesContent.indexOf(openMarker);
39
+ const i2 = rulesContent.indexOf(closeMarker, i1);
40
+ if (i1 === -1 || i2 === -1) return '';
41
+ return rulesContent.slice(i1 + openMarker.length, i2).trim();
42
+ }
43
+
44
+ let input = '';
45
+ process.stdin.on('data', chunk => { input += chunk; });
46
+ process.stdin.on('end', () => {
47
+ try {
48
+ if (input.trim()) {
49
+ const data = JSON.parse(input);
50
+ const sessionId = data.session_id || '';
51
+ if (sessionId && /[/\\]|\.\./.test(sessionId)) process.exit(0);
52
+ }
53
+
54
+ const stateExists = fs.existsSync(STATE_PATH);
55
+ const flagExists = fs.existsSync(FLAG_PATH);
56
+ const state = readState();
57
+
58
+ if (!stateExists) {
59
+ writeState(state);
60
+ }
61
+
62
+ if (!state.enabled) {
63
+ try { fs.unlinkSync(FLAG_PATH); } catch (_) {}
64
+ process.exit(0);
65
+ }
66
+
67
+ if (!flagExists) {
68
+ fs.writeFileSync(FLAG_PATH, state.intensity || DEFAULT_STATE.intensity);
69
+ }
70
+
71
+ const rulesText = readRules(state.intensity);
72
+ if (!rulesText) process.exit(0);
73
+
74
+ // SessionStart accepts plain stdout as context, matching caveman's hook shape.
75
+ process.stdout.write(rulesText);
76
+ } catch (_) {
77
+ process.exit(0);
78
+ }
79
+ });
package/hooks/hooks.json CHANGED
@@ -1,14 +1,24 @@
1
1
  {
2
2
  "description": "Inject feynman ASCII diagram rules before each Claude Code prompt.",
3
3
  "hooks": {
4
+ "SessionStart": [
5
+ {
6
+ "hooks": [
7
+ {
8
+ "type": "command",
9
+ "command": "FEYNMAN_HOME=\"$HOME/.claude\" node \"${CLAUDE_PLUGIN_ROOT}/hooks/feynman-session-start.js\"",
10
+ "timeout": 5
11
+ }
12
+ ]
13
+ }
14
+ ],
4
15
  "UserPromptSubmit": [
5
16
  {
6
17
  "hooks": [
7
18
  {
8
19
  "type": "command",
9
20
  "command": "FEYNMAN_HOME=\"$HOME/.claude\" node \"${CLAUDE_PLUGIN_ROOT}/hooks/feynman-activate.js\"",
10
- "timeout": 5,
11
- "statusMessage": "Injecting diagram rules..."
21
+ "timeout": 5
12
22
  }
13
23
  ]
14
24
  }
package/hooks.json CHANGED
@@ -1,13 +1,24 @@
1
1
  {
2
2
  "hooks": {
3
+ "SessionStart": [
4
+ {
5
+ "matcher": "startup|resume",
6
+ "hooks": [
7
+ {
8
+ "type": "command",
9
+ "command": "PLUGIN_ROOT=\"${PLUGIN_ROOT:-$CLAUDE_PLUGIN_ROOT}\"; FEYNMAN_HOME=\"$HOME/.codex\" node \"$PLUGIN_ROOT/hooks/feynman-session-start.js\"",
10
+ "timeout": 5
11
+ }
12
+ ]
13
+ }
14
+ ],
3
15
  "UserPromptSubmit": [
4
16
  {
5
17
  "hooks": [
6
18
  {
7
19
  "type": "command",
8
20
  "command": "PLUGIN_ROOT=\"${PLUGIN_ROOT:-$CLAUDE_PLUGIN_ROOT}\"; FEYNMAN_HOME=\"$HOME/.codex\" node \"$PLUGIN_ROOT/hooks/feynman-activate.js\"",
9
- "timeout": 5,
10
- "statusMessage": "Injecting diagram rules..."
21
+ "timeout": 5
11
22
  }
12
23
  ]
13
24
  }