feat: add --cookies-file flag for session persistence#2125
feat: add --cookies-file flag for session persistence#2125mvanhorn wants to merge 3 commits intolightpanda-io:mainfrom
Conversation
Add a --cookies-file CLI option that loads cookies from a JSON file
at startup and saves them back on exit. This enables AI agents to
maintain login sessions across multiple Lightpanda invocations.
The cookie format matches CDP Network.Cookie (compatible with
Puppeteer's page.cookies() export):
[{"name":"sid","value":"abc","domain":".example.com","path":"/",
"expires":1234567890,"secure":true,"httpOnly":true}]
Closes lightpanda-io#335
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
std.io.bufferedWriter doesn't exist in Zig 0.15.2. Use the file.writer(&buf) pattern that matches the rest of the codebase. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Thanks for the contribution.
|
- Escape braces in help text format string to avoid std.debug.print interpreting them as format specifiers - Use writer.interface for std.Io.Writer methods (writeAll, stringify) instead of calling them directly on fs.File.Writer - Replace writer.flush() with writer.end() per codebase convention Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Thanks for the detailed review! Fixed the build errors in cc6ffb5 — unescaped braces in the help text format string and wrong writer API usage ( On your points: 1. 2. JSON format: Good to know you're OK with the CDP-compatible JSON format. I'll keep it as-is. 3. CDP multi-session: This is the key design question. Currently each
I'd lean toward option 3 for serve/CDP since the cookie jar is really "this browser's state" rather than per-tab state. What do you think? 4. Scope: You're right, the current implementation only hooks into |
Summary
Adds a
--cookies-fileCLI option that loads cookies from a JSON file at startup and saves them back on exit. Works withfetch,serve, andmcpcommands.Why this matters
AI agents that need to maintain login sessions across multiple Lightpanda invocations are currently blocked. Issue #335 (7 comments) asks for cookie pass/dump. Steel Browser and Browserbase both offer session persistence, which is table-stakes for browser automation.
The cookie JSON format matches CDP
Network.Cookie, so cookies exported from Puppeteer'spage.cookies()or Playwright'scontext.cookies()work directly.Demo
Usage:
First run saves cookies to
session.json. Subsequent runs load them, sending cookies with requests. Updated cookies are saved back on exit.Changes
src/Config.zig: Addedcookies_filefield toCommonoptions, accessorcookiesFile(), CLI parsing for--cookies-file, and help text.src/cookies.zig(new): Cookie file I/O module withloadFromFileandsaveToFile. Parses/writes JSON arrays of{name, value, domain, path, expires, secure, httpOnly}objects. Usesstd.jsonfor parsing and manual JSON writing for output. Handles missing files gracefully (first run creates the file on exit).src/lightpanda.zig: Callscookies.loadFromFileafter session creation anddefercallscookies.saveToFilebefore session cleanup.Cookie file format
[ {"name": "session_id", "value": "abc123", "domain": ".example.com", "path": "/", "expires": 1744329600, "secure": true, "httpOnly": true} ]Fixes #335
This contribution was developed with AI assistance (Claude Code).