norn-cli 2.5.0 → 2.6.1

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.
package/CHANGELOG.md CHANGED
@@ -2,14 +2,22 @@
2
2
 
3
3
  All notable changes to the "Norn" extension will be documented in this file.
4
4
 
5
- ## [2.5.0] - 2026-05-27
5
+ ## [2.6.1] - 2026-05-30
6
+
7
+ ### Changed
8
+ - **VS Code webview theme polish** — response, coverage, and Postman panels now use theme-aware colors for JSON, cards, tabs, request details, and inline assertion values so light and high-contrast themes render more consistently.
9
+ - **Response panel layout polish** — long assertion expected/actual values now wrap inside their panels, and request detail/tabs spacing was tightened to reduce visual crowding.
10
+
11
+ ## [2.6.0] - 2026-05-28
6
12
 
7
13
  ### Added
8
14
  - **Verbatim string literals** — added C#-style `@"..."` strings for Norn values, assertions, regex patterns, request URLs, sequence arguments/defaults, SQL arguments, schema paths, assertion messages, and `@data` values. Backslashes are literal, doubled quotes decode to a single quote, and `{{...}}` tokens inside verbatim strings are not interpolated.
9
15
  - **Escape-aware highlighting** — `.norn` syntax highlighting now distinguishes decoded escapes from ordinary backslash text, including escaped quotes/backslashes in normal strings, regex escapes in `matches` patterns, doubled quotes in verbatim strings, and valid JSON escapes in request bodies.
16
+ - **Configurable HTTP request timeouts** — added a shared `http.timeoutMs` option in `norn.config.json`, a VS Code `norn.request.timeoutMs` setting for local overrides, and CLI `--timeout` duration parsing for values such as `180s`, `3m`, and `300000ms`.
10
17
 
11
18
  ### Changed
12
19
  - **Regex assertion authoring** — `matches` patterns are now guided toward quoted string patterns, with `@"..."` recommended for regexes that contain many backslashes or literal quotes.
20
+ - **Starter config output** — generated `norn.config.json` files now stay minimal and rely on schema-backed IntelliSense instead of inline `_comment` guidance fields.
13
21
 
14
22
  ### Fixed
15
23
  - **Quoted string handling** — escaped quotes and backslashes are decoded consistently across variables, request URLs, run arguments, sequence defaults, SQL arguments, `@data` values, `matchesSchema` paths, and assertion messages while preserving unknown escapes such as `\A` as literal text.
package/dist/cli.js CHANGED
@@ -101922,13 +101922,17 @@ function findVerbatimStringEnd(text, start) {
101922
101922
  i += 2;
101923
101923
  continue;
101924
101924
  }
101925
- if (char === '"') {
101925
+ if (char === '"' && isLikelyVerbatimStringTerminator(text, i)) {
101926
101926
  return i + 1;
101927
101927
  }
101928
101928
  i++;
101929
101929
  }
101930
101930
  return text.length;
101931
101931
  }
101932
+ function isLikelyVerbatimStringTerminator(text, quoteIndex) {
101933
+ const remainder = text.substring(quoteIndex + 1).trimStart();
101934
+ return remainder === "" || remainder.startsWith("|") || remainder.startsWith("#") || remainder.startsWith(",") || remainder.startsWith(")") || remainder.startsWith("]") || remainder.startsWith("}");
101935
+ }
101932
101936
  function decodeVerbatimStringLiteral(literal2) {
101933
101937
  const inner = literal2.slice(2, -1);
101934
101938
  let decoded = "";
@@ -108834,12 +108838,46 @@ function isNornError(error2) {
108834
108838
 
108835
108839
  // src/httpRuntimeOptions.ts
108836
108840
  var verifyTlsCertificates = true;
108841
+ var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
108842
+ var requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS;
108837
108843
  function setVerifyTlsCertificates(enabled) {
108838
108844
  verifyTlsCertificates = enabled;
108839
108845
  }
108840
108846
  function getVerifyTlsCertificates() {
108841
108847
  return verifyTlsCertificates;
108842
108848
  }
108849
+ function setRequestTimeoutMs(timeoutMs) {
108850
+ if (timeoutMs === void 0) {
108851
+ requestTimeoutMs = DEFAULT_REQUEST_TIMEOUT_MS;
108852
+ return;
108853
+ }
108854
+ if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
108855
+ throw new Error(`Request timeout must be greater than 0 ms.`);
108856
+ }
108857
+ requestTimeoutMs = Math.ceil(timeoutMs);
108858
+ }
108859
+ function getRequestTimeoutMs() {
108860
+ return requestTimeoutMs;
108861
+ }
108862
+ function parseDurationToMs(rawValue, defaultUnit = "ms") {
108863
+ const trimmed = rawValue.trim().toLowerCase();
108864
+ const match = trimmed.match(/^(\d+(?:\.\d+)?)\s*(ms|milliseconds?|s|sec|secs|seconds?|m|min|mins|minutes?)?$/);
108865
+ if (!match) {
108866
+ return void 0;
108867
+ }
108868
+ const amount = Number(match[1]);
108869
+ if (!Number.isFinite(amount) || amount <= 0) {
108870
+ return void 0;
108871
+ }
108872
+ const unit = match[2] ?? defaultUnit;
108873
+ if (unit === "ms" || unit === "millisecond" || unit === "milliseconds") {
108874
+ return Math.ceil(amount);
108875
+ }
108876
+ if (unit === "m" || unit === "min" || unit === "mins" || unit === "minute" || unit === "minutes") {
108877
+ return Math.ceil(amount * 6e4);
108878
+ }
108879
+ return Math.ceil(amount * 1e3);
108880
+ }
108843
108881
 
108844
108882
  // src/formUrlEncoded.ts
108845
108883
  function parseEqualsField(segment) {
@@ -109030,7 +109068,7 @@ async function sendRequestWithJar(request, jar, retryOptions) {
109030
109068
  adapter: "http",
109031
109069
  headers,
109032
109070
  data,
109033
- timeout: 3e4,
109071
+ timeout: getRequestTimeoutMs(),
109034
109072
  maxRedirects: 0,
109035
109073
  validateStatus: () => true,
109036
109074
  httpsAgent: getHttpsAgent()
@@ -109822,14 +109860,14 @@ function parseAssertContent(content, message, messageIsVerbatim) {
109822
109860
  { pattern: /^(.+?)\s*<=\s*(.+)$/, op: "<=" },
109823
109861
  { pattern: /^(.+?)\s*==\s*(.+)$/, op: "==" },
109824
109862
  { pattern: /^(.+?)\s*!=\s*(.+)$/, op: "!=" },
109825
- { pattern: /^(.+?)\s*>\s*(.+)$/, op: ">" },
109826
- { pattern: /^(.+?)\s*<\s*(.+)$/, op: "<" },
109827
109863
  { pattern: /^(.+?)\s+contains\s+(.+)$/i, op: "contains" },
109828
109864
  { pattern: /^(.+?)\s+startsWith\s+(.+)$/i, op: "startsWith" },
109829
109865
  { pattern: /^(.+?)\s+endsWith\s+(.+)$/i, op: "endsWith" },
109830
- { pattern: /^(.+?)\s+matches\s+(.+)$/i, op: "matches" },
109831
109866
  { pattern: /^(.+?)\s+matchesSchema\s+(.+)$/i, op: "matchesSchema" },
109832
- { pattern: /^(.+?)\s+isType\s+(.+)$/i, op: "isType" }
109867
+ { pattern: /^(.+?)\s+matches\s+(.+)$/i, op: "matches" },
109868
+ { pattern: /^(.+?)\s+isType\s+(.+)$/i, op: "isType" },
109869
+ { pattern: /^(.+?)\s*>\s*(.+)$/, op: ">" },
109870
+ { pattern: /^(.+?)\s*<\s*(.+)$/, op: "<" }
109833
109871
  ];
109834
109872
  for (const { pattern, op } of binaryOperators) {
109835
109873
  const binaryMatch = content.match(pattern);
@@ -109852,9 +109890,14 @@ function findUnquotedPipe(str) {
109852
109890
  let inVerbatimString = false;
109853
109891
  for (let i = 0; i < str.length; i++) {
109854
109892
  const char = str[i];
109855
- if (inQuote && inVerbatimString && char === '"' && str[i + 1] === '"') {
109856
- i++;
109857
- continue;
109893
+ if (inQuote && inVerbatimString && char === '"') {
109894
+ if (str[i + 1] === '"') {
109895
+ i++;
109896
+ continue;
109897
+ }
109898
+ if (!isVerbatimAssertionStringTerminator(str, i)) {
109899
+ continue;
109900
+ }
109858
109901
  }
109859
109902
  if (escapeNext) {
109860
109903
  escapeNext = false;
@@ -109878,6 +109921,10 @@ function findUnquotedPipe(str) {
109878
109921
  }
109879
109922
  return -1;
109880
109923
  }
109924
+ function isVerbatimAssertionStringTerminator(str, quoteIndex) {
109925
+ const remainder = str.substring(quoteIndex + 1).trimStart();
109926
+ return remainder === "" || remainder.startsWith("|");
109927
+ }
109881
109928
  function resolveValue(expr, responses, variables, getValueByPath2, responseIndexToVariable) {
109882
109929
  const trimmed = expr.trim();
109883
109930
  const wrappedResponseRefMatch = trimmed.match(/^\{\{(\$\d+(?:\..+)?)\}\}$/);
@@ -110652,6 +110699,18 @@ function isStringRecord(value) {
110652
110699
  function isKnownSection(value) {
110653
110700
  return value === void 0 || isObjectRecord(value);
110654
110701
  }
110702
+ function isNornHttpConfig(value) {
110703
+ if (value === void 0) {
110704
+ return true;
110705
+ }
110706
+ if (!isObjectRecord(value)) {
110707
+ return false;
110708
+ }
110709
+ if (value._comment !== void 0 && typeof value._comment !== "string" && (!Array.isArray(value._comment) || !value._comment.every((item) => typeof item === "string"))) {
110710
+ return false;
110711
+ }
110712
+ return value.timeoutMs === void 0 || typeof value.timeoutMs === "number" && Number.isFinite(value.timeoutMs) && value.timeoutMs > 0;
110713
+ }
110655
110714
  function isNornProjectConfig(value) {
110656
110715
  if (!isObjectRecord(value)) {
110657
110716
  return false;
@@ -110659,7 +110718,7 @@ function isNornProjectConfig(value) {
110659
110718
  if (value.version !== 1) {
110660
110719
  return false;
110661
110720
  }
110662
- return isKnownSection(value.sql) && isKnownSection(value.mcp);
110721
+ return isNornHttpConfig(value.http) && isKnownSection(value.sql) && isKnownSection(value.mcp);
110663
110722
  }
110664
110723
  function loadNornConfig(startPath) {
110665
110724
  const filePath = findNearestConfigFile(startPath, NORN_CONFIG_FILENAME);
@@ -134198,6 +134257,32 @@ function applyRegionRefactorToText(text, pattern) {
134198
134257
  ` : result;
134199
134258
  }
134200
134259
 
134260
+ // src/requestTimeoutConfig.ts
134261
+ function getProjectRequestTimeoutMs(startPath) {
134262
+ if (!findNearestConfigFile(startPath, NORN_CONFIG_FILENAME)) {
134263
+ return void 0;
134264
+ }
134265
+ const { config: config2 } = loadNornConfig(startPath);
134266
+ return config2.http?.timeoutMs;
134267
+ }
134268
+ function resolveRequestTimeoutMs(startPath, overrideMs) {
134269
+ if (overrideMs !== void 0) {
134270
+ return overrideMs;
134271
+ }
134272
+ if (startPath) {
134273
+ const projectTimeoutMs = getProjectRequestTimeoutMs(startPath);
134274
+ if (projectTimeoutMs !== void 0) {
134275
+ return projectTimeoutMs;
134276
+ }
134277
+ }
134278
+ return DEFAULT_REQUEST_TIMEOUT_MS;
134279
+ }
134280
+ function applyRequestTimeoutForPath(startPath, overrideMs) {
134281
+ const timeoutMs = resolveRequestTimeoutMs(startPath, overrideMs);
134282
+ setRequestTimeoutMs(timeoutMs);
134283
+ return timeoutMs;
134284
+ }
134285
+
134201
134286
  // src/cli.ts
134202
134287
  function handleImportResolutionErrors(errors, colors) {
134203
134288
  const { blockingErrors, warningErrors } = splitImportResolutionErrors(errors);
@@ -134286,6 +134371,14 @@ function buildCliEnvironmentValidationContext(resolvedEnv, selectedEnv) {
134286
134371
  availableEnvironments: resolvedEnv.availableEnvironments
134287
134372
  };
134288
134373
  }
134374
+ function applyCliRequestTimeout(filePath, options, colors) {
134375
+ try {
134376
+ applyRequestTimeoutForPath(filePath, options.timeoutMs);
134377
+ } catch (error2) {
134378
+ console.error(colors.error(`Invalid request timeout configuration: ${error2 instanceof Error ? error2.message : String(error2)}`));
134379
+ process.exit(1);
134380
+ }
134381
+ }
134289
134382
  function generateTimestamp() {
134290
134383
  const now = /* @__PURE__ */ new Date();
134291
134384
  const year = now.getFullYear();
@@ -134329,7 +134422,17 @@ function parseArgs(args) {
134329
134422
  } else if (arg === "--env" || arg === "-e") {
134330
134423
  options.env = args[++i];
134331
134424
  } else if (arg === "--timeout" || arg === "-t") {
134332
- options.timeout = parseInt(args[++i], 10) * 1e3;
134425
+ const rawTimeout = args[++i];
134426
+ if (!rawTimeout) {
134427
+ console.error("Error: --timeout requires a value, e.g. 180s, 3m, or 300000ms.");
134428
+ process.exit(1);
134429
+ }
134430
+ const timeoutMs = parseDurationToMs(rawTimeout, "s");
134431
+ if (timeoutMs === void 0) {
134432
+ console.error(`Error: Invalid timeout '${rawTimeout}'. Use values like 180s, 3m, or 300000ms.`);
134433
+ process.exit(1);
134434
+ }
134435
+ options.timeoutMs = timeoutMs;
134333
134436
  } else if (arg === "--insecure") {
134334
134437
  options.insecure = true;
134335
134438
  } else if (arg === "--refactor-region-pattern" || arg === "--refactor-nornenv-region-pattern") {
@@ -134380,7 +134483,7 @@ Options:
134380
134483
  -s, --sequence <name> Run a specific sequence by name (single file only)
134381
134484
  -r, --request <name> Run a specific named request (single file only)
134382
134485
  -e, --env <name> Use environment from .nornenv (e.g., dev, prod)
134383
- -t, --timeout <sec> Request timeout in seconds (default: no timeout)
134486
+ -t, --timeout <time> Request timeout override (e.g. 180s, 3m, 300000ms; default: norn.config.json or 30s)
134384
134487
  --insecure Disable TLS certificate verification (dev/self-signed only)
134385
134488
  -j, --json Output results as JSON (for CI/CD)
134386
134489
  -v, --verbose Show detailed output (headers, request/response bodies)
@@ -134705,6 +134808,7 @@ async function main() {
134705
134808
  }
134706
134809
  if (options.sequence || options.request) {
134707
134810
  const filePath = filesToRun[0];
134811
+ applyCliRequestTimeout(filePath, options, colors);
134708
134812
  const secretUnlockResult = await ensureCliSecretsUnlocked(filePath);
134709
134813
  if (!secretUnlockResult.ok) {
134710
134814
  if (secretUnlockResult.errors.length > 0) {
@@ -134909,6 +135013,7 @@ ${fileContent}` : fileContent;
134909
135013
  console.log("");
134910
135014
  }
134911
135015
  for (const filePath of filesToRun) {
135016
+ applyCliRequestTimeout(filePath, options, colors);
134912
135017
  const secretUnlockResult = await ensureCliSecretsUnlocked(filePath);
134913
135018
  if (!secretUnlockResult.ok) {
134914
135019
  if (secretUnlockResult.errors.length > 0) {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "norn-cli",
3
3
  "displayName": "Norn — API Tests in Your Repo",
4
4
  "description": "Version-controlled API tests your team can keep. Author and debug HTTP sequences in VS Code, then run the same files in CI.",
5
- "version": "2.5.0",
5
+ "version": "2.6.1",
6
6
  "publisher": "Norn-PeterKrustanov",
7
7
  "author": {
8
8
  "name": "Peter Krastanov"
@@ -291,7 +291,8 @@
291
291
  {
292
292
  "fileMatch": [
293
293
  "norn.config.json",
294
- "/norn.config.json"
294
+ "/norn.config.json",
295
+ "**/norn.config.json"
295
296
  ],
296
297
  "url": "./schemas/norn.config.schema.json"
297
298
  }
@@ -416,6 +417,15 @@
416
417
  "default": true,
417
418
  "description": "Verify SSL/TLS certificates for HTTPS requests and Swagger/OpenAPI fetches. Disable only for local development with self-signed certificates."
418
419
  },
420
+ "norn.request.timeoutMs": {
421
+ "type": [
422
+ "number",
423
+ "null"
424
+ ],
425
+ "default": null,
426
+ "minimum": 1,
427
+ "description": "Override the HTTP request timeout in milliseconds for this VS Code workspace or user. Set to null to use norn.config.json http.timeoutMs, then the built-in 30000ms default."
428
+ },
419
429
  "norn.testExplorer.exclude": {
420
430
  "type": "array",
421
431
  "default": [],
@@ -8,6 +8,16 @@
8
8
  "version"
9
9
  ],
10
10
  "defaultSnippets": [
11
+ {
12
+ "label": "Norn config with HTTP timeout",
13
+ "description": "Create a Norn config with a shared HTTP request timeout.",
14
+ "body": {
15
+ "version": 1,
16
+ "http": {
17
+ "timeoutMs": 180000
18
+ }
19
+ }
20
+ },
11
21
  {
12
22
  "label": "Norn config with SQL",
13
23
  "description": "Create a Norn config with an editable SQL connection and custom adapter example.",
@@ -68,6 +78,22 @@
68
78
  "const": 1,
69
79
  "description": "Config schema version."
70
80
  },
81
+ "http": {
82
+ "allOf": [
83
+ {
84
+ "$ref": "#/definitions/httpSection"
85
+ }
86
+ ],
87
+ "defaultSnippets": [
88
+ {
89
+ "label": "HTTP section definition",
90
+ "description": "Insert shared HTTP request options.",
91
+ "body": {
92
+ "timeoutMs": 180000
93
+ }
94
+ }
95
+ ]
96
+ },
71
97
  "sql": {
72
98
  "allOf": [
73
99
  {
@@ -144,6 +170,22 @@
144
170
  }
145
171
  ]
146
172
  },
173
+ "httpSection": {
174
+ "type": "object",
175
+ "additionalProperties": false,
176
+ "description": "HTTP request defaults used by .norn files.",
177
+ "properties": {
178
+ "_comment": {
179
+ "$ref": "#/definitions/comment",
180
+ "description": "Optional human-readable guidance ignored by Norn at runtime."
181
+ },
182
+ "timeoutMs": {
183
+ "type": "number",
184
+ "exclusiveMinimum": 0,
185
+ "description": "Default HTTP request timeout in milliseconds."
186
+ }
187
+ }
188
+ },
147
189
  "sqlSection": {
148
190
  "type": "object",
149
191
  "additionalProperties": false,
@@ -313,4 +355,4 @@
313
355
  }
314
356
  }
315
357
  }
316
- }
358
+ }
@@ -0,0 +1,490 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Reddit signal miner for the Norn LinkedIn campaign.
4
+
5
+ Anthropic's crawler is blocked by Reddit, so Claude can't fetch it — but this
6
+ script runs from YOUR machine/IP against Reddit's public JSON, so it can.
7
+
8
+ What it does:
9
+ - searches the campaign's target subreddits for the campaign's pain-terms
10
+ - pulls matching posts AND their top comments (the Post-5 gold was a comment)
11
+ - scores every bit of text by "juiciness" (spine-weighted keyword hits)
12
+ - isolates the exact verbatim sentences that carry the pain
13
+ - writes a ranked, paste-ready digest you can triage into Docs/market_signals.md
14
+
15
+ Discipline (from the campaign skill): quote VERBATIM + source URL + date.
16
+ Mining must not displace posting. Log it, pick one, go draft.
17
+
18
+ Where things live:
19
+ - This script's output (`Docs/reddit_signal_harvest.md`) is GITIGNORED SCRATCH —
20
+ overwritten by every run, never the source of truth.
21
+ - Permanent harvest archives live in `Docs/harvests/YYYY-MM-DD-*.md` (committed).
22
+ Copy a useful run there before the next one blows the scratch away.
23
+ - Curated wins (the actual market signals) live in `Docs/market_signals.md` as
24
+ numbered Signal entries — check those first to avoid re-mining the same ground.
25
+
26
+ Usage:
27
+ python3 scripts/reddit_signal_miner.py
28
+ python3 scripts/reddit_signal_miner.py --time year --limit 25 --top-threads 15
29
+ python3 scripts/reddit_signal_miner.py --subs QualityAssurance devops --out harvest.md
30
+
31
+ Set REDDIT_USERNAME below to your handle (Reddit asks for it in the User-Agent).
32
+ Uses the stdlib + certifi (already installed) for TLS; no other packages needed.
33
+
34
+ Troubleshooting:
35
+ - 403 on every request: Reddit is blocking your egress IP. Datacenter / VPN /
36
+ cloud IPs are refused wholesale — run it from a normal home connection, VPN off.
37
+ (This is why Claude can't run it for you: its sandbox IP is in a blocked range.)
38
+ - 429: you're going too fast — raise REQUEST_PAUSE.
39
+ - still 403 from home: anonymous JSON has gotten flaky; create a Reddit "script"
40
+ app and switch to the OAuth endpoint. Ask Claude to add that path if you need it.
41
+ """
42
+
43
+ import argparse
44
+ import html
45
+ import json
46
+ import re
47
+ import ssl
48
+ import sys
49
+ import time
50
+ import urllib.error
51
+ import urllib.parse
52
+ import urllib.request
53
+ from datetime import datetime, timezone
54
+
55
+ # python.org builds on macOS don't trust the system keychain; use certifi's
56
+ # bundle if present, otherwise fall back to the interpreter default.
57
+ try:
58
+ import certifi
59
+ SSL_CONTEXT = ssl.create_default_context(cafile=certifi.where())
60
+ except ImportError:
61
+ SSL_CONTEXT = ssl.create_default_context()
62
+
63
+ # --- config you can tweak --------------------------------------------------
64
+
65
+ REDDIT_USERNAME = "your_reddit_handle" # <- put your handle here (UA courtesy)
66
+
67
+ SUBREDDITS = [
68
+ "QualityAssurance",
69
+ "devops",
70
+ "ExperiencedDevs",
71
+ "webdev",
72
+ "programming",
73
+ "softwaretesting",
74
+ "QualityAssurance",
75
+ ]
76
+
77
+ QUERIES = [
78
+ "leaving Postman",
79
+ "Postman alternative",
80
+ ".http files",
81
+ "API testing",
82
+ "contract testing",
83
+ "flaky API tests",
84
+ "schema drift",
85
+ "tests passed but broke",
86
+ "staging didn't catch",
87
+ "lost my collection",
88
+ "tests out of date",
89
+ "nobody runs the tests",
90
+ "integration tests green",
91
+ "200 OK error body",
92
+ ]
93
+
94
+ # spine-weighted scoring. higher weight = closer to "tests on loan / rot / drift".
95
+ PAIN_PHRASES = {
96
+ # the ownership / loss wound (Post 5 territory) — heaviest
97
+ "lost years": 6, "lost my": 4, "lost all": 4, "evaporat": 5, "gone forever": 5,
98
+ "disappeared": 4, "wiped": 4, "no backup": 4, "couldn't recover": 4,
99
+ # rot / drift / staleness — the core thesis
100
+ "out of date": 5, "outdated": 4, "rot": 5, "stale": 5, "drift": 6,
101
+ "nobody updates": 6, "nobody maintains": 6, "nobody runs": 6, "never updated": 5,
102
+ "haven't touched": 4, "bit rot": 5, "abandoned": 4,
103
+ # passed-but-broke / staging-vs-prod
104
+ "passed but": 6, "tests passed": 5, "green but": 6, "didn't catch": 6,
105
+ "broke prod": 6, "broke in prod": 6, "worked on staging": 5, "works on staging": 5,
106
+ "only in production": 5, "false sense": 5, "lying": 5, "lied": 4,
107
+ # location / ownership / lock-in / friction
108
+ "source of truth": 5, "two sources": 5, "on someone": 4, "their laptop": 5,
109
+ "their account": 5, "behind a login": 5, "sign in": 3, "login wall": 5,
110
+ "paywall": 4, "enshittif": 5, "vendor lock": 5, "cloud sync": 4,
111
+ "fans": 3, "slow to open": 4, "bloat": 4, "enterprise monster": 5,
112
+ # 200-OK genre
113
+ "200 ok": 4, "success: false": 5, "status code": 2,
114
+ }
115
+
116
+ # leading word-boundary match: "rot" hits "rotten" but not "protocols"; stems
117
+ # like "evaporat"/"enshittif" still catch their variants.
118
+ _PAIN_PATTERNS = [(re.compile(r"\b" + re.escape(k)), k, w)
119
+ for k, w in PAIN_PHRASES.items()]
120
+
121
+ # a thread must actually be about APIs/testing to count — kills off-domain noise
122
+ # (a "schema drift" hit on Terraform, a "lost my collection" hit on WordPress).
123
+ DOMAIN_TERMS = [
124
+ "api", "endpoint", "postman", "graphql", "rest client", "request",
125
+ "response", "contract test", "mock", "openapi", "swagger", "insomnia",
126
+ "bruno", ".http", "integration test", "test suite", "payload", "qa ",
127
+ "automation", "regression",
128
+ ]
129
+ _DOMAIN_PATTERNS = [re.compile(r"\b" + re.escape(t)) for t in DOMAIN_TERMS]
130
+
131
+ REQUEST_PAUSE = 2.0 # seconds between requests (be polite)
132
+ COMMENT_FETCH_PAUSE = 2.0
133
+ MAX_RETRIES = 4
134
+
135
+ # --- http ------------------------------------------------------------------
136
+
137
+
138
+ def _ua() -> str:
139
+ return f"python:norn-signal-miner:1.0 (by /u/{REDDIT_USERNAME})"
140
+
141
+
142
+ def fetch_json(url: str) -> dict:
143
+ backoff = 3.0
144
+ for attempt in range(1, MAX_RETRIES + 1):
145
+ req = urllib.request.Request(url, headers={
146
+ "User-Agent": _ua(),
147
+ "Accept": "application/json",
148
+ "Accept-Language": "en-GB,en;q=0.9",
149
+ })
150
+ try:
151
+ with urllib.request.urlopen(req, timeout=30, context=SSL_CONTEXT) as resp:
152
+ return json.loads(resp.read().decode("utf-8"))
153
+ except urllib.error.HTTPError as e:
154
+ if e.code in (429, 500, 502, 503) and attempt < MAX_RETRIES:
155
+ wait = backoff * attempt
156
+ print(f" [{e.code}] backing off {wait:.0f}s "
157
+ f"(attempt {attempt}/{MAX_RETRIES})", file=sys.stderr)
158
+ time.sleep(wait)
159
+ continue
160
+ print(f" [http {e.code}] {url}", file=sys.stderr)
161
+ return {}
162
+ except (urllib.error.URLError, TimeoutError, json.JSONDecodeError) as e:
163
+ print(f" [err] {e} :: {url}", file=sys.stderr)
164
+ if attempt < MAX_RETRIES:
165
+ time.sleep(backoff * attempt)
166
+ continue
167
+ return {}
168
+ return {}
169
+
170
+
171
+ # pluggable fetcher: defaults to urllib, swapped to the browser in --browser mode
172
+ _FETCH = fetch_json
173
+
174
+
175
+ def get(url: str) -> dict:
176
+ return _FETCH(url)
177
+
178
+
179
+ class BrowserSession:
180
+ """Drives real Chrome so Reddit's anti-bot WAF serves us like a human.
181
+
182
+ The key move is the homepage warmup: landing on reddit.com first banks the
183
+ session cookie that the WAF then accepts on the .json endpoints. Without it
184
+ every request is a 'blocked by network security' 403.
185
+ """
186
+
187
+ def __init__(self, headless: bool = False):
188
+ from playwright.sync_api import sync_playwright
189
+ self._pw = sync_playwright().start()
190
+ self.browser = self._pw.chromium.launch(
191
+ channel="chrome", headless=headless,
192
+ args=["--disable-blink-features=AutomationControlled"])
193
+ self.ctx = self.browser.new_context(
194
+ locale="en-GB", timezone_id="Europe/London",
195
+ viewport={"width": 1280, "height": 900})
196
+ self.ctx.add_init_script(
197
+ "Object.defineProperty(navigator,'webdriver',{get:()=>undefined})")
198
+ self.page = self.ctx.new_page()
199
+ self._warmup()
200
+
201
+ def _warmup(self):
202
+ print(" [browser] homepage warmup (clearing WAF challenge)...", file=sys.stderr)
203
+ try:
204
+ self.page.goto("https://www.reddit.com/", wait_until="domcontentloaded",
205
+ timeout=45000)
206
+ time.sleep(6)
207
+ try:
208
+ self.page.goto("https://www.reddit.com/", wait_until="networkidle",
209
+ timeout=20000)
210
+ except Exception:
211
+ pass
212
+ except Exception as e:
213
+ print(f" [browser] warmup error: {e}", file=sys.stderr)
214
+
215
+ def fetch(self, url: str) -> dict:
216
+ try:
217
+ self.page.goto(url, wait_until="domcontentloaded", timeout=30000)
218
+ body = self.page.evaluate("document.body ? document.body.innerText : ''")
219
+ return json.loads(body)
220
+ except Exception as e:
221
+ print(f" [browser err] {e} :: {url}", file=sys.stderr)
222
+ return {}
223
+
224
+ def close(self):
225
+ try:
226
+ self.browser.close()
227
+ finally:
228
+ self._pw.stop()
229
+
230
+
231
+ # --- scoring ---------------------------------------------------------------
232
+
233
+
234
+ def clean(text: str) -> str:
235
+ text = html.unescape(text or "")
236
+ return re.sub(r"\s+", " ", text).strip()
237
+
238
+
239
+ def score_text(text: str):
240
+ low = text.lower()
241
+ hits = []
242
+ total = 0
243
+ for pattern, phrase, weight in _PAIN_PATTERNS:
244
+ if pattern.search(low):
245
+ total += weight
246
+ hits.append(phrase)
247
+ return total, hits
248
+
249
+
250
+ def is_on_domain(post: dict) -> bool:
251
+ blob = (post.get("title", "") + " " + post.get("selftext", "") + " " +
252
+ " ".join(c["body"] for c in post.get("juicy_comments", []))).lower()
253
+ return any(p.search(blob) for p in _DOMAIN_PATTERNS)
254
+
255
+
256
+ def juicy_sentences(text: str, max_n: int = 3):
257
+ """Return the verbatim sentences that actually carry the pain, best first."""
258
+ parts = re.split(r"(?<=[.!?])\s+|\n+", text)
259
+ scored = []
260
+ for s in parts:
261
+ s = s.strip()
262
+ if 25 <= len(s) <= 320:
263
+ sc, _ = score_text(s)
264
+ if sc > 0:
265
+ scored.append((sc, s))
266
+ scored.sort(key=lambda x: x[0], reverse=True)
267
+ seen, out = set(), []
268
+ for _, s in scored:
269
+ if s not in seen:
270
+ seen.add(s)
271
+ out.append(s)
272
+ if len(out) >= max_n:
273
+ break
274
+ return out
275
+
276
+
277
+ def when(ts) -> str:
278
+ try:
279
+ return datetime.fromtimestamp(ts, tz=timezone.utc).strftime("%Y-%m-%d")
280
+ except Exception:
281
+ return "????-??-??"
282
+
283
+
284
+ # --- reddit ----------------------------------------------------------------
285
+
286
+
287
+ def search(sub: str, query: str, t: str, limit: int):
288
+ q = urllib.parse.quote_plus(query)
289
+ url = (f"https://www.reddit.com/r/{sub}/search.json?"
290
+ f"q={q}&restrict_sr=on&sort=relevance&t={t}&limit={limit}")
291
+ data = get(url)
292
+ out = []
293
+ for child in data.get("data", {}).get("children", []):
294
+ d = child.get("data", {})
295
+ out.append({
296
+ "id": d.get("id"),
297
+ "sub": d.get("subreddit"),
298
+ "title": clean(d.get("title", "")),
299
+ "selftext": clean(d.get("selftext", "")),
300
+ "score": d.get("score", 0),
301
+ "num_comments": d.get("num_comments", 0),
302
+ "permalink": "https://www.reddit.com" + d.get("permalink", ""),
303
+ "created": when(d.get("created_utc", 0)),
304
+ "matched_query": query,
305
+ })
306
+ return out
307
+
308
+
309
+ def _walk_comments(children, out, depth, max_depth):
310
+ for child in children:
311
+ if child.get("kind") != "t1":
312
+ continue
313
+ d = child.get("data", {})
314
+ body = clean(d.get("body", ""))
315
+ if body and body not in ("[deleted]", "[removed]"):
316
+ out.append({
317
+ "author": d.get("author", "?"),
318
+ "body": body,
319
+ "score": d.get("score", 0),
320
+ "created": when(d.get("created_utc", 0)),
321
+ "depth": depth,
322
+ })
323
+ if depth < max_depth:
324
+ replies = d.get("replies")
325
+ if isinstance(replies, dict):
326
+ _walk_comments(replies.get("data", {}).get("children", []),
327
+ out, depth + 1, max_depth)
328
+
329
+
330
+ def top_comments(permalink: str, limit: int = 100, max_depth: int = 2):
331
+ # depth traversal captures the back-and-forth, not just top-level answers —
332
+ # a real argument in the replies is exactly the signal we want.
333
+ url = permalink.rstrip("/") + f"/.json?limit={limit}&sort=top"
334
+ data = get(url)
335
+ out = []
336
+ if not isinstance(data, list) or len(data) < 2:
337
+ return out
338
+ _walk_comments(data[1].get("data", {}).get("children", []), out, 0, max_depth)
339
+ return out
340
+
341
+
342
+ # --- main ------------------------------------------------------------------
343
+
344
+
345
+ def main():
346
+ ap = argparse.ArgumentParser(description="Mine Reddit for Norn campaign pain signals.")
347
+ ap.add_argument("--time", default="year", choices=["day", "week", "month", "year", "all"])
348
+ ap.add_argument("--limit", type=int, default=25, help="results per (sub, query)")
349
+ ap.add_argument("--top-threads", type=int, default=25,
350
+ help="how many threads to show in detail in the digest")
351
+ ap.add_argument("--dive", type=int, default=35,
352
+ help="how many most-discussed threads to fetch comments for")
353
+ ap.add_argument("--min-comments", type=int, default=15,
354
+ help="drop threads with fewer comments than this (dead posts)")
355
+ ap.add_argument("--subs", nargs="*", default=None, help="override subreddit list")
356
+ ap.add_argument("--queries", nargs="*", default=None, help="override query list")
357
+ ap.add_argument("--out", default="Docs/reddit_signal_harvest.md")
358
+ ap.add_argument("--browser", action="store_true",
359
+ help="drive real Chrome (beats Reddit's anti-bot WAF)")
360
+ ap.add_argument("--headless", action="store_true",
361
+ help="with --browser, run Chrome headless (less reliable vs WAF)")
362
+ args = ap.parse_args()
363
+
364
+ if REDDIT_USERNAME == "your_reddit_handle":
365
+ print("note: set REDDIT_USERNAME at the top of the script (Reddit UA courtesy).\n",
366
+ file=sys.stderr)
367
+
368
+ subs = list(dict.fromkeys(args.subs or SUBREDDITS))
369
+ queries = args.queries or QUERIES
370
+
371
+ print(f"mining {len(subs)} subs x {len(queries)} queries (t={args.time})"
372
+ f"{' [browser]' if args.browser else ''}...", file=sys.stderr)
373
+
374
+ global _FETCH
375
+ session = None
376
+ if args.browser:
377
+ session = BrowserSession(headless=args.headless)
378
+ _FETCH = session.fetch
379
+ try:
380
+ ranked = harvest(subs, queries, args)
381
+ finally:
382
+ if session:
383
+ session.close()
384
+
385
+ write_digest(ranked, args)
386
+ print(f"\ndone. ranked {len(ranked)} juicy threads -> {args.out}", file=sys.stderr)
387
+
388
+
389
+ def harvest(subs, queries, args):
390
+ posts = {}
391
+ for sub in subs:
392
+ for query in queries:
393
+ print(f" r/{sub} :: {query!r}", file=sys.stderr)
394
+ for p in search(sub, query, args.time, args.limit):
395
+ if not p["id"]:
396
+ continue
397
+ ps, phits = score_text(p["title"] + " " + p["selftext"])
398
+ p["score_title"] = ps
399
+ p["hits"] = phits
400
+ # keep the higher-scoring sighting if seen via multiple queries
401
+ if p["id"] not in posts or ps > posts[p["id"]].get("score_title", -1):
402
+ posts[p["id"]] = p
403
+ time.sleep(REQUEST_PAUSE)
404
+
405
+ # ENGAGEMENT-FIRST: the signal is a live debate, not a keyword match. Dive
406
+ # the most-discussed topical threads; dead posts (few comments) are useless
407
+ # even when they match the spine perfectly.
408
+ engaging = [p for p in posts.values() if p["num_comments"] >= args.min_comments]
409
+ engaging.sort(key=lambda p: (p["num_comments"], p["score"]), reverse=True)
410
+ dive = engaging[: args.dive]
411
+ print(f" {len(engaging)} threads >= {args.min_comments} comments; "
412
+ f"diving top {len(dive)}", file=sys.stderr)
413
+ for p in dive:
414
+ print(f" comments ({p['num_comments']}c): {p['title'][:55]!r}", file=sys.stderr)
415
+ scored = []
416
+ for c in top_comments(p["permalink"]):
417
+ cs, chits = score_text(c["body"])
418
+ if cs > 0:
419
+ c["score_juicy"] = cs
420
+ c["hits"] = chits
421
+ c["quotes"] = juicy_sentences(c["body"])
422
+ scored.append(c)
423
+ scored.sort(key=lambda c: (c["score_juicy"], c["score"]), reverse=True)
424
+ p["juicy_comments"] = scored[:6]
425
+ p["n_juicy_comments"] = len(scored)
426
+ p["score_comments"] = sum(c["score_juicy"] for c in scored[:3])
427
+ time.sleep(COMMENT_FETCH_PAUSE)
428
+
429
+ ranked = []
430
+ for p in dive:
431
+ p.setdefault("juicy_comments", [])
432
+ p.setdefault("score_comments", 0)
433
+ p.setdefault("n_juicy_comments", 0)
434
+ # engagement leads (comments weighted over upvotes); on-spine discussion boosts
435
+ p["engagement"] = p["num_comments"] + 0.25 * p["score"]
436
+ p["rank_score"] = p["engagement"] + 3 * p["score_comments"] + p["score_title"]
437
+ # keep only threads that are genuinely about APIs/testing AND have spine signal
438
+ if is_on_domain(p) and (p["score_comments"] > 0 or p["score_title"] > 0):
439
+ ranked.append(p)
440
+
441
+ ranked.sort(key=lambda p: (p["rank_score"], p["n_juicy_comments"]), reverse=True)
442
+ return ranked
443
+
444
+
445
+ def write_digest(ranked, args):
446
+ lines = []
447
+ lines.append("# Reddit signal harvest")
448
+ lines.append("")
449
+ lines.append(f"> Generated {datetime.now().strftime('%Y-%m-%d %H:%M')} · "
450
+ f"t={args.time} · ranked by LIVE ENGAGEMENT (comments) + on-spine discussion.")
451
+ lines.append("> Paste-ready for Docs/market_signals.md triage. Verbatim quotes only.")
452
+ lines.append("")
453
+
454
+ for i, p in enumerate(ranked[: args.top_threads], 1):
455
+ lines.append(f"## {i}. [{p['num_comments']}c · ↑{p['score']}] "
456
+ f"r/{p['sub']} — {p['title']}")
457
+ lines.append("")
458
+ lines.append(f"- **URL:** {p['permalink']}")
459
+ lines.append(f"- **Date:** {p['created']} · **Comments:** {p['num_comments']} · "
460
+ f"**Upvotes:** {p['score']} · **On-spine comments:** "
461
+ f"{p.get('n_juicy_comments', 0)} · **Found via:** {p['matched_query']!r}")
462
+ lines.append(f"- **Pain hits:** {', '.join(p['hits']) or '—'}")
463
+ if p.get("selftext"):
464
+ for q in juicy_sentences(p["selftext"]):
465
+ lines.append(f" - > {q}")
466
+ for c in p.get("juicy_comments", []):
467
+ lines.append(f"- **comment** (↑{c['score']}, {c['created']}) "
468
+ f"hits: {', '.join(c['hits'])}")
469
+ for q in c["quotes"]:
470
+ lines.append(f" - > {q}")
471
+ lines.append("")
472
+
473
+ # remaining threads, compact
474
+ rest = ranked[args.top_threads:]
475
+ if rest:
476
+ lines.append("---")
477
+ lines.append("")
478
+ lines.append("### More live candidates")
479
+ lines.append("")
480
+ for p in rest:
481
+ lines.append(f"- [{p['num_comments']}c ↑{p['score']}] r/{p['sub']} "
482
+ f"({p['created']}) — {p['title']} — {p['permalink']}")
483
+ lines.append("")
484
+
485
+ with open(args.out, "w", encoding="utf-8") as f:
486
+ f.write("\n".join(lines))
487
+
488
+
489
+ if __name__ == "__main__":
490
+ main()