|
| 1 | +# Scarf Java – Agent Notes |
| 2 | + |
| 3 | +This playbook gives future agents the quickest path to productive, low-risk work on the Java SDK. |
| 4 | + |
| 5 | +## TL;DR |
| 6 | +- Primary entry point is `src/main/java/sh/scarf/ScarfEventLogger.java`; its JSON helper lives in `JsonUtil.java`. |
| 7 | +- Run the full unit suite with `mvn -q test`. Use the integration test class’ embedded HTTP server to exercise HTTP flows. |
| 8 | +- Respect runtime toggles: analytics disablement via `DO_NOT_TRACK`/`SCARF_NO_ANALYTICS`, verbose diagnostics via `SCARF_VERBOSE`. |
| 9 | +- Example usage is in `src/main/java/sh/scarf/examples/LiveExampleBasic.java`. |
| 10 | +- Releases are tag-driven (`git tag vX.Y.Z && git push origin vX.Y.Z`); the GitHub workflow handles publishing. |
| 11 | + |
| 12 | +## Project Layout |
| 13 | +- `pom.xml`: Maven build config, Java 11+, JUnit 5, no runtime deps. |
| 14 | +- `src/main/java/sh/scarf/ScarfEventLogger.java`: Public client API; performs payload serialization, timeout handling, user-agent construction, and HTTP POST. |
| 15 | +- `src/main/java/sh/scarf/JsonUtil.java`: Minimal JSON encoder for primitives, maps, lists, arrays; keeps dependency footprint zero. |
| 16 | +- `src/main/java/sh/scarf/examples/LiveExampleBasic.java`: Sends sample events against `https://scarf.gateway.scarf.sh/scarf-java`. |
| 17 | +- `src/test/java/sh/scarf/*`: JUnit tests covering JSON encoding edge cases, environment switches, verbose logging, and a real HTTP round trip against an in-process `HttpServer`. |
| 18 | +- `README.md`: Installation, usage, development, and publishing instructions. Mirror its tone when expanding docs. |
| 19 | + |
| 20 | +## Development Workflow |
| 21 | +- Use Maven for everything (`mvn -q test`, `mvn -q -DskipTests package`). The repo also ships a `nix-shell` for an on-demand Java 17 + Maven environment (`nix-shell --run "mvn -q test"`). |
| 22 | +- Tests rely on standard JUnit assertions; no extra runner configuration required. The integration test spins up `com.sun.net.httpserver.HttpServer` automatically—no external services needed. |
| 23 | +- Keep the library dependency-free. Prefer standard library features; justify any new dependency with maintainers before adding it. |
| 24 | +- Stick to Java 11 language features unless asked otherwise. |
| 25 | + |
| 26 | +## Runtime Behavior & Diagnostics |
| 27 | +- Disable analytics by setting either `DO_NOT_TRACK=1` or `SCARF_NO_ANALYTICS=1`. `ScarfEventLogger` returns `false` immediately in these cases—tests assert this contract. |
| 28 | +- Verbose mode (`SCARF_VERBOSE=1`) streams payloads, user-agent strings, and response summaries to `System.err`. Integration tests capture this output; regressions will surface quickly. |
| 29 | +- User-Agent construction falls back to `dev` if the JAR manifest lacks `Implementation-Version`. When packaging, ensure the manifest is populated (handled automatically via Maven during release). |
| 30 | + |
| 31 | +## Extending the SDK |
| 32 | +- **Adding event helpers**: Build convenience wrappers around `logEvent` inside `ScarfEventLogger` or a new utility class. Maintain the class’ thread-safety (no shared mutable state beyond the constructor-populated fields). |
| 33 | +- **Timeouts & retries**: Default timeout lives in the constructor; per-call overrides convert seconds → `Duration`. Any new retry/backoff logic should be opt-in to preserve the current lightweight behavior. |
| 34 | +- **JSON enhancements**: Add serialization paths to `JsonUtil` if new data types are needed. Unit-test edge cases (nulls, escaping, NaN/Infinity) similarly to existing coverage. |
| 35 | +- **Configuration**: If introducing new env toggles, update `README.md` and add focused tests that exercise the new switch. |
| 36 | + |
| 37 | +## Testing Checklist |
| 38 | +- Run `mvn -q test` locally. The suite is fast (<5s) and must remain so. |
| 39 | +- For changes affecting HTTP behavior, extend `ScarfEventLoggerIntegrationTest` or add a sibling class; prefer deterministic embedded servers over external mocks. |
| 40 | +- If new JSON cases are added, cover them in `JsonUtilTest`. |
| 41 | +- Manual smoke: use `LiveExampleBasic` (`mvn -q -DskipTests compile && java -cp target/classes sh.scarf.examples.LiveExampleBasic`) against a non-production endpoint. |
| 42 | + |
| 43 | +## Releasing & Versioning |
| 44 | +- Release cadence is tag-based. Bump versions by creating a tag (`git tag v0.1.0`) and pushing it; GitHub Actions updates the manifest version, builds, signs, and ships to Maven Central. |
| 45 | +- Update dependency snippets in `README.md` when releasing (Maven & Gradle examples). |
| 46 | +- Ensure manifest metadata is correct before tagging (`mvn -q -DskipTests package` writes the version). |
| 47 | + |
| 48 | +## Ready-to-Ship Checklist |
| 49 | +- [ ] Code compiles (`mvn -q -DskipTests compile`). |
| 50 | +- [ ] Tests pass (`mvn -q test`). |
| 51 | +- [ ] Documentation updated (README and this file if guidance changes). |
| 52 | +- [ ] No new dependencies without discussion. |
| 53 | +- [ ] Verbose logging remains helpful and secure (no secrets emitted). |
| 54 | + |
| 55 | +Treat this file as a living document—keep it aligned with reality after every meaningful change. |
0 commit comments