Skip to content

cli: tensorfold chat talks to a model in the terminal, through a serve child of its own or a running server's --url - #3538

Merged
ashhart merged 11 commits into
mainfrom
lead/cli-chat
Nov 19, 2026
Merged

ashhart merged 11 commits into
mainfrom
lead/cli-chat

Conversation

@ashhart

@ashhart ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

tensorfold chat was a usage error. Talking to a model from the CLI meant running tensorfold serve, then curl or another client. This adds an interactive terminal chat that starts its own server, or talks to a running one.

What this changes

  • tensorfold chat MODEL: starts tensorfold serve MODEL as a child of the same binary on a free loopback port, waits until it is ready, chats, and stops it when the chat ends.

    • Flags after -- go to serve, checked up front with serve's own parser (tensorfold chat MODEL -- --parallel 1).
    • --host, --port, --api-key and --api-key-file are refused there, since the chat owns the child's address and key. --name is honoured.
  • The child answers only the chat. Each child gets a fresh key: 32 random bytes as hex, only in its environment as TENSORFOLD_API_KEY, sent as the bearer on every request including the readiness probe. It never appears in the command line or the chat's output, and a request without it gets 401. Without this, any local user could use the child's port while the chat runs.

  • --url URL [MODEL]: talks to a running server instead. Without MODEL it uses the first model /v1/models lists. When TENSORFOLD_API_KEY is set it is sent as the bearer, and a 401 names that variable.

  • Each turn: streams /v1/chat/completions and prints content as it arrives, resending the conversation.

    • Reasoning (reasoning_content, else reasoning) is hidden by default, and --show-thinking shows it dimmed.
    • --no-thinking sends chat_template_kwargs.enable_thinking: false, and is refused up front when /v1/models reports no thinking switch.
    • --system, --temperature, --max-tokens and --startup-timeout are accepted.
  • In the chat:

    • /bye or Ctrl-D ends it, /clear forgets the conversation but keeps the system prompt, /system TEXT sets it, and /help lists these.
    • Ctrl-C stops the current reply and returns to the prompt; a stopped reply keeps what it printed.
    • The child sits in its own process group, so the terminal's Ctrl-C reaches only the chat. SIGPIPE is ignored, so | head stops the child cleanly.
  • Piped stdin is sent as one message, and only the reply goes to stdout, exactly: echo hi | tensorfold chat MODEL. On a terminal, control characters other than newline and tab are dropped from replies.

  • Windows: starting a server is refused by name (the serve-child code is POSIX), and --url works. The new check-chat-windows-host step compiles it for Windows.

  • Reuse: the benchmark's serve child, SSE framing and JSON fetch are reused, not copied, extended with defaulted options for the served name, messages, lock, process group and key. The benchmark's own child and output bytes are unchanged, and a new test confirms its child still sees no key.

  • Issue: n/a. This came from the 16 Nov check of competitor gap docs against main (ollama run, MLC, llamafile, Locals and MNN all ship a terminal chat); no issue or PR covered it.

  • Stacked on: main. The router (serve --router, in progress) will stack on this, since both extend the benchmark's serve child.

  • atc overlap --json: to run after review.

Checks on this head

  • Head commit: 762c91e on main e7e2424.
  • Lean checker: 337 on base and head, 0 new (tools/pr_check.py: 0 errors, 1 advisory warning, the PR number).
  • Tests:
    • zig build test-chat-cli-host test-benchmark-cancel-host test-benchmark-publication-host test-benchmark-routing-host test-thinking-benchmark-host check-chat-windows-host test-windows-port-host test-golden test-server-cpu: 37/37 steps, 1667/1676 tests (9 skipped). Within that, test-chat-cli-host 45/45 and test-server-cpu 1503 pass.
    • zig fmt --check, git diff --check: clean.
  • Negative controls:
    • Spawning the child without its own process group fails the process-group test.
    • A parser that drops a line cut across reads fails 5 tests.
    • Referencing the POSIX signal installer breaks the Windows compile check.
    • Removing the bearer from chat requests fails both key tests.
  • By hand, with the engine-less CLI and the CPU fake server:
    • Piped mode, --show-thinking, --no-thinking and UTF-8 split across chunks all work.
    • A failed own-server start prints the server's last log line and leaves no log file.
    • In a pseudo-terminal, Ctrl-C stops a reply in 0.01 s and /system and /bye work.
    • With the CPU fake server keyed, /v1/models is 401 without the key and 200 with it, and both --url messages appear.
  • CI: pending.
  • Not run: a real checkpoint (the Metal native binary wasn't built), Linux (its test-host sub-steps ran on macOS), and Windows beyond the compile check.

Receipt

n/a: a CLI client over the existing HTTP API, which can't change what the engine computes. Host checks are above.

Checklist

  • The title and description match the head commit.

  • New tests fail before the change, pass after it, and skip cleanly without their dependency.

  • Comments and docstrings are one line. No measurements or history in the source.

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

  • Synthena (Ash's Agent)

ajensenwaud and others added 11 commits November 19, 2026 21:31
…g, answer and tool calls

A tokenizer with <|start|>, <|message|>, <|eom|> and <|eot|> as single tokens and a chat template that writes
to=self messages (Muse Spark Open's) replies as messages to recipients. self is reasoning_content; user, commentary
and no recipient are content; any other recipient is a tool call whose <atem:function_calls> block becomes the
<tool_call><function=...> markup tool_parse and tool_stream already read, so its arguments take the tools' schema
types. While a reply streams, an unfinished header, a call message that has not closed and a partial marker are held
back, so every split extends the last and the finished split extends them all; the finished reply feeds the call
streamer once more, so a last call ended by the end token streams too. Harmony's tokenizers have no <|eom|> or
<|eot|>, so gpt-oss replies keep their channel split; tests cover both formats whole and streamed.
…ompt's open message read, thinking off kept off

A call message max_tokens cuts stays open as far as it was written, named by its header until its invoke names the
tool, so tool_stream reads it as the reply's cut call, whole and streamed, as #1299 made a GLM call; content never
shows its markup. A stop string inside a call keeps the call's text, as the other formats keep theirs. The reply goes
on from the prompt's last unclosed <|start|> header, so a generation prompt that already opened to=self<|message|>
keeps the reasoning after it; Muse Spark Open's template ends at <|start|>assistant, with or without reasoning_effort.
Only calls to offered tools become <tool_call> markup: with no tools, or a tool not offered, a call message stays as
the model wrote it, as DSML, LFM2 and Qwen's XML calls do. Thinking off no longer turns the split's reasoning on: self
messages stay out of the reply, and the template, which has no thinking switch, hears its lowest reasoning effort
rather than its default (xhigh). Raw completion prompts keep the model's text. Tests cut a call before its invoke
names the tool, inside a value, a value's closer, a parameter's name, between parameters, after the invoke closed,
and in a call's header, at strides 1, 3, 7 and whole; and cover an opened prompt, no tools, an unoffered tool and
thinking off.
…e the last call; a reply with no markup streams as it comes
With main's loop-guard stop, chat.zig plus the recipient-reply hook reached 603 lines, past lean's 600. flag (Python's
bool(fields.get(key))) is a fields helper, so it moves there unchanged; chat.zig calls fields_mod.flag.
Move the existing probe body unchanged to a sibling module and keep its call through an alias.
…ommand's served name, words, lock and user agent
…e child of its own or a running server's --url
… its environment; --url sends TENSORFOLD_API_KEY
@ashhart
ashhart merged commit bd8b087 into main Nov 19, 2026
7 of 16 checks passed
@ashhart
ashhart deleted the lead/cli-chat branch November 19, 2026 21:32
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner Author

Thank you @ajensenwaud. This is merged into main with your commits under your own name, so you're credited as a TensorFold contributor.

ashhart added a commit to hobbesd/TensorFold that referenced this pull request Nov 19, 2026
ashhart added a commit that referenced this pull request Nov 19, 2026
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.

2 participants