@johngaynor/ocnus 2.0.0 → 2.0.1-next.12

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.
package/README.md CHANGED
@@ -16,11 +16,11 @@ Six skills do the work, one for each step an agent runs, and a seventh,
16
16
  |---|---|
17
17
  | `sdlc-define` | The define step: sets a card's type and writes its Specification |
18
18
  | `sdlc-plan` | The plan step: reads the card and the code, and writes the Plan |
19
- | `sdlc-breakdown` | The breakdown step: decides whether the card is one piece or several slices, then asks you to approve it, unless you have turned that approval off |
19
+ | `sdlc-breakdown` | The breakdown step: decides whether the card is one piece or several slices, then asks you to approve it, unless you have turned that approval off, and once it is approved records in Linear the other cards it waits on, and the cards in Todo that wait on it |
20
20
  | `sdlc-build` | The build step: builds an approved card in its task, making the change the Plan describes, running the tests and committing, pushing nothing |
21
21
  | `sdlc-review` | The review step: checks the build step's work against the card's Done when, writes what it found on the card as its Review, and sends what is not met back to the build step |
22
22
  | `sdlc-pr` | The pr step: pushes a reviewed card's branch, opens its pull requests, moves the card to In Review and waits while you merge them, running no watch of its own: ocnus's watch records the merge and finishes the task. It never merges |
23
- | `brief` | Not a step: a catch-up on the session, in no more than five lines and three parts: the original task, what has happened so far, and what the agent is working on right now. In a session ocnus started for a card, it reads the card, with its parent card and Linear project, `task log` and the branch's commits. Say "brief" or `/brief` in any Claude Code session for one. It records nothing, is never an answer to a question, and never stops a run |
23
+ | `brief` | Not a step: a catch-up on the session, in no more than five lines and three parts: the original task, what has happened so far, and what the agent is working on right now. In a session ocnus started for a card, it reads the card, with its parent card and Linear project, `task log` and the branch's commits. For a slice it reads the parent card too, and gives the card's place among its slices, as slice 2 of 4, and which of them have shipped; for a card with slices of its own, which of those have. Say "brief" or `/brief` in any Claude Code session for one. It records nothing, is never an answer to a question, and never stops a run |
24
24
 
25
25
  They live in [skills/](skills/). `ocnus install` links them into
26
26
  `~/.claude/skills`, so any Claude Code session on the machine can run one by
@@ -80,7 +80,8 @@ and the two phase profiles in [agents/](agents/), which it links into
80
80
  `~/.claude/agents`; see [herdr/README.md](herdr/README.md) for the full
81
81
  walkthrough. `install` ends by telling you to switch Herdr to the `default`
82
82
  layout, which holds ocnus's panels: the Ocnus spaces panel, the Cards
83
- and Steps panels, the Layout dropdown and the settings button. Herdr starts on
83
+ and Steps panels, the Usage section right above CPU, the Layout dropdown and
84
+ the settings button. Herdr's own Agents section is not on it. Herdr starts on
84
85
  its own layout, which has no Layout dropdown, so the first switch is
85
86
  `ctrl-shift-l`, pressed until `default` shows.
86
87
 
@@ -293,6 +294,26 @@ In Herdr, the settings button shows which is set and switches it with a
293
294
  click, through `billing subscription` and `billing api`; the key is saved in a
294
295
  terminal, with `billing key`.
295
296
 
297
+ On the subscription, the Usage section, right above CPU in the `default`
298
+ layout's sidebar, shows how much of your Claude subscription's five-hour and
299
+ weekly usage limits is used, as a percentage and a bar each, and when each
300
+ resets.
301
+
302
+ ```sh
303
+ ocnus usage # say the same in words (--json for programs, which the section reads every 30 seconds)
304
+ ```
305
+
306
+ The reading comes from the agents ocnus starts: on the subscription,
307
+ `claude` starts with a `statusLine` in `--settings=` that runs
308
+ `ocnus usage record`, which saves the limits Claude Code hands it to
309
+ `~/.config/ocnus/usage.json` and prints nothing, so the agents' status line
310
+ is blank. So it is as fresh as the last turn of such an agent. Older than ten
311
+ minutes, it says how old it is, and a limit whose reset has passed since
312
+ shows no number. With no reading, or one that cannot be read, the section
313
+ shows one line saying so. Claude Code sessions you start yourself are not
314
+ read, and an agent on `api` has no limits to read. See
315
+ [herdr/README.md](herdr/README.md#the-usage-section).
316
+
296
317
  ## Spaces
297
318
 
298
319
  A space groups the repos that belong together, with a `SPACE.md` saying
@@ -336,7 +357,9 @@ else. This needs `jq`; without it Claude Code asks once.
336
357
 
337
358
  A task is one piece of work in a space, usually a Linear card. Two commands
338
359
  make it, in this order: `task new`, then `task start`, which the write-up phase
339
- runs once you approve the card, unless you hold it for later.
360
+ runs as soon as you approve the card: approving a card starts it. A card you
361
+ want to hold, leave unapproved. A card that another card blocks waits by
362
+ itself: `task start` holds it until every card that blocks it is done.
340
363
 
341
364
  To see which cards there are first, `task ls` lists the open cards of the
342
365
  space's Linear teams, under their statuses:
@@ -354,9 +377,9 @@ lists a key Linear rejects as a problem, which makes `status` exit non-zero.
354
377
  For a key that works, it also checks whether the key can change cards, by
355
378
  asking Linear to change a card that cannot exist, which changes nothing. A
356
379
  key with Read permission alone lists cards, so `task ls` and the Cards panel
357
- work with it, but `linear type`, `linear section`, `linear slices` and
358
- `linear move` fail with it, and the card skills fall back to the agent's own
359
- Linear tools.
380
+ work with it, but `linear type`, `linear section`, `linear slices`,
381
+ `linear block` and `linear move` fail with it, and the card skills fall
382
+ back to the agent's own Linear tools.
360
383
  `status` lists such a key as a problem too, and exits non-zero. `linear key`
361
384
  checks the same before it saves a key: it saves one that cannot change cards,
362
385
  then says so. Either way, the fix is a key with Write permission, saved with
@@ -383,12 +406,23 @@ running, `task new` starts it, without the variables of a Claude Code session
383
406
  it was run from, so that a `claude` in its terminals still saves its
384
407
  transcript. See [herdr/README.md](herdr/README.md#writing-the-card-up).
385
408
 
409
+ Before it asks you to approve, breakdown looks for the cards this one waits
410
+ on: every other card that the card, or one of its slices, needs merged
411
+ first, a slice of another card too, and any card already approved and in
412
+ Todo that needs this one first. The Breakdown and the summary you approve
413
+ from name them. Nothing is recorded before you approve. Once you do, the
414
+ agent records them in Linear with `ocnus linear block`, before `task start`
415
+ runs: each blocker on the card, or on the slice it blocks, and this card as
416
+ a blocker of each card in Todo that needs it, so that one is not started
417
+ first. `ocnus linear read <card>` shows the cards that block a card and
418
+ the cards it blocks, each with its status.
419
+
386
420
  Recording your approval moves the card in Linear to its team's first
387
421
  not-started status (Todo in Lab), and each of its sub-issues with it. When a
388
422
  card cannot be moved, the approval is still recorded, and the agent tells you
389
423
  which card, and why. `task log` shows that too, under the approval's row.
390
- Then the agent starts the build phase with `task start`, unless you hold it
391
- for later. The approval itself closes nothing: the write-up session stays
424
+ Then the agent starts the build phase with `task start`. The approval itself
425
+ closes nothing: the write-up session stays
392
426
  open until `task start` has opened the build tab, which then puts it away
393
427
  (see below).
394
428
 
@@ -397,7 +431,7 @@ With the breakdown approval off (`ocnus approval --breakdown off`, see
397
431
  approve a card it has no doubt about: the setting approves it. It writes the
398
432
  Breakdown, makes the sub-issues when the card is sliced, records the
399
433
  approval, which moves the card as yours does and says the setting gave it,
400
- and runs `task start` itself, as for an approval to start now. A card it
434
+ and runs `task start` itself, as for your approval. A card it
401
435
  has a doubt about still waits for you, with the doubt in the summary: one
402
436
  it thinks too big, one that looks like several cards, or a sub-issue that
403
437
  looks too big to stay one piece. So do the questions define
@@ -458,8 +492,7 @@ before.
458
492
  `task start` starts the work, once you have approved the card. It takes the
459
493
  card too, makes a branch and a git worktree of it in every repo of the
460
494
  space, opens a tab for it, and starts the agent there that runs the build
461
- phase. The write-up phase runs it once you approve the card, or gives it to
462
- you when you chose to start later:
495
+ phase. The write-up phase runs it as soon as you approve the card:
463
496
 
464
497
  ```sh
465
498
  ocnus task start my-space ABC-122
@@ -474,6 +507,13 @@ as a tab named after the task. When herdr-core is not running,
474
507
  start, it stops before it makes anything. See
475
508
  [herdr/README.md](herdr/README.md#starting-the-work).
476
509
 
510
+ `task start` holds a card while a card that blocks it, or the card it was
511
+ sliced from, is not done yet in Linear. It makes no branch, worktree or tab,
512
+ leaves the card in Todo, names each card that holds it, with its status, and
513
+ exits non-zero. Run it again once those cards are done. When Linear is not
514
+ asked, as when the card's type and title are given with no key saved, it
515
+ starts the card with a note that its blockers were not checked.
516
+
477
517
  Once the build tab is open, `task start` puts the card's write-up session
478
518
  away, so finished write-up sessions do not pile up. For a sub-issue it is the
479
519
  session of the card it was sliced from, whose tab it was written up in. It
@@ -483,16 +523,17 @@ starts `task close` for that card in herdr-core, detached, with no tab:
483
523
  ocnus task close my-space ABC-122
484
524
  ```
485
525
 
486
- `task close` waits for the write-up agent's turn to end, which with start now
487
- is the turn in which it ran `task start`. Then it ends the agent's Claude session with `/exit`, waits
526
+ `task close` waits for the write-up agent's turn to end, which is the turn in
527
+ which it ran `task start`. Then it ends the agent's Claude session with `/exit`, waits
488
528
  for the tab's shell to come back, and closes the tab, or with the Herdr app
489
529
  closed ends its terminal in herdr-core, so opening the app later shows no tab
490
530
  for it. The clean exit is what archives the session in the Claude app's list
491
531
  of sessions, with Remote Control on. Its transcript stays, so
492
532
  `claude --resume` still opens it. A live agent is never killed: when it has
493
533
  not exited 30 seconds after `/exit`, or the tab runs a program that reports
494
- no status, the tab is left open. With start later, the write-up session stays
495
- open until you run `task start`, from any terminal. When `task start` stops,
534
+ no status, the tab is left open. When `task start` holds the card, the
535
+ write-up session stays open until you run `task start` again, from any
536
+ terminal, once the cards that hold it are done. When `task start` stops,
496
537
  before it has made the task or without opening its tab, the write-up session
497
538
  stays open. Only the card's own write-up tab is closed: the build tab, other
498
539
  cards' write-up tabs and the terminal `task start` ran in stay open, and a
@@ -813,8 +854,8 @@ ocnus's own cards, holds a copy of the CLI that is a change being built.
813
854
  Its `./cli/ocnus` runs only the commands that read: `help`, `status`,
814
855
  `space ls`, `space team <name>`, `profile ls`, `linear archive` (alone or
815
856
  with `--json`), `tests` (alone or with `--json`), `approval` (alone or with
816
- `--json`), `billing` (alone or with `--json`), `task ls`, `task log`,
817
- `host` and `host report`.
857
+ `--json`), `billing` (alone or with `--json`), `usage` (alone or with
858
+ `--json`), `task ls`, `task log`, `host` and `host report`.
818
859
  Any other command stops before it does anything, says that it is a
819
860
  worktree's copy, and gives the command that runs it for real:
820
861
  `OCNUS_LIVE=1 ./cli/ocnus …`, for that one run. The installed `ocnus` is never stopped, whatever folder
@@ -5,7 +5,7 @@
5
5
  # approved (skills/sdlc-breakdown). With the breakdown approval on, it asks
6
6
  # the human and waits for the answer. That is the default. With it off, it
7
7
  # approves a card it has no doubt about by itself and runs `task start`, as
8
- # for an approval to start now, without asking; a card it has a doubt about,
8
+ # for the human's approval, without asking; a card it has a doubt about,
9
9
  # such as one it thinks too big, still waits for the human. The choice is
10
10
  # "approval": {"breakdown": true|false} in the settings file (settings.sh),
11
11
  # an object so that a later approval gets a key beside it, and on when the
package/cli/lib/guard.sh CHANGED
@@ -46,7 +46,9 @@ is_worktree_copy() {
46
46
  # an Anthropic key is saved; with api or subscription it changes the
47
47
  # settings file, and with key the env file. `billing key --print` changes
48
48
  # nothing, but it prints the saved key, so it is not a read either: it is for
49
- # an agent's apiKeyHelper, which runs the copy that started the agent. `host`
49
+ # an agent's apiKeyHelper, which runs the copy that started the agent.
50
+ # `usage` alone, or with --json, reads the last reading of the Claude usage
51
+ # limits; `usage record` writes it, and is for an agent's statusLine. `host`
50
52
  # alone says whether the host is recorded, and `host report` reads its
51
53
  # readings; `host on` and `host off` make and delete them, and `host record`
52
54
  # writes them.
@@ -82,7 +84,7 @@ is_read() {
82
84
  *) return 1 ;;
83
85
  esac
84
86
  ;;
85
- approval | billing)
87
+ approval | billing | usage)
86
88
  case $# in
87
89
  1) return 0 ;;
88
90
  2) [ "$2" = --json ] ;;
@@ -126,7 +128,7 @@ guard_worktree() {
126
128
  [ "${OCNUS_LIVE:-}" != 1 ] || return 0
127
129
  GUARDED=1
128
130
  ! is_read "$@" || return 0
129
- die "this is the copy of ocnus in the worktree $REPO. It runs only the commands that read (help, status, space ls, space team <name>, profile ls, linear archive, tests, approval, billing, task ls, task log, host and host report), so \`$(shell_words "$@")\` was not run: it could change this machine's Herdr, spaces and tasks, hand-offs, host readings, Linear key or cards, or Claude Code's skills, agents and trust file.
131
+ die "this is the copy of ocnus in the worktree $REPO. It runs only the commands that read (help, status, space ls, space team <name>, profile ls, linear archive, tests, approval, billing, usage, task ls, task log, host and host report), so \`$(shell_words "$@")\` was not run: it could change this machine's Herdr, spaces and tasks, hand-offs, host readings, Linear key or cards, or Claude Code's skills, agents and trust file.
130
132
  - For the card's own steps, such as recording a hand-off, run plain \`ocnus\`: the copy this machine runs, not this one.
131
133
  - To try a command that changes the machine, write a test case for it, in test/.
132
134
  - To run it on this machine for real, only when that is wanted:
@@ -23,7 +23,7 @@ HANDOFF_KEYS='step status next summary flags approved question options answer re
23
23
  # start (record_start). Breakdown hands on to it once the human approves the
24
24
  # card, or the setting does, but it is only ever what comes next in a block, since it is a command
25
25
  # that records its own row, not a skill: the write-up agent runs it, or the
26
- # human does when they hold the build phase for later. A review that finds the work done hands on to pr, the step that pushes
26
+ # human does again once it has held a card for a card that blocks it. A review that finds the work done hands on to pr, the step that pushes
27
27
  # the task's branch, opens its pull requests and waits for the human to merge
28
28
  # them. merged is where a card ends: pr hands on to it once every pull request
29
29
  # has merged. It too is only ever what comes next, since nothing runs after it.
package/cli/lib/linear.sh CHANGED
@@ -169,7 +169,7 @@ WRITE_QUERY='mutation { issueUpdate(id: "00000000-0000-0000-0000-000000000000",
169
169
 
170
170
  # What is said of a key that can read cards but not change them, by `status`
171
171
  # as a problem and by `linear key` once it has saved such a key.
172
- NO_WRITE_WHY="The key can read cards but not change them, so linear type, linear section, linear slices and linear move fail with it. Make a key with Write permission in $KEY_PLACE, then save it with: ocnus linear key"
172
+ NO_WRITE_WHY="The key can read cards but not change them, so linear type, linear section, linear slices, linear block and linear move fail with it. Make a key with Write permission in $KEY_PLACE, then save it with: ocnus linear key"
173
173
 
174
174
  # How Linear's answer to WRITE_QUERY reads, as one word and what follows it:
175
175
  # cannot a refusal: an error of type or code forbidden, or one whose
@@ -332,34 +332,62 @@ linear_open_cards() {
332
332
  printf '%s' "$list" | jq -c 'del(.missing)'
333
333
  }
334
334
 
335
+ # How many of a card's relations, and of its parent's, the card's question
336
+ # reads, to find the cards that block it. A card with more than that has
337
+ # blockers that were not read, which CARD_SHAPE says.
338
+ CARD_RELATIONS=50
339
+ # A card's relations of every kind that name it as the related card. Those of
340
+ # the kind "blocks" name a card that blocks it, its blocker, as their issue.
341
+ CARD_BLOCKERS_READ='inverseRelations(first: '$CARD_RELATIONS') {
342
+ nodes { type issue { identifier state { name type } } } pageInfo { hasNextPage } }'
335
343
  # Find a card by its team's key and its number, as Linear keeps them: ABC-122
336
- # is team ABC, number 122. Its title, its labels and the card it was sliced
337
- # from, its parent, come with it.
344
+ # is team ABC, number 122. Its title, its status, its labels, the card it was
345
+ # sliced from, its parent, and the cards that block either of them, come with
346
+ # it.
338
347
  CARD_QUERY='query($team: String!, $number: Float!) {
339
348
  issues(first: 1, filter: {team: {key: {eq: $team}}, number: {eq: $number}}) {
340
- nodes { identifier title labels { nodes { name parent { name } } } parent { identifier } }
349
+ nodes { identifier title state { name } labels { nodes { name parent { name } } }
350
+ '$CARD_BLOCKERS_READ'
351
+ parent { identifier
352
+ '$CARD_BLOCKERS_READ' } }
341
353
  }
342
354
  }'
343
355
  # The label group a card's type is in. sdlc-define sets the type there.
344
356
  TYPE_GROUP=Type
345
- # Turn Linear's answer to CARD_QUERY into the card, {id, title, type,
346
- # parent}, or "none" when Linear has no such card. type is null when the card
347
- # has no label in the Type group ($group), and parent, the parent's ID, when
348
- # it has no parent. An answer with no list of cards in it is not Linear
349
- # saying it has none, so it is an error.
357
+ # Turn Linear's answer to CARD_QUERY into the card, {id, title, type, parent,
358
+ # status, blockers, blockersCut}, or "none" when Linear has no such card. type
359
+ # is null when the card has no label in the Type group ($group), parent, the
360
+ # parent's ID, when it has no parent, and status, the name of its status,
361
+ # when the answer has none. blockers are the cards that block it and those
362
+ # that block its parent, each as {id, status, type, of}: its ID, the name and
363
+ # kind of its status (completed for a card that is done), and the ID of the
364
+ # card it blocks. blockersCut is true when the card or its parent has more
365
+ # relations than were read, so that blockers may not be all of them. An
366
+ # answer with no list of cards in it is not Linear saying it has none, so it
367
+ # is an error.
350
368
  CARD_SHAPE='
369
+ def blockers: (.identifier? | strings) as $of
370
+ | [.inverseRelations.nodes[]? | select(.type? == "blocks") | .issue
371
+ | select(.identifier? | strings)
372
+ | {id: .identifier, status: ((.state.name? | strings) // ""), type: ((.state.type? | strings) // ""), of: $of}];
373
+ def cut: .inverseRelations.pageInfo.hasNextPage? == true;
351
374
  if (.issues.nodes | type) != "array" then error("no list of cards")
352
375
  elif (.issues.nodes | length) == 0 then "none"
353
376
  else .issues.nodes[0]
354
377
  | {id: .identifier, title: (.title // ""),
355
378
  type: ([.labels.nodes[]? | select(.parent.name? == $group) | .name] | first),
356
- parent: ((.parent.identifier? | strings) // null)}
379
+ parent: ((.parent.identifier? | strings) // null),
380
+ status: ((.state.name? | strings) // null),
381
+ blockers: (blockers + (if (.parent | type) == "object" then (.parent | blockers) else [] end)),
382
+ blockersCut: (cut or ((.parent | type) == "object" and (.parent | cut)))}
357
383
  end'
358
384
 
359
385
  # Print one card as one line of JSON, or stop and say why:
360
- # {"id":"ABC-122","title":"Export orders as CSV","type":"feat","parent":"ABC-100"}
361
- # type is null when the card has no label in the Type group, and parent when
362
- # it has no parent.
386
+ # {"id":"ABC-122","title":"Export orders as CSV","type":"feat","parent":"ABC-100","status":"Todo",
387
+ # "blockers":[{"id":"ABC-121","status":"In Progress","type":"started","of":"ABC-122"}],"blockersCut":false}
388
+ # type is null when the card has no label in the Type group, parent when it
389
+ # has no parent, and status when Linear did not say it. blockers are the
390
+ # cards that block it or its parent, done or not (CARD_SHAPE).
363
391
  # linear_card <card ID, in upper case>
364
392
  linear_card() {
365
393
  local card=$1 key vars data found
@@ -466,14 +494,18 @@ linear_key() {
466
494
  # it. `task ls` uses it too.
467
495
  PLAIN_DEF='def plain: explode | map(if . < 32 or (. > 126 and . < 160) then 32 else . end) | implode;'
468
496
 
469
- # How many labels, comments and sub-issues a read asks Linear for. A card
470
- # with more than that is not printed, since what was read would pass for the
471
- # whole of it.
497
+ # How many labels, comments, sub-issues and relations a read asks Linear
498
+ # for. A card with more than that is not printed, since what was read would
499
+ # pass for the whole of it. The relations are asked for each way: those from
500
+ # the card to another card, and those from another card to it.
472
501
  READ_LABELS=100
473
502
  READ_COMMENTS=100
474
503
  READ_SUBS=50
504
+ READ_RELATIONS=50
475
505
 
476
- # The question for linear read: the card whole.
506
+ # The question for linear read: the card whole. Its relations of the kind
507
+ # "blocks" name the cards it blocks, as their relatedIssue, and its inverse
508
+ # relations of that kind the cards that block it, as their issue.
477
509
  READ_QUERY='query($team: String!, $number: Float!) {
478
510
  issues(first: 1, filter: {team: {key: {eq: $team}}, number: {eq: $number}}) {
479
511
  nodes {
@@ -483,27 +515,39 @@ READ_QUERY='query($team: String!, $number: Float!) {
483
515
  parent { identifier title }
484
516
  project { name }
485
517
  comments(first: '$READ_COMMENTS') { nodes { body createdAt user { name } botActor { name } } pageInfo { hasNextPage } }
486
- children(first: '$READ_SUBS') { nodes { identifier title state { name type } } pageInfo { hasNextPage } }
518
+ children(first: '$READ_SUBS') { nodes { identifier title createdAt state { name type } } pageInfo { hasNextPage } }
519
+ relations(first: '$READ_RELATIONS') { nodes { type relatedIssue { identifier title state { name type } } } pageInfo { hasNextPage } }
520
+ inverseRelations(first: '$READ_RELATIONS') { nodes { type issue { identifier title state { name type } } } pageInfo { hasNextPage } }
487
521
  }
488
522
  }
489
523
  }'
490
524
 
491
525
  # From the answer to READ_QUERY, on stdin, print "none" when Linear has no
492
- # such card, {"more": <what>} when it has more comments, sub-issues or labels
493
- # than were asked for, or {"card": <the card>}, the card as linear read --json
494
- # prints it. A comment's author is the app Linear credits with it, or else
495
- # the user who wrote it. Comments come oldest first. An answer with a card in
496
- # it that is not in this form did not come from Linear. $group is TYPE_GROUP.
526
+ # such card, {"more": <what>} when it has more comments, sub-issues, labels
527
+ # or relations than were asked for, or {"card": <the card>}, the card as
528
+ # linear read --json prints it. A comment's author is the app Linear credits
529
+ # with it, or else the user who wrote it. Comments come oldest first, and so
530
+ # do sub-issues, in the order they were made, which is the order linear
531
+ # slices makes a card's slices in, whatever order Linear lists them in; when
532
+ # each was made is left out. blockedBy are the cards that block it and blocks
533
+ # the cards it blocks, from its relations of the kind "blocks" alone, done or
534
+ # not, each as a sub-issue is, in the order Linear lists them. An answer with
535
+ # a card in it that is not in this form did not come from Linear. $group is
536
+ # TYPE_GROUP.
497
537
  READ_SHAPE='
498
538
  def text: if type == "string" then . else error("unreadable") end;
499
539
  def list: if (.nodes | type) == "array" then .nodes[] else error("unreadable") end;
500
540
  def status: if type == "object" then {name: (.name | text), type: (.type | text)} else error("unreadable") end;
541
+ def other: if type == "object" then {id: (.identifier | text), title: (.title | text), status: (.state | status)}
542
+ else error("unreadable") end;
501
543
  if (.issues.nodes | type) != "array" then error("no list of cards")
502
544
  elif (.issues.nodes | length) == 0 then "none"
503
545
  else .issues.nodes[0]
504
546
  | if .comments.pageInfo.hasNextPage? == true then {more: "comments"}
505
547
  elif .children.pageInfo.hasNextPage? == true then {more: "sub-issues"}
506
548
  elif .labels.pageInfo.hasNextPage? == true then {more: "labels"}
549
+ elif .relations.pageInfo.hasNextPage? == true or .inverseRelations.pageInfo.hasNextPage? == true
550
+ then {more: "relations"}
507
551
  else {card: {
508
552
  id: (.identifier | text),
509
553
  title: (.title | text),
@@ -512,17 +556,22 @@ READ_SHAPE='
512
556
  type: ([.labels | list | select(.parent.name? == $group) | .name] | first),
513
557
  labels: [.labels | list | .name | text],
514
558
  parent: (if .parent == null then null else {id: (.parent.identifier | text), title: (.parent.title | text)} end),
559
+ blockedBy: [.inverseRelations | list | select((.type | text) == "blocks") | .issue | other],
560
+ blocks: [.relations | list | select((.type | text) == "blocks") | .relatedIssue | other],
515
561
  project: (if .project == null then null else {name: (.project.name | text)} end),
516
562
  description: (.description // "" | text),
517
563
  comments: ([.comments | list
518
564
  | {author: (.botActor.name? // .user.name? // "unknown" | text),
519
565
  createdAt: (.createdAt | text), body: (.body // "" | text)}] | sort_by(.createdAt)),
520
- subIssues: [.children | list | {id: (.identifier | text), title: (.title | text), status: (.state | status)}]
566
+ subIssues: ([.children | list
567
+ | {id: (.identifier | text), title: (.title | text), status: (.state | status),
568
+ createdAt: (.createdAt | text)}] | sort_by(.createdAt) | map(del(.createdAt)))
521
569
  }}
522
570
  end
523
571
  end'
524
572
 
525
- # The card from READ_SHAPE, on stdin, laid out like the card: its head, the
573
+ # The card from READ_SHAPE, on stdin, laid out like the card: its head, with
574
+ # a line for each card that blocks it and each card it blocks, the
526
575
  # description as written, the comments, each with its body indented under who
527
576
  # wrote it and when, and the sub-issues. Every field is made plain, but the
528
577
  # description and the comments keep their line breaks, without the \r Linear
@@ -536,6 +585,8 @@ READ_TEXT=$PLAIN_DEF'
536
585
  "Status: \(.status.name | plain)",
537
586
  "Labels: \(if .labels == [] then "none" else .labels | map(plain) | join(", ") end)",
538
587
  (.parent // empty | "Parent: \(.id | plain) \(.title | plain)"),
588
+ (.blockedBy[] | "Blocked by: \(.id | plain) \(.status.name | plain) \(.title | plain)"),
589
+ (.blocks[] | "Blocks: \(.id | plain) \(.status.name | plain) \(.title | plain)"),
539
590
  (.project // empty | "Project: \(.name | plain)"),
540
591
  "URL: \(.url | plain)",
541
592
  "",
@@ -552,13 +603,17 @@ READ_TEXT=$PLAIN_DEF'
552
603
  # Print a card whole, laid out like the card, or with --json as one line of
553
604
  # JSON, for a mod:
554
605
  # {"id":"ABC-122","title":"…","url":"…","status":{"name":"In Progress","type":"started"},
555
- # "type":"feat","labels":["feat"],"parent":{"id":"ABC-100","title":"…"},"project":{"name":"…"},
606
+ # "type":"feat","labels":["feat"],"parent":{"id":"ABC-100","title":"…"},
607
+ # "blockedBy":[{"id":"ABC-121","title":"…","status":{"name":"In Progress","type":"started"}}],
608
+ # "blocks":[{"id":"ABC-130","title":"…","status":{"name":"Todo","type":"unstarted"}}],
609
+ # "project":{"name":"…"},
556
610
  # "description":"…",
557
611
  # "comments":[{"author":"…","createdAt":"…","body":"…"}],
558
612
  # "subIssues":[{"id":"ABC-123","title":"…","status":{"name":"Todo","type":"unstarted"}}]}
559
613
  # type is the card's label in the Type group, or null; parent and project, the
560
- # card's Linear project, are null for a card with none. When it fails it
561
- # prints nothing on stdout.
614
+ # card's Linear project, are null for a card with none. blockedBy are the
615
+ # cards that block it, and blocks the cards it blocks, done or not. When it
616
+ # fails it prints nothing on stdout.
562
617
  # ocnus linear read <card> [--json]
563
618
  linear_read() {
564
619
  local card=${1:-} json=0 key vars data shape more=''
@@ -584,6 +639,7 @@ linear_read() {
584
639
  '{"more":"comments"}') more="$READ_COMMENTS comments" ;;
585
640
  '{"more":"sub-issues"}') more="$READ_SUBS sub-issues" ;;
586
641
  '{"more":"labels"}') more="$READ_LABELS labels" ;;
642
+ '{"more":"relations"}') more="$READ_RELATIONS relations" ;;
587
643
  *) die "Linear's answer was not one ocnus can read. Something between this machine and Linear may be answering for it." ;;
588
644
  esac
589
645
  [ -z "$more" ] ||
@@ -597,11 +653,12 @@ linear_read() {
597
653
 
598
654
  # --- the card skills' writes ------------------------------------------------
599
655
  #
600
- # The card skills write three things: a card's type label, a section of its
601
- # description, and its slices, the sub-issues sdlc-breakdown makes of it. Each
602
- # is one command here, so that it goes the same way on every run, and each
603
- # reads back what Linear saved before it says it is done. The slices are the
604
- # one thing created; nothing else in Linear is changed.
656
+ # The card skills write four things: a card's type label, a section of its
657
+ # description, its slices, the sub-issues sdlc-breakdown makes of it, and the
658
+ # cards that block a card. Each is one command here, so that it goes the same
659
+ # way on every run, and each reads back what Linear saved before it says it
660
+ # is done. The slices and the blockers are the only things created; nothing
661
+ # else in Linear is changed, and nothing is removed.
605
662
 
606
663
  # The types a card can have. Each is the start of the card's branch name.
607
664
  CARD_TYPES='feat fix chore'
@@ -1083,6 +1140,114 @@ linear_slices() {
1083
1140
  .others[] | "Left alone: \(.identifier | plain) \(.title | plain), a sub-issue of \($card) that is not one of its slices"'
1084
1141
  }
1085
1142
 
1143
+ # --- recording blockers -----------------------------------------------------
1144
+ #
1145
+ # sdlc-breakdown records every other card a card or a slice depends on, in
1146
+ # another parent card too, as one that blocks it, so that the cards start in
1147
+ # the order the work needs: `task start` holds a card while a card that
1148
+ # blocks it is not done. A blocker is a relation of the kind "blocks", from
1149
+ # the blocker to the card, as linear slices makes between slices. Nothing
1150
+ # here ever removes one.
1151
+
1152
+ # The question for linear block: a card's id, and the cards that block it
1153
+ # now, by their ids. It finds each blocker's id too. A card with more
1154
+ # relations than are read may have a blocker among the rest; asking for it
1155
+ # again costs nothing, since Linear keeps one relation of a kind between two
1156
+ # cards (SLICE_RELATIONS).
1157
+ BLOCK_QUERY='query($team: String!, $number: Float!) {
1158
+ issues(first: 1, filter: {team: {key: {eq: $team}}, number: {eq: $number}}) {
1159
+ nodes { id identifier
1160
+ inverseRelations(first: '$CARD_RELATIONS') { nodes { type issue { id } } } }
1161
+ }
1162
+ }'
1163
+
1164
+ # From the answer to BLOCK_QUERY, on stdin, the card as {"state": "ok", id,
1165
+ # blockers}, blockers being the ids of the cards that block it, or
1166
+ # {"state": "none"} when Linear has no such card. An answer with a card in it
1167
+ # that is not in this form did not come from Linear.
1168
+ BLOCK_SHAPE='
1169
+ def text: if type == "string" then . else error("unreadable") end;
1170
+ if (.issues.nodes | type) != "array" then error("no list of cards")
1171
+ elif (.issues.nodes | length) == 0 then {state: "none"}
1172
+ else .issues.nodes[0]
1173
+ | {state: "ok", id: (.id | text),
1174
+ blockers: [.inverseRelations.nodes[]? | select(.type? == "blocks") | .issue.id? | strings]}
1175
+ end'
1176
+
1177
+ # Print a card as BLOCK_SHAPE has it, one line of JSON, or stop and say why.
1178
+ # block_card <key> <card ID, in upper case>
1179
+ block_card() {
1180
+ local key=$1 card=$2 vars data found
1181
+ vars=$(jq -cn --arg team "${card%-*}" --arg number "${card##*-}" '{team: $team, number: ($number | tonumber)}') ||
1182
+ die "'$card' is not a card ID like ABC-122. Nothing was recorded."
1183
+ data=$(linear_ask "$key" "$BLOCK_QUERY" "$vars") || exit 1
1184
+ # Judged by what jq prints: some versions exit zero on an empty answer.
1185
+ found=$(printf '%s' "$data" | jq -c "$BLOCK_SHAPE" 2>/dev/null) || found=
1186
+ case $(printf '%s' "$found" | jq -r '.state' 2>/dev/null) in
1187
+ ok) printf '%s\n' "$found" ;;
1188
+ none) die "Linear has no card $card that the saved key can see. Check the ID, then run this again. Nothing was recorded." ;;
1189
+ *) die "Linear's answer was not one ocnus can read. Something between this machine and Linear may be answering for it. Nothing was recorded." ;;
1190
+ esac
1191
+ }
1192
+
1193
+ # Record in Linear that each blocker given blocks the card: a relation of the
1194
+ # kind blocks, from the blocker to the card. Every card is read before
1195
+ # anything is recorded, so an ID Linear does not have records nothing. A
1196
+ # blocker the card has already is skipped, one given twice is recorded once,
1197
+ # and each one recorded is read back before the next. A card never blocks
1198
+ # itself. Nothing is removed. It prints one line for each blocker:
1199
+ # <card> is now blocked by <blocker>
1200
+ # <card> is blocked by <blocker> already
1201
+ # A run that stopped partway is finished by running it again with the same
1202
+ # cards.
1203
+ # ocnus linear block <card> <blocker>...
1204
+ linear_block() {
1205
+ local card=${1:-} given blocker blockers='' key found card_id had ids='' id vars made
1206
+ [ "$#" -ge 2 ] ||
1207
+ die "usage: ocnus linear block <card> <blocker>..., like: ocnus linear block ABC-122 ABC-121, which records that ABC-121 blocks ABC-122"
1208
+ [[ $card =~ $CARD_RE ]] || die "'$card' is not a card ID like ABC-122. Nothing was recorded."
1209
+ card=$(printf '%s' "$card" | tr '[:lower:]' '[:upper:]')
1210
+ shift
1211
+ for given in "$@"; do
1212
+ [[ $given =~ $CARD_RE ]] || die "'$given' is not a card ID like ABC-121. Nothing was recorded."
1213
+ blocker=$(printf '%s' "$given" | tr '[:lower:]' '[:upper:]')
1214
+ [ "$blocker" != "$card" ] || die "$card cannot block itself. Nothing was recorded."
1215
+ in_list "$blocker" "$blockers" || blockers="$blockers $blocker"
1216
+ done
1217
+ linear_tools
1218
+ key=$(linear_saved_key) || exit 1
1219
+ found=$(block_card "$key" "$card") || exit 1
1220
+ card_id=$(printf '%s' "$found" | jq -r '.id')
1221
+ had=$(printf '%s' "$found" | jq -r '.blockers[]')
1222
+ # Each blocker's id, in the order given, before anything is recorded.
1223
+ for blocker in $blockers; do
1224
+ found=$(block_card "$key" "$blocker") || exit 1
1225
+ id=$(printf '%s' "$found" | jq -r '.id')
1226
+ [ "$id" != "$card_id" ] || die "$blocker is $card in Linear, and $card cannot block itself. Nothing was recorded."
1227
+ ids="$ids $id"
1228
+ done
1229
+ # The ids are Linear's, with no spaces in them, one for each blocker, in
1230
+ # the same order.
1231
+ # shellcheck disable=SC2086
1232
+ set -- $ids
1233
+ for blocker in $blockers; do
1234
+ id=$1
1235
+ shift
1236
+ if printf '%s\n' "$had" | grep -qxF -- "$id"; then
1237
+ say "$card is blocked by $blocker already"
1238
+ continue
1239
+ fi
1240
+ vars=$(jq -cn --arg from "$id" --arg to "$card_id" '{input: {issueId: $from, relatedIssueId: $to, type: "blocks"}}')
1241
+ made=$(linear_ask "$key" "$SLICE_BLOCK" "$vars" 2>&1) ||
1242
+ die "could not record $blocker as blocking $card: $(first_sentence "$made"). Run this again with the same cards to record the rest: it skips the blockers already there."
1243
+ [ "$(printf '%s' "$made" | jq -r --arg from "$id" --arg to "$card_id" '
1244
+ .issueRelationCreate.issueRelation | type == "object" and .type? == "blocks"
1245
+ and .issue.id? == $from and .relatedIssue.id? == $to' 2>/dev/null)" = true ] ||
1246
+ die "Linear did not record $blocker as blocking $card, as its answer shows. Look at $card in Linear, then run this again with the same cards to add it."
1247
+ say "$card is now blocked by $blocker"
1248
+ done
1249
+ }
1250
+
1086
1251
  # --- moving a card ----------------------------------------------------------
1087
1252
  #
1088
1253
  # ocnus moves a card at four points: the hand-off that records the
@@ -1381,9 +1546,10 @@ cmd_linear() {
1381
1546
  type) shift; linear_type "$@" ;;
1382
1547
  section) shift; linear_section "$@" ;;
1383
1548
  slices) shift; linear_slices "$@" ;;
1549
+ block) shift; linear_block "$@" ;;
1384
1550
  move) shift; linear_move_cmd "$@" ;;
1385
1551
  read) shift; linear_read "$@" ;;
1386
1552
  archive) shift; linear_archive_cmd "$@" ;;
1387
- *) die "usage: ocnus linear key, or: ocnus linear read <card> [--json], or: ocnus linear type <card> <feat|fix|chore>, or: ocnus linear section <card> <Specification|Plan|Breakdown|Review> [--replace], or: ocnus linear slices <card>, or: ocnus linear move <card> <approved|started|review|done>, or: ocnus linear archive [on|off|--json] (see: ocnus help)" ;;
1553
+ *) die "usage: ocnus linear key, or: ocnus linear read <card> [--json], or: ocnus linear type <card> <feat|fix|chore>, or: ocnus linear section <card> <Specification|Plan|Breakdown|Review> [--replace], or: ocnus linear slices <card>, or: ocnus linear block <card> <blocker>..., or: ocnus linear move <card> <approved|started|review|done>, or: ocnus linear archive [on|off|--json] (see: ocnus help)" ;;
1388
1554
  esac
1389
1555
  }