Skip to content

[feature] Path-branched tj onboard first run - #432

Merged
anilmurty merged 4 commits into
mainfrom
feat/onboard-path-branch
Jul 9, 2026
Merged

anilmurty merged 4 commits into
mainfrom
feat/onboard-path-branch

Conversation

@anilmurty

Copy link
Copy Markdown
Contributor

tj onboard (no flag) assumed an SDK/API user: it prompted for the Claude plan, showed a @watch/patch_anthropic snippet, and verified by polling for a live span. For a Claude Code user — the most common case — that left an empty tool and a verify that can never succeed (Claude Code emits no live span into an SDK pipeline; its data is on-disk logs). This redesigns the bare path to ask how the user actually runs agents, then backfill/wire per path.

Summary

  • Bare tj onboard opens with "How do you use AI agents?" → (1) Claude Code (2) Codex (3) Your own agents (Python/TS SDK or API) (4) A combination.
  • --claude-code / --codex stay as shortcuts that skip the question.
  • Backfill now happens after the choice, per path.
  • Every path ends on the shared branded home screen + tailored next-best-actions.
  • The path question is interactive-only; non-tty bare invocation (scripts/CI) keeps the historical generic SDK path byte-for-byte.

Per-path flow

  • Claude Code: existing --claude-code flow — Anthropic plan prompt → auto-backfill ~/.claude/projects via ingest_claude_code → statusline wiring → success reported as "N sessions backfilled" (not a live-span wait). No @watch step.
  • Codex: existing --codex [otel] wiring → then a defensive Codex backfill. The Codex on-disk adapter (tokenjam.core.ingest_adapters.codex.ingest_codex) is a separate workstream and may not have landed, so it's resolved via a guarded import (_try_backfill_codex): reports the count when present, forward-only framing ("Wired — data flows as you use Codex") when absent — never crashes, never over-claims.
  • Your own agents (SDK/API): existing generic path — plan/provider prompt → @watch/patch_anthropic snippet → live-span verify (persona sdk already references tj ping).
  • Combination: asks which surfaces they use (Claude Code / Codex / custom), delegates wiring to the per-path onboarders (keeps config/statusline/OTel setup DRY), reports what was backfilled, then shows the SDK instrument snippet — one coherent sequence.

Design decisions

  • Interactive-only branch. A non-tty bare tj onboard skips the question and runs the historical generic SDK path, so existing automation is unchanged. Gated behind a new _is_interactive() seam (CliRunner swaps sys.stdin, so this is the testable hook).
  • Defensive Codex backfill. Per the brief, cmd_backfill.py / the ingest adapters are owned by another agent. _try_backfill_codex never imports the adapter at module load — only inside the function, inside try/except — so this PR works whether or not tj backfill codex has shipped.
  • Shared closing banner. _print_setup_complete_home() reuses cli/home.print_home() for the next-best-actions block and prefixes an honest "N sessions backfilled" line only when a backfill actually ingested data.
  • Honesty discipline (Critical Rule 14). No promised savings; no "backfilled N" claim when nothing was ingested; forward-only framing when the Codex adapter is absent.

Tests / Verification

  • New tests/unit/test_onboard_path_branch.py (15 tests): the path prompt lists all four paths and returns the right key; bare onboard routes to the selected path; SDK choice falls through to generic; non-tty skips the question; --claude-code/--codex shortcuts skip it; _try_backfill_codex is forward-only when the adapter is missing and reports counts when present; the closing banner reports counts only with data.
  • ruff check tokenjam/ clean; mypy tokenjam/cli/cmd_onboard.py clean.
  • Full tests/unit/test_onboard*.py (128) + tests/integration/test_cli.py (78) green.

What's NOT in this PR

  • No changes to cmd_backfill.py or the ingest adapters — the Codex backfill adapter is another agent's workstream; this PR calls it defensively.
  • No changes to cmd_ping.py — tj ping is another agent's fix; the SDK-path verify text references it, nothing more.

🤖 Generated with Claude Code

anilmurty and others added 3 commits July 8, 2026 16:24
The bare `tj onboard` (no flag) assumed an SDK/API user: it prompted for the
Claude plan, showed a `@watch`/`patch_anthropic` snippet, and verified by
polling for a live span. For a Claude Code user — the most common case — that
left an empty, useless tool and a verify that could never succeed (no live
span is ever emitted; their data lives in on-disk logs).

Redesign the bare path to open with a path question that routes to the flow
matching how the user actually runs agents, so backfill happens AFTER the
choice, per path:

- Claude Code → the existing --claude-code flow (plan prompt, auto-backfill of
  ~/.claude/projects, statusline wiring); success = "N sessions backfilled",
  not a live-span wait.
- Codex → the existing --codex OTel wiring, THEN a defensive Codex backfill
  (`tokenjam.core.ingest_adapters.codex.ingest_codex`, resolved via a guarded
  import since that adapter is a separate workstream and may not have landed);
  forward-only framing when unavailable.
- Your own agents (SDK/API) → the existing generic path: plan/provider prompt,
  the `@watch`/`patch_anthropic` snippet, live-span verify via `tj ping`.
- A combination → ask which surfaces they use, run the necessary backfills,
  report what was done, then show the SDK instrument snippet — one sequence.

Every path now ends on the shared branded home screen + tailored
next-best-actions (reusing `cli/home.print_home`), prefixed with an honest
"N sessions backfilled" note only when a backfill actually ingested data.

`--claude-code` / `--codex` remain shortcuts that skip the question. The path
question is only asked interactively — a non-tty bare invocation (scripts, CI)
falls through to the historical generic SDK path unchanged. Copy keeps the
honesty discipline (Critical Rule 14): no promised savings, no backfill claim
when nothing was ingested, forward-only framing when the Codex adapter is
absent.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015e3DZGhb1R5sXX2x52Y2Ar
@anilmurty
anilmurty marked this pull request as ready for review July 9, 2026 00:00
@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR redesigns tj onboard (bare invocation) to ask how the user runs AI agents and route to the correct per-path flow — Claude Code, Codex, SDK, or a combination — instead of always assuming an SDK/API user. Non-interactive invocations keep the historical generic-SDK behaviour unchanged.

  • _prompt_usage_path presents the four-choice menu and gates the branch behind _is_interactive(), preserving CI/script compatibility.
  • _try_backfill_codex defensively resolves the Codex on-disk adapter at runtime so the Codex and combination paths work whether or not that adapter has landed.
  • _onboard_combination orchestrates the multi-surface flow by delegating to the existing per-path onboarders, but both sub-onboarders (_onboard_claude_code, _onboard_codex) now end with _print_setup_complete_home(), which means the banner is printed up to three times in a single combination run; additionally, _onboard_codex already calls _try_backfill_codex internally while _onboard_combination immediately calls it a second time for the same config, double-counting any ingested sessions.

Confidence Score: 3/5

The combination path has two concrete defects introduced by this PR: the Codex backfill runs twice and the closing banner prints up to three times.

The combination path has two concrete defects introduced by this PR: the Codex backfill runs twice and the closing banner prints up to three times. The Claude Code and SDK paths are correct. Neither combination-path bug is caught by the new test suite, which mocks out the sub-onboarders in routing tests.

cmd_onboard.py — specifically the _onboard_combination function and how it interacts with the closing-banner calls in _onboard_claude_code and _onboard_codex.

Important Files Changed

Filename Overview
tokenjam/cli/cmd_onboard.py Adds path-branched interactive routing, per-path closing banners, and defensive Codex backfill; two bugs: (1) _onboard_combination calls _try_backfill_codex a second time even though _onboard_codex already calls it internally, and (2) each sub-onboarder prints its own _print_setup_complete_home(), so the combination flow prints "You're set up." up to three times.
tests/unit/test_onboard_path_branch.py 15 new unit tests covering routing, tty detection, defensive Codex backfill, and the closing banner; no end-to-end test for the combination path with multiple surfaces selected, so the double-backfill and duplicate-banner bugs are not caught.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[tj onboard] --> B{Flag provided?}
    B -->|--claude-code| CC[_onboard_claude_code]
    B -->|--codex| CX[_onboard_codex]
    B -->|bare| C{_is_interactive?}
    C -->|No TTY| SDK[Generic SDK path]
    C -->|Yes| D[_prompt_usage_path]
    D -->|claude_code| CC
    D -->|codex| CX
    D -->|sdk| SDK
    D -->|combination| COMB[_onboard_combination]
    CC --> CC2[_print_setup_complete_home]
    CX --> CX1[_try_backfill_codex first call]
    CX --> CX2[_print_setup_complete_home]
    COMB --> Q1{uses_cc?}
    Q1 -->|Yes| CC
    CC2 --> Q2{uses_codex?}
    Q1 -->|No| Q2
    Q2 -->|Yes| CX
    CX2 --> DUP[_try_backfill_codex DUPLICATE second call]
    DUP --> Q3{uses_sdk?}
    Q2 -->|No| Q3
    Q3 -->|Yes| SNIP[Show SDK snippet]
    SNIP --> FINAL[_print_setup_complete_home third call]
    Q3 -->|No| FINAL
    SDK --> SDKFIN[_print_setup_complete_home]
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[tj onboard] --> B{Flag provided?}
    B -->|--claude-code| CC[_onboard_claude_code]
    B -->|--codex| CX[_onboard_codex]
    B -->|bare| C{_is_interactive?}
    C -->|No TTY| SDK[Generic SDK path]
    C -->|Yes| D[_prompt_usage_path]
    D -->|claude_code| CC
    D -->|codex| CX
    D -->|sdk| SDK
    D -->|combination| COMB[_onboard_combination]
    CC --> CC2[_print_setup_complete_home]
    CX --> CX1[_try_backfill_codex first call]
    CX --> CX2[_print_setup_complete_home]
    COMB --> Q1{uses_cc?}
    Q1 -->|Yes| CC
    CC2 --> Q2{uses_codex?}
    Q1 -->|No| Q2
    Q2 -->|Yes| CX
    CX2 --> DUP[_try_backfill_codex DUPLICATE second call]
    DUP --> Q3{uses_sdk?}
    Q2 -->|No| Q3
    Q3 -->|Yes| SNIP[Show SDK snippet]
    SNIP --> FINAL[_print_setup_complete_home third call]
    Q3 -->|No| FINAL
    SDK --> SDKFIN[_print_setup_complete_home]
Loading
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
tokenjam/cli/cmd_onboard.py:1717-1736
**Double Codex backfill in combination path**

`_onboard_codex` now calls `_try_backfill_codex` internally (added in this PR, line ~1584), and `_onboard_combination` then calls it a second time in the explicit try/except block immediately after. On the combination path with Codex selected, the adapter runs twice against the same on-disk logs, producing a duplicate console line and double-counting sessions. The stale comment ("The per-Codex onboarder is forward-only (no backfill of its own)") reflects the pre-PR state and is now incorrect. The second `_try_backfill_codex` call should be removed; the count surfaced by `_onboard_codex` is already reported inline.

### Issue 2 of 2
tokenjam/cli/cmd_onboard.py:1706-1757
**"You're set up." printed up to three times in combination flow**

`_onboard_claude_code` and `_onboard_codex` each end with their own `_print_setup_complete_home()` call (added in this PR). When `_onboard_combination` calls both sub-onboarders and then calls `_print_setup_complete_home()` itself on line 1757, a user who selects Claude Code + Codex + SDK will see the "You're set up." banner three times in a single run. The sub-onboarders should skip the closing banner when invoked as part of a combination flow (e.g. via an `is_subflow=False` guard), or `_onboard_combination` should call them in a way that suppresses the banner and renders it exactly once at the end.

Reviews (1): Last reviewed commit: "Merge branch 'main' into feat/onboard-pa..." | Re-trigger Greptile

Comment on lines +1717 to +1736
# --- Codex (full flow: OTel wiring) then a defensive backfill ---
if uses_codex:
console.print()
console.print("[bold]Setting up Codex…[/bold]")
_onboard_codex(
ctx, budget, no_daemon, force, reconfigure=False,
plan_override=plan_override, verify=False,
)
# The per-Codex onboarder is forward-only (no backfill of its own); try
# the on-disk Codex backfill defensively so combination reports it too.
try:
from tokenjam.core.config import load_config as _lc
global_path = Path.home() / ".config" / "tj" / "config.toml"
if global_path.exists():
codex_cfg = _lc(str(global_path))
cx_msg, _cx_has, _cx_n = _try_backfill_codex(codex_cfg)
if cx_msg:
console.print(f" Codex backfilled: {cx_msg}")
except Exception:
pass

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Double Codex backfill in combination path

_onboard_codex now calls _try_backfill_codex internally (added in this PR, line ~1584), and _onboard_combination then calls it a second time in the explicit try/except block immediately after. On the combination path with Codex selected, the adapter runs twice against the same on-disk logs, producing a duplicate console line and double-counting sessions. The stale comment ("The per-Codex onboarder is forward-only (no backfill of its own)") reflects the pre-PR state and is now incorrect. The second _try_backfill_codex call should be removed; the count surfaced by _onboard_codex is already reported inline.

Prompt To Fix With AI
This is a comment left during a code review.
Path: tokenjam/cli/cmd_onboard.py
Line: 1717-1736

Comment:
**Double Codex backfill in combination path**

`_onboard_codex` now calls `_try_backfill_codex` internally (added in this PR, line ~1584), and `_onboard_combination` then calls it a second time in the explicit try/except block immediately after. On the combination path with Codex selected, the adapter runs twice against the same on-disk logs, producing a duplicate console line and double-counting sessions. The stale comment ("The per-Codex onboarder is forward-only (no backfill of its own)") reflects the pre-PR state and is now incorrect. The second `_try_backfill_codex` call should be removed; the count surfaced by `_onboard_codex` is already reported inline.

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +1706 to +1757
# --- Claude Code (full flow: config + statusline + auto-backfill) ---
if uses_cc:
console.print()
console.print("[bold]Setting up Claude Code…[/bold]")
_onboard_claude_code(
ctx, budget, no_daemon, force, reconfigure=False,
plan_override=plan_override, project_override=project_override,
verify=False,
)
done.append("Claude Code")

# --- Codex (full flow: OTel wiring) then a defensive backfill ---
if uses_codex:
console.print()
console.print("[bold]Setting up Codex…[/bold]")
_onboard_codex(
ctx, budget, no_daemon, force, reconfigure=False,
plan_override=plan_override, verify=False,
)
# The per-Codex onboarder is forward-only (no backfill of its own); try
# the on-disk Codex backfill defensively so combination reports it too.
try:
from tokenjam.core.config import load_config as _lc
global_path = Path.home() / ".config" / "tj" / "config.toml"
if global_path.exists():
codex_cfg = _lc(str(global_path))
cx_msg, _cx_has, _cx_n = _try_backfill_codex(codex_cfg)
if cx_msg:
console.print(f" Codex backfilled: {cx_msg}")
except Exception:
pass
done.append("Codex")

# --- Custom SDK / API agents: show the instrument snippet ---
if uses_sdk:
console.print()
console.print("[bold]Instrument your own agents:[/bold]")
console.print()
_print_instrument_agent_snippet()
console.print()
console.print(
" Then run your agent and verify a live span is flowing with "
"[bold]tj ping[/bold] (in another terminal)."
)
done.append("SDK/API")

console.print()
console.print(
f"[bold green]Combination setup complete[/bold green] "
f"[dim]({', '.join(done)})[/dim]"
)
_print_setup_complete_home()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 "You're set up." printed up to three times in combination flow

_onboard_claude_code and _onboard_codex each end with their own _print_setup_complete_home() call (added in this PR). When _onboard_combination calls both sub-onboarders and then calls _print_setup_complete_home() itself on line 1757, a user who selects Claude Code + Codex + SDK will see the "You're set up." banner three times in a single run. The sub-onboarders should skip the closing banner when invoked as part of a combination flow (e.g. via an is_subflow=False guard), or _onboard_combination should call them in a way that suppresses the banner and renders it exactly once at the end.

Prompt To Fix With AI
This is a comment left during a code review.
Path: tokenjam/cli/cmd_onboard.py
Line: 1706-1757

Comment:
**"You're set up." printed up to three times in combination flow**

`_onboard_claude_code` and `_onboard_codex` each end with their own `_print_setup_complete_home()` call (added in this PR). When `_onboard_combination` calls both sub-onboarders and then calls `_print_setup_complete_home()` itself on line 1757, a user who selects Claude Code + Codex + SDK will see the "You're set up." banner three times in a single run. The sub-onboarders should skip the closing banner when invoked as part of a combination flow (e.g. via an `is_subflow=False` guard), or `_onboard_combination` should call them in a way that suppresses the banner and renders it exactly once at the end.

How can I resolve this? If you propose a fix, please make it concise.

@anilmurty
anilmurty merged commit 12f8e74 into main Jul 9, 2026
4 checks passed
@anilmurty
anilmurty deleted the feat/onboard-path-branch branch July 9, 2026 00:04
anilmurty added a commit that referenced this pull request Jul 9, 2026
… (#434)

Five review findings from the just-merged onboarding batch, addressed
together because they cluster around the same onboard/uninstall UX and
the SDK exporter/backfill idempotency touched in that batch.

#432 (P1): the combination path double-ran the Codex backfill and printed
the closing home banner up to three times. `_onboard_codex` ran its own
backfill AND printed the banner, `_onboard_claude_code` printed the banner
too, and `_onboard_combination` did both again. Fix: give both per-path
sub-onboarders a `standalone: bool = True` parameter. The combination flow
passes `standalone=False`, which suppresses each sub-onboarder's own Codex
backfill and closing banner — so the backfill runs exactly once (in the
combination leg) and the banner prints exactly once (at the very end).
Single-path flows keep `standalone=True` and are unchanged.

#430 (P1): after a successful `pipx uninstall tokenjam`, the venv is gone,
so the printed reinstall hint `pipx upgrade tokenjam` would fail ("not
installed"). Added `_package_fresh_install_hint()` (bare `pipx install` /
`pip install`) and use it only on the successful-removal path. The
upgrade/--force hint stays where it belongs — when the package REMAINS
(a plain install would no-op there).

#430 (P2): `--remove-package` help text overstated ("runs pipx/pip
uninstall") — for non-pipx installs it only PRINTS the command. Reworded
the help + option text and the interactive confirm prompt so they say
"runs" for pipx and "prints … for you to run" for pip/venv.

#431 (P2): a whitespace-only ingest secret was truthy, so `TjHttpExporter`
built `Authorization: Bearer  ` — the same illegal header value the empty
case already guards against. Now guard on `.strip()` so a blank secret
omits the header entirely.

#433 (P2): `ingest_codex` issued one `SELECT 1 FROM spans WHERE span_id=$1`
per span. Aligned it with the Claude Code path's bulk idempotency by
reusing `core.backfill._existing_span_ids` — one chunked
`WHERE span_id IN (...)` per session partitions new-vs-existing spans,
replacing the per-span existence SELECT. Backends without a `conn`
(InMemoryBackend) fall back to plain inserts and let the PRIMARY KEY
conflict skip duplicates.

Tests: combination path backfills each source ONCE and prints the banner
ONCE (TestCombinationPathNoDoubleRun); after-removal reinstall hint is
`pipx install` not upgrade; prompt wording matches install type;
whitespace-only secret omits the Authorization header; a DuckDB-backed
idempotency test exercises the new bulk `_existing_span_ids` path.


Claude-Session: https://claude.ai/code/session_015e3DZGhb1R5sXX2x52Y2Ar

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant