| 1 | // Package activity reads what a specialist started during a step (a |
| 2 | // wire.ActivityLine) for a person: a verb and, for a file, its path. A |
| 3 | // shell command arrives as kind "run" with its first line, wrapped or |
| 4 | // not (Codex sends `/bin/bash -lc "cat ios/..."`); Meaning unwraps it, |
| 5 | // takes its first meaningful part and maps the common tools. The verbs |
| 6 | // are copy keys' last parts (internal/equipo words them); anything it |
| 7 | // does not know is "command", so raw shell never reaches a person. |
| 8 | package activity |
| 9 | |
| 10 | import ( |
| 11 | "path" |
| 12 | "strings" |
| 13 | "unicode" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/wire" |
| 16 | ) |
| 17 | |
| 18 | // Meaning is a line's verb (a copy key's last part) and its |
| 19 | // subject: a path for files, a word for searches, "" for neither. |
| 20 | func Meaning(a wire.ActivityLine) (key, subject string) { |
| 21 | s := strings.TrimSpace(a.Subject) |
| 22 | if s == "" { |
| 23 | return "working", "" |
| 24 | } |
| 25 | switch a.Kind { |
| 26 | case wire.ActivityRead: |
| 27 | return "read", s |
| 28 | case wire.ActivityEdit: |
| 29 | return "edit", s |
| 30 | case wire.ActivitySearch: |
| 31 | return "search", s |
| 32 | case wire.ActivityRun: |
| 33 | return commandMeaning(s, 0) |
| 34 | } |
| 35 | return "working", "" |
| 36 | } |
| 37 | |
| 38 | // Name is a path's base name when it is plain enough to show. |
| 39 | func Name(p string) string { |
| 40 | p = strings.TrimSpace(p) |
| 41 | if p == "" || strings.ContainsAny(p, "$`(){}<>|;&*?[]=~!#%\\\"'\n") { |
| 42 | return "" |
| 43 | } |
| 44 | base := path.Base(strings.TrimSuffix(p, "/")) |
| 45 | if base == "/" || strings.HasPrefix(base, "-") { |
| 46 | return "" |
| 47 | } |
| 48 | return clipRunes(base, 60) |
| 49 | } |
| 50 | |
| 51 | // Word is a search term worth quoting: a word or a short phrase. |
| 52 | func Word(s string) bool { |
| 53 | if s == "" || len(s) > 40 || strings.HasPrefix(s, "-") { |
| 54 | return false |
| 55 | } |
| 56 | for _, r := range s { |
| 57 | if !unicode.IsLetter(r) && !unicode.IsDigit(r) && !strings.ContainsRune("_.:- ", r) { |
| 58 | return false |
| 59 | } |
| 60 | } |
| 61 | return true |
| 62 | } |
| 63 | |
| 64 | func clipRunes(s string, n int) string { |
| 65 | r := []rune(s) |
| 66 | if len(r) <= n { |
| 67 | return s |
| 68 | } |
| 69 | return string(r[:n-1]) + "…" |
| 70 | } |
| 71 | |
| 72 | // commandMeaning reads a command's first meaningful part. |
| 73 | func commandMeaning(cmd string, depth int) (key, subject string) { |
| 74 | words := shellWords(cmd) |
| 75 | // A shell wrapper: its script is what runs. |
| 76 | if len(words) > 0 && depth < 3 && isShell(words[0]) { |
| 77 | for i := 1; i < len(words); i++ { |
| 78 | w := words[i] |
| 79 | if strings.HasPrefix(w, "-") && !strings.HasPrefix(w, "--") && strings.Contains(w, "c") { |
| 80 | if i+1 < len(words) && !isOperator(words[i+1]) { |
| 81 | return commandMeaning(words[i+1], depth+1) |
| 82 | } |
| 83 | return "command", "" |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | var fallback []string |
| 88 | for _, part := range splitCommands(words) { |
| 89 | part = stripPrefixes(part) |
| 90 | if len(part) == 0 { |
| 91 | continue |
| 92 | } |
| 93 | switch path.Base(part[0]) { |
| 94 | case "cd", "pushd", "popd", "export", "set", "source", ".", "true", ":", "unset", "alias": |
| 95 | continue |
| 96 | case "echo", "printf": |
| 97 | if fallback == nil { |
| 98 | fallback = part |
| 99 | } |
| 100 | continue |
| 101 | } |
| 102 | return toolMeaning(part) |
| 103 | } |
| 104 | if fallback != nil { |
| 105 | return toolMeaning(fallback) |
| 106 | } |
| 107 | return "command", "" |
| 108 | } |
| 109 | |
| 110 | func isShell(w string) bool { |
| 111 | switch path.Base(w) { |
| 112 | case "sh", "bash", "zsh", "dash", "ksh": |
| 113 | return true |
| 114 | } |
| 115 | return false |
| 116 | } |
| 117 | |
| 118 | // shellWords splits a command into words the way a shell would, roughly: |
| 119 | // quotes group, a backslash escapes, and the operators && || ; | stand as |
| 120 | // words of their own. |
| 121 | func shellWords(s string) []string { |
| 122 | var out []string |
| 123 | var cur strings.Builder |
| 124 | in, quote := false, rune(0) |
| 125 | flush := func() { |
| 126 | if in { |
| 127 | out = append(out, cur.String()) |
| 128 | cur.Reset() |
| 129 | in = false |
| 130 | } |
| 131 | } |
| 132 | rs := []rune(s) |
| 133 | for i := 0; i < len(rs); i++ { |
| 134 | r := rs[i] |
| 135 | switch { |
| 136 | case quote != 0: |
| 137 | if r == quote { |
| 138 | quote = 0 |
| 139 | } else if r == '\\' && quote == '"' && i+1 < len(rs) { |
| 140 | i++ |
| 141 | cur.WriteRune(rs[i]) |
| 142 | } else { |
| 143 | cur.WriteRune(r) |
| 144 | } |
| 145 | case r == '\'' || r == '"': |
| 146 | quote, in = r, true |
| 147 | case r == '\\' && i+1 < len(rs): |
| 148 | i++ |
| 149 | cur.WriteRune(rs[i]) |
| 150 | in = true |
| 151 | case unicode.IsSpace(r): |
| 152 | flush() |
| 153 | case r == ';' || r == '|' || r == '&': |
| 154 | flush() |
| 155 | op := string(r) |
| 156 | if i+1 < len(rs) && (rs[i+1] == '|' || rs[i+1] == '&') && r != ';' { |
| 157 | op += string(rs[i+1]) |
| 158 | i++ |
| 159 | } |
| 160 | out = append(out, op) |
| 161 | default: |
| 162 | cur.WriteRune(r) |
| 163 | in = true |
| 164 | } |
| 165 | } |
| 166 | flush() |
| 167 | return out |
| 168 | } |
| 169 | |
| 170 | func isOperator(w string) bool { |
| 171 | switch w { |
| 172 | case "&&", "||", ";", "|", "&", "|&": |
| 173 | return true |
| 174 | } |
| 175 | return false |
| 176 | } |
| 177 | |
| 178 | // splitCommands cuts words at the operators. |
| 179 | func splitCommands(words []string) [][]string { |
| 180 | var out [][]string |
| 181 | var cur []string |
| 182 | for _, w := range words { |
| 183 | if isOperator(w) { |
| 184 | out = append(out, cur) |
| 185 | cur = nil |
| 186 | continue |
| 187 | } |
| 188 | cur = append(cur, w) |
| 189 | } |
| 190 | return append(out, cur) |
| 191 | } |
| 192 | |
| 193 | // stripPrefixes drops what only sets the scene for a command: variable |
| 194 | // assignments, env, sudo, nice, time, timeout and the shell's keywords. |
| 195 | func stripPrefixes(w []string) []string { |
| 196 | for len(w) > 0 { |
| 197 | head := w[0] |
| 198 | switch { |
| 199 | case strings.Contains(head, "=") && !strings.HasPrefix(head, "-") && !strings.Contains(head[:strings.Index(head, "=")], "/"): |
| 200 | w = w[1:] |
| 201 | case head == "env" || head == "sudo" || head == "command" || head == "exec" || head == "time" || head == "do" || head == "then" || head == "else" || head == "!": |
| 202 | w = w[1:] |
| 203 | case head == "nice": |
| 204 | w = w[1:] |
| 205 | if len(w) > 1 && w[0] == "-n" { |
| 206 | w = w[2:] |
| 207 | } else if len(w) > 0 && strings.HasPrefix(w[0], "-") { |
| 208 | w = w[1:] |
| 209 | } |
| 210 | case head == "timeout": |
| 211 | w = w[1:] |
| 212 | for len(w) > 0 && strings.HasPrefix(w[0], "-") { |
| 213 | w = w[1:] |
| 214 | } |
| 215 | if len(w) > 0 { |
| 216 | w = w[1:] |
| 217 | } |
| 218 | default: |
| 219 | return w |
| 220 | } |
| 221 | } |
| 222 | return w |
| 223 | } |
| 224 | |
| 225 | // args are a command's operands: its words after the flags, skipping the |
| 226 | // values of the flags that take one. |
| 227 | func args(words []string, valued ...string) []string { |
| 228 | var out []string |
| 229 | takes := map[string]bool{} |
| 230 | for _, v := range valued { |
| 231 | takes[v] = true |
| 232 | } |
| 233 | for i := 0; i < len(words); i++ { |
| 234 | w := words[i] |
| 235 | if w == "--" { |
| 236 | return append(out, words[i+1:]...) |
| 237 | } |
| 238 | if strings.HasPrefix(w, "-") && len(w) > 1 { |
| 239 | if takes[w] { |
| 240 | i++ |
| 241 | } |
| 242 | continue |
| 243 | } |
| 244 | out = append(out, w) |
| 245 | } |
| 246 | return out |
| 247 | } |
| 248 | |
| 249 | func has(words []string, flags ...string) bool { |
| 250 | for _, w := range words { |
| 251 | for _, f := range flags { |
| 252 | if w == f || (strings.HasPrefix(f, "--") && strings.HasPrefix(w, f+"=")) { |
| 253 | return true |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | return false |
| 258 | } |
| 259 | |
| 260 | func first(words []string) string { |
| 261 | if len(words) == 0 { |
| 262 | return "" |
| 263 | } |
| 264 | return words[0] |
| 265 | } |
| 266 | |
| 267 | // toolMeaning maps one command to its verb. |
| 268 | func toolMeaning(w []string) (key, subject string) { |
| 269 | name, rest := path.Base(w[0]), w[1:] |
| 270 | switch name { |
| 271 | case "cat", "bat", "less", "more", "nl", "wc", "file", "stat", "xxd", "od", "strings", "head", "tail": |
| 272 | return "read", first(args(rest, "-n", "-c", "-l", "--lines", "--bytes")) |
| 273 | case "sed": |
| 274 | if hasPrefix(rest, "-i") || has(rest, "--in-place") { |
| 275 | return "edit", last(args(rest, "-e", "-f", "--expression", "--file", "-l")) |
| 276 | } |
| 277 | a := args(rest, "-e", "-f", "--expression", "--file", "-l") |
| 278 | if !has(rest, "-e", "-f", "--expression", "--file") && len(a) > 0 { |
| 279 | a = a[1:] |
| 280 | } |
| 281 | return "read", first(a) |
| 282 | case "awk", "gawk": |
| 283 | a := args(rest, "-F", "-v", "-f") |
| 284 | if len(a) > 1 { |
| 285 | return "read", a[1] |
| 286 | } |
| 287 | return "read", "" |
| 288 | case "rg", "grep", "egrep", "fgrep", "ag", "ack": |
| 289 | if name == "rg" && has(rest, "--files") { |
| 290 | return "list", first(args(rest, "-g", "--glob", "-t", "--type")) |
| 291 | } |
| 292 | a := args(rest, "-e", "-f", "-g", "--glob", "-t", "--type", "-T", "--type-not", "-A", "-B", "-C", "-m", "--max-count", "-M", "--context") |
| 293 | return "search", first(a) |
| 294 | case "find", "fd", "ls", "tree", "du": |
| 295 | return "list", first(args(rest, "-name", "-iname", "-path", "-type", "-maxdepth", "-mindepth", "-e", "-d", "-L", "-I", "--max-depth")) |
| 296 | case "apply_patch", "patch": |
| 297 | return "edit", "" |
| 298 | case "tee", "touch": |
| 299 | return "edit", first(args(rest)) |
| 300 | case "mv", "cp", "rm", "mkdir", "ln", "chmod": |
| 301 | return "edit", "" |
| 302 | case "gofmt", "goimports", "prettier", "swiftformat", "black", "rustfmt": |
| 303 | if has(rest, "-w", "--write") || name == "black" || name == "swiftformat" || name == "rustfmt" { |
| 304 | return "format", "" |
| 305 | } |
| 306 | return "checks", "" |
| 307 | case "go": |
| 308 | switch first(args(rest)) { |
| 309 | case "test": |
| 310 | return "test", "" |
| 311 | case "build", "run", "install", "generate": |
| 312 | return "build", "" |
| 313 | case "vet": |
| 314 | return "checks", "" |
| 315 | case "fmt": |
| 316 | return "format", "" |
| 317 | case "mod", "get": |
| 318 | return "install", "" |
| 319 | } |
| 320 | return "command", "" |
| 321 | case "xcodebuild": |
| 322 | if has(rest, "test", "test-without-building") { |
| 323 | return "test", "" |
| 324 | } |
| 325 | return "build", "" |
| 326 | case "swift": |
| 327 | switch first(args(rest)) { |
| 328 | case "test": |
| 329 | return "test", "" |
| 330 | case "build": |
| 331 | return "build", "" |
| 332 | case "package": |
| 333 | return "install", "" |
| 334 | } |
| 335 | return "command", "" |
| 336 | case "npm", "pnpm", "yarn", "bun": |
| 337 | a := args(rest) |
| 338 | sub := first(a) |
| 339 | if sub == "run" || sub == "run-script" { |
| 340 | a = a[1:] |
| 341 | sub = first(a) |
| 342 | } |
| 343 | switch sub { |
| 344 | case "test", "t", "tests", "e2e": |
| 345 | return "test", "" |
| 346 | case "build": |
| 347 | return "build", "" |
| 348 | case "install", "i", "ci", "add": |
| 349 | return "install", "" |
| 350 | case "lint", "check", "typecheck", "format:check": |
| 351 | return "checks", "" |
| 352 | } |
| 353 | if name == "yarn" && sub == "" { |
| 354 | return "install", "" |
| 355 | } |
| 356 | return "command", "" |
| 357 | case "npx", "bunx", "pnpx": |
| 358 | switch path.Base(first(args(rest))) { |
| 359 | case "jest", "vitest", "mocha", "playwright", "ava", "tap": |
| 360 | return "test", "" |
| 361 | case "tsc", "eslint", "stylelint", "biome": |
| 362 | return "checks", "" |
| 363 | case "prettier": |
| 364 | return "format", "" |
| 365 | } |
| 366 | return "command", "" |
| 367 | case "jest", "vitest", "mocha", "pytest", "tox", "rspec", "phpunit": |
| 368 | return "test", "" |
| 369 | case "eslint", "tsc", "staticcheck", "golangci-lint", "swiftlint", "ruff", "flake8", "mypy", "shellcheck", "govulncheck": |
| 370 | return "checks", "" |
| 371 | case "cargo": |
| 372 | switch first(args(rest)) { |
| 373 | case "test", "nextest": |
| 374 | return "test", "" |
| 375 | case "build", "run": |
| 376 | return "build", "" |
| 377 | case "check", "clippy", "fmt": |
| 378 | return "checks", "" |
| 379 | } |
| 380 | return "command", "" |
| 381 | case "make", "gmake", "just": |
| 382 | switch first(args(rest, "-C", "-f", "-j")) { |
| 383 | case "test", "tests", "unit", "e2e": |
| 384 | return "test", "" |
| 385 | case "ci", "check", "checks", "lint", "vet", "verify": |
| 386 | return "checks", "" |
| 387 | case "", "build", "all", "install": |
| 388 | return "build", "" |
| 389 | } |
| 390 | return "command", "" |
| 391 | case "python", "python3", "node", "ruby", "deno", "perl", "php": |
| 392 | if has(rest, "-m") { |
| 393 | for i, x := range rest { |
| 394 | if x == "-m" && i+1 < len(rest) { |
| 395 | switch rest[i+1] { |
| 396 | case "pytest", "unittest", "tox": |
| 397 | return "test", "" |
| 398 | case "pip": |
| 399 | return "install", "" |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | return "command", "" |
| 404 | } |
| 405 | if has(rest, "-c", "-e", "-pi", "-p", "-i") { |
| 406 | return "command", "" |
| 407 | } |
| 408 | return "script", first(args(rest)) |
| 409 | case "git": |
| 410 | return gitMeaning(rest) |
| 411 | } |
| 412 | if strings.HasPrefix(w[0], "./") || strings.HasSuffix(name, ".sh") || strings.HasSuffix(name, ".py") { |
| 413 | return "script", w[0] |
| 414 | } |
| 415 | return "command", "" |
| 416 | } |
| 417 | |
| 418 | func hasPrefix(words []string, prefix string) bool { |
| 419 | for _, w := range words { |
| 420 | if strings.HasPrefix(w, prefix) && !strings.HasPrefix(w, "--") { |
| 421 | return true |
| 422 | } |
| 423 | } |
| 424 | return false |
| 425 | } |
| 426 | |
| 427 | func last(words []string) string { |
| 428 | if len(words) == 0 { |
| 429 | return "" |
| 430 | } |
| 431 | return words[len(words)-1] |
| 432 | } |
| 433 | |
| 434 | // gitMeaning maps git's subcommands. |
| 435 | func gitMeaning(rest []string) (key, subject string) { |
| 436 | // Global options come before the subcommand; -C and -c take a value. |
| 437 | for len(rest) > 0 && strings.HasPrefix(rest[0], "-") { |
| 438 | if (rest[0] == "-C" || rest[0] == "-c") && len(rest) > 1 { |
| 439 | rest = rest[1:] |
| 440 | } |
| 441 | rest = rest[1:] |
| 442 | } |
| 443 | switch first(rest) { |
| 444 | case "diff", "show", "range-diff": |
| 445 | return "diff", "" |
| 446 | case "log", "blame", "shortlog", "reflog": |
| 447 | return "history", "" |
| 448 | case "status": |
| 449 | return "status", "" |
| 450 | case "add", "commit": |
| 451 | return "commit", "" |
| 452 | case "grep": |
| 453 | return "search", first(args(rest[1:], "-e", "-f", "-A", "-B", "-C", "-m")) |
| 454 | case "ls-files": |
| 455 | return "list", "" |
| 456 | case "apply", "am": |
| 457 | return "edit", "" |
| 458 | } |
| 459 | return "git", "" |
| 460 | } |
| 461 | |