T-126: C clide shell client + _argv unwrap in the dispatcher
Third slice of T-99. After this `clide status` actually does
something when typed in a shell.
* native/clide-cli/clide.c — ~250 LOC C. Walks CWD up to .git,
hashes the workspace root with FNV-1a 64-bit (byte-for-byte
identical to the Dart side, pinned via reference vectors in
paths_test.dart), opens the per-workspace socket, and ships argv
across the wire as `{cmd:"_argv", args:{argv:[...]}}`.
* lib/src/cli/argv_dispatch.dart — registers the `_argv` sentinel
command on the dispatcher. The handler runs the T-125 parser on
the embedded argv and either re-dispatches the unwrapped request
through the same dispatcher or returns the pre-built error
response. Keeps the parser in Dart so the C side stays dumb.
* lib/src/ipc/paths.dart — fnv1a64Hex hoisted to a public helper +
fixed to format as unsigned (Dart `int` is signed int64; the high
bit lit a leading minus that broke the cross-language compare).
Reference-vector tests added against the FNV reference.
* `make clide-cli` builds it via the host `cc`; output lands at
native/<platform>/clide and is gitignored. Test
test/cli/clide_cli_e2e_test.dart compiles + exercises the full
round-trip; skips cleanly when no cc is on PATH.
* CONTRIBUTING.md gets a "C clide shell client" section.
T-128 (delete legacy IPC) unblocked.
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/// Register the `_argv` sentinel command on a [DaemonDispatcher].
|
||||
///
|
||||
/// The C client (T-126) doesn't know the dispatcher's command surface
|
||||
/// — it ships raw argv across the wire under cmd `_argv`. This handler
|
||||
/// runs [parseArgv] on the embedded argv and either dispatches the
|
||||
/// resulting [IpcRequest] or returns the pre-built error response.
|
||||
///
|
||||
/// Why a sentinel cmd rather than a top-level parse step in the IPC
|
||||
/// server: keeps the server transport-agnostic — every consumer that
|
||||
/// already has a typed [IpcRequest] goes the direct path; only the
|
||||
/// CLI's raw-argv envelope hits this unwrap shim.
|
||||
library;
|
||||
|
||||
import 'package:clide/src/cli/argv_to_request.dart';
|
||||
import 'package:clide/src/daemon/dispatcher.dart';
|
||||
import 'package:clide/src/ipc/envelope.dart';
|
||||
import 'package:clide/src/ipc/schema_v1.dart';
|
||||
|
||||
/// Sentinel command id the C `clide` client sends. Anything else
|
||||
/// goes through the normal dispatcher path unchanged.
|
||||
const String argvSentinelCmd = '_argv';
|
||||
|
||||
/// Wire the `_argv` sentinel handler onto [dispatcher]. The handler:
|
||||
/// 1. Extracts `args.argv` as a List<String>.
|
||||
/// 2. Calls [parseArgv].
|
||||
/// 3. If parsed → re-dispatches the inner request through the
|
||||
/// *same* dispatcher (so per-handler logic runs once).
|
||||
/// 4. If error → returns the pre-built [IpcResponse] verbatim,
|
||||
/// patched with the outer request id so the client correlates.
|
||||
void registerArgvUnwrap(DaemonDispatcher dispatcher) {
|
||||
dispatcher.register(argvSentinelCmd, (outer) async {
|
||||
final raw = outer.args['argv'];
|
||||
if (raw is! List) {
|
||||
return IpcResponse.err(
|
||||
id: outer.id,
|
||||
error: IpcError(
|
||||
code: IpcExitCode.userError,
|
||||
kind: IpcErrorKind.userError,
|
||||
message: '_argv requires args.argv to be a JSON array',
|
||||
),
|
||||
);
|
||||
}
|
||||
final argv = raw.cast<String>();
|
||||
final result = parseArgv(argv, requestId: outer.id);
|
||||
return switch (result) {
|
||||
ArgvParsed(:final request) => dispatcher.dispatch(request),
|
||||
ArgvError(:final response) => response,
|
||||
};
|
||||
});
|
||||
}
|
||||
+28
-23
@@ -1,3 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
/// Resolve the per-workspace Unix-domain socket path served by the
|
||||
@@ -27,28 +28,32 @@ String socketDirectory() {
|
||||
return '$base/clide';
|
||||
}
|
||||
|
||||
/// FNV-1a 64-bit, lower-case hex, fixed 16 chars. Matches the shape
|
||||
/// used by `lib/builtin/claude/src/session_naming.dart#_hash`. Not a
|
||||
/// cryptographic hash — D-70 explains why one isn't needed here.
|
||||
String _hash(String s) {
|
||||
// 0xcbf29ce484222325 as two 32-bit halves to dodge JS-precision
|
||||
// issues if this file ever runs under the web target.
|
||||
var hiHi = 0xcbf2, hiLo = 0x9ce4;
|
||||
var loHi = 0x8422, loLo = 0x2325;
|
||||
const primeHiHi = 0x0000, primeHiLo = 0x0100;
|
||||
const primeLoHi = 0x0000, primeLoLo = 0x01b3;
|
||||
for (var i = 0; i < s.length; i++) {
|
||||
loLo ^= s.codeUnitAt(i) & 0xffff;
|
||||
// 64-bit multiply, hand-rolled across four 16-bit limbs.
|
||||
final r0 = loLo * primeLoLo;
|
||||
final r1 = (loLo * primeLoHi) + (loHi * primeLoLo) + (r0 >> 16);
|
||||
final r2 = (loLo * primeHiLo) + (loHi * primeLoHi) + (hiLo * primeLoLo) + (r1 >> 16);
|
||||
final r3 = (loLo * primeHiHi) + (loHi * primeHiLo) + (hiLo * primeLoHi) + (hiHi * primeLoLo) + (r2 >> 16);
|
||||
loLo = r0 & 0xffff;
|
||||
loHi = r1 & 0xffff;
|
||||
hiLo = r2 & 0xffff;
|
||||
hiHi = r3 & 0xffff;
|
||||
/// FNV-1a 64-bit hash of [s] as a 16-char lower-case hex string.
|
||||
/// The C client (T-126) reproduces the same algorithm byte-for-byte
|
||||
/// so server + client always agree on socket path. Not cryptographic
|
||||
/// — D-70 explains why one isn't needed here. The algorithm:
|
||||
///
|
||||
/// h = 0xcbf29ce484222325 // FNV offset basis
|
||||
/// for each byte b in utf-8(s):
|
||||
/// h = (h xor b) * 0x100000001b3 mod 2^64 // FNV prime, 64-bit wrap
|
||||
///
|
||||
/// Reference: <http://isthe.com/chongo/tech/comp/fnv/> — FNV-1a 64-bit.
|
||||
String fnv1a64Hex(String s) {
|
||||
// Desktop-only (the IPC server is desktop-only per D-56). Dart VM
|
||||
// ints are 64-bit; arithmetic wraps modulo 2^64 naturally.
|
||||
var h = 0xcbf29ce484222325;
|
||||
const prime = 0x100000001b3;
|
||||
final bytes = utf8.encode(s);
|
||||
for (final b in bytes) {
|
||||
h ^= b;
|
||||
h = h * prime; // wraps mod 2^64 on the VM (signed int64)
|
||||
}
|
||||
String hex4(int v) => v.toRadixString(16).padLeft(4, '0');
|
||||
return '${hex4(hiHi)}${hex4(hiLo)}${hex4(loHi)}${hex4(loLo)}';
|
||||
// Dart's `int` is signed 64-bit on the VM; once the high bit lights
|
||||
// up, `toRadixString` would emit a leading minus. Split into two
|
||||
// unsigned 32-bit halves (>>> is logical shift) and concatenate.
|
||||
final hi = (h >>> 32) & 0xffffffff;
|
||||
final lo = h & 0xffffffff;
|
||||
return '${hi.toRadixString(16).padLeft(8, '0')}${lo.toRadixString(16).padLeft(8, '0')}';
|
||||
}
|
||||
|
||||
String _hash(String s) => fnv1a64Hex(s);
|
||||
|
||||
Reference in New Issue
Block a user