@indigoai-us/hq-cli 5.71.0 → 5.72.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/dist/bin/hq-auth-refresh.js +1 -4
  2. package/dist/cli-version.js +1 -4
  3. package/dist/commands/__fixtures__/make-tar.js +1 -4
  4. package/dist/commands/_patterns.js +1 -4
  5. package/dist/commands/add.js +1 -4
  6. package/dist/commands/agents.js +1 -4
  7. package/dist/commands/api-keys.js +1 -4
  8. package/dist/commands/auth.js +1 -4
  9. package/dist/commands/billing.js +1 -4
  10. package/dist/commands/channels.js +1 -4
  11. package/dist/commands/cloud-demote.js +1 -4
  12. package/dist/commands/cloud-provision.js +1 -4
  13. package/dist/commands/cloud.js +1 -4
  14. package/dist/commands/company.js +1 -4
  15. package/dist/commands/creators.js +1 -4
  16. package/dist/commands/crm.js +1 -4
  17. package/dist/commands/db-migrate.js +1 -4
  18. package/dist/commands/db-provision.js +1 -4
  19. package/dist/commands/db-sql.js +1 -4
  20. package/dist/commands/db-status.js +1 -4
  21. package/dist/commands/db.js +1 -4
  22. package/dist/commands/dm.js +1 -4
  23. package/dist/commands/feedback.js +1 -4
  24. package/dist/commands/files-browse.js +1 -4
  25. package/dist/commands/files.js +1 -4
  26. package/dist/commands/group-grants.js +1 -4
  27. package/dist/commands/groups.js +1 -4
  28. package/dist/commands/integrations.js +1 -4
  29. package/dist/commands/list.js +1 -4
  30. package/dist/commands/login.js +1 -4
  31. package/dist/commands/logout.js +1 -4
  32. package/dist/commands/mcp-registration.js +1 -4
  33. package/dist/commands/mcp-status.js +1 -4
  34. package/dist/commands/meetings.js +1 -4
  35. package/dist/commands/members.js +1 -4
  36. package/dist/commands/onboard-identity-guard.js +1 -4
  37. package/dist/commands/onboard-join.js +1 -4
  38. package/dist/commands/onboard-warning.js +1 -4
  39. package/dist/commands/onboard.js +1 -4
  40. package/dist/commands/outposts.d.ts +23 -1
  41. package/dist/commands/outposts.js +172 -8
  42. package/dist/commands/pack-install.js +1 -4
  43. package/dist/commands/packs.js +1 -4
  44. package/dist/commands/people.js +1 -4
  45. package/dist/commands/pkg-install.js +1 -4
  46. package/dist/commands/pkg-list.js +1 -4
  47. package/dist/commands/pkg-remove.js +1 -4
  48. package/dist/commands/pkg-update.js +1 -4
  49. package/dist/commands/publish.js +1 -4
  50. package/dist/commands/reindex.js +1 -4
  51. package/dist/commands/rescue.js +1 -4
  52. package/dist/commands/run.js +1 -4
  53. package/dist/commands/safe-extract.js +1 -4
  54. package/dist/commands/secrets-scope.js +1 -4
  55. package/dist/commands/secrets.js +1 -4
  56. package/dist/commands/signals.js +1 -4
  57. package/dist/commands/skill.js +1 -4
  58. package/dist/commands/sources.js +1 -4
  59. package/dist/commands/sync-mode.js +1 -4
  60. package/dist/commands/sync-narrow.js +1 -4
  61. package/dist/commands/sync.js +1 -4
  62. package/dist/commands/team-sync.js +1 -4
  63. package/dist/commands/update.js +1 -4
  64. package/dist/commands/whoami.js +1 -4
  65. package/dist/commands/workers.js +1 -4
  66. package/dist/index.js +1 -4
  67. package/dist/lib/db/control-plane.js +1 -4
  68. package/dist/lib/db/local.js +1 -4
  69. package/dist/lib/db/migrate.js +1 -4
  70. package/dist/lib/db/paths.js +1 -4
  71. package/dist/lib/db/remote-engine.js +1 -4
  72. package/dist/lib/db/remote-sql.js +1 -4
  73. package/dist/lib/db/sql.js +1 -4
  74. package/dist/lib/local-tree-diff.js +1 -4
  75. package/dist/lib/narrow-hint-banner.js +1 -4
  76. package/dist/main.js +1 -4
  77. package/dist/node-preflight.js +1 -4
  78. package/dist/run/discover-schemas.js +1 -4
  79. package/dist/run/hq-plugin.js +1 -4
  80. package/dist/sentry-before-send.js +1 -4
  81. package/dist/sentry-dsn.generated.js +1 -4
  82. package/dist/sentry.js +1 -4
  83. package/dist/strategies/link.js +1 -4
  84. package/dist/strategies/merge.js +1 -4
  85. package/dist/types.js +1 -4
  86. package/dist/utils/billing-gate.js +1 -4
  87. package/dist/utils/breadcrumb-buffer.js +1 -4
  88. package/dist/utils/cli-telemetry.js +1 -4
  89. package/dist/utils/cognito-session.js +1 -4
  90. package/dist/utils/contribution-table.js +1 -4
  91. package/dist/utils/environmental-error.js +1 -4
  92. package/dist/utils/epipe.js +1 -4
  93. package/dist/utils/feedback-diagnostics.js +1 -4
  94. package/dist/utils/feedback-screenshots.js +1 -4
  95. package/dist/utils/feedback-versions.js +1 -4
  96. package/dist/utils/git.js +1 -4
  97. package/dist/utils/integrity.js +1 -4
  98. package/dist/utils/intercepted-process-exit.js +1 -4
  99. package/dist/utils/login-provider.js +1 -4
  100. package/dist/utils/manifest.js +1 -4
  101. package/dist/utils/pack-contributions.js +1 -4
  102. package/dist/utils/people.js +1 -4
  103. package/dist/utils/registry-client.js +1 -4
  104. package/dist/utils/registry.js +1 -4
  105. package/dist/utils/sandbox-runner-client.js +1 -4
  106. package/dist/utils/secrets-cache.js +1 -4
  107. package/dist/utils/sentry-identity.js +1 -4
  108. package/dist/utils/vault-api.js +1 -4
  109. package/dist/utils/version-check.js +1 -4
  110. package/dist/utils/version-gate.js +1 -4
  111. package/package.json +3 -2
  112. package/scripts/chmod-bins.mjs +49 -0
  113. package/src/commands/outposts-replica-sync.test.ts +272 -0
  114. package/src/commands/outposts.ts +258 -5
  115. package/src/packaging.test.ts +55 -0
@@ -29,6 +29,7 @@ import * as os from "node:os";
29
29
  import * as path from "node:path";
30
30
  import * as readline from "node:readline";
31
31
  import { randomBytes } from "node:crypto";
32
+ import * as yaml from "js-yaml";
32
33
  import { loadCachedTokens } from "@indigoai-us/hq-cloud";
33
34
  import { ensureCognitoToken } from "../utils/cognito-session.js";
34
35
  import { vaultApiFetch } from "../utils/vault-api.js";
@@ -795,12 +796,246 @@ async function selfDeployOutpost(
795
796
  console.log(chalk.dim("It is unregistered and unmanaged by hq-pro (no console entry or remote management)."));
796
797
  }
797
798
 
799
+ // ---------------------------------------------------------------------------
800
+ // Hidden replica-sync command (box-side full-replica timer target)
801
+ // ---------------------------------------------------------------------------
802
+ //
803
+ // A MANAGED outpost (provisioned via `hq outposts provision`) runs this on a
804
+ // systemd timer to make the box a full replica of the operator's HQ:
805
+ // 1. hq auth refresh — freshen the seeded Cognito session
806
+ // 2. hq sync pull --personal — the personal vault (repos.yaml, policies, …)
807
+ // 3. hq rescue — re-lay the core kernel (needs companies/)
808
+ // 4. clone repos.yaml — the operator's OWN repos, gh-authed via vault
809
+ //
810
+ // Unlike `self-deploy` (which is a manual, unmanaged, all-company watcher on a
811
+ // box you own), replica-sync is GENERIC: it works for every HQ user off their
812
+ // own personal vault — no per-operator setup script. It is deliberately
813
+ // best-effort end to end: a repeating timer must never leave the systemd unit
814
+ // in `failed` just because one transient step (a mirror blip, a not-yet-signed
815
+ // box) didn't complete this cycle.
816
+
817
+ /**
818
+ * The extra local surface `replica-sync` needs on top of the self-deploy set:
819
+ * two filesystem probes for the repos clone. Kept as an extension so the
820
+ * `self-deploy` dependency surface stays minimal.
821
+ */
822
+ export interface ReplicaSyncDependencies extends SelfDeployDependencies {
823
+ pathExists: (p: string) => boolean;
824
+ mkdirp: (p: string) => void;
825
+ }
826
+
827
+ const defaultReplicaSyncDependencies: ReplicaSyncDependencies = {
828
+ ...defaultSelfDeployDependencies,
829
+ pathExists: (p) => fs.existsSync(p),
830
+ mkdirp: (p) => {
831
+ fs.mkdirSync(p, { recursive: true });
832
+ },
833
+ };
834
+
835
+ /** One repo entry resolved from `personal/data/repos.yaml`. */
836
+ export interface ReplicaRepo {
837
+ url: string;
838
+ visibility: "public" | "private";
839
+ name: string;
840
+ }
841
+
842
+ function repoNameFromUrl(url: string): string {
843
+ const tail = url.split("/").pop() ?? "";
844
+ return tail.replace(/\.git$/, "").trim();
845
+ }
846
+
847
+ /**
848
+ * A repo `name` becomes a path segment under repos/<visibility>/. A synced
849
+ * repos.yaml is operator data, but a malformed or hostile name must never
850
+ * escape that directory, so constrain it to a single, non-traversal component.
851
+ */
852
+ function isSafePathComponent(value: string): boolean {
853
+ return (
854
+ value.length > 0 &&
855
+ !value.includes("/") &&
856
+ !value.includes("\\") &&
857
+ value !== "." &&
858
+ value !== ".."
859
+ );
860
+ }
861
+
862
+ /**
863
+ * Parse `personal/data/repos.yaml` into a clone list. Pure + exported for
864
+ * tests. Mirrors setup.sh's `clone_repos`: `visibility` defaults to public,
865
+ * `name` defaults to the URL basename. Malformed entries are skipped, never
866
+ * thrown.
867
+ */
868
+ export function parseReposManifest(yamlText: string): ReplicaRepo[] {
869
+ let doc: unknown;
870
+ try {
871
+ doc = yaml.load(yamlText);
872
+ } catch {
873
+ return [];
874
+ }
875
+ const rawRepos = (doc as { repos?: unknown } | null)?.repos;
876
+ if (!Array.isArray(rawRepos)) return [];
877
+
878
+ const out: ReplicaRepo[] = [];
879
+ for (const entry of rawRepos) {
880
+ if (!entry || typeof entry !== "object") continue;
881
+ const e = entry as Record<string, unknown>;
882
+ const url = typeof e.url === "string" ? e.url.trim() : "";
883
+ if (!url) continue;
884
+ const visibility = e.visibility === "private" ? "private" : "public";
885
+ const name =
886
+ typeof e.name === "string" && e.name.trim()
887
+ ? e.name.trim()
888
+ : repoNameFromUrl(url);
889
+ if (!isSafePathComponent(name)) continue;
890
+ out.push({ url, visibility, name });
891
+ }
892
+ return out;
893
+ }
894
+
895
+ /**
896
+ * Best-effort spawn: log a one-line outcome and carry on. NEVER throws and
897
+ * NEVER echoes child output — the same secret-safety stance as `runChecked`,
898
+ * since these children (auth refresh, secrets exec) touch credentials.
899
+ */
900
+ function runBestEffort(
901
+ deps: ReplicaSyncDependencies,
902
+ command: string,
903
+ args: string[],
904
+ description: string,
905
+ options?: Parameters<typeof spawnSync>[2],
906
+ ): boolean {
907
+ let result: ReturnType<typeof spawnSync>;
908
+ try {
909
+ result = deps.spawnSync(command, args, options ?? { stdio: "inherit" });
910
+ } catch {
911
+ console.warn(`replica-sync: ${description} could not run — continuing.`);
912
+ return false;
913
+ }
914
+ if (!result || result.error || result.status !== 0) {
915
+ console.warn(`replica-sync: ${description} did not complete cleanly — continuing.`);
916
+ return false;
917
+ }
918
+ return true;
919
+ }
920
+
921
+ /**
922
+ * Authenticate `gh` for private clones by streaming GITHUB_TOKEN through the
923
+ * vault into the child's ENV ONLY, unsetting it in-shell, and piping it to
924
+ * `gh auth login --with-token` over stdin. The token never touches argv or
925
+ * this process's logs. Mirrors personal/scripts/setup.sh's `configure_gh`.
926
+ */
927
+ function authGitHubViaVault(deps: ReplicaSyncDependencies): void {
928
+ const ghLogin = [
929
+ 'TOKEN="$GITHUB_TOKEN"',
930
+ "unset GITHUB_TOKEN",
931
+ 'printf "%s" "$TOKEN" | gh auth login --with-token',
932
+ ].join("\n");
933
+ const authed = runBestEffort(
934
+ deps,
935
+ "hq",
936
+ ["secrets", "--personal", "exec", "--only", "GITHUB_TOKEN", "--", "bash", "-c", ghLogin],
937
+ "GitHub auth via vault",
938
+ );
939
+ if (authed) {
940
+ // Let plain `git clone https://github.com/...` reuse gh's token so private
941
+ // repos don't prompt for a username/password.
942
+ runBestEffort(deps, "gh", ["auth", "setup-git"], "gh auth setup-git");
943
+ }
944
+ }
945
+
946
+ /** Clone any repos from `personal/data/repos.yaml` that aren't on disk yet. */
947
+ function replicateRepos(deps: ReplicaSyncDependencies, hqRoot: string): void {
948
+ const manifestPath = path.join(hqRoot, "personal", "data", "repos.yaml");
949
+ let manifestText: string;
950
+ try {
951
+ manifestText = deps.readTextFile(manifestPath);
952
+ } catch {
953
+ console.log("replica-sync: no personal/data/repos.yaml — skipping repo clone.");
954
+ return;
955
+ }
956
+
957
+ const repos = parseReposManifest(manifestText);
958
+ if (repos.length === 0) {
959
+ console.log("replica-sync: repos.yaml lists no repos — skipping.");
960
+ return;
961
+ }
962
+
963
+ const missing = repos.filter(
964
+ (repo) =>
965
+ !deps.pathExists(path.join(hqRoot, "repos", repo.visibility, repo.name, ".git")),
966
+ );
967
+ if (missing.length === 0) {
968
+ console.log("replica-sync: all repos already present.");
969
+ return;
970
+ }
971
+
972
+ // Private repos need gh auth first; public-only manifests skip the vault call.
973
+ if (missing.some((repo) => repo.visibility === "private")) {
974
+ authGitHubViaVault(deps);
975
+ }
976
+
977
+ for (const repo of missing) {
978
+ deps.mkdirp(path.join(hqRoot, "repos", repo.visibility));
979
+ const dest = path.join(hqRoot, "repos", repo.visibility, repo.name);
980
+ console.log(`replica-sync: cloning ${repo.visibility}/${repo.name}…`);
981
+ runBestEffort(
982
+ deps,
983
+ "git",
984
+ ["clone", "--recurse-submodules", repo.url, dest],
985
+ `clone ${repo.visibility}/${repo.name}`,
986
+ );
987
+ }
988
+ }
989
+
990
+ async function replicaSyncOutpost(
991
+ opts: { hqRoot?: string },
992
+ deps: ReplicaSyncDependencies,
993
+ ): Promise<void> {
994
+ const hqRoot = opts.hqRoot ?? deps.defaultHqRoot();
995
+
996
+ // Gate: only replicate on a box that has an HQ session. A box that was never
997
+ // signed in has nothing to pull — exit cleanly so the timer stays green.
998
+ const session = deps.loadCachedTokens();
999
+ if (!session?.refreshToken) {
1000
+ console.log("replica-sync: no HQ session on this box yet — nothing to replicate.");
1001
+ return;
1002
+ }
1003
+
1004
+ // Auth refresh gates the run: if the seeded refresh token is dead, every
1005
+ // authenticated step below (pull, secrets exec) would just cascade 401s.
1006
+ // Stop the cycle cleanly instead — the timer retries next tick.
1007
+ if (!runBestEffort(deps, "hq", ["auth", "refresh"], "HQ auth refresh")) {
1008
+ console.warn(
1009
+ "replica-sync: auth refresh failed — skipping this cycle (token likely expired).",
1010
+ );
1011
+ return;
1012
+ }
1013
+ runBestEffort(
1014
+ deps,
1015
+ "hq",
1016
+ ["sync", "pull", "--personal", "--hq-root", hqRoot, "--on-conflict", "keep"],
1017
+ "personal vault pull",
1018
+ );
1019
+
1020
+ // rescue lays down the core kernel and expects companies/ to exist.
1021
+ deps.mkdirp(path.join(hqRoot, "companies"));
1022
+ runBestEffort(
1023
+ deps,
1024
+ "hq",
1025
+ ["rescue", "--hq-root", hqRoot, "--yes"],
1026
+ "HQ kernel rescue",
1027
+ );
1028
+
1029
+ replicateRepos(deps, hqRoot);
1030
+ console.log("replica-sync: complete.");
1031
+ }
1032
+
798
1033
  export function registerOutpostsCommand(
799
1034
  program: Command,
800
- selfDeployOverrides: Partial<SelfDeployDependencies> = {},
1035
+ selfDeployOverrides: Partial<ReplicaSyncDependencies> = {},
801
1036
  ): void {
802
- const selfDeployDependencies: SelfDeployDependencies = {
803
- ...defaultSelfDeployDependencies,
1037
+ const deps: ReplicaSyncDependencies = {
1038
+ ...defaultReplicaSyncDependencies,
804
1039
  ...selfDeployOverrides,
805
1040
  };
806
1041
  const outposts = program
@@ -811,15 +1046,33 @@ export function registerOutpostsCommand(
811
1046
  .command("self-deploy", { hidden: true })
812
1047
  .description("Configure this EC2 host as a locally self-hosted HQ outpost")
813
1048
  .option("--yes", "Skip the self-hosting confirmation")
814
- .option("--hq-root <path>", "HQ root to sync", selfDeployDependencies.defaultHqRoot())
1049
+ .option("--hq-root <path>", "HQ root to sync", deps.defaultHqRoot())
815
1050
  .action(async (opts: { yes?: boolean; hqRoot?: string }) => {
816
1051
  try {
817
- await selfDeployOutpost(opts, selfDeployDependencies);
1052
+ await selfDeployOutpost(opts, deps);
818
1053
  } catch (err) {
819
1054
  fail(err);
820
1055
  }
821
1056
  });
822
1057
 
1058
+ outposts
1059
+ .command("replica-sync", { hidden: true })
1060
+ .description(
1061
+ "Replicate your full HQ onto this box (personal pull + core rescue + repos). Runs on a timer.",
1062
+ )
1063
+ .option("--hq-root <path>", "HQ root to replicate into", deps.defaultHqRoot())
1064
+ .action(async (opts: { hqRoot?: string }) => {
1065
+ // Best-effort background job: never hard-fail the systemd unit on a
1066
+ // transient step. Swallow, log, and exit 0.
1067
+ try {
1068
+ await replicaSyncOutpost(opts, deps);
1069
+ } catch (err) {
1070
+ console.warn(
1071
+ `replica-sync: unexpected error — ${err instanceof Error ? err.message : String(err)}`,
1072
+ );
1073
+ }
1074
+ });
1075
+
823
1076
  outposts
824
1077
  .command("provision")
825
1078
  .alias("create")
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Packaging guard: every declared `bin` must ship EXECUTABLE.
3
+ *
4
+ * Regression pin for the "outpost is not CLI-ready" class of failure: `tsc`
5
+ * emits dist bins as 0644, and a root global install on a fresh outpost has
6
+ * been observed to leave the linked target non-executable, so `/usr/bin/hq`
7
+ * dies with "Permission denied" and the box never becomes usable. The build
8
+ * runs `scripts/chmod-bins.mjs` to 0755 the bins; this test locks that wiring
9
+ * in and, when a build has already produced `dist/`, asserts the bits directly.
10
+ */
11
+ import { existsSync, readFileSync, statSync } from "node:fs";
12
+ import { dirname, resolve } from "node:path";
13
+ import { fileURLToPath } from "node:url";
14
+ import { describe, expect, it } from "vitest";
15
+
16
+ const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
17
+ const pkg = JSON.parse(
18
+ readFileSync(resolve(repoRoot, "package.json"), "utf8"),
19
+ ) as {
20
+ bin?: Record<string, string> | string;
21
+ scripts?: Record<string, string>;
22
+ };
23
+
24
+ const binMap =
25
+ typeof pkg.bin === "string" ? { hq: pkg.bin } : (pkg.bin ?? {});
26
+
27
+ describe("packaging: bin executable bit", () => {
28
+ it("wires the chmod-bins step into build and prepublishOnly", () => {
29
+ expect(pkg.scripts?.build ?? "").toContain("chmod-bins");
30
+ // prepublishOnly must rebuild so a publish always re-chmods, regardless of
31
+ // the publisher's local dist state.
32
+ expect(pkg.scripts?.prepublishOnly ?? "").toContain("build");
33
+ });
34
+
35
+ it("declares at least one bin, all pointing at built .js files", () => {
36
+ const entries = Object.entries(binMap);
37
+ expect(entries.length).toBeGreaterThan(0);
38
+ for (const [, relPath] of entries) {
39
+ expect(relPath.endsWith(".js")).toBe(true);
40
+ }
41
+ });
42
+
43
+ it("ships every bin executable once dist is built", () => {
44
+ for (const [name, relPath] of Object.entries(binMap)) {
45
+ const target = resolve(repoRoot, relPath);
46
+ // Pre-build (e.g. a bare `vitest` before `tsc`) → nothing to assert yet.
47
+ if (!existsSync(target)) continue;
48
+ const mode = statSync(target).mode;
49
+ // Owner-executable bit is the one that matters for `npm` bin linking.
50
+ expect(mode & 0o100, `${name} (${relPath}) must be executable`).toBe(
51
+ 0o100,
52
+ );
53
+ }
54
+ });
55
+ });