pi-bro 0.1.0 → 0.1.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/README.md +253 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,6 +8,259 @@ adding extra messages to your conversation context.
|
|
|
8
8
|
[Google Antigravity CLI](https://antigravity.google/docs/cli-install) (`agy`)
|
|
9
9
|
and a Gemini model to create plain-language explanations.
|
|
10
10
|
|
|
11
|
+
## Bro in action
|
|
12
|
+
|
|
13
|
+
**Before**
|
|
14
|
+
|
|
15
|
+
[](https://raw.githubusercontent.com/tranhoangnguyen03/pi-bro/main/docs/images/bro-before.png)
|
|
16
|
+
|
|
17
|
+
**After `/bro`**
|
|
18
|
+
|
|
19
|
+
[](https://raw.githubusercontent.com/tranhoangnguyen03/pi-bro/main/docs/images/bro-after.png)
|
|
20
|
+
|
|
21
|
+
Bro optimizes for understanding, not simply for fewer words. The examples below
|
|
22
|
+
are synthetic coding-agent answers run through Bro's default prompt and edited
|
|
23
|
+
lightly for presentation and safety. Click a screenshot to see it at full size.
|
|
24
|
+
|
|
25
|
+
<details>
|
|
26
|
+
<summary><strong>Short:</strong> TypeScript says a value is <code>never</code></summary>
|
|
27
|
+
|
|
28
|
+
### Before
|
|
29
|
+
|
|
30
|
+
> The diagnostic is not evidence that `result` is intrinsically uninhabitable; it is an artifact of TypeScript's intraprocedural control-flow graph. Because `result` is initialized to `null` and only assigned inside the `forEach` callback, the outer scope does not model that callback as definitely executing. After `if (!result) return`, the remaining branch therefore collapses to `never`, producing the property-access error. Replace the callback-driven mutation with a control-flow-visible loop: `for (const item of items) { if (matches(item)) { result = item; break; } }`. Alternatively, derive the value expressionally with `const result = items.find(matches);` and guard `if (!result) return;` before dereferencing it. Avoid an `as Result` assertion; it suppresses the symptom while preserving the unsound state transition that caused the narrowing anomaly.
|
|
31
|
+
|
|
32
|
+
### After `/bro`
|
|
33
|
+
|
|
34
|
+
#### What happened
|
|
35
|
+
|
|
36
|
+
TypeScript showed an error saying your variable `result` cannot be used or
|
|
37
|
+
accessed.
|
|
38
|
+
|
|
39
|
+
This happened because `result` was set to `null` outside a `forEach` loop and
|
|
40
|
+
then modified inside the loop's helper function (callback). TypeScript tracks
|
|
41
|
+
code line by line. It cannot guarantee that code inside a `forEach` callback
|
|
42
|
+
will actually run.
|
|
43
|
+
|
|
44
|
+
#### What it means
|
|
45
|
+
|
|
46
|
+
Because TypeScript assumes the loop's callback might not run:
|
|
47
|
+
|
|
48
|
+
1. It believes `result` might still be `null`.
|
|
49
|
+
2. When you check `if (!result) return`, TypeScript thinks `result` can never
|
|
50
|
+
hold a valid value after that check.
|
|
51
|
+
3. It sets the type of `result` to `never` (a special type meaning "impossible
|
|
52
|
+
value") and blocks you from reading its properties.
|
|
53
|
+
|
|
54
|
+
The error is a limitation in how TypeScript analyzes loops with callbacks.
|
|
55
|
+
|
|
56
|
+
#### What you should do next
|
|
57
|
+
|
|
58
|
+
Choose one of these two fixes:
|
|
59
|
+
|
|
60
|
+
**Option 1: Use a standard `for...of` loop**
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
for (const item of items) {
|
|
64
|
+
if (matches(item)) {
|
|
65
|
+
result = item;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Option 2: Use `Array.prototype.find`**
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const result = items.find(matches);
|
|
75
|
+
if (!result) return;
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Avoid using a type assertion like `as Result`. It only hides the warning
|
|
79
|
+
without fixing the underlying tracking issue.
|
|
80
|
+
|
|
81
|
+
</details>
|
|
82
|
+
|
|
83
|
+
<details>
|
|
84
|
+
<summary><strong>Medium:</strong> Login succeeds, but the user is still logged out</summary>
|
|
85
|
+
|
|
86
|
+
### Before
|
|
87
|
+
|
|
88
|
+
> What you're observing is almost certainly not an authentication failure in the credential-validation layer; it is a post-authentication session persistence failure caused by an environment-dependent mismatch between Express's transport-security inference and the TLS termination topology in production.
|
|
89
|
+
>
|
|
90
|
+
> Locally, Express sees the request as plain HTTP and your development cookie configuration probably permits a non-secure session cookie. In production, however, the browser connects over HTTPS to Nginx, a load balancer, or an ingress controller, which terminates TLS and forwards the request to Node over HTTP. Unless Express trusts that intermediary, `req.secure` remains `false`, even when the proxy sends `X-Forwarded-Proto: https`. With `cookie.secure: true`, `express-session` can consequently decline to emit the cookie because it believes the originating request was insecure. The login route may still return `200`, making the credentials appear accepted, but the next request has no session identifier and is treated as anonymous.
|
|
91
|
+
>
|
|
92
|
+
> Configure proxy trust before registering session middleware, and make the cookie policy reflect whether the frontend and API are same-site or genuinely cross-site:
|
|
93
|
+
>
|
|
94
|
+
> ```js
|
|
95
|
+
> app.set("trust proxy", 1);
|
|
96
|
+
>
|
|
97
|
+
> app.use(session({
|
|
98
|
+
> secret: process.env.SESSION_SECRET,
|
|
99
|
+
> resave: false,
|
|
100
|
+
> saveUninitialized: false,
|
|
101
|
+
> proxy: true,
|
|
102
|
+
> cookie: {
|
|
103
|
+
> httpOnly: true,
|
|
104
|
+
> secure: true,
|
|
105
|
+
> sameSite: "lax",
|
|
106
|
+
> maxAge: 1000 * 60 * 60 * 24
|
|
107
|
+
> }
|
|
108
|
+
> }));
|
|
109
|
+
> ```
|
|
110
|
+
>
|
|
111
|
+
> Use `sameSite: "none"` instead of `"lax"` only when the browser must send the cookie in a cross-site context, such as a frontend and API on unrelated registrable domains. Browsers require `Secure` when `SameSite=None`, so that combination must remain HTTPS-only. Separate subdomains such as `app.example.com` and `api.example.com` are usually same-site, though they are still cross-origin.
|
|
112
|
+
>
|
|
113
|
+
> Also verify that the proxy explicitly forwards `X-Forwarded-Proto`, that the browser receives a `Set-Cookie` response header, and that frontend requests use `credentials: "include"` when crossing origins. If CORS is involved, the server must return the exact allowed origin plus `Access-Control-Allow-Credentials: true`; a wildcard origin is incompatible with credentialed requests.
|
|
114
|
+
>
|
|
115
|
+
> Avoid setting a cookie `domain` unless sharing across subdomains is intentional. A host-only cookie is safer and eliminates another production-only failure mode.
|
|
116
|
+
|
|
117
|
+
### After `/bro`
|
|
118
|
+
|
|
119
|
+
#### What happened
|
|
120
|
+
|
|
121
|
+
Your username and password check succeeded, and the login route returned `200
|
|
122
|
+
OK`. However, the browser did not receive or return a usable session cookie in
|
|
123
|
+
production.
|
|
124
|
+
|
|
125
|
+
#### What it means
|
|
126
|
+
|
|
127
|
+
In production, secure web traffic (HTTPS) hits an intermediary, such as a load
|
|
128
|
+
balancer or Nginx, before reaching your Node/Express app as plain HTTP.
|
|
129
|
+
|
|
130
|
+
Because your session cookie requires HTTPS (`secure: true`), Express may withhold
|
|
131
|
+
it when it does not trust the proxy forwarding the request. The exact trust
|
|
132
|
+
setting must match your deployment's proxy topology.
|
|
133
|
+
|
|
134
|
+
#### What you should do next
|
|
135
|
+
|
|
136
|
+
1. Configure Express to trust only your actual proxy topology. Put this before
|
|
137
|
+
the session setup. For example, use `1` only when there is exactly one trusted
|
|
138
|
+
proxy hop:
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
app.set("trust proxy", 1);
|
|
142
|
+
|
|
143
|
+
app.use(session({
|
|
144
|
+
secret: process.env.SESSION_SECRET,
|
|
145
|
+
resave: false,
|
|
146
|
+
saveUninitialized: false,
|
|
147
|
+
cookie: {
|
|
148
|
+
httpOnly: true,
|
|
149
|
+
secure: true,
|
|
150
|
+
sameSite: "lax",
|
|
151
|
+
maxAge: 1000 * 60 * 60 * 24
|
|
152
|
+
}
|
|
153
|
+
}));
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
2. Check your cookie settings:
|
|
157
|
+
- Keep `sameSite: "lax"` if your frontend and backend share the same main
|
|
158
|
+
domain, such as `app.example.com` and `api.example.com`.
|
|
159
|
+
- Use `sameSite: "none"` only when the cookie must be sent cross-site.
|
|
160
|
+
Browsers require `secure: true` with this setting.
|
|
161
|
+
- Avoid defining a custom cookie `domain` unless you deliberately need to
|
|
162
|
+
share cookies across subdomains.
|
|
163
|
+
|
|
164
|
+
3. Verify the proxy, browser, and CORS settings:
|
|
165
|
+
- Ensure the proxy forwards the `X-Forwarded-Proto` header.
|
|
166
|
+
- Check that the response contains a `Set-Cookie` header.
|
|
167
|
+
- Include `credentials: "include"` in cross-origin frontend requests.
|
|
168
|
+
- Return the exact allowed origin with
|
|
169
|
+
`Access-Control-Allow-Credentials: true`. Do not use a wildcard (`*`) for
|
|
170
|
+
credentialed requests.
|
|
171
|
+
|
|
172
|
+
</details>
|
|
173
|
+
|
|
174
|
+
<details>
|
|
175
|
+
<summary><strong>Long:</strong> A CI build fails only when its cache is restored</summary>
|
|
176
|
+
|
|
177
|
+
### Before
|
|
178
|
+
|
|
179
|
+
> The failure is not a nondeterministic pnpm installation problem, despite the misleading first error being emitted from the test runner immediately after dependency hydration. It is a state-contamination problem at the intersection of the repository cache, the package export map, and an incorrectly scoped cache key. The decisive observation is that the same commit succeeds in a clean checkout and in CI when cache restoration is disabled, but fails when the pre-build workspace cache is restored. That isolates the variable to files materialized outside Git rather than source, Node, or runner image drift.
|
|
180
|
+
>
|
|
181
|
+
> The affected package, `@acme/config`, was recently migrated from a dual CommonJS/ESM build to an ESM-oriented TypeScript output. Its root `package.json` declares `"type": "module"` and maps the `import` condition to `./dist/index.js`, while the legacy `require` condition still maps to `./dist/index.cjs`. The current compiler emits `index.js` but does not remove the previous build directory first. An older cached `dist` directory therefore contributes two files that no longer belong to the current build graph: `dist/index.cjs` and `dist/package.json`, the latter declaring `"type": "commonjs"`. The new compiler overlays `dist/index.js` but leaves both obsolete files intact. Because nested package boundaries override the root package type, Node interprets the newly emitted ESM `index.js` as CommonJS in that restored workspace and reports `Unexpected token 'export'`. Test processes entering through `require()` instead resolve the obsolete `index.cjs`, which references a removed chunk and can instead produce `MODULE_NOT_FOUND`. These apparently different errors are two projections of the same dirty-output condition.
|
|
182
|
+
>
|
|
183
|
+
> The cache configuration makes that contaminated state persistent. The workflow computes the key with `hashFiles('packages/**/pnpm-lock.yaml')`, but this workspace has only the root-level `pnpm-lock.yaml`. GitHub Actions consequently evaluates the hash expression to an empty value, yielding a key equivalent to `Linux-node20-workspace-`. A broad restore key then permits an archive produced before the module-format migration to satisfy the lookup. That archive combines the pnpm content-addressable store, Turborepo metadata, and every package's `dist` directory. Those data classes do not share valid invalidation semantics: pnpm store entries are immutable by content, Turborepo artifacts are task-hash addressed, and arbitrary build directories are mutable snapshots whose correctness depends on complete deletion or exact provenance. Treating them as one cache effectively elevates obsolete untracked files into undeclared build inputs.
|
|
184
|
+
>
|
|
185
|
+
> The evidence is visible by comparing the restored and clean workspaces before compilation. In the failing job, `packages/config/dist/package.json` exists with `"type":"commonjs"` and `packages/config/dist/index.cjs` has a timestamp and checksum predating the current commit. Neither file appears after `git clean -ffdx` followed by installation and build. The Actions cache log also shows the truncated key without a lockfile digest, while `git diff` remains empty because the problematic files are ignored. Finally, deleting only `packages/config/dist` before rerunning the unchanged test command converts the failure to a pass, which excludes dependency resolution and network variability as primary causes.
|
|
186
|
+
>
|
|
187
|
+
> The minimal durable remediation has three parts. First, stop caching package output directories. Cache the pnpm store separately, using a key that includes the actual root lockfile:
|
|
188
|
+
>
|
|
189
|
+
> ```yaml
|
|
190
|
+
> key: ${{ runner.os }}-node20-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
|
|
191
|
+
> restore-keys: |
|
|
192
|
+
> ${{ runner.os }}-node20-pnpm-
|
|
193
|
+
> ```
|
|
194
|
+
>
|
|
195
|
+
> A broad fallback is acceptable for the pnpm store because pnpm validates content-addressed entries; it is not acceptable for mutable `dist` snapshots. If remote or local Turborepo caching is retained, cache only `.turbo` and allow Turbo's task hash to govern artifact reuse rather than wrapping raw workspace outputs in a generic archive.
|
|
196
|
+
>
|
|
197
|
+
> Second, make every package build replace, rather than merge into, its output directory. `@acme/config` should delete `dist` before invoking `tsc`, `tsup`, or the existing bundler. The CI workflow should also run the repository's recursive clean target before the first build as a defense against future cache or runner reuse. This is not a substitute for fixing cache scope, but it restores the invariant that generated output is a function of the checked-out sources and declared toolchain.
|
|
198
|
+
>
|
|
199
|
+
> Third, make the export contract agree with emitted files. If `@acme/config` is intentionally ESM-only, remove the `require` condition and the obsolete CommonJS metadata. If CommonJS consumers remain supported, restore an explicit CJS build that deterministically emits `dist/index.cjs`; do not let a historical artifact impersonate that support. In either case, package publication should reject export targets that do not exist.
|
|
200
|
+
>
|
|
201
|
+
> Verification should exercise both cache states and both module entry paths. Run the full pipeline once with no cache, save only the corrected caches, and rerun the identical commit expecting a cache hit and the same result. Inspect the built package to confirm the nested CommonJS `package.json` and legacy chunks are absent unless intentionally generated. Add a lightweight smoke check using dynamic `import('@acme/config')`; if CommonJS remains supported, add a second check through `createRequire`. Finally, change the root lockfile and confirm the logged cache key changes. That matrix validates clean-build reproducibility, restored-cache reproducibility, export-map coherence, and dependency-cache invalidation independently rather than relying on a single green run.
|
|
202
|
+
|
|
203
|
+
### After `/bro`
|
|
204
|
+
|
|
205
|
+
#### What happened
|
|
206
|
+
|
|
207
|
+
Your build failed because old files were left behind by a broken cache (a saved
|
|
208
|
+
snapshot of previous build files).
|
|
209
|
+
|
|
210
|
+
The package `@acme/config` recently switched its module format to modern
|
|
211
|
+
JavaScript (ESM). However, the build process did not delete the old output
|
|
212
|
+
folder (`dist`) before creating new files.
|
|
213
|
+
|
|
214
|
+
Because the GitHub Actions setting searches the wrong path
|
|
215
|
+
(`hashFiles('packages/**/pnpm-lock.yaml')`), GitHub could not find the root
|
|
216
|
+
`pnpm-lock.yaml`. It restored an outdated cache containing `dist/index.cjs` and
|
|
217
|
+
a `dist/package.json` configured for older JavaScript (CommonJS). The new build
|
|
218
|
+
wrote some files but left the old ones behind, confusing Node and causing errors
|
|
219
|
+
such as `Unexpected token 'export'` and `MODULE_NOT_FOUND`.
|
|
220
|
+
|
|
221
|
+
A clean checkout without the cache succeeds. This proves the problem is old
|
|
222
|
+
cached files, not your source code or dependencies.
|
|
223
|
+
|
|
224
|
+
#### What it means
|
|
225
|
+
|
|
226
|
+
- **Cache contamination:** The CI workflow restores mutable `dist` folders
|
|
227
|
+
alongside package-manager files, mixing obsolete files into new builds.
|
|
228
|
+
- **Mismatched module settings:** The package says it supports both modern
|
|
229
|
+
(`import`) and legacy (`require`) formats, but it only builds the modern one
|
|
230
|
+
while relying on leftover cached files for the legacy one.
|
|
231
|
+
|
|
232
|
+
#### What you should do next
|
|
233
|
+
|
|
234
|
+
1. **Fix the GitHub Actions cache:** Stop caching `dist` folders. Cache only the
|
|
235
|
+
pnpm store, using the root lockfile:
|
|
236
|
+
|
|
237
|
+
```yaml
|
|
238
|
+
key: ${{ runner.os }}-node20-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
|
|
239
|
+
restore-keys: |
|
|
240
|
+
${{ runner.os }}-node20-pnpm-
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
If you use Turborepo, cache only `.turbo`.
|
|
244
|
+
|
|
245
|
+
2. **Clean output folders before building:** Make `@acme/config` delete `dist`
|
|
246
|
+
before running `tsc`, `tsup`, or the bundler. Run the repository's recursive
|
|
247
|
+
clean command before CI builds.
|
|
248
|
+
|
|
249
|
+
3. **Update the package exports:**
|
|
250
|
+
- If `@acme/config` is ESM-only, remove the `require` entry and old CommonJS
|
|
251
|
+
metadata.
|
|
252
|
+
- If CommonJS is still needed, add a real build step for `dist/index.cjs`.
|
|
253
|
+
|
|
254
|
+
4. **Verify the fix:**
|
|
255
|
+
- Delete the generated `packages/config/dist` folder, then run a clean build.
|
|
256
|
+
Do not use `git clean -ffdx` in a working copy with files you need.
|
|
257
|
+
- Run the workflow without a cache, save the new cache, and rerun the same
|
|
258
|
+
commit to verify that a cache hit also passes.
|
|
259
|
+
- Test `import('@acme/config')`, and test `createRequire` if CommonJS is
|
|
260
|
+
supported.
|
|
261
|
+
|
|
262
|
+
</details>
|
|
263
|
+
|
|
11
264
|
## Requirements
|
|
12
265
|
|
|
13
266
|
- Earendil Pi `>=0.78.1 <1` (tested on `0.84.2`)
|