talentos 1.4.6 → 1.4.8

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.
@@ -3,10 +3,15 @@
3
3
  export const ROOM_LOCAL_TOOLS = ["run_command"];
4
4
  export const ROOM_TOOLS = [
5
5
  "list_rooms",
6
+ "list_threads",
7
+ "reply_route",
8
+ "read_thread",
6
9
  "read_room",
10
+ "write_room_memory",
7
11
  "list_room_files",
8
12
  "read_file",
9
13
  "read_app_state",
14
+ "write_app_state",
10
15
  "get_skill",
11
16
  "suggest_change",
12
17
  "create_file",
@@ -17,6 +22,7 @@ export const ROOM_TOOLS = [
17
22
  "prepare_upload",
18
23
  "commit_upload",
19
24
  "post_message",
25
+ "post_to_thread",
20
26
  "share_link",
21
27
  "whoami",
22
28
  ];
@@ -123,14 +123,20 @@ export function protectedReason(path, mode, home, guard = protectedPaths(home))
123
123
  WRITE_PROTECTED_NAMES.includes(fold(name))) {
124
124
  return "provider, hook or MCP settings";
125
125
  }
126
- const gitAt = segments.lastIndexOf(".git");
127
- if (gitAt >= 0 &&
128
- (segments[gitAt + 1] === "hooks" || segments[gitAt + 1] === "config")) {
129
- return "git hooks or config";
126
+ if (inGitDir(path)) {
127
+ return "git hooks, config or a git directory";
130
128
  }
131
129
  }
132
130
  return;
133
131
  }
132
+ /**
133
+ * A `.git` file or folder, or anything inside one. Its config and hooks pick
134
+ * the programs git runs and the URL a push reaches, and a `.git` file points
135
+ * git at another folder (#12926).
136
+ */
137
+ export function inGitDir(path) {
138
+ return path.split(sep).some((segment) => fold(segment) === ".git");
139
+ }
134
140
  /** True when `dir` contains a protected path, so a recursive read from it would reach it. */
135
141
  export function reachesProtected(dir, home, guard = protectedPaths(home)) {
136
142
  return guard.readWrite.some((entry) => isInside(entry, dir));
@@ -1,5 +1,7 @@
1
+ import { existsSync, statSync } from "node:fs";
2
+ import { basename, dirname, join } from "node:path";
1
3
  import { ROOM_LOCAL_TOOLS, ROOM_TOOLS } from "../codex-policy.js";
2
- import { canonical, isInside, protectedPaths, protectedReason, reachesProtected, } from "./paths.js";
4
+ import { canonical, inGitDir, isInside, protectedPaths, protectedReason, reachesProtected, } from "./paths.js";
3
5
  import { parseShell } from "./shell.js";
4
6
  const NON_ALNUM = /[^A-Za-z\d]+/u;
5
7
  const GREP_R_DIFF = /^-\w*r|^--recursive/u;
@@ -498,7 +500,7 @@ const GIT_GLOBAL_OK = new Set([
498
500
  "--no-pager",
499
501
  "-P",
500
502
  ]);
501
- const GIT_DIR_FLAG = /^--(?:git-dir|work-tree)=(.+)$/u;
503
+ const GIT_DIR_FLAG = /^(--(?:git-dir|work-tree))=(.+)$/u;
502
504
  // `git [globals] <sub> ...`: only the globals above are understood.
503
505
  function splitGit(args) {
504
506
  const dirs = [];
@@ -510,14 +512,17 @@ function splitGit(args) {
510
512
  if (GIT_GLOBAL_OK.has(word.text)) {
511
513
  index += 1;
512
514
  }
513
- else if (attached?.[1]) {
514
- dirs.push({ chdir: false, word: { glob: word.glob, text: attached[1] } });
515
+ else if (attached?.[1] && attached[2]) {
516
+ dirs.push({
517
+ flag: attached[1],
518
+ word: { glob: word.glob, text: attached[2] },
519
+ });
515
520
  index += 1;
516
521
  }
517
522
  else if (["-C", "--git-dir", "--work-tree"].includes(word.text) &&
518
523
  next &&
519
524
  !isFlag(next)) {
520
- dirs.push({ chdir: word.text === "-C", word: next });
525
+ dirs.push({ flag: word.text, word: next });
521
526
  index += 2;
522
527
  }
523
528
  else {
@@ -569,15 +574,18 @@ function classifyGit(args, ctx) {
569
574
  // `-C dir` and friends read that path: the same check as `cat dir/x`. Later
570
575
  // paths resolve against the directory `-C` moved into.
571
576
  let local = ctx;
572
- for (const { chdir, word } of git.dirs) {
577
+ for (const { flag, word } of git.dirs) {
573
578
  const refused = pathCheck(word, local, false);
574
579
  if (refused) {
575
580
  return refused;
576
581
  }
577
- if (chdir) {
582
+ if (flag === "-C") {
578
583
  local = { ...local, cwd: canonical(word.text, local.cwd, local.home) };
579
584
  }
580
585
  }
586
+ if (foreignRepo(gitRepoOf(git, local.cwd, ctx.home), ctx)) {
587
+ return deny(2);
588
+ }
581
589
  for (const word of rest.filter((word) => !isFlag(word))) {
582
590
  const refused = pathCheck(word, local, false);
583
591
  if (refused) {
@@ -602,30 +610,161 @@ const GIT_TIER2 = new Set([
602
610
  "switch",
603
611
  ]);
604
612
  const PROTECTED_BRANCH = /^(?:main|master|trunk|develop|dev|release|production|prod|stable)$|^release[/-]/u;
605
- const PUSH_RISKY_FLAG = /^(-f$|--force|--delete$|-d$|--mirror|--all$|--prune)/u;
613
+ // Auto answers tier 2 on its own (#12926), so each tier 2 form must be one that
614
+ // keeps work: a push of talentos/ branches to origin, a checkout or switch that
615
+ // leaves uncommitted changes alone. A force, a URL or other remote, tags, mirror,
616
+ // delete, a second refspec onto main, or a discard is tier 1.
617
+ const PUSH_SAFE_FLAG = /^(?:-[quv]+|--set-upstream|--quiet|--verbose|--progress|--no-progress)$/u;
606
618
  const HEADS_PREFIX = /^refs\/heads\//u;
619
+ // Refspec and pathspec wildcards (a backslash too); `@{1}` is a plain revision.
620
+ const GIT_GLOB = /[*?[\\]/u;
621
+ const CHECKOUT_SAFE_FLAG = /^(?:-b|-t|--track|--no-track|-q|--quiet|--detach)$/u;
622
+ // `--create` asks like `-c`, which GIT_BAD_FLAG already catches.
623
+ const SWITCH_SAFE_FLAG = /^(?:-d|--detach|-t|--track|--no-track|-q|--quiet|--guess|--no-guess)$/u;
624
+ // Clone options that set config or hooks, or run a program.
625
+ const CLONE_BAD_FLAG = /^(?:-\w*[cu]|--config|--template|--upload-pack|--separate-git-dir)/u;
626
+ const GIT_DIR_PARTS = ["HEAD", "objects", "refs"];
627
+ // Git work that touches only `.git` and the network, never the folders around it.
628
+ const TRAILING_SLASHES = /[/\\]+$/u;
629
+ const URL_PATH_SEPARATOR = /[/:]/u;
630
+ const GIT_SUFFIX = /\.git$/u;
631
+ const GIT_KEEPS_TREE = new Set(["add", "commit", "fetch", "push"]);
632
+ const RESTORE_SAFE_FLAG = /^(?:-S|--staged|-q|--quiet|--)$/u;
633
+ const STASH_KEEPS = new Set([
634
+ "apply",
635
+ "branch",
636
+ "create",
637
+ "list",
638
+ "pop",
639
+ "push",
640
+ "save",
641
+ "show",
642
+ "store",
643
+ ]);
607
644
  const FILE_UTILS = new Set(["cp", "mkdir", "mv", "touch"]);
608
- function gitPushTier(args) {
609
- const rest = args.slice(1);
610
- const flags = rest.filter(isFlag).map((word) => word.text);
611
- if (flags.some((flag) => PUSH_RISKY_FLAG.test(flag))) {
612
- return 1;
645
+ const TARGET_DIR_FLAG = /^-\w*[tT]|^--(?:no-)?target-directory/u;
646
+ function pushesToThread(refspec) {
647
+ if (refspec.glob || GIT_GLOB.test(refspec.text)) {
648
+ return false;
613
649
  }
614
- const positional = rest
615
- .filter((word) => !isFlag(word))
616
- .map((word) => word.text);
617
- const refspec = positional[1];
618
- if (!refspec || refspec.startsWith("+") || refspec.startsWith(":")) {
619
- return 1;
620
- }
621
- const target = refspec.includes(":")
622
- ? (refspec.split(":")[1] ?? "")
623
- : refspec;
650
+ const [source = "", target = source, ...extra] = refspec.text.split(":");
624
651
  const branch = target.replace(HEADS_PREFIX, "");
625
- return branch.startsWith("talentos/") && !PROTECTED_BRANCH.test(branch)
652
+ return (source !== "" &&
653
+ !source.startsWith("+") &&
654
+ extra.length === 0 &&
655
+ branch.startsWith("talentos/") &&
656
+ !PROTECTED_BRANCH.test(branch));
657
+ }
658
+ function gitPushTier(rest) {
659
+ const [remote, ...refspecs] = rest.filter((word) => !isFlag(word));
660
+ // Only `origin` is the owner's own remote; a URL, a path or another name
661
+ // could be anyone's.
662
+ return rest.filter(isFlag).every((flag) => PUSH_SAFE_FLAG.test(flag.text)) &&
663
+ remote?.text === "origin" &&
664
+ refspecs.length > 0 &&
665
+ refspecs.every(pushesToThread)
626
666
  ? 2
627
667
  : 1;
628
668
  }
669
+ // `git checkout src/a.ts` and `git checkout talentos/a` read the same; a word
670
+ // that names a file, a pathspec or a glob is the discard form.
671
+ function namesPath(word, cwd, home) {
672
+ return (word.glob ||
673
+ GIT_GLOB.test(word.text) ||
674
+ word.text.startsWith(":") ||
675
+ existsSync(canonical(word.text, cwd, home)));
676
+ }
677
+ function gitKeepsWork(sub, rest, cwd, home) {
678
+ const flags = rest.filter(isFlag).map((word) => word.text);
679
+ switch (sub) {
680
+ case "checkout":
681
+ return rest.every((word) => isFlag(word)
682
+ ? CHECKOUT_SAFE_FLAG.test(word.text)
683
+ : !namesPath(word, cwd, home));
684
+ case "switch":
685
+ return flags.every((flag) => SWITCH_SAFE_FLAG.test(flag));
686
+ case "clone":
687
+ return !flags.some((flag) => CLONE_BAD_FLAG.test(flag));
688
+ case "restore":
689
+ // Without --staged, restore overwrites the working tree.
690
+ return (flags.every((flag) => RESTORE_SAFE_FLAG.test(flag)) &&
691
+ flags.some((flag) => flag === "-S" || flag === "--staged"));
692
+ case "stash": {
693
+ // A leading flag is the implicit `push`; drop and clear lose entries.
694
+ const [first] = rest;
695
+ return !first || isFlag(first) || STASH_KEEPS.has(first.text);
696
+ }
697
+ default:
698
+ return true;
699
+ }
700
+ }
701
+ // The repository git finds from `dir`: the nearest `.git` file or folder, or a
702
+ // folder that is itself a git directory (git opens it as a bare repository).
703
+ function gitRepoAt(dir) {
704
+ for (let at = dir;; at = dirname(at)) {
705
+ if (existsSync(join(at, ".git"))) {
706
+ return join(at, ".git");
707
+ }
708
+ if (GIT_DIR_PARTS.every((part) => existsSync(join(at, part)))) {
709
+ return at;
710
+ }
711
+ if (dirname(at) === at) {
712
+ return;
713
+ }
714
+ }
715
+ }
716
+ // Where git runs: each `-C` resolves from the one before.
717
+ function gitCwd(git, cwd, home) {
718
+ return git.dirs.reduce((dir, { flag, word }) => flag === "-C" ? canonical(word.text, dir, home) : dir, cwd);
719
+ }
720
+ // Every folder a clone could create: each positional word (a flag's value
721
+ // reads as one too) and the folder git names after the URL.
722
+ function cloneTargets(git, ctx) {
723
+ const words = git.rest.filter((word) => !isFlag(word));
724
+ const named = words[0]?.text
725
+ .replace(TRAILING_SLASHES, "")
726
+ .split(URL_PATH_SEPARATOR)
727
+ .pop()
728
+ ?.replace(GIT_SUFFIX, "");
729
+ const cwd = gitCwd(git, ctx.cwd, ctx.home);
730
+ return [...words, ...(named ? [{ glob: false, text: named }] : [])].map((word) => ({ ...word, text: canonical(word.text, cwd, ctx.home) }));
731
+ }
732
+ // A clone writes its folder like `cp -r`: the same protected targets.
733
+ function cloneIntoProtected(args, ctx) {
734
+ const git = splitGit(args);
735
+ return (git.sub?.text === "clone" &&
736
+ cloneTargets(git, ctx).some(({ glob, text }) => !glob &&
737
+ Boolean(protectedReason(text, "write", ctx.home, ctx.guard) ||
738
+ gitControlFile(text, ctx) ||
739
+ inGitDir(text))));
740
+ }
741
+ function gitRepoOf(git, cwd, home) {
742
+ const gitDir = git.dirs.filter(({ flag }) => flag === "--git-dir").pop();
743
+ return gitDir ? canonical(gitDir.word.text, cwd, home) : gitRepoAt(cwd);
744
+ }
745
+ // A repository inside the workspace that is not the workspace's own: one the
746
+ // model could have built, cloned or pointed at, so its config and hooks pick the
747
+ // programs git runs and the URL a push reaches (#12926).
748
+ function foreignRepo(repo, ctx) {
749
+ return (repo !== undefined &&
750
+ ctx.writeRoots.some((root) => isInside(repo, root)) &&
751
+ !ctx.writeRoots.some((root) => gitRepoAt(root) === repo));
752
+ }
753
+ // `mv`/`cp` onto an existing file replaces it: the old content is gone.
754
+ function overwrites(args, ctx) {
755
+ if (args.some((word) => isFlag(word) && TARGET_DIR_FLAG.test(word.text))) {
756
+ return true;
757
+ }
758
+ const paths = args
759
+ .filter((word) => !isFlag(word))
760
+ .map((word) => canonical(word.text, ctx.cwd, ctx.home));
761
+ const target = paths.pop();
762
+ if (!(target && paths.length > 0 && existsSync(target))) {
763
+ return false;
764
+ }
765
+ return (!statSync(target).isDirectory() ||
766
+ paths.some((path) => existsSync(join(target, basename(path)))));
767
+ }
629
768
  function shellAsk(command, ctx) {
630
769
  const [programWord, ...args] = command.argv;
631
770
  const program = programWord?.text ?? "?";
@@ -644,13 +783,28 @@ function shellAsk(command, ctx) {
644
783
  // A change to another repository is not the workspace's own work.
645
784
  const elsewhere = git.dirs.some(({ word }) => word.glob ||
646
785
  !ctx.writeRoots.some((root) => isInside(canonical(word.text, ctx.cwd, ctx.home), root)));
647
- if (!(git.ok && GIT_TIER2.has(sub)) || badFlag || elsewhere) {
786
+ // Paths after `-C` resolve from there, as in classifyGit.
787
+ const cwd = gitCwd(git, ctx.cwd, ctx.home);
788
+ // `--git-dir`/`--work-tree` or a repository the model made pick another
789
+ // config and hooks.
790
+ if (!(git.ok && GIT_TIER2.has(sub)) ||
791
+ badFlag ||
792
+ elsewhere ||
793
+ git.dirs.some(({ flag }) => flag !== "-C") ||
794
+ foreignRepo(gitRepoOf(git, cwd, ctx.home), ctx)) {
648
795
  return { kind: `git:${sub || "?"}`, tier: 1 };
649
796
  }
650
- return {
651
- kind: `git:${sub}`,
652
- tier: sub === "push" ? gitPushTier([git.sub, ...git.rest]) : 2,
653
- };
797
+ let tier = 2;
798
+ if (sub === "push") {
799
+ tier = gitPushTier(git.rest);
800
+ }
801
+ else if (!gitKeepsWork(sub, git.rest, cwd, ctx.home) ||
802
+ (sub === "clone" &&
803
+ cloneTargets(git, ctx).some((word) => word.glob ||
804
+ !ctx.writeRoots.some((root) => isInside(word.text, root))))) {
805
+ tier = 1;
806
+ }
807
+ return { kind: `git:${sub}`, tier };
654
808
  }
655
809
  if (FILE_UTILS.has(program)) {
656
810
  const inside = args
@@ -658,7 +812,8 @@ function shellAsk(command, ctx) {
658
812
  .every((word) => !word.glob &&
659
813
  ctx.writeRoots.some((root) => isInside(canonical(word.text, ctx.cwd, ctx.home), root)) &&
660
814
  !protectedReason(canonical(word.text, ctx.cwd, ctx.home), "write", ctx.home, ctx.guard));
661
- return { kind: `shell:${program}`, tier: inside ? 2 : 1 };
815
+ const replaces = (program === "mv" || program === "cp") && overwrites(args, ctx);
816
+ return { kind: `shell:${program}`, tier: inside && !replaces ? 2 : 1 };
662
817
  }
663
818
  const [first, second, third] = command.argv.map((word) => word.text);
664
819
  if (ctx.dirty &&
@@ -721,6 +876,15 @@ function classifySimple(command, ctx) {
721
876
  }
722
877
  const program = programWord.text;
723
878
  if (program.includes("/") || programWord.glob) {
879
+ // `/bin/cat ~/.ssh/id_rsa` still reads the key: a path-qualified program
880
+ // gets a bare one's credential checks, then asks (the path may be any file).
881
+ const bare = program.split("/").pop();
882
+ if (!programWord.glob && bare) {
883
+ const checked = classifySimple({ ...command, argv: [{ ...programWord, text: bare }, ...args] }, ctx);
884
+ if (!checked.allow && checked.tier === 0) {
885
+ return checked;
886
+ }
887
+ }
724
888
  return deny(2);
725
889
  }
726
890
  let verdict;
@@ -732,7 +896,11 @@ function classifySimple(command, ctx) {
732
896
  }
733
897
  else if (FILE_UTILS.has(program) &&
734
898
  args.some((word) => !(isFlag(word) || word.glob) &&
735
- gitControlFile(canonical(word.text, ctx.cwd, ctx.home), ctx))) {
899
+ (gitControlFile(canonical(word.text, ctx.cwd, ctx.home), ctx) ||
900
+ inGitDir(canonical(word.text, ctx.cwd, ctx.home))))) {
901
+ verdict = deny(0);
902
+ }
903
+ else if (program === "git" && cloneIntoProtected(args, ctx)) {
736
904
  verdict = deny(0);
737
905
  }
738
906
  else {
@@ -793,6 +961,31 @@ function changeDirectory(command, ctx) {
793
961
  moved: { ...ctx, cwd: canonical(target.text, ctx.cwd, ctx.home) },
794
962
  };
795
963
  }
964
+ // Parts of a chain that can create files: anything but a plain read, `cd` and
965
+ // git work that stays inside `.git`.
966
+ function writesFiles(command) {
967
+ const [program, ...args] = command.argv;
968
+ if (program?.text !== "git") {
969
+ return true;
970
+ }
971
+ const git = splitGit(args);
972
+ return !(git.ok && GIT_KEEPS_TREE.has(git.sub?.text ?? ""));
973
+ }
974
+ // After a part that can write, git asks every time unless it runs right where
975
+ // the workspace's own `.git` sits: git looks there first, and nothing in the
976
+ // chain may write a `.git`.
977
+ function runsBlind(command, ctx) {
978
+ const [program, ...args] = command.argv;
979
+ if (program?.text !== "git") {
980
+ return false;
981
+ }
982
+ const git = splitGit(args);
983
+ const repo = join(gitCwd(git, ctx.cwd, ctx.home), ".git");
984
+ return !(git.ok &&
985
+ git.dirs.every(({ flag }) => flag === "-C") &&
986
+ existsSync(repo) &&
987
+ ctx.writeRoots.some((root) => gitRepoAt(root) === repo));
988
+ }
796
989
  export function classifyShell(source, ctx) {
797
990
  const parsed = parseShell(source);
798
991
  const summary = redactSummary(source);
@@ -800,6 +993,7 @@ export function classifyShell(source, ctx) {
800
993
  return askDeny("The hook could not read this command safely. Use a simpler command, or ask the owner.", { detail: source, kind: "shell:unparseable", summary, tier: 1 });
801
994
  }
802
995
  let mutates = false;
996
+ let wrote = false;
803
997
  let worst;
804
998
  const asked = [];
805
999
  let here = ctx;
@@ -815,17 +1009,24 @@ export function classifyShell(source, ctx) {
815
1009
  continue;
816
1010
  }
817
1011
  const verdict = classifySimple(command, here);
818
- if (verdict.allow) {
1012
+ // The disk is read before the chain runs, so a repository an earlier part
1013
+ // clones or builds is not there yet (#12926).
1014
+ const blind = wrote && runsBlind(command, here);
1015
+ if (verdict.allow && !blind) {
819
1016
  mutates ||= verdict.mutates;
1017
+ wrote ||= verdict.mutates;
820
1018
  continue;
821
1019
  }
822
- if (verdict.tier === 0) {
1020
+ if (!verdict.allow && verdict.tier === 0) {
823
1021
  return verdict;
824
1022
  }
825
- asked.push(shellAsk(command, here));
826
- if (!worst || (!worst.allow && verdict.tier < worst.tier)) {
827
- worst = verdict;
1023
+ const ask = shellAsk(command, here);
1024
+ asked.push(blind ? { ...ask, tier: 1 } : ask);
1025
+ const held = blind ? deny(1) : verdict;
1026
+ if (!(held.allow || (worst && !worst.allow && held.tier >= worst.tier))) {
1027
+ worst = held;
828
1028
  }
1029
+ wrote ||= writesFiles(command);
829
1030
  }
830
1031
  if (!worst || worst.allow) {
831
1032
  return allow(mutates);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "talentos",
3
- "version": "1.4.6",
3
+ "version": "1.4.8",
4
4
  "type": "module",
5
5
  "description": "TalentOS CLI: push and pull skills, read and write team memory, and delegate work to your Talent from any coding agent.",
6
6
  "repository": {