@tamai/hub-cli 0.1.8 → 0.1.9

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
@@ -68,7 +68,7 @@ hub infra help
68
68
 
69
69
  動詞は `task` が `new` / `add` / `claim` / `next` / `show` / `report` / `note` / `list` / `audit` / `brief`、
70
70
  `project` が `list` / `show` / `create` / `update` / `move`、
71
- `infra` が `list` / `show` / `update` / `link` / `unlink`。
71
+ `infra` が `list` / `show` / `update` / `note` / `link` / `unlink`。
72
72
  **正確なオプションは常に `--help` を見る**(ここにも、スキルにも写経しない)。
73
73
  全動詞が `--json` を受ける。機械が読むときは必ず `--json` を使い、人向けの整形を parse しない。
74
74
 
@@ -156,8 +156,11 @@ hub task brief 183 --role review --worker codex1 --out /tmp/brief-183.md --sandb
156
156
  hub infra list # 自分で管理する資産だけ(既定)
157
157
  hub infra list --management all # 他社管理を含む全件
158
158
  hub infra show example.jp
159
- hub infra update example.jp --status to_cancel --append-note '解約申請済み'
159
+ hub infra update example.jp --status to_cancel --reason '11/30までに解約と決めた'
160
160
  hub infra update example.jp --prod-platform none # 本番の置き場なし(確定値)
161
+ hub infra note example.jp --note 'Xserverのパネルで自動更新onを確認'
162
+ hub infra note example.jp --note-file /tmp/note.md
163
+ hub infra note example.jp --note '…' --operation-id 01M33H236V8MACB2DFR641ATK3 # 送り直し
161
164
  hub infra link example.jp --project hub
162
165
  hub infra unlink example.jp --project hub
163
166
  hub infra unlink example.jp # 紐付けを全部外す
@@ -168,6 +171,28 @@ hub infra unlink example.jp # 紐付けを全部外す
168
171
  1回だけ再試行する。`link` は現在の紐付けへ追加、`unlink` は指定分を削除し、APIへはどちらも
169
172
  `PUT /:id/projects` の全置換として送る。
170
173
 
174
+ `note` は台帳の**経緯**を1件足す(`POST /:id/notes`)。資産の `version` を進めないので、
175
+ 画面を開いたまま追記しても、そのあとの保存が競合しない。本文は2,000文字までで**分割しない**
176
+ (`hub task note` と違い、長い本文は要点へ縮める。旧メモの原文保管はCLIでは行わない)。
177
+ 操作ID(ULID)は**1操作につき1つ**をCLIが作り、送信の前に決める。応答が返る前に通信が
178
+ 切れた失敗だけ、**同じ操作IDで1回だけ**送り直す——届いていた場合はAPIがその操作IDを見て
179
+ 既存のイベントを返すので、二重に積まれない。応答が返った失敗(400 / 409 など)は
180
+ サーバが答えているので再試行しない。
181
+
182
+ 失敗すると `操作ID: <ULID>` を出す。後から送り直すときは、素で打ち直さず
183
+ `--operation-id <ULID>` にその値を渡す(素の打ち直しは別の操作IDになるので、
184
+ 1回目が届いていた場合に2件積まれる)。
185
+
186
+ ```bash
187
+ hub infra note example.jp --note '…' --operation-id 01M33H236V8MACB2DFR641ATK3
188
+ ```
189
+
190
+ `update --reason <text>` は**その更新の理由**(500文字まで)で、列ではなく監査イベントへ入る。
191
+ 値の変更と一緒に指定する(理由だけの更新は400。経緯は `hub infra note`)。
192
+ **`--reason` を含む更新は409で自動再送しない**——理由は読んだ状態に依存するので、
193
+ 最新を確認して書き直す。`--append-note` は**次の版で外す**(使うと stderr へ予告が出る)。
194
+ 「現在の補足」を上書きする `--note` は残り、経緯は `note` 動詞へ分かれる。
195
+
171
196
  `--dns-host` / `--mail-host` / `--migration-plan` / `--contract` / `--hosted-on-basis`は
172
197
  `none`で未記入(`null`)へ戻せる。`--prod-platform none`だけは「本番の置き場は無い」という
173
198
  確定済みのenum値を送り、未記入には戻さない。
@@ -0,0 +1,114 @@
1
+ import { ApiError, withGuidance } from "../lib/api.mjs";
2
+ import { requireSinglePositional, usageError } from "../lib/args.mjs";
3
+ import { resolveInfraAssetId } from "../lib/infra.mjs";
4
+ import { newUlid } from "../lib/ulid.mjs";
5
+
6
+ /** APIの上限(`MAX_NOTE_TEXT`)と同じ。**分割しない**ので超えたら送らずに案内する。 */
7
+ export const INFRA_NOTE_LIMIT = 2_000;
8
+
9
+ /** 操作IDの形(`newUlid` が作る26文字のCrockford Base32)。`--operation-id` の受理判定。 */
10
+ const OPERATION_ID_PATTERN = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
11
+
12
+ /**
13
+ * 通信の結果が**分からない**失敗か(codex2レビュー High 1)。
14
+ *
15
+ * `fetch` が応答を返す前に落ちた失敗は、「届いていない」と「届いたが応答を失った」の
16
+ * 区別が付かない。一方、応答が返った失敗——`ApiError`(4xx / 5xx)と「JSONでない応答」
17
+ * (`cliError` なので `code === "cli_error"`)——はサーバが答えているので、同じ本文を
18
+ * 打ち直しても同じ答えになる。再試行するのは前者だけ。
19
+ */
20
+ function isUnknownOutcome(error) {
21
+ return !(error instanceof ApiError) && error?.code !== "cli_error";
22
+ }
23
+
24
+ function operationIdFrom(value) {
25
+ const id = String(value ?? "").trim().toUpperCase();
26
+ if (!OPERATION_ID_PATTERN.test(id)) {
27
+ throw usageError(
28
+ "--operation-id にはULID(26文字のCrockford Base32)を指定してください。"
29
+ + "前回の失敗が出した「操作ID: …」の値をそのまま渡します",
30
+ );
31
+ }
32
+ return id;
33
+ }
34
+
35
+ /**
36
+ * 失敗のときに**操作IDを必ず出す**(codex2レビュー High 1)。
37
+ *
38
+ * 届いたかどうか分からないまま素で打ち直すと別の操作IDになり、届いていた場合に2件積まれる。
39
+ * 人が同じ操作として送り直せるように、idと `--operation-id` の使い方を添える。
40
+ */
41
+ function operationGuidance(operationId, unknownOutcome) {
42
+ const line = `操作ID: ${operationId}`;
43
+ if (!unknownOutcome) return line;
44
+ return `${line}\n`
45
+ + "届いたかどうかは分かりません(応答の前に通信が切れ、同じ操作IDでの再送も失敗しました)。\n"
46
+ + `送り直すときは --operation-id ${operationId} を付けてください(届いていれば二重に積まれません)。`;
47
+ }
48
+
49
+ /**
50
+ * 同じ操作ID・同じbodyで最大2回送る(codex2レビュー High 1)。
51
+ *
52
+ * 2回目を送るのは**結果が不明な失敗**のときだけ。1回目がD1へ届いていた場合は、
53
+ * APIが同じ操作IDを見て200で既存のイベントを返す(`NOT EXISTS` のガード)ので、
54
+ * 2件目は積まれない。
55
+ */
56
+ async function postNote(api, assetId, init, operationId) {
57
+ const requestPath = `/api/infra/assets/${encodeURIComponent(assetId)}/notes`;
58
+ try {
59
+ return await api(requestPath, init);
60
+ } catch (error) {
61
+ if (!isUnknownOutcome(error)) throw withGuidance(error, operationGuidance(operationId, false));
62
+ try {
63
+ return await api(requestPath, init);
64
+ } catch (retryError) {
65
+ throw withGuidance(retryError, operationGuidance(operationId, isUnknownOutcome(retryError)));
66
+ }
67
+ }
68
+ }
69
+
70
+ /**
71
+ * 経緯を1件足す(記録188 §3.5)。
72
+ *
73
+ * `hub task note` と違って**分割しない**。看板のnoteは同じタスクに何件積んでも読めるが、
74
+ * 台帳の経緯は「1件=1つの事実」で読むもので、機械的に切ると意味の単位が壊れる。
75
+ * 長すぎるものは人が要点へ縮める(原文の保管は `legacy_note`=便Dの口の仕事)。
76
+ *
77
+ * 操作IDは**1操作につき1回だけ**作り、再試行でも使い回す。同じidの再送はAPIが200で
78
+ * 既存イベントを返すので、通信が切れて結果が分からないときも二重に積まれない。
79
+ * 人が後から送り直すときは `--operation-id <ULID>`(失敗時のメッセージに出る)。
80
+ */
81
+ export const infraNoteCommand = {
82
+ name: "note",
83
+ options: { allowed: ["note", "note-file", "operation-id"] },
84
+ async run({ options, positional, flags, dependencies, api }) {
85
+ const reference = requireSinglePositional(positional, "資産名またはid");
86
+ if (flags.note && flags["note-file"]) throw usageError("--note と --note-file は同時に指定できません");
87
+ const source = flags["note-file"]
88
+ ? await dependencies.readFileImpl(options["note-file"], "utf8")
89
+ : options.note;
90
+ const text = String(source ?? "").trim();
91
+ if (text === "") throw usageError("--note か --note-file を指定してください");
92
+ if (text.length > INFRA_NOTE_LIMIT) {
93
+ throw usageError(
94
+ `経緯は${INFRA_NOTE_LIMIT}文字までです(${text.length}文字)。`
95
+ + "分割せずに要点へ縮めてください(旧メモの原文保管はCLIでは行いません)",
96
+ );
97
+ }
98
+
99
+ // 操作IDは**送信の外で1回だけ**決める。POSTの直前で作ると、応答を失った1回目と
100
+ // 2回目が別のidになり、届いていた場合に2件積まれる(codex2レビュー High 1)
101
+ const operationId = flags["operation-id"] ? operationIdFrom(options["operation-id"]) : newUlid();
102
+
103
+ const id = await resolveInfraAssetId(reference, api);
104
+ const payload = await postNote(api, id, {
105
+ method: "POST",
106
+ body: JSON.stringify({ id: operationId, text }),
107
+ }, operationId);
108
+ if (options.json) {
109
+ dependencies.stdout.write(`${JSON.stringify(payload, null, 2)}\n`);
110
+ return;
111
+ }
112
+ dependencies.stdout.write(`経緯を1件追加しました(${payload?.event?.id ?? "-"})。\n`);
113
+ },
114
+ };
@@ -30,7 +30,22 @@ const OPTION_FIELDS = {
30
30
  "hosted-on-basis": "hosted_on_basis",
31
31
  };
32
32
 
33
- const ALLOWED_OPTIONS = [...Object.keys(OPTION_FIELDS), "append-note", "hosted-on"];
33
+ const ALLOWED_OPTIONS = [...Object.keys(OPTION_FIELDS), "append-note", "hosted-on", "reason"];
34
+
35
+ /**
36
+ * `--append-note` は次の版で外す(記録188 §3.5)。既存noteの末尾へ「 / 」で足す作りは、
37
+ * 「現在の補足」と「経緯」を1つの列に混ぜる——経緯は `hub infra note` が別の表へ積む。
38
+ * 打つたびに出す(1回だけの告知だと、使い続けている人には届かない)。
39
+ */
40
+ const APPEND_NOTE_DEPRECATION =
41
+ "警告: --append-note は次の版で外します。経緯は hub infra note <name|id> --note <text> へ。\n";
42
+
43
+ /**
44
+ * 理由は**読んだ状態に依存する**ので、409で自動的に取り直して再送しない(所見 Medium 2)。
45
+ * 「A を B にした理由」を書いたあとに他人が A を C へ変えていたら、その理由はもう合わない。
46
+ */
47
+ const REASON_CONFLICT_GUIDANCE =
48
+ "理由つきの更新は自動で再送しません。最新の内容を確認し、理由を書き直してからもう一度実行してください";
34
49
  const NULLABLE_VALUE_OPTIONS = new Set([
35
50
  "dns-host",
36
51
  "mail-host",
@@ -47,12 +62,13 @@ function amountValue(value) {
47
62
  return parsed;
48
63
  }
49
64
 
50
- function bodyFor(current, prepared, appendNote) {
65
+ function bodyFor(current, prepared, appendNote, reason) {
51
66
  const body = { version: requireInfraVersion(current), ...prepared };
52
67
  if (appendNote !== null) {
53
68
  const note = typeof current.note === "string" ? current.note.trim() : "";
54
69
  body.note = note === "" ? appendNote : `${note} / ${appendNote}`;
55
70
  }
71
+ if (reason !== null) body.reason = reason;
56
72
  return body;
57
73
  }
58
74
 
@@ -67,6 +83,16 @@ export const infraUpdateCommand = {
67
83
  if (flags.note && flags["append-note"]) {
68
84
  throw usageError("--note と --append-note は同時に指定できません");
69
85
  }
86
+ // 理由だけの更新はAPIが400にする(経緯は `hub infra note`)。往復する前にここで案内する
87
+ let reason = null;
88
+ if (flags.reason) {
89
+ if (!ALLOWED_OPTIONS.some((name) => name !== "reason" && flags[name])) {
90
+ throw usageError("--reason は値の変更と一緒に指定してください(経緯だけを足すときは hub infra note)");
91
+ }
92
+ reason = options.reason.trim();
93
+ if (reason === "") throw usageError("--reason に空文字は指定できません");
94
+ }
95
+ if (flags["append-note"]) dependencies.stderr.write(APPEND_NOTE_DEPRECATION);
70
96
 
71
97
  let currentPayload = await getInfraAsset(reference, api);
72
98
  let current = currentPayload.item;
@@ -107,12 +133,15 @@ export const infraUpdateCommand = {
107
133
  try {
108
134
  const payload = await api(`/api/infra/assets/${encodeURIComponent(current.id)}`, {
109
135
  method: "PATCH",
110
- body: JSON.stringify(bodyFor(current, prepared, appendNote)),
136
+ body: JSON.stringify(bodyFor(current, prepared, appendNote, reason)),
111
137
  });
112
138
  printInfraResult(payload, options.json, dependencies.stdout, "update");
113
139
  return;
114
140
  } catch (error) {
115
141
  if (!isConflictError(error)) throw error;
142
+ // 理由つきは取り直して送り直さない。**書いた理由が古い前提のままになる**ので、
143
+ // 人が最新を読んで理由を書き直す(所見 Medium 2・記録188 §3.5)
144
+ if (reason !== null) throw withGuidance(error, REASON_CONFLICT_GUIDANCE);
116
145
  if (attempt === 1) {
117
146
  throw withGuidance(error, "再取得後にも競合しました。最新の内容を確認してから、もう一度実行してください");
118
147
  }
package/lib/args.mjs CHANGED
@@ -118,7 +118,10 @@ export const INFRA_USAGE = `使い方:
118
118
  [--account <text>] [--account-ref <text>]
119
119
  [--note <text>] [--append-note <text>]
120
120
  [--hosted-on <server name|id>|none]
121
- [--hosted-on-basis <value>|none] [--json]
121
+ [--hosted-on-basis <value>|none]
122
+ [--reason <text>] [--json]
123
+ hub infra note <name|id> (--note <text> | --note-file <path>)
124
+ [--operation-id <ULID>] [--json]
122
125
  hub infra link <name|id> --project <slug> [--project <slug>]... [--json]
123
126
  hub infra unlink <name|id> [--project <slug>]... [--json]
124
127
 
@@ -126,7 +129,17 @@ export const INFRA_USAGE = `使い方:
126
129
  list の --status はカンマ区切りで複数指定できる。
127
130
  show / update / link / unlink の資産は名前の完全一致またはidで指定する。
128
131
  update は指定した項目だけを更新する。version競合時は最新を取り直して1回だけ再試行する。
129
- --append-note は既存noteの末尾へ「 / 」で追記する。
132
+ --reason は更新の理由(500文字まで)。値の変更と一緒に指定する(理由だけの更新はできない)。
133
+ **--reason を含む更新は409で自動再送しない**(理由は読んだ状態に依存するため、
134
+ 最新を確認して書き直す)。
135
+ --append-note は既存noteの末尾へ「 / 」で追記する。**次の版で外す**(経緯は note へ)。
136
+ note は台帳の経緯を1件足す(2,000文字まで・分割しない)。noteは資産のversionを進めない
137
+ ので、シートを開いたまま追記しても、その後の保存が競合しない。
138
+ --note-file はファイルの中身をそのまま1件として送る(超過分は切らずにエラーにする)。
139
+ note の操作IDは1操作につき1つ。応答の前に通信が切れた失敗だけ、**同じIDで1回だけ**
140
+ 送り直す(届いていればAPIが既存のイベントを返すので二重に積まれない)。
141
+ 失敗すると「操作ID: …」を出すので、後から送り直すときは --operation-id <ULID> に
142
+ その値を渡す(素で打ち直すと別の操作になり、届いていた場合は2件積まれる)。
130
143
  server資産には dns-host / mail-host / migration-plan / hosted-on / hosted-on-basis /
131
144
  prod-platform / prod-ref を指定できない。
132
145
  link は現在の紐付けへ追加し、unlink は指定分を外す。unlink の --project 省略は全解除。
@@ -139,7 +152,7 @@ export const USAGE = `使い方:
139
152
  hub task help
140
153
  hub project <verb> ... プロジェクト操作(list / show / create / update / move)
141
154
  hub project help
142
- hub infra <verb> ... インフラ資産操作(list / show / update / link / unlink)
155
+ hub infra <verb> ... インフラ資産操作(list / show / update / note / link / unlink)
143
156
  hub infra help
144
157
  hub --version 入っているCLIの版を出す(hub version も同じ)
145
158
 
package/lib/cli.mjs CHANGED
@@ -4,7 +4,7 @@
4
4
  //
5
5
  // hub task <verb> ... タスク操作
6
6
  // hub project <verb> ... プロジェクト操作(読み取り + 作成・更新)
7
- // hub infra <verb> ... インフラ資産操作(読み取り + 更新・紐付け)
7
+ // hub infra <verb> ... インフラ資産操作(読み取り + 更新・経緯・紐付け)
8
8
  //
9
9
  // 全サブコマンドが `--json` を受け付け、付けると生のAPI応答をそのまま出力する。
10
10
  //
@@ -33,6 +33,7 @@ import {
33
33
  } from "./args.mjs";
34
34
  import { infraLinkCommand } from "../commands/infra-link.mjs";
35
35
  import { infraListCommand } from "../commands/infra-list.mjs";
36
+ import { infraNoteCommand } from "../commands/infra-note.mjs";
36
37
  import { infraShowCommand } from "../commands/infra-show.mjs";
37
38
  import { infraUnlinkCommand } from "../commands/infra-unlink.mjs";
38
39
  import { infraUpdateCommand } from "../commands/infra-update.mjs";
@@ -104,6 +105,7 @@ const COMMAND_GROUPS = {
104
105
  infraListCommand,
105
106
  infraShowCommand,
106
107
  infraUpdateCommand,
108
+ infraNoteCommand,
107
109
  infraLinkCommand,
108
110
  infraUnlinkCommand,
109
111
  ]),
package/lib/format.mjs CHANGED
@@ -284,10 +284,49 @@ export function printInfraResult(payload, json, stdout, kind) {
284
284
  const lines = infraAssetLines(payload?.item);
285
285
  if (kind === "show" && Array.isArray(payload?.events) && payload.events.length > 0) {
286
286
  lines.push("", "events:");
287
- for (const event of payload.events.slice(0, 5)) {
288
- const note = event?.note ? ` — ${displayValue(event.note)}` : "";
289
- lines.push(`- ${displayValue(event?.at)} ${displayValue(event?.kind)} (${displayValue(event?.actor)})${note}`);
287
+ // **5件で切らない**(記録188 §3.5)。経緯が読みものになった以上、直近5件では
288
+ // 「いつ何があったか」が追えない。1件ずつは120文字で切るので行数だけが増える
289
+ for (const event of payload.events) {
290
+ const summary = infraEventSummary(event);
291
+ const body = summary === "" ? "" : ` — ${summary}`;
292
+ // `edited` の `note` は更新の理由。`noted` / `legacy_note` の `note` は本文なので、
293
+ // そちらは `body` に出ている(二重に出さない)
294
+ const reason = event?.kind === "edited" && event.note ? `(理由: ${shortened(event.note)})` : "";
295
+ lines.push(`- ${displayValue(event?.at)} ${displayValue(event?.kind)} (${displayValue(event?.actor)})${body}${reason}`);
290
296
  }
291
297
  }
292
298
  stdout.write(`${lines.join("\n")}\n`);
293
299
  }
300
+
301
+ /** 経緯1行の本文の上限。`legacy_note` は5,000文字まで在りうるので、行の密度を守って切る。 */
302
+ const INFRA_EVENT_BODY_LIMIT = 120;
303
+
304
+ function shortened(text, limit = INFRA_EVENT_BODY_LIMIT) {
305
+ const body = String(text ?? "").replace(/\s+/g, " ").trim();
306
+ return body.length > limit ? `${body.slice(0, limit)}…` : body;
307
+ }
308
+
309
+ /**
310
+ * 経緯1件の要約(記録188 §3.5)。
311
+ *
312
+ * - `noted` / `legacy_note` … `note` 列が本文なのでそのまま(120文字で切る)
313
+ * - `edited` … `meta.fields` を `status: to_cancel → active` の形で1行に
314
+ * (`note` は理由なので呼び出し側が末尾へ回す)
315
+ * - `created` / `synced` … 出す本文が無い(`meta` は作成時の全列なので行に収まらない)
316
+ */
317
+ function infraEventSummary(event) {
318
+ if (event?.kind === "noted" || event?.kind === "legacy_note") return shortened(event.note);
319
+ if (event?.kind !== "edited") return "";
320
+ const meta = event.meta;
321
+ if (meta === null || typeof meta !== "object" || Array.isArray(meta)) return "";
322
+ const fields = Array.isArray(meta.fields) ? meta.fields.filter((field) => typeof field === "string") : [];
323
+ const omitted = new Set(Array.isArray(meta.omitted) ? meta.omitted : []);
324
+ const from = meta.from ?? {};
325
+ const to = meta.to ?? {};
326
+ return fields
327
+ .map((field) => (omitted.has(field)
328
+ // `note` は監査metaへ値を積まない列なので、変わったことだけを出す
329
+ ? `${field}: 変更あり`
330
+ : `${field}: ${formatEventValue(from[field])} → ${formatEventValue(to[field])}`))
331
+ .join(" / ");
332
+ }
package/lib/ulid.mjs ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * ULIDの採番(Crockford Base32の26文字)。CLIが**操作ID**を作るのに使う。
3
+ *
4
+ * Worker・SPA は `@hub/shared/ulid` を使うが、**CLIはそこをimportしない**——
5
+ * `@tamai/hub-cli` は依存ゼロのnpmパッケージで、repo内の workspace パッケージを
6
+ * 抱えると配布物が壊れる(AGENTS.md「CLIの実体はここだけ」)。採番の挙動は共有側と同じ
7
+ * (ミリ秒10文字+乱数16文字)にしてあるが、**CLIが作るのは自分が送るIDだけ**なので、
8
+ * 共有側の実装が変わっても突き合わせる必要は無い。
9
+ *
10
+ * 用途は「同じ操作を二重に積まないための識別子」で、並び順には使わない
11
+ * (同一ミリ秒内では辞書順が生成順にならない。共有側と同じ制約)。
12
+ */
13
+ const ULID_ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
14
+
15
+ export function newUlid(date = new Date()) {
16
+ let timestamp = date.getTime();
17
+ let prefix = "";
18
+ for (let index = 0; index < 10; index += 1) {
19
+ prefix = ULID_ALPHABET[timestamp % 32] + prefix;
20
+ timestamp = Math.floor(timestamp / 32);
21
+ }
22
+ const random = crypto.getRandomValues(new Uint8Array(16));
23
+ let suffix = "";
24
+ for (const value of random) suffix += ULID_ALPHABET[value & 31];
25
+ return prefix + suffix;
26
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamai/hub-cli",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "CLI for the hub personal dashboard: D1-backed task board and project registry.",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",