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>
60 lines
2.3 KiB
Dart
60 lines
2.3 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
/// Resolve the per-workspace Unix-domain socket path served by the
|
|
/// running clide app. Per D-70:
|
|
///
|
|
/// Linux: `$XDG_RUNTIME_DIR/clide/<hash>.sock`
|
|
/// macOS: `$HOME/Library/Caches/clide/<hash>.sock`
|
|
///
|
|
/// The C `clide` client and any other consumer derive the same path
|
|
/// from the same workspace root, so server + client always agree
|
|
/// without configuration.
|
|
String workspaceSocketPath(String workspaceRoot) {
|
|
final dir = socketDirectory();
|
|
return '$dir/${_hash(workspaceRoot)}.sock';
|
|
}
|
|
|
|
/// Parent directory that holds every per-workspace socket for this
|
|
/// user. Created with `0700` on bind (see D-71). Exposed separately
|
|
/// so the server can prepare/perm-fix the directory before binding.
|
|
String socketDirectory() {
|
|
if (Platform.isMacOS) {
|
|
final home = Platform.environment['HOME'] ?? '/tmp';
|
|
return '$home/Library/Caches/clide';
|
|
}
|
|
final xdg = Platform.environment['XDG_RUNTIME_DIR'];
|
|
final base = (xdg != null && xdg.isNotEmpty) ? xdg : '/tmp';
|
|
return '$base/clide';
|
|
}
|
|
|
|
/// 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)
|
|
}
|
|
// 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);
|