Repository navigation
Expand file tree
/
Copy pathdiff.py
More file actions
195 lines (151 loc) · 6.73 KB
/
Copy pathdiff.py
File metadata and controls
195 lines (151 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""
Diff Mode
---------
Compares two competitive briefs for the same product — typically
across different time periods — and surfaces what changed strategically.
This is for tracking competitor momentum over time. The output isn't
a line-by-line diff; it's a structured change analysis that answers
the questions a PM actually cares about:
- Did their threat level change?
- What new features or moves appeared?
- Did pricing shift?
- Are they moving closer to your ICP?
- Did anything de-escalate?
Usage:
python diff.py --before briefs/notion-ai-jan.md --after briefs/notion-ai-apr.md
python diff.py --before briefs/notion-ai-jan.md --after briefs/notion-ai-apr.md --target "Notion AI"
python diff.py --before briefs/notion-ai-jan.md --after briefs/notion-ai-apr.md --output diff.md
"""
import anthropic
import argparse
import sys
from datetime import datetime
from pathlib import Path
MODEL = "claude-sonnet-4-20250514"
MAX_TOKENS = 2048
# ── Diff Prompt ───────────────────────────────────────────────────────────────
DIFF_SYSTEM = """You are a senior competitive intelligence analyst.
You will be given two competitive briefs for the same product, written at different points in time.
Your job is to produce a structured strategic change analysis — not a line-by-line diff.
Focus on what matters to a Product Manager making roadmap and positioning decisions.
Structure your output as clean Markdown with these sections:
## Threat Trajectory
Did the competitor become more or less dangerous? Why? Lead with a clear verdict:
↑ Escalating / → Stable / ↓ De-escalating
## What's New
Specific new features, product launches, pricing changes, partnerships, or strategic moves
that appear in the newer brief but not the older one.
## What Changed
Things that existed before but have materially shifted — pricing, positioning, ICP,
AI capabilities, enterprise readiness, etc.
## What Disappeared
Things present in the older brief that are notably absent in the newer one.
(Could signal pivots, failures, or de-prioritization.)
## ICP Drift
Is the competitor moving toward or away from your target customer? Be specific.
## AI Maturity Delta
How has their AI integration changed between the two briefs?
## Strategic Implications
What should a competing PM do differently based on these changes?
Concrete, actionable, no hedging.
## Confidence
Flag anything where the difference might be due to research quality rather than
real product changes. Rate overall confidence: High / Medium / Low.
Be direct. A PM reading this brief doesn't have time for caveats on every sentence.
Flag uncertainty at the end, not throughout."""
# ── Core Diff Function ────────────────────────────────────────────────────────
def diff_briefs(
target: str,
before_text: str,
after_text: str,
before_label: str = "Earlier brief",
after_label: str = "Later brief",
verbose: bool = True,
) -> str:
"""
Runs a strategic diff between two competitive briefs.
Returns a structured Markdown change analysis.
"""
client = anthropic.Anthropic()
if verbose:
print(f"\n🔄 Diffing briefs for: {target}")
print(f" Before: {before_label}")
print(f" After: {after_label}\n")
response = client.messages.create(
model=MODEL,
max_tokens=MAX_TOKENS,
system=DIFF_SYSTEM,
messages=[
{
"role": "user",
"content": (
f"Target product: {target}\n\n"
f"---\n\n"
f"## {before_label}\n\n"
f"{before_text}\n\n"
f"---\n\n"
f"## {after_label}\n\n"
f"{after_text}"
),
}
],
)
return response.content[0].text
def format_diff(target: str, diff: str, before_label: str, after_label: str) -> str:
"""Wraps the diff with header and metadata."""
return f"""# Competitive Change Analysis: {target}
> **Generated by PM Research Agent** | {datetime.now().strftime("%B %d, %Y")}
> *Comparing: "{before_label}" → "{after_label}"*
> *This is a strategic diff, not a line-by-line comparison.*
---
{diff}
---
*Generated by [PM Research Agent](https://github.com/dvmukul/pm-research-agent)*
"""
# ── CLI ───────────────────────────────────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(
description="Diff two competitive briefs to surface strategic changes over time"
)
parser.add_argument("--before", required=True, help="Path to the earlier brief .md file")
parser.add_argument("--after", required=True, help="Path to the more recent brief .md file")
parser.add_argument("--target", default=None, help="Competitor name (inferred from filename if omitted)")
parser.add_argument("--output", default=None, help="Save diff to a markdown file")
parser.add_argument("--quiet", action="store_true", help="Suppress progress output")
args = parser.parse_args()
before_path = Path(args.before)
after_path = Path(args.after)
for p in [before_path, after_path]:
if not p.exists():
print(f"❌ File not found: {p}")
sys.exit(1)
# Infer target from the "after" filename if not provided
target = args.target or after_path.stem.replace("-", " ").replace("_", " ").title()
# Use filenames as labels (strip path and extension)
before_label = before_path.stem
after_label = after_path.stem
before_text = before_path.read_text(encoding="utf-8")
after_text = after_path.read_text(encoding="utf-8")
try:
raw_diff = diff_briefs(
target=target,
before_text=before_text,
after_text=after_text,
before_label=before_label,
after_label=after_label,
verbose=not args.quiet,
)
except anthropic.AuthenticationError:
print("\n❌ Error: ANTHROPIC_API_KEY is missing or invalid.")
sys.exit(1)
except Exception as e:
print(f"\n❌ Unexpected error: {e}")
sys.exit(1)
final_diff = format_diff(target, raw_diff, before_label, after_label)
print(final_diff)
if args.output:
output_path = Path(args.output)
output_path.write_text(final_diff, encoding="utf-8")
print(f"\n💾 Diff saved to: {output_path.resolve()}\n")
if __name__ == "__main__":
main()